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