akritishakya commented on code in PR #2678: URL: https://github.com/apache/iceberg-go/pull/2678#discussion_r4693224681
########## table/add_files_conflict_test.go: ########## @@ -0,0 +1,330 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package table_test + +import ( + "strconv" + "testing" + + "github.com/apache/iceberg-go" + "github.com/apache/iceberg-go/table" + "github.com/stretchr/testify/require" +) + +// newAddFilesConflictTable builds a table, seeded with one data file holding +// id 0, whose catalog checks commit requirements, so a writer that loses the +// race refreshes and replays. It returns the table and the path of a parquet +// file holding ids 1 and 2 that is not yet in the table. +func newAddFilesConflictTable(t *testing.T, version int, numRetries string) (*table.Table, *concurrentTestCatalog, string) { + t.Helper() + + tbl, cat := newConcurrentRewriteTestTableVersion(t, version) + + arrowSc, err := table.SchemaToArrowSchema(tbl.Schema(), nil, false, false) + require.NoError(t, err) + + location := tbl.Location() + seed := location + "/data/a.parquet" + writeParquetFile(t, seed, arrowSc, `[{"id": 0, "data": "a"}]`) + tx := tbl.NewTransaction() + require.NoError(t, tx.SetProperties(iceberg.Properties{table.CommitNumRetriesKey: numRetries})) + require.NoError(t, tx.AddDataFilesByPath(t.Context(), []string{seed}, nil, false)) + tbl, err = tx.Commit(t.Context()) + require.NoError(t, err) + + imported := location + "/data/imported.parquet" + writeParquetFile(t, imported, arrowSc, `[{"id": 1, "data": "b"}, {"id": 2, "data": "c"}]`) + + return tbl, cat, imported +} + +// loadCurrent returns the table as the catalog currently has it. +func loadCurrent(t *testing.T, cat *concurrentTestCatalog) *table.Table { + t.Helper() + + tbl, err := cat.LoadTable(t.Context(), table.Identifier{"db", "concurrent_rewrite"}) + require.NoError(t, err) + + return tbl +} + +// newImportedDataFile describes the parquet file at path as an +// iceberg.DataFile, the input that AddDataFiles takes. +func newImportedDataFile(t *testing.T, path string) iceberg.DataFile { + t.Helper() + + b, err := iceberg.NewDataFileBuilder( + *iceberg.UnpartitionedSpec, iceberg.EntryContentData, + path, iceberg.ParquetFile, nil, nil, nil, 2, mustFileSize(t, path)) + require.NoError(t, err) + + return b.Build() +} + +// TestAddDataFilesByPath_ConcurrentDuplicateRejected proves the duplicate +// check covers files a concurrent commit added. Both writers plan against +// the same snapshot, so both pass the plan-time check; without the +// commit-time check, the loser's retry would add the file a second time +// and its rows would be read twice. +func TestAddDataFilesByPath_ConcurrentDuplicateRejected(t *testing.T) { + for _, version := range []int{2, 3} { + t.Run("v"+strconv.Itoa(version), func(t *testing.T) { + ctx := t.Context() + tbl, cat, imported := newAddFilesConflictTable(t, version, "4") + + txB := tbl.NewTransaction() + require.NoError(t, txB.AddDataFilesByPath(ctx, []string{imported}, nil, false)) + + txA := tbl.NewTransaction() + require.NoError(t, txA.AddDataFilesByPath(ctx, []string{imported}, nil, false)) + _, err := txA.Commit(ctx) + require.NoError(t, err) + + _, err = txB.Commit(ctx) + require.ErrorIs(t, err, table.ErrDuplicateDataFiles) + require.ErrorIs(t, err, table.ErrTransactionUnusable) + require.Equal(t, []int64{0, 1, 2}, idsInTable(t, loadCurrent(t, cat))) + }) + } +} + +// TestAddDataFilesByPath_ConcurrentDuplicateRejectedWithManifestMerge covers +// the merge append producer. Writer A's commit merges the table's manifests +// into a new manifest, so the retry must read it and reject the duplicate. +func TestAddDataFilesByPath_ConcurrentDuplicateRejectedWithManifestMerge(t *testing.T) { + ctx := t.Context() + tbl, cat, imported := newAddFilesConflictTable(t, 2, "4") + + tx := tbl.NewTransaction() + require.NoError(t, tx.SetProperties(iceberg.Properties{ + table.ManifestMergeEnabledKey: "true", + table.ManifestMinMergeCountKey: "2", + })) + tbl, err := tx.Commit(ctx) + require.NoError(t, err) + + txB := tbl.NewTransaction() + require.NoError(t, txB.AddDataFilesByPath(ctx, []string{imported}, nil, false)) + + txA := tbl.NewTransaction() + require.NoError(t, txA.AddDataFilesByPath(ctx, []string{imported}, nil, false)) + _, err = txA.Commit(ctx) + require.NoError(t, err) Review Comment: The test now checks that writer A's commit leaves one manifest with a different path from the planned one, so it will fail if the merge stops happening. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
