laskoviymishka commented on code in PR #2678:
URL: https://github.com/apache/iceberg-go/pull/2678#discussion_r4687997254


##########
table/snapshot_producers.go:
##########
@@ -172,7 +172,9 @@ func (fa *fastAppendFiles) parentDependentManifests(ctx 
context.Context, parent
 // resulting manifest list. Two concurrent fast-appends adding the
 // same file path is a writer-side error (paths are expected to be
 // unique, normally via UUID-stamped filenames); detecting it here
-// is out of Java parity scope.
+// is out of Java parity scope. AddDataFiles and AddDataFilesByPath,
+// which take caller-supplied paths, recheck them against the fresh
+// parent when a commit retry rebuilds the snapshot.

Review Comment:
   Could the comment say the retry recheck is iceberg-go-only? Right after "out 
of Java parity scope" it's unclear whether it has a Java counterpart. As I read 
it, Java's FastAppend/MergeAppend don't re-check added paths on retry 
([SnapshotProducer.java](https://github.com/apache/iceberg/blob/2114bf631e49af532d66e2ce148ee49dd1dd1f1f/core/src/main/java/org/apache/iceberg/SnapshotProducer.java#L241-L261)).



##########
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)
+
+       _, 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)))
+}
+
+// TestAddDataFiles_ConcurrentDuplicateRejected is the AddDataFiles
+// counterpart of TestAddDataFilesByPath_ConcurrentDuplicateRejected.
+func TestAddDataFiles_ConcurrentDuplicateRejected(t *testing.T) {
+       ctx := t.Context()
+       tbl, cat, imported := newAddFilesConflictTable(t, 2, "4")
+       df := newImportedDataFile(t, imported)
+
+       txB := tbl.NewTransaction()
+       require.NoError(t, txB.AddDataFiles(ctx, []iceberg.DataFile{df}, nil))
+
+       txA := tbl.NewTransaction()
+       require.NoError(t, txA.AddDataFiles(ctx, []iceberg.DataFile{df}, nil))
+       _, err := txA.Commit(ctx)
+       require.NoError(t, err)
+
+       _, err = txB.Commit(ctx)
+       require.ErrorIs(t, err, table.ErrTransactionUnusable)
+       require.ErrorIs(t, err, table.ErrDuplicateDataFiles)
+       require.Equal(t, []int64{0, 1, 2}, idsInTable(t, loadCurrent(t, cat)))
+}
+
+// TestAddDataFilesByPath_ConcurrentDistinctFilesCommit is the control: a
+// concurrent commit that added a different file must not trip the duplicate
+// check, so the replayed commit lands with both files.
+func TestAddDataFilesByPath_ConcurrentDistinctFilesCommit(t *testing.T) {
+       ctx := t.Context()
+       tbl, _, imported := newAddFilesConflictTable(t, 2, "4")
+
+       arrowSc, err := table.SchemaToArrowSchema(tbl.Schema(), nil, false, 
false)
+       require.NoError(t, err)
+       other := tbl.Location() + "/data/other.parquet"
+       writeParquetFile(t, other, arrowSc, `[{"id": 3, "data": "d"}]`)
+
+       txB := tbl.NewTransaction()
+       require.NoError(t, txB.AddDataFilesByPath(ctx, []string{other}, nil, 
false))
+
+       txA := tbl.NewTransaction()
+       require.NoError(t, txA.AddDataFilesByPath(ctx, []string{imported}, nil, 
false))
+       _, err = txA.Commit(ctx)
+       require.NoError(t, err)
+
+       committed, err := txB.Commit(ctx)
+       require.NoError(t, err)
+       require.Equal(t, []int64{0, 1, 2, 3}, idsInTable(t, committed))
+}
+
+// TestAddDataFilesSkippedDuplicateCheckAllowsConcurrentDuplicate pins the
+// opt-outs: ignoreDuplicates and WithoutDuplicateCheck disable the
+// commit-time check as well as the plan-time one.
+func TestAddDataFilesSkippedDuplicateCheckAllowsConcurrentDuplicate(t 
*testing.T) {
+       t.Run("AddDataFilesByPath", func(t *testing.T) {
+               ctx := t.Context()
+               tbl, _, imported := newAddFilesConflictTable(t, 2, "4")
+
+               txB := tbl.NewTransaction()
+               require.NoError(t, txB.AddDataFilesByPath(ctx, 
[]string{imported}, nil, true))
+
+               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.NoError(t, err)
+       })
+
+       t.Run("AddDataFiles", func(t *testing.T) {
+               ctx := t.Context()
+               tbl, _, imported := newAddFilesConflictTable(t, 2, "4")
+               df := newImportedDataFile(t, imported)
+
+               txB := tbl.NewTransaction()
+               require.NoError(t, txB.AddDataFiles(ctx, 
[]iceberg.DataFile{df}, nil, table.WithoutDuplicateCheck()))
+
+               txA := tbl.NewTransaction()
+               require.NoError(t, txA.AddDataFiles(ctx, 
[]iceberg.DataFile{df}, nil))
+               _, err := txA.Commit(ctx)
+               require.NoError(t, err)
+
+               _, err = txB.Commit(ctx)
+               require.NoError(t, err)
+       })
+}
+
+// TestAddDataFilesByPath_RecommitAfterFailedCommitRejected covers a caller
+// that retries by calling Commit again. With no retries configured, the first
+// Commit fails the stale branch assertion; the second resumes from a refresh
+// and must still reject the duplicate.
+func TestAddDataFilesByPath_RecommitAfterFailedCommitRejected(t *testing.T) {
+       ctx := t.Context()
+       tbl, cat, imported := newAddFilesConflictTable(t, 2, "0")
+
+       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.ErrCommitFailed)
+       require.NotErrorIs(t, err, table.ErrDuplicateDataFiles)
+
+       _, err = txB.Commit(ctx)
+       require.ErrorIs(t, err, table.ErrDuplicateDataFiles)
+       require.Equal(t, []int64{0, 1, 2}, idsInTable(t, loadCurrent(t, cat)))
+}
+
+// TestAddDataFilesByPath_ConcurrentRollbackCommits checks that a concurrent
+// rollback past the writer's base does not fail the commit: the retry
+// rebuilds on the rolled-back head, where the path is not present.
+func TestAddDataFilesByPath_ConcurrentRollbackCommits(t *testing.T) {
+       ctx := t.Context()
+       tbl, cat, imported := newAddFilesConflictTable(t, 2, "4")
+       seedID := tbl.CurrentSnapshot().SnapshotID
+
+       arrowSc, err := table.SchemaToArrowSchema(tbl.Schema(), nil, false, 
false)
+       require.NoError(t, err)
+       extra := tbl.Location() + "/data/extra.parquet"
+       writeParquetFile(t, extra, arrowSc, `[{"id": 9, "data": "z"}]`)
+       tx := tbl.NewTransaction()
+       require.NoError(t, tx.AddDataFilesByPath(ctx, []string{extra}, nil, 
false))
+       tbl, err = tx.Commit(ctx)
+       require.NoError(t, err)
+
+       txB := tbl.NewTransaction()
+       require.NoError(t, txB.AddDataFilesByPath(ctx, []string{imported}, nil, 
false))
+
+       rollback := loadCurrent(t, cat).NewTransaction()
+       require.NoError(t, rollback.RollbackToSnapshot(seedID))
+       _, err = rollback.Commit(ctx)
+       require.NoError(t, err)
+
+       _, err = txB.Commit(ctx)
+       require.NoError(t, err)
+       require.Equal(t, []int64{0, 1, 2}, idsInTable(t, loadCurrent(t, cat)))
+}
+
+// TestAddDataFilesByPath_ConcurrentExpiryCommits checks that expiring the
+// snapshots committed after the writer's base does not fail the commit.
+func TestAddDataFilesByPath_ConcurrentExpiryCommits(t *testing.T) {
+       ctx := t.Context()
+       tbl, cat, imported := newAddFilesConflictTable(t, 2, "4")
+
+       arrowSc, err := table.SchemaToArrowSchema(tbl.Schema(), nil, false, 
false)
+       require.NoError(t, err)
+
+       txB := tbl.NewTransaction()
+       require.NoError(t, txB.AddDataFilesByPath(ctx, []string{imported}, nil, 
false))
+
+       for i, name := range []string{"x", "y"} {
+               path := tbl.Location() + "/data/" + name + ".parquet"
+               writeParquetFile(t, path, arrowSc, `[{"id": 
`+strconv.Itoa(3+i)+`, "data": "q"}]`)
+               tx := loadCurrent(t, cat).NewTransaction()
+               require.NoError(t, tx.AddDataFilesByPath(ctx, []string{path}, 
nil, false))
+               _, err := tx.Commit(ctx)
+               require.NoError(t, err)
+       }
+
+       expire := loadCurrent(t, cat).NewTransaction()
+       require.NoError(t, expire.ExpireSnapshots(table.WithOlderThan(0), 
table.WithRetainLast(1)))
+       _, err = expire.Commit(ctx)
+       require.NoError(t, err)
+       require.Len(t, loadCurrent(t, cat).Metadata().Snapshots(), 1)
+
+       _, err = txB.Commit(ctx)

Review Comment:
   Could we add the duplicate case next to this test: txA commits `imported`, 
the planned snapshot is expired, then `txB.Commit`? With `checkedManifests` 
forced to nil, this test and `TestAddDataFilesByPath_ConcurrentRollbackCommits` 
still pass, since `imported` is never committed, so neither covers a duplicate 
across history changes. That variant gets `ErrDuplicateDataFiles` at HEAD, so 
this is coverage only.



##########
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:
   Could we assert here that txA's commit actually merged the manifests, e.g. 
the current snapshot has one manifest whose path differs from the planned one? 
With logging added, the merge does happen at HEAD (planned manifests=1, current 
manifests=1, different paths), but nothing pins it, so a change to the merge 
defaults would quietly turn this into the fast-append case.



-- 
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]

Reply via email to