Line data Source code
1 : //! A C-Rust shim: defines implementation of C walproposer API, assuming wp
2 : //! callback_data stores Box to some Rust implementation.
3 :
4 : #![allow(dead_code)]
5 :
6 : use std::ffi::{CStr, CString};
7 :
8 : use crate::bindings::{
9 : NeonWALReadResult, PGAsyncReadResult, PGAsyncWriteResult, Safekeeper, Size, StringInfoData,
10 : TimestampTz, WalProposer, WalProposerConnStatusType, WalProposerConnectPollStatusType,
11 : WalProposerExecStatusType, WalproposerShmemState, XLogRecPtr, uint32, walproposer_api,
12 : };
13 : use crate::walproposer::{ApiImpl, StreamingCallback, WaitResult};
14 :
15 1074 : extern "C" fn get_shmem_state(wp: *mut WalProposer) -> *mut WalproposerShmemState {
16 1074 : unsafe {
17 1074 : let callback_data = (*(*wp).config).callback_data;
18 1074 : let api = callback_data as *mut Box<dyn ApiImpl>;
19 1074 : (*api).get_shmem_state()
20 1074 : }
21 1074 : }
22 :
23 167 : extern "C-unwind" fn start_streaming(wp: *mut WalProposer, startpos: XLogRecPtr) {
24 167 : unsafe {
25 167 : let callback_data = (*(*wp).config).callback_data;
26 167 : let api = callback_data as *mut Box<dyn ApiImpl>;
27 167 : let callback = StreamingCallback::new(wp);
28 167 : (*api).start_streaming(startpos, &callback);
29 167 : }
30 167 : }
31 :
32 102 : extern "C" fn get_flush_rec_ptr(wp: *mut WalProposer) -> XLogRecPtr {
33 102 : unsafe {
34 102 : let callback_data = (*(*wp).config).callback_data;
35 102 : let api = callback_data as *mut Box<dyn ApiImpl>;
36 102 : (*api).get_flush_rec_ptr()
37 102 : }
38 102 : }
39 :
40 910 : extern "C" fn update_donor(wp: *mut WalProposer, donor: *mut Safekeeper, donor_lsn: XLogRecPtr) {
41 910 : unsafe {
42 910 : let callback_data = (*(*wp).config).callback_data;
43 910 : let api = callback_data as *mut Box<dyn ApiImpl>;
44 910 : (*api).update_donor(&mut (*donor), donor_lsn)
45 910 : }
46 910 : }
47 :
48 307603 : extern "C" fn get_current_timestamp(wp: *mut WalProposer) -> TimestampTz {
49 307603 : unsafe {
50 307603 : let callback_data = (*(*wp).config).callback_data;
51 307603 : let api = callback_data as *mut Box<dyn ApiImpl>;
52 307603 : (*api).get_current_timestamp()
53 307603 : }
54 307603 : }
55 :
56 10105 : extern "C" fn conn_error_message(sk: *mut Safekeeper) -> *mut ::std::os::raw::c_char {
57 10105 : unsafe {
58 10105 : let callback_data = (*(*(*sk).wp).config).callback_data;
59 10105 : let api = callback_data as *mut Box<dyn ApiImpl>;
60 10105 : let msg = (*api).conn_error_message(&mut (*sk));
61 10105 : let msg = CString::new(msg).unwrap();
62 10105 : // TODO: fix leaking error message
63 10105 : msg.into_raw()
64 10105 : }
65 10105 : }
66 :
67 32599 : extern "C" fn conn_status(sk: *mut Safekeeper) -> WalProposerConnStatusType {
68 32599 : unsafe {
69 32599 : let callback_data = (*(*(*sk).wp).config).callback_data;
70 32599 : let api = callback_data as *mut Box<dyn ApiImpl>;
71 32599 : (*api).conn_status(&mut (*sk))
72 32599 : }
73 32599 : }
74 :
75 32599 : extern "C" fn conn_connect_start(sk: *mut Safekeeper) {
76 32599 : unsafe {
77 32599 : let callback_data = (*(*(*sk).wp).config).callback_data;
78 32599 : let api = callback_data as *mut Box<dyn ApiImpl>;
79 32599 : (*api).conn_connect_start(&mut (*sk))
80 32599 : }
81 32599 : }
82 :
83 29363 : extern "C" fn conn_connect_poll(sk: *mut Safekeeper) -> WalProposerConnectPollStatusType {
84 29363 : unsafe {
85 29363 : let callback_data = (*(*(*sk).wp).config).callback_data;
86 29363 : let api = callback_data as *mut Box<dyn ApiImpl>;
87 29363 : (*api).conn_connect_poll(&mut (*sk))
88 29363 : }
89 29363 : }
90 :
91 29363 : extern "C" fn conn_send_query(sk: *mut Safekeeper, query: *mut ::std::os::raw::c_char) -> bool {
92 29363 : let query = unsafe { CStr::from_ptr(query) };
93 29363 : let query = query.to_str().unwrap();
94 29363 :
95 29363 : unsafe {
96 29363 : let callback_data = (*(*(*sk).wp).config).callback_data;
97 29363 : let api = callback_data as *mut Box<dyn ApiImpl>;
98 29363 : (*api).conn_send_query(&mut (*sk), query)
99 29363 : }
100 29363 : }
101 :
102 29363 : extern "C" fn conn_get_query_result(sk: *mut Safekeeper) -> WalProposerExecStatusType {
103 29363 : unsafe {
104 29363 : let callback_data = (*(*(*sk).wp).config).callback_data;
105 29363 : let api = callback_data as *mut Box<dyn ApiImpl>;
106 29363 : (*api).conn_get_query_result(&mut (*sk))
107 29363 : }
108 29363 : }
109 :
110 0 : extern "C" fn conn_flush(sk: *mut Safekeeper) -> ::std::os::raw::c_int {
111 0 : unsafe {
112 0 : let callback_data = (*(*(*sk).wp).config).callback_data;
113 0 : let api = callback_data as *mut Box<dyn ApiImpl>;
114 0 : (*api).conn_flush(&mut (*sk))
115 0 : }
116 0 : }
117 :
118 10369 : extern "C" fn conn_finish(sk: *mut Safekeeper) {
119 10369 : unsafe {
120 10369 : let callback_data = (*(*(*sk).wp).config).callback_data;
121 10369 : let api = callback_data as *mut Box<dyn ApiImpl>;
122 10369 : (*api).conn_finish(&mut (*sk))
123 10369 : }
124 10369 : }
125 :
126 17299 : extern "C" fn conn_async_read(
127 17299 : sk: *mut Safekeeper,
128 17299 : buf: *mut *mut ::std::os::raw::c_char,
129 17299 : amount: *mut ::std::os::raw::c_int,
130 17299 : ) -> PGAsyncReadResult {
131 17299 : unsafe {
132 17299 : let callback_data = (*(*(*sk).wp).config).callback_data;
133 17299 : let api = callback_data as *mut Box<dyn ApiImpl>;
134 17299 :
135 17299 : // This function has guarantee that returned buf will be valid until
136 17299 : // the next call. So we can store a Vec in each Safekeeper and reuse
137 17299 : // it on the next call.
138 17299 : let mut inbuf = take_vec_u8(&mut (*sk).inbuf).unwrap_or_default();
139 17299 : inbuf.clear();
140 17299 :
141 17299 : let result = (*api).conn_async_read(&mut (*sk), &mut inbuf);
142 17299 :
143 17299 : // Put a Vec back to sk->inbuf and return data ptr.
144 17299 : *amount = inbuf.len() as i32;
145 17299 : *buf = store_vec_u8(&mut (*sk).inbuf, inbuf);
146 17299 :
147 17299 : result
148 17299 : }
149 17299 : }
150 :
151 4810 : extern "C" fn conn_async_write(
152 4810 : sk: *mut Safekeeper,
153 4810 : buf: *const ::std::os::raw::c_void,
154 4810 : size: usize,
155 4810 : ) -> PGAsyncWriteResult {
156 4810 : unsafe {
157 4810 : let buf = std::slice::from_raw_parts(buf as *const u8, size);
158 4810 : let callback_data = (*(*(*sk).wp).config).callback_data;
159 4810 : let api = callback_data as *mut Box<dyn ApiImpl>;
160 4810 : (*api).conn_async_write(&mut (*sk), buf)
161 4810 : }
162 4810 : }
163 :
164 32965 : extern "C" fn conn_blocking_write(
165 32965 : sk: *mut Safekeeper,
166 32965 : buf: *const ::std::os::raw::c_void,
167 32965 : size: usize,
168 32965 : ) -> bool {
169 32965 : unsafe {
170 32965 : let buf = std::slice::from_raw_parts(buf as *const u8, size);
171 32965 : let callback_data = (*(*(*sk).wp).config).callback_data;
172 32965 : let api = callback_data as *mut Box<dyn ApiImpl>;
173 32965 : (*api).conn_blocking_write(&mut (*sk), buf)
174 32965 : }
175 32965 : }
176 :
177 532 : extern "C-unwind" fn recovery_download(wp: *mut WalProposer, sk: *mut Safekeeper) -> bool {
178 532 : unsafe {
179 532 : let callback_data = (*(*(*sk).wp).config).callback_data;
180 532 : let api = callback_data as *mut Box<dyn ApiImpl>;
181 532 :
182 532 : // currently `recovery_download` is always called right after election
183 532 : (*api).after_election(&mut (*wp));
184 532 :
185 532 : (*api).recovery_download(&mut (*wp), &mut (*sk))
186 532 : }
187 532 : }
188 :
189 924 : extern "C" fn wal_reader_allocate(sk: *mut Safekeeper) {
190 924 : unsafe {
191 924 : let callback_data = (*(*(*sk).wp).config).callback_data;
192 924 : let api = callback_data as *mut Box<dyn ApiImpl>;
193 924 : (*api).wal_reader_allocate(&mut (*sk));
194 924 : }
195 924 : }
196 :
197 : #[allow(clippy::unnecessary_cast)]
198 914 : extern "C" fn wal_read(
199 914 : sk: *mut Safekeeper,
200 914 : buf: *mut ::std::os::raw::c_char,
201 914 : startptr: XLogRecPtr,
202 914 : count: Size,
203 914 : _errmsg: *mut *mut ::std::os::raw::c_char,
204 914 : ) -> NeonWALReadResult {
205 914 : unsafe {
206 914 : let buf = std::slice::from_raw_parts_mut(buf as *mut u8, count);
207 914 : let callback_data = (*(*(*sk).wp).config).callback_data;
208 914 : let api = callback_data as *mut Box<dyn ApiImpl>;
209 914 : // TODO: errmsg is not forwarded
210 914 : (*api).wal_read(&mut (*sk), buf, startptr)
211 914 : }
212 914 : }
213 :
214 16372 : extern "C" fn wal_reader_events(sk: *mut Safekeeper) -> uint32 {
215 16372 : unsafe {
216 16372 : let callback_data = (*(*(*sk).wp).config).callback_data;
217 16372 : let api = callback_data as *mut Box<dyn ApiImpl>;
218 16372 : (*api).wal_reader_events(&mut (*sk))
219 16372 : }
220 16372 : }
221 :
222 8891 : extern "C" fn init_event_set(wp: *mut WalProposer) {
223 8891 : unsafe {
224 8891 : let callback_data = (*(*wp).config).callback_data;
225 8891 : let api = callback_data as *mut Box<dyn ApiImpl>;
226 8891 : (*api).init_event_set(&mut (*wp));
227 8891 : }
228 8891 : }
229 :
230 64168 : extern "C" fn update_event_set(sk: *mut Safekeeper, events: uint32) {
231 64168 : unsafe {
232 64168 : let callback_data = (*(*(*sk).wp).config).callback_data;
233 64168 : let api = callback_data as *mut Box<dyn ApiImpl>;
234 64168 : (*api).update_event_set(&mut (*sk), events);
235 64168 : }
236 64168 : }
237 :
238 5953 : extern "C" fn active_state_update_event_set(sk: *mut Safekeeper) {
239 5953 : unsafe {
240 5953 : let callback_data = (*(*(*sk).wp).config).callback_data;
241 5953 : let api = callback_data as *mut Box<dyn ApiImpl>;
242 5953 : (*api).active_state_update_event_set(&mut (*sk));
243 5953 : }
244 5953 : }
245 :
246 58726 : extern "C" fn add_safekeeper_event_set(sk: *mut Safekeeper, events: uint32) {
247 58726 : unsafe {
248 58726 : let callback_data = (*(*(*sk).wp).config).callback_data;
249 58726 : let api = callback_data as *mut Box<dyn ApiImpl>;
250 58726 : (*api).add_safekeeper_event_set(&mut (*sk), events);
251 58726 : }
252 58726 : }
253 :
254 36496 : extern "C" fn rm_safekeeper_event_set(sk: *mut Safekeeper) {
255 36496 : unsafe {
256 36496 : let callback_data = (*(*(*sk).wp).config).callback_data;
257 36496 : let api = callback_data as *mut Box<dyn ApiImpl>;
258 36496 : (*api).rm_safekeeper_event_set(&mut (*sk));
259 36496 : }
260 36496 : }
261 :
262 85717 : extern "C-unwind" fn wait_event_set(
263 85717 : wp: *mut WalProposer,
264 85717 : timeout: ::std::os::raw::c_long,
265 85717 : event_sk: *mut *mut Safekeeper,
266 85717 : events: *mut uint32,
267 85717 : ) -> ::std::os::raw::c_int {
268 85717 : unsafe {
269 85717 : let callback_data = (*(*wp).config).callback_data;
270 85717 : let api = callback_data as *mut Box<dyn ApiImpl>;
271 85717 : let result = (*api).wait_event_set(&mut (*wp), timeout);
272 85717 : match result {
273 : WaitResult::Latch => {
274 8940 : *event_sk = std::ptr::null_mut();
275 8940 : *events = crate::bindings::WL_LATCH_SET;
276 8940 : 1
277 : }
278 : WaitResult::Timeout => {
279 2665 : *event_sk = std::ptr::null_mut();
280 2665 : // WaitEventSetWait returns 0 for timeout.
281 2665 : *events = 0;
282 2665 : 0
283 : }
284 74112 : WaitResult::Network(sk, event_mask) => {
285 74112 : *event_sk = sk;
286 74112 : *events = event_mask;
287 74112 : 1
288 : }
289 : }
290 : }
291 85717 : }
292 :
293 0 : extern "C" fn strong_random(
294 0 : wp: *mut WalProposer,
295 0 : buf: *mut ::std::os::raw::c_void,
296 0 : len: usize,
297 0 : ) -> bool {
298 0 : unsafe {
299 0 : let buf = std::slice::from_raw_parts_mut(buf as *mut u8, len);
300 0 : let callback_data = (*(*wp).config).callback_data;
301 0 : let api = callback_data as *mut Box<dyn ApiImpl>;
302 0 : (*api).strong_random(buf)
303 0 : }
304 0 : }
305 :
306 240 : extern "C" fn get_redo_start_lsn(wp: *mut WalProposer) -> XLogRecPtr {
307 240 : unsafe {
308 240 : let callback_data = (*(*wp).config).callback_data;
309 240 : let api = callback_data as *mut Box<dyn ApiImpl>;
310 240 : (*api).get_redo_start_lsn()
311 240 : }
312 240 : }
313 :
314 361 : extern "C-unwind" fn finish_sync_safekeepers(wp: *mut WalProposer, lsn: XLogRecPtr) {
315 361 : unsafe {
316 361 : let callback_data = (*(*wp).config).callback_data;
317 361 : let api = callback_data as *mut Box<dyn ApiImpl>;
318 361 : (*api).finish_sync_safekeepers(lsn)
319 361 : }
320 361 : }
321 :
322 2232 : extern "C" fn process_safekeeper_feedback(wp: *mut WalProposer, sk: *mut Safekeeper) {
323 2232 : unsafe {
324 2232 : let callback_data = (*(*wp).config).callback_data;
325 2232 : let api = callback_data as *mut Box<dyn ApiImpl>;
326 2232 : (*api).process_safekeeper_feedback(&mut (*wp), &mut (*sk));
327 2232 : }
328 2232 : }
329 :
330 146427 : extern "C-unwind" fn log_internal(
331 146427 : wp: *mut WalProposer,
332 146427 : level: ::std::os::raw::c_int,
333 146427 : line: *const ::std::os::raw::c_char,
334 146427 : ) {
335 146427 : unsafe {
336 146427 : let callback_data = (*(*wp).config).callback_data;
337 146427 : let api = callback_data as *mut Box<dyn ApiImpl>;
338 146427 : let line = CStr::from_ptr(line);
339 146427 : let line = line.to_str().unwrap();
340 146427 : (*api).log_internal(&mut (*wp), Level::from(level as u32), line)
341 146427 : }
342 146427 : }
343 :
344 : #[derive(Debug, PartialEq)]
345 : pub enum Level {
346 : Debug5,
347 : Debug4,
348 : Debug3,
349 : Debug2,
350 : Debug1,
351 : Log,
352 : Info,
353 : Notice,
354 : Warning,
355 : Error,
356 : Fatal,
357 : Panic,
358 : WPEvent,
359 : }
360 :
361 : impl Level {
362 146427 : pub fn from(elevel: u32) -> Level {
363 : use crate::bindings::*;
364 :
365 146427 : match elevel {
366 3886 : DEBUG5 => Level::Debug5,
367 0 : DEBUG4 => Level::Debug4,
368 0 : DEBUG3 => Level::Debug3,
369 2232 : DEBUG2 => Level::Debug2,
370 0 : DEBUG1 => Level::Debug1,
371 128483 : LOG => Level::Log,
372 0 : INFO => Level::Info,
373 0 : NOTICE => Level::Notice,
374 11755 : WARNING => Level::Warning,
375 0 : ERROR => Level::Error,
376 66 : FATAL => Level::Fatal,
377 5 : PANIC => Level::Panic,
378 0 : WPEVENT => Level::WPEvent,
379 0 : _ => panic!("unknown log level {}", elevel),
380 : }
381 146427 : }
382 : }
383 :
384 8891 : pub(crate) fn create_api() -> walproposer_api {
385 8891 : walproposer_api {
386 8891 : get_shmem_state: Some(get_shmem_state),
387 8891 : start_streaming: Some(start_streaming),
388 8891 : get_flush_rec_ptr: Some(get_flush_rec_ptr),
389 8891 : update_donor: Some(update_donor),
390 8891 : get_current_timestamp: Some(get_current_timestamp),
391 8891 : conn_error_message: Some(conn_error_message),
392 8891 : conn_status: Some(conn_status),
393 8891 : conn_connect_start: Some(conn_connect_start),
394 8891 : conn_connect_poll: Some(conn_connect_poll),
395 8891 : conn_send_query: Some(conn_send_query),
396 8891 : conn_get_query_result: Some(conn_get_query_result),
397 8891 : conn_flush: Some(conn_flush),
398 8891 : conn_finish: Some(conn_finish),
399 8891 : conn_async_read: Some(conn_async_read),
400 8891 : conn_async_write: Some(conn_async_write),
401 8891 : conn_blocking_write: Some(conn_blocking_write),
402 8891 : recovery_download: Some(recovery_download),
403 8891 : wal_reader_allocate: Some(wal_reader_allocate),
404 8891 : wal_read: Some(wal_read),
405 8891 : wal_reader_events: Some(wal_reader_events),
406 8891 : init_event_set: Some(init_event_set),
407 8891 : update_event_set: Some(update_event_set),
408 8891 : active_state_update_event_set: Some(active_state_update_event_set),
409 8891 : add_safekeeper_event_set: Some(add_safekeeper_event_set),
410 8891 : rm_safekeeper_event_set: Some(rm_safekeeper_event_set),
411 8891 : wait_event_set: Some(wait_event_set),
412 8891 : strong_random: Some(strong_random),
413 8891 : get_redo_start_lsn: Some(get_redo_start_lsn),
414 8891 : finish_sync_safekeepers: Some(finish_sync_safekeepers),
415 8891 : process_safekeeper_feedback: Some(process_safekeeper_feedback),
416 8891 : log_internal: Some(log_internal),
417 8891 : }
418 8891 : }
419 :
420 8891 : pub fn empty_shmem() -> crate::bindings::WalproposerShmemState {
421 8891 : let empty_feedback = crate::bindings::PageserverFeedback {
422 8891 : present: false,
423 8891 : currentClusterSize: 0,
424 8891 : last_received_lsn: 0,
425 8891 : disk_consistent_lsn: 0,
426 8891 : remote_consistent_lsn: 0,
427 8891 : replytime: 0,
428 8891 : shard_number: 0,
429 8891 : };
430 8891 :
431 8891 : crate::bindings::WalproposerShmemState {
432 8891 : propEpochStartLsn: crate::bindings::pg_atomic_uint64 { value: 0 },
433 8891 : donor_name: [0; 64],
434 8891 : donor_conninfo: [0; 1024],
435 8891 : donor_lsn: 0,
436 8891 : mutex: 0,
437 8891 : mineLastElectedTerm: crate::bindings::pg_atomic_uint64 { value: 0 },
438 8891 : backpressureThrottlingTime: crate::bindings::pg_atomic_uint64 { value: 0 },
439 8891 : currentClusterSize: crate::bindings::pg_atomic_uint64 { value: 0 },
440 8891 : shard_ps_feedback: [empty_feedback; 128],
441 8891 : num_shards: 0,
442 8891 : replica_promote: false,
443 8891 : min_ps_feedback: empty_feedback,
444 8891 : }
445 8891 : }
446 :
447 : impl std::fmt::Display for Level {
448 1618 : fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
449 1618 : write!(f, "{:?}", self)
450 1618 : }
451 : }
452 :
453 : /// Take ownership of `Vec<u8>` from StringInfoData.
454 : #[allow(clippy::unnecessary_cast)]
455 43964 : pub(crate) fn take_vec_u8(pg: &mut StringInfoData) -> Option<Vec<u8>> {
456 43964 : if pg.data.is_null() {
457 26670 : return None;
458 17294 : }
459 17294 :
460 17294 : let ptr = pg.data as *mut u8;
461 17294 : let length = pg.len as usize;
462 17294 : let capacity = pg.maxlen as usize;
463 17294 :
464 17294 : pg.data = std::ptr::null_mut();
465 17294 : pg.len = 0;
466 17294 : pg.maxlen = 0;
467 17294 :
468 17294 : unsafe { Some(Vec::from_raw_parts(ptr, length, capacity)) }
469 43964 : }
470 :
471 : /// Store `Vec<u8>` in StringInfoData.
472 17299 : fn store_vec_u8(pg: &mut StringInfoData, vec: Vec<u8>) -> *mut ::std::os::raw::c_char {
473 17299 : let ptr = vec.as_ptr() as *mut ::std::os::raw::c_char;
474 17299 : let length = vec.len();
475 17299 : let capacity = vec.capacity();
476 17299 :
477 17299 : assert!(pg.data.is_null());
478 :
479 17299 : pg.data = ptr;
480 17299 : pg.len = length as i32;
481 17299 : pg.maxlen = capacity as i32;
482 17299 :
483 17299 : std::mem::forget(vec);
484 17299 :
485 17299 : ptr
486 17299 : }
|