Skip to content

Commit

Permalink
chore: simplify error handling in export() (#13090)
Browse files Browse the repository at this point in the history
  • Loading branch information
drmingdrmer authored Oct 6, 2023
1 parent c89d812 commit 1fb7f54
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 36 deletions.
56 changes: 20 additions & 36 deletions src/meta/service/src/store/store_inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,11 @@ impl StoreInner {
/// Export data that can be used to restore a meta-service node.
#[minitrace::trace]
pub async fn export(&self) -> Result<Vec<String>, io::Error> {
// Convert to io::Error(InvalidData).
fn invalid_data(e: impl std::error::Error + Send + Sync + 'static) -> io::Error {
io::Error::new(ErrorKind::InvalidData, e)
}

// Lock all components so that we have a consistent view.
//
// Hold the snapshot lock to prevent snapshot from being replaced until exporting finished.
Expand All @@ -405,16 +410,12 @@ impl StoreInner {

// Export data header first
{
let header_tree = SledTree::open(&self.db, TREE_HEADER, false)
.map_err(|e| io::Error::new(ErrorKind::InvalidData, e))?;
let header_tree = SledTree::open(&self.db, TREE_HEADER, false).map_err(invalid_data)?;

let header_kvs = header_tree
.export()
.map_err(|e| io::Error::new(ErrorKind::InvalidData, e))?;
let header_kvs = header_tree.export()?;

for kv in header_kvs.iter() {
let line = vec_kv_to_json(TREE_HEADER, kv)
.map_err(|e| io::Error::new(ErrorKind::InvalidData, e))?;
let line = vec_kv_to_json(TREE_HEADER, kv)?;
res.push(line);
}
}
Expand All @@ -425,52 +426,42 @@ impl StoreInner {

let ks = raft_state.inner.key_space::<RaftStateKV>();

let id = ks
.get(&RaftStateKey::Id)
.map_err(|e| io::Error::new(ErrorKind::InvalidData, e))?
.map(NodeId::from);
let id = ks.get(&RaftStateKey::Id)?.map(NodeId::from);

if let Some(id) = id {
let ent_id = RaftStoreEntry::RaftStateKV {
key: RaftStateKey::Id,
value: RaftStateValue::NodeId(id),
};

let s = serde_json::to_string(&(tree_name, ent_id))
.map_err(|e| io::Error::new(ErrorKind::InvalidData, e))?;
let s = serde_json::to_string(&(tree_name, ent_id)).map_err(invalid_data)?;

res.push(s);
}

let vote = ks
.get(&RaftStateKey::HardState)
.map_err(|e| io::Error::new(ErrorKind::InvalidData, e))?
.map(Vote::from);
let vote = ks.get(&RaftStateKey::HardState)?.map(Vote::from);

if let Some(vote) = vote {
let ent_vote = RaftStoreEntry::RaftStateKV {
key: RaftStateKey::HardState,
value: RaftStateValue::HardState(vote),
};

let s = serde_json::to_string(&(tree_name, ent_vote))
.map_err(|e| io::Error::new(ErrorKind::InvalidData, e))?;
let s = serde_json::to_string(&(tree_name, ent_vote)).map_err(invalid_data)?;

res.push(s);
}

let committed = ks
.get(&RaftStateKey::Committed)
.map_err(|e| io::Error::new(ErrorKind::InvalidData, e))?
.get(&RaftStateKey::Committed)?
.and_then(Option::<LogId>::from);

let ent_committed = RaftStoreEntry::RaftStateKV {
key: RaftStateKey::Committed,
value: RaftStateValue::Committed(committed),
};

let s = serde_json::to_string(&(tree_name, ent_committed))
.map_err(|e| io::Error::new(ErrorKind::InvalidData, e))?;
let s = serde_json::to_string(&(tree_name, ent_committed)).map_err(invalid_data)?;

res.push(s);
};
Expand All @@ -479,18 +470,13 @@ impl StoreInner {
{
let tree_name = &log.inner.name;

let log_kvs = log
.inner
.export()
.map_err(|e| io::Error::new(ErrorKind::InvalidData, e))?;
let log_kvs = log.inner.export()?;

for kv in log_kvs.iter() {
let kv_entry = RaftStoreEntry::deserialize(&kv[0], &kv[1])
.map_err(|e| io::Error::new(ErrorKind::InvalidData, e))?;
let kv_entry = RaftStoreEntry::deserialize(&kv[0], &kv[1])?;

let tree_kv = (tree_name, kv_entry);
let line = serde_json::to_string(&tree_kv)
.map_err(|e| io::Error::new(ErrorKind::InvalidData, e))?;
let line = serde_json::to_string(&tree_kv).map_err(invalid_data)?;

res.push(line);
}
Expand All @@ -512,18 +498,16 @@ impl StoreInner {
let f = snapshot_store
.load_snapshot(&meta.snapshot_id)
.await
.map_err(|e| io::Error::new(ErrorKind::InvalidData, e))?;
.map_err(invalid_data)?;
let bf = BufReader::new(f);
let mut lines = AsyncBufReadExt::lines(bf);

while let Some(l) = lines.next_line().await? {
let ent: RaftStoreEntry = serde_json::from_str(&l)
.map_err(|e| io::Error::new(ErrorKind::InvalidData, e))?;
let ent: RaftStoreEntry = serde_json::from_str(&l).map_err(invalid_data)?;

let named_entry = (tree_name, ent);

let l = serde_json::to_string(&named_entry)
.map_err(|e| io::Error::new(ErrorKind::InvalidData, e))?;
let l = serde_json::to_string(&named_entry).map_err(invalid_data)?;

res.push(l);
}
Expand Down
7 changes: 7 additions & 0 deletions src/meta/stoerr/src/meta_storage_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

use std::fmt;
use std::io;

use anyerror::AnyError;
use serde::Deserialize;
Expand Down Expand Up @@ -85,3 +86,9 @@ impl From<UnabortableTransactionError> for MetaStorageError {
}
}
}

impl From<MetaStorageError> for io::Error {
fn from(e: MetaStorageError) -> Self {
io::Error::new(io::ErrorKind::InvalidData, e)
}
}

1 comment on commit 1fb7f54

@vercel
Copy link

@vercel vercel bot commented on 1fb7f54 Oct 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.