-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
[Enhancement] Do checkpoint on follower node to relieve memory pressure on leader #52103
Open
gengjun-git
wants to merge
3
commits into
StarRocks:main
Choose a base branch
from
gengjun-git:do_checkpoint_by_follower
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 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
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
21 changes: 21 additions & 0 deletions
21
fe/fe-core/src/main/java/com/starrocks/journal/CheckpointException.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,21 @@ | ||
// 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.journal; | ||
|
||
public class CheckpointException extends Exception { | ||
public CheckpointException(String msg) { | ||
super(msg); | ||
} | ||
} |
159 changes: 159 additions & 0 deletions
159
fe/fe-core/src/main/java/com/starrocks/journal/CheckpointWorker.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,159 @@ | ||
// 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.journal; | ||
|
||
import com.starrocks.common.Config; | ||
import com.starrocks.common.FeConstants; | ||
import com.starrocks.common.util.FrontendDaemon; | ||
import com.starrocks.leader.CheckpointController; | ||
import com.starrocks.persist.ImageLoader; | ||
import com.starrocks.rpc.ThriftConnectionPool; | ||
import com.starrocks.rpc.ThriftRPCRequestExecutor; | ||
import com.starrocks.server.GlobalStateMgr; | ||
import com.starrocks.thrift.TFinishCheckpointRequest; | ||
import com.starrocks.thrift.TFinishCheckpointResponse; | ||
import com.starrocks.thrift.TStatus; | ||
import com.starrocks.thrift.TStatusCode; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.apache.thrift.TException; | ||
|
||
import java.io.IOException; | ||
|
||
public abstract class CheckpointWorker extends FrontendDaemon { | ||
public static final Logger LOG = LogManager.getLogger(CheckpointWorker.class); | ||
|
||
protected final String imageDir; | ||
protected final Journal journal; | ||
private NextPoint nextPoint; | ||
protected final GlobalStateMgr servingGlobalState; | ||
|
||
public CheckpointWorker(String name, Journal journal, String subDir) { | ||
super(name, FeConstants.checkpoint_interval_second * 1000L); | ||
this.servingGlobalState = GlobalStateMgr.getServingState(); | ||
this.imageDir = servingGlobalState.getImageDir() + subDir; | ||
this.journal = journal; | ||
} | ||
|
||
abstract void doCheckpoint(long epoch, long journalId) throws Exception; | ||
abstract CheckpointController getCheckpointController(); | ||
abstract boolean isBelongToGlobalStateMgr(); | ||
|
||
public void setNextCheckpoint(long epoch, long journalId) throws CheckpointException { | ||
if (epoch != servingGlobalState.getEpoch()) { | ||
throw new CheckpointException(String.format("epoch: %d is not equal to current epoch: %d", | ||
epoch, servingGlobalState.getEpoch())); | ||
} | ||
if (journalId > journal.getMaxJournalId()) { | ||
throw new CheckpointException(String.format("can not find journal id: %d , current max journal id is: %d", | ||
journalId, journal.getMaxJournalId())); | ||
} | ||
|
||
nextPoint = new NextPoint(epoch, journalId); | ||
} | ||
|
||
@Override | ||
protected void runAfterCatalogReady() { | ||
if (nextPoint == null) { | ||
return; | ||
} | ||
if (nextPoint.journalId <= getImageJournalId()) { | ||
return; | ||
} | ||
if (nextPoint.epoch != servingGlobalState.getEpoch()) { | ||
return; | ||
} | ||
|
||
createImage(nextPoint.epoch, nextPoint.journalId); | ||
} | ||
|
||
private void createImage(long epoch, long journalId) { | ||
try { | ||
doCheckpoint(epoch, journalId); | ||
} catch (Exception e) { | ||
LOG.warn("create image failed", e); | ||
finishCheckpoint(epoch, journalId, false, e.getMessage()); | ||
return; | ||
} | ||
|
||
finishCheckpoint(epoch, journalId, true, "success"); | ||
} | ||
|
||
private void finishCheckpoint(long epoch, long journalId, boolean isSuccess, String message) { | ||
if (epoch != servingGlobalState.getEpoch()) { | ||
LOG.warn("epoch outdated, do not finish checkpoint"); | ||
return; | ||
} | ||
|
||
String nodeName = servingGlobalState.getNodeMgr().getNodeName(); | ||
if (servingGlobalState.isLeader()) { | ||
CheckpointController controller = getCheckpointController(); | ||
if (isSuccess) { | ||
try { | ||
controller.finishCheckpoint(journalId, nodeName); | ||
} catch (CheckpointException e) { | ||
LOG.warn("finish checkpoint failed", e); | ||
} | ||
} else { | ||
controller.cancelCheckpoint(nodeName, message); | ||
} | ||
} else { | ||
TFinishCheckpointRequest request = new TFinishCheckpointRequest(); | ||
request.setJournal_id(journalId); | ||
request.setNode_name(nodeName); | ||
request.setIs_success(isSuccess); | ||
request.setMessage(message); | ||
request.setIs_global_state_mgr(isBelongToGlobalStateMgr()); | ||
|
||
try { | ||
TFinishCheckpointResponse response = ThriftRPCRequestExecutor.call( | ||
ThriftConnectionPool.frontendPool, | ||
servingGlobalState.getNodeMgr().getLeaderRpcEndpoint(), | ||
Config.thrift_rpc_timeout_ms, | ||
client -> client.finishCheckpoint(request)); | ||
TStatus status = response.getStatus(); | ||
if (status.getStatus_code() != TStatusCode.OK) { | ||
String errMessage = ""; | ||
if (status.getError_msgs() != null && !status.getError_msgs().isEmpty()) { | ||
errMessage = String.join(",", status.getError_msgs()); | ||
} | ||
LOG.warn("call finishCheckpoint failed, error message: {}", errMessage); | ||
} | ||
} catch (TException e) { | ||
LOG.warn("call finishCheckpoint failed", e); | ||
} | ||
} | ||
} | ||
|
||
private long getImageJournalId() { | ||
try { | ||
ImageLoader imageLoader = new ImageLoader(imageDir); | ||
return imageLoader.getImageJournalId(); | ||
} catch (IOException e) { | ||
LOG.warn("get image journal id failed", e); | ||
return 0; | ||
} | ||
} | ||
|
||
static class NextPoint { | ||
private final long epoch; | ||
private final long journalId; | ||
|
||
public NextPoint(long epoch, long journalId) { | ||
this.epoch = epoch; | ||
this.journalId = journalId; | ||
} | ||
} | ||
} |
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.