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


##########
table/transaction_internal_test.go:
##########
@@ -2526,3 +2526,56 @@ func 
TestTransactionStagedTableConcurrentWithExpireSnapshots(t *testing.T) {
        })
        wg.Wait()
 }
+
+// Why: Scan read the transaction's metadata builder without the transaction
+// lock while apply swaps it, a data race under -race.
+// Condition: Scan and SetProperties run concurrently on one transaction.
+// Assertion: both succeed with no race reported, and the final scan sees
+// every property.
+func TestTransactionScanConcurrentWithSetProperties(t *testing.T) {
+       txn := createTestTransaction(t, iceio.LocalFS{}, 
*iceberg.UnpartitionedSpec)
+
+       const n = 50
+       var wg sync.WaitGroup
+       wg.Go(func() {
+               for i := range n {
+                       assert.NoError(t, 
txn.SetProperties(iceberg.Properties{fmt.Sprintf("k%d", i): "v"}))
+               }
+       })
+       wg.Go(func() {
+               for range n {
+                       _, err := txn.Scan()
+                       assert.NoError(t, err)
+               }
+       })
+       wg.Wait()
+
+       scan, err := txn.Scan()
+       require.NoError(t, err)
+       for i := range n {
+               assert.Equal(t, "v", 
scan.metadata.Properties()[fmt.Sprintf("k%d", i)])
+       }
+}
+
+// Why: ExpireSnapshots swaps t.tbl under the transaction lock, and Scan read
+// t.tbl's fields without it.
+// Condition: Scan and ExpireSnapshots run concurrently on one transaction.
+// Assertion: both succeed with no race reported.
+func TestTransactionScanConcurrentWithExpireSnapshots(t *testing.T) {
+       txn := createTestTransaction(t, iceio.LocalFS{}, 
*iceberg.UnpartitionedSpec)

Review Comment:
   Could we add a case with `snapshotHistoryRefsOnly` set, so `ExpireSnapshots` 
actually swaps in a different table through `loadExpirationHistory`? 
`createTestTransaction` has no snapshots, so here [`t.tbl = 
base`](https://github.com/apache/iceberg-go/blob/1eb003862a3889df938fd942829f30974a8a607a/table/transaction.go#L846-L873)
 rewrites the same pointer. `-race` still flags that store on `main`, so this 
is extra coverage rather than a hole.



##########
table/transaction_internal_test.go:
##########
@@ -2526,3 +2526,56 @@ func 
TestTransactionStagedTableConcurrentWithExpireSnapshots(t *testing.T) {
        })
        wg.Wait()
 }
+
+// Why: Scan read the transaction's metadata builder without the transaction
+// lock while apply swaps it, a data race under -race.
+// Condition: Scan and SetProperties run concurrently on one transaction.
+// Assertion: both succeed with no race reported, and the final scan sees
+// every property.
+func TestTransactionScanConcurrentWithSetProperties(t *testing.T) {
+       txn := createTestTransaction(t, iceio.LocalFS{}, 
*iceberg.UnpartitionedSpec)
+
+       const n = 50
+       var wg sync.WaitGroup
+       wg.Go(func() {
+               for i := range n {
+                       assert.NoError(t, 
txn.SetProperties(iceberg.Properties{fmt.Sprintf("k%d", i): "v"}))
+               }
+       })
+       wg.Go(func() {
+               for range n {
+                       _, err := txn.Scan()
+                       assert.NoError(t, err)
+               }
+       })
+       wg.Wait()
+
+       scan, err := txn.Scan()
+       require.NoError(t, err)
+       for i := range n {
+               assert.Equal(t, "v", 
scan.metadata.Properties()[fmt.Sprintf("k%d", i)])
+       }
+}
+
+// Why: ExpireSnapshots swaps t.tbl under the transaction lock, and Scan read
+// t.tbl's fields without it.
+// Condition: Scan and ExpireSnapshots run concurrently on one transaction.
+// Assertion: both succeed with no race reported.
+func TestTransactionScanConcurrentWithExpireSnapshots(t *testing.T) {
+       txn := createTestTransaction(t, iceio.LocalFS{}, 
*iceberg.UnpartitionedSpec)
+
+       const n = 3000

Review Comment:
   Would a start barrier (a channel both goroutines wait on) make the overlap 
less dependent on scheduling? With `main`'s `transaction.go` and 
`GOMAXPROCS=1`, this test caught the race in 10 of 15 runs; at `GOMAXPROCS>=2` 
it was 15 of 15, and the `SetProperties` test caught it every time.



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