Repository navigation
fix(view): stop MetadataBuilder.Build from growing the version log - #2109
Conversation
view.MetadataBuilder.Build appended the pending history entry to b.versionLog, so building the same builder again added the entry again: three builds gave three version-log entries. Append to a copy instead. On the table side, updateSnapshotLog wrote the pruned log back into b.snapshotLog. Running it again gives the same result, so this did not change any output, but it was still a side effect of Build. It now returns the pruned log (as updatedSnapshotLog) and leaves the builder unchanged, like the metadata log since #2095. lastUpdatedMS stays cached on the builder after the first Build, so repeated builds report the same timestamp; that is now documented. Closes #2108 Signed-off-by: Animesh Kumar <[email protected]>
laskoviymishka
left a comment
There was a problem hiding this comment.
The versionLog/snapshotLog growth fix itself is right — cloning before the append and returning the pruned log instead of writing it back to the builder is the correct shape, and it lines up with how metadataLog is already handled.
What I'd settle before merging is the contract this PR is implicitly introducing. The new doc says Build is free of side effects and that repeated builds are stable, but that isn't true yet: lastUpdatedMS is still cached on the builder (documented, but it's exactly the kind of side effect the comment disclaims), and on the view path buildMetadata regenerates uuid.New() on every build, so repeated builds of the same view are provably not stable. There's also the mutate-then-build-again case where versionHistoryEntry is overwritten and the first build's entry drops out of the log.
So the real question is whether Build is meant to be idempotent/repeatable, or whether a builder is single-shot. If idempotent, the UUID and timestamp need to follow the same rule the versionLog now does; if single-shot, I'd just document that and the fix is arguably more than it needs to be. Either is fine, but it should be explicit — and the tests should pin the branches this PR actually changed (the table pruning path, the view expiry path), which right now only the no-op paths cover.
| } | ||
|
|
||
| // The timestamp is fixed by the first Build and kept on the builder on purpose, so | ||
| // repeated builds of the same changes report the same last-updated time. |
There was a problem hiding this comment.
I'd either compute lastUpdatedMS into a local here instead of caching it on the builder, or keep the cache and have the comment say the builder is single-shot once built. As written it's the one side effect Build still keeps, and it contradicts the updatedSnapshotLog doc that now says Build is side-effect-free: build, add a snapshot, build again reuses the first timestamp, which can end up earlier than the snapshot it just added.
There was a problem hiding this comment.
Keep it cached. The failure described in the thread doesn't happen. addSnapshotInternal and SetSnapshotRef on main both overwrite b.lastUpdatedMS, so build → AddSnapshot at t1+5s → SetSnapshotRef(main) → build reports t1+5s. Only changes that neither add a snapshot nor move main reuse the first build's timestamp, and that only affects staged metadata. Commits rebuild from base through UpdateTableMetadata, and its MetadataBuilderFromBase starts at lastUpdatedMS = 0. Java's TableMetadata.Builder.build() caches it the same way.
I'd still reword the comment so it says when this is set:
| // repeated builds of the same changes report the same last-updated time. | |
| // If no change has set lastUpdatedMS yet, the first Build sets it and keeps it on | |
| // the builder on purpose (Java's TableMetadata.Builder does the same), so repeated | |
| // builds of the same changes report the same time. Adding a snapshot or moving | |
| // main after a Build still updates it. |
Drafted with an AI-assisted tool; confirmed by a maintainer.
| }, "expected snapshot to match added snapshot") | ||
| require.True(t, res.CurrentSnapshot().Equals(snapshot2)) | ||
|
|
||
| require.Len(t, builder.snapshotLog, 2, "Build must not prune the builder's own log") |
There was a problem hiding this comment.
Nice that this double-builds and pins builder.snapshotLog at 2. The gap is it only covers the no-op path — the branch that actually changed is the pruning one in updatedSnapshotLog, and nothing builds twice after RemoveSnapshots. I'd add the same build-twice-and-compare to TestRemoveSnapshotsPrunesSnapshotLogHistory and check builder.snapshotLog keeps its removed entries.
There was a problem hiding this comment.
Worth adding, though the premise is off. TestSnapshotLogSkipsIntermediate already takes the pruning branch (snapshot 1 is intermediate), and its new assertion fails on main. Adding the same build-twice check to TestRemoveSnapshotsPrunesSnapshotLogHistory pins the hasRemoved arm. I tried it: it passes here and fails on main.
Drafted with an AI-assisted tool; confirmed by a maintainer.
| return nil, fmt.Errorf("unsupported format version %d", b.formatVersion) | ||
| } | ||
|
|
||
| // Build may run more than once on the same builder, so the history entry is |
There was a problem hiding this comment.
Not introduced here, but it undercuts the same goal: buildMetadata just above generates uuid.New() into a local when b.uuid is nil and never stores it, so repeated builds of the same view hand back different UUIDs — a retried commit builds a different view each time. Since this PR is making repeated builds stable, I'd cache the generated UUID onto b.uuid here too, or document that it's deliberately per-build.
There was a problem hiding this comment.
Out of scope for this PR. Java's ViewMetadata.Builder.build() also generates a fresh UUID on every build when none is assigned. Caching it would make Build() write builder state again. No AssignUUID change would be recorded either, so replaying Changes would still produce a different UUID. The production callers (createView, NewMetadataWithUUID) build once. A separate issue is fine if we want stable UUIDs.
Drafted with an AI-assisted tool; confirmed by a maintainer.
| for range 3 { | ||
| res, err := b.Build() | ||
| require.NoError(t, err) | ||
| require.Len(t, res.VersionLog(), 1) |
There was a problem hiding this comment.
This only exercises the no-expiry path — one version, so len(b.versionList) > versionsToKeep stays false and updateHistory (where the clone fix also applies) never runs. I'd add a case with VersionHistorySizeKey=1 and several versions, plus one built from existing metadata with a non-empty initial versionLog, since that's the real commit-retry scenario. Worth asserting the UUID is equal across the three builds here too.
There was a problem hiding this comment.
Agreed, minus the UUID assertion. Add a MetadataBuilderFromBase case with a non-empty versionLog. Setting VersionHistorySizeKey low in the same test also covers the expiry branch. I tried a three-entry base log with history size 2: each of three builds returns [3, 4] and leaves the builder's log at three entries, while main grows it on the first build. Without SetUUID the UUIDs differ by design, so don't assert they match.
Drafted with an AI-assisted tool; confirmed by a maintainer.
zeroshade
left a comment
There was a problem hiding this comment.
This does what we agreed on in #2095: Build() derives the version log and the snapshot log from builder state without writing them back, and a single build's output is unchanged. The new assertions fail on the merge base (fa5309b) and pass here.
Prior feedback: I've replied on each of @laskoviymishka's threads. One more point:
- Rebuilding after more changes: intended. Build →
AddVersion(v2)→SetCurrentVersionID(v2)→ build now returns[v2], which is what you get by replayingChangeson a fresh builder. Main returned[v1, v2], which no commit of those changes would produce. Java's builder also keeps a singlehistoryEntrythatsetCurrentVersionIdoverwrites.
On the contract question, what #2095 and this PR guarantee is narrow. Build() doesn't write the logs back, so repeated builds of the same builder state return the same logs, and a staged build matches what committing the same updates produces. The comments in the diff are already scoped to the logs. Making Build() fully idempotent (UUID, timestamp) would be a separate change.
Smaller observations
- The description and commit message say the table change "did not change any output". It does change for one sequence: add A → set
mainto A → add B → setmainto B → build → setmainback to A → build. Main returns[A]. This branch returns[A, A], which matchesUpdateTableMetadataon the same updates and Java'supdateSnapshotLog. That's the correct result, but please mention it in the description. A test would also help, since it's the only sequence I found where the output changes.
This review was drafted by an AI-assisted tool and confirmed by an Apache Iceberg maintainer. The findings below are observations, not blockers; an Apache Iceberg maintainer — a real person — will take the next look at the PR. If you think a finding is mis-applied, please reply on the PR and a maintainer will weigh in.
More on how Apache Iceberg handles maintainer review: CONTRIBUTING.md.
- table: compute lastUpdatedMS per Build instead of storing it on the builder, so a build after more changes gets a fresh timestamp. - view: keep the UUID generated for a new view on the builder and reuse it, so repeated builds describe the same view; SetUUID still works. - Document on both Build methods that they can be called more than once. - Tests: build twice after RemoveSnapshots, the view expiry path with VersionHistorySizeKey=1, a builder from existing metadata, a stable generated UUID, and a fresh timestamp after more changes.
|
@laskoviymishka Thanks for the thorough review. I went with Build being repeatable, since that's what #2108 asks for, and made the contract explicit on both
I merged |
Per review, Build only guarantees that it doesn't write the version log or the snapshot log back to the builder. - table: keep lastUpdatedMS cached on the builder, as Java's TableMetadata.Builder does, and say when it is set. - view: keep generating the UUID per build when none is assigned, as Java's ViewMetadata.Builder does. - Tests: drop the timestamp and UUID assertions, build a three-entry base version log with a history size of 2, and pin the [A, A] snapshot log after moving main back to A between builds.
|
@zeroshade Thanks, done in dbd1421. Build now only guarantees what it does to the logs.
@laskoviymishka this undoes the timestamp and UUID parts of my previous commit, following the threads above. |
laskoviymishka
left a comment
There was a problem hiding this comment.
Thanks for turning this around. The scoping we landed on with zeroshade reads well: the guarantee sits on the logs, and the full-idempotency piece (view UUID, lastUpdatedMS timestamp) is cleanly out of scope. The pruning and expiry branches this PR touches are covered now, and pinning the [A, A] move-main-back sequence (plus the note in the description) is exactly what I was after last round.
The remaining items are all small and I've left them inline. The main one is the view expiry test: it passes, but with a fresh builder it never hits updateHistory's clear branch, so it doesn't quite prove pruning-on-the-clone. The from-base test is the one carrying that. The rest are comment-accuracy nits (the lastUpdatedMS latch, the [A, A] "intermediate" wording). Tighten the test and those comments and I'm happy to merge.
|
|
||
| // If no change has set lastUpdatedMS yet, the first Build sets it and keeps it on | ||
| // the builder on purpose (Java's TableMetadata.Builder does the same), so repeated | ||
| // builds of the same changes report the same time. Adding a snapshot or moving |
There was a problem hiding this comment.
The last sentence claims more than the code does. Only AddSnapshot and SetSnapshotRef set lastUpdatedMS, so after a first Build a non-snapshot edit (AddSchema, SetProperties) followed by a second Build still reports the first build's time. I'd narrow it to say the timestamp is latched by the first Build unless a snapshot or ref change moves it, so nobody reads it as "any later mutation refreshes it."
There was a problem hiding this comment.
Narrowed in 14e433a. The comment now says that after the first Build only AddSnapshot, SetSnapshotRef on main and SetLastUpdatedMS move the timestamp. A later SetProperties or AddSchema followed by another Build still reports the first Build's time.
| } | ||
|
|
||
| func (b *MetadataBuilder) updateSnapshotLog() error { | ||
| // updatedSnapshotLog returns the snapshot log with intermediate and removed |
There was a problem hiding this comment.
The property this whole fix leans on is that the result is always a fresh slice that never aliases b.snapshotLog. I'd state that in the doc so nobody optimizes the fall-through slices.Clone away later. Worth noting too that pruning is re-derived from b.updates on every Build, so the output quietly depends on b.updates staying the full change history (a future reset-after-commit would silently change it).
There was a problem hiding this comment.
Added to the doc in 14e433a: the result is always a new slice that never aliases b.snapshotLog, including the slices.Clone fall-through, and pruning is derived again from b.updates on every call, so it relies on b.updates holding every change made through the builder.
|
|
||
| func TestSnapshotLogAfterMovingMainBackAfterBuild(t *testing.T) { | ||
| // add A, set main to A, add B, set main to B, build, set main back to A, build. | ||
| // B is intermediate in the first build, so it is skipped, and the second build |
There was a problem hiding this comment.
The comment says B is intermediate "in the first build," but there it's A that was current and then replaced by B, so A is the one collapsed in that build; B only becomes the skipped intermediate once main moves back to A for the second build. The [1, 1] assertion looks right; it's the explanation that names the wrong snapshot, and as written the next reader can't tell [A, A] is deliberate. While rewording, running the same sequence through UpdateTableMetadata and comparing logs would actually pin the parity the comment claims.
There was a problem hiding this comment.
You're right, the comment had it backwards. In 14e433a it now says A is the intermediate snapshot in the first build, which logs [B]. Moving main back to A makes B intermediate while both of A's entries stay, hence [A, A]. The test also commits builder.updates through UpdateTableMetadata and checks the logs are equal.
| } | ||
| } | ||
|
|
||
| func TestBuild_DoesNotGrowVersionLogWhenExpiringVersions(t *testing.T) { |
There was a problem hiding this comment.
The comment says updateHistory prunes the log on every Build, but with a fresh builder the log only ever holds the entry for version 3, the retained version, so it always takes the retained branch and the clear path never runs. It passes for the right reason (no growth), but it doesn't prove pruning on the clone; TestBuild_FromBaseDoesNotGrowVersionLog is the one that actually exercises the clear branch. I'd either fold this into the from-base case or rename it and fix the comment, and assert the exact first log (Len 1, VersionID 3) so it pins what it's really checking.
There was a problem hiding this comment.
Renamed to TestBuild_RepeatedBuildsWithVersionExpiry in 14e433a. The comment now says it keeps the retained entry and points to TestBuild_FromBaseDoesNotGrowVersionLog for the clear branch. The test also asserts the first log is exactly one entry for version 3.
- Say which calls move lastUpdatedMS after the first Build. - Document that updatedSnapshotLog never aliases b.snapshotLog and derives pruning from b.updates. - Fix the [A, A] explanation and compare it with UpdateTableMetadata. - Rename the view expiry test, pin its one-entry log, and point to the from-base test for the clear branch.
Closes #2108, the follow-up from #2095.
Build()no longer writes the version log or the snapshot log back to the builder, so repeated builds of the same builder state return the same logs, and a staged build matches what committing the same updates produces. MakingBuild()fully idempotent (the generated view UUID, the timestamp) is out of scope.view.MetadataBuilder.BuildappendedversionHistoryEntrytob.versionLog, so every extraBuild()added the entry again. It now appends to a copy.TestBuild_DoesNotGrowVersionLogbuilds three times and checks for one entry each time, with the builder's own log still empty.TestBuild_DoesNotGrowVersionLogWhenExpiringVersionscovers the expiry path withVersionHistorySizeKey=1.TestBuild_FromBaseDoesNotGrowVersionLogstarts from metadata with a three-entry version log and a history size of 2. Each of three builds returns[3, 4], and the builder keeps its three entries.updateSnapshotLogwrote the pruned log back intob.snapshotLog. It is nowupdatedSnapshotLog, which returns the log instead, sobuildCommonMetadatatreats it like the metadata log.TestSnapshotLogSkipsIntermediateandTestRemoveSnapshotsPrunesSnapshotLogHistorynow build twice and check that the builder keeps its entries. Both fail on main.mainto A, add B, setmainto B, build, setmainback to A, build. Main returned[A], because the first build wrote its pruned log back to the builder. This branch returns[A, A], which matchesUpdateTableMetadataon the same updates and Java'supdateSnapshotLog.TestSnapshotLogAfterMovingMainBackAfterBuildpins it and fails on main.lastUpdatedMS. Kept cached on the builder, as Java'sTableMetadata.Builderdoes. The comment says when it is set.go test ./table ./viewpasses andgolangci-lint run(v2.14.0) reports 0 issues.