Skip to content

fix(view): stop MetadataBuilder.Build from growing the version log - #2109

Merged
laskoviymishka merged 6 commits into
apache:mainfrom
CaptainAni187:fix/2108-build-side-effects
Oct 11, 2026
Merged

laskoviymishka merged 6 commits into
apache:mainfrom
CaptainAni187:fix/2108-build-side-effects

Conversation

@CaptainAni187

@CaptainAni187 CaptainAni187 commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

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. Making Build() fully idempotent (the generated view UUID, the timestamp) is out of scope.

  • View version log (the bug). view.MetadataBuilder.Build appended versionHistoryEntry to b.versionLog, so every extra Build() added the entry again. It now appends to a copy.
    • TestBuild_DoesNotGrowVersionLog builds three times and checks for one entry each time, with the builder's own log still empty.
    • TestBuild_DoesNotGrowVersionLogWhenExpiringVersions covers the expiry path with VersionHistorySizeKey=1.
    • TestBuild_FromBaseDoesNotGrowVersionLog starts 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.
    • All three fail on main.
  • Table snapshot log. updateSnapshotLog wrote the pruned log back into b.snapshotLog. It is now updatedSnapshotLog, which returns the log instead, so buildCommonMetadata treats it like the metadata log.
    • TestSnapshotLogSkipsIntermediate and TestRemoveSnapshotsPrunesSnapshotLogHistory now build twice and check that the builder keeps its entries. Both fail on main.
  • One sequence where the output changes. Add A, set main to A, add B, set main to B, build, set main back to A, build. Main returned [A], because the first build wrote its pruned log back to the builder. This branch returns [A, A], which matches UpdateTableMetadata on the same updates and Java's updateSnapshotLog. TestSnapshotLogAfterMovingMainBackAfterBuild pins it and fails on main.
  • lastUpdatedMS. Kept cached on the builder, as Java's TableMetadata.Builder does. The comment says when it is set.

go test ./table ./view passes and golangci-lint run (v2.14.0) reports 0 issues.

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 laskoviymishka left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread table/metadata.go Outdated
}

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Suggested change
// 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")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread view/metadata_builder.go
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 zeroshade left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 replaying Changes on a fresh builder. Main returned [v1, v2], which no commit of those changes would produce. Java's builder also keeps a single historyEntry that setCurrentVersionId overwrites.

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 main to A → add B → set main to B → build → set main back to A → build. Main returns [A]. This branch returns [A, A], which matches UpdateTableMetadata on the same updates and Java's updateSnapshotLog. 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.
@CaptainAni187

Copy link
Copy Markdown
Contributor Author

@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 Build methods.

  • Timestamp: lastUpdatedMS is now computed into a local in table.MetadataBuilder.Build and no longer stored on the builder. Building, making more changes and building again gives a later last-updated time (TestBuild_LastUpdatedIsNotFixedByAnEarlierBuild, which fails on the previous commit). TestLastUpdateIncreasedForPropertyOnlyUpdate now reads the time from the built metadata instead of the builder field.
  • View UUID: the UUID generated for a new view without one is now kept on the builder and reused, so retried builds describe the same view. It's in its own field rather than b.uuid, so SetUUID still works after a build. TestBuild_RepeatedBuildsAreStable checks both.
  • Pruning path: TestRemoveSnapshotsPrunesSnapshotLogHistory builds twice after RemoveSnapshots, compares the logs, and checks builder.snapshotLog keeps all three entries. That last check fails on main.
  • View expiry path: TestBuild_DoesNotGrowVersionLogWhenExpiringVersions uses VersionHistorySizeKey=1 with three versions, so updateHistory runs.
  • Retry from existing metadata: TestBuild_FromBaseDoesNotGrowVersionLog starts from metadata with a non-empty version log. Both tests also compare the UUID across three builds.
  • Mutate-then-build-again: each Build describes the builder's whole change set from its base, so after SetCurrentVersionID is called again the log records the version the builder now ends at. A version that was set in between but never committed isn't kept. That's also what happens when the current version is set twice before a single build, and as far as I can tell it matches Java's view builder. I've left it as is, but tell me if you'd rather keep intermediate entries.

I merged main into the branch. go test ./table/... ./view/... and golangci-lint pass locally.

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.
@CaptainAni187

Copy link
Copy Markdown
Contributor Author

@zeroshade Thanks, done in dbd1421. Build now only guarantees what it does to the logs.

  • lastUpdatedMS stays cached on the builder, with your comment.
  • The view UUID is generated per build again, and the tests no longer assert that UUIDs match.
  • TestBuild_FromBaseDoesNotGrowVersionLog uses a three-entry base log with a history size of 2. It checks [3, 4] on each of three builds and that the builder keeps its three entries.
  • TestSnapshotLogAfterMovingMainBackAfterBuild pins the [A, A] sequence, and the description now mentions it. The new tests fail on main.

@laskoviymishka this undoes the timestamp and UUID parts of my previous commit, following the threads above.

@laskoviymishka laskoviymishka left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread table/metadata.go Outdated

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread table/metadata.go
}

func (b *MetadataBuilder) updateSnapshotLog() error {
// updatedSnapshotLog returns the snapshot log with intermediate and removed

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread table/metadata_builder_internal_test.go Outdated

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread view/metadata_builder_test.go Outdated
}
}

func TestBuild_DoesNotGrowVersionLogWhenExpiringVersions(t *testing.T) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@laskoviymishka
laskoviymishka merged commit d9bf431 into apache:main Oct 11, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make MetadataBuilder.Build side-effect free for the view version log and the table snapshot log

3 participants