[safekeeper] Serialize LSN in the term_history according to the spec (#2896)

Use string format in the timeline status HTTP API reponse.
This commit is contained in:
Alexey Kondratov
2022-11-24 17:19:01 +01:00
committed by GitHub
parent 0b0cb77da4
commit e6db4b63eb

View File

@@ -12,7 +12,6 @@ use tokio::task::JoinError;
use crate::safekeeper::ServerInfo;
use crate::safekeeper::Term;
use crate::safekeeper::TermHistory;
use crate::timelines_global_map::TimelineDeleteForceResult;
use crate::GlobalTimelines;
@@ -62,12 +61,21 @@ where
s.serialize_str(&format!("{}", z))
}
/// Same as TermSwitchEntry, but serializes LSN using display serializer
/// in Postgres format, i.e. 0/FFFFFFFF. Used only for the API response.
#[derive(Debug, Serialize)]
struct TermSwitchApiEntry {
pub term: Term,
#[serde(serialize_with = "display_serialize")]
pub lsn: Lsn,
}
/// Augment AcceptorState with epoch for convenience
#[derive(Debug, Serialize)]
struct AcceptorStateStatus {
term: Term,
epoch: Term,
term_history: TermHistory,
term_history: Vec<TermSwitchApiEntry>,
}
/// Info about timeline on safekeeper ready for reporting.
@@ -112,10 +120,21 @@ async fn timeline_status_handler(request: Request<Body>) -> Result<Response<Body
let (inmem, state) = tli.get_state();
let flush_lsn = tli.get_flush_lsn();
let epoch = state.acceptor_state.get_epoch(flush_lsn);
let term_history = state
.acceptor_state
.term_history
.0
.into_iter()
.map(|ts| TermSwitchApiEntry {
term: ts.term,
lsn: ts.lsn,
})
.collect();
let acc_state = AcceptorStateStatus {
term: state.acceptor_state.term,
epoch: state.acceptor_state.get_epoch(flush_lsn),
term_history: state.acceptor_state.term_history,
epoch,
term_history,
};
// Note: we report in memory values which can be lost.
@@ -277,3 +296,25 @@ pub fn make_router(
record_safekeeper_info,
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_term_switch_entry_api_serialize() {
let state = AcceptorStateStatus {
term: 1,
epoch: 1,
term_history: vec![TermSwitchApiEntry {
term: 1,
lsn: Lsn(0x16FFDDDD),
}],
};
let json = serde_json::to_string(&state).unwrap();
assert_eq!(
json,
"{\"term\":1,\"epoch\":1,\"term_history\":[{\"term\":1,\"lsn\":\"0/16FFDDDD\"}]}"
);
}
}