Files
seaweedfs/weed/s3api/lance/listing_lookup_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

212 lines
7.6 KiB
Go

package lance
import (
"net/http"
"net/http/httptest"
"path"
"slices"
"strings"
"sync"
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3tables"
)
// lookupCounter counts, by entry name, the lookups the filer answers. The
// filer runs them on its own goroutines, hence the lock.
type lookupCounter struct {
mu sync.Mutex
counts map[string]int
}
func countLookups(h *testHarness) *lookupCounter {
c := &lookupCounter{counts: map[string]int{}}
h.filer.OnLookup = func(dir, name string) {
c.mu.Lock()
c.counts[name]++
c.mu.Unlock()
}
return c
}
func (c *lookupCounter) reset() {
c.mu.Lock()
c.counts = map[string]int{}
c.mu.Unlock()
}
func (c *lookupCounter) get(name string) int {
c.mu.Lock()
defer c.mu.Unlock()
return c.counts[name]
}
// declareLanceTable declares a table and returns the filer directory of its
// dataset, creating that directory if the declare did not.
func declareLanceTable(t *testing.T, h *testHarness, id string) string {
t.Helper()
declared := decode[DeclareTableResponse](t,
h.mustDo(t, http.MethodPost, "/v1/table/"+id+"/declare", `{}`, http.StatusOK))
dir := "/buckets/" + strings.TrimPrefix(declared.Location, "s3://")
if h.filer.Get(path.Dir(dir), path.Base(dir)) == nil {
h.filer.Put(path.Dir(dir), path.Base(dir), nil)
}
return dir
}
// A listing needs a table's _versions/ only to drop declared-only tables,
// which ListTables does for include_declared=false and nothing else does.
// Probing it for every Lance table regardless doubles the filer lookups of
// the default listing and of ListAllTables (N tables, 2N lookups) for an
// answer that is thrown away. A deregistered table is dropped on its marker
// alone, so its _versions/ is never needed either.
func TestListingProbesVersionsOnlyWhenNeeded(t *testing.T) {
h := newTestHarness(t)
h.createBucket(t, "analytics", s3tables.FormatLance)
h.createBucket(t, "metrics", s3tables.FormatLance)
h.mustDo(t, http.MethodPost, "/v1/namespace/analytics$sales/create", `{}`, http.StatusOK)
h.mustDo(t, http.MethodPost, "/v1/namespace/metrics$ops/create", `{}`, http.StatusOK)
declareLanceTable(t, h, "analytics$sales$declared")
withData := declareLanceTable(t, h, "analytics$sales$withdata")
h.filer.Put(withData, versionsDir, nil)
h.filer.PutFile(withData+"/"+versionsDir, "1.manifest", time.Now())
gone := declareLanceTable(t, h, "analytics$sales$gone")
h.filer.Put(gone, versionsDir, nil)
h.filer.PutFile(gone+"/"+versionsDir, "1.manifest", time.Now())
h.mustDo(t, http.MethodPost, "/v1/table/analytics$sales$gone/deregister", `{}`, http.StatusOK)
declareLanceTable(t, h, "metrics$ops$latency")
lookups := countLookups(h)
cases := []struct {
name string
target string
wantTables []string
wantMarker int // .lance-deregistered: one per Lance table listed from
wantVersions int // _versions: only where its answer decides the listing
}{
{
name: "ListTables default",
target: "/v1/namespace/analytics$sales/table/list",
wantTables: []string{"analytics$sales$declared", "analytics$sales$withdata"},
wantMarker: 3,
wantVersions: 0,
},
{
name: "ListTables include_declared=true",
target: "/v1/namespace/analytics$sales/table/list?include_declared=true",
wantTables: []string{"analytics$sales$declared", "analytics$sales$withdata"},
wantMarker: 3,
wantVersions: 0,
},
{
name: "ListTables include_declared=false",
target: "/v1/namespace/analytics$sales/table/list?include_declared=false",
wantTables: []string{"analytics$sales$withdata"},
wantMarker: 3,
// declared and withdata; gone is dropped on its marker
wantVersions: 2,
},
{
name: "ListAllTables",
target: "/v1/table",
wantTables: []string{"analytics$sales$declared", "analytics$sales$withdata", "metrics$ops$latency"},
wantMarker: 4,
wantVersions: 0,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
lookups.reset()
listed := decode[ListTablesResponse](t, h.mustDo(t, http.MethodGet, tc.target, "", http.StatusOK))
got := slices.Clone(listed.Tables)
slices.Sort(got)
if !slices.Equal(got, tc.wantTables) {
t.Errorf("tables = %v, want %v", got, tc.wantTables)
}
if n := lookups.get(deregisteredMarker); n != tc.wantMarker {
t.Errorf("%s lookups = %d, want %d", deregisteredMarker, n, tc.wantMarker)
}
if n := lookups.get(versionsDir); n != tc.wantVersions {
t.Errorf("%s lookups = %d, want %d: the listing probed data it does not use", versionsDir, n, tc.wantVersions)
}
})
}
}
// Describe, exists and register read a table's _versions/ only where the
// answer is used: describe with check_declared, and the register of a new
// name, which needs a dataset at the location. Everything else decides on the
// .lance-deregistered marker alone.
func TestHandlersProbeVersionsOnlyWhenNeeded(t *testing.T) {
h := newTestHarness(t)
h.createBucket(t, "analytics", s3tables.FormatLance)
h.mustDo(t, http.MethodPost, "/v1/namespace/analytics$sales/create", `{}`, http.StatusOK)
declareLanceTable(t, h, "analytics$sales$declared")
addVersions(h, declareLanceTable(t, h, "analytics$sales$withdata"))
addVersions(h, declareLanceTable(t, h, "analytics$sales$gone"))
h.mustDo(t, http.MethodPost, "/v1/table/analytics$sales$gone/deregister", `{}`, http.StatusOK)
h.filer.Put("/buckets/analytics/sales", "imported", nil)
addVersions(h, "/buckets/analytics/sales/imported")
internal := func(id string) {
t.Helper()
parts, err := parseIdentifier(id, defaultDelimiter)
if err != nil {
t.Fatalf("parse %s: %v", id, err)
}
if _, _, err := h.server.DescribeTableInternal(httptest.NewRequest(http.MethodPost, "/", nil), parts); err != nil {
t.Fatalf("DescribeTableInternal(%s): %v", id, err)
}
}
lookups := countLookups(h)
// in order: re-registering gone and registering imported change the state
cases := []struct {
name string
do func()
wantMarker int
wantVersions int
}{
{"describe", func() {
h.mustDo(t, http.MethodPost, "/v1/table/analytics$sales$declared/describe", `{}`, http.StatusOK)
}, 1, 0},
{"describe check_declared", func() {
h.mustDo(t, http.MethodPost, "/v1/table/analytics$sales$withdata/describe?check_declared=true", `{}`, http.StatusOK)
}, 1, 1},
{"describe check_declared deregistered", func() {
h.mustDo(t, http.MethodPost, "/v1/table/analytics$sales$gone/describe?check_declared=true", `{}`, http.StatusNotFound)
}, 1, 0},
{"DescribeTableInternal", func() { internal("analytics$sales$withdata") }, 1, 0},
{"exists", func() {
h.mustDo(t, http.MethodPost, "/v1/table/analytics$sales$withdata/exists", `{}`, http.StatusOK)
}, 1, 0},
{"register existing name", func() {
h.mustDo(t, http.MethodPost, "/v1/table/analytics$sales$withdata/register",
`{"location":"s3://analytics/sales/withdata"}`, http.StatusConflict)
}, 1, 0},
{"register deregistered name", func() {
h.mustDo(t, http.MethodPost, "/v1/table/analytics$sales$gone/register",
`{"location":"s3://analytics/sales/gone"}`, http.StatusOK)
}, 1, 0},
{"register new name", func() {
h.mustDo(t, http.MethodPost, "/v1/table/analytics$sales$imported/register",
`{"location":"s3://analytics/sales/imported"}`, http.StatusOK)
}, 1, 1},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
lookups.reset()
tc.do()
if n := lookups.get(deregisteredMarker); n != tc.wantMarker {
t.Errorf("%s lookups = %d, want %d", deregisteredMarker, n, tc.wantMarker)
}
if n := lookups.get(versionsDir); n != tc.wantVersions {
t.Errorf("%s lookups = %d, want %d: the handler probed data it does not use", versionsDir, n, tc.wantVersions)
}
})
}
}