From bf8acd6b59f31261068062477b34185f76868c16 Mon Sep 17 00:00:00 2001 From: zwang28 <84491488@qq.com> Date: Mon, 18 Sep 2023 14:13:30 +0800 Subject: [PATCH] fix(backup): ensure correct delta log order --- .../backup_restore/meta_snapshot_builder.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/meta/src/backup_restore/meta_snapshot_builder.rs b/src/meta/src/backup_restore/meta_snapshot_builder.rs index 0df9966607b93..e54c9f443f125 100644 --- a/src/meta/src/backup_restore/meta_snapshot_builder.rs +++ b/src/meta/src/backup_restore/meta_snapshot_builder.rs @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::collections::HashMap; +use std::collections::{BTreeMap, HashMap}; use std::future::Future; use anyhow::anyhow; @@ -65,18 +65,22 @@ impl MetaSnapshotBuilder { // hummock_version and version_stats is guaranteed to exist in a initialized cluster. let hummock_version = { let mut redo_state = hummock_version; - let hummock_version_deltas = - HummockVersionDelta::list_at_snapshot::(&meta_store_snapshot).await?; - for version_delta in &hummock_version_deltas { + let hummock_version_deltas: BTreeMap<_, _> = + HummockVersionDelta::list_at_snapshot::(&meta_store_snapshot) + .await? + .into_iter() + .map(|d| (d.id, d)) + .collect(); + for version_delta in hummock_version_deltas.values() { if version_delta.prev_id == redo_state.id { redo_state.apply_version_delta(version_delta); } } - if let Some(log) = hummock_version_deltas.iter().next_back() { - if log.id != redo_state.id { + if let Some((max_log_id, _)) = hummock_version_deltas.last_key_value() { + if *max_log_id != redo_state.id { return Err(BackupError::Other(anyhow::anyhow!(format!( "inconsistent hummock version: expected {}, actual {}", - log.id, redo_state.id + max_log_id, redo_state.id )))); } }