Skip to content

Commit

Permalink
Hotfix frequent member change logs after #3168 (#3174)
Browse files Browse the repository at this point in the history
* Fix operate for unmodifiableList error

* Hotfix frequent member change logs after #3168
  • Loading branch information
KomachiSion authored Jun 25, 2020
1 parent aac1c6f commit 49c163b
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class MemberMetaDataConstants {
public static final String VERSION = "version";

public static final String[] META_KEY_LIST = new String[] {SITE_KEY, AD_WEIGHT, RAFT_PORT, WEIGHT,
LAST_REFRESH_TIME, VERSION};
LAST_REFRESH_TIME, VERSION};

public static final String[] META_KEY_LIST_WITHOUT_LAST_REFRESH_TIME = new String[] {SITE_KEY, AD_WEIGHT, RAFT_PORT,
WEIGHT, VERSION};
}
70 changes: 62 additions & 8 deletions core/src/main/java/com/alibaba/nacos/core/cluster/MemberUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,12 @@ public static Collection<Member> multiParse(Collection<String> addresses) {
* @param member {@link Member}
*/
public static void onSuccess(Member member) {
Member cloneMember = new Member();
copy(member, cloneMember);
manager.getMemberAddressInfos().add(member.getAddress());
member.setState(NodeState.UP);
member.setFailAccessCnt(0);
manager.update(member);
cloneMember.setState(NodeState.UP);
cloneMember.setFailAccessCnt(0);
manager.update(cloneMember);
}

public static void onFail(Member member) {
Expand All @@ -135,18 +137,20 @@ public static void onFail(Member member) {
* @param ex {@link Throwable}
*/
public static void onFail(Member member, Throwable ex) {
Member cloneMember = new Member();
copy(member, cloneMember);
manager.getMemberAddressInfos().remove(member.getAddress());
member.setState(NodeState.SUSPICIOUS);
member.setFailAccessCnt(member.getFailAccessCnt() + 1);
cloneMember.setState(NodeState.SUSPICIOUS);
cloneMember.setFailAccessCnt(member.getFailAccessCnt() + 1);
int maxFailAccessCnt = ApplicationUtils.getProperty("nacos.core.member.fail-access-cnt", Integer.class, 3);

// If the number of consecutive failures to access the target node reaches
// a maximum, or the link request is rejected, the state is directly down
if (member.getFailAccessCnt() > maxFailAccessCnt || StringUtils
if (cloneMember.getFailAccessCnt() > maxFailAccessCnt || StringUtils
.containsIgnoreCase(ex.getMessage(), TARGET_MEMBER_CONNECT_REFUSE_ERRMSG)) {
member.setState(NodeState.DOWN);
cloneMember.setState(NodeState.DOWN);
}
manager.update(member);
manager.update(cloneMember);
}

/**
Expand Down Expand Up @@ -209,13 +213,63 @@ public static Collection<Member> readServerConf(Collection<String> members) {
return nodes;
}

/**
* Select target members with filter.
*
* @param members original members
* @param filter filter
* @return target members
*/
public static Set<Member> selectTargetMembers(Collection<Member> members, Predicate<Member> filter) {
return members.stream().filter(filter).collect(Collectors.toSet());
}

/**
* Get address list of members.
*
* @param members members
* @return address list
*/
public static List<String> simpleMembers(Collection<Member> members) {
return members.stream().map(Member::getAddress).sorted()
.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
}

/**
* Judge whether two member is full equals.
*
* @param actual actual member
* @param expected expected member
* @return true if all content is same, otherwise false
*/
public static boolean fullEquals(Member actual, Member expected) {
if (null == expected) {
return null == actual;
}
if (!expected.getIp().equals(actual.getIp())) {
return false;
}
if (expected.getPort() != actual.getPort()) {
return false;
}
if (!expected.getAddress().equals(actual.getAddress())) {
return false;
}
if (!expected.getState().equals(actual.getState())) {
return false;
}
return equalsExtendInfo(expected, actual);
}

private static boolean equalsExtendInfo(Member expected, Member actual) {
for (String each : MemberMetaDataConstants.META_KEY_LIST_WITHOUT_LAST_REFRESH_TIME) {
if (expected.getExtendInfo().containsKey(each) != actual.getExtendInfo().containsKey(each)) {
return false;
}
if (null != expected.getExtendVal(each) && !expected.getExtendVal(each).equals(actual.getExtendVal(each))) {
return false;
}
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,6 @@ public boolean update(Member newMember) {
Loggers.CLUSTER.debug("member information update : {}", newMember);

String address = newMember.getAddress();
newMember.setExtendVal(MemberMetaDataConstants.LAST_REFRESH_TIME, System.currentTimeMillis());

if (!serverList.containsKey(address)) {
return false;
}
Expand All @@ -211,15 +209,15 @@ public boolean update(Member newMember) {
if (NodeState.DOWN.equals(newMember.getState())) {
memberAddressInfos.remove(newMember.getAddress());
}
MemberUtils.copy(newMember, member);
if (!MemberUtils.fullEquals(newMember, member)) {
newMember.setExtendVal(MemberMetaDataConstants.LAST_REFRESH_TIME, System.currentTimeMillis());
MemberUtils.copy(newMember, member);
// member data changes and all listeners need to be notified
NotifyCenter.publishEvent(MembersChangeEvent.builder().members(allMembers()).build());
}
return member;
});

// member data changes and all listeners need to be notified
NotifyCenter.publishEvent(MembersChangeEvent.builder()
.members(allMembers())
.build());

return true;
}

Expand Down Expand Up @@ -311,19 +309,13 @@ synchronized boolean memberChange(Collection<Member> members) {
tmpAddressInfo.add(address);
}

Map<String, Member> oldList = serverList;
Set<String> oldSet = memberAddressInfos;

serverList = tmpMap;
memberAddressInfos = tmpAddressInfo;

Collection<Member> finalMembers = allMembers();

Loggers.CLUSTER.warn("[serverlist] updated to : {}", finalMembers);

oldList.clear();
oldSet.clear();

// Persist the current cluster node information to cluster.conf
// <important> need to put the event publication into a synchronized block to ensure
// that the event publication is sequential
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public Object listDetail(@RequestParam(required = false) boolean withInstances,
ObjectNode result = JacksonUtils.createEmptyJsonNode();

List<Service> services = new ArrayList<>();

final int total = serviceManager.getPagedService(namespaceId, pageNo - 1, pageSize, param, containedInstance, services, hasIpCount);
if (CollectionUtils.isEmpty(services)) {
result.replace("serviceList", JacksonUtils.transferToJsonNode(Collections.emptyList()));
result.put("count", 0);
Expand All @@ -232,8 +232,6 @@ public Object listDetail(@RequestParam(required = false) boolean withInstances,
serviceViews.add(serviceView);
}

int total = serviceManager
.getPagedService(namespaceId, pageNo - 1, pageSize, param, containedInstance, services, hasIpCount);
result.replace("serviceList", JacksonUtils.transferToJsonNode(serviceViews));
result.put("count", total);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ public void onEvent(MembersChangeEvent event) {
Collection<String> old = healthyList;
healthyList = Collections.unmodifiableList(list);
Loggers.SRV_LOG.info("[NACOS-DISTRO] healthy server list changed, old: {}, new: {}", old, healthyList);
old.clear();
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@
<tomcat-embed-jasper.version>7.0.59</tomcat-embed-jasper.version>
<truth.version>0.30</truth.version>
<HikariCP.version>3.4.2</HikariCP.version>
<jraft-core.version>1.3.2.beta1</jraft-core.version>
<rpc-grpc-impl.version>1.3.2.beta1</rpc-grpc-impl.version>
<jraft-core.version>1.3.2</jraft-core.version>
<rpc-grpc-impl.version>1.3.2</rpc-grpc-impl.version>
</properties>
<!-- == -->
<!-- =========================================================Build plugins================================================ -->
Expand Down

0 comments on commit 49c163b

Please sign in to comment.