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