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