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