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