Skip to content

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

fix: separate LANGFUSE_CLICKHOUSE_DELETED_MASK_CLEANER_CLUSTER_MODE_ENABLED toggle for delete mask cleaner (#14050)

Valery Meleshkin7a9efdf78899
diff --git a/.env.prod.example b/.env.prod.example
index d7f3675ace..09e74927f6 100644
--- a/.env.prod.example
+++ b/.env.prod.example
@@ -228,6 +228,7 @@ LANGFUSE_ENABLE_BLOB_STORAGE_FILE_LOG=true
# LANGFUSE_CLICKHOUSE_DELETED_MASK_CLEANER_ENABLED=false
# LANGFUSE_CLICKHOUSE_DELETED_MASK_CLEANER_INTERVAL_MS=3600000
# LANGFUSE_CLICKHOUSE_DELETED_MASK_CLEANER_SUBMIT_TIMEOUT_MS=60000
+# LANGFUSE_CLICKHOUSE_DELETED_MASK_CLEANER_CLUSTER_MODE_ENABLED=false
# Ingestion configuration
# LANGFUSE_INGESTION_QUEUE_DELAY_MS=
diff --git a/worker/src/__tests__/deletedMaskCleaner.test.ts b/worker/src/__tests__/deletedMaskCleaner.test.ts
index 50d4f2e247..27a4d427b9 100644
--- a/worker/src/__tests__/deletedMaskCleaner.test.ts
+++ b/worker/src/__tests__/deletedMaskCleaner.test.ts
@@ -26,6 +26,7 @@ import {
DELETED_MASK_CLEANER_WORK_QUERY,
normalizeMutationCounts,
selectCandidateToProcess,
+ shouldUseDeletedMaskCleanerClusterMode,
type MutationCountRow,
type WorkCandidateRow,
} from "../features/deleted-mask-cleaner/helpers";
@@ -162,7 +163,12 @@ async function getActiveMutationCount(
): Promise<number> {
const rows = await queryClickhouse<MutationCountRow>({
query: buildMutationCountQuery(
- env.CLICKHOUSE_CLUSTER_ENABLED === "true",
+ shouldUseDeletedMaskCleanerClusterMode({
+ clusterEnabled: env.CLICKHOUSE_CLUSTER_ENABLED === "true",
+ cleanerClusterModeEnabled:
+ env.LANGFUSE_CLICKHOUSE_DELETED_MASK_CLEANER_CLUSTER_MODE_ENABLED ===
+ "true",
+ }),
env.CLICKHOUSE_CLUSTER_NAME,
),
params: {
@@ -199,7 +205,12 @@ async function applyDeletedMaskIfNeeded(
},
{
database: env.CLICKHOUSE_DB,
- clusterEnabled: env.CLICKHOUSE_CLUSTER_ENABLED === "true",
+ clusterEnabled: shouldUseDeletedMaskCleanerClusterMode({
+ clusterEnabled: env.CLICKHOUSE_CLUSTER_ENABLED === "true",
+ cleanerClusterModeEnabled:
+ env.LANGFUSE_CLICKHOUSE_DELETED_MASK_CLEANER_CLUSTER_MODE_ENABLED ===
+ "true",
+ }),
clusterName: env.CLICKHOUSE_CLUSTER_NAME,
},
),
@@ -268,6 +279,34 @@ describe("DeletedMaskCleaner helpers", () => {
);
});
+ it("uses cleaner cluster mode only when ClickHouse cluster mode and the explicit cleaner flag are enabled", () => {
+ expect(
+ shouldUseDeletedMaskCleanerClusterMode({
+ clusterEnabled: true,
+ cleanerClusterModeEnabled: true,
+ }),
+ ).toBe(true);
+ expect(
+ shouldUseDeletedMaskCleanerClusterMode({
+ clusterEnabled: true,
+ cleanerClusterModeEnabled: false,
+ }),
+ ).toBe(false);
+ expect(
+ shouldUseDeletedMaskCleanerClusterMode({
+ clusterEnabled: false,
+ cleanerClusterModeEnabled: true,
+ }),
+ ).toBe(false);
+
+ expect(buildMutationCountQuery(true, "default")).toContain(
+ "clusterAllReplicas",
+ );
+ expect(buildMutationCountQuery(false, "default")).toContain(
+ "FROM system.mutations",
+ );
+ });
+
it("selects the first candidate without active table mutations", () => {
const candidates: WorkCandidateRow[] = [
workCandidate({
diff --git a/worker/src/env.ts b/worker/src/env.ts
index 10ed34f02d..a69a645d13 100644
--- a/worker/src/env.ts
+++ b/worker/src/env.ts
@@ -417,6 +417,9 @@ const EnvSchema = z.object({
.number()
.positive()
.default(60_000), // Wait up to 1 minute for ALTER submission; mutation can run for hours
+ LANGFUSE_CLICKHOUSE_DELETED_MASK_CLEANER_CLUSTER_MODE_ENABLED: z
+ .enum(["true", "false"])
+ .default("false"), // Use ON CLUSTER and clusterAllReplicas for cleaner operations
// Media Retention Cleaner configuration (S3/PostgreSQL)
LANGFUSE_MEDIA_RETENTION_CLEANER_ITEM_LIMIT: z.coerce
diff --git a/worker/src/features/deleted-mask-cleaner/helpers.ts b/worker/src/features/deleted-mask-cleaner/helpers.ts
index 15a09628d0..bcfdfcf472 100644
--- a/worker/src/features/deleted-mask-cleaner/helpers.ts
+++ b/worker/src/features/deleted-mask-cleaner/helpers.ts
@@ -134,6 +134,16 @@ export function normalizeMutationCounts(
return mutationCounts;
}
+export function shouldUseDeletedMaskCleanerClusterMode({
+ clusterEnabled,
+ cleanerClusterModeEnabled,
+}: {
+ clusterEnabled: boolean;
+ cleanerClusterModeEnabled: boolean;
+}): boolean {
+ return clusterEnabled && cleanerClusterModeEnabled;
+}
+
export function buildApplyDeletedMaskQuery(
candidate: WorkCandidateRow,
config: ClickHouseDdlConfig,
@@ -158,14 +168,14 @@ export function buildApplyDeletedMaskQuery(
}
export function buildMutationCountQuery(
- clusterEnabled: boolean,
+ useClusterAllReplicas: boolean,
clusterName: string,
): string {
- if (clusterEnabled) {
+ if (useClusterAllReplicas) {
assertClickHouseName(clusterName, "cluster");
}
- const mutationSource = clusterEnabled
+ const mutationSource = useClusterAllReplicas
? `clusterAllReplicas(${quoteClickhouseString(clusterName)}, 'system.mutations')`
: "system.mutations";
diff --git a/worker/src/features/deleted-mask-cleaner/index.ts b/worker/src/features/deleted-mask-cleaner/index.ts
index 7eaff07396..b06c4c2752 100644
--- a/worker/src/features/deleted-mask-cleaner/index.ts
+++ b/worker/src/features/deleted-mask-cleaner/index.ts
@@ -15,6 +15,7 @@ import {
isAbortError,
normalizeMutationCounts,
selectCandidateToProcess,
+ shouldUseDeletedMaskCleanerClusterMode,
type MutationCountRow,
type WorkCandidateRow,
} from "./helpers";
@@ -142,7 +143,12 @@ export class DeletedMaskCleaner extends PeriodicExclusiveRunner {
return queryClickhouse<MutationCountRow>({
query: buildMutationCountQuery(
- env.CLICKHOUSE_CLUSTER_ENABLED === "true",
+ shouldUseDeletedMaskCleanerClusterMode({
+ clusterEnabled: env.CLICKHOUSE_CLUSTER_ENABLED === "true",
+ cleanerClusterModeEnabled:
+ env.LANGFUSE_CLICKHOUSE_DELETED_MASK_CLEANER_CLUSTER_MODE_ENABLED ===
+ "true",
+ }),
env.CLICKHOUSE_CLUSTER_NAME,
),
params: {
@@ -159,7 +165,12 @@ export class DeletedMaskCleaner extends PeriodicExclusiveRunner {
private async applyDeletedMask(candidate: WorkCandidateRow): Promise<void> {
const query = buildApplyDeletedMaskQuery(candidate, {
database: env.CLICKHOUSE_DB,
- clusterEnabled: env.CLICKHOUSE_CLUSTER_ENABLED === "true",
+ clusterEnabled: shouldUseDeletedMaskCleanerClusterMode({
+ clusterEnabled: env.CLICKHOUSE_CLUSTER_ENABLED === "true",
+ cleanerClusterModeEnabled:
+ env.LANGFUSE_CLICKHOUSE_DELETED_MASK_CLEANER_CLUSTER_MODE_ENABLED ===
+ "true",
+ }),
clusterName: env.CLICKHOUSE_CLUSTER_NAME,
});