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