Skip to main content

risingwave_meta/hummock/manager/
utils.rs

1// Copyright 2024 RisingWave Labs
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/// Commit multiple `ValTransaction`s to state store and upon success update the local in-mem state
16/// by the way
17/// After called, the `ValTransaction` will be dropped.
18macro_rules! commit_multi_var {
19    ($meta_store:expr, $($val_txn:expr),*) => {
20        {
21            async {
22                use crate::model::{MetadataModelError, InMemValTransaction, ValTransaction};
23                use sea_orm::TransactionTrait;
24                let mut txn = $meta_store.conn.begin().await.map_err(MetadataModelError::from)?;
25                $(
26                    $val_txn.apply_to_txn(&mut txn).await?;
27                )*
28                txn.commit().await.map_err(MetadataModelError::from)?;
29                $(
30                    $val_txn.commit();
31                )*
32                Result::Ok(())
33            }.await
34        }
35    };
36}
37
38macro_rules! commit_multi_var_with_provided_txn {
39    ($txn:expr, $($val_txn:expr),*) => {
40        {
41            async {
42                use crate::model::{InMemValTransaction, ValTransaction};
43                use crate::model::MetadataModelError;
44                $(
45                    $val_txn.apply_to_txn(&mut $txn).await?;
46                )*
47                $txn.commit().await.map_err(MetadataModelError::from)?;
48                $(
49                    $val_txn.commit();
50                )*
51                Result::Ok(())
52            }.await
53        }
54    };
55}
56
57pub(crate) use commit_multi_var;
58pub(crate) use commit_multi_var_with_provided_txn;
59use risingwave_hummock_sdk::ObjectIdRange;
60
61use crate::hummock::HummockManager;
62use crate::hummock::error::Result;
63use crate::hummock::sequence::next_raw_object_id;
64
65impl HummockManager {
66    #[cfg(test)]
67    pub(super) async fn check_state_consistency(&self) {
68        use crate::hummock::manager::compaction::Compaction;
69        use crate::hummock::manager::context::ContextInfo;
70        use crate::hummock::manager::versioning::Versioning;
71        let mut compaction_guard = self.compaction.write().await;
72        let mut versioning_guard = self.versioning.write().await;
73        let mut context_info_guard = self.context_info.write().await;
74        // We don't check `checkpoint` because it's allowed to update its in memory state without
75        // persisting to object store.
76        let get_state = |compaction_guard: &mut Compaction,
77                         versioning_guard: &mut Versioning,
78                         context_info_guard: &mut ContextInfo| {
79            let compact_statuses_copy = compaction_guard.compaction_statuses.clone();
80            let compact_task_assignment_copy = compaction_guard.compact_task_assignment.clone();
81            let pinned_versions_copy = context_info_guard.pinned_versions.clone();
82            let hummock_version_deltas_copy = versioning_guard.hummock_version_deltas.clone();
83            let version_stats_copy = versioning_guard.version_stats.clone();
84            ((
85                compact_statuses_copy,
86                compact_task_assignment_copy,
87                pinned_versions_copy,
88                hummock_version_deltas_copy,
89                version_stats_copy,
90            ),)
91        };
92        let mem_state = get_state(
93            &mut compaction_guard,
94            &mut versioning_guard,
95            &mut context_info_guard,
96        );
97        self.load_meta_store_state_impl(
98            &mut compaction_guard,
99            &mut versioning_guard,
100            &mut context_info_guard,
101        )
102        .await
103        .expect("Failed to load state from meta store");
104        let loaded_state = get_state(
105            &mut compaction_guard,
106            &mut versioning_guard,
107            &mut context_info_guard,
108        );
109        assert_eq!(
110            mem_state, loaded_state,
111            "hummock in-mem state is inconsistent with meta store state",
112        );
113    }
114
115    pub async fn get_new_object_ids(&self, number: u32) -> Result<ObjectIdRange> {
116        let start_id = next_raw_object_id(&self.env, number).await?;
117        Ok(ObjectIdRange::new(start_id, start_id + number as u64))
118    }
119}