-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[core] Fix snapshot expiration for chain table delta commits #9611
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
huyuanfeng2018
wants to merge
4
commits into
apache:master
Choose a base branch
from
huyuanfeng2018:fix-chain-table-snapshot-expire-9595
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+209
−2
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c9f317a
[core] expire snapshot branch snapshots from chain delta commits
huyuanfeng2018 ddf703a
[core] add chain table snapshot expiration regression test
huyuanfeng2018 37bff71
[core] strengthen chain table snapshot expiration regression test
huyuanfeng2018 8259410
[core] Fix spotless formatting in chain table snapshot expiration test
huyuanfeng2018 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
193 changes: 193 additions & 0 deletions
193
paimon-core/src/test/java/org/apache/paimon/table/ChainTableSnapshotExpireTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| /* | ||
| * 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 org.apache.paimon.table; | ||
|
|
||
| import org.apache.paimon.CoreOptions; | ||
| import org.apache.paimon.data.BinaryString; | ||
| import org.apache.paimon.data.GenericRow; | ||
| import org.apache.paimon.fs.Path; | ||
| import org.apache.paimon.fs.local.LocalFileIO; | ||
| import org.apache.paimon.manifest.PartitionEntry; | ||
| import org.apache.paimon.options.Options; | ||
| import org.apache.paimon.schema.FileSystemSchemaManager; | ||
| import org.apache.paimon.schema.Schema; | ||
| import org.apache.paimon.schema.SchemaChange; | ||
| import org.apache.paimon.schema.SchemaManager; | ||
| import org.apache.paimon.schema.TableSchema; | ||
| import org.apache.paimon.table.sink.CommitMessage; | ||
| import org.apache.paimon.table.sink.StreamTableWrite; | ||
| import org.apache.paimon.table.sink.TableCommitImpl; | ||
| import org.apache.paimon.types.DataTypes; | ||
| import org.apache.paimon.types.RowType; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.io.TempDir; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.time.format.DateTimeFormatter; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.UUID; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| /** Tests snapshot expiration maintenance for chain table branches. */ | ||
| public class ChainTableSnapshotExpireTest { | ||
|
|
||
| private static final DateTimeFormatter PARTITION_FORMATTER = | ||
| DateTimeFormatter.ofPattern("yyyyMMdd"); | ||
|
|
||
| @TempDir java.nio.file.Path tempDir; | ||
|
|
||
| @Test | ||
| public void testDeltaCommitExpiresSnapshotBranchSnapshotsAfterPartitionExpiration() | ||
| throws Exception { | ||
| Path tablePath = new Path(tempDir.toUri().toString(), "chain_snapshot_expire"); | ||
| createChainTable(tablePath); | ||
|
|
||
| FileStoreTable mainTable = loadTable(tablePath); | ||
| FileStoreTable snapshotTable = mainTable.switchToBranch("snapshot"); | ||
| FileStoreTable deltaTable = mainTable.switchToBranch("delta"); | ||
| String commitUser = UUID.randomUUID().toString(); | ||
|
|
||
| String day90 = partitionDate(-90); | ||
| String day65 = partitionDate(-65); | ||
| String day40 = partitionDate(-40); | ||
| String day10 = partitionDate(-10); | ||
|
|
||
| // Build three snapshot anchors while partition expiration is disabled. | ||
| write(snapshotTable, commitUser, day90, "v1"); | ||
| write(snapshotTable, commitUser, day65, "v2"); | ||
| write(snapshotTable, commitUser, day40, "v3"); | ||
|
|
||
| snapshotTable = loadTable(tablePath).switchToBranch("snapshot"); | ||
| assertThat(listPartitions(snapshotTable)).containsExactly(day90, day65, day40); | ||
| assertThat(snapshotTable.snapshotManager().snapshotCount()).isEqualTo(3); | ||
| long latestSnapshotBeforeExpiration = snapshotTable.snapshotManager().latestSnapshotId(); | ||
|
|
||
| Map<String, String> expireOptions = new HashMap<>(); | ||
| expireOptions.put(CoreOptions.WRITE_ONLY.key(), "false"); | ||
| expireOptions.put(CoreOptions.PARTITION_EXPIRATION_TIME.key(), "30 d"); | ||
| expireOptions.put(CoreOptions.END_INPUT_CHECK_PARTITION_EXPIRE.key(), "true"); | ||
| expireOptions.put(CoreOptions.SNAPSHOT_NUM_RETAINED_MIN.key(), "1"); | ||
| expireOptions.put(CoreOptions.SNAPSHOT_NUM_RETAINED_MAX.key(), "1"); | ||
| expireOptions.put(CoreOptions.SNAPSHOT_TIME_RETAINED.key(), "0 ms"); | ||
| expireOptions.put(CoreOptions.SNAPSHOT_EXPIRE_EXECUTION_MODE.key(), "sync"); | ||
| deltaTable = deltaTable.copy(expireOptions); | ||
|
|
||
| // A bounded Delta commit deterministically triggers ChainTablePartitionExpire. The two | ||
| // oldest snapshot anchors are expired and the latest expired-time anchor (day40) is kept. | ||
| // Dropping those Snapshot-branch partitions creates a new Snapshot-branch metadata | ||
| // snapshot, which must then be expired according to the same snapshot retention policy. | ||
| write(deltaTable, commitUser, day10, "v4"); | ||
|
|
||
| snapshotTable = loadTable(tablePath).switchToBranch("snapshot"); | ||
| assertThat(listPartitions(snapshotTable)).containsExactly(day40); | ||
| assertThat(snapshotTable.snapshotManager().latestSnapshotId()) | ||
| .isGreaterThan(latestSnapshotBeforeExpiration); | ||
| assertThat(snapshotTable.snapshotManager().snapshotCount()).isEqualTo(1); | ||
| } | ||
|
|
||
| private void createChainTable(Path tablePath) throws Exception { | ||
| LocalFileIO fileIO = LocalFileIO.create(); | ||
| SchemaManager schemaManager = new FileSystemSchemaManager(fileIO, tablePath); | ||
|
|
||
| Map<String, String> options = new HashMap<>(); | ||
| options.put(CoreOptions.BUCKET.key(), "1"); | ||
| options.put(CoreOptions.MERGE_ENGINE.key(), "deduplicate"); | ||
| options.put(CoreOptions.SEQUENCE_FIELD.key(), "v"); | ||
|
|
||
| Schema schema = | ||
| new Schema( | ||
| RowType.of( | ||
| new org.apache.paimon.types.DataType[] { | ||
| DataTypes.STRING(), | ||
| DataTypes.STRING(), | ||
| DataTypes.STRING() | ||
| }, | ||
| new String[] {"dt", "pk", "v"}) | ||
| .getFields(), | ||
| Collections.singletonList("dt"), | ||
| Arrays.asList("pk", "dt"), | ||
| options, | ||
| ""); | ||
| schemaManager.createTable(schema); | ||
|
|
||
| FileStoreTable mainTable = loadTable(tablePath); | ||
| mainTable.createBranch("snapshot"); | ||
| mainTable.createBranch("delta"); | ||
|
|
||
| List<SchemaChange> chainOptions = | ||
| Arrays.asList( | ||
| SchemaChange.setOption(CoreOptions.CHAIN_TABLE_ENABLED.key(), "true"), | ||
| SchemaChange.setOption( | ||
| CoreOptions.SCAN_FALLBACK_SNAPSHOT_BRANCH.key(), "snapshot"), | ||
| SchemaChange.setOption( | ||
| CoreOptions.SCAN_FALLBACK_DELTA_BRANCH.key(), "delta"), | ||
| SchemaChange.setOption( | ||
| CoreOptions.PARTITION_TIMESTAMP_PATTERN.key(), "$dt"), | ||
| SchemaChange.setOption( | ||
| CoreOptions.PARTITION_TIMESTAMP_FORMATTER.key(), "yyyyMMdd")); | ||
| schemaManager.commitChanges(chainOptions); | ||
| new FileSystemSchemaManager(fileIO, tablePath, "snapshot").commitChanges(chainOptions); | ||
| new FileSystemSchemaManager(fileIO, tablePath, "delta").commitChanges(chainOptions); | ||
| } | ||
|
|
||
| private FileStoreTable loadTable(Path tablePath) { | ||
| LocalFileIO fileIO = LocalFileIO.create(); | ||
| Options options = new Options(); | ||
| options.set(CoreOptions.PATH, tablePath.toString()); | ||
| String branchName = CoreOptions.branch(options.toMap()); | ||
| TableSchema tableSchema = | ||
| new FileSystemSchemaManager(fileIO, tablePath, branchName).latest().get(); | ||
| return FileStoreTableFactory.create( | ||
| fileIO, tablePath, tableSchema, CatalogEnvironment.empty()); | ||
| } | ||
|
|
||
| private void write(FileStoreTable table, String commitUser, String dt, String value) | ||
| throws Exception { | ||
| StreamTableWrite write = table.newWrite(commitUser); | ||
| write.write( | ||
| GenericRow.of( | ||
| BinaryString.fromString(dt), | ||
| BinaryString.fromString(value), | ||
| BinaryString.fromString(value))); | ||
| try (TableCommitImpl commit = table.newCommit(commitUser)) { | ||
| List<CommitMessage> commitMessages = write.prepareCommit(true, Long.MAX_VALUE); | ||
| commit.commit(Long.MAX_VALUE, commitMessages); | ||
| } | ||
| write.close(); | ||
| } | ||
|
|
||
| private List<String> listPartitions(FileStoreTable table) { | ||
| return table.newSnapshotReader().partitionEntries().stream() | ||
| .map(PartitionEntry::partition) | ||
| .map(partition -> partition.getString(0).toString()) | ||
| .sorted() | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| private String partitionDate(int daysFromToday) { | ||
| return LocalDate.now().plusDays(daysFromToday).format(PARTITION_FORMATTER); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P1] Preserve the Snapshot branch's own retention policy
Here options belongs to the Delta writer, including its runtime overrides. switchToBranch loads the Snapshot branch's schema/options, but this config call replaces that branch's retention policy with Delta's. I reproduced it with identical persisted retain-min/max=3 and time-retained=365d on main/snapshot/delta, and only delta.copy(...) overriding retention to 1/0ms: after the Delta commit, Snapshot history falls to 1 instead of retaining 3. Using the target policy in a control keeps 3. Thus tuning ingestion retention can prematurely remove Snapshot history and the files needed for its time-travel/recovery contract. Please run maintenance with the target branch's own policy (including its changelog lifecycle settings), or introduce an explicit validated shared policy rather than silently borrowing a writer override. Add a regression with distinct Delta runtime and Snapshot retention settings.