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