-
Notifications
You must be signed in to change notification settings - Fork 377
Add a registration function for table allocation in master-slave mode. #3920
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
Open
wardlican
wants to merge
15
commits into
apache:master
Choose a base branch
from
wardlican:amoro#3919
base: master
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
15 commits
Select commit
Hold shift + click to select a range
42a5161
[Subtask]: Add a registration function for table allocation in master…
050b30a
[Subtask]: Add a registration function for table allocation in master…
edab58d
[Subtask]: Add a registration function for table allocation in master…
335bb39
[Subtask]: Replace zk with mocking. #3919
2ff65e6
[Subtask]: Replace zk with mocking. #3919
ce228c2
[Subtask]: Add a registration function for table allocation in master…
9b8ed28
[Subtask]: Use a new configuration item to control whether master & s…
bf35401
[Subtask]: Add a registration function for table allocation in master…
1f8df23
[Subtask]: Add a registration function for table allocation in master…
d9fe303
[Subtask]: Add a registration function for table allocation in master…
e7371c8
[Subtask]: Replace zk with mocking. #3919
b6dd04a
[Subtask]: Replace zk with mocking. #3919
77ce644
[Subtask]: Add a registration function for table allocation in master…
6908549
[Subtask]: Add a registration function for table allocation in master…
6f7d1fc
[Subtask]: Fixing conflicts after a forced push following a rebase.
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 hidden or 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 |
|---|---|---|
|
|
@@ -28,6 +28,8 @@ | |
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.UUID; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.Executors; | ||
|
|
@@ -135,6 +137,44 @@ public void waitFollowerShip() throws InterruptedException { | |
| LOG.info("Became the follower of AMS (Database lease)"); | ||
| } | ||
|
|
||
| @Override | ||
| public void registAndElect() throws Exception { | ||
| boolean isMasterSlaveMode = serviceConfig.getBoolean(AmoroManagementConf.USE_MASTER_SLAVE_MODE); | ||
| if (!isMasterSlaveMode) { | ||
| LOG.debug("Master-slave mode is not enabled, skip node registration"); | ||
| return; | ||
| } | ||
| // In master-slave mode, register node to database by writing OPTIMIZING_SERVICE info | ||
| // This is similar to ZK mode registering ephemeral nodes | ||
| long now = System.currentTimeMillis(); | ||
| String optimizingInfoJson = JacksonUtil.toJSONString(optimizingServiceServerInfo); | ||
| try { | ||
| doAsIgnoreError( | ||
| HaLeaseMapper.class, | ||
| mapper -> { | ||
| int updated = | ||
| mapper.updateServerInfo( | ||
| clusterName, OPTIMIZING_SERVICE, nodeId, nodeIp, optimizingInfoJson, now); | ||
| if (updated == 0) { | ||
| mapper.insertServerInfoIfAbsent( | ||
|
Contributor
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. why not need to lease_expire_ts to first register time? |
||
| clusterName, OPTIMIZING_SERVICE, nodeId, nodeIp, optimizingInfoJson, now); | ||
| } | ||
| }); | ||
| LOG.info( | ||
| "Registered AMS node to database: nodeId={}, optimizingService={}", | ||
| nodeId, | ||
| optimizingServiceServerInfo); | ||
| } catch (Exception e) { | ||
| LOG.error("Failed to register node to database", e); | ||
| throw e; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean hasLeadership() { | ||
| return isLeader.get(); | ||
| } | ||
|
|
||
| /** Closes the heartbeat executor safely. */ | ||
| @Override | ||
| public void close() { | ||
|
|
@@ -147,9 +187,6 @@ public void close() { | |
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void registAndElect() throws Exception {} | ||
|
|
||
| private class HeartbeatRunnable implements Runnable { | ||
| @Override | ||
| public void run() { | ||
|
|
@@ -304,6 +341,40 @@ private void onLeaderLost() { | |
| } | ||
| } | ||
|
|
||
| @Override | ||
| public List<AmsServerInfo> getAliveNodes() { | ||
| List<AmsServerInfo> aliveNodes = new ArrayList<>(); | ||
| if (!isLeader.get()) { | ||
| LOG.warn("Only leader node can get alive nodes list"); | ||
| return aliveNodes; | ||
| } | ||
| try { | ||
| long currentTime = System.currentTimeMillis(); | ||
| List<HaLeaseMeta> leases = | ||
| getAs( | ||
| HaLeaseMapper.class, | ||
| mapper -> mapper.selectLeasesByService(clusterName, OPTIMIZING_SERVICE)); | ||
| for (HaLeaseMeta lease : leases) { | ||
| // Only include nodes with valid (non-expired) leases | ||
| if (lease.getLeaseExpireTs() != null && lease.getLeaseExpireTs() > currentTime) { | ||
| if (lease.getServerInfoJson() != null && !lease.getServerInfoJson().isEmpty()) { | ||
| try { | ||
| AmsServerInfo nodeInfo = | ||
| JacksonUtil.parseObject(lease.getServerInfoJson(), AmsServerInfo.class); | ||
| aliveNodes.add(nodeInfo); | ||
| } catch (Exception e) { | ||
| LOG.warn("Failed to parse server info for node {}", lease.getNodeId(), e); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } catch (Exception e) { | ||
| LOG.error("Failed to get alive nodes from database", e); | ||
| throw e; | ||
| } | ||
| return aliveNodes; | ||
| } | ||
|
|
||
| private AmsServerInfo buildServerInfo(String host, int thriftBindPort, int restBindPort) { | ||
| AmsServerInfo amsServerInfo = new AmsServerInfo(); | ||
| amsServerInfo.setHost(host); | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
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.
method name charge to registerAndElect