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