Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 5 additions & 29 deletions clickhouse-admin/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -899,37 +899,13 @@ mod tests {
ErrorStatusCode::SERVICE_UNAVAILABLE
));

// Initialize the database, then check the retention policy again.
//
// We need to wait for the `system.query_log` table to exist, which can
// take a while.
context.init_db(false).await.expect("failed to initialize database");

let policy = dev::poll::wait_for_condition(
|| async {
match context.retention_policy().await {
Ok(pol) => {
if pol.tables.contains_key("query_log") {
Ok(pol)
} else {
Err(dev::poll::CondCheckError::<()>::NotYet)
}
}
Err(_) => Err(dev::poll::CondCheckError::<()>::NotYet),
}
},
&std::time::Duration::from_millis(100),
&std::time::Duration::from_secs(30),
)
.await
.expect("failed to get retention policy");
assert!(!policy.tables.is_empty());

// The query log defaults to 7 days TTL, everything else is 30.
assert!(policy.tables.iter().all(|pol| {
let expected = if pol.table == "query_log" { 7 } else { 30 };
u8::from(pol.days) == expected
}));
let policy = context
.retention_policy()
.await
.expect("failed to get retention policy");
assert!(policy.tables.iter().all(|pol| { u8::from(pol.days) == 30 }));

// Set everything to 3, and ensure we can read it back.
let days = Days::new(3).unwrap();
Expand Down
56 changes: 5 additions & 51 deletions oximeter/db/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1069,8 +1069,6 @@ impl Client {
Some("toDateTime(timestamp)")
} else if table.contains("fields") {
Some("last_updated_at")
} else if table.contains("query_log") {
Some("event_date")
} else {
None
}
Expand Down Expand Up @@ -1098,32 +1096,9 @@ impl Client {
tables.push(format!("{}.{}", crate::DATABASE_NAME, table));
}

// TTL applies to the query log table too, if it exists.
if self.database_has_query_log_table(handle).await? {
tables.push(String::from("system.query_log"));
}
Ok(tables)
}

/// Return true if the database has the `system.query_log` table.
async fn database_has_query_log_table(
&self,
handle: &mut Handle,
) -> Result<bool, Error> {
const SQL: &str = "\
SELECT 1 \
FROM system.tables \
WHERE name = 'query_log' \
LIMIT 1";
let n_rows = self
.execute_with_block(handle, SQL)
.await?
.data
.ok_or_else(|| Error::QueryMissingData { query: SQL.to_string() })?
.n_rows();
Ok(n_rows > 0)
}

/// Return the retention policy for the oximeter database tables.
///
/// An error is returned if the database cannot be reached or the policy
Expand Down Expand Up @@ -1152,7 +1127,7 @@ impl Client {
startsWith(name, 'fields') \
) \
AND position(engine, 'MergeTree') != 0\
) OR (database = 'system' AND name = 'query_log');";
);";
let result = self
.execute_with_block(&mut self.claim_connection().await?, SQL)
.await?;
Expand Down Expand Up @@ -1213,10 +1188,9 @@ impl Client {
ifNull(total_bytes, 0) AS total_bytes, \
ifNull(total_rows, 0) AS total_rows \
FROM system.tables \
WHERE (\
database = '{}' OR \
(database = 'system' AND name = 'query_log')\
) AND has_own_data",
WHERE \
database = '{}'\
AND has_own_data",
crate::DATABASE_NAME,
);
let columns = self
Expand Down Expand Up @@ -1705,26 +1679,15 @@ impl Client {
//
// for the field tables.
//
// Lastly, the TTL line could look like this
//
// ```
// TTL event_date + toIntervalDay(<N>)
// ```
//
// for the system.query_log table.
//
//
// We're picking out the number of days from the function argument as a string.
fn extract_ttl_in_days_from_create_table_query(
row: &str,
) -> Result<Days, Error> {
const NEEDLES: [&str; 3] = [
const NEEDLES: [&str; 2] = [
// For measurement tables
"TTL toDateTime(timestamp) + toIntervalDay(",
// For field tables
"TTL last_updated_at + toIntervalDay(",
// For system.query_log
"TTL event_date + toIntervalDay(",
];

// Find the first needle that matches, and error if none do.
Expand Down Expand Up @@ -5370,15 +5333,6 @@ mod tests {
"some junk TTL last_updated_at + toIntervalDay(7) other stuff"
).unwrap()),
);
assert_eq!(
7u8,
u8::from(
extract_ttl_in_days_from_create_table_query(
"some junk TTL event_date + toIntervalDay(7) other stuff"
)
.unwrap()
),
);

for invalid in [
"some junk TTL toDateTime(timestamp) + toIntervalDay(-1) other stuf",
Expand Down
Loading