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