-
Notifications
You must be signed in to change notification settings - Fork 990
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
[core] Introduce RollbackToTimestamp Procedure and Action #4410
Merged
JingsongLi
merged 4 commits into
apache:master
from
xuzifu666:rollback_timestamp_support
Oct 31, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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
59 changes: 59 additions & 0 deletions
59
...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,59 @@ | ||
/* | ||
* 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 to timestamp procedure. Usage: | ||
* | ||
* <pre><code> | ||
* -- rollback to the snapshot which earlier or equal than timestamp. | ||
* 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 { | ||
Preconditions.checkNotNull(tableId, "table can not be empty"); | ||
Table table = catalog.getTable(Identifier.fromString(tableId)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. check table not empty |
||
FileStoreTable fileStoreTable = (FileStoreTable) table; | ||
Snapshot snapshot = fileStoreTable.snapshotManager().earlierOrEqualTimeMills(timestamp); | ||
Preconditions.checkNotNull( | ||
snapshot, String.format("count not find snapshot earlier than %s", timestamp)); | ||
long snapshotId = snapshot.id(); | ||
fileStoreTable.rollbackTo(snapshotId); | ||
return new String[] {String.format("Success roll back to snapshot: %s .", snapshotId)}; | ||
} | ||
|
||
@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(); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...-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,66 @@ | ||
/* | ||
* 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 the snapshot which earlier or equal than 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)); | ||
long snapshotId = snapshot.id(); | ||
fileStoreTable.rollbackTo(snapshotId); | ||
return new String[] {String.format("Success roll back to snapshot: %s .", snapshotId)}; | ||
} | ||
|
||
@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
Oops, something went wrong.
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.
rollback_to -> rollback_to_timestamp
snapshot_id -> timestamp
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.
My mistake, Thanks! @LinMingQiang