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