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