Files
seaweedfs/weed/s3api/lance/dataset_state_test.go
T
d9eaf85cd7 perf(lance): skip unused _versions lookups (#13371)
* lance: skip unused _versions lookups

Read _versions/ only for include_declared=false, check_declared
or a new-name register: listings now do one lookup per table
instead of two (about 45% faster at 100-500 tables).
A failed _versions lookup no longer hides a table that never needed it.

Adds visibility regression tests, lookup-count tests and a benchmark.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* lance: read the deregistered marker through datasetState

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* lance: trim the dataset-state comments

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
2026-12-03 23:44:47 -08:00

295 lines
12 KiB
Go

package lance
import (
"errors"
"net/http"
"net/http/httptest"
"slices"
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3tables"
)
// These tests pin what the catalog reports about a Lance table from the
// marker files beside its dataset: .lance-deregistered hides it, and
// _versions/ is what separates a table holding data from one only declared.
// addVersions gives the dataset in dir a version manifest, which is what a
// Lance client writing data leaves behind.
func addVersions(h *testHarness, dir string) {
h.filer.Put(dir, versionsDir, nil)
h.filer.PutFile(dir+"/"+versionsDir, "1.manifest", time.Now())
}
// seedTableStates declares, in namespace bucket$ns, one table in each state a
// listing has to tell apart, and returns the identifiers of the two that are
// registered.
func seedTableStates(t *testing.T, h *testHarness, bucket, ns string) (declaredOnly, withData string) {
t.Helper()
prefix := bucket + "$" + ns + "$"
h.mustDo(t, http.MethodPost, "/v1/namespace/"+bucket+"$"+ns+"/create", `{}`, http.StatusOK)
declareLanceTable(t, h, prefix+"declared_only")
addVersions(h, declareLanceTable(t, h, prefix+"with_data"))
addVersions(h, declareLanceTable(t, h, prefix+"deregistered_with_data"))
h.mustDo(t, http.MethodPost, "/v1/table/"+prefix+"deregistered_with_data/deregister", `{}`, http.StatusOK)
declareLanceTable(t, h, prefix+"deregistered_empty")
h.mustDo(t, http.MethodPost, "/v1/table/"+prefix+"deregistered_empty/deregister", `{}`, http.StatusOK)
return prefix + "declared_only", prefix + "with_data"
}
func listedTables(t *testing.T, h *testHarness, target string) []string {
t.Helper()
listed := decode[ListTablesResponse](t, h.mustDo(t, http.MethodGet, target, "", http.StatusOK))
tables := slices.Clone(listed.Tables)
slices.Sort(tables)
return tables
}
func TestListTablesVisibility(t *testing.T) {
h := newTestHarness(t)
h.createBucket(t, "analytics", s3tables.FormatLance)
declaredOnly, withData := seedTableStates(t, h, "analytics", "sales")
// an undeclared bucket holds both formats; only the Lance table is listed
h.createBucket(t, "mixed", "")
h.mustDo(t, http.MethodPost, "/v1/namespace/mixed$sales/create", `{}`, http.StatusOK)
addVersions(h, declareLanceTable(t, h, "mixed$sales$vectors"))
var created s3tables.CreateTableResponse
if err := h.admin.Execute(s3tables.WithCatalogAssignedLocation(t.Context()), s3tables.NewManagerClient(h.filer.Client), "CreateTable",
&s3tables.CreateTableRequest{
TableBucketARN: h.bucketARN(t, "mixed"),
Namespace: []string{"sales"},
Name: "ledger",
Format: s3tables.FormatIceberg,
MetadataLocation: "s3://mixed/sales/ledger/metadata/v1.metadata.json",
}, &created, ""); err != nil {
t.Fatalf("create iceberg table: %v", err)
}
for _, tc := range []struct {
query string
analytics []string
}{
{query: "", analytics: []string{declaredOnly, withData}},
{query: "?include_declared=true", analytics: []string{declaredOnly, withData}},
{query: "?include_declared=false", analytics: []string{withData}},
} {
if got := listedTables(t, h, "/v1/namespace/analytics$sales/table/list"+tc.query); !slices.Equal(got, tc.analytics) {
t.Errorf("ListTables%s = %v, want %v", tc.query, got, tc.analytics)
}
if got, want := listedTables(t, h, "/v1/namespace/mixed$sales/table/list"+tc.query), []string{"mixed$sales$vectors"}; !slices.Equal(got, want) {
t.Errorf("ListTables%s in a mixed bucket = %v, want %v", tc.query, got, want)
}
}
}
// ListAllTables lists declared-only tables too, and does not read
// include_declared at all.
func TestListAllTablesVisibility(t *testing.T) {
h := newTestHarness(t)
h.createBucket(t, "analytics", s3tables.FormatLance)
h.createBucket(t, "metrics", s3tables.FormatLance)
a1, a2 := seedTableStates(t, h, "analytics", "sales")
m1, m2 := seedTableStates(t, h, "metrics", "ops")
want := []string{a1, a2, m1, m2}
slices.Sort(want)
for _, query := range []string{"", "?include_declared=false"} {
if got := listedTables(t, h, "/v1/table"+query); !slices.Equal(got, want) {
t.Errorf("ListAllTables%s = %v, want %v", query, got, want)
}
}
}
func TestListTablesDelimiterAndPaging(t *testing.T) {
h := newTestHarness(t)
h.createBucket(t, "analytics", s3tables.FormatLance)
declaredOnly, withData := seedTableStates(t, h, "analytics", "sales")
dotted := listedTables(t, h, "/v1/namespace/analytics.sales/table/list?delimiter=.")
if want := []string{"analytics.sales.declared_only", "analytics.sales.with_data"}; !slices.Equal(dotted, want) {
t.Errorf("ListTables with delimiter . = %v, want %v", dotted, want)
}
// One table a page: the token walks every catalog record, the deregistered
// ones answer as empty pages, and nothing is listed twice.
var all []string
token := ""
for page := 0; ; page++ {
if page > 10 {
t.Fatalf("paging did not end; listed so far %v", all)
}
target := "/v1/namespace/analytics$sales/table/list?limit=1"
if token != "" {
target += "&page_token=" + token
}
listed := decode[ListTablesResponse](t, h.mustDo(t, http.MethodGet, target, "", http.StatusOK))
if len(listed.Tables) > 1 {
t.Fatalf("page %d holds %v, more than limit=1", page, listed.Tables)
}
all = append(all, listed.Tables...)
if listed.PageToken == "" {
break
}
token = listed.PageToken
}
slices.Sort(all)
if want := []string{declaredOnly, withData}; !slices.Equal(all, want) {
t.Errorf("tables across pages = %v, want %v", all, want)
}
}
func TestDescribeTableCheckDeclared(t *testing.T) {
h := newTestHarness(t)
h.createBucket(t, "analytics", s3tables.FormatLance)
declaredOnly, withData := seedTableStates(t, h, "analytics", "sales")
describe := func(id, query, body string) DescribeTableResponse {
t.Helper()
return decode[DescribeTableResponse](t,
h.mustDo(t, http.MethodPost, "/v1/table/"+id+"/describe"+query, body, http.StatusOK))
}
for _, id := range []string{declaredOnly, withData} {
for _, req := range []struct{ query, body string }{
{"", `{}`},
{"?check_declared=false", `{}`},
{"", `{"check_declared":false}`},
} {
if got := describe(id, req.query, req.body).IsOnlyDeclared; got != nil {
t.Errorf("describe %s%s %s: is_only_declared = %v, want it absent", id, req.query, req.body, *got)
}
}
}
for _, req := range []struct{ query, body string }{
{"?check_declared=true", `{}`},
{"", `{"check_declared":true}`},
} {
for id, want := range map[string]bool{declaredOnly: true, withData: false} {
got := describe(id, req.query, req.body).IsOnlyDeclared
if got == nil || *got != want {
t.Errorf("describe %s%s %s: is_only_declared = %v, want %v", id, req.query, req.body, got, want)
}
}
recorder := h.mustDo(t, http.MethodPost, "/v1/table/analytics$sales$deregistered_with_data/describe"+req.query, req.body, http.StatusNotFound)
if code := decode[errorResponse](t, recorder).Code; code != codeTableNotFound {
t.Errorf("describe of a deregistered table%s %s: code %d, want %d", req.query, req.body, code, codeTableNotFound)
}
}
}
// A deregistered table is absent to every operation until it is registered
// again, and registering it again is not a conflict.
func TestDeregisteredTableIsAbsent(t *testing.T) {
h := newTestHarness(t)
h.createBucket(t, "analytics", s3tables.FormatLance)
declaredOnly, withData := seedTableStates(t, h, "analytics", "sales")
gone := "analytics$sales$deregistered_with_data"
internal := func(id string) error {
t.Helper()
parts, err := parseIdentifier(id, defaultDelimiter)
if err != nil {
t.Fatalf("parse %s: %v", id, err)
}
_, _, err = h.server.DescribeTableInternal(httptest.NewRequest(http.MethodPost, "/", nil), parts)
return err
}
for _, id := range []string{declaredOnly, withData} {
h.mustDo(t, http.MethodPost, "/v1/table/"+id+"/exists", `{}`, http.StatusOK)
h.mustDo(t, http.MethodPost, "/v1/table/"+id+"/describe", `{}`, http.StatusOK)
if err := internal(id); err != nil {
t.Errorf("DescribeTableInternal(%s) = %v, want the table", id, err)
}
}
location := `{"location":"s3://analytics/sales/with_data"}`
h.mustDo(t, http.MethodPost, "/v1/table/"+withData+"/register", location, http.StatusConflict)
h.mustDo(t, http.MethodPost, "/v1/table/"+gone+"/exists", `{}`, http.StatusNotFound)
h.mustDo(t, http.MethodPost, "/v1/table/"+gone+"/describe", `{}`, http.StatusNotFound)
var tablesErr *s3tables.S3TablesError
if err := internal(gone); !errors.As(err, &tablesErr) || tablesErr.Type != s3tables.ErrCodeNoSuchTable {
t.Errorf("DescribeTableInternal of a deregistered table = %v, want %s", err, s3tables.ErrCodeNoSuchTable)
}
h.mustDo(t, http.MethodPost, "/v1/table/"+gone+"/register",
`{"location":"s3://analytics/sales/deregistered_with_data"}`, http.StatusOK)
if h.filer.Get("/buckets/analytics/sales/deregistered_with_data", deregisteredMarker) != nil {
t.Errorf("register left %s behind", deregisteredMarker)
}
h.mustDo(t, http.MethodPost, "/v1/table/"+gone+"/exists", `{}`, http.StatusOK)
if got := listedTables(t, h, "/v1/namespace/analytics$sales/table/list"); !slices.Contains(got, gone) {
t.Errorf("re-registered table missing from the listing %v", got)
}
}
// The markers are read where the catalog record says the dataset is: its
// recorded location, or the name path when it records none.
func TestListingReadsMarkersAtTheRecordedLocation(t *testing.T) {
h := newTestHarness(t)
h.createBucket(t, "analytics", s3tables.FormatLance)
h.mustDo(t, http.MethodPost, "/v1/namespace/analytics$sales/create", `{}`, http.StatusOK)
client := s3tables.NewManagerClient(h.filer.Client)
create := func(name, location string) {
t.Helper()
var resp s3tables.CreateTableResponse
if err := h.admin.Execute(s3tables.WithCatalogAssignedLocation(t.Context()), client, "CreateTable", &s3tables.CreateTableRequest{
TableBucketARN: h.bucketARN(t, "analytics"),
Namespace: []string{"sales"},
Name: name,
Format: s3tables.FormatLance,
MetadataLocation: location,
}, &resp, ""); err != nil {
t.Fatalf("create %s: %v", name, err)
}
}
create("unplaced", "")
create("alias", "s3://analytics/sales/store")
var summaries s3tables.ListTablesResponse
if err := h.admin.Execute(t.Context(), client, "ListTables", &s3tables.ListTablesRequest{
TableBucketARN: h.bucketARN(t, "analytics"), Namespace: []string{"sales"},
}, &summaries, ""); err != nil {
t.Fatalf("list tables: %v", err)
}
for _, summary := range summaries.Tables {
if summary.Name == "unplaced" && summary.MetadataLocation != "" {
t.Fatalf("unplaced records location %q; the fallback case needs none", summary.MetadataLocation)
}
}
list := func(query string) []string {
t.Helper()
return listedTables(t, h, "/v1/namespace/analytics$sales/table/list"+query)
}
both := []string{"analytics$sales$alias", "analytics$sales$unplaced"}
if got := list(""); !slices.Equal(got, both) {
t.Fatalf("listing = %v, want %v", got, both)
}
// data at the name path does not count for a table recorded elsewhere
addVersions(h, "/buckets/analytics/sales/alias")
if got := list("?include_declared=false"); len(got) != 0 {
t.Errorf("include_declared=false with data only at the name paths = %v, want none", got)
}
addVersions(h, "/buckets/analytics/sales/store")
addVersions(h, "/buckets/analytics/sales/unplaced")
if got := list("?include_declared=false"); !slices.Equal(got, both) {
t.Errorf("include_declared=false with data at the recorded locations = %v, want %v", got, both)
}
// a marker at the name path does not hide a table recorded elsewhere
h.filer.PutFile("/buckets/analytics/sales/alias", deregisteredMarker, time.Now())
if got := list(""); !slices.Equal(got, both) {
t.Errorf("listing with a marker only at alias's name path = %v, want %v", got, both)
}
h.filer.PutFile("/buckets/analytics/sales/store", deregisteredMarker, time.Now())
h.filer.PutFile("/buckets/analytics/sales/unplaced", deregisteredMarker, time.Now())
if got := list(""); len(got) != 0 {
t.Errorf("listing with markers at the recorded locations = %v, want none", got)
}
}