Files
seaweedfs/weed/s3api/iceberg/iceberg_create_table_test.go
T
edbcc718d3 iceberg: read remote-only metadata files, and fail a table whose metadata cannot be read (#13134)
* iceberg: read remote-only metadata files, and fail a table whose metadata cannot be read

## Problem

In a remote-mounted bucket (`remote.mount` + `filer.remote.sync`), a table's `metadata.json` that is not cached locally is a filer entry holding only its `RemoteEntry`: no inline content, no chunks. The Iceberg REST catalog read metadata files as inline content (and, for a register, chunks), so such a file read as empty bytes, and an empty read was then treated as "no metadata":

- a table stored with its metadata location alone (registered, or created through the S3 Tables API) loaded as an empty table: no schema, no snapshots, and clients read no rows;
- a commit to it was refused on a table UUID mismatch for ever, and a commit without requirements replaced the table's history with an empty one;
- registering such a file failed with `unexpected end of JSON input`.

The same empty stand-ins were served when the file was gone or unreadable, and a synthetic empty table when stored metadata did not parse, with the same consequences.

## Fix

- `loadMetadataFile` (and through it `currentMetadataBytes` and RegisterTable) reads a remote-only file of a mounted bucket: its remote size is checked against a 64 MiB bound before anything is fetched, it is cached into the cluster with `CacheRemoteObjectToLocalCluster` (one retry: a cache can race an uncache or a write-back), then inline content or chunks are read.
- An empty file is `errEmptyMetadataFile`. A staged create's empty placeholder is still treated as absent where the staged commit reads it.
- A location whose file is gone or unreadable, or stored metadata that does not parse, fails load, commit and transaction instead of serving an empty stand-in a writer could commit over. A table created without a location (no metadata yet) still loads as a new empty table, and a catalog-only record (Lance) is never read as Iceberg.
- RegisterTable refuses, before anything is read, a metadata location that is not a file directly in a table's `metadata/` directory (a deeper path was read as the same-named file there), and refuses metadata whose own `location` is not the table location it is registered from (a file copied from another table names that table's files).
- The table UUID a commit or a transaction stores is the base metadata's, not a record's that disagrees with it.

`loadTableMetadataBlob` (used only by the manifest repair) is unchanged.

## Tests

- New `iceberg_register_load_test.go` (inline and remote-only files; a location-only table loads from its file and fails with the file gone; commits and transactions keep the history, and fail leaving the table unchanged when the file is gone; oversized remote files and nested locations refused) and `TestACommitStoresTheBaseMetadatasUUID`. Each rule is red with it undone, except the refusal of an entry still remote-only after two cache calls, which fails closed anyway (it reads as an empty file).
- Adapted: the fixtures of `iceberg_register_table_test.go` (`seedRegistrableMetadata`, `seedOversizedMetadata`) give the metadata the table's own location; `TestLoadTableResultOmitsConfigForCredentialVendingClients` and `seedOwnedTable` store the table with its metadata; `TestBuildLoadTableResultNeverReturnsNilMetadata` now expects an error for a location whose file cannot be read and for unparseable metadata, and keeps the empty-metadata and no-location cases.
- `go vet ./weed/s3api/iceberg/`, `go test -race ./weed/s3api/iceberg/` pass, and `weed` builds.

No unit test reads a chunked `metadata.json` (the in-memory filer has no volume servers); that path was checked against a real cluster with a remote-mounted bucket.

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

* iceberg: refuse commits to catalog-only tables

A Lance table loads as an empty Iceberg table, and a commit built on that
placeholder wrote Iceberg metadata and repointed the record's location
(the Lance dataset root) to it. Both commit paths now answer 400.

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

* iceberg: pass the metadata size limit to the remote cache request

The size check ran on the entry the lookup returned. A remote refresh
between the lookup and the cache call could swap in a larger object, which
the filer then downloaded whole. With MaxSize the filer checks the entry
it actually downloads.

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

* iceberg: apply the metadata size limit to inline content

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-11-30 10:36:13 -08:00

403 lines
15 KiB
Go

