You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by "CaptainAni187 (via GitHub)" <gi...@apache.org> on 2026/10/01 21:34:02 UTC

[PR] fix(table): stop MetadataBuilder.Build from growing the metadata log [iceberg-go]

CaptainAni187 opened a new pull request, #2095:
URL: https://github.com/apache/iceberg-go/pull/2095

   Closes #2091.
   
   `buildCommonMetadata` appended the previous metadata file to `b.metadataLog` and trimmed it in place, so every `Build()` on a builder with changes added one more identical entry. It now extends and trims a copy, so `Build()` leaves the builder unchanged and returns the same log however many times it runs.
   
   This only affects metadata built from an in-flight transaction (`StagedTable()`, `Transaction.Scan`, and the builds inside a copy-on-write delete). As the issue notes, committed metadata was already correct, because catalogs rebuild it from the base and the updates.
   
   Tests:
   
   - `TestBuildDoesNotGrowMetadataLog`: three `Build()` calls on one builder each return a single previous-file entry. On main the second call returns two.
   - `TestBuildDoesNotTrimTheBuilderMetadataLog`: with `write.metadata.previous-versions-max=2` and the log at its limit, repeated builds keep `[v2, v3]`. On main the in-place trim turns the second build into `[v3, v3]`, which a length check alone would miss.
   - `TestCopyOnWriteDeleteStagesSinglePreviousMetadataLogEntry`: the reproduction from the issue. A copy-on-write delete that rewrites two files, then `StagedTable()`. On main it lists the same file 5 times; now it lists it once.
   
   `go test ./...` passes and `golangci-lint run` (v2.14.0) reports 0 issues.
   
   I left the separate idea from the issue, building once per operation rather than once per file on the copy-on-write path, out of this PR.
   


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] fix(table): stop MetadataBuilder.Build from growing the metadata log [iceberg-go]

Posted by "laskoviymishka (via GitHub)" <gi...@apache.org>.
laskoviymishka commented on code in PR #2095:
URL: https://github.com/apache/iceberg-go/pull/2095#discussion_r4165106172


##########
table/metadata.go:
##########
@@ -1517,12 +1517,17 @@ func (b *MetadataBuilder) buildCommonMetadata() (*commonMetadata, error) {
 		b.lastUpdatedMS = time.Now().UnixMilli()
 	}
 
