Skip to content

Commit

Permalink
[ISSUE#3179]Replace the NotifyCenter with new refactor in the core an…
Browse files Browse the repository at this point in the history
…d test module. (alibaba#3309)

* [ISSUE#3179]Replace the NotifyCenter with new refactor in the core and test module.

* [ISSUE#3179]Replace package in the nacos-logback.xml.

* [ISSUE#3179]code reformat.
  • Loading branch information
zongtanghu authored Jul 13, 2020
1 parent c1515b6 commit 665d9d7
Show file tree
Hide file tree
Showing 18 changed files with 51 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.alibaba.nacos.common.notify;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.exception.runtime.NacosRuntimeException;
import com.alibaba.nacos.common.JustForTest;
import com.alibaba.nacos.common.notify.listener.Subscriber;
Expand Down Expand Up @@ -310,8 +309,7 @@ public static EventPublisher registerToSharePublisher(final Class<? extends Slow
* @param eventType class Instances type of the event type.
* @param queueMaxSize the publisher's queue max size.
*/
public static EventPublisher registerToPublisher(final Class<? extends Event> eventType, final int queueMaxSize)
throws NacosException {
public static EventPublisher registerToPublisher(final Class<? extends Event> eventType, final int queueMaxSize) {
if (ClassUtils.isAssignableFrom(SlowEvent.class, eventType)) {
return INSTANCE.sharePublisher;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,24 @@

package com.alibaba.nacos.core.cluster;

import com.alibaba.nacos.core.notify.Event;
import com.alibaba.nacos.core.notify.listener.Subscribe;
import com.alibaba.nacos.common.notify.Event;
import com.alibaba.nacos.common.notify.listener.Subscriber;

/**
* Node change listeners.
*
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
*/
public interface MemberChangeListener extends Subscribe<MembersChangeEvent> {
@SuppressWarnings("PMD.AbstractClassShouldStartWithAbstractNamingRule")
public abstract class MemberChangeListener extends Subscriber<MembersChangeEvent> {

/**
* return NodeChangeEvent.class info.
*
* @return {@link MembersChangeEvent#getClass()}
*/
@Override
default Class<? extends Event> subscribeType() {
public Class<? extends Event> subscribeType() {
return MembersChangeEvent.class;
}

Expand All @@ -42,7 +43,7 @@ default Class<? extends Event> subscribeType() {
* @return default value is {@link Boolean#TRUE}
*/
@Override
default boolean ignoreExpireEvent() {
public boolean ignoreExpireEvent() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@

package com.alibaba.nacos.core.cluster;

import com.alibaba.nacos.core.notify.Event;
import com.alibaba.nacos.common.notify.Event;

import java.util.Collection;
import java.util.concurrent.atomic.AtomicLong;

/**
* Publish this event when the node list changes,All interested in the node list change event can listen to this event.
Expand All @@ -32,16 +31,12 @@
*
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
*/
public class MembersChangeEvent implements Event {

private static final AtomicLong SEQUENCE = new AtomicLong(0);
public class MembersChangeEvent extends Event {

private static final long serialVersionUID = 7308126651076668976L;

private Collection<Member> members;

private long no = SEQUENCE.getAndIncrement();

public static MemberChangeEventBuilder builder() {
return new MemberChangeEventBuilder();
}
Expand All @@ -54,14 +49,9 @@ public void setMembers(Collection<Member> members) {
this.members = members;
}

@Override
public long sequence() {
return no;
}

@Override
public String toString() {
return "MembersChangeEvent{" + "members=" + members + ", no=" + no + '}';
return "MembersChangeEvent{" + "members=" + members + ", no=" + sequence() + '}';
}

public static final class MemberChangeEventBuilder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
import com.alibaba.nacos.common.http.param.Header;
import com.alibaba.nacos.common.http.param.Query;
import com.alibaba.nacos.common.model.RestResult;
import com.alibaba.nacos.common.notify.Event;
import com.alibaba.nacos.common.notify.NotifyCenter;
import com.alibaba.nacos.common.notify.listener.Subscriber;
import com.alibaba.nacos.common.utils.ConcurrentHashSet;
import com.alibaba.nacos.common.utils.ExceptionUtil;
import com.alibaba.nacos.common.utils.VersionUtils;
import com.alibaba.nacos.core.cluster.lookup.LookupFactory;
import com.alibaba.nacos.core.notify.Event;
import com.alibaba.nacos.core.notify.NotifyCenter;
import com.alibaba.nacos.core.notify.listener.Subscribe;
import com.alibaba.nacos.core.utils.ApplicationUtils;
import com.alibaba.nacos.core.utils.Commons;
import com.alibaba.nacos.core.utils.Constants;
Expand Down Expand Up @@ -166,7 +166,7 @@ private void registerClusterEvent() {

// The address information of this node needs to be dynamically modified
// when registering the IP change of this node
NotifyCenter.registerSubscribe(new Subscribe<InetUtils.IPChangeEvent>() {
NotifyCenter.registerSubscriber(new Subscriber<InetUtils.IPChangeEvent>() {
@Override
public void onEvent(InetUtils.IPChangeEvent event) {
String newAddress = event.getNewIp() + ":" + port;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import com.alibaba.nacos.common.executor.NameThreadFactory;
import com.alibaba.nacos.common.executor.ThreadPoolManager;
import com.alibaba.nacos.common.http.HttpClientManager;
import com.alibaba.nacos.common.notify.NotifyCenter;
import com.alibaba.nacos.core.file.WatchFileCenter;
import com.alibaba.nacos.core.notify.NotifyCenter;
import com.alibaba.nacos.core.utils.ApplicationUtils;
import com.alibaba.nacos.core.utils.DiskUtils;
import com.alibaba.nacos.core.utils.InetUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.alibaba.nacos.core.distributed;

import com.alibaba.nacos.common.notify.NotifyCenter;
import com.alibaba.nacos.consistency.Config;
import com.alibaba.nacos.consistency.ap.APProtocol;
import com.alibaba.nacos.consistency.cp.CPProtocol;
Expand All @@ -25,7 +26,6 @@
import com.alibaba.nacos.core.cluster.MemberMetaDataConstants;
import com.alibaba.nacos.core.cluster.MemberUtils;
import com.alibaba.nacos.core.cluster.ServerMemberManager;
import com.alibaba.nacos.core.notify.NotifyCenter;
import com.alibaba.nacos.core.utils.ApplicationUtils;
import com.alibaba.nacos.core.utils.ClassUtils;
import org.springframework.beans.factory.DisposableBean;
Expand All @@ -49,7 +49,7 @@
@SuppressWarnings("all")
@Component(value = "ProtocolManager")
@DependsOn("serverMemberManager")
public class ProtocolManager implements ApplicationListener<ContextStartedEvent>, DisposableBean, MemberChangeListener {
public class ProtocolManager extends MemberChangeListener implements ApplicationListener<ContextStartedEvent>, DisposableBean {

private CPProtocol cpProtocol;

Expand Down Expand Up @@ -85,7 +85,7 @@ private static Set<String> toCPMembersInfo(Collection<Member> members) {
@PostConstruct
public void init() {
this.memberManager = memberManager;
NotifyCenter.registerSubscribe(this);
NotifyCenter.registerSubscriber(this);
}

public CPProtocol getCpProtocol() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
package com.alibaba.nacos.core.distributed.raft;

import com.alibaba.nacos.common.model.RestResult;
import com.alibaba.nacos.common.notify.NotifyCenter;
import com.alibaba.nacos.common.notify.listener.Subscriber;
import com.alibaba.nacos.common.notify.Event;
import com.alibaba.nacos.common.utils.MapUtils;
import com.alibaba.nacos.common.utils.ThreadUtils;
import com.alibaba.nacos.consistency.ProtocolMetaData;
Expand All @@ -32,9 +35,6 @@
import com.alibaba.nacos.core.cluster.ServerMemberManager;
import com.alibaba.nacos.core.distributed.AbstractConsistencyProtocol;
import com.alibaba.nacos.core.distributed.raft.exception.NoSuchRaftGroupException;
import com.alibaba.nacos.core.notify.Event;
import com.alibaba.nacos.core.notify.NotifyCenter;
import com.alibaba.nacos.core.notify.listener.Subscribe;
import com.alibaba.nacos.core.utils.Loggers;
import com.alipay.sofa.jraft.Node;

Expand Down Expand Up @@ -123,7 +123,7 @@ public void init(RaftConfig config) {

// There is only one consumer to ensure that the internal consumption
// is sequential and there is no concurrent competition
NotifyCenter.registerSubscribe(new Subscribe<RaftEvent>() {
NotifyCenter.registerSubscriber(new Subscriber<RaftEvent>() {
@Override
public void onEvent(RaftEvent event) {
Loggers.RAFT.info("This Raft event changes : {}", event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.alibaba.nacos.core.distributed.raft;

import com.alibaba.nacos.common.notify.NotifyCenter;
import com.alibaba.nacos.common.utils.ExceptionUtil;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.common.utils.LoggerUtils;
Expand All @@ -30,7 +31,6 @@
import com.alibaba.nacos.consistency.snapshot.SnapshotOperation;
import com.alibaba.nacos.consistency.snapshot.Writer;
import com.alibaba.nacos.core.distributed.raft.utils.JRaftUtils;
import com.alibaba.nacos.core.notify.NotifyCenter;
import com.alibaba.nacos.core.utils.Loggers;
import com.alipay.sofa.jraft.Closure;
import com.alipay.sofa.jraft.Iterator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@

package com.alibaba.nacos.core.distributed.raft;

import com.alibaba.nacos.core.notify.Event;
import com.alibaba.nacos.common.notify.Event;

/**
* The RAFT protocol runs an exception event. If this event is published, it means that the current raft Group cannot
* continue to run normally
*
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
*/
public class RaftErrorEvent implements Event {
public class RaftErrorEvent extends Event {

private static final long serialVersionUID = 3016514657754158167L;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.alibaba.nacos.core.distributed.raft;

import com.alibaba.nacos.core.notify.SlowEvent;
import com.alibaba.nacos.common.notify.SlowEvent;

import java.util.Collections;
import java.util.List;
Expand All @@ -27,7 +27,7 @@
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
*/
@SuppressWarnings("all")
public class RaftEvent implements SlowEvent {
public class RaftEvent extends SlowEvent {

private static final long serialVersionUID = -4304258594602886451L;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package com.alibaba.nacos.core.utils;

import com.alibaba.nacos.core.notify.NotifyCenter;
import com.alibaba.nacos.core.notify.SlowEvent;
import com.alibaba.nacos.common.notify.NotifyCenter;
import com.alibaba.nacos.common.notify.SlowEvent;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -229,7 +229,7 @@ public static boolean isIP(String str) {
* {@link com.alibaba.nacos.core.cluster.ServerMemberManager} is listener.
*/
@SuppressWarnings({"PMD.ClassNamingShouldBeCamelRule", "checkstyle:AbbreviationAsWordInName"})
public static class IPChangeEvent implements SlowEvent {
public static class IPChangeEvent extends SlowEvent {

private String oldIp;

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/resources/META-INF/logback/nacos.xml
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@
<level value="INFO"/>
</logger>

<logger name="com.alibaba.nacos.core.notify.NotifyCenter">
<logger name="com.alibaba.nacos.common.notify.NotifyCenter">
<appender-ref ref="CONSOLE"/>
<level value="INFO"/>
</logger>
Expand Down
2 changes: 1 addition & 1 deletion distribution/conf/nacos-logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@
<level value="INFO"/>
</logger>

<logger name="com.alibaba.nacos.core.notify.NotifyCenter">
<logger name="com.alibaba.nacos.common.notify.NotifyCenter">
<appender-ref ref="CONSOLE"/>
<level value="INFO"/>
</logger>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

package com.alibaba.nacos.naming.cluster;

import com.alibaba.nacos.common.notify.NotifyCenter;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.core.cluster.Member;
import com.alibaba.nacos.core.cluster.MembersChangeEvent;
import com.alibaba.nacos.core.cluster.MemberChangeListener;
import com.alibaba.nacos.core.cluster.MemberMetaDataConstants;
import com.alibaba.nacos.core.cluster.NodeState;
import com.alibaba.nacos.core.cluster.ServerMemberManager;
import com.alibaba.nacos.core.notify.NotifyCenter;
import com.alibaba.nacos.core.utils.ApplicationUtils;
import com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeer;
import com.alibaba.nacos.naming.misc.GlobalExecutor;
Expand Down Expand Up @@ -54,7 +54,7 @@
* @deprecated 1.3.0 This object will be deleted sometime after version 1.3.0
*/
@Component("serverListManager")
public class ServerListManager implements MemberChangeListener {
public class ServerListManager extends MemberChangeListener {

private static final String LOCALHOST_SITE = UtilsAndCommons.UNKNOWN_SITE;

Expand All @@ -69,7 +69,7 @@ public class ServerListManager implements MemberChangeListener {
public ServerListManager(final SwitchDomain switchDomain, final ServerMemberManager memberManager) {
this.switchDomain = switchDomain;
this.memberManager = memberManager;
NotifyCenter.registerSubscribe(this);
NotifyCenter.registerSubscriber(this);
this.servers = new ArrayList<>(memberManager.allMembers());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

package com.alibaba.nacos.naming.consistency.persistent.raft;

import com.alibaba.nacos.common.notify.NotifyCenter;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.core.cluster.Member;
import com.alibaba.nacos.core.cluster.MemberChangeListener;
import com.alibaba.nacos.core.cluster.MembersChangeEvent;
import com.alibaba.nacos.core.cluster.ServerMemberManager;
import com.alibaba.nacos.core.notify.NotifyCenter;
import com.alibaba.nacos.core.utils.ApplicationUtils;
import com.alibaba.nacos.naming.misc.HttpClient;
import com.alibaba.nacos.naming.misc.Loggers;
Expand Down Expand Up @@ -53,7 +53,7 @@
*/
@Component
@DependsOn("ProtocolManager")
public class RaftPeerSet implements MemberChangeListener {
public class RaftPeerSet extends MemberChangeListener {

private final ServerMemberManager memberManager;

Expand All @@ -75,7 +75,7 @@ public RaftPeerSet(ServerMemberManager memberManager) {

@PostConstruct
public void init() {
NotifyCenter.registerSubscribe(this);
NotifyCenter.registerSubscriber(this);
changePeers(memberManager.allMembers());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

package com.alibaba.nacos.naming.core;

import com.alibaba.nacos.common.notify.NotifyCenter;
import com.alibaba.nacos.core.cluster.MemberChangeListener;
import com.alibaba.nacos.core.cluster.MemberUtils;
import com.alibaba.nacos.core.cluster.MembersChangeEvent;
import com.alibaba.nacos.core.cluster.NodeState;
import com.alibaba.nacos.core.cluster.ServerMemberManager;
import com.alibaba.nacos.core.notify.NotifyCenter;
import com.alibaba.nacos.core.utils.ApplicationUtils;
import com.alibaba.nacos.naming.misc.Loggers;
import com.alibaba.nacos.naming.misc.SwitchDomain;
Expand All @@ -40,7 +40,7 @@
* @author nkorange
*/
@Component("distroMapper")
public class DistroMapper implements MemberChangeListener {
public class DistroMapper extends MemberChangeListener {

/**
* List of service nodes, you must ensure that the order of healthyList is the same for all nodes.
Expand All @@ -65,7 +65,7 @@ public List<String> getHealthyList() {
*/
@PostConstruct
public void init() {
NotifyCenter.registerSubscribe(this);
NotifyCenter.registerSubscriber(this);
this.healthyList = MemberUtils.simpleMembers(memberManager.allMembers());
}

Expand Down
Loading

0 comments on commit 665d9d7

Please sign in to comment.