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