mirror of
https://github.com/lancedb/lancedb.git
synced 2026-11-26 12:33:59 +00:00
Merge remote-tracking branch 'origin/main' into xuanwo/add-node-version-parity
# Conflicts: # nodejs/src/table.rs
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-FileCopyrightText: Copyright The LanceDB Authors
|
||||
|
||||
import {
|
||||
type CleanupOptions,
|
||||
decodeCleanupPlan,
|
||||
decodeCleanupResult,
|
||||
validateCleanupOptions,
|
||||
} from "../lancedb/cleanup";
|
||||
|
||||
test.each([null, [], 1, true, "options"])(
|
||||
"cleanup rejects malformed options instead of inheriting retention (%p)",
|
||||
(options) => {
|
||||
expect(() =>
|
||||
validateCleanupOptions(options as unknown as CleanupOptions),
|
||||
).toThrow("cleanup options must be an object");
|
||||
},
|
||||
);
|
||||
|
||||
test("cleanup never silently discards an unknown option", () => {
|
||||
expect(() =>
|
||||
validateCleanupOptions({ force: true } as unknown as CleanupOptions),
|
||||
).toThrow("Unknown cleanup option");
|
||||
});
|
||||
|
||||
test.each([undefined, 0, 604800])(
|
||||
"cleanup retention accepts omission and explicit zero (%s)",
|
||||
(age) => {
|
||||
expect(() => validateCleanupOptions({ minAgeSeconds: age })).not.toThrow();
|
||||
},
|
||||
);
|
||||
|
||||
test.each([
|
||||
-1,
|
||||
1.5,
|
||||
Number.NaN,
|
||||
Number.POSITIVE_INFINITY,
|
||||
Number.MAX_SAFE_INTEGER + 1,
|
||||
])("cleanup retention rejects invalid values (%s)", (age) => {
|
||||
expect(() => validateCleanupOptions({ minAgeSeconds: age })).toThrow();
|
||||
});
|
||||
|
||||
test.each([0, 2, null])(
|
||||
"cleanup results distinguish known zero and unknown (%s)",
|
||||
(count) => {
|
||||
const result = decodeCleanupResult(
|
||||
JSON.parse(
|
||||
JSON.stringify({
|
||||
["format_version"]: 1,
|
||||
["min_age_seconds"]: 0,
|
||||
["statistics_complete"]: count !== null,
|
||||
["old_versions"]: count,
|
||||
["bytes_removed"]: count,
|
||||
future: "ignored",
|
||||
}),
|
||||
),
|
||||
);
|
||||
expect(result.oldVersions).toBe(count);
|
||||
},
|
||||
);
|
||||
|
||||
test.each([
|
||||
null,
|
||||
{},
|
||||
{ ["format_version"]: 2 },
|
||||
{
|
||||
["format_version"]: 1,
|
||||
["min_age_seconds"]: 0,
|
||||
["statistics_complete"]: false,
|
||||
["old_versions"]: 1,
|
||||
},
|
||||
])("cleanup rejects incompatible results (%p)", (value) => {
|
||||
expect(() => decodeCleanupResult(value)).toThrow();
|
||||
});
|
||||
|
||||
test("cleanup preview validates its versioned contract", () => {
|
||||
const value = JSON.parse(
|
||||
'{"format_version":1,"source_version":42,"min_age_seconds":0,"cutoff_millis":-1,"versions_to_remove":2,"bytes_to_remove":100}',
|
||||
);
|
||||
expect(decodeCleanupPlan(value).versionsToRemove).toBe(2);
|
||||
value.format_version = 2;
|
||||
expect(() => decodeCleanupPlan(value)).toThrow();
|
||||
});
|
||||
@@ -3328,3 +3328,59 @@ describe("table overrides", () => {
|
||||
expect(requests).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("cleanup typed results", () => {
|
||||
test.each(["counts", "no_op", "unknown", "missing", "FAILED", "CANCELLED"])(
|
||||
"reopens and waits for cleanup (%s)",
|
||||
async (outcome) => {
|
||||
const known = outcome !== "unknown";
|
||||
await withMockDatabase(
|
||||
(request, response) => {
|
||||
expect(request.url).toBe("/v1/jobs/describe");
|
||||
response.writeHead(200, { "Content-Type": "application/json" });
|
||||
response.end(
|
||||
JSON.stringify({
|
||||
["job_id"]: "cleanup-job",
|
||||
["job_type"]: "cleanup",
|
||||
["job_state"]: ["FAILED", "CANCELLED"].includes(outcome)
|
||||
? outcome
|
||||
: "DONE",
|
||||
result:
|
||||
outcome === "missing"
|
||||
? null
|
||||
: {
|
||||
["format_version"]: 1,
|
||||
["min_age_seconds"]: 0,
|
||||
["statistics_complete"]: known,
|
||||
["old_versions"]: known
|
||||
? outcome === "no_op"
|
||||
? 0
|
||||
: 2
|
||||
: null,
|
||||
["bytes_removed"]: known
|
||||
? outcome === "no_op"
|
||||
? 0
|
||||
: 123
|
||||
: null,
|
||||
},
|
||||
}),
|
||||
);
|
||||
},
|
||||
async (db) => {
|
||||
const job =
|
||||
await db.openJob<import("../lancedb").CleanupResult>("cleanup-job");
|
||||
if (["missing", "FAILED", "CANCELLED"].includes(outcome)) {
|
||||
await expect(job.wait()).rejects.toThrow();
|
||||
} else {
|
||||
const result = await job.wait();
|
||||
expect(result.statisticsComplete).toBe(known);
|
||||
expect(result.oldVersions).toBe(
|
||||
known ? (outcome === "no_op" ? 0 : 2) : null,
|
||||
);
|
||||
expect(result.minAgeSeconds).toBe(0);
|
||||
}
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
}
|
||||
@@ -669,6 +669,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(
|
||||
|
||||
@@ -572,6 +572,11 @@ impl Connection {
|
||||
job.into_typed::<lancedb::table::PrewarmDataResult>()
|
||||
.default_error()?,
|
||||
))
|
||||
} else if job.job_type().as_deref() == Some("cleanup") {
|
||||
Ok(crate::job::Job::new_typed(
|
||||
job.into_typed::<lancedb::table::CleanupResult>()
|
||||
.default_error()?,
|
||||
))
|
||||
} else if job.job_type().as_deref() == Some("compact") {
|
||||
Ok(crate::job::Job::new_typed(
|
||||
job.into_typed::<lancedb::table::CompactionResult>()
|
||||
|
||||
@@ -1001,6 +1001,33 @@ impl Table {
|
||||
Ok(self.inner_ref()?.current_branch())
|
||||
}
|
||||
|
||||
#[napi(catch_unwind)]
|
||||
pub async fn explain_cleanup(
|
||||
&self,
|
||||
min_age_seconds: Option<f64>,
|
||||
) -> napi::Result<serde_json::Value> {
|
||||
let plan = self
|
||||
.inner_ref()?
|
||||
.explain_cleanup(parse_cleanup_options(min_age_seconds)?)
|
||||
.await
|
||||
.default_error()?;
|
||||
serde_json::to_value(plan).map_err(|error| napi::Error::from_reason(error.to_string()))
|
||||
}
|
||||
|
||||
#[napi(catch_unwind)]
|
||||
pub async fn cleanup_async(
|
||||
&self,
|
||||
min_age_seconds: Option<f64>,
|
||||
) -> napi::Result<crate::job::Job> {
|
||||
let options = parse_cleanup_options(min_age_seconds)?;
|
||||
let job = self
|
||||
.inner_ref()?
|
||||
.cleanup_async(options)
|
||||
.await
|
||||
.default_error()?;
|
||||
Ok(crate::job::Job::new_typed(job))
|
||||
}
|
||||
|
||||
#[napi(catch_unwind)]
|
||||
pub async fn compact_async(
|
||||
&self,
|
||||
@@ -2600,6 +2627,22 @@ impl Branches {
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_cleanup_options(
|
||||
min_age_seconds: Option<f64>,
|
||||
) -> napi::Result<lancedb::table::CleanupOptions> {
|
||||
let mut options = lancedb::table::CleanupOptions::new();
|
||||
if let Some(age) = min_age_seconds {
|
||||
if !age.is_finite() || age.fract() != 0.0 || !(0.0..=9_007_199_254_740_991.0).contains(&age)
|
||||
{
|
||||
return Err(napi::Error::from_reason(
|
||||
"minAgeSeconds must be a nonnegative safe integer",
|
||||
));
|
||||
}
|
||||
options.min_age_seconds = Some(age as u64);
|
||||
}
|
||||
Ok(options)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::from_tag_hint;
|
||||
|
||||
Reference in New Issue
Block a user