Skip to main content

mito2/manifest/
compat.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Reject unpublished prototype task-state records without adding persisted fields.
16//! IgnoredAny skips file metadata without allocating a second manifest tree.
17
18#[cfg(test)]
19mod tests;
20
21use std::collections::HashMap;
22
23use serde::Deserialize;
24use serde::de::IgnoredAny;
25use snafu::{ResultExt, ensure};
26
27use crate::error::{Result, SerdeJsonSnafu, UnsupportedOperationSnafu, Utf8Snafu};
28
29/// Serialized tag of [`crate::manifest::action::RegionMetaAction::Edit`].
30///
31/// The delta guard inspects raw JSON, so serde cannot select the variant for it.
32/// Keep in sync with the enum; `compat::tests` fails if the tag drifts.
33const EDIT_ACTION_TAG: &str = "Edit";
34
35#[derive(Deserialize)]
36struct TaskStateGuard {
37    table_compaction: Option<IgnoredAny>,
38    extensions: Option<IgnoredAny>,
39}
40
41impl TaskStateGuard {
42    fn validate(&self) -> Result<()> {
43        ensure!(self.table_compaction.is_none() && self.extensions.is_none(), UnsupportedOperationSnafu {
44            err_msg: "Legacy compaction task state requires explicit migration; it cannot be discarded".to_string(),
45        });
46        Ok(())
47    }
48}
49
50/// Validates a standalone manifest, such as a staging blob.
51///
52/// Staging manifests are recovery evidence like checkpoints and deltas, so they
53/// must not silently drop prototype task state either.
54pub(crate) fn validate_manifest(bytes: &[u8]) -> Result<()> {
55    let data = std::str::from_utf8(bytes).context(Utf8Snafu)?;
56    let state: TaskStateGuard = serde_json::from_str(data).context(SerdeJsonSnafu)?;
57    state.validate()
58}
59
60pub(crate) fn validate_checkpoint(json: &str) -> Result<()> {
61    #[derive(Deserialize)]
62    struct CheckpointGuard {
63        checkpoint: Option<TaskStateGuard>,
64    }
65    let guard: CheckpointGuard = serde_json::from_str(json).context(SerdeJsonSnafu)?;
66    if let Some(state) = guard.checkpoint {
67        state.validate()?;
68    }
69    Ok(())
70}
71
72pub(crate) fn validate_actions(json: &str) -> Result<()> {
73    #[derive(Deserialize)]
74    struct ActionListGuard {
75        actions: Vec<HashMap<String, TaskStateGuard>>,
76    }
77    let guard: ActionListGuard = serde_json::from_str(json).context(SerdeJsonSnafu)?;
78    for action in guard.actions {
79        if let Some(edit) = action.get(EDIT_ACTION_TAG) {
80            edit.validate()?;
81        }
82    }
83    Ok(())
84}