laskoviymishka commented on code in PR #2354: URL: https://github.com/apache/iceberg-go/pull/2354#discussion_r4490606550
########## table/expire_replay.go: ########## @@ -0,0 +1,189 @@ +// 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 + +import ( + "context" + "fmt" + "slices" + + "github.com/apache/iceberg-go/io" +) + +type stagedReplay struct { + branch string + records []expirationRecord + + // Record positions stay fixed even when replanning changes the update count. + // Snapshots use their most recent rebuild. + updates []Update + + // Refs walked by expiries that produced updates. + walkedRefs []string + + // Updates from the last successful replay. + // ran distinguishes an empty replay from no replay. + landed []Update + ran bool +} + +func newStagedReplay(branch string, updates []Update, records []expirationRecord) *stagedReplay { + return &stagedReplay{ + branch: branch, + records: records, + updates: slices.Clone(updates), + walkedRefs: stagedWalkedRefs(records), + } +} + +// On error, replay returns partial updates and replaced lists for cleanup. +// The partial updates include a rebuilt snapshot that failed to apply. +func (r *stagedReplay) replay(ctx context.Context, fresh Metadata, fs io.WriteFileIO, attempt int, force bool) ([]Update, []string, error) { + b, err := MetadataBuilderFromBase(fresh, "") + if err != nil { + return nil, nil, err + } + + var ( + out []Update + orphaned []string + walkedRefs []string + records = r.records + chainRebuilt = force + ) + apply := func(updates ...Update) error { + for _, u := range updates { + if err := u.Apply(b); err != nil { + return fmt.Errorf("apply %s: %w", u.Action(), err) + } + out = append(out, u) + } + + return nil + } + + for i := 0; i <= len(r.updates); { + if len(records) > 0 && records[0].pos == i { + rec := records[0] + records = records[1:] + updates, walked, err := r.replan(b, rec) + if err != nil { + return out, orphaned, err + } + if err := apply(updates...); err != nil { + return out, orphaned, err + } + if len(updates) > 0 { + walkedRefs = append(walkedRefs, walked...) + } + i += rec.count + + continue + } + if i == len(r.updates) { + break + } + + if su, ok := r.updates[i].(*addSnapshotUpdate); ok && su.rebuildManifestList != nil { + // Sequence numbers and row IDs must include earlier replayed updates. + state, err := b.clone().Build() + if err != nil { + return out, orphaned, err + } + next, err := rebuildStagedSnapshot(ctx, su, state, b.currentSnapshotForRef(r.branch), fs, attempt, chainRebuilt) + if err != nil { + return out, orphaned, err + } + if next != su { + r.updates[i] = next + orphaned = append(orphaned, su.Snapshot.ManifestList) + chainRebuilt = true + } + } + if err := apply(r.updates[i]); err != nil { + return append(out, r.updates[i]), orphaned, fmt.Errorf("%w: staged update does not apply after refresh: %w", ErrTransactionUnusable, err) + } + i++ + } + r.walkedRefs = walkedRefs + r.landed, r.ran = out, true + + return out, orphaned, nil +} + +func (r *stagedReplay) replan(b *MetadataBuilder, rec expirationRecord) ([]Update, []string, error) { + if err := validateGCEnabledForSnapshotExpiration(b.props); err != nil { Review Comment: Should this check only fire when the replan has updates to apply? With an append plus a no-op `ExpireSnapshots`, a peer setting `gc.enabled=false` between attempts fails the retry with `ErrTransactionUnusable`. On `main` that expiry recorded nothing ([transaction.go#L917-L922](https://github.com/apache/iceberg-go/blob/c37ed1a8401c325fbc3291a04360a2686dd939d8/table/transaction.go#L917-L922)) and the append committed. Java checks GC only in the `RemoveSnapshots` constructor ([RemoveSnapshots.java#L84-L89](https://github.com/apache/iceberg/blob/6275107e38cc87a7d416ef231019ceb51556f196/core/src/main/java/org/apache/iceberg/RemoveSnapshots.java#L84-L89)). Checking after `planExpiration` when `len(updates) > 0` keeps removals guarded. ########## table/commit.go: ########## @@ -87,6 +88,7 @@ func (t *Transaction) TableCommit() (TableCommit, error) { // their updates stage no ref requirement. The helper returns a fresh // slice, so appending here cannot touch t.reqs. reqs := append(transactionRequirements(t.reqs, t.branch, t.tbl.metadata), AssertTableUUID(meta.uuid)) + reqs = append(reqs, expirationAssertions(t.tbl.metadata, cmp.Or(t.branch, MainBranch), stagedWalkedRefs(t.expirations), reqs)...) Review Comment: Could we add a small `TableCommit` test for a branch transaction that creates its branch and then calls `ExpireSnapshots`? As I read it, the commit branch is skipped here and keeps its single absence assertion from `transactionRequirements`, but nothing pins that. [`TestExpirationAssertionsCoverPlansWithUpdates`](https://github.com/apache/iceberg-go/blob/fc53481256230bd02e25de2ffcbdedbbbf9d0ea7/table/expire_replay_test.go#L519-L554) only uses main, and the [absent-branch case](https://github.com/apache/iceberg-go/blob/fc53481256230bd02e25de2ffcbdedbbbf9d0ea7/table/transaction_internal_test.go#L2288-L2300) has no expiry. -- 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]
