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