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