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