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