package iceberg
import (
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/apache/iceberg-go"
"github.com/apache/iceberg-go/table"
"github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3tables"
)
func TestValidateCreateTableRequestRequiresName(t *testing.T) {
err := validateCreateTableRequest(CreateTableRequest{})
if !errors.Is(err, errTableNameRequired) {
t.Fatalf("validateCreateTableRequest() error = %v, want errTableNameRequired", err)
}
}
func TestValidateCreateTableRequestAcceptsWithName(t *testing.T) {
err := validateCreateTableRequest(CreateTableRequest{Name: "orders"})
if err != nil {
t.Fatalf("validateCreateTableRequest() error = %v, want nil", err)
}
}
func TestIsStageCreateEnabledDefaultsToTrue(t *testing.T) {
t.Setenv("ICEBERG_ENABLE_STAGE_CREATE", "")
if !isStageCreateEnabled() {
t.Fatalf("isStageCreateEnabled() = false, want true")
}
}
func TestIsStageCreateEnabledFalseValues(t *testing.T) {
falseValues := []string{"0", "false", "FALSE", "no", "off"}
for _, value := range falseValues {
t.Setenv("ICEBERG_ENABLE_STAGE_CREATE", value)
if isStageCreateEnabled() {
t.Fatalf("isStageCreateEnabled() = true for value %q, want false", value)
}
}
}
func mustParseSchema(t *testing.T, raw string) *iceberg.Schema {
t.Helper()
var schema iceberg.Schema
if err := json.Unmarshal([]byte(raw), &schema); err != nil {
t.Fatalf("parse schema: %v", err)
}
return &schema
}
const variantSchema = `{"type":"struct","schema-id":0,"fields":[
{"id":1,"name":"id","required":true,"type":"long"},
{"id":2,"name":"payload","required":false,"type":"variant"}]}`
// A v3-only column type without format-version 3 is the client's mistake, and
// the reason has to travel back to them rather than only into the server log.
func TestNewTableMetadataRejectsV3TypeBelowV3(t *testing.T) {
_, err := newTableMetadata(uuid.New(), "s3://bkt/ns/t", mustParseSchema(t, variantSchema), nil, nil, nil)
if err == nil {
t.Fatal("newTableMetadata() error = nil, want invalid schema")
}
if !errors.Is(err, iceberg.ErrInvalidSchema) {
t.Fatalf("newTableMetadata() error = %v, want ErrInvalidSchema", err)
}
// writeManagerError turns this into a 400; see TestWriteManagerError.
if !strings.Contains(err.Error(), "variant is not supported until v3") {
t.Errorf("error = %q, want the underlying reason", err.Error())
}
}
func TestNewTableMetadataAcceptsV3TypeAtV3(t *testing.T) {
metadata, err := newTableMetadata(uuid.New(), "s3://bkt/ns/t", mustParseSchema(t, variantSchema), nil, nil,
iceberg.Properties{"format-version": "3"})
if err != nil {
t.Fatalf("newTableMetadata() error = %v, want nil", err)
}
if got := metadata.Version(); got != 3 {
t.Fatalf("metadata.Version() = %d, want 3", got)
}
if _, found := metadata.CurrentSchema().FindFieldByName("payload"); !found {
t.Error("variant field missing from stored schema")
}
}
// A LoadTable response must never carry nil metadata: it serializes as
// "metadata":null under HTTP 200, which no Iceberg client can parse.
func TestBuildLoadTableResultNeverReturnsNilMetadata(t *testing.T) {
// A table stored with its location alone is served from the file there
// (TestRegisteredTableLoadsWithItsMetadata); when that cannot be read the
// load fails rather than serving an empty table.
r := httptest.NewRequest(http.MethodGet, "/v1/namespaces/ns/tables/t", nil)
if _, err := (&Server{filerClient: newMemFiler()}).buildLoadTableResult(r, s3tables.GetTableResponse{MetadataLocation: "s3://bkt/ns/t/metadata/v1.metadata.json"}, "bkt", []string{"ns"}, "t"); err == nil {
t.Error("a location whose metadata cannot be read: want an error, not an empty table")
}
// stored metadata that does not parse fails the load rather than serving an empty table
if _, err := (&Server{filerClient: newMemFiler()}).buildLoadTableResult(r, s3tables.GetTableResponse{Metadata: &s3tables.TableMetadata{FullMetadata: json.RawMessage(`{"nope":`)}}, "bkt", []string{"ns"}, "t"); err == nil {
t.Error("unparseable stored metadata: want an error, not an empty table")
}
cases := map[string]s3tables.GetTableResponse{
"empty full metadata": {Metadata: &s3tables.TableMetadata{}},
"no location": {},
}
for name, getResp := range cases {
t.Run(name, func(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "/v1/namespaces/ns/tables/t", nil)
// The filer is empty, so a record holding only a location has no
// file behind it to serve.
result, err := (&Server{filerClient: newMemFiler()}).buildLoadTableResult(r, getResp, "bkt", []string{"ns"}, "t")
if err != nil {
t.Fatalf("buildLoadTableResult() error = %v, want nil", err)
}
if result.Metadata == nil {
t.Fatal("buildLoadTableResult() returned nil metadata with no error")
}
})
}
}
func newCreateTableRequest(t *testing.T, bucket, namespace, body, identity string) *http.Request {
t.Helper()
r := httptest.NewRequest(http.MethodPost, "/v1/"+bucket+"/namespaces/"+namespace+"/tables", strings.NewReader(body))
r = mux.SetURLVars(r, map[string]string{"prefix": bucket, "namespace": namespace})
return r.WithContext(s3_constants.SetIdentityNameInContext(r.Context(), identity))
}
// A caller who may not create the table must be refused before anything is
// written: both branches of CreateTable write to the bucket, and stage-create
// never reaches the registration that carries the authorization.
func TestCreateTableDeniedBeforeAnyWrite(t *testing.T) {
const bucket = "warehouse"
for name, body := range map[string]string{
"stage-create": `{"name":"quarterly_reports","stage-create":true}`,
"create": `{"name":"quarterly_reports"}`,
} {
t.Run(name, func(t *testing.T) {
fc := newMemFiler()
seedNamespace(fc, bucket, "finance", "alice")
s := NewServer(fc, nil)
w := httptest.NewRecorder()
s.handleCreateTable(w, newCreateTableRequest(t, bucket, "finance", body, "mallory"))
if w.Code != http.StatusForbidden {
t.Errorf("status = %d, want %d (body: %s)", w.Code, http.StatusForbidden, w.Body.String())
}
for p, entry := range fc.entries {
if !entry.IsDirectory && strings.Contains(p, "quarterly_reports") {
t.Errorf("unauthorized caller wrote %s", p)
}
}
})
}
}
// The namespace owner still gets the staged metadata and marker stage-create
// exists to leave behind.
func TestStageCreateWritesStagedFilesForOwner(t *testing.T) {
const bucket = "warehouse"
fc := newMemFiler()
seedNamespace(fc, bucket, "finance", "alice")
s := NewServer(fc, nil)
w := httptest.NewRecorder()
s.handleCreateTable(w, newCreateTableRequest(t, bucket, "finance", `{"name":"quarterly_reports","stage-create":true}`, "alice"))
if w.Code != http.StatusOK {
t.Fatalf("status = %d, want %d (body: %s)", w.Code, http.StatusOK, w.Body.String())
}
staged := 0
for p, entry := range fc.entries {
if !entry.IsDirectory && strings.Contains(p, stageCreateMarkerDirName) {
staged++
}
}
if staged != 2 {
t.Fatalf("staged files = %d, want the metadata file and its marker", staged)
}
if _, ok := fc.entries[s3tables.GetTablePath(bucket, "finance", "quarterly_reports")]; ok {
t.Error("stage-create registered the table")
}
}
// A caller allowed by the actions on its own identity, rather than by ownership
// or a resource policy, has to survive the hop into the s3tables manager: the
// gate must not refuse what the create behind it would allow.
func TestCreateTableAllowedByIdentityActions(t *testing.T) {
const bucket = "warehouse"
fc := newMemFiler()
seedNamespace(fc, bucket, "finance", "alice")
s := NewServer(fc, nil)
r := newCreateTableRequest(t, bucket, "finance", `{"name":"quarterly_reports","stage-create":true}`, "mallory")
r = r.WithContext(s3_constants.SetIdentityInContext(r.Context(), &struct {
Name string
Actions []string
}{Name: "mallory", Actions: []string{"s3tables:CreateTable"}}))
w := httptest.NewRecorder()
s.handleCreateTable(w, r)
if w.Code != http.StatusOK {
t.Fatalf("status = %d, want %d (body: %s)", w.Code, http.StatusOK, w.Body.String())
}
}
// Apache Spark numbers the columns of a new table from 0
// and leaves assigning fresh IDs to the catalog. This is the schema Spark
// sent for CREATE TABLE t (id BIGINT, v STRING, ts TIMESTAMP).
const zeroBasedSchema = `{"type":"struct","schema-id":0,"fields":[
{"id":0,"name":"id","required":false,"type":"long"},
{"id":1,"name":"v","required":false,"type":"string"},
{"id":2,"name":"ts","required":false,"type":"timestamptz"}]}`
// The same table as PyIceberg sends it, numbered from 1.
const oneBasedSchema = `{"type":"struct","schema-id":0,"fields":[
{"id":1,"name":"id","required":false,"type":"long"},
{"id":2,"name":"v","required":false,"type":"string"},
{"id":3,"name":"ts","required":false,"type":"timestamptz"}]}`
func createTableBody(name, schema, spec, order string) string {
parts := []string{`"name":"` + name + `"`, `"schema":` + schema}
if spec != "" {
parts = append(parts, `"partition-spec":`+spec)
}
if order != "" {
parts = append(parts, `"write-order":`+order)
}
return "{" + strings.Join(parts, ",") + "}"
}
type partitionWant struct{ name, transform, column string }
type sourceIDCase struct {
name string
// spec and order reference the zero-based schema; oneBasedSpec and
// oneBasedOrder are the same definitions against oneBasedSchema.
spec, order, oneBasedSpec, oneBasedOrder string
wantPartitions []partitionWant
wantSortColumns []string
}
var sourceIDCases = []sourceIDCase{
{
// The body Spark sent for PARTITIONED BY (bucket(4, id)).
name: "bucket on the first column",
spec: `{"spec-id":0,"fields":[{"name":"id_bucket","transform":"bucket[4]","source-id":0,"field-id":1000}]}`,
oneBasedSpec: `{"spec-id":0,"fields":[{"name":"id_bucket","transform":"bucket[4]","source-id":1,"field-id":1000}]}`,
wantPartitions: []partitionWant{{"id_bucket", "bucket[4]", "id"}},
},
{
name: "identity on the first column and day on another",
spec: `{"spec-id":0,"fields":[{"name":"id","transform":"identity","source-id":0,"field-id":1000},
{"name":"ts_day","transform":"day","source-id":2,"field-id":1001}]}`,
oneBasedSpec: `{"spec-id":0,"fields":[{"name":"id","transform":"identity","source-id":1,"field-id":1000},
{"name":"ts_day","transform":"day","source-id":3,"field-id":1001}]}`,
wantPartitions: []partitionWant{{"id", "identity", "id"}, {"ts_day", "day", "ts"}},
},
{
name: "write order on the first column",
order: `{"order-id":1,"fields":[{"source-id":0,"transform":"identity","direction":"asc","null-order":"nulls-first"}]}`,
oneBasedOrder: `{"order-id":1,"fields":[{"source-id":1,"transform":"identity","direction":"asc","null-order":"nulls-first"}]}`,
wantSortColumns: []string{"id"},
},
}
func metadataFromBody(t *testing.T, body string) table.Metadata {
t.Helper()
var req CreateTableRequest
if err := json.Unmarshal([]byte(body), &req); err != nil {
t.Fatalf("decode create-table request: %v", err)
}
metadata, err := newTableMetadata(uuid.New(), "s3://bkt/ns/t", req.Schema, req.partitionSpec(), req.writeOrder(), req.Properties)
if err != nil {
t.Fatalf("newTableMetadata() error = %v", err)
}
return metadata
}
func checkResolvedSourceIDs(t *testing.T, metadata table.Metadata, tc sourceIDCase) {
t.Helper()
schema := metadata.CurrentSchema()
if id, ok := schema.FindFieldByName("id"); !ok || id.ID != 1 {
t.Errorf("first column id = %+v, want fresh field ID 1", id)
}
spec := metadata.PartitionSpec()
var got []partitionWant
for _, field := range spec.Fields() {
column, _ := schema.FindColumnName(field.SourceID())
got = append(got, partitionWant{field.Name, field.Transform.String(), column})
}
if len(got) != len(tc.wantPartitions) {
t.Fatalf("partition fields = %+v, want %+v", got, tc.wantPartitions)
}
for i := range got {
if got[i] != tc.wantPartitions[i] {
t.Errorf("partition field %d = %+v, want %+v", i, got[i], tc.wantPartitions[i])
}
}
var sortColumns []string
for _, field := range metadata.SortOrder().Fields() {
column, _ := schema.FindColumnName(field.SourceIDs[0])
sortColumns = append(sortColumns, column)
}
if strings.Join(sortColumns, ",") != strings.Join(tc.wantSortColumns, ",") {
t.Errorf("sort columns = %v, want %v", sortColumns, tc.wantSortColumns)
}
}
// A bound PartitionSpec or SortOrder refuses source-id 0, which rejected every
// Spark CREATE TABLE partitioned or sorted by its first column. newTableMetadata
// resolves the client's placeholders through the request schema, so a
// zero-based request yields the same table as the one-based request PyIceberg
// sends, which the bound types always accepted.
func TestNewTableMetadataResolvesZeroBasedSourceIDs(t *testing.T) {
marshal := func(t *testing.T, metadata table.Metadata) string {
t.Helper()
spec := metadata.PartitionSpec()
out, err := json.Marshal([]any{metadata.CurrentSchema(), &spec, metadata.SortOrder()})
if err != nil {
t.Fatalf("marshal metadata parts: %v", err)
}
return string(out)
}
for _, tc := range sourceIDCases {
t.Run(tc.name, func(t *testing.T) {
zero := metadataFromBody(t, createTableBody("t", zeroBasedSchema, tc.spec, tc.order))
checkResolvedSourceIDs(t, zero, tc)
one := metadataFromBody(t, createTableBody("t", oneBasedSchema, tc.oneBasedSpec, tc.oneBasedOrder))
if got, want := marshal(t, zero), marshal(t, one); got != want {
t.Errorf("zero-based request produced\n%s\nwant (one-based)\n%s", got, want)
}
})
}
}
// Through the handler, the request Spark sends is accepted and answered with
// the resolved spec.
func TestCreateTablePartitionedByFirstColumn(t *testing.T) {
const bucket = "warehouse"
tc := sourceIDCases[1]
fc := newMemFiler()
seedNamespace(fc, bucket, "finance", "alice")
s := NewServer(fc, nil)
w := httptest.NewRecorder()
body := createTableBody("events", zeroBasedSchema, tc.spec, "")
s.handleCreateTable(w, newCreateTableRequest(t, bucket, "finance", body, "alice"))
if w.Code != http.StatusOK {
t.Fatalf("status = %d, want %d (body: %s)", w.Code, http.StatusOK, w.Body.String())
}
var result struct {
Metadata json.RawMessage `json:"metadata"`
}
if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil {
t.Fatalf("decode response: %v", err)
}
metadata, err := table.ParseMetadataBytes(result.Metadata)
if err != nil {
t.Fatalf("parse response metadata: %v", err)
}
checkResolvedSourceIDs(t, metadata, tc)
}
// A spec or order naming a column the request schema lacks is the client's
// error: 400, not 500.
func TestCreateTableRejectsUnresolvableSourceID(t *testing.T) {
const bucket = "warehouse"
for name, body := range map[string]string{
"partition spec": createTableBody("orders", zeroBasedSchema,
`{"spec-id":0,"fields":[{"name":"x","transform":"identity","source-id":42,"field-id":1000}]}`, ""),
"write order": createTableBody("orders", zeroBasedSchema, "",
`{"order-id":1,"fields":[{"source-id":42,"transform":"identity","direction":"asc","null-order":"nulls-first"}]}`),
} {
t.Run(name, func(t *testing.T) {
fc := newMemFiler()
seedNamespace(fc, bucket, "finance", "alice")
s := NewServer(fc, nil)
w := httptest.NewRecorder()
s.handleCreateTable(w, newCreateTableRequest(t, bucket, "finance", body, "alice"))
if w.Code != http.StatusBadRequest {
t.Fatalf("status = %d, want %d (body: %s)", w.Code, http.StatusBadRequest, w.Body.String())
}
if !strings.Contains(w.Body.String(), "BadRequestException") {
t.Errorf("body = %s, want a BadRequestException", w.Body.String())
}
})
}
}