Skip to content

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

chore: add stateSuffix to observation migration for potential parallelization (#4101)

Steffen Schmitzd6065e8b89c4
diff --git a/packages/shared/prisma/generated/types.ts b/packages/shared/prisma/generated/types.ts
index 458a8056f5..3a1269aa75 100644
--- a/packages/shared/prisma/generated/types.ts
+++ b/packages/shared/prisma/generated/types.ts
@@ -148,7 +148,7 @@ export type BackgroundMigration = {
name: string;
script: string;
args: unknown;
- state: unknown;
+ state: Generated<unknown>;
finished_at: Timestamp | null;
failed_at: Timestamp | null;
failed_reason: string | null;
diff --git a/worker/src/backgroundMigrations/migrateObservationsFromPostgresToClickhouse.ts b/worker/src/backgroundMigrations/migrateObservationsFromPostgresToClickhouse.ts
index 4188ad81a2..0f1b2224e8 100644
--- a/worker/src/backgroundMigrations/migrateObservationsFromPostgresToClickhouse.ts
+++ b/worker/src/backgroundMigrations/migrateObservationsFromPostgresToClickhouse.ts
@@ -36,21 +36,22 @@ export default class MigrateObservationsFromPostgresToClickhouse
);
// @ts-ignore
- const initialMigrationState: { state: { maxDate: string | undefined } } =
+ const initialMigrationState: { state: Record<string, string | undefined> } =
await prisma.backgroundMigration.findUniqueOrThrow({
where: { id: backgroundMigrationId },
select: { state: true },
});
+ const stateSuffix = (args.stateSuffix as string) ?? "";
const maxRowsToProcess = Number(args.maxRowsToProcess ?? Infinity);
const batchSize = Number(args.batchSize ?? 5000);
- const maxDate = initialMigrationState.state?.maxDate
- ? new Date(initialMigrationState.state.maxDate)
+ const maxDate = initialMigrationState.state?.[`maxDate${stateSuffix}`]
+ ? new Date(initialMigrationState.state[`maxDate${stateSuffix}`] as string)
: new Date((args.maxDate as string) ?? new Date());
await prisma.backgroundMigration.update({
where: { id: backgroundMigrationId },
- data: { state: { maxDate } },
+ data: { state: { [`maxDate${stateSuffix}`]: maxDate } },
});
let processedRows = 0;
@@ -62,7 +63,7 @@ export default class MigrateObservationsFromPostgresToClickhouse
const fetchStart = Date.now();
// @ts-ignore
- const migrationState: { state: { maxDate: string } } =
+ const migrationState: { state: Record<string, string> } =
await prisma.backgroundMigration.findUniqueOrThrow({
where: { id: backgroundMigrationId },
select: { state: true },
@@ -74,7 +75,7 @@ export default class MigrateObservationsFromPostgresToClickhouse
SELECT o.id, o.trace_id, o.project_id, o.type, o.parent_observation_id, o.start_time, o.end_time, o.name, o.metadata, o.level, o.status_message, o.version, o.input, o.output, o.unit, o.model, o.internal_model_id, o."modelParameters" as model_parameters, o.prompt_tokens, o.completion_tokens, o.total_tokens, o.completion_start_time, o.prompt_id, p.name as prompt_name, p.version as prompt_version, o.input_cost, o.output_cost, o.total_cost, o.calculated_input_cost, o.calculated_output_cost, o.calculated_total_cost, o.created_at, o.updated_at
FROM observations o
LEFT JOIN prompts p ON o.prompt_id = p.id
- WHERE o.created_at <= ${new Date(migrationState.state.maxDate)}
+ WHERE o.created_at <= ${new Date(migrationState.state[`maxDate${stateSuffix}`])}
ORDER BY o.created_at DESC
LIMIT ${batchSize};
`);
@@ -102,7 +103,9 @@ export default class MigrateObservationsFromPostgresToClickhouse
where: { id: backgroundMigrationId },
data: {
state: {
- maxDate: new Date(observations[observations.length - 1].created_at),
+ [`maxDate${stateSuffix}`]: new Date(
+ observations[observations.length - 1].created_at,
+ ),
},
},
});
@@ -148,6 +151,9 @@ async function main() {
short: "d",
default: new Date().toISOString(),
},
+ // State prefix can be used to start multiple migrations at once.
+ // We add it to the end of the `maxDate` state key which makes the runs unique and restartable.
+ stateSuffix: { type: "string", short: "s", default: "" },
},
});