Skip to main content

google_cloud_bigtable_admin_v2/
wait_for_consistency.rs

1// Copyright 2026 Google LLC
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//     https://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//! Helpers to await data consistency for a [CheckConsistency] request.
16
17use crate::builder::bigtable_table_admin::CheckConsistency;
18use google_cloud_gax::polling_state::PollingState;
19use google_cloud_gax::retry_policy::NeverRetry;
20use google_cloud_gax::retry_result::RetryResult;
21
22impl CheckConsistency {
23    /// Polls [check_consistency][crate::client::BigtableTableAdmin::check_consistency]
24    /// until the table is consistent, or until the polling policy is exhausted.
25    ///
26    /// # Example
27    /// ```
28    /// # use google_cloud_bigtable_admin_v2::client::BigtableTableAdmin;
29    /// # async fn sample(
30    /// #     client: &BigtableTableAdmin,
31    /// #     table_name: &str,
32    /// #     token: &str,
33    /// # ) -> google_cloud_bigtable_admin_v2::Result<()> {
34    /// client
35    ///     .check_consistency()
36    ///     .set_name(table_name)
37    ///     .set_consistency_token(token)
38    ///     .until_consistent()
39    ///     .await?;
40    /// # Ok(())
41    /// # }
42    /// ```
43    pub async fn until_consistent(mut self) -> crate::Result<()> {
44        let error_policy = self
45            .0
46            .options
47            .polling_error_policy()
48            .clone()
49            .unwrap_or_else(|| self.0.stub.get_polling_error_policy(&self.0.options));
50        let backoff_policy = self
51            .0
52            .options
53            .polling_backoff_policy()
54            .clone()
55            .unwrap_or_else(|| self.0.stub.get_polling_backoff_policy(&self.0.options));
56        self.0.options.set_retry_policy(NeverRetry);
57
58        let mut state = PollingState::default();
59        loop {
60            state.attempt_count += 1;
61            match self.clone().send().await {
62                Ok(r) if r.consistent => return Ok(()),
63                Ok(_) => error_policy.on_in_progress(&state, &self.0.request.name)?,
64                Err(e) => match error_policy.on_error(&state, e) {
65                    RetryResult::Continue(_) => {}
66                    RetryResult::Exhausted(e) | RetryResult::Permanent(e) => return Err(e),
67                },
68            }
69            tokio::time::sleep(backoff_policy.wait_period(&state)).await;
70        }
71    }
72}