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