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