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