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