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