Files
seaweedfs/weed/s3api/iceberg/vector_namespace_test.go
T
8bb5c069ec iceberg, s3tables: new tables take only fresh locations (#13160)
* s3tables, iceberg, lance: the catalog assigns every new location; an existing name answers only a reader

A REST create without a location gets <ns>/<name>-<uuid> (#12475) and a
location another record holds is refused (#12473), but a location a
client names is still taken: the REST create's location field and
warehouse property, S3 Tables CreateTable and CreateView with a
MetadataLocation, the Lance declare's location. A free location a client
picks can be one another caller holds staged write credentials for
(stage markers are not claims), or files no record claims any more.
Separately, CreateTable of a name that already holds a table returns
that table (ARN, version token, metadata location) to any caller who may
create, including one who may not read it.

- A REST create or create view naming a location (field or property,
  stage-create or not, the bare bucket included) is refused with 400
  before anything is written; the location is always placeNewEntry's:
  <ns>/<name>-<uuid> in the bucket, or under the operator's
  ICEBERG_WAREHOUSE (no longer under a client's warehouse property).
- A create commit uses a stage marker only when its assign-uuid matches
  the staged table's UUID (PyIceberg and Java send it); any other create
  commit is placed at a fresh <name>-<uuid>, never at the bare name path
  and never at another caller's staged location. Its updates may not set
  another location than the one assigned (400).
- S3 Tables CreateTable and CreateView naming a MetadataLocation are
  refused with 400 (ErrClientTableLocation) unless the call is the
  process's own (WithCatalogAssignedLocation: the REST create, create
  view and create commit at the location they placed; the Lance declare
  at the dataset's name path and register at an existing dataset). AWS's
  CreateTable takes no location either.
- A Lance declare naming a location is refused with 400 (register is for
  existing datasets).
- CreateTable of an existing name answers the table only to a caller who
  may GetTable it; a caller who may only create gets 409
  TableAlreadyExists and nothing about it.

Tests: TestAClientCreateNamesNoLocation (positive controls: a client
create without a location, the catalog's own create with one),
TestARESTCreateNamesNoLocation, TestACreateCommitCannotSetAnotherLocation,
TestACreateCommitTakesOnlyItsOwnStage (the stager's own commit is the
positive control), TestCreateExistingTableAnswersOnlyAReaderWithIt (a
reader gets the table). Existing tests adapted: the s3tables fixtures and
request helpers run as the catalog's own calls (assignedCtx), as do the
iceberg tests that seed tables through the manager;
iceberg_create_table_conflict_test.go's creates send no location and
newConflictEnv points placeNewEntry at conflictLocation, so its races
still collide on one location; TestCreatePathsRefuseAnotherTablesLocation
expects 400 for the three create paths and 409 for the register;
TestCreateTableWithoutLocationUsesItsOwnDirectory drops the bare-bucket
body; lance TestIcebergTablesAreInvisible seeds its Iceberg table as the
catalog; TestDeclareOfAnExistingTableKeepsItsLocation expects 400 for a
declare naming the index's location; TestDeregisterAndReregisterNeedUpdateTable
(#12552) expects 409 instead of 404 for the register and the declare of
a caller without GetTable: the table stays deregistered and nothing is
vended. Each rule is red with it undone.

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

* iceberg: a create may name a fresh location

A REST CreateTable naming a location (Trino always sends one built from
the namespace location, Spark LOCATION, PyIceberg location=) is accepted
again when nothing is stored under it and no live stage holds it; a used
or staged location answers 400. The check runs after the create's
authorization and before anything is written. A bare-bucket location
still gets the assigned <name>-<uuid>. CreateView always uses the
assigned location: a view holds only metadata files.

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

* lance: a declare may restate its name path as its location

pylance and other namespace clients may send the location they expect;
the dataset's own name path is accepted, any other location still
answers 400.

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

* iceberg: a commit cannot set another location on an existing table

CommitTable and CommitTransaction applied a set-location update, so a
table's metadata could name a location outside the directory its record
holds. Such a commit now answers 400 and nothing is written.

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

* iceberg: the fresh-location check pages through every stage marker

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

* iceberg: a fresh location stays apart from stages and tables above or under it

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

* iceberg: a create checks its location again after claiming it

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

* s3tables, iceberg: a live stage holds its location against every create

The catalog counts each live stage-create as a root of the record it
would commit to, so a native create, register, rename or move over a
stage's location is refused. The fresh-location check reads the same
roots, so a stage also backs out of a record that claimed an ancestor
after its first check. A location held by a record or stage answers 409,
as the create gate already did.

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

* iceberg: a create removes only the stages its location covers

A create or commit used to drop every stage marker of its name, which
freed the location of a stage whose credentials were still live.
Expired markers now go with their staged metadata.

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

* iceberg: an assigned stage location checks its claim again too

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

* iceberg: a stage marker lasts as long as its vended credentials

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

* iceberg: test a create that runs while a stage checks its claim

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

* iceberg: a stage holds its location against a create of the same name

A live stage-create now counts under its marker rather than under the
record of its name, so a same-name create over its location is refused
and a stage refuses a same-name table created over its location. Only a
staged commit takes the location its own stage holds.

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

* s3tables: a stage holds only its location against its own record's create

A live stage keeps the path of the record it would commit to away from
other records' files, but a create of that same record writes it: the
stage then holds only its location against it. A stage at its name path
can commit while a later stage of its name, elsewhere, is live.

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

* iceberg: a staged commit's create gate reads its location as a create does

The gate maps a metadata file's location to its table directory, so the
bare location of a stage under a metadata directory resolved to the
namespace and conflicted with the stage's own claim.

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

* iceberg, lance: tests from master create through the catalog's own call

Their creates name a location, which only the catalog's own calls may, and
a caller who may only create gets 409 for an existing name whatever
location its register names.

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

---------

Co-authored-by: Javier de la Torre <jatorre@carto.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 11:47:29 -08:00

183 lines
7.6 KiB
Go

package iceberg
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gorilla/mux"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3tables"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3tables/s3tablestest"
)
type openAuthenticator struct{}
func (openAuthenticator) AuthenticateRequest(*http.Request) (string, interface{}, s3err.ErrorCode) {
return s3tables.DefaultAccountID, nil, s3err.ErrNone
}
func (openAuthenticator) DefaultAllow() bool { return true }
// An index in a vector bucket is a catalog table the Iceberg catalog can load,
// but a caller holding every s3tables grant gets neither write credentials to
// it nor a commit into it.
func TestVectorIndexIsReadOnlyOverIceberg(t *testing.T) {
filer := s3tablestest.Start(t)
vendor := &stubVendor{credentials: &VendedCredentials{AccessKeyID: "ASIAEXAMPLE"}}
s := NewServer(s3tables.NewManagerClient(filer.Client), openAuthenticator{})
s.SetCredentialVendor(vendor)
s.s3Endpoint = "http://127.0.0.1:8333"
router := mux.NewRouter().SkipClean(true)
s.RegisterRoutes(router)
admin := s3tables.NewManager()
admin.SetTrusted(true)
client := s3tables.NewManagerClient(filer.Client)
arn, err := s3tables.BuildBucketARN(s3tables.DefaultRegion, s3tables.DefaultAccountID, "vec")
if err != nil {
t.Fatal(err)
}
metadata, _ := json.Marshal(map[string]any{"dimension": 4, "distanceMetric": "cosine"})
for _, op := range []struct {
name string
req interface{}
}{
{"CreateTableBucket", &s3tables.CreateTableBucketRequest{Name: "vec", Format: s3tables.FormatLance}},
{"CreateNamespace", &s3tables.CreateNamespaceRequest{TableBucketARN: arn, Namespace: []string{s3tables.VectorIndexNamespace}}},
{"CreateTable", &s3tables.CreateTableRequest{TableBucketARN: arn, Namespace: []string{s3tables.VectorIndexNamespace},
Name: "idx", Format: s3tables.FormatLance, Metadata: &s3tables.TableMetadata{FullMetadata: metadata}}},
} {
if err := admin.Execute(t.Context(), client, op.name, op.req, nil, ""); err != nil {
t.Fatalf("%s: %v", op.name, err)
}
}
req := httptest.NewRequest(http.MethodGet, "/v1/vec/namespaces/vector/tables/idx", nil)
req.Header.Set(accessDelegationHeader, "vended-credentials")
rec := httptest.NewRecorder()
router.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("load = %d (%s)", rec.Code, rec.Body.String())
}
if vendor.gotPrefix != "vector/idx" || !vendor.gotReadOnly {
t.Errorf("vended prefix %q read-only %v, want vector/idx read-only", vendor.gotPrefix, vendor.gotReadOnly)
}
reqErr := s.authorizeCommit(t.Context(), arn, []string{s3tables.VectorIndexNamespace}, "idx", s3tables.DefaultAccountID)
if reqErr == nil || reqErr.status != http.StatusForbidden {
t.Errorf("commit authorization = %+v, want 403", reqErr)
}
}
// Stage-create registers nothing, so the catalog's own location and vector
// namespace checks never see it. It must not hand out write credentials to an
// index, whether the staged table names the index's location from another
// namespace or sits in the vector namespace itself.
func TestStageCreateCannotReachVectorIndex(t *testing.T) {
filer := s3tablestest.Start(t)
s := NewServer(s3tables.NewManagerClient(filer.Client), openAuthenticator{})
s.s3Endpoint = "http://127.0.0.1:8333"
router := mux.NewRouter().SkipClean(true)
s.RegisterRoutes(router)
admin := s3tables.NewManager()
admin.SetTrusted(true)
client := s3tables.NewManagerClient(filer.Client)
arn, err := s3tables.BuildBucketARN(s3tables.DefaultRegion, s3tables.DefaultAccountID, "vec")
if err != nil {
t.Fatal(err)
}
for _, op := range []struct {
name string
req interface{}
}{
{"CreateTableBucket", &s3tables.CreateTableBucketRequest{Name: "vec", Format: s3tables.FormatLance}},
{"CreateNamespace", &s3tables.CreateNamespaceRequest{TableBucketARN: arn, Namespace: []string{s3tables.VectorIndexNamespace}}},
{"CreateNamespace", &s3tables.CreateNamespaceRequest{TableBucketARN: arn, Namespace: []string{"other"}}},
{"CreateTable", &s3tables.CreateTableRequest{TableBucketARN: arn, Namespace: []string{s3tables.VectorIndexNamespace},
Name: "idx", Format: s3tables.FormatLance}},
} {
if err := admin.Execute(t.Context(), client, op.name, op.req, nil, ""); err != nil {
t.Fatalf("%s: %v", op.name, err)
}
}
for name, tc := range map[string]struct {
namespace, body string
status int
}{
"index location from another namespace": {"other", `{"name":"x","stage-create":true,"location":"s3://vec/vector/idx"}`, http.StatusBadRequest},
"vector namespace": {s3tables.VectorIndexNamespace, `{"name":"y","stage-create":true}`, http.StatusForbidden},
} {
t.Run(name, func(t *testing.T) {
vendor := &stubVendor{credentials: &VendedCredentials{AccessKeyID: "ASIAEXAMPLE"}}
s.SetCredentialVendor(vendor)
req := httptest.NewRequest(http.MethodPost, "/v1/vec/namespaces/"+tc.namespace+"/tables", strings.NewReader(tc.body))
req.Header.Set(accessDelegationHeader, "vended-credentials")
rec := httptest.NewRecorder()
router.ServeHTTP(rec, req)
if rec.Code != tc.status {
t.Errorf("stage-create = %d (%s), want %d", rec.Code, rec.Body.String(), tc.status)
}
if vendor.gotPrefix != "" && !vendor.gotReadOnly {
t.Errorf("vended write credentials to %q", vendor.gotPrefix)
}
})
}
}
// A Lance dataset outside the vector namespace loads as an empty Iceberg
// table, but neither commit path may write Iceberg metadata over it: the
// record's location is the Lance client's dataset root.
func TestCatalogOnlyTableRefusesIcebergCommits(t *testing.T) {
filer := s3tablestest.Start(t)
s := NewServer(s3tables.NewManagerClient(filer.Client), openAuthenticator{})
router := mux.NewRouter().SkipClean(true)
s.RegisterRoutes(router)
admin := s3tables.NewManager()
admin.SetTrusted(true)
client := s3tables.NewManagerClient(filer.Client)
arn, err := s3tables.BuildBucketARN(s3tables.DefaultRegion, s3tables.DefaultAccountID, "lbk")
if err != nil {
t.Fatal(err)
}
const datasetRoot = "s3://lbk/ds/t.lance"
for _, op := range []struct {
name string
req interface{}
}{
{"CreateTableBucket", &s3tables.CreateTableBucketRequest{Name: "lbk", Format: s3tables.FormatLance}},
{"CreateNamespace", &s3tables.CreateNamespaceRequest{TableBucketARN: arn, Namespace: []string{"ds"}}},
{"CreateTable", &s3tables.CreateTableRequest{TableBucketARN: arn, Namespace: []string{"ds"},
Name: "t", Format: s3tables.FormatLance, MetadataLocation: datasetRoot}},
} {
if err := admin.Execute(s3tables.WithCatalogAssignedLocation(t.Context()), client, op.name, op.req, nil, ""); err != nil {
t.Fatalf("%s: %v", op.name, err)
}
}
for name, tc := range map[string]struct{ path, body string }{
"commit table": {"/v1/lbk/namespaces/ds/tables/t", `{"requirements":[],"updates":[]}`},
"commit transaction": {"/v1/lbk/transactions/commit", `{"table-changes":[{"identifier":{"namespace":["ds"],"name":"t"},"requirements":[],"updates":[]}]}`},
} {
t.Run(name, func(t *testing.T) {
rec := httptest.NewRecorder()
router.ServeHTTP(rec, httptest.NewRequest(http.MethodPost, tc.path, strings.NewReader(tc.body)))
if rec.Code != http.StatusBadRequest {
t.Errorf("commit = %d (%s), want 400", rec.Code, rec.Body.String())
}
var got s3tables.GetTableResponse
if err := admin.Execute(t.Context(), client, "GetTable", &s3tables.GetTableRequest{TableBucketARN: arn, Namespace: []string{"ds"}, Name: "t"}, &got, ""); err != nil {
t.Fatal(err)
}
if got.MetadataLocation != datasetRoot {
t.Errorf("metadata location = %q, want %q", got.MetadataLocation, datasetRoot)
}
})
}
}