+	// Build may run more than once on the same builder, so the log is extended on a
+	// copy. Appending to b.metadataLog would add the previous file again on every call.
+	metadataLog := b.metadataLog
 	if b.previousFileEntry != nil && b.HasChanges() {
 		maxMetadataLogEntries := max(1,
 			b.props.GetInt(
 				MetadataPreviousVersionsMaxKey, MetadataPreviousVersionsMaxDefault))
-		b.AppendMetadataLog(*b.previousFileEntry)
-		b.TrimMetadataLogs(maxMetadataLogEntries)
+		metadataLog = append(slices.Clone(b.metadataLog), *b.previousFileEntry)
+		if len(metadataLog) > maxMetadataLogEntries {
+			metadataLog = metadataLog[len(metadataLog)-maxMetadataLogEntries:]

Review Comment:
   This inline trim is now a copy of `TrimMetadataLogs`, just without the receiver mutation. I'd pull the shared logic into a pure helper (something like `trimMetadataLog(log, maxEntries)` that returns the last `maxEntries` without touching its input) and call it from both here and `TrimMetadataLogs`. Otherwise a future change to trim semantics has to land in two places and one will drift.



##########
table/metadata.go:
##########
@@ -1517,12 +1517,17 @@ func (b *MetadataBuilder) buildCommonMetadata() (*commonMetadata, error) {
 		b.lastUpdatedMS = time.Now().UnixMilli()
 	}
 
+	// Build may run more than once on the same builder, so the log is extended on a
+	// copy. Appending to b.metadataLog would add the previous file again on every call.
+	metadataLog := b.metadataLog

Review Comment:
   I'd clone here too, `metadataLog := slices.Clone(b.metadataLog)`, so both exits return a fresh slice. The changed branch below clones but this no-changes path hands back `b.metadataLog` by alias; it's safe today given how `append` behaves, but the asymmetry is the kind of thing that bites whoever edits this next, and it's one alloc only on the no-change path.



##########
table/metadata_builder_internal_test.go:
##########
@@ -1489,6 +1489,48 @@ func TestExpireMetadataLog(t *testing.T) {
 	require.Len(t, meta.(*metadataV2).MetadataLog, 2)
 }
 
+func TestBuildDoesNotGrowMetadataLog(t *testing.T) {
+	builder := builderWithoutChanges(2)
+	require.NoError(t, builder.SetProperties(map[string]string{"test.prop": "value"}))
+
+	for range 3 {
+		meta, err := builder.Build()
+		require.NoError(t, err)
+		require.Len(t, meta.(*metadataV2).MetadataLog, 1)

Review Comment:
   Since this is an internal test, I'd assert on the builder's own slice directly inside the loop, `require.Len(t, builder.metadataLog, 1, "builder log must not grow across Build calls")`. Right now non-mutation is inferred from the returned length staying at 1; asserting on `builder.metadataLog` pins the exact invariant the fix is about.



##########
table/metadata.go:
##########
@@ -1517,12 +1517,17 @@ func (b *MetadataBuilder) buildCommonMetadata() (*commonMetadata, error) {
 		b.lastUpdatedMS = time.Now().UnixMilli()

Review Comment:
   Not blocking, but while we're making `Build()` side-effect-free on the log: it still writes `b.lastUpdatedMS` on the first call, and `updateSnapshotLog()` further down still writes back `b.snapshotLog`, so `Build()` is idempotent on the metadata log now but not on those two. Harmless in the current CoW path since both get overwritten from the snapshot timestamp before commit, but a builder cloned after a `Build()` inherits a frozen `lastUpdatedMS`. Do we extend the same copy-on-build treatment here, or document that `Build()` caches these on purpose? wdyt?



##########
table/metadata_builder_internal_test.go:
##########
@@ -1489,6 +1489,48 @@ func TestExpireMetadataLog(t *testing.T) {
 	require.Len(t, meta.(*metadataV2).MetadataLog, 2)
 }
 
+func TestBuildDoesNotGrowMetadataLog(t *testing.T) {

Review Comment:
   Could we add a case for the other new branch, `previousFileEntry` set but `HasChanges() == false`? That's the path where the fix returns `b.metadataLog` untouched, and right now `builderWithoutChanges` immediately gets a `SetProperties` so every test enters the active-changes branch. A quick `Build()` with no updates asserting `PreviousFiles()` stays empty would cover it.



##########
table/copy_on_write_metadata_log_test.go:
##########
@@ -0,0 +1,64 @@
+// 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 (
+	"context"
+	"path/filepath"
+	"slices"
+	"testing"
+
+	"github.com/apache/iceberg-go"
+	iceio "github.com/apache/iceberg-go/io"
+	"github.com/apache/iceberg-go/table"
+	"github.com/stretchr/testify/require"
+)
+
+// A copy-on-write delete builds the transaction's metadata once per rewritten

Review Comment:
   The comment says `Build` runs "once per rewritten file," but the duplicates on `main` actually come from classification and `StagedTable()` building the same builder too, not just the per-file rewrites, so the real count is higher than the comment implies. I'd reword it so the regression stays legible if those call sites ever move. While here, give the final `require.Len` a message like the setup assertion has.



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] fix(table): stop MetadataBuilder.Build from growing the metadata log [iceberg-go]

Posted by "CaptainAni187 (via GitHub)" <gi...@apache.org>.
CaptainAni187 commented on code in PR #2095:
URL: https://github.com/apache/iceberg-go/pull/2095#discussion_r4166175192


##########
table/metadata.go:
##########
@@ -1517,12 +1517,17 @@ func (b *MetadataBuilder) buildCommonMetadata() (*commonMetadata, error) {
 		b.lastUpdatedMS = time.Now().UnixMilli()
 	}
 
+	// Build may run more than once on the same builder, so the log is extended on a
+	// copy. Appending to b.metadataLog would add the previous file again on every call.
+	metadataLog := b.metadataLog

Review Comment:
   Done in 4ae0171, it clones on both paths now.



##########
table/metadata.go:
##########
@@ -1517,12 +1517,17 @@ func (b *MetadataBuilder) buildCommonMetadata() (*commonMetadata, error) {
 		b.lastUpdatedMS = time.Now().UnixMilli()
 	}
 
+	// Build may run more than once on the same builder, so the log is extended on a
+	// copy. Appending to b.metadataLog would add the previous file again on every call.
+	metadataLog := b.metadataLog
 	if b.previousFileEntry != nil && b.HasChanges() {
 		maxMetadataLogEntries := max(1,
 			b.props.GetInt(
 				MetadataPreviousVersionsMaxKey, MetadataPreviousVersionsMaxDefault))
-		b.AppendMetadataLog(*b.previousFileEntry)
-		b.TrimMetadataLogs(maxMetadataLogEntries)
+		metadataLog = append(slices.Clone(b.metadataLog), *b.previousFileEntry)
+		if len(metadataLog) > maxMetadataLogEntries {
+			metadataLog = metadataLog[len(metadataLog)-maxMetadataLogEntries:]

Review Comment:
   Done, pulled it into `trimMetadataLog`, and `TrimMetadataLogs` uses it too.



##########
table/metadata_builder_internal_test.go:
##########
@@ -1489,6 +1489,48 @@ func TestExpireMetadataLog(t *testing.T) {
 	require.Len(t, meta.(*metadataV2).MetadataLog, 2)
 }
 
+func TestBuildDoesNotGrowMetadataLog(t *testing.T) {
+	builder := builderWithoutChanges(2)
+	require.NoError(t, builder.SetProperties(map[string]string{"test.prop": "value"}))
+
+	for range 3 {
+		meta, err := builder.Build()
+		require.NoError(t, err)
+		require.Len(t, meta.(*metadataV2).MetadataLog, 1)

Review Comment:
   Added, with one tweak: this builder's base has an empty log, so I assert that `builder.metadataLog` stays empty across builds rather than at 1.



##########
table/metadata_builder_internal_test.go:
##########
@@ -1489,6 +1489,48 @@ func TestExpireMetadataLog(t *testing.T) {
 	require.Len(t, meta.(*metadataV2).MetadataLog, 2)
 }
 
+func TestBuildDoesNotGrowMetadataLog(t *testing.T) {

Review Comment:
   Added `TestBuildWithoutChangesAddsNoMetadataLogEntry`: previous file set, no updates, two builds, no log entry.



##########
table/copy_on_write_metadata_log_test.go:
##########
@@ -0,0 +1,64 @@
+// 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 (
+	"context"
+	"path/filepath"
+	"slices"
+	"testing"
+
+	"github.com/apache/iceberg-go"
+	iceio "github.com/apache/iceberg-go/io"
+	"github.com/apache/iceberg-go/table"
+	"github.com/stretchr/testify/require"
+)
+
+// A copy-on-write delete builds the transaction's metadata once per rewritten

Review Comment:
   Reworded it to say the transaction builds several times (classification, each rewritten file, `StagedTable`), and added a message to the final assert.



##########
table/metadata.go:
##########
@@ -1517,12 +1517,17 @@ func (b *MetadataBuilder) buildCommonMetadata() (*commonMetadata, error) {
 		b.lastUpdatedMS = time.Now().UnixMilli()

Review Comment:
   Good catch. I'd keep this PR to the metadata log, since that's what #2091 covers. For the other two, my lean would be to copy `snapshotLog` the same way, but to keep `lastUpdatedMS` cached and document it, because repeated builds returning the same timestamp seems like what callers would expect. Happy to do that as a follow-up if that sounds right.



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] fix(table): stop MetadataBuilder.Build from growing the metadata log [iceberg-go]

Posted by "zeroshade (via GitHub)" <gi...@apache.org>.
zeroshade merged PR #2095:
URL: https://github.com/apache/iceberg-go/pull/2095


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org