-
Notifications
You must be signed in to change notification settings - Fork 993
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core] Introduce RollbackToTimestamp Procedure and Action
- Loading branch information
xuyu
committed
Oct 30, 2024
1 parent
8e4de02
commit c956b35
Showing
11 changed files
with
505 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
58 changes: 58 additions & 0 deletions
58
...nk-1.18/src/main/java/org/apache/paimon/flink/procedure/RollbackToTimestampProcedure.java
This file contains 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,58 @@ | ||
/* | ||
* 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.procedure; | ||
|
||
import org.apache.paimon.Snapshot; | ||
import org.apache.paimon.catalog.Catalog; | ||
import org.apache.paimon.catalog.Identifier; | ||
import org.apache.paimon.table.FileStoreTable; | ||
import org.apache.paimon.table.Table; | ||
import org.apache.paimon.utils.Preconditions; | ||
|
||
import org.apache.flink.table.procedure.ProcedureContext; | ||
|
||
/** | ||
* Rollback procedure. Usage: | ||
* | ||
* <pre><code> | ||
* -- rollback to a snapshot | ||
* CALL sys.rollback_to_timestamp('tableId', timestamp) | ||
* </code></pre> | ||
*/ | ||
public class RollbackToTimestampProcedure extends ProcedureBase { | ||
|
||
public static final String IDENTIFIER = "rollback_to_timestamp"; | ||
|
||
public String[] call(ProcedureContext procedureContext, String tableId, long timestamp) | ||
throws Catalog.TableNotExistException { | ||
Table table = catalog.getTable(Identifier.fromString(tableId)); | ||
FileStoreTable fileStoreTable = (FileStoreTable) table; | ||
Snapshot snapshot = fileStoreTable.snapshotManager().earlierOrEqualTimeMills(timestamp); | ||
Preconditions.checkNotNull( | ||
snapshot, String.format("count not find snapshot earlier than %s", timestamp)); | ||
fileStoreTable.rollbackTo(snapshot.id()); | ||
|
||
return new String[] {"Success"}; | ||
} | ||
|
||
@Override | ||
public String identifier() { | ||
return IDENTIFIER; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...-flink-common/src/main/java/org/apache/paimon/flink/action/RollbackToTimestampAction.java
This file contains 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,62 @@ | ||
/* | ||
* 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.Snapshot; | ||
import org.apache.paimon.table.DataTable; | ||
import org.apache.paimon.table.FileStoreTable; | ||
import org.apache.paimon.utils.Preconditions; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Map; | ||
|
||
/** Rollback to specific timestamp action for Flink. */ | ||
public class RollbackToTimestampAction extends TableActionBase { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(RollbackToTimestampAction.class); | ||
|
||
private final Long timestamp; | ||
|
||
public RollbackToTimestampAction( | ||
String warehouse, | ||
String databaseName, | ||
String tableName, | ||
Long timestamp, | ||
Map<String, String> catalogConfig) { | ||
super(warehouse, databaseName, tableName, catalogConfig); | ||
this.timestamp = timestamp; | ||
} | ||
|
||
@Override | ||
public void run() throws Exception { | ||
LOG.debug("Run rollback-to-timestamp action with timestamp '{}'.", timestamp); | ||
|
||
if (!(table instanceof DataTable)) { | ||
throw new IllegalArgumentException("Unknown table: " + identifier); | ||
} | ||
|
||
FileStoreTable fileStoreTable = (FileStoreTable) table; | ||
Snapshot snapshot = fileStoreTable.snapshotManager().earlierOrEqualTimeMills(timestamp); | ||
Preconditions.checkNotNull( | ||
snapshot, String.format("count not find snapshot earlier than %s", timestamp)); | ||
fileStoreTable.rollbackTo(snapshot.id()); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...common/src/main/java/org/apache/paimon/flink/action/RollbackToTimestampActionFactory.java
This file contains 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,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 org.apache.flink.api.java.tuple.Tuple3; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
/** Factory to create {@link RollbackToTimestampAction}. */ | ||
public class RollbackToTimestampActionFactory implements ActionFactory { | ||
|
||
public static final String IDENTIFIER = "rollback_to_timestamp"; | ||
|
||
private static final String TIMESTAMP = "timestamp"; | ||
|
||
@Override | ||
public String identifier() { | ||
return IDENTIFIER; | ||
} | ||
|
||
@Override | ||
public Optional<Action> create(MultipleParameterToolAdapter params) { | ||
Tuple3<String, String, String> tablePath = getTablePath(params); | ||
|
||
checkRequiredArgument(params, TIMESTAMP); | ||
String timestamp = params.get(TIMESTAMP); | ||
|
||
Map<String, String> catalogConfig = optionalConfigMap(params, CATALOG_CONF); | ||
|
||
RollbackToTimestampAction action = | ||
new RollbackToTimestampAction( | ||
tablePath.f0, | ||
tablePath.f1, | ||
tablePath.f2, | ||
Long.parseLong(timestamp), | ||
catalogConfig); | ||
|
||
return Optional.of(action); | ||
} | ||
|
||
@Override | ||
public void printHelp() { | ||
System.out.println( | ||
"Action \"rollback_to_timestamp\" roll back a table to a specific timestamp."); | ||
System.out.println(); | ||
|
||
System.out.println("Syntax:"); | ||
System.out.println( | ||
" rollback_to --warehouse <warehouse_path> --database <database_name> " | ||
+ "--table <table_name> --timestamp <timestamp_string>"); | ||
System.out.println(" <timestamp_string> can be a long value representing a timestamp."); | ||
System.out.println(); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...-common/src/main/java/org/apache/paimon/flink/procedure/RollbackToTimestampProcedure.java
This file contains 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,65 @@ | ||
/* | ||
* 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.procedure; | ||
|
||
import org.apache.paimon.Snapshot; | ||
import org.apache.paimon.catalog.Catalog; | ||
import org.apache.paimon.catalog.Identifier; | ||
import org.apache.paimon.table.FileStoreTable; | ||
import org.apache.paimon.table.Table; | ||
import org.apache.paimon.utils.Preconditions; | ||
|
||
import org.apache.flink.table.annotation.ArgumentHint; | ||
import org.apache.flink.table.annotation.DataTypeHint; | ||
import org.apache.flink.table.annotation.ProcedureHint; | ||
import org.apache.flink.table.procedure.ProcedureContext; | ||
|
||
/** | ||
* Rollback to timestamp procedure. Usage: | ||
* | ||
* <pre><code> | ||
* -- rollback to a timestamp | ||
* CALL sys.rollback_to_timestamp(`table` => 'tableId', timestamp => timestamp) | ||
* </code></pre> | ||
*/ | ||
public class RollbackToTimestampProcedure extends ProcedureBase { | ||
|
||
public static final String IDENTIFIER = "rollback_to_timestamp"; | ||
|
||
@ProcedureHint( | ||
argument = { | ||
@ArgumentHint(name = "table", type = @DataTypeHint("STRING")), | ||
@ArgumentHint(name = "timestamp", type = @DataTypeHint("BIGINT")) | ||
}) | ||
public String[] call(ProcedureContext procedureContext, String tableId, Long timestamp) | ||
throws Catalog.TableNotExistException { | ||
Table table = catalog.getTable(Identifier.fromString(tableId)); | ||
FileStoreTable fileStoreTable = (FileStoreTable) table; | ||
Snapshot snapshot = fileStoreTable.snapshotManager().earlierOrEqualTimeMills(timestamp); | ||
Preconditions.checkNotNull( | ||
snapshot, String.format("count not find snapshot earlier than %s", timestamp)); | ||
fileStoreTable.rollbackTo(snapshot.id()); | ||
return new String[] {"Success"}; | ||
} | ||
|
||
@Override | ||
public String identifier() { | ||
return IDENTIFIER; | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.