Skip to content

Commit a426a42

Browse files
authored
[core] add more tests for clustering bucketed append table (#6981)
1 parent 536abee commit a426a42

File tree

2 files changed

+103
-5
lines changed

2 files changed

+103
-5
lines changed

paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/AppendOnlyTableITCase.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,37 @@ public void testAutoCompaction() {
472472
Row.of(12, "MMM"));
473473
}
474474

475+
@Test
476+
public void testAutoCluster() {
477+
batchSql("ALTER TABLE append_table SET ('num-sorted-run.compaction-trigger' = '3')");
478+
batchSql("ALTER TABLE append_table SET ('num-levels' = '6')");
479+
batchSql("ALTER TABLE append_table SET ('bucket-append-ordered' = 'false')");
480+
batchSql("ALTER TABLE append_table SET ('clustering.columns' = 'data')");
481+
batchSql("ALTER TABLE append_table SET ('clustering.strategy' = 'order')");
482+
batchSql("ALTER TABLE append_table SET ('clustering.incremental' = 'true')");
483+
484+
assertAutoCompaction(
485+
"INSERT INTO append_table VALUES (1, '9')", 1L, Snapshot.CommitKind.APPEND);
486+
assertAutoCompaction(
487+
"INSERT INTO append_table VALUES (2, '8')", 2L, Snapshot.CommitKind.APPEND);
488+
assertAutoCompaction(
489+
"INSERT INTO append_table VALUES (3, '7')", 4L, Snapshot.CommitKind.COMPACT);
490+
assertAutoCompaction(
491+
"INSERT INTO append_table VALUES (4, '6')", 5L, Snapshot.CommitKind.APPEND);
492+
assertAutoCompaction(
493+
"INSERT INTO append_table VALUES (5, '5')", 7L, Snapshot.CommitKind.COMPACT);
494+
495+
List<Row> rows = batchSql("SELECT * FROM append_table");
496+
assertThat(rows.size()).isEqualTo(5);
497+
assertThat(rows)
498+
.containsExactly(
499+
Row.of(5, "5"),
500+
Row.of(4, "6"),
501+
Row.of(3, "7"),
502+
Row.of(2, "8"),
503+
Row.of(1, "9"));
504+
}
505+
475506
@Test
476507
public void testRejectDelete() {
477508
testRejectChanges(RowKind.DELETE);

paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/action/IncrementalClusterActionITCase.java

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@
4040
import org.apache.paimon.table.sink.BatchWriteBuilder;
4141
import org.apache.paimon.table.sink.CommitMessage;
4242
import org.apache.paimon.table.sink.CommitMessageImpl;
43+
import org.apache.paimon.table.sink.StreamWriteBuilder;
4344
import org.apache.paimon.table.source.DataSplit;
4445
import org.apache.paimon.table.source.ReadBuilder;
4546
import org.apache.paimon.table.source.Split;
4647
import org.apache.paimon.types.DataTypes;
48+
import org.apache.paimon.utils.SnapshotManager;
4749
import org.apache.paimon.utils.StringUtils;
4850

4951
import org.apache.paimon.shade.guava30.com.google.common.collect.Lists;
@@ -56,6 +58,7 @@
5658
import java.util.HashMap;
5759
import java.util.List;
5860
import java.util.Map;
61+
import java.util.Objects;
5962
import java.util.concurrent.ThreadLocalRandom;
6063
import java.util.stream.Collectors;
6164

@@ -686,6 +689,7 @@ public void testClusterWithDeletionVector() throws Exception {
686689
@Test
687690
public void testClusterWithBucket() throws Exception {
688691
Map<String, String> dynamicOptions = commonOptions();
692+
dynamicOptions.put(CoreOptions.WRITE_ONLY.key(), "true");
689693
dynamicOptions.put(CoreOptions.BUCKET.key(), "2");
690694
dynamicOptions.put(CoreOptions.BUCKET_KEY.key(), "pt");
691695
dynamicOptions.put(CoreOptions.BUCKET_APPEND_ORDERED.key(), "false");
@@ -848,6 +852,35 @@ public void testClusterWithBucket() throws Exception {
848852
assertThat(result5).containsExactlyElementsOf(expected5);
849853
}
850854

855+
@Test
856+
public void testStreamingClusterWithBucket() throws Exception {
857+
Map<String, String> dynamicOptions = commonOptions();
858+
dynamicOptions.put(CoreOptions.WRITE_ONLY.key(), "true");
859+
dynamicOptions.put(CoreOptions.BUCKET.key(), "1");
860+
dynamicOptions.put(CoreOptions.BUCKET_KEY.key(), "pt");
861+
dynamicOptions.put(CoreOptions.BUCKET_APPEND_ORDERED.key(), "false");
862+
dynamicOptions.put(CoreOptions.CONTINUOUS_DISCOVERY_INTERVAL.key(), "1s");
863+
FileStoreTable table = createTable(null, dynamicOptions);
864+
StreamWriteBuilder streamWriteBuilder =
865+
table.newStreamWriteBuilder().withCommitUser(commitUser);
866+
write = streamWriteBuilder.newWrite();
867+
commit = streamWriteBuilder.newCommit();
868+
869+
// base records
870+
writeData(GenericRow.of(2, 2, BinaryString.fromString("test"), 0));
871+
writeData(GenericRow.of(2, 1, BinaryString.fromString("test"), 0));
872+
writeData(GenericRow.of(2, 0, BinaryString.fromString("test"), 0));
873+
874+
checkSnapshot(table, Snapshot.CommitKind.APPEND);
875+
runAction(true, Collections.emptyList());
876+
checkSnapshot(table, 4, Snapshot.CommitKind.COMPACT, 60_000);
877+
878+
// incremental records
879+
writeData(GenericRow.of(1, 2, BinaryString.fromString("test"), 0));
880+
writeData(GenericRow.of(1, 1, BinaryString.fromString("test"), 0));
881+
checkSnapshot(table, 7, Snapshot.CommitKind.COMPACT, 60_000);
882+
}
883+
851884
protected FileStoreTable createTable(String partitionKeys) throws Exception {
852885
return createTable(partitionKeys, commonOptions());
853886
}
@@ -923,8 +956,28 @@ private static String randomString(int length) {
923956
}
924957

925958
private void checkSnapshot(FileStoreTable table) {
926-
assertThat(table.latestSnapshot().get().commitKind())
927-
.isEqualTo(Snapshot.CommitKind.COMPACT);
959+
checkSnapshot(table, Snapshot.CommitKind.COMPACT);
960+
}
961+
962+
private void checkSnapshot(FileStoreTable table, Snapshot.CommitKind commitKind) {
963+
assertThat(table.latestSnapshot().get().commitKind()).isEqualTo(commitKind);
964+
}
965+
966+
protected void checkSnapshot(
967+
FileStoreTable table, long snapshotId, Snapshot.CommitKind commitKind, long timeout)
968+
throws Exception {
969+
SnapshotManager snapshotManager = table.snapshotManager();
970+
long start = System.currentTimeMillis();
971+
while (!Objects.equals(snapshotManager.latestSnapshotId(), snapshotId)) {
972+
Thread.sleep(500);
973+
if (System.currentTimeMillis() - start > timeout) {
974+
throw new RuntimeException("can't wait for a compaction.");
975+
}
976+
}
977+
978+
Snapshot snapshot = snapshotManager.snapshot(snapshotManager.latestSnapshotId());
979+
assertThat(snapshot.id()).isEqualTo(snapshotId);
980+
assertThat(snapshot.commitKind()).isEqualTo(commitKind);
928981
}
929982

930983
private List<CommitMessage> produceDvIndexMessages(
@@ -954,7 +1007,16 @@ private List<CommitMessage> produceDvIndexMessages(
9541007
}
9551008

9561009
private void runAction(List<String> extra) throws Exception {
957-
StreamExecutionEnvironment env = streamExecutionEnvironmentBuilder().batchMode().build();
1010+
runAction(false, extra);
1011+
}
1012+
1013+
private void runAction(boolean isStreaming, List<String> extra) throws Exception {
1014+
StreamExecutionEnvironment env;
1015+
if (isStreaming) {
1016+
env = streamExecutionEnvironmentBuilder().streamingMode().build();
1017+
} else {
1018+
env = streamExecutionEnvironmentBuilder().batchMode().build();
1019+
}
9581020
ArrayList<String> baseArgs =
9591021
Lists.newArrayList("compact", "--database", database, "--table", tableName);
9601022
ThreadLocalRandom random = ThreadLocalRandom.current();
@@ -966,7 +1028,12 @@ private void runAction(List<String> extra) throws Exception {
9661028
baseArgs.addAll(extra);
9671029

9681030
CompactAction action = createAction(CompactAction.class, baseArgs.toArray(new String[0]));
969-
action.withStreamExecutionEnvironment(env);
970-
action.run();
1031+
action.withStreamExecutionEnvironment(env).build();
1032+
if (isStreaming) {
1033+
env.executeAsync();
1034+
} else {
1035+
env.execute();
1036+
}
1037+
// action.run();
9711038
}
9721039
}

0 commit comments

Comments
 (0)