LCOV - code coverage report
Current view: top level - libs/walproposer/src - api_bindings.rs (source / functions) Coverage Total Hit
Test: 322b88762cba8ea666f63cda880cccab6936bf37.info Lines: 95.9 % 369 354
Test Date: 2024-02-29 11:57:12 Functions: 97.3 % 37 36

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

Generated by: LCOV version 2.1-beta