Skip to content

Commit

Permalink
feat: Switch to randomized UAID for health check (#670)
Browse files Browse the repository at this point in the history
Closes: SYNC-4203
  • Loading branch information
jrconlin authored Mar 29, 2024
1 parent de752ec commit 0609378
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
30 changes: 16 additions & 14 deletions autopush-common/src/db/bigtable/bigtable_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use cadence::{CountedExt, StatsdClient};
use futures_util::StreamExt;
use google_cloud_rust_raw::bigtable::admin::v2::bigtable_table_admin::DropRowRangeRequest;
use google_cloud_rust_raw::bigtable::admin::v2::bigtable_table_admin_grpc::BigtableTableAdminClient;
use google_cloud_rust_raw::bigtable::v2::bigtable::{PingAndWarmRequest, ReadRowsRequest};
use google_cloud_rust_raw::bigtable::v2::bigtable::ReadRowsRequest;
use google_cloud_rust_raw::bigtable::v2::bigtable_grpc::BigtableClient;
use google_cloud_rust_raw::bigtable::v2::data::{RowFilter, RowFilter_Chain};
use google_cloud_rust_raw::bigtable::v2::{bigtable, data};
Expand Down Expand Up @@ -762,44 +762,46 @@ impl BigTableClientImpl {
pub struct BigtableDb {
pub(super) conn: BigtableClient,
pub(super) health_metadata: Metadata,
instance_name: String,
table_name: String,
}

impl BigtableDb {
pub fn new(channel: Channel, health_metadata: &Metadata, instance_name: &str) -> Self {
pub fn new(channel: Channel, health_metadata: &Metadata, table_name: &str) -> Self {
Self {
conn: BigtableClient::new(channel),
health_metadata: health_metadata.clone(),
instance_name: instance_name.to_owned(),
table_name: table_name.to_owned(),
}
}
/// Perform a simple connectivity check. This should return no actual results
/// but should verify that the connection is valid. We use this for the
/// Recycle check as well, so it has to be fairly low in the implementation
/// stack.
///
/// "instance_name" is the "projects/{project}/instances/{instance}" portion of
/// the tablename.
///
pub async fn health_check(&mut self, metrics: Arc<StatsdClient>) -> DbResult<bool> {
let req = PingAndWarmRequest {
name: self.instance_name.clone(),
..Default::default()
};
// PingAndWarmResponse does not implement a stream since it does not return data.
// It is recommended that we pick a random key to perform the health check. Selecting
// a single key for all health checks causes a "hot tablet" to arise. The `PingAndWarm`
// is intended to be used prior to large updates and is not recommended for use in
// health checks.
// This health check is to see if the database is present, the response is not important
// other than it does not return an error.
let random_uaid = Uuid::new_v4().simple().to_string();
let mut req = read_row_request(&self.table_name, &random_uaid);
let mut filter = data::RowFilter::default();
filter.set_block_all_filter(true);
req.set_filter(filter);
let _r = retry_policy(RETRY_COUNT)
.retry_if(
|| async {
self.conn
.ping_and_warm_opt(&req, call_opts(self.health_metadata.clone()))
.read_rows_opt(&req, call_opts(self.health_metadata.clone()))
},
retryable_error(metrics.clone()),
)
.await
.map_err(|e| DbError::General(format!("BigTable connectivity error: {:?}", e)))?;

// Since this should return no rows (with the row key set to a value that shouldn't exist)
// the first component of the tuple should be None.
debug!("🉑 health check");
Ok(true)
}
Expand Down
7 changes: 2 additions & 5 deletions autopush-common/src/db/bigtable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,9 @@ impl BigTableDbSettings {
.map_err(BigTableError::GRPC)
}

// Health may require a different metadata declaration.
pub fn health_metadata(&self) -> Result<Metadata, BigTableError> {
MetadataBuilder::with_prefix(&self.table_name)
.routing_param("name", &self.get_instance_name()?)
.route_to_leader(self.route_to_leader)
.build()
.map_err(BigTableError::GRPC)
self.metadata()
}

pub fn admin_metadata(&self) -> Result<Metadata, BigTableError> {
Expand Down
2 changes: 1 addition & 1 deletion autopush-common/src/db/bigtable/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ impl Manager for BigtableClientManager {
let entry = BigtableDb::new(
self.get_channel()?,
&self.settings.health_metadata()?,
&self.settings.get_instance_name()?,
&self.settings.table_name,
);
debug!("🏊 Bigtable connection acquired");
Ok(entry)
Expand Down

0 comments on commit 0609378

Please sign in to comment.