From d29b0d430cbf42438b28aec2a3855cd12a1077c6 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Mon, 30 Nov 2026 11:30:17 -0800 Subject: [PATCH] lance: a register of a new name needs a dataset at its location (#13158) * lance: a register of a new name needs a Lance dataset at its location A Lance register of a new name takes any free location in the table's namespace (#12473 refuses one another record holds), so it can claim an empty location, or a dropped table's emptied root, in the catalog's stead. A register of a name without a record now authorizes the create with its location (AuthorizeCreateTable: the permission, the namespace check and the refusal of a claimed location run before anything is read, so a location in another namespace is answered alike whether it holds a dataset or not), then refuses with 400 a location holding no Lance dataset (no _versions/). A name that has a record, readable by the caller or not (deregistered, or without GetTable), keeps the existing path: CreateTable answers for it and bringing it back needs UpdateTable (#12552). Test: TestRegisterNeedsALanceDataset: an empty location 400; a caller who may not create 403; a dropped table's root 400; another namespace's path answers the same with and without a dataset; a written dataset registers (positive control). Red with the dataset check removed, the authorization skipped, or the location left out of the authorization; TestDeregisterAndReregisterNeedUpdateTable is red when the dataset check also runs for a name that has a record. Co-Authored-By: Claude Opus 5.5 * lance: a register of a name with a record keeps the CreateTable path Co-Authored-By: Claude Opus 5.5 * lance: a register needs _versions to be a directory Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Javier de la Torre Co-authored-by: Claude Opus 5.5 --- weed/s3api/lance/handlers_table.go | 28 ++++++++ weed/s3api/lance/handlers_test.go | 110 +++++++++++++++++++++++++++++ weed/s3api/lance/markers.go | 11 ++- 3 files changed, 146 insertions(+), 3 deletions(-) diff --git a/weed/s3api/lance/handlers_table.go b/weed/s3api/lance/handlers_table.go index e67c01363d..9d4397105b 100644 --- a/weed/s3api/lance/handlers_table.go +++ b/weed/s3api/lance/handlers_table.go @@ -5,6 +5,7 @@ import ( "strings" "github.com/seaweedfs/seaweedfs/weed/glog" + "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants" "github.com/seaweedfs/seaweedfs/weed/s3api/s3tables" ) @@ -419,6 +420,33 @@ func (s *Server) handleRegisterTable(w http.ResponseWriter, r *http.Request) { } } default: + // an existing record, of any format and even one the caller cannot read, is left to createTable + recordErr := s.executeInternal(r, "GetTable", &s3tables.GetTableRequest{ + TableBucketARN: bucketARN(bucket), Namespace: ns, Name: name, + }, &s3tables.GetTableResponse{}) + if recordErr != nil && !isNotFound(recordErr) { + writeStorageError(w, r, recordErr) + return + } + if recordErr != nil { + // a register of a new name needs an existing dataset; authorize before probing the location + if authErr := s.manager(r).AuthorizeCreateTable(r.Context(), s.filerClient, &s3tables.CreateTableRequest{ + TableBucketARN: bucketARN(bucket), Namespace: ns, Name: name, Format: s3tables.FormatLance, + MetadataLocation: location, + }, s3_constants.GetIdentityNameFromContext(r)); authErr != nil { + writeStorageError(w, r, authErr) + return + } + versions, lookupErr := s.lookupEntry(r, datasetDir(location), versionsDir) + if lookupErr != nil { + writeError(w, r, http.StatusInternalServerError, codeInternal, lookupErr.Error()) + return + } + if versions == nil || !versions.IsDirectory { + writeError(w, r, http.StatusBadRequest, codeInvalidInput, "location holds no Lance dataset (no "+versionsDir+"/): register points a name at an existing dataset") + return + } + } if location, err = s.createTable(r, bucket, ns, name, location); err != nil { writeStorageError(w, r, err) return diff --git a/weed/s3api/lance/handlers_test.go b/weed/s3api/lance/handlers_test.go index 713e33734a..b0eafbf524 100644 --- a/weed/s3api/lance/handlers_test.go +++ b/weed/s3api/lance/handlers_test.go @@ -5,6 +5,7 @@ import ( "errors" "net/http" "net/http/httptest" + "path" "strings" "testing" "time" @@ -606,3 +607,112 @@ func TestDescribeTableInternalVendsWriteForTheVectorNamespace(t *testing.T) { } } } + +// actionsAuthenticator authenticates every request as name, holding actions. +type actionsAuthenticator struct { + name string + actions []string +} + +func (a actionsAuthenticator) AuthenticateRequest(*http.Request) (string, interface{}, s3err.ErrorCode) { + return a.name, &struct { + Name string + Actions []string + }{Name: a.name, Actions: a.actions}, s3err.ErrNone +} + +func (actionsAuthenticator) DefaultAllow() bool { return false } + +func (actionsAuthenticator) AuthEnabled() bool { return true } + +// A register points a name at a dataset that exists: a location holding no +// Lance dataset (nothing there, or a dropped table's root, which its drop +// emptied) is refused, so a register never claims a location in the +// catalog's stead; a location holding one (_versions/) registers. +func TestRegisterNeedsALanceDataset(t *testing.T) { + h := newTestHarness(t) + h.createBucket(t, "analytics", s3tables.FormatLance) + h.mustDo(t, http.MethodPost, "/v1/namespace/analytics$sales/create", `{}`, http.StatusOK) + + h.mustDo(t, http.MethodPost, "/v1/table/analytics$sales$empty/register", `{"location":"s3://analytics/sales/a"}`, http.StatusBadRequest) + // a caller who may not create there learns nothing about the location + server := NewServer(s3tables.NewManagerClient(h.filer.Client), actionsAuthenticator{name: "nobody", actions: []string{"s3tables:GetTable"}}) + router := mux.NewRouter().SkipClean(true) + server.RegisterRoutes(router) + nobody := &testHarness{router: router, filer: h.filer, admin: h.admin, server: server} + if r := nobody.do(t, http.MethodPost, "/v1/table/analytics$sales$empty/register", `{"location":"s3://analytics/sales/a"}`); r.Code != http.StatusForbidden { + t.Errorf("register by a caller who may not create: %d (%s), want 403", r.Code, r.Body.String()) + } + + declared := decode[DeclareTableResponse](t, + h.mustDo(t, http.MethodPost, "/v1/table/analytics$sales$orders/declare", `{}`, http.StatusOK)) + dir := "/buckets/" + strings.TrimPrefix(declared.Location, "s3://") + if h.filer.Get(path.Dir(dir), path.Base(dir)) == nil { // a declare at the name path made it already + h.filer.Put(path.Dir(dir), path.Base(dir), nil) + } + h.filer.Put(dir, versionsDir, nil) + h.filer.PutFile(dir+"/"+versionsDir, "1.manifest", time.Now()) + h.mustDo(t, http.MethodPost, "/v1/table/analytics$sales$orders/drop", `{}`, http.StatusOK) + h.mustDo(t, http.MethodPost, "/v1/table/analytics$sales$dropped/register", `{"location":"`+declared.Location+`"}`, http.StatusBadRequest) + + // a location in another namespace is refused alike whether a dataset is + // there or not: the register is no probe of other namespaces + h.mustDo(t, http.MethodPost, "/v1/namespace/analytics$secret/create", `{}`, http.StatusOK) + h.filer.Put("/buckets/analytics/secret", "ledger", nil) + h.filer.Put("/buckets/analytics/secret/ledger", versionsDir, nil) + h.filer.PutFile("/buckets/analytics/secret/ledger/"+versionsDir, "1.manifest", time.Now()) + existing := h.do(t, http.MethodPost, "/v1/table/analytics$sales$probe/register", `{"location":"s3://analytics/secret/ledger"}`) + absent := h.do(t, http.MethodPost, "/v1/table/analytics$sales$probe/register", `{"location":"s3://analytics/secret/nothing"}`) + if existing.Code < 400 || existing.Code != absent.Code || existing.Body.String() != absent.Body.String() { + t.Errorf("register at another namespace's dataset: %d %s; at an absent path there: %d %s; want one refusal", + existing.Code, existing.Body.String(), absent.Code, absent.Body.String()) + } + + // a file named _versions is no dataset + h.filer.Put("/buckets/analytics/sales", "flat", nil) + h.filer.PutFile("/buckets/analytics/sales/flat", versionsDir, time.Now()) + h.mustDo(t, http.MethodPost, "/v1/table/analytics$sales$flat/register", `{"location":"s3://analytics/sales/flat"}`, http.StatusBadRequest) + + // positive control: a dataset written there registers + h.filer.Put("/buckets/analytics/sales", "imported", nil) + h.filer.Put("/buckets/analytics/sales/imported", versionsDir, nil) + h.filer.PutFile("/buckets/analytics/sales/imported/"+versionsDir, "1.manifest", time.Now()) + h.mustDo(t, http.MethodPost, "/v1/table/analytics$sales$imported/register", `{"location":"s3://analytics/sales/imported"}`, http.StatusOK) +} + +// A register of a name that already has a record answers for that record, +// whatever the location it names holds. +func TestRegisterOfAnExistingName(t *testing.T) { + h := newTestHarness(t) + h.createBucket(t, "analytics", "") + h.mustDo(t, http.MethodPost, "/v1/namespace/analytics$sales/create", `{}`, http.StatusOK) + if err := h.admin.Execute(t.Context(), s3tables.NewManagerClient(h.filer.Client), "CreateTable", &s3tables.CreateTableRequest{ + TableBucketARN: h.bucketARN(t, "analytics"), + Namespace: []string{"sales"}, + Name: "ledger", + Format: s3tables.FormatIceberg, + MetadataLocation: "s3://analytics/sales/ledger/metadata/v1.metadata.json", + }, &s3tables.CreateTableResponse{}, ""); err != nil { + t.Fatalf("create iceberg table: %v", err) + } + declared := decode[DeclareTableResponse](t, + h.mustDo(t, http.MethodPost, "/v1/table/analytics$sales$orders/declare", `{}`, http.StatusOK)) + + t.Run("another format", func(t *testing.T) { + recorder := h.mustDo(t, http.MethodPost, "/v1/table/analytics$sales$ledger/register", `{"location":"s3://analytics/sales/nothing"}`, http.StatusConflict) + if got := decode[errorResponse](t, recorder); got.Code != codeTableAlreadyExists { + t.Fatalf("error code = %d, want %d", got.Code, codeTableAlreadyExists) + } + }) + t.Run("a caller who may only create", func(t *testing.T) { + server := NewServer(s3tables.NewManagerClient(h.filer.Client), actionsAuthenticator{name: "creator", actions: []string{"s3tables:CreateTable"}}) + router := mux.NewRouter().SkipClean(true) + server.RegisterRoutes(router) + creator := &testHarness{router: router, filer: h.filer, admin: h.admin, server: server} + got := decode[RegisterTableResponse](t, + creator.mustDo(t, http.MethodPost, "/v1/table/analytics$sales$orders/register", `{"location":"s3://analytics/sales/ledger"}`, http.StatusOK)) + if got.Location != declared.Location { + t.Fatalf("register answered %q, want the record's own %q", got.Location, declared.Location) + } + }) +} diff --git a/weed/s3api/lance/markers.go b/weed/s3api/lance/markers.go index 9b16336024..88f4977f7d 100644 --- a/weed/s3api/lance/markers.go +++ b/weed/s3api/lance/markers.go @@ -23,9 +23,14 @@ func datasetDir(location string) string { } func (s *Server) entryExists(r *http.Request, dir, name string) (bool, error) { - found := false + entry, err := s.lookupEntry(r, dir, name) + return entry != nil, err +} + +func (s *Server) lookupEntry(r *http.Request, dir, name string) (*filer_pb.Entry, error) { + var found *filer_pb.Entry err := s.filerClient.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error { - _, lookupErr := filer_pb.LookupEntry(r.Context(), client, &filer_pb.LookupDirectoryEntryRequest{ + resp, lookupErr := filer_pb.LookupEntry(r.Context(), client, &filer_pb.LookupDirectoryEntryRequest{ Directory: dir, Name: name, }) @@ -35,7 +40,7 @@ func (s *Server) entryExists(r *http.Request, dir, name string) (bool, error) { if lookupErr != nil { return lookupErr } - found = true + found = resp.Entry return nil }) return found, err