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