Patch safekeeper control file on HTTP request (#6455)

Closes #6397
This commit is contained in:
Arthur Petukhovsky
2024-01-29 18:20:57 +00:00
committed by GitHub
parent ec8dcc2231
commit 2ff1a5cecd
6 changed files with 191 additions and 1 deletions

View File

@@ -28,7 +28,7 @@ use crate::safekeeper::Term;
use crate::safekeeper::{ServerInfo, TermLsn};
use crate::send_wal::WalSenderState;
use crate::timeline::PeerInfo;
use crate::{copy_timeline, debug_dump, pull_timeline};
use crate::{copy_timeline, debug_dump, patch_control_file, pull_timeline};
use crate::timelines_global_map::TimelineDeleteForceResult;
use crate::GlobalTimelines;
@@ -465,6 +465,26 @@ async fn dump_debug_handler(mut request: Request<Body>) -> Result<Response<Body>
Ok(response)
}
async fn patch_control_file_handler(
mut request: Request<Body>,
) -> Result<Response<Body>, ApiError> {
check_permission(&request, None)?;
let ttid = TenantTimelineId::new(
parse_request_param(&request, "tenant_id")?,
parse_request_param(&request, "timeline_id")?,
);
let tli = GlobalTimelines::get(ttid).map_err(ApiError::from)?;
let patch_request: patch_control_file::Request = json_request(&mut request).await?;
let response = patch_control_file::handle_request(tli, patch_request)
.await
.map_err(ApiError::InternalServerError)?;
json_response(StatusCode::OK, response)
}
/// Safekeeper http router.
pub fn make_router(conf: SafeKeeperConf) -> RouterBuilder<hyper::Body, ApiError> {
let mut router = endpoint::make_router();
@@ -526,6 +546,10 @@ pub fn make_router(conf: SafeKeeperConf) -> RouterBuilder<hyper::Body, ApiError>
"/v1/tenant/:tenant_id/timeline/:source_timeline_id/copy",
|r| request_span(r, timeline_copy_handler),
)
.patch(
"/v1/tenant/:tenant_id/timeline/:timeline_id/control_file",
|r| request_span(r, patch_control_file_handler),
)
// for tests
.post("/v1/record_safekeeper_info/:tenant_id/:timeline_id", |r| {
request_span(r, record_safekeeper_info)

View File

@@ -22,6 +22,7 @@ pub mod handler;
pub mod http;
pub mod json_ctrl;
pub mod metrics;
pub mod patch_control_file;
pub mod pull_timeline;
pub mod receive_wal;
pub mod recovery;

View File

@@ -0,0 +1,85 @@
use std::sync::Arc;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tracing::info;
use crate::{state::TimelinePersistentState, timeline::Timeline};
#[derive(Deserialize, Debug, Clone)]
pub struct Request {
/// JSON object with fields to update
pub updates: serde_json::Value,
/// List of fields to apply
pub apply_fields: Vec<String>,
}
#[derive(Serialize)]
pub struct Response {
pub old_control_file: TimelinePersistentState,
pub new_control_file: TimelinePersistentState,
}
/// Patch control file with given request. Will update the persistent state using
/// fields from the request and persist the new state on disk.
pub async fn handle_request(tli: Arc<Timeline>, request: Request) -> anyhow::Result<Response> {
let response = tli
.map_control_file(|state| {
let old_control_file = state.clone();
let new_control_file = state_apply_diff(&old_control_file, &request)?;
info!(
"patching control file, old: {:?}, new: {:?}, patch: {:?}",
old_control_file, new_control_file, request
);
*state = new_control_file.clone();
Ok(Response {
old_control_file,
new_control_file,
})
})
.await?;
Ok(response)
}
fn state_apply_diff(
state: &TimelinePersistentState,
request: &Request,
) -> anyhow::Result<TimelinePersistentState> {
let mut json_value = serde_json::to_value(state)?;
if let Value::Object(a) = &mut json_value {
if let Value::Object(b) = &request.updates {
json_apply_diff(a, b, &request.apply_fields)?;
} else {
anyhow::bail!("request.updates is not a json object")
}
} else {
anyhow::bail!("TimelinePersistentState is not a json object")
}
let new_state: TimelinePersistentState = serde_json::from_value(json_value)?;
Ok(new_state)
}
fn json_apply_diff(
object: &mut serde_json::Map<String, Value>,
updates: &serde_json::Map<String, Value>,
apply_keys: &Vec<String>,
) -> anyhow::Result<()> {
for key in apply_keys {
if let Some(new_value) = updates.get(key) {
if let Some(existing_value) = object.get_mut(key) {
*existing_value = new_value.clone();
} else {
anyhow::bail!("key not found in original object: {}", key);
}
} else {
anyhow::bail!("key not found in request.updates: {}", key);
}
}
Ok(())
}

View File

@@ -901,6 +901,20 @@ impl Timeline {
file_open,
}
}
/// Apply a function to the control file state and persist it.
pub async fn map_control_file<T>(
&self,
f: impl FnOnce(&mut TimelinePersistentState) -> Result<T>,
) -> Result<T> {
let mut state = self.write_shared_state().await;
let mut persistent_state = state.sk.state.start_change();
// If f returns error, we abort the change and don't persist anything.
let res = f(&mut persistent_state)?;
// If persisting fails, we abort the change and return error.
state.sk.state.finish_change(&persistent_state).await?;
Ok(res)
}
}
/// Deletes directory and it's contents. Returns false if directory does not exist.