use of com.alipay.sofa.jraft.util.concurrent.ConcurrentHashSet in project sofa-jraft by sofastack.
the class AppendEntriesRequestProcessorTest method setup.
@Override
public void setup() {
super.setup();
this.asyncContext = new MockAsyncContext() {
@Override
public Connection getConnection() {
return AppendEntriesRequestProcessorTest.this.conn;
}
};
Set<PeerPair> pairs = new ConcurrentHashSet<>();
pairs.add(new PeerPair(this.peerIdStr, this.serverId));
Mockito.when(this.conn.getAttribute(AppendEntriesRequestProcessor.PAIR_ATTR)).thenReturn(pairs);
}
use of com.alipay.sofa.jraft.util.concurrent.ConcurrentHashSet in project mmqtt by MrHKing.
the class ServerMemberManager method memberChange.
synchronized boolean memberChange(Collection<Member> members) {
if (members == null || members.isEmpty()) {
return false;
}
boolean isContainSelfIp = members.stream().anyMatch(ipPortTmp -> Objects.equals(localAddress, ipPortTmp.getAddress()));
if (isContainSelfIp) {
isInIpList = true;
} else {
isInIpList = false;
members.add(this.self);
Loggers.CLUSTER.warn("[serverlist] self ip {} not in serverlist {}", self, members);
}
// If the number of old and new clusters is different, the cluster information
// must have changed; if the number of clusters is the same, then compare whether
// there is a difference; if there is a difference, then the cluster node changes
// are involved and all recipients need to be notified of the node change event
boolean hasChange = members.size() != serverList.size();
ConcurrentSkipListMap<String, Member> tmpMap = new ConcurrentSkipListMap<>();
Set<String> tmpAddressInfo = new ConcurrentHashSet<>();
for (Member member : members) {
final String address = member.getAddress();
Member existMember = serverList.get(address);
if (existMember == null) {
hasChange = true;
tmpMap.put(address, member);
} else {
// to keep extendInfo and abilities that report dynamically.
tmpMap.put(address, existMember);
}
if (NodeState.UP.equals(member.getState())) {
tmpAddressInfo.add(address);
}
}
serverList = tmpMap;
memberAddressInfos = tmpAddressInfo;
Collection<Member> finalMembers = allMembers();
Loggers.CLUSTER.warn("[serverlist] updated to : {}", finalMembers);
// that the event publication is sequential
if (hasChange) {
MemberUtil.syncToFile(finalMembers);
Event event = MembersChangeEvent.builder().members(finalMembers).build();
NotifyCenter.publishEvent(event);
}
return hasChange;
}
use of com.alipay.sofa.jraft.util.concurrent.ConcurrentHashSet in project sofa-jraft by sofastack.
the class AppendEntriesRequestProcessor method getOrCreatePeerRequestContext.
@SuppressWarnings("unchecked")
PeerRequestContext getOrCreatePeerRequestContext(final String groupId, final PeerPair pair, final Connection conn) {
ConcurrentMap<PeerPair, PeerRequestContext> groupContexts = this.peerRequestContexts.get(groupId);
if (groupContexts == null) {
groupContexts = new ConcurrentHashMap<>();
final ConcurrentMap<PeerPair, PeerRequestContext> existsCtxs = this.peerRequestContexts.putIfAbsent(groupId, groupContexts);
if (existsCtxs != null) {
groupContexts = existsCtxs;
}
}
PeerRequestContext peerCtx = groupContexts.get(pair);
if (peerCtx == null) {
synchronized (Utils.withLockObject(groupContexts)) {
peerCtx = groupContexts.get(pair);
// double check in lock
if (peerCtx == null) {
// only one thread to process append entries for every jraft node
final PeerId peer = new PeerId();
final boolean parsed = peer.parse(pair.local);
assert (parsed);
final Node node = NodeManager.getInstance().get(groupId, peer);
assert (node != null);
peerCtx = new PeerRequestContext(groupId, pair, node.getRaftOptions().getMaxReplicatorInflightMsgs());
groupContexts.put(pair, peerCtx);
}
}
}
// Add the pair to connection attribute metadata.
if (conn != null) {
Set<PeerPair> pairs;
if ((pairs = (Set<AppendEntriesRequestProcessor.PeerPair>) conn.getAttribute(PAIR_ATTR)) == null) {
pairs = new ConcurrentHashSet<>();
Set<PeerPair> existsPairs = (Set<PeerPair>) conn.setAttributeIfAbsent(PAIR_ATTR, pairs);
if (existsPairs != null) {
pairs = existsPairs;
}
}
pairs.add(pair);
}
return peerCtx;
}
Aggregations