Skip to content

Commit

Permalink
add CreateTagFromWatermarkAction
Browse files Browse the repository at this point in the history
  • Loading branch information
herefree committed Aug 29, 2024
1 parent abb72db commit 6e7a6ef
Show file tree
Hide file tree
Showing 5 changed files with 263 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ public Optional<Action> create(MultipleParameterToolAdapter params) {
String timeRetained = params.get(TIME_RETAINED);
Map<String, String> catalogConfig = optionalConfigMap(params, CATALOG_CONF);

CreateTagFromTimestampAction migrateFileAction =
CreateTagFromTimestampAction createTagFromTimestampAction =
new CreateTagFromTimestampAction(
warehouse, table, tag, timestamp, timeRetained, catalogConfig);
return Optional.of(migrateFileAction);
return Optional.of(createTagFromTimestampAction);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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.flink.action;

import org.apache.paimon.flink.procedure.CreateTagFromWatermarkProcedure;

import org.apache.flink.table.procedure.DefaultProcedureContext;

import java.util.Map;

/** Create tag from watermark action for Flink. */
public class CreateTagFromWatermarkAction extends ActionBase {
private final String table;
private final String tag;
private final Long watermark;
private final String timeRetained;

public CreateTagFromWatermarkAction(
String warehouse,
String table,
String tag,
Long watermark,
String timeRetained,
Map<String, String> catalogConfig) {
super(warehouse, catalogConfig);
this.table = table;
this.tag = tag;
this.watermark = watermark;
this.timeRetained = timeRetained;
}

@Override
public void run() throws Exception {
CreateTagFromWatermarkProcedure createTagFromWatermarkProcedure =
new CreateTagFromWatermarkProcedure();
createTagFromWatermarkProcedure.withCatalog(catalog);
createTagFromWatermarkProcedure.call(
new DefaultProcedureContext(env), table, tag, watermark, timeRetained);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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.flink.action;

import java.util.Map;
import java.util.Optional;

/** Factory to create {@link CreateTagFromWatermarkAction}. */
public class CreateTagFromWatermarkActionFactory implements ActionFactory {

public static final String IDENTIFIER = "create_tag_from_watermark";

private static final String TABLE = "table";

private static final String TAG = "tag";

private static final String WATERMARK = "watermark";

private static final String TIME_RETAINED = "time_retained";

@Override
public String identifier() {
return IDENTIFIER;
}

@Override
public Optional<Action> create(MultipleParameterToolAdapter params) {
String warehouse = params.get(WAREHOUSE);
String table = params.get(TABLE);
String tag = params.get(TAG);
Long watermark = Long.parseLong(params.get(WATERMARK));
String timeRetained = params.get(TIME_RETAINED);
Map<String, String> catalogConfig = optionalConfigMap(params, CATALOG_CONF);

CreateTagFromWatermarkAction createTagFromWatermarkAction =
new CreateTagFromWatermarkAction(
warehouse, table, tag, watermark, timeRetained, catalogConfig);
return Optional.of(createTagFromWatermarkAction);
}

@Override
public void printHelp() {
System.out.println("Action \"create_tag_from_watermark\" create tag from watermark.");
System.out.println();

System.out.println("Syntax:");
System.out.println(
" create_tag_from_watermark --warehouse <warehouse_path> "
+ "--table <database.table_name> "
+ "--tag <tag> "
+ "--watermark <watermark> "
+ "[--timeRetained <duration>] "
+ "[--options <key>=<value>,<key>=<value>,...]");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ org.apache.paimon.flink.action.MergeIntoActionFactory
org.apache.paimon.flink.action.RollbackToActionFactory
org.apache.paimon.flink.action.CreateTagActionFactory
org.apache.paimon.flink.action.CreateTagFromTimestampActionFactory
org.apache.paimon.flink.action.CreateTagFromWatermarkActionFactory
org.apache.paimon.flink.action.DeleteTagActionFactory
org.apache.paimon.flink.action.ResetConsumerActionFactory
org.apache.paimon.flink.action.MigrateTableActionFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@

import org.apache.paimon.Snapshot;
import org.apache.paimon.flink.CatalogITCaseBase;
import org.apache.paimon.flink.action.ActionBase;
import org.apache.paimon.flink.action.ActionFactory;
import org.apache.paimon.flink.action.CreateTagFromWatermarkAction;
import org.apache.paimon.table.FileStoreTable;
import org.apache.paimon.utils.SnapshotNotExistException;

import org.apache.flink.types.Row;
import org.junit.jupiter.api.Test;

import java.util.concurrent.ThreadLocalRandom;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatException;

Expand Down Expand Up @@ -80,30 +85,67 @@ public void testCreatTagsFromSnapshotsWatermark() throws Exception {
assertThat(watermark1 == Long.MIN_VALUE).isTrue();
assertThat(watermark2 == 1000).isTrue();
assertThat(watermark3 == 2000).isTrue();
if (ThreadLocalRandom.current().nextBoolean()) {
assertThat(
sql(
"CALL sys.create_tag_from_watermark("
+ "`table` => 'default.T',"
+ "`tag` => 'tag2',"
+ "`watermark` => %s)",
watermark2 - 1)
.stream()
.map(Row::toString))
.containsExactlyInAnyOrder(
String.format("+I[tag2, 2, %s, %s]", commitTime2, watermark2));
} else {
createAction(
CreateTagFromWatermarkAction.class,
"create_tag_from_watermark",
"--warehouse",
path,
"--table",
"default.T",
"--tag",
"tag2",
"--watermark",
Long.toString(watermark2 - 1))
.run();
}
Snapshot snapshot = table.tagManager().taggedSnapshot("tag2");
assertThat(table.tagManager().tagExists("tag2")).isTrue();
assertThat(snapshot.watermark()).isEqualTo(watermark2);
assertThat(snapshot.timeMillis()).isEqualTo(commitTime2);

assertThat(
sql(
"CALL sys.create_tag_from_watermark("
+ "`table` => 'default.T',"
+ "`tag` => 'tag2',"
+ "`watermark` => %s)",
watermark2 - 1)
.stream()
.map(Row::toString))
.containsExactlyInAnyOrder(
String.format("+I[tag2, 2, %s, %s]", commitTime2, watermark2));

assertThat(
sql(
"CALL sys.create_tag_from_watermark("
+ "`table` => 'default.T',"
+ "`tag` => 'tag3',"
+ "`watermark` => %s)",
watermark2 + 1)
.stream()
.map(Row::toString))
.containsExactlyInAnyOrder(
String.format("+I[tag3, 3, %s, %s]", commitTime3, watermark3));
if (ThreadLocalRandom.current().nextBoolean()) {
assertThat(
sql(
"CALL sys.create_tag_from_watermark("
+ "`table` => 'default.T',"
+ "`tag` => 'tag3',"
+ "`watermark` => %s)",
watermark2 + 1)
.stream()
.map(Row::toString))
.containsExactlyInAnyOrder(
String.format("+I[tag3, 3, %s, %s]", commitTime3, watermark3));
} else {
createAction(
CreateTagFromWatermarkAction.class,
"create_tag_from_watermark",
"--warehouse",
path,
"--table",
"default.T",
"--tag",
"tag3",
"--watermark",
Long.toString(watermark2 + 1))
.run();
}

assertThat(table.tagManager().tagExists("tag3")).isTrue();
assertThat(table.tagManager().taggedSnapshot("tag3").watermark()).isEqualTo(watermark3);
assertThat(table.tagManager().taggedSnapshot("tag3").timeMillis()).isEqualTo(commitTime3);

assertThatException()
.isThrownBy(
Expand All @@ -121,7 +163,7 @@ public void testCreatTagsFromSnapshotsWatermark() throws Exception {
}

@Test
public void testCreatTagsFromTagsWatermark() throws Exception {
public void testCreateTagsFromTagsWatermark() throws Exception {
sql(
"CREATE TABLE T ("
+ " k STRING,"
Expand Down Expand Up @@ -160,28 +202,72 @@ public void testCreatTagsFromTagsWatermark() throws Exception {
assertThat(watermark2 == 2000).isTrue();

// create tag from tag1 that snapshot is 1.
assertThat(
sql(
"CALL sys.create_tag_from_watermark("
+ "`table` => 'default.T',"
+ "`tag` => 'tag2',"
+ "`watermark` => %s)",
tagsWatermark - 1)
.stream()
.map(Row::toString))
.containsExactlyInAnyOrder(
String.format("+I[tag2, 1, %s, %s]", tagsCommitTime, tagsWatermark));

assertThat(
sql(
"CALL sys.create_tag_from_watermark("
+ "`table` => 'default.T',"
+ "`tag` => 'tag3',"
+ "`watermark` => %s)",
watermark2 - 1)
.stream()
.map(Row::toString))
.containsExactlyInAnyOrder(
String.format("+I[tag3, 2, %s, %s]", commitTime2, watermark2));
if (ThreadLocalRandom.current().nextBoolean()) {
assertThat(
sql(
"CALL sys.create_tag_from_watermark("
+ "`table` => 'default.T',"
+ "`tag` => 'tag2',"
+ "`watermark` => %s)",
tagsWatermark - 1)
.stream()
.map(Row::toString))
.containsExactlyInAnyOrder(
String.format("+I[tag2, 1, %s, %s]", tagsCommitTime, tagsWatermark));
} else {
createAction(
CreateTagFromWatermarkAction.class,
"create_tag_from_watermark",
"--warehouse",
path,
"--table",
"default.T",
"--tag",
"tag2",
"--watermark",
Long.toString(tagsWatermark - 1))
.run();
}
assertThat(table.tagManager().tagExists("tag2")).isTrue();
assertThat(table.tagManager().taggedSnapshot("tag2").watermark()).isEqualTo(tagsWatermark);
assertThat(table.tagManager().taggedSnapshot("tag2").timeMillis())
.isEqualTo(tagsCommitTime);

if (ThreadLocalRandom.current().nextBoolean()) {
assertThat(
sql(
"CALL sys.create_tag_from_watermark("
+ "`table` => 'default.T',"
+ "`tag` => 'tag3',"
+ "`watermark` => %s)",
watermark2 - 1)
.stream()
.map(Row::toString))
.containsExactlyInAnyOrder(
String.format("+I[tag3, 2, %s, %s]", commitTime2, watermark2));
} else {
createAction(
CreateTagFromWatermarkAction.class,
"create_tag_from_watermark",
"--warehouse",
path,
"--table",
"default.T",
"--tag",
"tag3",
"--watermark",
Long.toString(watermark2 - 1))
.run();
}
assertThat(table.tagManager().tagExists("tag3")).isTrue();
assertThat(table.tagManager().taggedSnapshot("tag3").watermark()).isEqualTo(watermark2);
assertThat(table.tagManager().taggedSnapshot("tag3").timeMillis()).isEqualTo(commitTime2);
}

private <T extends ActionBase> T createAction(Class<T> clazz, String... args) {
return ActionFactory.createAction(args)
.filter(clazz::isInstance)
.map(clazz::cast)
.orElseThrow(() -> new RuntimeException("Failed to create action"));
}
}

0 comments on commit 6e7a6ef

Please sign in to comment.