Files
seaweedfs/weed/s3api/s3tables/handler_update_after_rename_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

257 lines
12 KiB
Go

package s3tables
import (
"context"
"encoding/json"
"errors"
"testing"
"github.com/stretchr/testify/require"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3tables/s3tablestest"
)
// A rename is catalog-only: a table or view renamed into another namespace keeps
// its files, and so the directory its metadata location names, under the
// namespace it came from. Its next update writes there and must be accepted,
// while an update that moves it elsewhere stays confined to its new namespace.
func addRenameNamespace(t *testing.T, fs *s3tablestest.MemFiler, namespace string) {
t.Helper()
nsMeta, err := json.Marshal(namespaceMetadata{Namespace: []string{namespace}, OwnerAccountID: DefaultAccountID})
require.NoError(t, err)
fs.Put(GetTableBucketPath(renameTestBucket), namespace, map[string][]byte{ExtendedKeyMetadata: nsMeta})
}
// renamedAcrossNamespaces seeds ns/t, whose files sit under ns/t, and renames it
// to ns2/t.
func renamedAcrossNamespaces(t *testing.T) (*s3tablestest.MemFiler, *Manager) {
t.Helper()
fs, m := startRenameManager(t)
addRenameNamespace(t, fs, "ns2")
require.NoError(t, runRename(t, m, fs, &RenameTableRequest{TableBucketARN: mustBucketARN(t),
SourceNamespace: []string{"ns"}, SourceName: "t", DestNamespace: []string{"ns2"}, DestName: "t"}))
return fs, m
}
func seedRenameTable(t *testing.T, fs *s3tablestest.MemFiler, namespace, name string) {
t.Helper()
meta, err := json.Marshal(tableMetadataInternal{Name: name, Namespace: namespace, Format: "ICEBERG", OwnerAccountID: DefaultAccountID,
MetadataVersion: 1, MetadataLocation: "s3://" + renameTestBucket + "/" + namespace + "/" + name + "/metadata/v1.metadata.json"})
require.NoError(t, err)
fs.Put(GetNamespacePath(renameTestBucket, namespace), name, map[string][]byte{ExtendedKeyMetadata: meta})
}
func updateTableAt(ctx context.Context, t *testing.T, m *Manager, fs *s3tablestest.MemFiler, namespace, location string) error {
return m.Execute(ctx, NewManagerClient(fs.Client), "UpdateTable", &UpdateTableRequest{TableBucketARN: mustBucketARN(t),
Namespace: []string{namespace}, Name: "t", MetadataVersion: 4, MetadataLocation: location}, nil, "")
}
func s3TablesErrorText(err error) string {
var tableErr *S3TablesError
if errors.As(err, &tableErr) {
return tableErr.Type + ": " + tableErr.Message
}
if err != nil {
return err.Error()
}
return ""
}
func TestUpdateAfterRenameAcrossNamespacesKeepsItsDirectory(t *testing.T) {
fs, m := renamedAcrossNamespaces(t)
location := "s3://" + renameTestBucket + "/ns/t/metadata/v4.metadata.json"
require.NoError(t, updateTableAt(context.Background(), t, m, fs, "ns2", location))
got, err := runGetTable(t, m, fs, "ns2", "t")
require.NoError(t, err)
require.Equal(t, location, got.MetadataLocation)
}
func TestUpdateAfterRenameAcrossNamespacesRefusesOtherDirectoriesOfTheOldNamespace(t *testing.T) {
fs, m := renamedAcrossNamespaces(t)
err := updateTableAt(context.Background(), t, m, fs, "ns2", "s3://"+renameTestBucket+"/ns/elsewhere/metadata/v4.metadata.json")
require.Error(t, err)
require.Contains(t, s3TablesErrorText(err), "must be within namespace ns2")
}
func TestUpdateAfterRenameAcrossNamespacesRefusesAnotherTablesDirectory(t *testing.T) {
// One the old namespace holds: not the table's own directory, and outside
// its current namespace.
fs, m := renamedAcrossNamespaces(t)
seedRenameTable(t, fs, "ns", "u")
err := updateTableAt(context.Background(), t, m, fs, "ns2", "s3://"+renameTestBucket+"/ns/u/metadata/v9.metadata.json")
require.Error(t, err)
require.Contains(t, s3TablesErrorText(err), "must be within namespace ns2")
// One the current namespace holds: inside the namespace, but another root.
// The catalog refuses every update that moves a record's root, before it
// looks at who holds the new one (ErrLocationMove).
fs, m = renamedAcrossNamespaces(t)
seedRenameTable(t, fs, "ns2", "w")
err = updateTableAt(context.Background(), t, m, fs, "ns2", "s3://"+renameTestBucket+"/ns2/w/metadata/v9.metadata.json")
require.Error(t, err)
require.Contains(t, s3TablesErrorText(err), ErrLocationMove.Error())
}
// A table created under the renamed table's old name has its entry on that
// directory and its files in a directory of its own, so the renamed table still
// keeps its files there alone. The record is seeded rather than created, since
// a rename may keep the old name taken.
func TestUpdateAfterRenameAcrossNamespacesKeepsItsDirectoryUnderANewTableOfTheOldName(t *testing.T) {
ctx := context.Background()
fs, m := renamedAcrossNamespaces(t)
meta, err := json.Marshal(tableMetadataInternal{Name: "t", Namespace: "ns", Format: "ICEBERG", OwnerAccountID: DefaultAccountID,
MetadataVersion: 1, WarehouseLocation: "s3://" + renameTestBucket + "/ns/t-fresh"})
require.NoError(t, err)
fs.Put(GetNamespacePath(renameTestBucket, "ns"), "t", map[string][]byte{ExtendedKeyMetadata: meta})
location := "s3://" + renameTestBucket + "/ns/t/metadata/v4.metadata.json"
require.NoError(t, updateTableAt(ctx, t, m, fs, "ns2", location))
got, err := runGetTable(t, m, fs, "ns2", "t")
require.NoError(t, err)
require.Equal(t, location, got.MetadataLocation)
}
// A record written before locations were confined to their namespace and
// claimed can keep its files in another table's directory. It gets no
// exception: it stays held to its namespace, as before.
func TestUpdateRefusesADirectoryOutsideTheNamespaceThatAnotherTableShares(t *testing.T) {
fs, m := startRenameManager(t)
addRenameNamespace(t, fs, "ns2")
meta, err := json.Marshal(tableMetadataInternal{Name: "t", Namespace: "ns2", Format: "ICEBERG", OwnerAccountID: DefaultAccountID,
MetadataVersion: 3, MetadataLocation: "s3://" + renameTestBucket + "/ns/t/metadata/v3.metadata.json"})
require.NoError(t, err)
fs.Put(GetNamespacePath(renameTestBucket, "ns2"), "t", map[string][]byte{ExtendedKeyMetadata: meta})
err = updateTableAt(context.Background(), t, m, fs, "ns2", "s3://"+renameTestBucket+"/ns/t/metadata/v4.metadata.json")
require.Error(t, err)
require.Contains(t, s3TablesErrorText(err), "must be within namespace ns2")
}
// The catalog refuses every update that moves a record's root (ErrLocationMove):
// a renamed table keeps committing under the directory it has
// (TestUpdateAfterRenameAcrossNamespacesKeepsItsDirectory) and does not move
// into its new namespace, where its older snapshots' files are not.
func TestUpdateAfterRenameAcrossNamespacesMayMoveIntoItsNamespace(t *testing.T) {
fs, m := renamedAcrossNamespaces(t)
err := updateTableAt(context.Background(), t, m, fs, "ns2", "s3://"+renameTestBucket+"/ns2/t-moved/metadata/v4.metadata.json")
require.Error(t, err)
require.Contains(t, s3TablesErrorText(err), ErrLocationMove.Error())
}
func TestUpdateStillConfinesAMoveToTheTablesNamespace(t *testing.T) {
fs, m := startRenameManager(t)
addRenameNamespace(t, fs, "ns2")
err := updateTableAt(context.Background(), t, m, fs, "ns", "s3://"+renameTestBucket+"/ns2/elsewhere/metadata/v4.metadata.json")
require.Error(t, err)
require.Contains(t, s3TablesErrorText(err), "must be within namespace ns")
}
func TestUpdateLocationIsCheckedAfterAuthorization(t *testing.T) {
fs, m := renamedAcrossNamespaces(t)
m.SetTrusted(false)
m.SetDefaultAllow(false)
stranger := &testIdentity{Name: "stranger", Account: &testIdentityAccount{Id: "stranger"}}
ctx := s3_constants.SetIdentityInContext(assignedCtx(), stranger)
err := updateTableAt(ctx, t, m, fs, "ns2", "s3://"+renameTestBucket+"/ns/elsewhere/metadata/v4.metadata.json")
require.Error(t, err)
require.NotContains(t, s3TablesErrorText(err), "must be within namespace",
"a caller who may not update the table must not learn where its files may go")
}
func renameViewAcrossNamespaces(t *testing.T) (*s3tablestest.MemFiler, *Manager) {
t.Helper()
fs, m := startRenameManager(t)
seedView(t, fs, "v")
addRenameNamespace(t, fs, "ns2")
require.NoError(t, m.Execute(assignedCtx(), NewManagerClient(fs.Client), "RenameView", &RenameTableRequest{
TableBucketARN: mustBucketARN(t), SourceNamespace: []string{"ns"}, SourceName: "v", DestNamespace: []string{"ns2"}, DestName: "v",
}, nil, ""))
return fs, m
}
func updateViewAt(t *testing.T, m *Manager, fs *s3tablestest.MemFiler, location string) error {
return m.Execute(assignedCtx(), NewManagerClient(fs.Client), "UpdateView", &UpdateViewRequest{TableBucketARN: mustBucketARN(t),
Namespace: []string{"ns2"}, Name: "v", MetadataVersion: 2, MetadataLocation: location}, nil, "")
}
func TestUpdateViewAfterRenameAcrossNamespacesKeepsItsDirectory(t *testing.T) {
fs, m := renameViewAcrossNamespaces(t)
location := "s3://" + renameTestBucket + "/ns/v/metadata/v2.metadata.json"
require.NoError(t, updateViewAt(t, m, fs, location))
entry := fs.Get(GetNamespacePath(renameTestBucket, "ns2"), "v")
require.NotNil(t, entry)
var stored tableMetadataInternal
require.NoError(t, json.Unmarshal(entry.Extended[ExtendedKeyMetadata], &stored))
require.Equal(t, location, stored.MetadataLocation)
}
func TestUpdateViewAfterRenameAcrossNamespacesRefusesOtherDirectories(t *testing.T) {
fs, m := renameViewAcrossNamespaces(t)
err := updateViewAt(t, m, fs, "s3://"+renameTestBucket+"/ns/elsewhere/metadata/v2.metadata.json")
require.Error(t, err)
require.Contains(t, s3TablesErrorText(err), "must be within namespace ns2")
}
// The same-directory exception compares the directory the location names. A
// location whose directory is the bucket root (s3://bucket/metadata/...) names
// no table directory, so it must not match the root a record falls back to and
// must meet the namespace check, as it did before the exception existed.
func TestUpdateRefusesAMetadataDirectoryAtTheBucketRoot(t *testing.T) {
bucketRoot := "s3://" + renameTestBucket + "/metadata/v9.metadata.json"
ctx := context.Background()
t.Run("table created without a location", func(t *testing.T) {
fs, m := startRenameManager(t)
require.NoError(t, m.Execute(ctx, NewManagerClient(fs.Client), "CreateTable", &CreateTableRequest{TableBucketARN: mustBucketARN(t),
Namespace: []string{"ns"}, Name: "fresh", Format: "ICEBERG"}, nil, ""))
err := m.Execute(ctx, NewManagerClient(fs.Client), "UpdateTable", &UpdateTableRequest{TableBucketARN: mustBucketARN(t),
Namespace: []string{"ns"}, Name: "fresh", MetadataVersion: 2, MetadataLocation: bucketRoot}, nil, "")
require.Error(t, err)
require.Contains(t, s3TablesErrorText(err), "must be within namespace ns")
})
t.Run("table stored at its name path", func(t *testing.T) {
fs, m := startRenameManager(t)
err := updateTableAt(ctx, t, m, fs, "ns", bucketRoot)
require.Error(t, err)
require.Contains(t, s3TablesErrorText(err), "must be within namespace ns")
})
t.Run("view", func(t *testing.T) {
fs, m := startRenameManager(t)
seedView(t, fs, "v")
err := m.Execute(ctx, NewManagerClient(fs.Client), "UpdateView", &UpdateViewRequest{TableBucketARN: mustBucketARN(t),
Namespace: []string{"ns"}, Name: "v", MetadataVersion: 2, MetadataLocation: bucketRoot}, nil, "")
require.Error(t, err)
require.Contains(t, s3TablesErrorText(err), "must be within namespace ns")
})
}
// A table created without a location keeps its files in the warehouse directory
// it was given; its first commit writes there, also after a rename across
// namespaces.
func TestUpdateOfATableCreatedWithoutALocationWritesItsWarehouse(t *testing.T) {
ctx := context.Background()
fs, m := startRenameManager(t)
addRenameNamespace(t, fs, "ns2")
client := NewManagerClient(fs.Client)
require.NoError(t, m.Execute(ctx, client, "CreateTable", &CreateTableRequest{TableBucketARN: mustBucketARN(t),
Namespace: []string{"ns"}, Name: "fresh", Format: "ICEBERG"}, nil, ""))
created, err := runGetTable(t, m, fs, "ns", "fresh")
require.NoError(t, err)
require.NotEmpty(t, created.WarehouseLocation)
require.NoError(t, runRename(t, m, fs, &RenameTableRequest{TableBucketARN: mustBucketARN(t),
SourceNamespace: []string{"ns"}, SourceName: "fresh", DestNamespace: []string{"ns2"}, DestName: "fresh"}))
location := created.WarehouseLocation + "/metadata/v1.metadata.json"
require.NoError(t, m.Execute(ctx, client, "UpdateTable", &UpdateTableRequest{TableBucketARN: mustBucketARN(t),
Namespace: []string{"ns2"}, Name: "fresh", MetadataVersion: 1, MetadataLocation: location}, nil, ""))
got, err := runGetTable(t, m, fs, "ns2", "fresh")
require.NoError(t, err)
require.Equal(t, location, got.MetadataLocation)
}