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