mirror of
https://github.com/lancedb/lancedb.git
synced 2026-11-29 22:13:58 +00:00
feat: add cleanup jobs and previews across SDKs (#5107)
LanceDB SDK users connected to Enterprise had no explicit API to submit durable cleanup or preview its work; Python's existing cleanup helper remained a warning-only no-op. This PR adds the SDK support for [ENT-2687](https://linear.app/lancedb/issue/ENT-2687) through public SQL and Job APIs. It requires a compatible Enterprise release supporting `VACUUM`, JSON preview and result format version 1. Existing cleanup/optimize helpers retain their behavior; the new APIs reject Embedded and historical-version handles. This was accomplished with the following changes: - Add `cleanup_async()` / `cleanupAsync()` and `explain_cleanup()` / `explainCleanup()` in Python sync/async, TypeScript and Rust. - Preserve the handle's namespace/branch and resolve retention as invocation → table/branch TTL → deployment default. Omission inherits, zero is explicit, and malformed options fail rather than being discarded. - Require one nonempty durable SQL receipt, preserving diagnostics without REST fallback, query-ID substitution or automatic resubmission after ambiguous acceptance. - Add versioned `CleanupPlan` estimates and `CleanupResult` measurements. Preview creates no cleanup job or reservation; results preserve known zero, unknown retry/recovery totals and nonfatal failed deletes, excluding auxiliary sweeps. - Retain typed results on reopening and reuse shared status/progress/events/wait/cancel APIs. Timeout leaves work running; cancellation cannot undo deletion or prove storage I/O stopped. - Document Ownership/parent Usage and required coordination at every retention value: disable/drain automatic cleanup and prevent competing cleanup, tag creation/update, branch creation and restore across all clients until completion. The server does not enforce exclusion; concurrent restore can leave the current table unreadable. - Update Python's legacy no-op warning and the maintenance, Python, Rust and generated TypeScript references. | Interface | Submit with seven-day retention | | --- | --- | | SQL | `VACUUM events WITH (min_age_seconds = 604800);` | | CLI | `lancedb admin table cleanup --table events --min-age-seconds 604800 --wait` | | Python | `job = table.cleanup_async(min_age_seconds=604800)` (use `await` with AsyncTable) | | TypeScript | `const job = await table.cleanupAsync({ minAgeSeconds: 604800 });` | | Rust | `let job = table.cleanup_async(CleanupOptions::new().min_age_seconds(604800)).await?;` | ### Testing Added submission/preview, validation, typed-result and reopening tests, and updated the legacy no-op warning assertion. Focused Python cleanup/Job and compaction/prewarm regressions passed, alongside **34 TypeScript** and **46 Rust** tests. **31 end-to-end scenarios** passed against a real Enterprise server, covering CLI/SQL/REST/SDK submission, retention precedence, restart/reopening, timeouts, cancellation and branch/namespace isolation. Local native SDK validation used a separate `libsais-rs 0.2.0` lockfile because the registry could not resolve `0.2.4`; this PR retains main's dependency pins and lockfile.
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-FileCopyrightText: Copyright The LanceDB Authors
|
||||
|
||||
/** Per-run retention; omission inherits selected table/branch then deployment policy. */
|
||||
export interface CleanupOptions {
|
||||
/** Minimum old-version age in seconds. Zero is explicit, not omitted. */
|
||||
minAgeSeconds?: number;
|
||||
}
|
||||
|
||||
/** Version-deletion outcome. Best-effort auxiliary sweeps are excluded. */
|
||||
export interface CleanupResult {
|
||||
formatVersion: number;
|
||||
minAgeSeconds: number;
|
||||
/** False after uncertain retry/recovery; both counts are then null. */
|
||||
statisticsComplete: boolean;
|
||||
oldVersions: number | null;
|
||||
bytesRemoved: number | null;
|
||||
/** Failed object deletions, if measured. Successful removals are retained. */
|
||||
failedDeletes: number | null;
|
||||
}
|
||||
|
||||
/** Read-only deletion estimates; no job or deletion snapshot is reserved. */
|
||||
export interface CleanupPlan {
|
||||
formatVersion: number;
|
||||
sourceVersion: number;
|
||||
minAgeSeconds: number;
|
||||
cutoffMillis: number;
|
||||
versionsToRemove: number;
|
||||
bytesToRemove: number;
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export function decodeCleanupPlan(value: unknown): CleanupPlan {
|
||||
if (typeof value !== "object" || value === null)
|
||||
throw new Error("invalid cleanup preview");
|
||||
const plan = value as Record<string, unknown>;
|
||||
if (
|
||||
plan.format_version !== 1 ||
|
||||
[
|
||||
"source_version",
|
||||
"min_age_seconds",
|
||||
"cutoff_millis",
|
||||
"versions_to_remove",
|
||||
"bytes_to_remove",
|
||||
].some(
|
||||
(key) =>
|
||||
!Number.isSafeInteger(plan[key]) ||
|
||||
(key !== "cutoff_millis" && (plan[key] as number) < 0),
|
||||
)
|
||||
) {
|
||||
throw new Error("unsupported or invalid cleanup preview");
|
||||
}
|
||||
return {
|
||||
formatVersion: 1,
|
||||
sourceVersion: plan.source_version as number,
|
||||
minAgeSeconds: plan.min_age_seconds as number,
|
||||
cutoffMillis: plan.cutoff_millis as number,
|
||||
versionsToRemove: plan.versions_to_remove as number,
|
||||
bytesToRemove: plan.bytes_to_remove as number,
|
||||
};
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export function validateCleanupOptions(options?: CleanupOptions): void {
|
||||
if (
|
||||
options !== undefined &&
|
||||
(typeof options !== "object" || options === null || Array.isArray(options))
|
||||
) {
|
||||
throw new Error("cleanup options must be an object");
|
||||
}
|
||||
for (const key of Object.keys(options ?? {})) {
|
||||
if (key !== "minAgeSeconds")
|
||||
throw new Error(`Unknown cleanup option: ${key}`);
|
||||
}
|
||||
const age = options?.minAgeSeconds;
|
||||
if (age !== undefined && (!Number.isSafeInteger(age) || age < 0)) {
|
||||
throw new Error("minAgeSeconds must be a nonnegative safe integer");
|
||||
}
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export function decodeCleanupResult(value: unknown): CleanupResult {
|
||||
if (typeof value !== "object" || value === null)
|
||||
throw new Error("invalid cleanup result");
|
||||
const result = value as Record<string, unknown>;
|
||||
const counts = [result.old_versions ?? null, result.bytes_removed ?? null];
|
||||
const failed = result.failed_deletes ?? null;
|
||||
if (
|
||||
failed !== null &&
|
||||
(!Number.isSafeInteger(failed) ||
|
||||
(failed as number) < 0 ||
|
||||
result.statistics_complete !== true)
|
||||
) {
|
||||
throw new Error("invalid cleanup failed-deletion measurement");
|
||||
}
|
||||
if (
|
||||
result.format_version !== 1 ||
|
||||
typeof result.statistics_complete !== "boolean" ||
|
||||
!Number.isSafeInteger(result.min_age_seconds) ||
|
||||
(result.min_age_seconds as number) < 0 ||
|
||||
counts.some(
|
||||
(count) =>
|
||||
count !== null &&
|
||||
(!Number.isSafeInteger(count) || (count as number) < 0),
|
||||
) ||
|
||||
(result.statistics_complete
|
||||
? counts.some((count) => count === null)
|
||||
: counts.some((count) => count !== null))
|
||||
) {
|
||||
throw new Error("unsupported or inconsistent cleanup result");
|
||||
}
|
||||
return {
|
||||
formatVersion: 1,
|
||||
minAgeSeconds: result.min_age_seconds as number,
|
||||
statisticsComplete: result.statistics_complete,
|
||||
oldVersions: counts[0] as number | null,
|
||||
bytesRemoved: counts[1] as number | null,
|
||||
failedDeletes: (result.failed_deletes as number | null) ?? null,
|
||||
};
|
||||
}
|
||||
@@ -668,6 +668,7 @@ export {
|
||||
export { Listing, ListingOptions } from "./listing";
|
||||
|
||||
export type { CompactionResult } from "./compaction";
|
||||
export type { CleanupOptions, CleanupPlan, CleanupResult } from "./cleanup";
|
||||
|
||||
export type {
|
||||
PrewarmDataPlan,
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// SPDX-FileCopyrightText: Copyright The LanceDB Authors
|
||||
|
||||
import { Table as ArrowTable, tableFromIPC } from "apache-arrow";
|
||||
import { decodeCleanupResult } from "./cleanup";
|
||||
import { decodeCompactionResult } from "./compaction";
|
||||
import { JobFailureInfo, Job as NativeJob } from "./native";
|
||||
import { decodePrewarmDataResult } from "./prewarm";
|
||||
@@ -123,7 +124,9 @@ export class Job<T = void> {
|
||||
? (decodePrewarmDataResult as (value: unknown) => T)
|
||||
: inner.jobType === "compact"
|
||||
? (decodeCompactionResult as (value: unknown) => T)
|
||||
: undefined);
|
||||
: inner.jobType === "cleanup"
|
||||
? (decodeCleanupResult as (value: unknown) => T)
|
||||
: undefined);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,6 +1,14 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-FileCopyrightText: Copyright The LanceDB Authors
|
||||
|
||||
import {
|
||||
CleanupOptions,
|
||||
CleanupPlan,
|
||||
CleanupResult,
|
||||
decodeCleanupPlan,
|
||||
decodeCleanupResult,
|
||||
validateCleanupOptions,
|
||||
} from "./cleanup";
|
||||
import { CompactionResult, decodeCompactionResult } from "./compaction";
|
||||
import {
|
||||
PrewarmDataPlan,
|
||||
@@ -1341,6 +1349,28 @@ export abstract class Table {
|
||||
* out state and the read_consistency_interval, if any, will apply.
|
||||
*/
|
||||
abstract restore(): Promise<void>;
|
||||
/**
|
||||
* Submit durable remote cleanup through SQL VACUUM and return after acceptance.
|
||||
* Requires table Ownership and parent Usage. Retention inherits from the table
|
||||
* then deployment when omitted. Local/historical handles are unsupported.
|
||||
* Before submission, disable/drain automatic cleanup and prevent other cleanup,
|
||||
* tag/branch publication and restore until completion, including direct clients.
|
||||
* Coordination is not enforced by the server. Cancellation cannot undo deletion
|
||||
* or prove storage I/O stopped. Preview is available without deletion.
|
||||
* Wait timeout leaves work running; save the ID and reopen with db.openJob().
|
||||
* @example
|
||||
* const job = await table.cleanupAsync({ minAgeSeconds: 604800 });
|
||||
* const result = await job.wait({ timeoutMs: 600000 });
|
||||
*/
|
||||
async cleanupAsync(_options?: CleanupOptions): Promise<Job<CleanupResult>> {
|
||||
throw new Error("durable cleanup jobs are only supported on remote tables");
|
||||
}
|
||||
|
||||
/** Preview version deletion without creating a job. Execution re-evaluates current state. */
|
||||
async explainCleanup(_options?: CleanupOptions): Promise<CleanupPlan> {
|
||||
throw new Error("cleanup previews require LanceDB Enterprise");
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit remote compaction and return its durable job after acceptance.
|
||||
*
|
||||
@@ -2091,6 +2121,21 @@ export class LocalTable extends Table {
|
||||
);
|
||||
}
|
||||
|
||||
async cleanupAsync(options?: CleanupOptions): Promise<Job<CleanupResult>> {
|
||||
validateCleanupOptions(options);
|
||||
return new Job(
|
||||
await this.inner.cleanupAsync(options?.minAgeSeconds),
|
||||
decodeCleanupResult,
|
||||
);
|
||||
}
|
||||
|
||||
async explainCleanup(options?: CleanupOptions): Promise<CleanupPlan> {
|
||||
validateCleanupOptions(options);
|
||||
return decodeCleanupPlan(
|
||||
await this.inner.explainCleanup(options?.minAgeSeconds),
|
||||
);
|
||||
}
|
||||
|
||||
async optimize(options?: Partial<OptimizeOptions>): Promise<OptimizeStats> {
|
||||
validateCompactionOptions(options?.compactionOptions);
|
||||
return await this.inner.optimize(
|
||||
|
||||
Reference in New Issue
Block a user