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