Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix: make delete mask cleaner wait for submitted mutations to appear (#14056)

Valery Meleshkine6903191352e
diff --git a/worker/src/features/deleted-mask-cleaner/helpers.ts b/worker/src/features/deleted-mask-cleaner/helpers.ts
index bcfdfcf472..789b19a39b 100644
--- a/worker/src/features/deleted-mask-cleaner/helpers.ts
+++ b/worker/src/features/deleted-mask-cleaner/helpers.ts
@@ -86,6 +86,19 @@ function quoteClickhouseIdentifier(value: string, label: string): string {
return `\`${value.replace(/\\/g, "\\\\").replace(/`/g, "\\`")}\``;
}
+function buildMutationSource(
+ useClusterAllReplicas: boolean,
+ clusterName: string,
+): string {
+ if (useClusterAllReplicas) {
+ assertClickHouseName(clusterName, "cluster");
+ }
+
+ return useClusterAllReplicas
+ ? `clusterAllReplicas(${quoteClickhouseString(clusterName)}, 'system.mutations')`
+ : "system.mutations";
+}
+
export function isAbortError(error: unknown): boolean {
if (!(error instanceof Error)) {
return false;
@@ -171,20 +184,12 @@ export function buildMutationCountQuery(
useClusterAllReplicas: boolean,
clusterName: string,
): string {
- if (useClusterAllReplicas) {
- assertClickHouseName(clusterName, "cluster");
- }
-
- const mutationSource = useClusterAllReplicas
- ? `clusterAllReplicas(${quoteClickhouseString(clusterName)}, 'system.mutations')`
- : "system.mutations";
-
return `
SELECT
database,
table,
count() AS mutation_count
- FROM ${mutationSource}
+ FROM ${buildMutationSource(useClusterAllReplicas, clusterName)}
WHERE database = {database: String}
AND table IN ({tables: Array(String)})
AND is_done = 0
diff --git a/worker/src/features/deleted-mask-cleaner/index.ts b/worker/src/features/deleted-mask-cleaner/index.ts
index b06c4c2752..00dde6ecd6 100644
--- a/worker/src/features/deleted-mask-cleaner/index.ts
+++ b/worker/src/features/deleted-mask-cleaner/index.ts
@@ -26,6 +26,12 @@ export const DELETED_MASK_CLEANER_LOCK_KEY =
"langfuse:clickhouse-deleted-mask-cleaner";
const METRIC_PREFIX = "langfuse.clickhouse_deleted_mask_cleaner";
+const MUTATION_VISIBILITY_WAIT_TIMEOUT_MS = 30_000;
+const MUTATION_VISIBILITY_POLL_INTERVAL_MS = 500;
+
+function sleep(ms: number): Promise<void> {
+ return new Promise((resolve) => setTimeout(resolve, ms));
+}
/**
* DeletedMaskCleaner physically applies ClickHouse lightweight delete masks for
@@ -143,12 +149,7 @@ export class DeletedMaskCleaner extends PeriodicExclusiveRunner {
return queryClickhouse<MutationCountRow>({
query: buildMutationCountQuery(
- shouldUseDeletedMaskCleanerClusterMode({
- clusterEnabled: env.CLICKHOUSE_CLUSTER_ENABLED === "true",
- cleanerClusterModeEnabled:
- env.LANGFUSE_CLICKHOUSE_DELETED_MASK_CLEANER_CLUSTER_MODE_ENABLED ===
- "true",
- }),
+ this.useCleanerClusterMode(),
env.CLICKHOUSE_CLUSTER_NAME,
),
params: {
@@ -162,15 +163,19 @@ export class DeletedMaskCleaner extends PeriodicExclusiveRunner {
});
}
+ private useCleanerClusterMode(): boolean {
+ return shouldUseDeletedMaskCleanerClusterMode({
+ clusterEnabled: env.CLICKHOUSE_CLUSTER_ENABLED === "true",
+ cleanerClusterModeEnabled:
+ env.LANGFUSE_CLICKHOUSE_DELETED_MASK_CLEANER_CLUSTER_MODE_ENABLED ===
+ "true",
+ });
+ }
+
private async applyDeletedMask(candidate: WorkCandidateRow): Promise<void> {
const query = buildApplyDeletedMaskQuery(candidate, {
database: env.CLICKHOUSE_DB,
- clusterEnabled: shouldUseDeletedMaskCleanerClusterMode({
- clusterEnabled: env.CLICKHOUSE_CLUSTER_ENABLED === "true",
- cleanerClusterModeEnabled:
- env.LANGFUSE_CLICKHOUSE_DELETED_MASK_CLEANER_CLUSTER_MODE_ENABLED ===
- "true",
- }),
+ clusterEnabled: this.useCleanerClusterMode(),
clusterName: env.CLICKHOUSE_CLUSTER_NAME,
});
@@ -226,5 +231,33 @@ export class DeletedMaskCleaner extends PeriodicExclusiveRunner {
} finally {
clearTimeout(timeoutId);
}
+
+ await this.waitForSubmittedMutationVisibility(candidate.table);
+ }
+
+ private async waitForSubmittedMutationVisibility(
+ table: string,
+ ): Promise<void> {
+ const deadline = Date.now() + MUTATION_VISIBILITY_WAIT_TIMEOUT_MS;
+
+ // This is a best-effort lock hold after async DDL submission. Tiny mutations
+ // may complete before system.mutations ever exposes them as unfinished, which
+ // is fine because the lock was still held through the duplicate-submit window.
+ while (Date.now() < deadline) {
+ if ((await this.getActiveMutationCount(table)) > 0) {
+ return;
+ }
+
+ await sleep(MUTATION_VISIBILITY_POLL_INTERVAL_MS);
+ }
+ }
+
+ private async getActiveMutationCount(table: string): Promise<number> {
+ const mutationCounts = normalizeMutationCounts(
+ [table],
+ await this.getMutationCountRows([table]),
+ );
+
+ return mutationCounts.get(table) ?? 0;
}
}