forked from apache/incubator-seata
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: support raft cluster (apache#5226)
- Loading branch information
1 parent
2d6ae18
commit e9919e4
Showing
131 changed files
with
5,659 additions
and
297 deletions.
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
54 changes: 54 additions & 0 deletions
54
common/src/main/java/io/seata/common/metadata/ClusterRole.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,54 @@ | ||
/* | ||
* Copyright 1999-2019 Seata.io Group. | ||
* | ||
* 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 | ||
* | ||
* 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 io.seata.common.metadata; | ||
|
||
/** | ||
* @author funkye | ||
*/ | ||
public enum ClusterRole { | ||
|
||
/** | ||
* raft mode leader | ||
*/ | ||
LEADER(0), | ||
/** | ||
* raft mode follower | ||
*/ | ||
FOLLOWER(1), | ||
/** | ||
* raft mode learner | ||
*/ | ||
LEARNER(2), | ||
/** | ||
* cluster mode member | ||
*/ | ||
MEMBER(3); | ||
|
||
private int roleCode; | ||
|
||
ClusterRole(int roleCode) { | ||
this.roleCode = roleCode; | ||
} | ||
|
||
public int getRoleCode() { | ||
return roleCode; | ||
} | ||
|
||
public void setRoleCode(int roleCode) { | ||
this.roleCode = roleCode; | ||
} | ||
|
||
} |
115 changes: 115 additions & 0 deletions
115
common/src/main/java/io/seata/common/metadata/Metadata.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,115 @@ | ||
/* | ||
* Copyright 1999-2019 Seata.io Group. | ||
* | ||
* 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 | ||
* | ||
* 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 io.seata.common.metadata; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
|
||
import io.seata.common.store.StoreMode; | ||
import io.seata.common.util.StringUtils; | ||
|
||
/** | ||
* @author funkye | ||
*/ | ||
public class Metadata { | ||
|
||
private final Map<String/*vgroup*/, Map<String/*raft-group*/, Node>> leaders = new ConcurrentHashMap<>(); | ||
|
||
private final Map<String/*vgroup*/, Map<String/*raft-group*/, Long/*term*/>> clusterTerm = new ConcurrentHashMap<>(); | ||
|
||
private final Map<String/*vgroup*/, Map<String/*raft-group*/, List<Node>>> clusterNodes = | ||
new ConcurrentHashMap<>(); | ||
|
||
private StoreMode storeMode = StoreMode.FILE; | ||
|
||
public Node getLeader(String clusterName) { | ||
Map<String/*raft-group*/, Node> map = leaders.computeIfAbsent(clusterName, k -> new ConcurrentHashMap<>()); | ||
List<Node> nodes = new ArrayList<>(map.values()); | ||
return nodes.size() > 0 ? nodes.get(ThreadLocalRandom.current().nextInt(nodes.size())) : null; | ||
} | ||
|
||
public void setLeaderNode(String clusterName, Node node) { | ||
String group = node.getGroup(); | ||
Map<String/*raft-group*/, Node> map = leaders.computeIfAbsent(clusterName, k -> new ConcurrentHashMap<>()); | ||
map.put(group, node); | ||
this.leaders.put(clusterName, map); | ||
} | ||
|
||
public List<Node> getNodes(String clusterName, String group) { | ||
return clusterNodes.computeIfAbsent(clusterName, k -> new ConcurrentHashMap<>()).get(group); | ||
} | ||
|
||
public List<Node> getNodes(String clusterName) { | ||
return clusterNodes.computeIfAbsent(clusterName, k -> new ConcurrentHashMap<>()).values().stream() | ||
.flatMap(List::stream).collect(ArrayList::new, ArrayList::add, ArrayList::addAll); | ||
} | ||
|
||
public void setNodes(String clusterName, String group, List<Node> nodes) { | ||
this.clusterNodes.computeIfAbsent(clusterName, k -> new ConcurrentHashMap<>()).put(group, nodes); | ||
} | ||
|
||
public boolean containsGroup(String group) { | ||
return clusterNodes.containsKey(group); | ||
} | ||
|
||
public Set<String> groups(String clusterName) { | ||
return clusterNodes.computeIfAbsent(clusterName, k -> new ConcurrentHashMap<>()).keySet(); | ||
} | ||
|
||
public StoreMode getStoreMode() { | ||
return storeMode; | ||
} | ||
|
||
public boolean isRaftMode() { | ||
return Objects.equals(storeMode, StoreMode.RAFT); | ||
} | ||
|
||
public void setStoreMode(StoreMode storeMode) { | ||
this.storeMode = storeMode; | ||
} | ||
|
||
public Map<String, Long> getClusterTerm(String clusterName) { | ||
return clusterTerm.computeIfAbsent(clusterName, k -> new ConcurrentHashMap<>()); | ||
} | ||
|
||
public void refreshMetadata(String clusterName, MetadataResponse metadataResponse) { | ||
List<Node> list = new ArrayList<>(); | ||
for (Node node : metadataResponse.getNodes()) { | ||
if (node.getRole() == ClusterRole.LEADER) { | ||
this.setLeaderNode(clusterName, node); | ||
} | ||
list.add(node); | ||
} | ||
this.storeMode = StoreMode.get(metadataResponse.getStoreMode()); | ||
if (!list.isEmpty()) { | ||
String group = list.get(0).getGroup(); | ||
this.setNodes(clusterName, group, list); | ||
this.clusterTerm.computeIfAbsent(clusterName, k -> new ConcurrentHashMap<>()).put(group, | ||
metadataResponse.getTerm()); | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return StringUtils.toString(this); | ||
} | ||
|
||
} |
Oops, something went wrong.