-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
[Feature] Support Cluster Snapshot Backup: SQL Interface and meta data (part 1) #54447
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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
118 changes: 118 additions & 0 deletions
118
fe/fe-core/src/main/java/com/starrocks/lake/snapshot/ClusterSnapshotMgr.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,118 @@ | ||
// Copyright 2021-present StarRocks, Inc. All rights reserved. | ||
// | ||
// Licensed 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 | ||
// | ||
// https://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 com.starrocks.lake.snapshot; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
import com.starrocks.persist.ClusterSnapshotLog; | ||
import com.starrocks.persist.ImageWriter; | ||
import com.starrocks.persist.gson.GsonPostProcessable; | ||
import com.starrocks.persist.metablock.SRMetaBlockEOFException; | ||
import com.starrocks.persist.metablock.SRMetaBlockException; | ||
import com.starrocks.persist.metablock.SRMetaBlockID; | ||
import com.starrocks.persist.metablock.SRMetaBlockReader; | ||
import com.starrocks.persist.metablock.SRMetaBlockWriter; | ||
import com.starrocks.server.GlobalStateMgr; | ||
import com.starrocks.sql.ast.AdminSetAutomatedSnapshotOffStmt; | ||
import com.starrocks.sql.ast.AdminSetAutomatedSnapshotOnStmt; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import java.io.IOException; | ||
|
||
// only used for AUTOMATED snapshot for now | ||
public class ClusterSnapshotMgr implements GsonPostProcessable { | ||
public static final Logger LOG = LogManager.getLogger(ClusterSnapshotMgr.class); | ||
public static final String AUTOMATED_NAME_PREFIX = "automated_cluster_snapshot"; | ||
|
||
@SerializedName(value = "automatedSnapshotSvName") | ||
private String automatedSnapshotSvName = ""; | ||
|
||
public ClusterSnapshotMgr() {} | ||
|
||
// Turn on automated snapshot, use stmt for extension in future | ||
public void setAutomatedSnapshotOn(AdminSetAutomatedSnapshotOnStmt stmt) { | ||
String storageVolumeName = stmt.getStorageVolumeName(); | ||
setAutomatedSnapshotOn(storageVolumeName); | ||
|
||
ClusterSnapshotLog log = new ClusterSnapshotLog(); | ||
log.setCreateSnapshotNamePrefix(AUTOMATED_NAME_PREFIX, storageVolumeName); | ||
GlobalStateMgr.getCurrentState().getEditLog().logClusterSnapshotLog(log); | ||
} | ||
|
||
protected void setAutomatedSnapshotOn(String storageVolumeName) { | ||
automatedSnapshotSvName = storageVolumeName; | ||
} | ||
|
||
public String getAutomatedSnapshotSvName() { | ||
return automatedSnapshotSvName; | ||
} | ||
|
||
public boolean isAutomatedSnapshotOn() { | ||
return automatedSnapshotSvName != null && !automatedSnapshotSvName.isEmpty(); | ||
} | ||
|
||
// Turn off automated snapshot, use stmt for extension in future | ||
public void setAutomatedSnapshotOff(AdminSetAutomatedSnapshotOffStmt stmt) { | ||
setAutomatedSnapshotOff(); | ||
|
||
ClusterSnapshotLog log = new ClusterSnapshotLog(); | ||
log.setDropSnapshot(AUTOMATED_NAME_PREFIX); | ||
GlobalStateMgr.getCurrentState().getEditLog().logClusterSnapshotLog(log); | ||
} | ||
|
||
protected void setAutomatedSnapshotOff() { | ||
// drop AUTOMATED snapshot | ||
automatedSnapshotSvName = ""; | ||
} | ||
|
||
public void replayLog(ClusterSnapshotLog log) { | ||
ClusterSnapshotLog.ClusterSnapshotLogType logType = log.getType(); | ||
switch (logType) { | ||
case CREATE_SNAPSHOT_PREFIX: { | ||
String createSnapshotNamePrefix = log.getCreateSnapshotNamePrefix(); | ||
String storageVolumeName = log.getStorageVolumeName(); | ||
if (createSnapshotNamePrefix.equals(AUTOMATED_NAME_PREFIX)) { | ||
setAutomatedSnapshotOn(storageVolumeName); | ||
} | ||
break; | ||
} | ||
case DROP_SNAPSHOT: { | ||
String dropSnapshotName = log.getDropSnapshotName(); | ||
if (dropSnapshotName.equals(AUTOMATED_NAME_PREFIX)) { | ||
setAutomatedSnapshotOff(); | ||
} | ||
break; | ||
} | ||
default: { | ||
LOG.warn("Invalid Cluster Snapshot Log Type {}", logType); | ||
} | ||
} | ||
} | ||
|
||
public void save(ImageWriter imageWriter) throws IOException, SRMetaBlockException { | ||
SRMetaBlockWriter writer = imageWriter.getBlockWriter(SRMetaBlockID.CLUSTER_SNAPSHOT_MGR, 1); | ||
writer.writeJson(this); | ||
writer.close(); | ||
} | ||
|
||
public void load(SRMetaBlockReader reader) | ||
throws SRMetaBlockEOFException, IOException, SRMetaBlockException { | ||
ClusterSnapshotMgr data = reader.readJson(ClusterSnapshotMgr.class); | ||
} | ||
|
||
@Override | ||
public void gsonPostProcess() throws IOException { | ||
} | ||
} | ||
78 changes: 78 additions & 0 deletions
78
fe/fe-core/src/main/java/com/starrocks/persist/ClusterSnapshotLog.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,78 @@ | ||
// Copyright 2021-present StarRocks, Inc. All rights reserved. | ||
// | ||
// Licensed 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 | ||
// | ||
// https://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 com.starrocks.persist; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
import com.starrocks.common.io.Text; | ||
import com.starrocks.common.io.Writable; | ||
import com.starrocks.persist.gson.GsonUtils; | ||
|
||
import java.io.DataInput; | ||
import java.io.DataOutput; | ||
import java.io.IOException; | ||
|
||
public class ClusterSnapshotLog implements Writable { | ||
public enum ClusterSnapshotLogType { NONE, CREATE_SNAPSHOT_PREFIX, DROP_SNAPSHOT } | ||
@SerializedName(value = "type") | ||
private ClusterSnapshotLogType type = ClusterSnapshotLogType.NONE; | ||
// For CREATE_SNAPSHOT_PREFIX | ||
@SerializedName(value = "createSnapshotNamePrefix") | ||
private String createSnapshotNamePrefix = ""; | ||
@SerializedName(value = "storageVolumeName") | ||
private String storageVolumeName = ""; | ||
// For DROP_SNAPSHOT | ||
@SerializedName(value = "dropSnapshotName") | ||
private String dropSnapshotName = ""; | ||
|
||
public ClusterSnapshotLog() {} | ||
|
||
public void setCreateSnapshotNamePrefix(String createSnapshotNamePrefix, String storageVolumeName) { | ||
this.type = ClusterSnapshotLogType.CREATE_SNAPSHOT_PREFIX; | ||
this.createSnapshotNamePrefix = createSnapshotNamePrefix; | ||
this.storageVolumeName = storageVolumeName; | ||
} | ||
|
||
public void setDropSnapshot(String dropSnapshotName) { | ||
this.type = ClusterSnapshotLogType.DROP_SNAPSHOT; | ||
this.dropSnapshotName = dropSnapshotName; | ||
} | ||
|
||
public ClusterSnapshotLogType getType() { | ||
return type; | ||
} | ||
|
||
public String getCreateSnapshotNamePrefix() { | ||
return this.createSnapshotNamePrefix; | ||
} | ||
|
||
public String getStorageVolumeName() { | ||
return this.storageVolumeName; | ||
} | ||
|
||
public String getDropSnapshotName() { | ||
return this.dropSnapshotName; | ||
} | ||
|
||
public static ClusterSnapshotLog read(DataInput in) throws IOException { | ||
String json = Text.readString(in); | ||
return GsonUtils.GSON.fromJson(json, ClusterSnapshotLog.class); | ||
} | ||
|
||
@Override | ||
public void write(DataOutput out) throws IOException { | ||
String json = GsonUtils.GSON.toJson(this); | ||
Text.writeString(out, json); | ||
} | ||
} | ||
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. The most risky bug in this code is: You can modify the code like this: public static ClusterSnapshotLog read(DataInput in) throws IOException {
try {
String json = Text.readString(in);
ClusterSnapshotLog log = GsonUtils.GSON.fromJson(json, ClusterSnapshotLog.class);
// Add checks to ensure required fields are present and valid
if (log.type == null) {
throw new IOException("ClusterSnapshotLog type cannot be null");
}
return log;
} catch (Exception e) {
throw new IOException("Failed to read ClusterSnapshotLog from input", e);
}
} |
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
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.
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.
The most risky bug in this code is:
In the
load
method, the deserialized objectdata
is not used to update the state of the currentClusterSnapshotMgr
. This means that the data read from theSRMetaBlockReader
, which presumably represents a saved state, does nothing to restore or set the internal state of the object using it.You can modify the code like this: