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