Skip to content

Commit

Permalink
sofa-registry-meta-select (sofastack#235)
Browse files Browse the repository at this point in the history
* annotation

* 添加文章连接

* delete node

Co-authored-by: dongdong.yang <[email protected]>
  • Loading branch information
Webster-Yang and ddy1012 authored Jun 16, 2022
1 parent 9dc6eec commit 6e740c5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ SOFARegistry 是蚂蚁金服开源的一个生产级、高时效、高可用的
- 源码解析
- [SlotTable](https://www.sofastack.tech/projects/sofa-registry/code-analyze/code-analyze-slottable/)
- [发布订阅推送](https://www.sofastack.tech/projects/sofa-registry/code-analyze/code-analyze-publish-subscription-push/)
- [registry meta 选主](https://www.sofastack.tech/projects/sofa-registry/code-analyze/code-analyze-registry-meta/)


## 贡献
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void waitingUnthrowable() {

public void elect() {
synchronized (this) {
if (isObserver) {
if (isObserver) {//如果是Observer,不参与选主
leaderInfo = doQuery();
LOG.info("meta role: Observer, leaderInfo: {}", leaderInfo);
} else {
Expand All @@ -97,7 +97,7 @@ public void elect() {

if (amILeader()) {
onIamLeader();
} else {
} else {//我不是leader
onIamNotLeader();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,23 @@ public class MetaJdbcLeaderElector extends AbstractLeaderElector implements Reco
*/
@Override
protected LeaderInfo doElect() {
//1、查询锁
DistributeLockDomain lock =
distributeLockMapper.queryDistLock(defaultCommonConfig.getClusterId(tableName()), lockName);

/** compete and return leader */
//2、不存在则创建锁
if (lock == null) {
return competeLeader(defaultCommonConfig.getClusterId(tableName()));
}

//3、判断角色
ElectorRole role = amILeader(lock.getOwner()) ? ElectorRole.LEADER : ElectorRole.FOLLOWER;
if (role == ElectorRole.LEADER) {
lock = onLeaderWorking(lock, myself());
lock = onLeaderWorking(lock, myself());//4、提交心跳
} else {
lock = onFollowWorking(lock, myself());
lock = onFollowWorking(lock, myself());//5、判断过期与否,如过期,则cas竞争锁
}
LeaderInfo result = leaderFrom(lock);
LeaderInfo result = leaderFrom(lock);//6、锁信息转换为LeaderInfo
LOG.info("meta role : {}, leaderInfo: {}", role, result);
return result;
}
Expand Down

0 comments on commit 6e740c5

Please sign in to comment.