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