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