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