diff --git a/core-swift/src/main/java/com/ware/swift/core/AliwareRaftManager.java b/core-swift/src/main/java/com/ware/swift/core/AliwareRaftManager.java deleted file mode 100644 index 87c6907..0000000 --- a/core-swift/src/main/java/com/ware/swift/core/AliwareRaftManager.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.ware.swift.core; - -import com.ware.swift.core.inter.ICoreSwiftManager; -import com.ware.swift.core.remoting.RemotingManager; -import io.netty.util.internal.logging.InternalLogger; -import io.netty.util.internal.logging.InternalLoggerFactory; - -import static com.ware.swift.core.WareCoreSwiftGlobalContext.RAFT_CONTEXT_ATTR_KEY_CONFIG; -import static com.ware.swift.core.WareCoreSwiftGlobalContext.RAFT_CONTEXT_ATTR_KEY_CONFIG_PATH; - -/** - * - */ -public class AliwareRaftManager implements ICoreSwiftManager { - - private InternalLogger logger = InternalLoggerFactory - .getInstance(AliwareRaftManager.class); - - public void init(WareCoreSwiftGlobalContext raftGlobalContext) { - startBefore(raftGlobalContext); - start(raftGlobalContext); - startAfter(raftGlobalContext); - } - - /** - * - * @param raftGlobaleContext - */ - public void startBefore(WareCoreSwiftGlobalContext raftGlobaleContext) { - String configPath = raftGlobaleContext.getAttribute( - RAFT_CONTEXT_ATTR_KEY_CONFIG_PATH, "aliware-raft.properties"); - try { - WareCoreSwiftConfig raftConfig = WareCoreSwiftConfigManager.initRaftConfig(configPath, - this.getClass().getClassLoader()); - RemotingManager.getRemotingManager().init(raftConfig); - raftGlobaleContext.putAttribute(RAFT_CONTEXT_ATTR_KEY_CONFIG, raftConfig); - } - catch (Exception e) { - logger.error(WareCoreSwiftExceptionCode.getConfigInitializerErrorMessage( - " init raft cause an exception with " + configPath), e); - } - } - - /** - * - * @param raftGlobaleContext - */ - public void start(WareCoreSwiftGlobalContext raftGlobaleContext) { - - RemotingManager.getRemotingManager().startLeaderElection(raftGlobaleContext); - } - - /** - * - * @param raftGlobaleContext - */ - public void startAfter(WareCoreSwiftGlobalContext raftGlobaleContext) { - - } - - /** - * - * @param nodeInformation - */ - public void shutdown(NodeInformation nodeInformation) { - - } - - /** - * - */ - public void getAllNodeInformations() { - - } - - /** - * - * @param clusterName - */ - public void getClusterNodeInformations(String clusterName) { - - } -} diff --git a/core-swift/src/main/java/com/ware/swift/core/CoreSwiftManager.java b/core-swift/src/main/java/com/ware/swift/core/CoreSwiftManager.java deleted file mode 100644 index 0e0f398..0000000 --- a/core-swift/src/main/java/com/ware/swift/core/CoreSwiftManager.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.ware.swift.core; - -import com.ware.swift.core.inter.ICoreSwiftManager; - -import java.util.Set; - -/** - * - */ -public final class CoreSwiftManager implements ICoreSwiftManager { - - private static ICoreSwiftManager iRaftManager; - - static { - try { - Set raftManagers = WareCoreSwiftPluginLoader.load(ICoreSwiftManager.class, - AliwareRaftManager.class.getClassLoader()); - - boolean isLoad = false; - if (raftManagers != null && raftManagers.size() > 0) { - iRaftManager = raftManagers.iterator().next(); - isLoad = true; - } - - if (!isLoad) { - iRaftManager = new AliwareRaftManager(); - } - } - catch (Exception e) { - e.printStackTrace(); - } - } - - private final static CoreSwiftManager ALIWARE_RAFT_CORE = new CoreSwiftManager(); - - private CoreSwiftManager() { - } - - public static CoreSwiftManager getInstance() { - return ALIWARE_RAFT_CORE; - } - - public void init(WareCoreSwiftGlobalContext raftGlobalContext) { - - iRaftManager.init(raftGlobalContext); - } - - public void startBefore(WareCoreSwiftGlobalContext raftGlobalContext) { - iRaftManager.startBefore(raftGlobalContext); - } - - public void start(WareCoreSwiftGlobalContext raftGlobalContext) { - iRaftManager.start(raftGlobalContext); - } - - public void startAfter(WareCoreSwiftGlobalContext raftGlobalContext) { - iRaftManager.startAfter(raftGlobalContext); - } - - public void shutdown(NodeInformation nodeInformation) { - iRaftManager.shutdown(nodeInformation); - } - - public void getAllNodeInformations() { - iRaftManager.getAllNodeInformations(); - } - - public void getClusterNodeInformations(String clusterName) { - iRaftManager.getClusterNodeInformations(clusterName); - } -} diff --git a/core-swift/src/main/java/com/ware/swift/core/CoreSwiftManagerDelegate.java b/core-swift/src/main/java/com/ware/swift/core/CoreSwiftManagerDelegate.java new file mode 100644 index 0000000..d0632a6 --- /dev/null +++ b/core-swift/src/main/java/com/ware/swift/core/CoreSwiftManagerDelegate.java @@ -0,0 +1,70 @@ +package com.ware.swift.core; + +import com.ware.swift.core.inter.ICoreSwiftManager; + +import java.util.Set; + +/** + * delegate the core swift manager + */ +public final class CoreSwiftManagerDelegate implements ICoreSwiftManager { + + private static ICoreSwiftManager coreSwiftManager; + + static { + try { + Set wareSwiftManagers = WareCoreSwiftPluginLoader.load(ICoreSwiftManager.class, + CoreSwiftManagerImpl.class.getClassLoader()); + + boolean isLoad = false; + if (wareSwiftManagers != null && wareSwiftManagers.size() > 0) { + coreSwiftManager = wareSwiftManagers.iterator().next(); + isLoad = true; + } + + if (!isLoad) { + coreSwiftManager = new CoreSwiftManagerImpl(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + private final static CoreSwiftManagerDelegate CORE_SWIFT_MANAGER = new CoreSwiftManagerDelegate(); + + private CoreSwiftManagerDelegate() { + } + + public static CoreSwiftManagerDelegate getInstance() { + return CORE_SWIFT_MANAGER; + } + + public void init(WareCoreSwiftGlobalContext wareSwiftGlobalContext) { + + coreSwiftManager.init(wareSwiftGlobalContext); + } + + public void startBefore(WareCoreSwiftGlobalContext wareSwiftGlobalContext) { + coreSwiftManager.startBefore(wareSwiftGlobalContext); + } + + public void start(WareCoreSwiftGlobalContext wareSwiftGlobalContext) { + coreSwiftManager.start(wareSwiftGlobalContext); + } + + public void startAfter(WareCoreSwiftGlobalContext wareSwiftGlobalContext) { + coreSwiftManager.startAfter(wareSwiftGlobalContext); + } + + public void shutdown(NodeInformation nodeInformation) { + coreSwiftManager.shutdown(nodeInformation); + } + + public void getAllNodeInformation() { + coreSwiftManager.getAllNodeInformation(); + } + + public void getClusterNodeInformation(String clusterName) { + coreSwiftManager.getClusterNodeInformation(clusterName); + } +} diff --git a/core-swift/src/main/java/com/ware/swift/core/CoreSwiftManagerImpl.java b/core-swift/src/main/java/com/ware/swift/core/CoreSwiftManagerImpl.java new file mode 100644 index 0000000..889b0c0 --- /dev/null +++ b/core-swift/src/main/java/com/ware/swift/core/CoreSwiftManagerImpl.java @@ -0,0 +1,79 @@ +package com.ware.swift.core; + +import com.ware.swift.core.inter.ICoreSwiftManager; +import com.ware.swift.core.remoting.RemotingManager; +import io.netty.util.internal.logging.InternalLogger; +import io.netty.util.internal.logging.InternalLoggerFactory; +import io.netty.util.internal.logging.JdkLoggerFactory; + +import static com.ware.swift.core.WareCoreSwiftGlobalContext.RAFT_CONTEXT_ATTR_KEY_CONFIG; +import static com.ware.swift.core.WareCoreSwiftGlobalContext.RAFT_CONTEXT_ATTR_KEY_CONFIG_PATH; + +/** + * + */ +public class CoreSwiftManagerImpl implements ICoreSwiftManager { + + private InternalLogger logger = InternalLoggerFactory + .getInstance(CoreSwiftManagerImpl.class); + + public void init(WareCoreSwiftGlobalContext wareSwiftGlobalContext) { + startBefore(wareSwiftGlobalContext); + start(wareSwiftGlobalContext); + startAfter(wareSwiftGlobalContext); + } + + /** + * @param wareSwiftGlobalContext + */ + public void startBefore(WareCoreSwiftGlobalContext wareSwiftGlobalContext) { + InternalLoggerFactory.setDefaultFactory(JdkLoggerFactory.INSTANCE); + String configPath = wareSwiftGlobalContext.getAttribute( + RAFT_CONTEXT_ATTR_KEY_CONFIG_PATH, "ware-swift.properties"); + try { + WareCoreSwiftConfig wareSwiftConfig = WareCoreSwiftConfigManager.initRaftConfig(configPath, + this.getClass().getClassLoader()); + RemotingManager.getRemotingManager().init(wareSwiftConfig); + wareSwiftGlobalContext.putAttribute(RAFT_CONTEXT_ATTR_KEY_CONFIG, wareSwiftConfig); + } catch (Exception e) { + logger.error(WareCoreSwiftExceptionCode.getConfigInitializerErrorMessage( + " init wareSwift cause an exception with " + configPath), e); + } + } + + /** + * @param wareSwiftGlobalContext + */ + public void start(WareCoreSwiftGlobalContext wareSwiftGlobalContext) { + + RemotingManager.getRemotingManager().startLeaderElection(wareSwiftGlobalContext); + } + + /** + * @param wareSwiftGlobalContext + */ + public void startAfter(WareCoreSwiftGlobalContext wareSwiftGlobalContext) { + + } + + /** + * @param nodeInformation + */ + public void shutdown(NodeInformation nodeInformation) { + + } + + /** + * + */ + public void getAllNodeInformation() { + + } + + /** + * @param clusterName + */ + public void getClusterNodeInformation(String clusterName) { + + } +} diff --git a/core-swift/src/main/java/com/ware/swift/core/IPluginAble.java b/core-swift/src/main/java/com/ware/swift/core/IPluginAble.java new file mode 100644 index 0000000..e79fe5d --- /dev/null +++ b/core-swift/src/main/java/com/ware/swift/core/IPluginAble.java @@ -0,0 +1,12 @@ +package com.ware.swift.core; + +/** + * + */ +public interface IPluginAble { + + /** + * @return + */ + String getPluginName(); +} diff --git a/core-swift/src/main/java/com/ware/swift/core/IPluginable.java b/core-swift/src/main/java/com/ware/swift/core/IPluginable.java deleted file mode 100644 index f59a05a..0000000 --- a/core-swift/src/main/java/com/ware/swift/core/IPluginable.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.ware.swift.core; - -/** - * - */ -public interface IPluginable { - - String getPluginName(); -} diff --git a/core-swift/src/main/java/com/ware/swift/core/NodeInformation.java b/core-swift/src/main/java/com/ware/swift/core/NodeInformation.java index e39e5b5..4c6a464 100644 --- a/core-swift/src/main/java/com/ware/swift/core/NodeInformation.java +++ b/core-swift/src/main/java/com/ware/swift/core/NodeInformation.java @@ -13,218 +13,215 @@ */ public class NodeInformation implements Information { - public static final String BIND_ADDRESS = "bind.address"; - public static final String BIND_PORT = "bind.port"; - public static final String REQUEST_REQUIRED_ACKS = "request.required.acks"; - public static final String MAX_SYNCING_WATER_MARKER = "max.syncing.water.marker"; - public static final String MAX_COMMITTED_IDLE_TIME = "max.committed.idle.time"; - public static final String EMPTY_VOTE_FOR = "emptyVoteFor"; - public static final String SEND_HEARTBEAT_INTERVAL = "send.heartbeat.interval"; - - private volatile long voteCount = 0; - private volatile long term = ClusterDataSyncManager.DEFAULT_SYNCING_TERM; - private volatile long voteForTime; - private volatile long hasCommittedCount = 0; - private AtomicReference atomicVotedFor = new AtomicReference<>( - EMPTY_VOTE_FOR); - /** - * node start in the follower state. - */ - private volatile NodeState nodeState = NodeState.Follower; - - /** - * Which cluster does a node belong to? - */ - private String clusterName = "RAFT_DEFAULT_CLUSTER"; - private String bindAddress; - private String bindPort; - - /** - * subjectively down 的投票数。当达到剩下的 Follower 节点半数以上同意,则认为确实是下线了。 - */ - private AtomicLong sdownVoteCount = new AtomicLong(); - - private transient Properties config; - - public void setConfig(Properties config) { - this.config = config; - this.bindAddress = config.getProperty("bind.address", "0.0.0.0"); - this.bindPort = config.getProperty("bind.port", "19090"); - this.clusterName = config.getProperty("cluster.name", clusterName); - } - - public NodeState getNodeState() { - return nodeState; - } - - public void setNodeState(NodeState nodeState) { - this.nodeState = nodeState; - } - - public String getClusterName() { - return clusterName; - } - - public InetSocketAddress getInetSocketAddress() { - String ip = config.getProperty(BIND_ADDRESS, "0.0.0.0"); - int port = Integer.valueOf(config.getProperty(BIND_PORT, "19090")); - return new InetSocketAddress(ip, port); - } - - public String getAddressPort() { - if (this.bindAddress == null && this.bindPort == null) { - return null; - } - - return this.bindAddress + ":" + this.bindPort; - } - - public String getBindAddress() { - return this.bindAddress; - } - - public String getBindPort() { - return this.bindPort; - } - - public long getVoteCount() { - return voteCount; - } - - public synchronized void increVoteCount() { - this.voteCount++; - this.voteForTime += System.currentTimeMillis(); - } - - public long getTerm() { - return term; - } - - public synchronized void increTerm() { - this.term++; - } - - public String getVotedFor() { - return atomicVotedFor.get(); - } - - public boolean isLeaderMeet() { - return Boolean.valueOf(config.getProperty("leader.meet", "false")); - } - - /** - * 投票的时候,需要保证并发性。因此这里需要处理如果发现已经投过票了,则返回 false. - * - * @param votedFor - * @return - */ - public boolean setVotedFor(String votedFor) { - return atomicVotedFor.compareAndSet(EMPTY_VOTE_FOR, votedFor); - } - - public int getRequestRequiredAcks(int defaultValue) { - - String acks = config.getProperty(REQUEST_REQUIRED_ACKS); - - return StringUtil.isNullOrEmpty(acks) ? defaultValue : Integer.valueOf(acks); - } - - /** - * @param defaultValue - * @return - */ - public long getSyncingMaxWaterMarker(long defaultValue) { - - String maxSyncingWaterMarker = config.getProperty(MAX_SYNCING_WATER_MARKER); - return StringUtil.isNullOrEmpty(maxSyncingWaterMarker) ? defaultValue - : Long.valueOf(maxSyncingWaterMarker); - } - - /** - * 即用来表示 - * @param defaultValue - * @return - */ - public long getCommittedMaxIdleTime(long defaultValue) { - - String maxCommittedIdleTimes = config.getProperty(MAX_COMMITTED_IDLE_TIME); - - return StringUtil.isNullOrEmpty(maxCommittedIdleTimes) ? defaultValue - : Long.valueOf(maxCommittedIdleTimes); - } - - /** - * 在感知到 leader 之后,做一些基本信息的 update 操作。 - *

- * 注意: 需要更新本 节点的 vote for 信息 set is null。因此这个过程中有可能本节点已经投自己一票了,但是最终没有选举成功。 - *

- * 为下一次选举投票做好准备。 - * - * @param leader - */ - public synchronized void updateAfterAwareLeader(NodeInformation leader) { - this.atomicVotedFor.set(EMPTY_VOTE_FOR); - this.term = leader.getTerm(); - this.hasCommittedCount = leader.getCommittedCount(); - if (leader.identify().equalsIgnoreCase(this.identify())) { - this.setNodeState(NodeState.Leader); - } - else { - this.setNodeState(NodeState.Follower); - } - } - - /** - * 清除之前的一些必要的投票状态信息 - */ - public synchronized void clearVoteStatus() { - this.atomicVotedFor.set(EMPTY_VOTE_FOR); - this.voteForTime = 0; - this.voteCount = 0; - this.nodeState = NodeState.Follower; - } - - public AtomicLong getSdownVoteCount() { - return sdownVoteCount; - } - - public void setSdownVoteCount(AtomicLong sdownVoteCount) { - this.sdownVoteCount = sdownVoteCount; - } - - /** - * 节点在哪个集群。${address:port}@${cluster_name} - * - * @return - */ - @Override - public String identify() { - return getAddressPort() + "@" + getClusterName(); - } - - public long incrementCommittedCount() { - final long tmpHasCommittedCount = hasCommittedCount++; - // System.err.println("\t\t\t committed =" + tmpHasCommittedCount); - return tmpHasCommittedCount; - } - - public long getCommittedCount() { - - return hasCommittedCount; - } - - public long getHeartbeatInterval(long defaultValue) { - - return Long.valueOf(config.getProperty(SEND_HEARTBEAT_INTERVAL, - String.valueOf(defaultValue))); - } - - @Override - public String toString() { - return "NodeInformation{" + "votedFor='" + atomicVotedFor.get() + '\'' - + ", voteCount=" + voteCount + ", term=" + term + ", voteForTime=" - + voteForTime + ", nodeState=" + nodeState + ", clusterName='" - + clusterName + '\'' + ", bindAddress='" + bindAddress + '\'' - + ", bindPort='" + bindPort + '\'' + ", config=" + config + '}'; - } + public static final String BIND_ADDRESS = "bind.address"; + public static final String BIND_PORT = "bind.port"; + public static final String REQUEST_REQUIRED_ACKS = "request.required.acks"; + public static final String MAX_SYNCING_WATER_MARKER = "max.syncing.water.marker"; + public static final String MAX_COMMITTED_IDLE_TIME = "max.committed.idle.time"; + public static final String EMPTY_VOTE_FOR = "emptyVoteFor"; + public static final String SEND_HEARTBEAT_INTERVAL = "send.heartbeat.interval"; + public static final String WARE_SWIFT_DEBUG = "ware.swift.debug"; + + private volatile long voteCount = 0; + private volatile long term = ClusterDataSyncManager.DEFAULT_SYNCING_TERM; + private volatile long voteForTime; + private volatile long hasCommittedCount = 0; + private AtomicReference atomicVotedFor = new AtomicReference<>( + EMPTY_VOTE_FOR); + /** + * node start in the follower state. + */ + private volatile NodeState nodeState = NodeState.Follower; + + /** + * Which cluster does a node belong to? + */ + private String clusterName = "RAFT_DEFAULT_CLUSTER"; + private String bindAddress; + private String bindPort; + + /** + * subjectively down 的投票数。当达到剩下的 Follower 节点半数以上同意,则认为确实是下线了。 + */ + private AtomicLong sdownVoteCount = new AtomicLong(); + + private transient Properties config; + + public void setConfig(Properties config) { + this.config = config; + this.bindAddress = config.getProperty("bind.address", "0.0.0.0"); + this.bindPort = config.getProperty("bind.port", "19090"); + this.clusterName = config.getProperty("cluster.name", clusterName); + } + + public NodeState getNodeState() { + return nodeState; + } + + public void setNodeState(NodeState nodeState) { + this.nodeState = nodeState; + } + + public String getClusterName() { + return clusterName; + } + + public InetSocketAddress getInetSocketAddress() { + String ip = config.getProperty(BIND_ADDRESS, "0.0.0.0"); + int port = Integer.valueOf(config.getProperty(BIND_PORT, "19090")); + return new InetSocketAddress(ip, port); + } + + public String getAddressPort() { + if (this.bindAddress == null && this.bindPort == null) { + return null; + } + + return this.bindAddress + ":" + this.bindPort; + } + + public boolean isDebug() { + + return Boolean.valueOf(config.getProperty(WARE_SWIFT_DEBUG)); + } + + public String getBindAddress() { + return this.bindAddress; + } + + public String getBindPort() { + return this.bindPort; + } + + public long getVoteCount() { + return voteCount; + } + + public synchronized void increVoteCount() { + this.voteCount++; + this.voteForTime += System.currentTimeMillis(); + } + + public long getTerm() { + return term; + } + + public synchronized void increTerm() { + this.term++; + } + + public String getVotedFor() { + return atomicVotedFor.get(); + } + + /** + * 投票的时候,需要保证并发性。因此这里需要处理如果发现已经投过票了,则返回 false. + * + * @param votedFor + * @return + */ + public boolean setVotedFor(String votedFor) { + return atomicVotedFor.compareAndSet(EMPTY_VOTE_FOR, votedFor); + } + + public int getRequestRequiredAcks(int defaultValue) { + + String acks = config.getProperty(REQUEST_REQUIRED_ACKS); + + return StringUtil.isNullOrEmpty(acks) ? defaultValue : Integer.valueOf(acks); + } + + /** + * @param defaultValue + * @return + */ + public long getSyncingMaxWaterMarker(long defaultValue) { + + String maxSyncingWaterMarker = config.getProperty(MAX_SYNCING_WATER_MARKER); + return StringUtil.isNullOrEmpty(maxSyncingWaterMarker) ? defaultValue + : Long.valueOf(maxSyncingWaterMarker); + } + + /** + * 即用来表示 + * + * @param defaultValue + * @return + */ + public long getCommittedMaxIdleTime(long defaultValue) { + + String maxCommittedIdleTimes = config.getProperty(MAX_COMMITTED_IDLE_TIME); + + return StringUtil.isNullOrEmpty(maxCommittedIdleTimes) ? defaultValue + : Long.valueOf(maxCommittedIdleTimes); + } + + /** + * 在感知到 leader 之后,做一些基本信息的 update 操作。 + *

+ * 注意: 需要更新本 节点的 vote for 信息 set is null。因此这个过程中有可能本节点已经投自己一票了,但是最终没有选举成功。 + *

+ * 为下一次选举投票做好准备。 + * + * @param leader + */ + public synchronized void updateAfterAwareLeader(NodeInformation leader) { + this.atomicVotedFor.set(EMPTY_VOTE_FOR); + this.term = leader.getTerm(); + this.hasCommittedCount = leader.getCommittedCount(); + if (leader.identify().equalsIgnoreCase(this.identify())) { + this.setNodeState(NodeState.Leader); + } else { + this.setNodeState(NodeState.Follower); + } + } + + /** + * 清除之前的一些必要的投票状态信息 + */ + public synchronized void clearVoteStatus() { + this.atomicVotedFor.set(EMPTY_VOTE_FOR); + this.voteForTime = 0; + this.voteCount = 0; + this.nodeState = NodeState.Follower; + } + + public AtomicLong getSdownVoteCount() { + return sdownVoteCount; + } + + /** + * 节点在哪个集群。${address:port}@${cluster_name} + * + * @return + */ + @Override + public String identify() { + return getAddressPort() + "@" + getClusterName(); + } + + public long incrementCommittedCount() { + final long tmpHasCommittedCount = hasCommittedCount++; + return tmpHasCommittedCount; + } + + public long getCommittedCount() { + + return hasCommittedCount; + } + + public long getHeartbeatInterval(long defaultValue) { + + return Long.valueOf(config.getProperty(SEND_HEARTBEAT_INTERVAL, + String.valueOf(defaultValue))); + } + + @Override + public String toString() { + return "NodeInformation{" + "votedFor='" + atomicVotedFor.get() + '\'' + + ", voteCount=" + voteCount + ", term=" + term + ", voteForTime=" + + voteForTime + ", nodeState=" + nodeState + ", clusterName='" + + clusterName + '\'' + ", bindAddress='" + bindAddress + '\'' + + ", bindPort='" + bindPort + '\'' + ", config=" + config + '}'; + } } diff --git a/core-swift/src/main/java/com/ware/swift/core/NodeState.java b/core-swift/src/main/java/com/ware/swift/core/NodeState.java index 03e485e..4e6f48b 100644 --- a/core-swift/src/main/java/com/ware/swift/core/NodeState.java +++ b/core-swift/src/main/java/com/ware/swift/core/NodeState.java @@ -5,5 +5,5 @@ */ public enum NodeState { - Follower, Candidate, Leader, SDOWN, ODOWN, PEER_TO_PEER, + Follower, Candidate, Leader, ODOWN, PEER_TO_PEER, } diff --git a/core-swift/src/main/java/com/ware/swift/core/WareCoreSwiftConfig.java b/core-swift/src/main/java/com/ware/swift/core/WareCoreSwiftConfig.java index 49a08a0..f4ba848 100644 --- a/core-swift/src/main/java/com/ware/swift/core/WareCoreSwiftConfig.java +++ b/core-swift/src/main/java/com/ware/swift/core/WareCoreSwiftConfig.java @@ -1,76 +1,75 @@ package com.ware.swift.core; -import java.util.*; +import java.util.LinkedHashSet; +import java.util.LinkedList; +import java.util.List; import java.util.concurrent.atomic.AtomicInteger; /** - * + * */ public class WareCoreSwiftConfig { - /** - * raft 集群 节点的信息。其中包含当前节点的 ip 和 端口号 - */ - private List clusterNodes = new LinkedList<>(); + /** + * wareSwift 集群 节点的信息。其中包含当前节点的 ip 和 端口号 + */ + private List clusterNodes = new LinkedList<>(); - /** - * 代表客观下线的 leader 节点 - */ - private volatile LinkedHashSet oDownList = new LinkedHashSet<>(); + /** + * 代表客观下线的 leader 节点 + */ + private volatile LinkedHashSet oDownList = new LinkedHashSet<>(); - private volatile NodeInformation leader; - /** - * 当前节点的配置信息 - */ - private NodeInformation nodeInformation = new NodeInformation(); + private volatile NodeInformation leader; + /** + * 当前节点的配置信息 + */ + private NodeInformation nodeInformation = new NodeInformation(); - /** - * 记录 say hello 发出去的消息包,收回的 ACK 中包含 多少个含有 leader 的信息。 - * - * 根据这个数达到半数以上,才可以进行 node - */ - private AtomicInteger sayHelloResponseLeaderCount = new AtomicInteger(); + /** + * 记录 say hello 发出去的消息包,收回的 ACK 中包含 多少个含有 leader 的信息。 + *

+ * 根据这个数达到半数以上,才可以进行 node + */ + private AtomicInteger sayHelloResponseLeaderCount = new AtomicInteger(); - public List getClusterNodes() { - return clusterNodes; - } + public List getClusterNodes() { + return clusterNodes; + } - public void setClusterNodes(List clusterNodes) { - this.clusterNodes = clusterNodes; - } + public void setClusterNodes(List clusterNodes) { + this.clusterNodes = clusterNodes; + } - public NodeInformation getNodeInformation() { - return nodeInformation; - } + public NodeInformation getNodeInformation() { + return nodeInformation; + } - public Set getoDownList() { - return Collections.unmodifiableSet(oDownList); - } + public void addODownNode(NodeInformation odownLeaderNode) { + oDownList.add(odownLeaderNode); + } - public void addODownNode(NodeInformation odownLeaderNode) { - oDownList.add(odownLeaderNode); - } + public NodeInformation getLeader() { + return leader; + } - public NodeInformation getLeader() { - return leader; - } + public synchronized NodeInformation getAndResetLeader() { + NodeInformation tmpLeader = leader; + leader = null; + return tmpLeader; + } - public synchronized NodeInformation getAndResetLeader() { - NodeInformation tmpLeader = leader; - leader = null; - return tmpLeader; - } + /** + * 注意:这里如果是 leader 本身设置的话,还需 + * + * @param leader + */ + public void setLeader(NodeInformation leader) { + this.leader = leader; + this.getNodeInformation().updateAfterAwareLeader(leader); + } - /** - * 注意:这里如果是 leader 本身设置的话,还需 - * @param leader - */ - public void setLeader(NodeInformation leader) { - this.leader = leader; - this.getNodeInformation().updateAfterAwareLeader(leader); - } - - public AtomicInteger getSayHelloResponseLeaderCount() { - return sayHelloResponseLeaderCount; - } + public AtomicInteger getSayHelloResponseLeaderCount() { + return sayHelloResponseLeaderCount; + } } diff --git a/core-swift/src/main/java/com/ware/swift/core/WareCoreSwiftConfigManager.java b/core-swift/src/main/java/com/ware/swift/core/WareCoreSwiftConfigManager.java index 682d016..d6d9e47 100644 --- a/core-swift/src/main/java/com/ware/swift/core/WareCoreSwiftConfigManager.java +++ b/core-swift/src/main/java/com/ware/swift/core/WareCoreSwiftConfigManager.java @@ -8,42 +8,40 @@ import java.util.Properties; /** - * + * */ public final class WareCoreSwiftConfigManager { - private static final InternalLogger logger = InternalLoggerFactory - .getInstance(WareCoreSwiftConfigManager.class); - - private static final String CONFIG_SUFIX_XML = "xml"; - - private static final String CONFIG_SUFIX_PROPERTIES = "properties"; - - public static WareCoreSwiftConfig initRaftConfig(String path, ClassLoader classLoader) - throws Exception { - String sufix = path.substring(path.lastIndexOf(".") + 1); - WareCoreSwiftConfig rafConfig = null; - switch (sufix) { - case CONFIG_SUFIX_PROPERTIES: { - rafConfig = initRaftConfigByProperties(classLoader.getResource(path)); - break; - } - } - return rafConfig; - } - - private static WareCoreSwiftConfig initRaftConfigByProperties(URL url) throws Exception { - Properties properties = new Properties(); - properties.load(url.openStream()); - String clusterNodes = properties.getProperty("cluster.nodes"); - if (WareCoreSwiftStringUtils.isBlank(clusterNodes)) { - throw new WareCoreSwiftException("cluster.nodes must config."); - } - String[] clusterNodeArr = clusterNodes.split("[;]"); - WareCoreSwiftConfig raftConfig = new WareCoreSwiftConfig(); - raftConfig.setClusterNodes(Arrays.asList(clusterNodeArr)); - raftConfig.getNodeInformation().setConfig(properties); - return raftConfig; - } + private static final InternalLogger logger = InternalLoggerFactory + .getInstance(WareCoreSwiftConfigManager.class); + + private static final String CONFIG_SUFFIX_PROPERTIES = "properties"; + + public static WareCoreSwiftConfig initRaftConfig(String path, ClassLoader classLoader) + throws Exception { + String suffix = path.substring(path.lastIndexOf(".") + 1); + WareCoreSwiftConfig rafConfig = null; + switch (suffix) { + case CONFIG_SUFFIX_PROPERTIES: { + rafConfig = initRaftConfigByProperties(classLoader.getResource(path)); + break; + } + } + return rafConfig; + } + + private static WareCoreSwiftConfig initRaftConfigByProperties(URL url) throws Exception { + Properties properties = new Properties(); + properties.load(url.openStream()); + String clusterNodes = properties.getProperty("cluster.nodes"); + if (WareCoreSwiftStringUtils.isBlank(clusterNodes)) { + throw new WareCoreSwiftException("cluster.nodes must config."); + } + String[] clusterNodeArr = clusterNodes.split("[;]"); + WareCoreSwiftConfig wareSwiftConfig = new WareCoreSwiftConfig(); + wareSwiftConfig.setClusterNodes(Arrays.asList(clusterNodeArr)); + wareSwiftConfig.getNodeInformation().setConfig(properties); + return wareSwiftConfig; + } } diff --git a/core-swift/src/main/java/com/ware/swift/core/WareCoreSwiftExceptionCode.java b/core-swift/src/main/java/com/ware/swift/core/WareCoreSwiftExceptionCode.java index f8fac64..7d9bddc 100644 --- a/core-swift/src/main/java/com/ware/swift/core/WareCoreSwiftExceptionCode.java +++ b/core-swift/src/main/java/com/ware/swift/core/WareCoreSwiftExceptionCode.java @@ -2,39 +2,42 @@ import java.text.MessageFormat; +/** + * + */ public final class WareCoreSwiftExceptionCode { - public final static String CONFIG_NOT_FOUNT_CODE = "RAFT-0001"; + public final static String CONFIG_NOT_FOUNT_CODE = "RAFT-0001"; - public final static String CONFIG_MALFORMED_URL_CODE = "RAFT-0002"; + public final static String CONFIG_MALFORMED_URL_CODE = "RAFT-0002"; - public final static String CONFIG_INITIALIZER_ERROR_CODE = "RAFT-0003"; + public final static String CONFIG_INITIALIZER_ERROR_CODE = "RAFT-0003"; - public final static String REMOTING_STARTUP_SERVER_ERROR_CODE = "RAFT-1001"; + public final static String REMOTING_STARTUP_SERVER_ERROR_CODE = "RAFT-1001"; - public final static String LEADER_ELECTION_VOTE_ERROR_CODE = "RAFT-1002"; + public final static String LEADER_ELECTION_VOTE_ERROR_CODE = "RAFT-1002"; - public final static String REMOTING_BROADCAST_LEADER_ERROR_CODE = "RAFT-1003"; + public final static String REMOTING_BROADCAST_LEADER_ERROR_CODE = "RAFT-1003"; - public final static String REMOTING_OUTBOUND_DATASET_ERROR_CODE = "RAFT-1004"; + public final static String REMOTING_OUTBOUND_DATASET_ERROR_CODE = "RAFT-1004"; - public static String getConfigNotFountExceptionMessage(String message) { + public static String getConfigNotFountExceptionMessage(String message) { - return formatExceptionMessage(CONFIG_NOT_FOUNT_CODE, message); - } + return formatExceptionMessage(CONFIG_NOT_FOUNT_CODE, message); + } - public static String getConfigMalformedUrlExceptionMessage(String message) { + public static String getConfigMalformedUrlExceptionMessage(String message) { - return formatExceptionMessage(CONFIG_MALFORMED_URL_CODE, message); - } + return formatExceptionMessage(CONFIG_MALFORMED_URL_CODE, message); + } - public static String getConfigInitializerErrorMessage(String message) { + public static String getConfigInitializerErrorMessage(String message) { - return formatExceptionMessage(CONFIG_INITIALIZER_ERROR_CODE, message); - } + return formatExceptionMessage(CONFIG_INITIALIZER_ERROR_CODE, message); + } - public static String formatExceptionMessage(String code, String message) { + public static String formatExceptionMessage(String code, String message) { - return MessageFormat.format("[%s]-%s", code, message); - } + return MessageFormat.format("[%s]-%s", code, message); + } } diff --git a/core-swift/src/main/java/com/ware/swift/core/WareCoreSwiftGlobalContext.java b/core-swift/src/main/java/com/ware/swift/core/WareCoreSwiftGlobalContext.java index 11da990..31d53bb 100644 --- a/core-swift/src/main/java/com/ware/swift/core/WareCoreSwiftGlobalContext.java +++ b/core-swift/src/main/java/com/ware/swift/core/WareCoreSwiftGlobalContext.java @@ -1,43 +1,83 @@ package com.ware.swift.core; import com.ware.swift.core.remoting.RemotingManager; +import com.ware.swift.event.object.AbstractAsyncEventObject; +import com.ware.swift.event.object.IEventObjectListener; import java.util.Hashtable; /** - * + * */ -public class WareCoreSwiftGlobalContext { +public class WareCoreSwiftGlobalContext extends AbstractAsyncEventObject { - public static final String RAFT_CONTEXT_ATTR_KEY_CONFIG_PATH = "B"; + public WareCoreSwiftGlobalContext(String executorName, boolean isOptimism) { + super(executorName, isOptimism); + } - public static final String RAFT_CONTEXT_ATTR_KEY_CONFIG = "C"; + private static final WareCoreSwiftGlobalContext WARE_CORE_SWIFT_GLOBAL_CONTEXT = new WareCoreSwiftGlobalContext("ware-swift-executor", true); - private Hashtable contextAttrs = new Hashtable(); + /** + * + */ + public static final String RAFT_CONTEXT_ATTR_KEY_CONFIG_PATH = "B"; - public void setConfigPath(String path) { - contextAttrs.put(RAFT_CONTEXT_ATTR_KEY_CONFIG_PATH, path); - } + /** + * + */ + public static final String RAFT_CONTEXT_ATTR_KEY_CONFIG = "C"; - public void putAttribute(String key, V value) { - assert key != null; - contextAttrs.put(key, value); - } + /** + * + */ + private Hashtable contextAttrs = new Hashtable<>(); - public V getAttribute(String key) { - assert key != null; - return (V) contextAttrs.get(key); - } + public static WareCoreSwiftGlobalContext getInstance() { - public V getAttribute(String key, String defaultValue) { - assert key != null; - return (V) contextAttrs.getOrDefault(key, defaultValue); - } + return WARE_CORE_SWIFT_GLOBAL_CONTEXT; + } - public NodeInformation getLeader() { - if (RemotingManager.getRemotingManager().getRaftConfig() == null) { - return null; - } - return RemotingManager.getRemotingManager().getRaftConfig().getLeader(); - } + /** + * @param path + */ + public void setConfigPath(String path) { + contextAttrs.put(RAFT_CONTEXT_ATTR_KEY_CONFIG_PATH, path); + } + + public void putAttribute(String key, V value) { + assert key != null; + contextAttrs.put(key, value); + } + + public V getAttribute(String key) { + assert key != null; + return (V) contextAttrs.get(key); + } + + public V getAttribute(String key, String defaultValue) { + assert key != null; + return (V) contextAttrs.getOrDefault(key, defaultValue); + } + + public NodeInformation getLeader() { + if (RemotingManager.getRemotingManager().getRaftConfig() == null) { + return null; + } + return RemotingManager.getRemotingManager().getRaftConfig().getLeader(); + } + + @Override + public void attachListener() { + // nothing to do + } + + /** + * @param objectListener + * @param eventType + * @param + */ + public void registerEventListener(IEventObjectListener objectListener, Integer eventType) { + + addListener(objectListener, eventType); + } } diff --git a/core-swift/src/main/java/com/ware/swift/core/WareCoreSwiftStringUtils.java b/core-swift/src/main/java/com/ware/swift/core/WareCoreSwiftStringUtils.java index debb63c..2a4c2f6 100644 --- a/core-swift/src/main/java/com/ware/swift/core/WareCoreSwiftStringUtils.java +++ b/core-swift/src/main/java/com/ware/swift/core/WareCoreSwiftStringUtils.java @@ -4,253 +4,191 @@ import java.util.regex.Pattern; public class WareCoreSwiftStringUtils { - /** - * A String for a space character. - * - * @since 3.2 - */ - public static final String SPACE = " "; - /** - * The empty String {@code ""}. - * @since 2.0 - */ - public static final String EMPTY = ""; - - /** - * A String for linefeed LF ("\n"). - * - * @see JLF: - * Escape Sequences for Character and String Literals - * @since 3.2 - */ - public static final String LF = "\n"; - - /** - * A String for carriage return CR ("\r"). - * - * @see JLF: - * Escape Sequences for Character and String Literals - * @since 3.2 - */ - public static final String CR = "\r"; - - /** - * Represents a failed index search. - * @since 2.1 - */ - public static final int INDEX_NOT_FOUND = -1; - - /** - *

- * The maximum size to which the padding constant(s) can expand. - *

- */ - private static final int PAD_LIMIT = 8192; - - /** - * A regex pattern for recognizing blocks of whitespace characters. The apparent - * convolutedness of the pattern serves the purpose of ignoring "blocks" consisting of - * only a single space: the pattern is used only to normalize whitespace, condensing - * "blocks" down to a single space, thus matching the same would likely cause a great - * many noop replacements. - */ - private static final Pattern WHITESPACE_PATTERN = Pattern - .compile("(?: |\\u00A0|\\s|[\\s&&[^ ]])\\s*"); - - /** - *

- * {@code StringUtils} instances should NOT be constructed in standard programming. - * Instead, the class should be used as {@code StringUtils.trim(" foo ");}. - *

- * - *

- * This constructor is public to permit tools that require a JavaBean instance to - * operate. - *

- */ - public WareCoreSwiftStringUtils() { - super(); - } - - public static boolean isEmpty(final CharSequence cs) { - return cs == null || cs.length() == 0; - } - - public static boolean isNotEmpty(final CharSequence cs) { - return !WareCoreSwiftStringUtils.isEmpty(cs); - } - - public static boolean isAnyEmpty(CharSequence... css) { - if (css == null) { - return true; - } - for (CharSequence cs : css) { - if (isEmpty(cs)) { - return true; - } - } - return false; - } - - public static boolean isNoneEmpty(CharSequence... css) { - return !isAnyEmpty(css); - } - - public static boolean isBlank(final CharSequence cs) { - int strLen; - if (cs == null || (strLen = cs.length()) == 0) { - return true; - } - for (int i = 0; i < strLen; i++) { - if (Character.isWhitespace(cs.charAt(i)) == false) { - return false; - } - } - return true; - } - - public static boolean isNotBlank(final CharSequence cs) { - return !WareCoreSwiftStringUtils.isBlank(cs); - } - - public static boolean isAnyBlank(CharSequence... css) { - if (css == null) { - return true; - } - for (CharSequence cs : css) { - if (isBlank(cs)) { - return true; - } - } - return false; - } - - public static boolean isNoneBlank(CharSequence... css) { - return !isAnyBlank(css); - } - - public static String trim(final String str) { - return str == null ? null : str.trim(); - } - - public static String trimToNull(final String str) { - final String ts = trim(str); - return isEmpty(ts) ? null : ts; - } - - public static String trimToEmpty(final String str) { - return str == null ? EMPTY : str.trim(); - } - - public static String strip(final String str) { - return strip(str, null); - } - - public static String stripToNull(String str) { - if (str == null) { - return null; - } - str = strip(str, null); - return str.isEmpty() ? null : str; - } - - public static String stripToEmpty(final String str) { - return str == null ? EMPTY : strip(str, null); - } - - public static String strip(String str, final String stripChars) { - if (isEmpty(str)) { - return str; - } - str = stripStart(str, stripChars); - return stripEnd(str, stripChars); - } - - public static String stripStart(final String str, final String stripChars) { - int strLen; - if (str == null || (strLen = str.length()) == 0) { - return str; - } - int start = 0; - if (stripChars == null) { - while (start != strLen && Character.isWhitespace(str.charAt(start))) { - start++; - } - } - else if (stripChars.isEmpty()) { - return str; - } - else { - while (start != strLen - && stripChars.indexOf(str.charAt(start)) != INDEX_NOT_FOUND) { - start++; - } - } - return str.substring(start); - } - - public static String stripEnd(final String str, final String stripChars) { - int end; - if (str == null || (end = str.length()) == 0) { - return str; - } - - if (stripChars == null) { - while (end != 0 && Character.isWhitespace(str.charAt(end - 1))) { - end--; - } - } - else if (stripChars.isEmpty()) { - return str; - } - else { - while (end != 0 - && stripChars.indexOf(str.charAt(end - 1)) != INDEX_NOT_FOUND) { - end--; - } - } - return str.substring(0, end); - } - - public static String[] stripAll(final String... strs) { - return stripAll(strs, null); - } - - public static String[] stripAll(final String[] strs, final String stripChars) { - int strsLen; - if (strs == null || (strsLen = strs.length) == 0) { - return strs; - } - final String[] newArr = new String[strsLen]; - for (int i = 0; i < strsLen; i++) { - newArr[i] = strip(strs[i], stripChars); - } - return newArr; - } - - public static String stripAccents(final String input) { - if (input == null) { - return null; - } - final Pattern pattern = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");//$NON-NLS-1$ - final String decomposed = Normalizer.normalize(input, Normalizer.Form.NFD); - // Note that this doesn't correctly remove ligatures... - return pattern.matcher(decomposed).replaceAll("");//$NON-NLS-1$ - } - - public static boolean equals(final CharSequence cs1, final CharSequence cs2) { - if (cs1 == cs2) { - return true; - } - if (cs1 == null || cs2 == null) { - return false; - } - if (cs1 instanceof String && cs2 instanceof String) { - return cs1.equals(cs2); - } - return false; - } + /** + * The empty String {@code ""}. + * + * @since 2.0 + */ + public static final String EMPTY = ""; + + /** + * Represents a failed index search. + * + * @since 2.1 + */ + public static final int INDEX_NOT_FOUND = -1; + + /** + *

+ * {@code StringUtils} instances should NOT be constructed in standard programming. + * Instead, the class should be used as {@code StringUtils.trim(" foo ");}. + *

+ * + *

+ * This constructor is public to permit tools that require a JavaBean instance to + * operate. + *

+ */ + public WareCoreSwiftStringUtils() { + super(); + } + + public static boolean isEmpty(final CharSequence cs) { + return cs == null || cs.length() == 0; + } + + public static boolean isAnyEmpty(CharSequence... css) { + if (css == null) { + return true; + } + for (CharSequence cs : css) { + if (isEmpty(cs)) { + return true; + } + } + return false; + } + + public static boolean isBlank(final CharSequence cs) { + int strLen; + if (cs == null || (strLen = cs.length()) == 0) { + return true; + } + for (int i = 0; i < strLen; i++) { + if (Character.isWhitespace(cs.charAt(i)) == false) { + return false; + } + } + return true; + } + + public static boolean isNotBlank(final CharSequence cs) { + return !WareCoreSwiftStringUtils.isBlank(cs); + } + + public static boolean isAnyBlank(CharSequence... css) { + if (css == null) { + return true; + } + for (CharSequence cs : css) { + if (isBlank(cs)) { + return true; + } + } + return false; + } + + public static String trim(final String str) { + return str == null ? null : str.trim(); + } + + public static String trimToEmpty(final String str) { + return str == null ? EMPTY : str.trim(); + } + + public static String strip(final String str) { + return strip(str, null); + } + + public static String stripToNull(String str) { + if (str == null) { + return null; + } + str = strip(str, null); + return str.isEmpty() ? null : str; + } + + public static String stripToEmpty(final String str) { + return str == null ? EMPTY : strip(str, null); + } + + public static String strip(String str, final String stripChars) { + if (isEmpty(str)) { + return str; + } + str = stripStart(str, stripChars); + return stripEnd(str, stripChars); + } + + public static String stripStart(final String str, final String stripChars) { + int strLen; + if (str == null || (strLen = str.length()) == 0) { + return str; + } + int start = 0; + if (stripChars == null) { + while (start != strLen && Character.isWhitespace(str.charAt(start))) { + start++; + } + } else if (stripChars.isEmpty()) { + return str; + } else { + while (start != strLen + && stripChars.indexOf(str.charAt(start)) != INDEX_NOT_FOUND) { + start++; + } + } + return str.substring(start); + } + + public static String stripEnd(final String str, final String stripChars) { + int end; + if (str == null || (end = str.length()) == 0) { + return str; + } + + if (stripChars == null) { + while (end != 0 && Character.isWhitespace(str.charAt(end - 1))) { + end--; + } + } else if (stripChars.isEmpty()) { + return str; + } else { + while (end != 0 + && stripChars.indexOf(str.charAt(end - 1)) != INDEX_NOT_FOUND) { + end--; + } + } + return str.substring(0, end); + } + + public static String[] stripAll(final String... strs) { + return stripAll(strs, null); + } + + public static String[] stripAll(final String[] strs, final String stripChars) { + int strsLen; + if (strs == null || (strsLen = strs.length) == 0) { + return strs; + } + final String[] newArr = new String[strsLen]; + for (int i = 0; i < strsLen; i++) { + newArr[i] = strip(strs[i], stripChars); + } + return newArr; + } + + public static String stripAccents(final String input) { + if (input == null) { + return null; + } + final Pattern pattern = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");//$NON-NLS-1$ + final String decomposed = Normalizer.normalize(input, Normalizer.Form.NFD); + // Note that this doesn't correctly remove ligatures... + return pattern.matcher(decomposed).replaceAll("");//$NON-NLS-1$ + } + + public static boolean equals(final CharSequence cs1, final CharSequence cs2) { + if (cs1 == cs2) { + return true; + } + if (cs1 == null || cs2 == null) { + return false; + } + if (cs1 instanceof String && cs2 instanceof String) { + return cs1.equals(cs2); + } + return false; + } } diff --git a/core-swift/src/main/java/com/ware/swift/core/WareSwiftDeveloperManager.java b/core-swift/src/main/java/com/ware/swift/core/WareSwiftDeveloperManager.java new file mode 100644 index 0000000..c4abe5f --- /dev/null +++ b/core-swift/src/main/java/com/ware/swift/core/WareSwiftDeveloperManager.java @@ -0,0 +1,155 @@ +/* + * Copyright (C) 2019 the original author or authors. + * + * 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 com.ware.swift.core; + +import com.google.protobuf.ByteString; +import com.ware.swift.core.remoting.ICapabilityModel; +import com.ware.swift.core.remoting.RemotingDomainSupport; +import com.ware.swift.core.remoting.RemotingInteractiveConstants; +import com.ware.swift.core.remoting.RemotingManager; +import com.ware.swift.event.ICallbackHook; +import com.ware.swift.proto.InteractivePayload; +import io.netty.util.CharsetUtil; + +import java.util.logging.Logger; + +/** + * 统一面向使用 ware swift 开发者的管理类。 + *

+ * 使用该类,你可以获取到当前系统配置的{@link com.ware.swift.core.remoting.ICapabilityModel} + * + * @author pbting + * @date 2019-05-19 9:49 PM + */ +public final class WareSwiftDeveloperManager { + + private static final Logger logger = Logger.getLogger(WareSwiftDeveloperManager.class.getCanonicalName()); + + /** + * 客户端响应的状态码。head 为 100 的为客户端保留的值。建议业务方自己定义自己的key 范围在 1000 及以上。 + */ + public static final Integer RESPONSE_STATUS_CODE_HEAD_KEY = 100; + /** + * 客户端响应的原因短语。 + */ + public static final Integer RESPONSE_REASON_PHRASE_HEAD_KEY = 101; + + /** + * 向客户端返回成功状态码 + */ + public static final Integer SUCCESS_STATUS_CODE = 8200; + + /** + * 向客户端返回需要注意的提示信息状态。 + *

+ * 此状态是表示客户端注意了,当前处理是 ack 小于 {@link NodeInformation#REQUEST_REQUIRED_ACKS} + */ + public static final Integer ATTENTION_ACK_STATUS_CODE = 8100; + + + public static final Integer EXCEPTION_STATUS_CODE = 8500; + + public static boolean isDebug() { + + return RemotingManager.getRemotingManager().getRaftConfig().getNodeInformation().isDebug(); + } + + /** + * @return + */ + public static ICapabilityModel getCapabilityModel() { + + return RemotingManager.getRemotingManager().getCapabilityModel(); + } + + public static V getClientPayloadHandler() { + + return (V) RemotingManager.getRemotingManager().getClientInteractivePayloadHandler(); + } + + /** + * @param successPayload + * @param reasonPhrase + * @return + */ + public static InteractivePayload buildSuccessInteractivePayload(String successPayload, String reasonPhrase) { + InteractivePayload.Builder builder = InteractivePayload.newBuilder(); + builder.putHeaders(RESPONSE_STATUS_CODE_HEAD_KEY, String.valueOf(SUCCESS_STATUS_CODE)); + builder.putHeaders(RESPONSE_REASON_PHRASE_HEAD_KEY, reasonPhrase); + builder.setPayload(ByteString.copyFrom(successPayload.getBytes(CharsetUtil.UTF_8))); + return builder.build(); + } + + /** + * @param payload + * @param reasonPhrase + * @return + */ + public static InteractivePayload buildResponseInteractivePayload(String payload, String reasonPhrase, ICallbackHook callbackHook) { + InteractivePayload.Builder builder = InteractivePayload.newBuilder(); + callbackHook.callback(builder); + builder.putHeaders(RESPONSE_REASON_PHRASE_HEAD_KEY, reasonPhrase); + builder.setPayload(ByteString.copyFrom(payload.getBytes(CharsetUtil.UTF_8))); + return builder.build(); + } + + /** + * @param sink + * @param payload + * @param callbackHook + * @param + * @return + */ + public static InteractivePayload buildInteractivePayload(String sink, V payload, ICallbackHook callbackHook) { + InteractivePayload.Builder builder = InteractivePayload.newBuilder(); + callbackHook.callback(builder); + builder.setSink(sink); + builder.setPayload(ByteString.copyFrom(RemotingInteractiveConstants.OBJECT_ENCODING_HANDLER.encodingResult(payload))); + return builder.build(); + } + + /** + * @param sink + * @param payload + * @param + * @return + */ + public static InteractivePayload buildInteractivePayload(String sink, V payload) { + + return buildInteractivePayload(sink, payload, new ICallbackHook.EmptyCallbackHook()); + } + + public static V deserialize(byte[] source, Class clazz) { + + return (V) RemotingInteractiveConstants.OBJECT_ENCODING_HANDLER.decodeResult(source, clazz); + } + + public static byte[] serialize(RemotingDomainSupport remotingDomainSupport) { + + return RemotingInteractiveConstants.OBJECT_ENCODING_HANDLER.encodingResult(remotingDomainSupport).array(); + } + + public static WareCoreSwiftGlobalContext wareSwiftInit(String configFile) { + long start = System.currentTimeMillis(); + logger.info("start to init ware swift with the config file: " + configFile); + WareCoreSwiftGlobalContext wareCoreSwiftGlobalContext = WareCoreSwiftGlobalContext.getInstance(); + wareCoreSwiftGlobalContext.setConfigPath(configFile); + CoreSwiftManagerDelegate.getInstance().init(wareCoreSwiftGlobalContext); + long end = System.currentTimeMillis(); + logger.info("end to init ware swift and cost time= " + (end - start) + " Ms."); + return wareCoreSwiftGlobalContext; + } +} \ No newline at end of file diff --git a/core-swift/src/main/java/com/ware/swift/core/inter/ICoreSwiftManager.java b/core-swift/src/main/java/com/ware/swift/core/inter/ICoreSwiftManager.java index 917178a..880703e 100644 --- a/core-swift/src/main/java/com/ware/swift/core/inter/ICoreSwiftManager.java +++ b/core-swift/src/main/java/com/ware/swift/core/inter/ICoreSwiftManager.java @@ -4,44 +4,42 @@ import com.ware.swift.core.WareCoreSwiftGlobalContext; /** - * + * */ public interface ICoreSwiftManager { - void init(WareCoreSwiftGlobalContext raftGlobalContext); - - /** - * - * @param raftGlobaleContext - */ - void startBefore(WareCoreSwiftGlobalContext raftGlobaleContext); - - /** - * - * @param raftGlobaleContext - */ - void start(WareCoreSwiftGlobalContext raftGlobaleContext); - - /** - * - * @param raftGlobaleContext - */ - void startAfter(WareCoreSwiftGlobalContext raftGlobaleContext); - - /** - * - * @param nodeInformation - */ - void shutdown(NodeInformation nodeInformation); - - /** - * - */ - void getAllNodeInformations(); - - /** - * - * @param clusterName - */ - void getClusterNodeInformations(String clusterName); + /** + * @param wareSwiftGlobalContext + */ + void init(WareCoreSwiftGlobalContext wareSwiftGlobalContext); + + /** + * @param wareSwiftGlobalContext + */ + void startBefore(WareCoreSwiftGlobalContext wareSwiftGlobalContext); + + /** + * @param wareSwiftGlobalContext + */ + void start(WareCoreSwiftGlobalContext wareSwiftGlobalContext); + + /** + * @param wareSwiftGlobalContext + */ + void startAfter(WareCoreSwiftGlobalContext wareSwiftGlobalContext); + + /** + * @param nodeInformation + */ + void shutdown(NodeInformation nodeInformation); + + /** + * + */ + void getAllNodeInformation(); + + /** + * @param clusterName + */ + void getClusterNodeInformation(String clusterName); } diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/AbstractRemotingManager.java b/core-swift/src/main/java/com/ware/swift/core/remoting/AbstractRemotingManager.java index bde5c7e..66ce84f 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/AbstractRemotingManager.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/AbstractRemotingManager.java @@ -11,6 +11,8 @@ import com.ware.swift.event.loop.EventLoopConstants; import com.ware.swift.event.parallel.SuperFastParallelQueueExecutor; import com.ware.swift.proto.InteractivePayload; +import io.netty.util.internal.logging.InternalLogger; +import io.netty.util.internal.logging.InternalLoggerFactory; import java.util.*; import java.util.concurrent.ConcurrentHashMap; @@ -24,27 +26,27 @@ * */ public abstract class AbstractRemotingManager - implements IRemotingManager, IPluginable, IMailbox { + implements IRemotingManager, IPluginAble, IMailbox { + private static final InternalLogger logger = InternalLoggerFactory.getInstance(AbstractRemotingManager.class); protected static final DefaultEventLoopGroup remotingEventLoopGroup = new DefaultEventLoopGroup( new SuperFastParallelQueueExecutor( - Runtime.getRuntime().availableProcessors() * 2 + 1, "raft-thread"), + Runtime.getRuntime().availableProcessors() * 2 + 1, "wareSwift-thread"), true); protected final AtomicLong SEND_IDENTIFY_COUNT = new AtomicLong(); - protected WareCoreSwiftConfig raftConfig; + protected WareCoreSwiftConfig wareSwiftConfig; - protected WareCoreSwiftGlobalContext raftGlobalContext; + protected WareCoreSwiftGlobalContext wareSwiftGlobalContext; protected ICapabilityModel iCapabilityModel = ICapabilityModel .getInstance(this.getClass().getClassLoader()); - protected IClientInteractivePayloadHandler clientInteractivePayloadHandler = IClientInteractivePayloadHandler - .getInstance(this.getClass().getClassLoader()); + protected IClientInteractivePayloadHandler clientInteractivePayloadHandler; /** - * raft 节点之间的通信或者数据同步 使用 Remoting Channel 实例对象来发送数据 + * wareSwift 节点之间的通信或者数据同步 使用 Remoting Channel 实例对象来发送数据 */ private final ConcurrentHashMap remotingChannelRegistry = new ConcurrentHashMap<>(); @@ -128,6 +130,14 @@ public abstract class AbstractRemotingManager */ private String implyVoteFor; + { + this.clientInteractivePayloadHandler = + IClientInteractivePayloadHandler + .getInstance(this.getClass().getClassLoader()); + + clientInteractivePayloadHandler.onAfterConstructInstance(); + } + public void initEventPartitioner() { // final List partitionerEventList = new LinkedList<>(); @@ -219,9 +229,9 @@ public String getPluginName() { return this.getClass().getName(); } - public boolean init(WareCoreSwiftConfig raftConfig) { + public boolean init(WareCoreSwiftConfig wareSwiftConfig) { initEventListener(); - remotingEventLoopGroup.publish(raftConfig, START_UP_EVENT_TYPE); + remotingEventLoopGroup.publish(wareSwiftConfig, START_UP_EVENT_TYPE); return true; } @@ -233,14 +243,14 @@ public boolean init(WareCoreSwiftConfig raftConfig) { * 2、集群运行过程中,如果发现收到 leader 的心跳超时了,也会触发一次 leader 选择。不过这个时候会判断超过半数以上认为 leader 确实已经 * offline 了,才会启动一次新的选举。 */ - public void startLeaderElection(WareCoreSwiftGlobalContext raftGlobaleContext) { - this.raftGlobalContext = raftGlobaleContext; - remotingEventLoopGroup.publish(raftGlobaleContext, LEADER_ELECTION_EVENT_TYPE); + public void startLeaderElection(WareCoreSwiftGlobalContext wareSwiftGlobaleContext) { + this.wareSwiftGlobalContext = wareSwiftGlobaleContext; + remotingEventLoopGroup.publish(wareSwiftGlobaleContext, LEADER_ELECTION_EVENT_TYPE); } public WareCoreSwiftConfig getRaftConfig() { - return raftConfig; + return wareSwiftConfig; } @Override @@ -269,14 +279,13 @@ public void sendHeartbeatsIfExist(String sourceIdentifyChannel) { remotingChannel.closeOnlineCheckStatus(); sendHeartbeats(remotingChannel); } - System.err.println(" --> sendHeartbeatsIfExist:" + remotingChannel.identify() - + "; " + remotingChannel.isOpenOnlineCheck()); } @Override public void startLeaderHeartbeatTimeoutCheck() { - remotingEventLoopGroup.publish(this, - START_LEADER_HEARTBEAT_TIMEOUT_CHECK_EVENT_TYPE); + ObjectEvent objectEvent = new ObjectEvent(this, this, START_LEADER_HEARTBEAT_TIMEOUT_CHECK_EVENT_TYPE); + objectEvent.setParameter(EventLoopConstants.EVENT_LOOP_INTERVAL_PARAM, wareSwiftConfig.getNodeInformation().getHeartbeatInterval(1000)); + remotingEventLoopGroup.notifyListeners(objectEvent); } @Override @@ -299,20 +308,20 @@ public void broadcastSdown(NodeInformation sdownNode) { @Override public void reElectionForLeader() { // 检测是否需要重新选举 - if (raftConfig.getLeader() != null) { - NodeInformation odown = raftConfig.getAndResetLeader(); + if (wareSwiftConfig.getLeader() != null) { + NodeInformation odown = wareSwiftConfig.getAndResetLeader(); if (odown != null) { odown.setNodeState(NodeState.ODOWN); - raftConfig.addODownNode(odown); + wareSwiftConfig.addODownNode(odown); } } - raftConfig.getNodeInformation().clearVoteStatus(); + wareSwiftConfig.getNodeInformation().clearVoteStatus(); // 检测是否开启重新选举 remotingEventLoopGroup.removeListener(LEADER_ELECTION_EVENT_TYPE); initLeaderElectionEventListener(false); - System.err.println("开始新一轮的 Leader 选举.."); - startLeaderElection(raftGlobalContext); + logger.info("开始新一轮的 Leader 选举.."); + startLeaderElection(wareSwiftGlobalContext); } @Override @@ -386,7 +395,7 @@ public void removeSyncingRemotingDoamin(RemotingDomainWrapper remotingDoaminWrap * @param remotingChannel */ @Override - public Collection getCommittedRemotingDomains( + public Collection getCommittedRemotingDomains( Collection committedIds, AbstractRemotingChannel remotingChannel) { collectorCommittedIds.addAll(committedIds); // check @@ -394,8 +403,8 @@ public Collection getCommittedRemotingDomains( if (size == 0) { return Collections.emptyList(); } - Collection committedRemotingDomain = new LinkedList<>(); - final Collection outOfSyncingWaterMarkerRemotingDomain = new LinkedList<>(); + Collection committedRemotingDomain = new LinkedList<>(); + final Collection outOfSyncingWaterMarkerRemotingDomain = new LinkedList<>(); int activeChannel = RemotingManager.getRemotingManager().getActiveChannelCount(); Set removedIds = new HashSet<>(); @@ -511,10 +520,10 @@ public void clearSyncingRemotingDomainIds() { * @return */ @Override - public Collection committedSyncingDomains(long term) { + public Collection committedSyncingDomains(long term) { - List committedRemotingDomains = new LinkedList<>(); - final List outOfSyncingWaterMarkerRemotingDomains = new LinkedList<>(); + List committedRemotingDomains = new LinkedList<>(); + final List outOfSyncingWaterMarkerRemotingDomains = new LinkedList<>(); List removeIds = new LinkedList<>(); for (RemotingDomainWrapper remotingDomainWrapper : syncingRemotingDomainRepository .values()) { @@ -583,12 +592,12 @@ public int getActiveChannelCount() { */ @Override public void isOnlineWithRemotingChannel(String source) { - if (raftConfig.getLeader() == null) { + if (wareSwiftConfig.getLeader() == null) { return; } - if (!raftConfig.getLeader().identify() - .equals(raftConfig.getNodeInformation().identify())) { + if (!wareSwiftConfig.getLeader().identify() + .equals(wareSwiftConfig.getNodeInformation().identify())) { return; } @@ -596,8 +605,8 @@ public void isOnlineWithRemotingChannel(String source) { } @Override - public void setRaftConfig(WareCoreSwiftConfig raftConfig) { - this.raftConfig = raftConfig; + public void setRaftConfig(WareCoreSwiftConfig wareSwiftConfig) { + this.wareSwiftConfig = wareSwiftConfig; } @Override diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/ClusterDataSyncManager.java b/core-swift/src/main/java/com/ware/swift/core/remoting/ClusterDataSyncManager.java index 313be11..fba49f1 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/ClusterDataSyncManager.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/ClusterDataSyncManager.java @@ -1,11 +1,11 @@ package com.ware.swift.core.remoting; +import com.google.protobuf.ByteString; import com.ware.swift.core.remoting.avalispart.IAvailableCapabilityModel; import com.ware.swift.core.remoting.channel.AbstractRemotingChannel; import com.ware.swift.core.remoting.conspart.IConsistenceCapabilityModel; import com.ware.swift.core.remoting.event.remoting.RemotingEventDispatcher; import com.ware.swift.proto.InteractivePayload; -import com.google.protobuf.ByteString; import io.netty.util.CharsetUtil; import java.util.Arrays; @@ -39,22 +39,24 @@ public final class ClusterDataSyncManager { public static final String DATA_SYNC_STREAM_COMMITTED_TOPIC = "dataSyncStreamCommittedTopic"; public static final String DATA_SYNC_STREAM_SYNCING_TOPIC = "dataSyncStreamSyncingTopic"; + public static final IClusterSyncCallbackHoot EMPTY_CALLBACK_HOOK = new IClusterSyncCallbackHoot.EmptyClusterSyncCallbackHook(); + /** - * @param nodeIndentify + * @param nodeIdentify * @return */ - public static final String formatMovedResonPhrase(String nodeIndentify) { + public static final String formatMovedResonPhrase(String nodeIdentify) { - return String.format(DATA_SYNC_REASON_PHRASE_MOVED, nodeIndentify); + return String.format(DATA_SYNC_REASON_PHRASE_MOVED, nodeIdentify); } /** - * @param remotingDoaminClass + * @param remotingDomainClass * @return */ - public static final String formatSuccessResonPhrase(String remotingDoaminClass) { + public static final String formatSuccessReasonPhrase(String remotingDomainClass) { - return String.format(DATA_SYNC_REASON_PHRASE_SUCCESS, remotingDoaminClass); + return String.format(DATA_SYNC_REASON_PHRASE_SUCCESS, remotingDomainClass); } /** @@ -84,9 +86,9 @@ public static int[] buildRequireAcksAndGetActiveChannel() { return requireAcksAndActiveChannel; } - public static String generatorDataSyncingId(String localNodeIndentify) { + public static String generatorDataSyncingId(String localNodeIdentify) { String idFormat = "%s.%s.%s"; - return String.format(idFormat, localNodeIndentify, + return String.format(idFormat, localNodeIdentify, DATA_SYNCING_REMOTING_DOMAIN_ID.incrementAndGet(), System.currentTimeMillis()); } @@ -98,15 +100,30 @@ public static String generatorDataSyncingId(String localNodeIndentify) { * @return */ public static InteractivePayload newSyncInteractivePayload( - RemotingDomain remotingDomain) { + RemotingDomainSupport remotingDomain) { + + return newSyncInteractivePayload(remotingDomain, EMPTY_CALLBACK_HOOK); + } + + /** + * 基于需要同步的数据 new 一个用来两个进程之间需要同步的数据结构{@link InteractivePayload} + * + * @param remotingDomain + * @return + */ + public static InteractivePayload newSyncInteractivePayload( + RemotingDomainSupport remotingDomain, IClusterSyncCallbackHoot callbackHook) { IRemotingManager remotingManager = RemotingManager.getRemotingManager(); long term = remotingManager.getRaftConfig().getNodeInformation().getTerm(); String currentDataSyncingId = ClusterDataSyncManager.generatorDataSyncingId( remotingManager.getRaftConfig().getNodeInformation().identify()); // 这块构建 Interactive payload 代码是可以优化的。 InteractivePayload.Builder builder = InteractivePayload.newBuilder(); + builder.setSource(remotingManager.getChannelIdentify( remotingManager.getRaftConfig().getNodeInformation().identify())); + callbackHook.callback(builder); + // callback hook 放在下面的前面,是因为以下这些参数的设置是不能被覆盖的。 builder.putHeaders(ClusterDataSyncManager.HEADER_KEY_REMOTING_DOMAIN_CLASS, remotingDomain.getClass().getName()); builder.putHeaders(ClusterDataSyncManager.HEADER_KEY_TERM_VALUE, term + ""); @@ -190,7 +207,7 @@ private static void onReSyncCommitted(InteractivePayload interactivePayload) { .get(ClusterDataSyncManager.HEADER_KEY_REMOTING_DOMAIN_CLASS); try { Class clazz = Class.forName(remotingDomainClass); - RemotingDomain remotingDomain = (RemotingDomain) RemotingInteractiveConstants.OBJECT_ENCODING_HANDLER + RemotingDomainSupport remotingDomain = (RemotingDomainSupport) RemotingInteractiveConstants.OBJECT_ENCODING_HANDLER .decodeResult(interactivePayload.getPayload().toByteArray(), clazz); ICapabilityModel capabilityModel = RemotingManager.getRemotingManager() diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/DataSyncEmitter.java b/core-swift/src/main/java/com/ware/swift/core/remoting/DataSyncEmitter.java index 526f105..ffdff75 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/DataSyncEmitter.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/DataSyncEmitter.java @@ -13,7 +13,7 @@ public class DataSyncEmitter { private IInteractive interactive; private AtomicLong syncDataSize = new AtomicLong(); - private List syncFails = new LinkedList<>(); + private List syncFails = new LinkedList<>(); public DataSyncEmitter(IInteractive interactive) { this.interactive = interactive; @@ -24,14 +24,12 @@ public DataSyncEmitter(IInteractive interactive) { * * @param remotingDomain */ - public void onEmit(RemotingDomain remotingDomain) { + public void onEmit(RemotingDomainSupport remotingDomain) { boolean isSendSuccess = interactive.sendPayload( ClusterDataSyncManager.newSyncInteractivePayload(remotingDomain)); if (!isSendSuccess) { syncFails.add(remotingDomain); } - System.err.println("\t Leader send syncing size=>" - + syncDataSize.incrementAndGet() + "; send success:" + isSendSuccess); } public void onEmitFinish() { diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/DefaultClientInteractivePayloadHandler.java b/core-swift/src/main/java/com/ware/swift/core/remoting/DefaultClientInteractivePayloadHandler.java index 60f7326..5c14fd6 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/DefaultClientInteractivePayloadHandler.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/DefaultClientInteractivePayloadHandler.java @@ -2,11 +2,11 @@ import com.google.protobuf.ByteString; import com.ware.swift.core.NodeInformation; +import com.ware.swift.proto.InteractivePayload; import io.netty.util.CharsetUtil; import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLoggerFactory; -import com.ware.swift.proto.InteractivePayload; import java.util.Set; /** @@ -17,13 +17,13 @@ * 1. 判断当前的请求是查询的,还是 create/update/delete *

* 2. - * 如果确定数据要同步,则调用{@link ICapabilityModel#onOutboundDataSet(RemotingDomain, OutboundCallback)} + * 如果确定数据要同步,则调用{@link ICapabilityModel#onOutboundDataSet(RemotingDomainSupport, OutboundCallback)} * (InteractivePayload)} *

* 需要同步的数据默认情况下会在下一次发送心跳的时候给同步其他节点。这种情况下通常是在 AP 模型下需要。 *

* 3. - * 如果无需等待下一次发送心跳的时候就需要将数据立马同步到其他节点,这个时候可以调用{@link ICapabilityModel#onOutboundDataSet(RemotingDomain, OutboundCallback)} + * 如果无需等待下一次发送心跳的时候就需要将数据立马同步到其他节点,这个时候可以调用{@link ICapabilityModel#onOutboundDataSet(RemotingDomainSupport, OutboundCallback)} * (InteractivePayload, Callable)} */ public class DefaultClientInteractivePayloadHandler @@ -57,7 +57,7 @@ public class DefaultClientInteractivePayloadHandler public void handler(final IInteractive interactive) { InteractivePayload interactivePayload = interactive.getInteractivePayload(); String sink = interactivePayload.getSink(); - if (sink.equals(CLIENT_SINK_GET_LEADER)) { + if (CLIENT_SINK_GET_LEADER.equals(sink)) { RemotingManager remotingManager = (RemotingManager) RemotingManager .getRemotingManager(); if (remotingManager.isLeaderFollowerRemotingManager()) { @@ -68,9 +68,12 @@ public void handler(final IInteractive interactive) { processGetLeader(RemotingManager.getRemotingManager().getRaftConfig() .getNodeInformation(), interactive); } - } else if (sink.equals(CLIENT_SINK_DATA_SYNC)) { + } else if (CLIENT_SINK_DATA_SYNC.equals(sink)) { processDataSync(interactive, interactivePayload); + } else { + + processCustomSink(sink, interactive); } } @@ -89,13 +92,12 @@ private void processDataSync(IInteractive interactive, String clazzName = interactivePayload.getHeadersMap() .get(ClusterDataSyncManager.HEADER_KEY_REMOTING_DOMAIN_CLASS); Class clazz = Class.forName(clazzName); - RemotingDomain remotingDomain = (RemotingDomain) RemotingInteractiveConstants.OBJECT_ENCODING_HANDLER + RemotingDomainSupport remotingDomain = (RemotingDomainSupport) RemotingInteractiveConstants.OBJECT_ENCODING_HANDLER .decodeResult(interactivePayload.getPayload().toByteArray(), clazz); final OutboundCallback outboundCallback = new OutboundCallback() { @Override public void onSyncingSuccess(Set channelIdentifies) { - System.err.println(channelIdentifies.toArray()); InteractivePayload.Builder builder = InteractivePayload.newBuilder(); builder.setPayload(ByteString.copyFrom( channelIdentifies.toString().getBytes(CharsetUtil.UTF_8))); @@ -107,7 +109,7 @@ public void onSyncingFailed(Set successChannelIdentifies, Set failedChannelIdentifies) { String re = successChannelIdentifies.toString() + ";" + failedChannelIdentifies.toString(); - System.err.println(successChannelIdentifies.toArray() + ";" + log.info(successChannelIdentifies.toArray() + ";" + failedChannelIdentifies.toArray()); InteractivePayload.Builder builder = InteractivePayload.newBuilder(); builder.setPayload( @@ -135,4 +137,12 @@ public void onMinRequestRequiredAcks(int requestRequiredAcks, log.error("data sync cause an exception.", e); } } + + /** + * 当从客户端接收到的消息,默认处理没有到具体的 sink 类型。就会触发这里的逻辑。 + */ + public void processCustomSink(final String sink, final IInteractive interactive) { + + // nothing to do + } } diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/ICapabilityModel.java b/core-swift/src/main/java/com/ware/swift/core/remoting/ICapabilityModel.java index 5bba966..956cd30 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/ICapabilityModel.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/ICapabilityModel.java @@ -1,6 +1,7 @@ package com.ware.swift.core.remoting; import com.ware.swift.core.WareCoreSwiftPluginLoader; +import com.ware.swift.proto.InteractivePayload; import java.util.Collection; import java.util.Map; @@ -11,52 +12,62 @@ */ public interface ICapabilityModel { - /** - * 节点接收数据的处理入口。 - * @param inBoundValue - * @param {@link InteractivePayload} - * @return - */ - void onInboundDataSet(RemotingDomain inBoundValue, Map headsMap) - throws RemotingInteractiveException; - - /** - * 提供数据输出完成后的一个可执行回调的能力。 - * @param outBoundValue - * @param outboundCallback - */ - void onOutboundDataSet(RemotingDomain outBoundValue, - OutboundCallback outboundCallback); - - /** - * 数据流同步 - * @param syncDataEmitter - */ - void onDataStreamSyncing(DataSyncEmitter syncDataEmitter); - - /** - * out of syncing water marker will notify the business. - * @param outOfSyncingWaterMarker - */ - default void onOutOfSyncingWaterMarker( - Collection outOfSyncingWaterMarker) { - // 不处理的话,就可以不实现该接口。否则可以实现该接口来处理 committed 超时的那些个数据 - } - - /** - * obtain the {@link ICapabilityModel} from file - * @param classLoader - * @return - */ - static ICapabilityModel getInstance(ClassLoader classLoader) { - Set capabilityModels = WareCoreSwiftPluginLoader - .load(ICapabilityModel.class, classLoader); - if (capabilityModels == null) { - // the default model is CP - throw new IllegalStateException( - "the " + ICapabilityModel.class.getName() + " must be set."); - } - - return capabilityModels.iterator().next(); - } + /** + * 节点接收数据的处理入口。 + * + * @param inBoundValue + * @param {@link InteractivePayload} + * @return + */ + void onInboundDataSet(RemotingDomainSupport inBoundValue, Map headsMap) + throws RemotingInteractiveException; + + /** + * 提供数据输出完成后的一个可执行回调的能力。 + * + * @param outBoundValue + * @param outboundCallback + */ + void onOutboundDataSet(RemotingDomainSupport outBoundValue, + OutboundCallback outboundCallback); + + /** + * 数据流同步 + * + * @param syncDataEmitter + */ + void onDataStreamSyncing(DataSyncEmitter syncDataEmitter); + + /** + * out of syncing water marker will notify the business. + * + * @param outOfSyncingWaterMarker + */ + default void onOutOfSyncingWaterMarker( + Collection outOfSyncingWaterMarker) { + // 不处理的话,就可以不实现该接口。否则可以实现该接口来处理 committed 超时的那些个数据 + } + + default InteractivePayload buildSyncInteractivePayload(RemotingDomainSupport remotingDomain) { + + return ClusterDataSyncManager.newSyncInteractivePayload(remotingDomain); + } + + /** + * obtain the {@link ICapabilityModel} from file + * + * @param classLoader + * @return + */ + static ICapabilityModel getInstance(ClassLoader classLoader) { + Set capabilityModels = WareCoreSwiftPluginLoader + .load(ICapabilityModel.class, classLoader); + if (capabilityModels == null) { + // the default model is CP + throw new IllegalStateException( + "the " + ICapabilityModel.class.getName() + " must be set."); + } + + return capabilityModels.iterator().next(); + } } diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/IClientInteractivePayloadHandler.java b/core-swift/src/main/java/com/ware/swift/core/remoting/IClientInteractivePayloadHandler.java index a1ecc6b..e483593 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/IClientInteractivePayloadHandler.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/IClientInteractivePayloadHandler.java @@ -10,6 +10,14 @@ */ public interface IClientInteractivePayloadHandler { + /** + * 在构造玩实例之后可能需要一些初始化的操作。 + */ + default void onAfterConstructInstance() { + + // nothing to do + } + /** * @param interactive */ diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/IClusterSyncCallbackHoot.java b/core-swift/src/main/java/com/ware/swift/core/remoting/IClusterSyncCallbackHoot.java new file mode 100644 index 0000000..3278785 --- /dev/null +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/IClusterSyncCallbackHoot.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2019 the original author or authors. + * + * 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 com.ware.swift.core.remoting; + +import com.ware.swift.event.ICallbackHook; +import com.ware.swift.proto.InteractivePayload; + +/** + * @author pbting + * @date 2019-05-19 10:35 AM + */ +public interface IClusterSyncCallbackHoot extends ICallbackHook { + + /** + * 提供一个空实现。 + */ + class EmptyClusterSyncCallbackHook implements IClusterSyncCallbackHoot { + + @Override + public void callback(InteractivePayload.Builder target) { + + } + } +} diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/IDecentrationRemotingManager.java b/core-swift/src/main/java/com/ware/swift/core/remoting/IDecentralizeRemotingManager.java similarity index 63% rename from core-swift/src/main/java/com/ware/swift/core/remoting/IDecentrationRemotingManager.java rename to core-swift/src/main/java/com/ware/swift/core/remoting/IDecentralizeRemotingManager.java index a49982b..4af5d8e 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/IDecentrationRemotingManager.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/IDecentralizeRemotingManager.java @@ -1,16 +1,16 @@ package com.ware.swift.core.remoting; import com.ware.swift.core.remoting.channel.AbstractRemotingChannel; -import com.ware.swift.core.remoting.event.local.DecentrationInitRemotingChannelEventListener; - +import com.ware.swift.core.remoting.event.local.DecentralizeInitRemotingChannelEventListener; import com.ware.swift.proto.InteractivePayload; +import java.util.Collection; import java.util.Map; /** * */ -public interface IDecentrationRemotingManager { +public interface IDecentralizeRemotingManager { /** * @param value @@ -30,12 +30,41 @@ default void processClusterNodes(AbstractRemotingManager abstractRemotingManager // nothing to do } + default StringBuilder builderRemotingChannels(String source) { + + //nothing to do + return new StringBuilder(); + } + /** * */ - class DecentrationRemotingManager implements IDecentrationRemotingManager { + class DecentralizeRemotingManager implements IDecentralizeRemotingManager { + + public static final IDecentralizeRemotingManager DEFAULT_IMPL = new DecentralizeRemotingManager(); - public static final IDecentrationRemotingManager DEFAULT_IMPL = new DecentrationRemotingManager(); + @Override + public StringBuilder builderRemotingChannels(String source) { + Collection remotingChannels = RemotingManager + .getRemotingManager().getRemotingChannels(); + + final StringBuilder remotingChannelsSB = new StringBuilder(); + remotingChannels.forEach(remotingChannel -> { + if (remotingChannel.identify().equalsIgnoreCase(source)) { + return; + } + + remotingChannelsSB.append(remotingChannel.identify()); + remotingChannelsSB.append("|");// 竖线分隔多个节点。 + }); + + // 加上本节点。 + remotingChannelsSB.append(RemotingManager.getRemotingManager() + .getChannelIdentify(RemotingManager.getRemotingManager().getRaftConfig() + .getNodeInformation().identify())); + + return remotingChannelsSB; + } /** * @param value @@ -57,7 +86,6 @@ public void producer(InteractivePayload value, Map nodesHeartb */ @Override public void processClusterNodes(AbstractRemotingManager abstractRemotingManager, String nodes) { - System.err.println("去中心化在 node meet 之后收到的 nodes: " + nodes); String[] ptpNodesArry = nodes.split("[|]"); for (String ptpNode : ptpNodesArry) { // role@ip:port@cluster_name @@ -66,7 +94,7 @@ public void processClusterNodes(AbstractRemotingManager abstractRemotingManager, continue; } - if (!RemotingInteractiveConstants.PTP_ROLE_PRIFIX + if (!RemotingInteractiveConstants.PTP_ROLE_PREFIX .equalsIgnoreCase(infos[0])) { continue; } @@ -78,15 +106,13 @@ public void processClusterNodes(AbstractRemotingManager abstractRemotingManager, } if (abstractRemotingManager.isContainsRemotingChannel(ptpNode)) { - System.err.println("去中心话 已经包含该节点:" + ptpNode); continue; } // 不包含,则建立起连接 AbstractRemotingChannel abstractRemotingChannel = abstractRemotingManager.getRemotingChannelFactory() .newRemotingChannel(infos[1], infos[2]); - System.err.println("---->去中心化感知到节点:" + ptpNode); if (abstractRemotingManager.addRemotingChannel(abstractRemotingChannel)) { - new DecentrationInitRemotingChannelEventListener(abstractRemotingManager) + new DecentralizeInitRemotingChannelEventListener(abstractRemotingManager) .onAfterNewRemotingChannel(abstractRemotingChannel); } } diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/IInteractive.java b/core-swift/src/main/java/com/ware/swift/core/remoting/IInteractive.java index bc78777..e1e1830 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/IInteractive.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/IInteractive.java @@ -12,5 +12,5 @@ default InteractivePayload getInteractivePayload() { return null; } - boolean sendPayload(InteractivePayload raftInteractivePayload); + boolean sendPayload(InteractivePayload wareSwiftInteractivePayload); } diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/ILeaderFollowerRemotingManager.java b/core-swift/src/main/java/com/ware/swift/core/remoting/ILeaderFollowerRemotingManager.java index d0c56af..e44a952 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/ILeaderFollowerRemotingManager.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/ILeaderFollowerRemotingManager.java @@ -29,8 +29,8 @@ class LeaderFollowerRemotingManager implements ILeaderFollowerRemotingManager { @Override public void processClusterNodes(AbstractRemotingManager abstractRemotingManager, String nodes) { - String[] identifys = nodes.split("[|]"); - List identifyList = Arrays.asList(identifys); + String[] identifies = nodes.split("[|]"); + List identifyList = Arrays.asList(identifies); for (Iterator iterator = identifyList.iterator(); iterator.hasNext(); ) { String identify = iterator.next(); diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/IRemotingManager.java b/core-swift/src/main/java/com/ware/swift/core/remoting/IRemotingManager.java index d7a56a1..a3a0951 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/IRemotingManager.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/IRemotingManager.java @@ -28,15 +28,15 @@ public interface IRemotingManager extends IMailbox { void increSendIdentifyCount(); /** - * @param raftConfig + * @param wareSwiftConfig * @return */ - boolean init(WareCoreSwiftConfig raftConfig); + boolean init(WareCoreSwiftConfig wareSwiftConfig); /** * 开始节点选举 */ - void startLeaderElection(WareCoreSwiftGlobalContext raftGlobaleContext); + void startLeaderElection(WareCoreSwiftGlobalContext wareSwiftGlobaleContext); /** * @return @@ -134,7 +134,7 @@ public interface IRemotingManager extends IMailbox { * * @param remotingChannel */ - Collection getCommittedRemotingDomains( + Collection getCommittedRemotingDomains( Collection committedIds, AbstractRemotingChannel remotingChannel); /** @@ -158,7 +158,7 @@ Collection getCommittedRemotingDomains( * @param term 当前收到 leader 发送过来的 term。需要考虑网络分区后的重建,处理落后的 term 同步数据 * @return */ - Collection committedSyncingDomains(long term); + Collection committedSyncingDomains(long term); /** * @return @@ -171,9 +171,9 @@ Collection getCommittedRemotingDomains( void isOnlineWithRemotingChannel(String source); /** - * @param raftConfig + * @param wareSwiftConfig */ - void setRaftConfig(WareCoreSwiftConfig raftConfig); + void setRaftConfig(WareCoreSwiftConfig wareSwiftConfig); /** * @return diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/OutboundCallback.java b/core-swift/src/main/java/com/ware/swift/core/remoting/OutboundCallback.java index e543a94..d6a63cb 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/OutboundCallback.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/OutboundCallback.java @@ -6,59 +6,62 @@ /** * 数据在集群节点间同步后的回调。提供以下几种基本的语义: - * + *

* 1. 成功同步后的回调。同时会给出成功同步后的节点数,以及同步的具体节点信息 - * + *

* 2. 同步失败。 - * + *

* 2.1 存活的连接数小于 * {@link NodeInformation#getRequestRequiredAcks(int)},此时也会给业务方一个回调处理的结果 - * + *

* 2.2 * 开始同步时发现存活的连接已经达到{@link NodeInformation#REQUEST_REQUIRED_ACKS}, - * + *

* 但是同步过程中,发现部分节点失败,导致最后部分节点同步成功。 */ public interface OutboundCallback { - /** - * @param channelIdentifys 每个 remoting channel 的 identify - */ - default void onSyncingSuccess(Set channelIdentifys) { - // nothing to do - } + /** + * @param channelIdentifies 每个 remoting channel 的 identify + */ + default void onSyncingSuccess(Set channelIdentifies) { + // nothing to do + } - /** - * 注意: 发生此方法的回调已经进行了数据的同步。 - * @param successChannelIdentifys 同步成功的 remoting channel - * @param failedChannelIdentifys 同步失败的 remoting channel - */ - default void onSyncingFailed(Set successChannelIdentifys, - Set failedChannelIdentifys) { - /** - * nothing to do - */ - } + /** + * 注意: 发生此方法的回调已经进行了数据的同步。 + * + * @param successChannelIdentifies 同步成功的 remoting channel + * @param failedChannelIdentifies 同步失败的 remoting channel + */ + default void onSyncingFailed(Set successChannelIdentifies, + Set failedChannelIdentifies) { + /** + * nothing to do + */ + } - /** - * 注意: 当发生此方法的回调时,还没进行数据的同步 - * @param requestRequiredAcks 请求需要的 ACK 数 - * @param realActiveChannels 实际存活的连接数 - */ - default void onMinRequestRequiredAcks(int requestRequiredAcks, - int realActiveChannels) { - /** - * nothing to do - */ - } + /** + * 注意: 当发生此方法的回调时,还没进行数据的同步 + * + * @param requestRequiredAcks 请求需要的 ACK 数 + * @param realActiveChannels 实际存活的连接数 + */ + default void onMinRequestRequiredAcks(int requestRequiredAcks, + int realActiveChannels) { + /** + * nothing to do + */ + } - /** - * 当收到的请求发送当前节点感知到 leader 的 term 和实际的 leader 的 term 低时,应该通知客户端更新最新的 leader 信息。 - * @param nodeIdentify - */ - default void onMoved(String nodeIdentify) { - /** - * nothing to do - */ - } + /** + * 当收到的请求发送当前节点感知到 leader 的 term 和实际的 leader 的 term 低时,应该通知客户端更新最新的 leader 信息。 + * + * @param nodeIdentify + */ + default void onMoved(String nodeIdentify) { + /** + * nothing to do + */ + } } diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/RemotingDomain.java b/core-swift/src/main/java/com/ware/swift/core/remoting/RemotingDomain.java deleted file mode 100644 index cfa4d4d..0000000 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/RemotingDomain.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.ware.swift.core.remoting; - -import java.io.Serializable; - -/** - * 用于两个进程间进行通信的 object - */ -public interface RemotingDomain extends Serializable { - -} diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/RemotingDomainManager.java b/core-swift/src/main/java/com/ware/swift/core/remoting/RemotingDomainManager.java new file mode 100644 index 0000000..764214a --- /dev/null +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/RemotingDomainManager.java @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2019 the original author or authors. + * + * 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 com.ware.swift.core.remoting; + +/** + * @author pbting + * @date 2019-05-19 10:01 PM + */ +public final class RemotingDomainManager { + + /** + * 统一提供数据操作的四种{C/R/U/D}可能性。其目的是当有数据变更时向客户端推送时,可以得知当前数据变更的行为来实现不同的业务逻辑。 + *

+ * 例如。基于热点数据发现的数据中间件,当某一个热点 topic 的热点数据有新增或者删除或者被修改时,这个时候可以给客户端推送当前操作 + * 的数据项,其中数据项里面就包含四种操作类型中的某一种。 + *

+ */ + + + /** + * 提供具有数据新增语义的 operator + */ + public static final byte DOMAIN_OPERATOR_ADD = 1 << 1; + + /** + * 提供具有数据删除语义的 operator + */ + public static final byte DOMAIN_OPERATOR_DELETE = 1 << 2; + + /** + * 提供具有数据变更语义的 operator + */ + public static final byte DOMAIN_OPERATOR_UPDATE = 1 << 3; + + /** + * 提供具有数据获取或者查询语义的 operator + */ + public static final byte DOMAIN_OPERATOR_RETRIEVE = 1 << 4; + + /** + * @param remotingDomainSupport + * @return + */ + public static boolean isAddOperator(RemotingDomainSupport remotingDomainSupport) { + + return (remotingDomainSupport.getDomainOperator() & DOMAIN_OPERATOR_ADD) != 0; + } + + /** + * @param remotingDomainSupport + * @return + */ + public static boolean isDeleteOperator(RemotingDomainSupport remotingDomainSupport) { + + return (remotingDomainSupport.getDomainOperator() & DOMAIN_OPERATOR_DELETE) != 0; + } + + /** + * @param remotingDomainSupport + * @return + */ + public static boolean isUpdateOperator(RemotingDomainSupport remotingDomainSupport) { + + return (remotingDomainSupport.getDomainOperator() & DOMAIN_OPERATOR_UPDATE) != 0; + } + + /** + * @param remotingDomainSupport + * @return + */ + public static boolean isRetrieveOperator(RemotingDomainSupport remotingDomainSupport) { + + return (remotingDomainSupport.getDomainOperator() & DOMAIN_OPERATOR_RETRIEVE) != 0; + } + +} \ No newline at end of file diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/RemotingDomainSupport.java b/core-swift/src/main/java/com/ware/swift/core/remoting/RemotingDomainSupport.java new file mode 100644 index 0000000..ce6ef00 --- /dev/null +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/RemotingDomainSupport.java @@ -0,0 +1,30 @@ +package com.ware.swift.core.remoting; + +import java.io.Serializable; + +/** + * 用于两个进程间进行通信的 object + */ +public abstract class RemotingDomainSupport implements Serializable { + + /** + * the default operator + */ + public byte domainOperator = RemotingDomainManager.DOMAIN_OPERATOR_ADD; + + public RemotingDomainSupport() { + + } + + public RemotingDomainSupport(byte domainOperator) { + this.domainOperator = domainOperator; + } + + public byte getDomainOperator() { + return domainOperator; + } + + public void setDomainOperator(byte domainOperator) { + this.domainOperator = domainOperator; + } +} diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/RemotingDomainWrapper.java b/core-swift/src/main/java/com/ware/swift/core/remoting/RemotingDomainWrapper.java index 217d4de..2c28074 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/RemotingDomainWrapper.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/RemotingDomainWrapper.java @@ -26,7 +26,7 @@ public class RemotingDomainWrapper { /** * 当前正在同步的数据 */ - private RemotingDomain remotingDomain; + private RemotingDomainSupport remotingDomain; /** * 用来记录当前这个实例已经同步成功的 remoting channel. @@ -48,8 +48,8 @@ public class RemotingDomainWrapper { */ private AtomicBoolean isReSyncCommitted = new AtomicBoolean(false); - public RemotingDomainWrapper(RemotingDomain remotingDomain, int requestRequireAcks, - String syncingId, long syncingTerm) { + public RemotingDomainWrapper(RemotingDomainSupport remotingDomain, int requestRequireAcks, + String syncingId, long syncingTerm) { this.remotingDomain = remotingDomain; this.requestRequireAcks = requestRequireAcks; this.syncingId = syncingId; @@ -62,11 +62,11 @@ public boolean committed() { return hasCommitted.compareAndSet(false, true); } - public RemotingDomain getRemotingDomain() { + public RemotingDomainSupport getRemotingDomain() { return remotingDomain; } - public void setRemotingDomain(RemotingDomain remotingDomain) { + public void setRemotingDomain(RemotingDomainSupport remotingDomain) { this.remotingDomain = remotingDomain; } diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/RemotingInteractiveConstants.java b/core-swift/src/main/java/com/ware/swift/core/remoting/RemotingInteractiveConstants.java index 1b1d40e..d390c25 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/RemotingInteractiveConstants.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/RemotingInteractiveConstants.java @@ -4,35 +4,24 @@ public interface RemotingInteractiveConstants { - ObjectEncodingHandler OBJECT_ENCODING_HANDLER = new ObjectEncodingHandlerProtobufImpl(); + ObjectEncodingHandler OBJECT_ENCODING_HANDLER = new ObjectEncodingHandlerProtobufImpl(); - Integer LEADER_ELECTION_VOTE_ONE = 0x1002; + Integer LEADER_ELECTION_VOTE_ONE = 0x1002; - /** - * Leader 检测 Follower 节点 ,心跳发送失败多少次,即可判断其主观下线的阈值。 - */ - Integer LEADER_CHECK_FOLLOWER_SDOWN = 3; + String RAFT_ROLE_PREFIX = "RAFT"; - String RAFT_ROLE_PRIFIX = "RAFT"; + /** + * peer-to-peer + */ + String PTP_ROLE_PREFIX = "PTP"; - /** - * peer-to-peer - */ - String PTP_ROLE_PRIFIX = "PTP"; + String RECEIVE_LEADER_ACK = "L1"; - String RECEIVE_LEADER_ACK = "L1"; + String HEARTBEAT_TIMEOUT_COUNT = "L3"; - String HEARTBEAT_ACK = "L2"; + String HEARTBEAT_CHECK_COUNT = "L4"; - String HEARTBEAT_RESYNCING = "resyncing"; + String PONG = "pong"; - String HEARTBEAT_TIMEOUT_COUNT = "L3"; - - String HEARTBEAT_CHECK_COUNT = "L4"; - - String LEADER_NULL = "Null"; - - String PONG = "pong"; - - String ROLE_MISMATCH = "L5"; + String ROLE_MISMATCH = "L5"; } diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/RemotingManager.java b/core-swift/src/main/java/com/ware/swift/core/remoting/RemotingManager.java index 86f6dfc..18b0a57 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/RemotingManager.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/RemotingManager.java @@ -6,9 +6,9 @@ import com.ware.swift.core.WareCoreSwiftPluginLoader; import com.ware.swift.core.remoting.channel.AbstractRemotingChannel; import com.ware.swift.core.remoting.channel.IRemotingChannelFactory; - import com.ware.swift.event.loop.AbstractAsyncEventLoopGroup; import com.ware.swift.proto.InteractivePayload; + import java.util.Collection; import java.util.Set; import java.util.concurrent.TimeUnit; @@ -20,13 +20,11 @@ public class RemotingManager implements IRemotingManager { private static final String LEADER_FOLLOWER_RSOCKET_REMOTING_MANAGER = - "java.com.ware.rsocket.swift.LeaderFollowerRSocketRemotingManager"; - - private static final String LEADER_FOLLOWER_GRPC_REMOTING_MANAGER = "com.ware.swift.grpc.swift.LeaderFollowerGrpcRemotingManager"; + "com.ware.swift.rsocket.LeaderFollowerRSocketRemotingManager"; private static final int LEADER_FOLLOWER_REMOTING_MANAGER_TYPE = 1 << 1; - private static final int DECENTRATION_REMOTING_MANAGER_TYPE = 1 << 2; + private static final int DECENTRALIZE_REMOTING_MANAGER_TYPE = 1 << 2; public static int REMOTING_MANAGER_TYPE = 1; @@ -50,8 +48,8 @@ public class RemotingManager implements IRemotingManager { REMOTING_MANAGER_TYPE |= LEADER_FOLLOWER_REMOTING_MANAGER_TYPE; } - if (remotingManagerWrapper instanceof IDecentrationRemotingManager) { - REMOTING_MANAGER_TYPE |= DECENTRATION_REMOTING_MANAGER_TYPE; + if (remotingManagerWrapper instanceof IDecentralizeRemotingManager) { + REMOTING_MANAGER_TYPE |= DECENTRALIZE_REMOTING_MANAGER_TYPE; } } catch (Exception e) { e.printStackTrace(); @@ -66,7 +64,7 @@ public static boolean isLeaderFollowerRemotingManager() { public static boolean isDecenterationRemotingManager() { return (REMOTING_MANAGER_TYPE - & DECENTRATION_REMOTING_MANAGER_TYPE) == DECENTRATION_REMOTING_MANAGER_TYPE; + & DECENTRALIZE_REMOTING_MANAGER_TYPE) == DECENTRALIZE_REMOTING_MANAGER_TYPE; } public static IRemotingManager getRemotingManager() { @@ -74,13 +72,13 @@ public static IRemotingManager getRemotingManager() { } @Override - public boolean init(WareCoreSwiftConfig raftConfig) { - return remotingManagerWrapper.init(raftConfig); + public boolean init(WareCoreSwiftConfig wareSwiftConfig) { + return remotingManagerWrapper.init(wareSwiftConfig); } @Override - public void startLeaderElection(WareCoreSwiftGlobalContext raftGlobaleContext) { - remotingManagerWrapper.startLeaderElection(raftGlobaleContext); + public void startLeaderElection(WareCoreSwiftGlobalContext wareSwiftGlobaleContext) { + remotingManagerWrapper.startLeaderElection(wareSwiftGlobaleContext); } @Override @@ -155,8 +153,8 @@ public void isOnlineWithRemotingChannel(String source) { } @Override - public void setRaftConfig(WareCoreSwiftConfig raftConfig) { - remotingManagerWrapper.setRaftConfig(raftConfig); + public void setRaftConfig(WareCoreSwiftConfig wareSwiftConfig) { + remotingManagerWrapper.setRaftConfig(wareSwiftConfig); } @Override @@ -228,7 +226,7 @@ public int addSyncingRemotingDoamin(RemotingDomainWrapper remotingDoaminWrapper) } @Override - public Collection getCommittedRemotingDomains( + public Collection getCommittedRemotingDomains( Collection committedIds, AbstractRemotingChannel remotingChannel) { return remotingManagerWrapper.getCommittedRemotingDomains(committedIds, remotingChannel); @@ -253,7 +251,7 @@ public void clearSyncingRemotingDomainIds() { } @Override - public Collection committedSyncingDomains(long term) { + public Collection committedSyncingDomains(long term) { return remotingManagerWrapper.committedSyncingDomains(term); } diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/avalispart/AbstractAvailableCapabilityModel.java b/core-swift/src/main/java/com/ware/swift/core/remoting/avalispart/AbstractAvailableCapabilityModel.java index ea68c24..fef6443 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/avalispart/AbstractAvailableCapabilityModel.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/avalispart/AbstractAvailableCapabilityModel.java @@ -2,9 +2,8 @@ import com.ware.swift.core.WareCoreSwiftExceptionCode; import com.ware.swift.core.remoting.OutboundCallback; -import com.ware.swift.core.remoting.RemotingDomain; +import com.ware.swift.core.remoting.RemotingDomainSupport; import com.ware.swift.core.remoting.RemotingManager; -import com.ware.swift.core.remoting.ClusterDataSyncManager; import com.ware.swift.proto.InteractivePayload; import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLoggerFactory; @@ -38,13 +37,13 @@ public abstract class AbstractAvailableCapabilityModel * @return */ @Override - public void onOutboundDataSet(final RemotingDomain remotingDomain, + public void onOutboundDataSet(final RemotingDomainSupport remotingDomain, OutboundCallback callback) { RemotingManager.getRemotingManager().getRaftConfig().getNodeInformation() .incrementCommittedCount(); - final InteractivePayload interactivePayload = ClusterDataSyncManager - .newSyncInteractivePayload(remotingDomain); + final InteractivePayload interactivePayload = buildSyncInteractivePayload(remotingDomain); final Set syncingSuccessChannels = new TreeSet<>(); + syncingSuccessChannels.add(RemotingManager.getRemotingManager().getChannelIdentify(RemotingManager.getRemotingManager().getRaftConfig().getNodeInformation().identify())); final Set syncingFailedChannels = new TreeSet<>(); RemotingManager.getRemotingManager().getRemotingChannels() .forEach(abstractRemotingChannel -> { @@ -60,7 +59,7 @@ public void onOutboundDataSet(final RemotingDomain remotingDomain, WareCoreSwiftExceptionCode.REMOTING_OUTBOUND_DATASET_ERROR_CODE, e.getMessage())); abstractRemotingChannel - .addSyncingRemotingDoamin(interactivePayload); + .addSyncingRemotingDomain(interactivePayload); syncingFailedChannels.add(abstractRemotingChannel.identify()); } }); @@ -69,5 +68,16 @@ public void onOutboundDataSet(final RemotingDomain remotingDomain, } else { callback.onSyncingFailed(syncingSuccessChannels, syncingFailedChannels); } + onAfterOutboundDataSet(remotingDomain); + } + + /** + * 在 集群间同步完之后,做一些事情。 + * + * @param remotingDomain + */ + public void onAfterOutboundDataSet(final RemotingDomainSupport remotingDomain) { + + //nothing to do } } diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/channel/AbstractRemotingChannel.java b/core-swift/src/main/java/com/ware/swift/core/remoting/channel/AbstractRemotingChannel.java index d758d7a..da721b6 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/channel/AbstractRemotingChannel.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/channel/AbstractRemotingChannel.java @@ -1,12 +1,12 @@ package com.ware.swift.core.remoting.channel; -import io.netty.util.internal.logging.InternalLogger; -import io.netty.util.internal.logging.InternalLoggerFactory; - import com.ware.swift.core.Information; import com.ware.swift.core.remoting.ClusterDataSyncManager; import com.ware.swift.core.remoting.RemotingManager; import com.ware.swift.proto.InteractivePayload; +import io.netty.util.internal.logging.InternalLogger; +import io.netty.util.internal.logging.InternalLoggerFactory; + import java.util.Collection; import java.util.LinkedList; import java.util.concurrent.ConcurrentHashMap; @@ -132,7 +132,7 @@ public void setCommittedCount(long committedCount) { this.committedCount = committedCount; } - public int addSyncingRemotingDoamin(final InteractivePayload interactivePayload) { + public int addSyncingRemotingDomain(final InteractivePayload interactivePayload) { final String syncingId = interactivePayload.getHeadersMap() .get(ClusterDataSyncManager.HEADER_KEY_SYNCING_REMOTING_DOMAIN_ID); diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/conspart/AbstractConsistenceCapabilityModel.java b/core-swift/src/main/java/com/ware/swift/core/remoting/conspart/AbstractConsistenceCapabilityModel.java index 31652ec..8687749 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/conspart/AbstractConsistenceCapabilityModel.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/conspart/AbstractConsistenceCapabilityModel.java @@ -13,7 +13,7 @@ import java.util.TreeSet; /** - * + * 提供 CP 能力模型的抽象。如果你的系统需要实现一个 CP 架构的能力,那么实现该基本抽象类是一个不错的选择。 */ public abstract class AbstractConsistenceCapabilityModel implements IConsistenceCapabilityModel { @@ -29,7 +29,7 @@ public abstract class AbstractConsistenceCapabilityModel * @throws RemotingInteractiveException */ @Override - public void onInboundDataSet(final RemotingDomain remotingDomain, + public void onInboundDataSet(final RemotingDomainSupport remotingDomain, Map headsMap) { // 添加到正在同步 payload 的消息列表中。 String remotingDomainId = headsMap @@ -40,9 +40,8 @@ public void onInboundDataSet(final RemotingDomain remotingDomain, remotingDomain, ClusterDataSyncManager.buildRequireAcksAndGetActiveChannel()[0], remotingDomainId, term); - int size = RemotingManager.getRemotingManager() + RemotingManager.getRemotingManager() .addSyncingRemotingDoamin(remotingDomainWrapper); - System.err.println("\t in syncing data size is=" + size); } /** @@ -54,7 +53,7 @@ public void onInboundDataSet(final RemotingDomain remotingDomain, * @param outboundCallback */ @Override - public void onOutboundDataSet(final RemotingDomain remotingDomain, + public void onOutboundDataSet(final RemotingDomainSupport remotingDomain, OutboundCallback outboundCallback) { RemotingManager remotingManager = (RemotingManager) RemotingManager .getRemotingManager(); @@ -80,8 +79,7 @@ public void onOutboundDataSet(final RemotingDomain remotingDomain, return; } - final InteractivePayload interactivePayload = ClusterDataSyncManager - .newSyncInteractivePayload(remotingDomain); + final InteractivePayload interactivePayload = buildSyncInteractivePayload(remotingDomain); final Set syncingSuccessChannels = new TreeSet<>(); final Set syncingFailedChannels = new TreeSet<>(); @@ -116,7 +114,7 @@ public void onOutboundDataSet(final RemotingDomain remotingDomain, return; } - // 先写入本地内存。raft 是写入本地磁盘。个人觉得这里可能根据具体的场景来区别是暂写内存还是磁盘。 + // 先写入本地内存。wareSwift 是写入本地磁盘。个人觉得这里可能根据具体的场景来区别是暂写内存还是磁盘。 String currentDataSyncingId = interactivePayload.getHeadersMap() .get(ClusterDataSyncManager.HEADER_KEY_SYNCING_REMOTING_DOMAIN_ID); long term = Long.valueOf(interactivePayload.getHeadersMap() diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/conspart/IConsistenceCapabilityModel.java b/core-swift/src/main/java/com/ware/swift/core/remoting/conspart/IConsistenceCapabilityModel.java index 6af9335..ad46ebd 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/conspart/IConsistenceCapabilityModel.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/conspart/IConsistenceCapabilityModel.java @@ -1,22 +1,23 @@ package com.ware.swift.core.remoting.conspart; import com.ware.swift.core.remoting.ICapabilityModel; -import com.ware.swift.core.remoting.RemotingDomain; +import com.ware.swift.core.remoting.RemotingDomainSupport; import java.util.Collection; /** - * 具有一致性协议的能力模型 + * 具有一致性协议的能力模型。 */ public interface IConsistenceCapabilityModel extends ICapabilityModel { - /** - * 对于 Leader-Follower 架构的系统。 Leader 已经确认所有的 Follower 已经成功接收 committed 的信息。 - * - *

- * 这个时候应该给业务层一个回调,来处理这个已经到达一致性的数据。 - *

- * @param committedRemotingDomains 已经提交成功的一组 remoting domains 集合。 - */ - void onCommitted(Collection committedRemotingDomains); + /** + * 对于 Leader-Follower 架构的系统。 Leader 已经确认所有的 Follower 已经成功接收 committed 的信息。 + * + *

+ * 这个时候应该给业务层一个回调,来处理这个已经到达一致性的数据。 + *

+ * + * @param committedRemotingDomains 已经提交成功的一组 remoting domains 集合。 + */ + void onCommitted(Collection committedRemotingDomains); } diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/conspart/ServiceInstance.java b/core-swift/src/main/java/com/ware/swift/core/remoting/conspart/ServiceInstance.java deleted file mode 100644 index 59df61e..0000000 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/conspart/ServiceInstance.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.ware.swift.core.remoting.conspart; - -import com.ware.swift.core.remoting.RemotingDomain; - -/** - * - */ -public class ServiceInstance implements RemotingDomain { - - private String serviceId; - private String host; - private int port; - private boolean secure; - private String scheme; - private long version; - - public String getServiceId() { - return serviceId; - } - - public void setServiceId(String serviceId) { - this.serviceId = serviceId; - } - - public String getHost() { - return host; - } - - public void setHost(String host) { - this.host = host; - } - - public int getPort() { - return port; - } - - public void setPort(int port) { - this.port = port; - } - - public boolean isSecure() { - return secure; - } - - public void setSecure(boolean secure) { - this.secure = secure; - } - - public String getScheme() { - return scheme; - } - - public void setScheme(String scheme) { - this.scheme = scheme; - } - - public long getVersion() { - return version; - } - - public void setVersion(long version) { - this.version = version; - } - - @Override - public String toString() { - return "ServiceInstance{" + "serviceId='" + serviceId + '\'' + ", host='" + host - + '\'' + ", port=" + port + ", secure=" + secure + ", scheme='" + scheme - + '\'' + ", version=" + version + '}'; - } -} diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/BroadcastLeaderEventListener.java b/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/BroadcastLeaderEventListener.java index 47342d9..a00af06 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/BroadcastLeaderEventListener.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/BroadcastLeaderEventListener.java @@ -28,7 +28,7 @@ public BroadcastLeaderEventListener(IRemotingManager remotingManager) { public boolean onEvent(ObjectEvent event, int listenerIndex) { AbstractRemotingChannelModel remotingChannel = event.getValue(); - System.err.println("-------------->" + remotingChannel.identify() + " " + logger.info("-------------->" + remotingChannel.identify() + " " + this.getClass().getName()); try { diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/BroadcastNewNodeEventListener.java b/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/BroadcastNewNodeEventListener.java index 81fb02a..003370b 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/BroadcastNewNodeEventListener.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/BroadcastNewNodeEventListener.java @@ -8,6 +8,8 @@ import com.ware.swift.event.ObjectEvent; import com.ware.swift.proto.InteractivePayload; import com.google.protobuf.ByteString; +import io.netty.util.internal.logging.InternalLogger; +import io.netty.util.internal.logging.InternalLoggerFactory; /** * 不管是 去中心化还是 Leader-Follower ,当一个新的节点加入到集群中来的时候,每个节点都有能力感知到新加入的节点。 @@ -21,6 +23,7 @@ public class BroadcastNewNodeEventListener extends AbstractLocalPipelineEventListener { + private static final InternalLogger logger = InternalLoggerFactory.getInstance(BroadcastNewNodeEventListener.class); public BroadcastNewNodeEventListener(IRemotingManager remotingManager) { super(remotingManager); } @@ -42,13 +45,13 @@ public boolean onEvent(ObjectEvent event, int listenerIndex) { try { InteractivePayload response = remotingChannel .requestResponse(builder.build()); - System.err.println("response broadcast new node:" + logger.info("response broadcast new node:" + response.getPayload().toStringUtf8()); } catch (Exception e) { if (remotingChannel.isNetUnavailable(e)) { // 等待下一秒发送,直到 channel 可用为止。 - System.err.println(remotingChannel.identify() - + " net unavailable for ...." + this.getClass().getName()); + logger.error(remotingChannel.identify() + + " net unavailable for ...." + this.getClass().getName(),e); } return false; } diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/BroadcastSdownLeaderEventListener.java b/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/BroadcastSdownLeaderEventListener.java index 184266c..e880bd1 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/BroadcastSdownLeaderEventListener.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/BroadcastSdownLeaderEventListener.java @@ -21,7 +21,6 @@ public BroadcastSdownLeaderEventListener(IRemotingManager remotingManager) { @Override public boolean onEvent(ObjectEvent event, int listenerIndex) { final NodeInformation sdownLeader = event.getValue(); - System.err.println("广播....sdown." + sdownLeader.identify()); remotingManager.getRemotingChannels().forEach(remotingChannel -> { if (remotingManager.getChannelIdentify(sdownLeader.identify()) .equalsIgnoreCase(remotingChannel.identify())) { diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/BroadcastVoteCountEventListener.java b/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/BroadcastVoteCountEventListener.java index 3a7c5f0..8c687d5 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/BroadcastVoteCountEventListener.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/BroadcastVoteCountEventListener.java @@ -29,10 +29,10 @@ public boolean onEvent(ObjectEvent event, int listen if (remotingManager.getActiveChannelCount() == 0 || remotingManager .getRaftConfig().getNodeInformation() .getVoteCount() > (remotingManager.getActiveChannelCount()) / 2 + 1) { - event.setInterruptor(true); - WareCoreSwiftConfig raftConfig = remotingManager.getRaftConfig(); - raftConfig.getNodeInformation().increTerm(); - raftConfig.setLeader(remotingManager.getRaftConfig().getNodeInformation()); + event.setInterrupt(true); + WareCoreSwiftConfig wareSwiftConfig = remotingManager.getRaftConfig(); + wareSwiftConfig.getNodeInformation().increTerm(); + wareSwiftConfig.setLeader(remotingManager.getRaftConfig().getNodeInformation()); logger.debug(remotingManager.getRaftConfig().getNodeInformation().identify() + " has become leader,so will broadcast the leader information to other nodes."); diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/CheckIsStartupLeaderElectionEventListener.java b/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/CheckIsStartupLeaderElectionEventListener.java index c7e19c6..d8120e9 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/CheckIsStartupLeaderElectionEventListener.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/CheckIsStartupLeaderElectionEventListener.java @@ -42,7 +42,7 @@ public CheckIsStartupLeaderElectionEventListener(boolean isWaitAllReady, @Override public boolean onEvent(ObjectEvent event, int listenerIndex) { // 中断后面的 listener 执行。已经是最后一个 listener 执行了,因此此时返回的 true/false 会生效。 - event.setInterruptor(true); + event.setInterrupt(true); if (remotingManager.getRaftConfig() == null) { // wait next event loop return false; diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/DecentrationInitRemotingChannelEventListener.java b/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/DecentralizeInitRemotingChannelEventListener.java similarity index 92% rename from core-swift/src/main/java/com/ware/swift/core/remoting/event/local/DecentrationInitRemotingChannelEventListener.java rename to core-swift/src/main/java/com/ware/swift/core/remoting/event/local/DecentralizeInitRemotingChannelEventListener.java index a4d8646..88fc091 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/DecentrationInitRemotingChannelEventListener.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/DecentralizeInitRemotingChannelEventListener.java @@ -11,10 +11,10 @@ /** * 去中心化架构下初始化完 remoting channel 后的一个操作 */ -public class DecentrationInitRemotingChannelEventListener +public class DecentralizeInitRemotingChannelEventListener extends InitRemotingChannelEventListener { - public DecentrationInitRemotingChannelEventListener( + public DecentralizeInitRemotingChannelEventListener( IRemotingManager remotingManager) { super(remotingManager); } @@ -34,7 +34,7 @@ public void onAfterNewRemotingChannel( int eventType = AbstractRemotingManager.getNextHeartbeatEventType(); // 1、初始化好心跳超时检测 remotingManager.getEventLoopGroup().addLast( - new DecentrationNodeTimeoutCheckEventListener(abstractRemotingChannel, + new DecentralizeNodeTimeoutCheckEventListener(abstractRemotingChannel, remotingManager), eventType); diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/DecentrationNodeMeetEventListener.java b/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/DecentralizeNodeMeetEventListener.java similarity index 94% rename from core-swift/src/main/java/com/ware/swift/core/remoting/event/local/DecentrationNodeMeetEventListener.java rename to core-swift/src/main/java/com/ware/swift/core/remoting/event/local/DecentralizeNodeMeetEventListener.java index 6cbdda7..ee6e080 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/DecentrationNodeMeetEventListener.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/DecentralizeNodeMeetEventListener.java @@ -14,10 +14,10 @@ *

* 发生此操作,一般是在对等的节点中还没有包含当前此节点,大多数情况下是在新加入一个节点时发生的行为。 */ -public class DecentrationNodeMeetEventListener +public class DecentralizeNodeMeetEventListener extends AbstractLocalPipelineEventListener { - public DecentrationNodeMeetEventListener(IRemotingManager remotingManager) { + public DecentralizeNodeMeetEventListener(IRemotingManager remotingManager) { super(remotingManager); } diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/DecentrationNodeTimeoutCheckEventListener.java b/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/DecentralizeNodeTimeoutCheckEventListener.java similarity index 94% rename from core-swift/src/main/java/com/ware/swift/core/remoting/event/local/DecentrationNodeTimeoutCheckEventListener.java rename to core-swift/src/main/java/com/ware/swift/core/remoting/event/local/DecentralizeNodeTimeoutCheckEventListener.java index b356105..ab10fcf 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/DecentrationNodeTimeoutCheckEventListener.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/DecentralizeNodeTimeoutCheckEventListener.java @@ -21,14 +21,14 @@ *

* 去中心化:需要一直感知到当前节点的 online/offline 状态。 */ -public class DecentrationNodeTimeoutCheckEventListener +public class DecentralizeNodeTimeoutCheckEventListener extends NodeTimeoutCheckEventListenerSupport> { private AbstractRemotingChannel remotingChannel; private final InternalLogger logger = InternalLoggerFactory - .getInstance(DecentrationNodeTimeoutCheckEventListener.class); + .getInstance(DecentralizeNodeTimeoutCheckEventListener.class); - public DecentrationNodeTimeoutCheckEventListener( + public DecentralizeNodeTimeoutCheckEventListener( AbstractRemotingChannel remotingChannel, IRemotingManager remotingManager) { super(remotingManager); this.remotingChannel = remotingChannel; @@ -85,7 +85,7 @@ public boolean onEvent(ObjectEvent> event, .setPayload(ByteString .copyFrom(selfIdentify.getBytes(CharsetUtil.UTF_8))) .build()); - System.err.println(String.format("double check node is contains %s:%s", + logger.info(String.format("double check node is contains %s:%s", selfIdentify, responsePayload.getPayload().toStringUtf8()) + "; channel identify :" + remotingChannel.identify()); // is active,说明当前 {@remoting channel} 指向的节点列表中没有当前这个节点,需要进行一次 leader meet diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/LeaderFollowerSendSayHelloEventListener.java b/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/LeaderFollowerSendSayHelloEventListener.java index f63a63b..970a975 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/LeaderFollowerSendSayHelloEventListener.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/LeaderFollowerSendSayHelloEventListener.java @@ -80,7 +80,7 @@ private void processResponseLeader(AbstractRemotingChannel remotingChannel, String leaderIdentify = RemotingManager.getRemotingManager() .getChannelIdentify(responseLeader.identify()); if (!remotingManager.isContainsRemotingChannel(leaderIdentify)) { - System.err.println("初始化和 leader 建立连接:" + RemotingManager.getRemotingManager() + log.info("初始化和 leader 建立连接:" + RemotingManager.getRemotingManager() .getChannelIdentify(responseLeader.identify())); AbstractRemotingChannel leaderRemotingChannel = RemotingManager .getRemotingManager().getRemotingChannelFactory() diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/LeaderHeartbeatTimeoutCheckEventListener.java b/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/LeaderHeartbeatTimeoutCheckEventListener.java index b893412..4ab2760 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/LeaderHeartbeatTimeoutCheckEventListener.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/LeaderHeartbeatTimeoutCheckEventListener.java @@ -5,7 +5,6 @@ import com.ware.swift.core.remoting.channel.AbstractRemotingChannel; import com.ware.swift.core.remoting.event.remoting.RemotingEventDispatcher; import com.ware.swift.event.ObjectEvent; -import com.ware.swift.event.loop.EventLoopConstants; import com.ware.swift.proto.InteractivePayload; import io.netty.util.CharsetUtil; import io.netty.util.internal.logging.InternalLogger; @@ -32,9 +31,6 @@ public LeaderHeartbeatTimeoutCheckEventListener(IRemotingManager remotingManager public boolean onEvent(ObjectEvent> event, int listenerIndex) { - event.setParameter(EventLoopConstants.EVENT_LOOP_INTERVAL_PARAM, - TimeUnit.SECONDS.toMillis(1)); - Long checkCount = event .getParameter(RemotingInteractiveConstants.HEARTBEAT_CHECK_COUNT); @@ -51,9 +47,12 @@ public boolean onEvent(ObjectEvent> event, InteractivePayload heartbeatPayload = mailbox.consumer(10, TimeUnit.MILLISECONDS); if (heartbeatPayload != null) { logger.debug(String.format( - "cost time [%s] ms to wait leader send information with heartbeat.", - (System.currentTimeMillis() - s))); + "cost time [%s] ms to wait leader[%s] send information with heartbeat.", + (System.currentTimeMillis() - s), heartbeatPayload.getSource())); processHeartbeatPayload(heartbeatPayload); + logger.info(String.format( + "cost time [%s] ms to wait leader[%s] send information with heartbeat.", + (System.currentTimeMillis() - s), heartbeatPayload.getSource())); return false; } // case 1:处理一次选举没有选举成功的情况 @@ -93,7 +92,7 @@ public boolean onEvent(ObjectEvent> event, .setPayload(ByteString .copyFrom(selfIdentify.getBytes(CharsetUtil.UTF_8))) .build()); - System.err.println(String.format("double check leader is contains %s:%s", + logger.info(String.format("double check leader is contains %s:%s", selfIdentify, responsePayload.getPayload().toStringUtf8())); // case 1: 新加入的节点: // is active,说明 leader 列表中没有当前这个节点,需要进行一次 leader meet diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/LeaderNodeMeetEventListener.java b/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/LeaderNodeMeetEventListener.java index fe09906..67c89f9 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/LeaderNodeMeetEventListener.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/LeaderNodeMeetEventListener.java @@ -50,7 +50,6 @@ public boolean onEvent(ObjectEvent event, .requestResponse(builder.build()); String nodes = interactivePayload.getPayload().toStringUtf8(); - System.err.println("receive follower nodes from leader:" + nodes); remotingManager.processClusterNodes(nodes); } catch (Exception e) { e.printStackTrace(); diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/NodeTimeoutCheckEventListenerSupport.java b/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/NodeTimeoutCheckEventListenerSupport.java index 2da994c..c7c40e9 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/NodeTimeoutCheckEventListenerSupport.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/NodeTimeoutCheckEventListenerSupport.java @@ -23,7 +23,7 @@ public NodeTimeoutCheckEventListenerSupport(IRemotingManager remotingManager) { * Follower 节点在接收到 Leader 节点发送过来的心跳后,基于 CP 能力模型的需要检测二阶段需要提交的数据。然后进行业务层代码的回调。 *

* 基于 AP 能力模型的,因为收到数据就直接对业务方可见,没有二阶段提交。因此这里无需处理 AP 能力模型下的 - * {@link IAvailableCapabilityModel#onInboundDataSet(RemotingDomain, Map)} + * {@link IAvailableCapabilityModel#onInboundDataSet(RemotingDomainSupport, Map)} * * @param heartbeatPayload */ @@ -42,7 +42,7 @@ public void processHeartbeatPayload(final InteractivePayload heartbeatPayload) { // 包含需要处理 committed 的 remoting domain ids. 异步通知业务 RemotingManager.getRemotingManager().getEventLoopGroup() .getParallelQueueExecutor().executeOneTime(() -> { - final Collection remotingDomains = RemotingManager + final Collection remotingDomains = RemotingManager .getRemotingManager() .committedSyncingDomains(receiveNode.getTerm()); consistenceCapabilityModel.onCommitted(remotingDomains); diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/SendHeartbeatsEventListener.java b/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/SendHeartbeatsEventListener.java index 8fcb157..f5a165a 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/SendHeartbeatsEventListener.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/SendHeartbeatsEventListener.java @@ -39,7 +39,7 @@ public boolean onEvent(ObjectEvent event, } AbstractRemotingChannel remotingChannel = event.getValue(); - System.err.println("\t\theart L=>" + logger.info("\t\theart L=>" + remotingManager.getRaftConfig().getNodeInformation().identify() + "; R =>" + remotingChannel.identify()); try { @@ -94,17 +94,16 @@ public void onSendHeartbeatSuccess(final AbstractRemotingChannel remotingChannel String responsePayload = response.getPayload().toStringUtf8(); if (!ClusterDataSyncManager.isOutOfCommittedMaxIdleTime( remotingChannel.getLastCloseOnlineCheckTime(), 3)) { - System.err.println("\t is in syncing water marker phase. "); + logger.info("\t is in syncing water marker phase. "); return; } - System.err.println("\t\t is out of syncing water marker ,so start to syncing. "); ICapabilityModel capabilityModel = RemotingManager.getRemotingManager() .getCapabilityModel(); if (capabilityModel instanceof IConsistenceCapabilityModel) { IConsistenceCapabilityModel consistenceCapabilityModel = (IConsistenceCapabilityModel) capabilityModel; // 心跳发送成功,对正在同步中的数据进行 committed + 1 的操作。 - final Collection committedRemotingDomains = RemotingManager + final Collection committedRemotingDomains = RemotingManager .getRemotingManager().getCommittedRemotingDomains( processHeartbeatResponse(remotingChannel, responsePayload), remotingChannel); diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/StartLeaderElectionEventListener.java b/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/StartLeaderElectionEventListener.java index 9681398..14b6f93 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/StartLeaderElectionEventListener.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/StartLeaderElectionEventListener.java @@ -30,7 +30,7 @@ public StartLeaderElectionEventListener(IRemotingManager remotingManager) { @Override public boolean onEvent(ObjectEvent event, int listenerIndex) { // 第一次 timeout 时间到,将自己设置为 candidate。 - event.setInterruptor(false); + event.setInterrupt(false); boolean isStop = false; remotingManager.getRaftConfig().getNodeInformation() .setNodeState(NodeState.Candidate); @@ -51,7 +51,7 @@ public boolean onEvent(ObjectEvent event, int listen logger.debug( "has receive the leader election result,so will skip voted by others."); // 不需要再执行后面的 listener 了 - event.setInterruptor(true); + event.setInterrupt(true); remotingManager.getEventLoopGroup().removeListener( AbstractRemotingManager.LEADER_ELECTION_EVENT_TYPE); isStop = true; // 结束。无需 event loop diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/StartupServerEventListener.java b/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/StartupServerEventListener.java index 715f5f8..2091ee8 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/StartupServerEventListener.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/StartupServerEventListener.java @@ -27,17 +27,17 @@ public boolean onEvent(ObjectEvent event, int listenerIndex try { startupServer(event.getValue()); RemotingManager.getRemotingManager().setRaftConfig(event.getValue()); - event.setInterruptor(false); + event.setInterrupt(false); remotingManager.getEventLoopGroup().removeListener(this, 1); } catch (Exception e) { logger.error(WareCoreSwiftExceptionCode.formatExceptionMessage( WareCoreSwiftExceptionCode.REMOTING_STARTUP_SERVER_ERROR_CODE, e.getMessage()), e); - event.setInterruptor(true); + event.setInterrupt(true); isSuccess = false; } return isSuccess; } - public abstract void startupServer(WareCoreSwiftConfig raftConfig); + public abstract void startupServer(WareCoreSwiftConfig wareSwiftConfig); } diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/SyncFailEventListener.java b/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/SyncFailEventListener.java index 9acfeb5..30493c3 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/SyncFailEventListener.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/event/local/SyncFailEventListener.java @@ -2,9 +2,11 @@ import com.ware.swift.core.remoting.IInteractive; import com.ware.swift.core.remoting.IRemotingManager; -import com.ware.swift.core.remoting.RemotingDomain; +import com.ware.swift.core.remoting.RemotingDomainSupport; import com.ware.swift.core.remoting.ClusterDataSyncManager; import com.ware.swift.event.ObjectEvent; +import io.netty.util.internal.logging.InternalLogger; +import io.netty.util.internal.logging.InternalLoggerFactory; import java.util.LinkedList; import java.util.List; @@ -13,19 +15,20 @@ * */ public class SyncFailEventListener - extends AbstractLocalPipelineEventListener> { + extends AbstractLocalPipelineEventListener> { + private static final InternalLogger logger = InternalLoggerFactory.getInstance(SyncFailEventListener.class); public SyncFailEventListener(IRemotingManager remotingManager) { super(remotingManager); } @Override - public boolean onEvent(ObjectEvent> event, int listenerIndex) { + public boolean onEvent(ObjectEvent> event, int listenerIndex) { IInteractive source = (IInteractive) event.getSource(); - List fails = event.getValue(); - System.err.println("\t检测到同步失败数:" + fails.size()); - List collectorFails = new LinkedList<>(); + List fails = event.getValue(); + logger.info("\t检测到同步失败数:" + fails.size()); + List collectorFails = new LinkedList<>(); fails.forEach(remotingDomain -> { if (!source.sendPayload( ClusterDataSyncManager.newSyncInteractivePayload(remotingDomain))) { diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/AbstractRemotingPipelineEventListener.java b/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/AbstractRemotingPipelineEventListener.java index f53dcae..2b3c25e 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/AbstractRemotingPipelineEventListener.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/AbstractRemotingPipelineEventListener.java @@ -22,16 +22,16 @@ public AbstractRemotingPipelineEventListener() { /** * 将当前节点所有已经连接的 channel 发送给请求的节点 */ - public void onSendAllRemotingChannels(IInteractive raftInteractive) throws Exception { + public void onSendAllRemotingChannels(IInteractive wareSwiftInteractive) throws Exception { InteractivePayload.Builder builder = InteractivePayload.newBuilder(); - builder.setSource(raftInteractive.getInteractivePayload().getSink()); - builder.setSink(raftInteractive.getInteractivePayload().getSource()); + builder.setSource(wareSwiftInteractive.getInteractivePayload().getSink()); + builder.setSink(wareSwiftInteractive.getInteractivePayload().getSource()); builder.setPayload(ByteString.copyFrom(builderRemotingChannels( - raftInteractive.getInteractivePayload().getSource()).toString() + wareSwiftInteractive.getInteractivePayload().getSource()).toString() .getBytes(CharsetUtil.UTF_8))); // response - raftInteractive.sendPayload(builder.build()); + wareSwiftInteractive.sendPayload(builder.build()); } public StringBuilder builderRemotingChannels(String source) { diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RemotingDecentrationNodeMeetEventListener.java b/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RemotingDecentralizeNodeMeetEventListener.java similarity index 68% rename from core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RemotingDecentrationNodeMeetEventListener.java rename to core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RemotingDecentralizeNodeMeetEventListener.java index d487f34..a91634d 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RemotingDecentrationNodeMeetEventListener.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RemotingDecentralizeNodeMeetEventListener.java @@ -1,23 +1,21 @@ package com.ware.swift.core.remoting.event.remoting; -import com.ware.swift.core.remoting.IInteractive; -import com.ware.swift.core.remoting.channel.AbstractRemotingChannel; -import com.ware.swift.core.remoting.event.local.DecentrationInitRemotingChannelEventListener; import com.ware.swift.core.NodeInformation; +import com.ware.swift.core.remoting.IDecentralizeRemotingManager; +import com.ware.swift.core.remoting.IInteractive; import com.ware.swift.core.remoting.RemotingInteractiveConstants; import com.ware.swift.core.remoting.RemotingManager; - +import com.ware.swift.core.remoting.channel.AbstractRemotingChannel; +import com.ware.swift.core.remoting.event.local.DecentralizeInitRemotingChannelEventListener; import com.ware.swift.event.ObjectEvent; -import java.util.Collection; - /** * 处理去中心化发送过来的 node meet 的操作 */ -public class RemotingDecentrationNodeMeetEventListener +public class RemotingDecentralizeNodeMeetEventListener extends AbstractRemotingPipelineEventListener { - public RemotingDecentrationNodeMeetEventListener() { + public RemotingDecentralizeNodeMeetEventListener() { } @Override @@ -41,7 +39,7 @@ public boolean onEvent(ObjectEvent event, int listenerIndex) { // 2、告诉新增加的节点当前集群中有哪些节点。 onSendAllRemotingChannels(event.getValue()); // 3、启动给这个新加入的节点发送心跳 - new DecentrationInitRemotingChannelEventListener( + new DecentralizeInitRemotingChannelEventListener( RemotingManager.getRemotingManager()) .onAfterNewRemotingChannel(remotingChannel); return true; @@ -59,24 +57,7 @@ public boolean onEvent(ObjectEvent event, int listenerIndex) { */ @Override public StringBuilder builderRemotingChannels(String source) { - Collection remotingChannels = RemotingManager - .getRemotingManager().getRemotingChannels(); - - final StringBuilder remotingChannelsSB = new StringBuilder(); - remotingChannels.forEach(remotingChannel -> { - if (remotingChannel.identify().equalsIgnoreCase(source)) { - return; - } - - remotingChannelsSB.append(remotingChannel.identify()); - remotingChannelsSB.append("|");// 竖线分隔多个节点。 - }); - - // 加上本节点。 - remotingChannelsSB.append(RemotingManager.getRemotingManager() - .getChannelIdentify(RemotingManager.getRemotingManager().getRaftConfig() - .getNodeInformation().identify())); - return remotingChannelsSB; + return IDecentralizeRemotingManager.DecentralizeRemotingManager.DEFAULT_IMPL.builderRemotingChannels(source); } } diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RemotingDecenterationSayHelloEventListener.java b/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RemotingDecentralizeSayHelloEventListener.java similarity index 50% rename from core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RemotingDecenterationSayHelloEventListener.java rename to core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RemotingDecentralizeSayHelloEventListener.java index 79fc34a..a7e980a 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RemotingDecenterationSayHelloEventListener.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RemotingDecentralizeSayHelloEventListener.java @@ -1,39 +1,37 @@ package com.ware.swift.core.remoting.event.remoting; +import com.google.protobuf.ByteString; +import com.ware.swift.core.remoting.IDecentralizeRemotingManager; import com.ware.swift.core.remoting.IInteractive; import com.ware.swift.core.remoting.RemotingInteractiveConstants; import com.ware.swift.core.remoting.RemotingManager; -import com.ware.swift.core.remoting.channel.AbstractRemotingChannel; import com.ware.swift.event.ObjectEvent; import com.ware.swift.proto.InteractivePayload; -import com.google.protobuf.ByteString; import io.netty.util.CharsetUtil; -import java.util.Collection; - /** * 处理去中心化集群发送 say hello 的 event listener。 */ -public class RemotingDecenterationSayHelloEventListener +public class RemotingDecentralizeSayHelloEventListener extends AbstractRemotingPipelineEventListener { - public RemotingDecenterationSayHelloEventListener() { + public RemotingDecentralizeSayHelloEventListener() { } @Override public boolean onEvent(ObjectEvent event, int listenerIndex) { - IInteractive raftInteractive = event.getValue(); - InteractivePayload raftInteractivePayload = raftInteractive + IInteractive wareSwiftInteractive = event.getValue(); + InteractivePayload wareSwiftInteractivePayload = wareSwiftInteractive .getInteractivePayload(); // 不做身份节点身份校验 - if (!raftInteractivePayload.getSource() - .startsWith(RemotingInteractiveConstants.PTP_ROLE_PRIFIX)) { + if (!wareSwiftInteractivePayload.getSource() + .startsWith(RemotingInteractiveConstants.PTP_ROLE_PREFIX)) { RemotingManager.getRemotingManager() - .isOnlineWithRemotingChannel(raftInteractivePayload.getSource()); - raftInteractive.sendPayload(InteractivePayload.newBuilder() - .setSource(raftInteractivePayload.getSink()) - .setSink(raftInteractivePayload.getSink()) + .isOnlineWithRemotingChannel(wareSwiftInteractivePayload.getSource()); + wareSwiftInteractive.sendPayload(InteractivePayload.newBuilder() + .setSource(wareSwiftInteractivePayload.getSink()) + .setSink(wareSwiftInteractivePayload.getSink()) .setPayload( ByteString.copyFrom(RemotingInteractiveConstants.ROLE_MISMATCH .getBytes(CharsetUtil.UTF_8))) @@ -56,24 +54,7 @@ public boolean onEvent(ObjectEvent event, int listenerIndex) { */ @Override public StringBuilder builderRemotingChannels(String source) { - Collection remotingChannels = RemotingManager - .getRemotingManager().getRemotingChannels(); - - final StringBuilder remotingChannelsSB = new StringBuilder(); - remotingChannels.forEach(remotingChannel -> { - if (remotingChannel.identify().equalsIgnoreCase(source)) { - return; - } - - remotingChannelsSB.append(remotingChannel.identify()); - remotingChannelsSB.append("|");// 竖线分隔多个节点。 - }); - - // 加上本节点。 - remotingChannelsSB.append(RemotingManager.getRemotingManager() - .getChannelIdentify(RemotingManager.getRemotingManager().getRaftConfig() - .getNodeInformation().identify())); - return remotingChannelsSB; + return IDecentralizeRemotingManager.DecentralizeRemotingManager.DEFAULT_IMPL.builderRemotingChannels(source); } } diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RemotingEventDispatcher.java b/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RemotingEventDispatcher.java index 43c46d3..7c333fa 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RemotingEventDispatcher.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RemotingEventDispatcher.java @@ -131,9 +131,9 @@ public void attachListener() { REMOTING_BROADCAST_NEWNODE_EVENT_TYPE); this.addFirst(new CheckIsInChannelRegistryEventListener(), REMOTING_CHECK_IS_IN_CHANNEL_REGISTRY_EVENT_TYPE); - this.addLast(new RemotingDecenterationSayHelloEventListener(), + this.addLast(new RemotingDecentralizeSayHelloEventListener(), REMOTING_DECENTRATION_SAY_HELLO_EVENT_TYPE); - this.addLast(new RemotingDecentrationNodeMeetEventListener(), + this.addLast(new RemotingDecentralizeNodeMeetEventListener(), REMOTING_DECENTRATION_NODE_MEET_EVENT_TYPE); this.addLast(new RemotingStreamDataSyncEventListener(), REMOTING_STREAM_DATA_SYNC_EVENT_TYPE); diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RemotingLeaderElectionVoteEventListener.java b/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RemotingLeaderElectionVoteEventListener.java index b322fc5..566cadc 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RemotingLeaderElectionVoteEventListener.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RemotingLeaderElectionVoteEventListener.java @@ -25,12 +25,12 @@ public RemotingLeaderElectionVoteEventListener() { @Override public boolean onEvent(ObjectEvent event, int listenerIndex) { - IInteractive raftInteractive = event.getValue(); - InteractivePayload raftInteractivePayload = raftInteractive + IInteractive wareSwiftInteractive = event.getValue(); + InteractivePayload wareSwiftInteractivePayload = wareSwiftInteractive .getInteractivePayload(); - String source = raftInteractivePayload.getSource(); + String source = wareSwiftInteractivePayload.getSource(); logger.debug(String.format("receive vote from %s,and the sink is %s.", source, - raftInteractivePayload.getSink())); + wareSwiftInteractivePayload.getSink())); IRemotingManager remotingManager = RemotingManager.getRemotingManager(); InteractivePayload.Builder builder = InteractivePayload.newBuilder(); @@ -47,11 +47,11 @@ public boolean onEvent(ObjectEvent event, int listenerIndex) { "1"); } - logger.debug(raftInteractivePayload.getSink() + " has voted for " + logger.debug(wareSwiftInteractivePayload.getSink() + " has voted for " + remotingManager.getRaftConfig().getNodeInformation().getVotedFor() + " success."); - builder.setSource(raftInteractivePayload.getSink()); - builder.setSink(raftInteractivePayload.getSource()); - return raftInteractive.sendPayload(builder.build()); + builder.setSource(wareSwiftInteractivePayload.getSink()); + builder.setSink(wareSwiftInteractivePayload.getSource()); + return wareSwiftInteractive.sendPayload(builder.build()); } } diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RemotingReceiveHeartbeatEventListener.java b/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RemotingReceiveHeartbeatEventListener.java index d8293b0..c55db09 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RemotingReceiveHeartbeatEventListener.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RemotingReceiveHeartbeatEventListener.java @@ -26,11 +26,11 @@ public boolean onEvent(ObjectEvent event, int listenerIndex) { .getInteractivePayload(); // 往 mailbox 里生产一个消息 // 注意:这里需要判断网络分区后又重新收到 Leader/ptp 发送过来的心跳这种情况。 - WareCoreSwiftConfig raftConfig = RemotingManager.getRemotingManager().getRaftConfig(); + WareCoreSwiftConfig wareSwiftConfig = RemotingManager.getRemotingManager().getRaftConfig(); InteractivePayload.Builder builder = InteractivePayload.newBuilder(); long defaultTerm = ClusterDataSyncManager.DEFAULT_SYNCING_TERM; - if (raftConfig.getLeader() != null) { - long currentTerm = raftConfig.getLeader().getTerm(); + if (wareSwiftConfig.getLeader() != null) { + long currentTerm = wareSwiftConfig.getLeader().getTerm(); long receiveTerm = Long.valueOf(heartbeatPayload.getHeadersMap() .get(ClusterDataSyncManager.HEADER_KEY_TERM_VALUE)); if (currentTerm < receiveTerm) { @@ -38,7 +38,7 @@ public boolean onEvent(ObjectEvent event, int listenerIndex) { NodeInformation leaderNode = (NodeInformation) RemotingInteractiveConstants.OBJECT_ENCODING_HANDLER .decodeResult(heartbeatPayload.getPayload().toByteArray(), NodeInformation.class); - raftConfig.setLeader(leaderNode); + wareSwiftConfig.setLeader(leaderNode); } defaultTerm = currentTerm; } diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RemotingReceiveLeaderElectionResultEventListener.java b/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RemotingReceiveLeaderElectionResultEventListener.java index 7b704d5..cba822e 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RemotingReceiveLeaderElectionResultEventListener.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RemotingReceiveLeaderElectionResultEventListener.java @@ -29,20 +29,20 @@ public RemotingReceiveLeaderElectionResultEventListener() { @Override public boolean onEvent(ObjectEvent event, int listenerIndex) { - IInteractive raftInteractive = event.getValue(); - InteractivePayload interactivePayload = raftInteractive.getInteractivePayload(); + IInteractive wareSwiftInteractive = event.getValue(); + InteractivePayload interactivePayload = wareSwiftInteractive.getInteractivePayload(); byte[] result = interactivePayload.getPayload().toByteArray(); try { NodeInformation leader = (NodeInformation) RemotingInteractiveConstants.OBJECT_ENCODING_HANDLER .decodeResult(result, NodeInformation.class); logger.debug("receive leader election result " + leader.toString()); - WareCoreSwiftConfig raftConfig = RemotingManager.getRemotingManager().getRaftConfig(); + WareCoreSwiftConfig wareSwiftConfig = RemotingManager.getRemotingManager().getRaftConfig(); if (!leader.identify() - .equalsIgnoreCase(raftConfig.getNodeInformation().identify())) { + .equalsIgnoreCase(wareSwiftConfig.getNodeInformation().identify())) { // 更新当前节点的角色 - raftConfig.setLeader(leader); + wareSwiftConfig.setLeader(leader); if (leader.getTerm() > 1) { AbstractRemotingChannel leaderRemotingChannel = RemotingManager .getRemotingManager() @@ -50,7 +50,7 @@ public boolean onEvent(ObjectEvent event, int listenerIndex) { ClusterDataSyncManager.startDataSyncing(leaderRemotingChannel); } } else { - System.err.println("is same to self receive leader election .receive is " + logger.info("is same to self receive leader election .receive is " + leader.identify() + " and self is " + RemotingManager.getRemotingManager().getRaftConfig().getLeader() .identify()); @@ -61,7 +61,7 @@ public boolean onEvent(ObjectEvent event, int listenerIndex) { builder.setPayload( ByteString.copyFrom(RemotingInteractiveConstants.RECEIVE_LEADER_ACK .getBytes(Charset.forName("UTF-8")))); - boolean sendSuccess = raftInteractive.sendPayload(builder.build()); + boolean sendSuccess = wareSwiftInteractive.sendPayload(builder.build()); logger.debug("receive leader election result " + leader.toString() + " ACK send :" + sendSuccess); } catch (Exception e) { diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RemotingReceiveSdownLeaderEventListener.java b/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RemotingReceiveSdownLeaderEventListener.java index c7c2782..ae50897 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RemotingReceiveSdownLeaderEventListener.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RemotingReceiveSdownLeaderEventListener.java @@ -31,9 +31,7 @@ public boolean onEvent(ObjectEvent event, int listenerIndex) { NodeInformation sdownLeader = (NodeInformation) RemotingInteractiveConstants.OBJECT_ENCODING_HANDLER .decodeResult(payload.getPayload().toByteArray(), NodeInformation.class); - // logger.debug(String.format("receive sdown for %s from %s Follower .", - // sdownLeader.identify(), payload.getSource())); - System.err.println(String.format("receive sdown for %s from %s Follower .", + logger.info(String.format("receive sdown for %s from %s Follower .", sdownLeader.identify(), payload.getSource())); String identify = RemotingManager.getRemotingManager().getRaftConfig() .getLeader().identify(); diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RemotingSayHelloEventListener.java b/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RemotingSayHelloEventListener.java index c4244c0..a74043d 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RemotingSayHelloEventListener.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RemotingSayHelloEventListener.java @@ -24,17 +24,17 @@ public RemotingSayHelloEventListener() { @Override public boolean onEvent(ObjectEvent event, int listenerIndex) { - InteractivePayload raftInteractivePayload = event.getValue() + InteractivePayload wareSwiftInteractivePayload = event.getValue() .getInteractivePayload(); // 回复当前节点的信息 InteractivePayload.Builder builder = InteractivePayload.newBuilder(); builder.setSource(RemotingManager.getRemotingManager().getRaftConfig() .getNodeInformation().identify()); - builder.setSink(raftInteractivePayload.getSource()); + builder.setSink(wareSwiftInteractivePayload.getSource()); NodeInformation leader = RemotingManager.getRemotingManager().getRaftConfig() .getLeader(); RemotingManager.getRemotingManager() - .isOnlineWithRemotingChannel(raftInteractivePayload.getSource()); + .isOnlineWithRemotingChannel(wareSwiftInteractivePayload.getSource()); leader = (leader == null ? new NodeInformation() : leader); builder.setPayload( ByteString.copyFrom(RemotingInteractiveConstants.OBJECT_ENCODING_HANDLER diff --git a/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RuntimeDataSyncEventListener.java b/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RuntimeDataSyncEventListener.java index b74b56c..ce5eede 100644 --- a/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RuntimeDataSyncEventListener.java +++ b/core-swift/src/main/java/com/ware/swift/core/remoting/event/remoting/RuntimeDataSyncEventListener.java @@ -1,20 +1,15 @@ package com.ware.swift.core.remoting.event.remoting; +import com.google.protobuf.ByteString; import com.ware.swift.core.WareCoreSwiftConfig; -import com.ware.swift.core.remoting.IInteractive; -import com.ware.swift.core.remoting.RemotingDomain; -import com.ware.swift.core.remoting.RemotingInteractiveConstants; -import com.ware.swift.core.remoting.RemotingManager; -import com.ware.swift.core.remoting.ClusterDataSyncManager; +import com.ware.swift.core.remoting.*; import com.ware.swift.event.ObjectEvent; import com.ware.swift.proto.InteractivePayload; -import com.google.protobuf.ByteString; import io.netty.util.CharsetUtil; import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLoggerFactory; import java.util.HashMap; -import java.util.concurrent.atomic.AtomicLong; /** * 运行态下的一个数据同步处理入口。处理 Leader-Follower 架构下的数据同步 @@ -23,28 +18,26 @@ public class RuntimeDataSyncEventListener extends AbstractRemotingPipelineEventL private static final InternalLogger log = InternalLoggerFactory .getInstance(RuntimeDataSyncEventListener.class); - private static final AtomicLong inboundDataSetSize = new AtomicLong(); - @Override public boolean onEvent(ObjectEvent event, int listenerIndex) { InteractivePayload interactivePayload = event.getValue().getInteractivePayload(); InteractivePayload.Builder builder = InteractivePayload.newBuilder(); - WareCoreSwiftConfig raftConfig = RemotingManager.getRemotingManager().getRaftConfig(); + WareCoreSwiftConfig wareSwiftConfig = RemotingManager.getRemotingManager().getRaftConfig(); builder.setSource(RemotingManager.getRemotingManager() - .getChannelIdentify(raftConfig.getNodeInformation().identify())); + .getChannelIdentify(wareSwiftConfig.getNodeInformation().identify())); // 添加到正在同步 payload 的消息列表中。 String className = interactivePayload.getHeadersMap() .get(ClusterDataSyncManager.HEADER_KEY_REMOTING_DOMAIN_CLASS); try { Class clazz = Class.forName(className); - RemotingDomain remotingDomain = (RemotingDomain) RemotingInteractiveConstants.OBJECT_ENCODING_HANDLER + RemotingDomainSupport remotingDomain = (RemotingDomainSupport) RemotingInteractiveConstants.OBJECT_ENCODING_HANDLER .decodeResult(interactivePayload.getPayload().toByteArray(), clazz); RemotingManager.getRemotingManager().getCapabilityModel().onInboundDataSet( remotingDomain, new HashMap(interactivePayload.getHeadersMap())); builder.putHeaders(ClusterDataSyncManager.DATA_SYNC_STATUS_CODE, String.valueOf(ClusterDataSyncManager.DATA_SYNC_STATUS_CODE_SUCCESS)); builder.setPayload(ByteString.copyFrom(ClusterDataSyncManager - .formatSuccessResonPhrase(className).getBytes(CharsetUtil.UTF_8))); + .formatSuccessReasonPhrase(className).getBytes(CharsetUtil.UTF_8))); } catch (Exception e) { builder.putHeaders(ClusterDataSyncManager.DATA_SYNC_STATUS_CODE, String.valueOf(ClusterDataSyncManager.DATA_SYNC_STATUS_CODE_ERROR)); diff --git a/eureka-on-swift/src/main/java/com/ware/swift/eureka/avaliable/EurekaAvailableCapabilityModel.java b/eureka-on-swift/src/main/java/com/ware/swift/eureka/avaliable/EurekaAvailableCapabilityModel.java deleted file mode 100644 index 5461918..0000000 --- a/eureka-on-swift/src/main/java/com/ware/swift/eureka/avaliable/EurekaAvailableCapabilityModel.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.ware.swift.eureka.avaliable; - -import com.ware.swift.core.remoting.DataSyncEmitter; -import com.ware.swift.core.remoting.OutboundCallback; -import com.ware.swift.core.remoting.RemotingDomain; -import com.ware.swift.core.remoting.RemotingManager; -import com.ware.swift.core.remoting.avalispart.AbstractAvailableCapabilityModel; -import com.ware.swift.core.remoting.conspart.ServiceInstance; - -import java.util.Collection; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.function.BiConsumer; - -/** - * 基于 AP 模型下的服务注册中心 - */ -public class EurekaAvailableCapabilityModel extends AbstractAvailableCapabilityModel { - - /** - * - */ - private ConcurrentHashMap serviceInstanceRegistry = new ConcurrentHashMap<>( - 128); - - /** - * 运行态本节点收到其他节点同步过来的数据。 - * - * @param remotingDomain - * @param headsMap - */ - @Override - public void onInboundDataSet(RemotingDomain remotingDomain, - Map headsMap) { - RemotingManager.getRemotingManager().getRaftConfig().getNodeInformation() - .incrementCommittedCount(); - ServiceInstance serviceInstance = (ServiceInstance) remotingDomain; - ServiceInstance oldServiceInstance = serviceInstanceRegistry - .putIfAbsent(serviceInstance.getServiceId(), serviceInstance); - if (oldServiceInstance != null - && oldServiceInstance.getVersion() < serviceInstance.getVersion()) { - serviceInstanceRegistry.putIfAbsent(serviceInstance.getServiceId(), - serviceInstance); - } - - System.err.println("\t AP 数据大小=" + serviceInstanceRegistry.size()); - } - - /** - * 运行态,其他节点需要向本节点进行数据同步。 - * - * @param syncDataEmitter - */ - @Override - public void onDataStreamSyncing(DataSyncEmitter syncDataEmitter) { - serviceInstanceRegistry.forEach(Runtime.getRuntime().availableProcessors(), - new BiConsumer() { - @Override - public void accept(String s, ServiceInstance serviceInstance) { - syncDataEmitter.onEmit(serviceInstance); - } - }); - syncDataEmitter.onEmitFinish(); - } - - @Override - public void onOutboundDataSet(RemotingDomain remotingDomain, - OutboundCallback callable) { - RemotingManager.getRemotingManager().getRaftConfig().getNodeInformation() - .incrementCommittedCount(); - ServiceInstance serviceInstance = (ServiceInstance) remotingDomain; - ServiceInstance oldServiceInstance = serviceInstanceRegistry - .putIfAbsent(serviceInstance.getServiceId(), serviceInstance); - if (oldServiceInstance != null - && serviceInstance.getVersion() > oldServiceInstance.getVersion()) { - serviceInstanceRegistry.put(serviceInstance.getServiceId(), serviceInstance); - } - System.err.println("\t AP 数据大小=" + serviceInstanceRegistry.size()); - super.onOutboundDataSet(remotingDomain, callable); - } - - @Override - public void onOutOfSyncingWaterMarker( - Collection outOfSyncingWaterMarker) { - // nothing to do - } -} diff --git a/eureka-on-swift/src/main/java/com/ware/swift/eureka/consistence/EurekaConsistenceCapabilityModel.java b/eureka-on-swift/src/main/java/com/ware/swift/eureka/consistence/EurekaConsistenceCapabilityModel.java deleted file mode 100644 index 1639f35..0000000 --- a/eureka-on-swift/src/main/java/com/ware/swift/eureka/consistence/EurekaConsistenceCapabilityModel.java +++ /dev/null @@ -1,77 +0,0 @@ -package com.ware.swift.eureka.consistence; - -import com.ware.swift.core.remoting.DataSyncEmitter; -import com.ware.swift.core.remoting.RemotingDomain; -import com.ware.swift.core.remoting.conspart.AbstractConsistenceCapabilityModel; -import com.ware.swift.core.remoting.conspart.ServiceInstance; - -import java.util.Collection; -import java.util.concurrent.ConcurrentHashMap; - -/** - * 服务注册中心的基于 raft 的一致性能力模型的实现 - */ -public class EurekaConsistenceCapabilityModel extends AbstractConsistenceCapabilityModel { - - /** - * - */ - private ConcurrentHashMap serviceInstanceRegistry = new ConcurrentHashMap<>( - 128); - - /** - * 1. 运行态一个数据 committed 成功之后,会回调这里 - * - * 2. 新加入一个节点,从其他节点同步数据过来也会触发这里的回调。 - * @param committedRemotingDomains - */ - @Override - public void onCommitted(Collection committedRemotingDomains) { - - if (committedRemotingDomains.isEmpty()) { - return; - } - final ServiceInstance serviceInstance = (ServiceInstance) committedRemotingDomains - .iterator().next(); - committedRemotingDomains.forEach(remotingDomain -> { - ServiceInstance committedServiceInstance = (ServiceInstance) remotingDomain; - ServiceInstance isExist = serviceInstanceRegistry.putIfAbsent( - committedServiceInstance.getServiceId(), committedServiceInstance); - if (isExist != null - && committedServiceInstance.getVersion() > isExist.getVersion()) { - // update - serviceInstanceRegistry.put(committedServiceInstance.getServiceId(), - committedServiceInstance); - } - }); - - System.err.println("\t" + serviceInstance.getServiceId() + "; " - + serviceInstance.getVersion() + "; size=" - + serviceInstanceRegistry.size()); - } - - /** - * 用来处理那些 out of syncing water marker 的数据。 - * @param outOfSyncingWaterMarker - */ - @Override - public void onOutOfSyncingWaterMarker( - Collection outOfSyncingWaterMarker) { - - } - - /** - * 这里对已经 committed 的数据进行同步。 - * @param dataSyncEmitter - */ - @Override - public void onDataStreamSyncing(DataSyncEmitter dataSyncEmitter) { - // 同步出去 - System.err.println(" -> data size=" + serviceInstanceRegistry.size()); - serviceInstanceRegistry.forEach( - (key, serviceInstance) -> dataSyncEmitter.onEmit(serviceInstance)); - - dataSyncEmitter.onEmitFinish(); - } - -} diff --git a/eureka-on-swift/src/test/java/com/ware/swift/eureka/tests/ClientTests.java b/eureka-on-swift/src/test/java/com/ware/swift/eureka/tests/ClientTests.java deleted file mode 100644 index 0d79f7c..0000000 --- a/eureka-on-swift/src/test/java/com/ware/swift/eureka/tests/ClientTests.java +++ /dev/null @@ -1,318 +0,0 @@ -package com.ware.swift.eureka.tests; - -import com.google.protobuf.ByteString; -import com.ware.swift.core.NodeInformation; -import com.ware.swift.core.remoting.ClusterDataSyncManager; -import com.ware.swift.core.remoting.IRemotingManager; -import com.ware.swift.core.remoting.RemotingInteractiveConstants; -import com.ware.swift.core.remoting.channel.AbstractRemotingChannel; -import com.ware.swift.core.remoting.conspart.ServiceInstance; -import com.ware.swift.grpc.GrpcRemotingChannel; -import com.ware.swift.proto.InteractivePayload; -import com.ware.swift.rsocket.*; -import com.ware.swift.rsocket.handler.RSocketRequestHandlerSupport; -import io.grpc.ManagedChannel; -import io.grpc.netty.NettyChannelBuilder; -import org.junit.Test; - -import java.util.*; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicLong; - -public class ClientTests { - - private final static int DATA_TOTAL_COUNT = 1000; - private final static int MULTI_THREAD_COUNT = 10; - private final static IRemotingManager remo = new LeaderFollowerRSocketRemotingManager(); - - public static AbstractRemotingChannel newGrpcRemotingChannel(String addressPort, - String clusterName) { - String[] addressPortArr = addressPort.split("[:]"); - NettyChannelBuilder nettyChannelBuilder = NettyChannelBuilder.forAddress( - addressPortArr[0].trim(), Integer.valueOf(addressPortArr[1].trim())); - ManagedChannel channel = nettyChannelBuilder.usePlaintext() - .flowControlWindow(NettyChannelBuilder.DEFAULT_FLOW_CONTROL_WINDOW) - .build(); - GrpcRemotingChannel remotingChannel = new GrpcRemotingChannel(channel, - addressPort); - remotingChannel.setIdentify(clusterName + "@" + addressPort + "@" + clusterName); - return remotingChannel; - } - - public static AbstractRemotingChannel newRSocketRemotingChannel(String addressPort, - String clusterName) { - String[] addressPortArr = addressPort.split("[:]"); - - RSocketClientBootstrap clientBootstrap = new RSocketClientBootstrap( - addressPortArr[0], Integer.valueOf(addressPortArr[1])); - - clientBootstrap - .setSocketRequestHandlerFactory(new IRSocketRequestHandlerFactory() { - @Override - public RSocketRequestHandlerSupport createWithClient() { - return new RSocketClientSideInboundHandler( - RSocketRequestHandlerSupport.RSocketRequestHandlerRole.CLIENT); - } - }); - while (true) { - try { - AbstractRemotingChannel remotingChannel = new RSocketRemotingChannel( - addressPort, clientBootstrap); - remotingChannel.setIdentify("RAFT@" + addressPort + "@" + clusterName); - return remotingChannel; - } catch (Exception e) { - System.err.println(addressPort + " is unavliable..."); - try { - TimeUnit.SECONDS.sleep(1); - } catch (InterruptedException e1) { - } - } - } - } - - /** - * DefaultClientInteractivePayloadHandler#getLeaderWithRSocket - */ - @Test - public void getLeaderWithRSocket() { - System.err.println(getLeaderRSocketRequest().toString()); - } - - @Test - public void getLeaderWithGrpc() { - System.err.println(getLeaderGrpcRequest().toString()); - } - - public NodeInformation getLeaderRSocketRequest() { - AbstractRemotingChannel abstractRemotingChannel = newRSocketRemotingChannel( - "127.0.0.1:19092", "DEFAULT"); - InteractivePayload.Builder builder = InteractivePayload.newBuilder(); - builder.setSink("getLeader"); - - InteractivePayload responsePayload = abstractRemotingChannel - .requestResponse(builder.build()); - NodeInformation leader = (NodeInformation) RemotingInteractiveConstants.OBJECT_ENCODING_HANDLER - .decodeResult(responsePayload.getPayload().toByteArray(), - NodeInformation.class); - System.err.println(leader.toString()); - return leader; - } - - public NodeInformation getLeaderGrpcRequest() { - AbstractRemotingChannel abstractRemotingChannel = newGrpcRemotingChannel( - "127.0.0.1:19092", "DEFAULT"); - InteractivePayload.Builder builder = InteractivePayload.newBuilder(); - builder.setSink("getLeader"); - - InteractivePayload responsePayload = abstractRemotingChannel - .requestResponse(builder.build()); - NodeInformation leader = (NodeInformation) RemotingInteractiveConstants.OBJECT_ENCODING_HANDLER - .decodeResult(responsePayload.getPayload().toByteArray(), - NodeInformation.class); - System.err.println(leader.toString()); - return leader; - } - - public void addSomeDataWithRSocket() { - NodeInformation leader = new ClientTests().getLeaderRSocketRequest(); - System.err.println("get the leader is:" + leader.toString()); - AbstractRemotingChannel abstractRemotingChannel = newRSocketRemotingChannel( - leader.getAddressPort(), "DEFAULT"); - for (int i = 0; i < DATA_TOTAL_COUNT; i++) { - InteractivePayload.Builder builder = InteractivePayload.newBuilder(); - builder.setSink("dataSync"); - ServiceInstance serviceInstance = new ServiceInstance(); - serviceInstance.setServiceId( - randaomeServiceName("rsocker", "order.service") + (i + 1)); - serviceInstance.setHost("127.0.0.1"); - serviceInstance.setPort(9090); - serviceInstance.setScheme("tcp://"); - serviceInstance.setSecure(true); - serviceInstance.setVersion(new AtomicLong().incrementAndGet()); - builder.putHeaders(ClusterDataSyncManager.HEADER_KEY_REMOTING_DOMAIN_CLASS, - serviceInstance.getClass().getName()); - builder.setPayload(ByteString - .copyFrom(RemotingInteractiveConstants.OBJECT_ENCODING_HANDLER - .encodingResult(serviceInstance))); - - InteractivePayload responsePayload = abstractRemotingChannel - .requestResponse(builder.build()); - System.err.println( - serviceInstance.getServiceId() + ":" + serviceInstance.getVersion() - + "; " + responsePayload.getPayload().toStringUtf8()); - } - } - - @Test - public void boolInit() { - - Properties properties = new Properties(); - properties.setProperty("aaa", "true"); - System.err.println(Boolean.valueOf(properties.getProperty("aaa"))); - } - - @Test - public void multiThreadDataSyncWithRSocket() throws Exception { - - for (int i = 0; i < MULTI_THREAD_COUNT; i++) { - new Thread(() -> { - try { - leaderFollowerdataSyncWithRSocket(); - } catch (Exception e) { - e.printStackTrace(); - } - }).start(); - } - - System.in.read(); - } - - @Test - public void decenterationDataSyncWithRSocket() throws Exception { - AtomicLong syncCount = new AtomicLong(); - List addressPortList = new LinkedList<>(); - addressPortList.add("127.0.0.1:19091"); - addressPortList.add("127.0.0.1:19092"); - addressPortList.add("127.0.0.1:19093"); - HashMap remotingChannels = new HashMap<>(); - while (true) { - int index = new Random().nextInt(addressPortList.size()); - String addressPort = addressPortList.get(index); - AbstractRemotingChannel abstractRemotingChannel = remotingChannels - .get(addressPort); - System.err.println("\tremoting channel :" + addressPort); - if (abstractRemotingChannel == null) { - AbstractRemotingChannel newRSocketRemotingChannel = newRSocketRemotingChannel( - addressPortList.get(index), "DEFAULT"); - remotingChannels.put(addressPort, newRSocketRemotingChannel); - abstractRemotingChannel = newRSocketRemotingChannel; - } - InteractivePayload.Builder builder = InteractivePayload.newBuilder(); - builder.setSink("dataSync"); - ServiceInstance serviceInstance = new ServiceInstance(); - serviceInstance.setServiceId(randaomeServiceName("rsocket", "consumer")); - serviceInstance.setHost("127.0.0.1"); - serviceInstance.setPort(9090); - serviceInstance.setScheme("tcp://"); - serviceInstance.setSecure(true); - serviceInstance.setVersion(syncCount.incrementAndGet()); - builder.putHeaders(ClusterDataSyncManager.HEADER_KEY_REMOTING_DOMAIN_CLASS, - serviceInstance.getClass().getName()); - builder.setPayload(ByteString - .copyFrom(RemotingInteractiveConstants.OBJECT_ENCODING_HANDLER - .encodingResult(serviceInstance))); - - InteractivePayload responsePayload; - try { - responsePayload = abstractRemotingChannel - .requestResponse(builder.build()); - System.err.println(syncCount.get() + "; " - + responsePayload.getPayload().toStringUtf8()); - } catch (Exception e) { - e.printStackTrace(); - } - TimeUnit.SECONDS.sleep(1); - } - } - - @Test - public void leaderFollowerdataSyncWithRSocket() throws Exception { - addSomeDataWithRSocket(); - AtomicLong syncCount = new AtomicLong(); - NodeInformation leader = getLeaderRSocketRequest(); - System.err.println("get the leader is:" + leader.toString()); - AbstractRemotingChannel abstractRemotingChannel = newRSocketRemotingChannel( - leader.getAddressPort(), "DEFAULT"); - while (true) { - InteractivePayload.Builder builder = InteractivePayload.newBuilder(); - builder.setSink("dataSync"); - ServiceInstance serviceInstance = new ServiceInstance(); - serviceInstance.setServiceId("rsocket.order.consumer"); - serviceInstance.setHost("127.0.0.1"); - serviceInstance.setPort(9090); - serviceInstance.setScheme("tcp://"); - serviceInstance.setSecure(true); - serviceInstance.setVersion(syncCount.incrementAndGet()); - builder.putHeaders(ClusterDataSyncManager.HEADER_KEY_REMOTING_DOMAIN_CLASS, - serviceInstance.getClass().getName()); - builder.setPayload(ByteString - .copyFrom(RemotingInteractiveConstants.OBJECT_ENCODING_HANDLER - .encodingResult(serviceInstance))); - - InteractivePayload responsePayload = abstractRemotingChannel - .requestResponse(builder.build()); - System.err.println( - syncCount.get() + "; " + responsePayload.getPayload().toStringUtf8()); - - TimeUnit.SECONDS.sleep(5); - } - } - - public String randaomeServiceName(String prefix, String suffix) { - - return prefix + "." + UUID.randomUUID().toString().substring(0, 4) + "." + suffix; - } - - public AbstractRemotingChannel addSomeDataWithGrpc() { - NodeInformation leader = new ClientTests().getLeaderGrpcRequest(); - System.err.println("get the leader is:" + leader.toString()); - AbstractRemotingChannel abstractRemotingChannel = newGrpcRemotingChannel( - leader.getAddressPort(), "DEFAULT"); - for (int i = 0; i < DATA_TOTAL_COUNT; i++) { - InteractivePayload.Builder builder = InteractivePayload.newBuilder(); - builder.setSink("dataSync"); - ServiceInstance serviceInstance = new ServiceInstance(); - serviceInstance - .setServiceId(randaomeServiceName("grpc", "order.service") + (i + 1)); - serviceInstance.setHost("127.0.0.1"); - serviceInstance.setPort(9090); - serviceInstance.setScheme("tcp://"); - serviceInstance.setSecure(true); - serviceInstance.setVersion(new AtomicLong().incrementAndGet()); - builder.putHeaders(ClusterDataSyncManager.HEADER_KEY_REMOTING_DOMAIN_CLASS, - serviceInstance.getClass().getName()); - builder.setPayload(ByteString - .copyFrom(RemotingInteractiveConstants.OBJECT_ENCODING_HANDLER - .encodingResult(serviceInstance))); - - InteractivePayload responsePayload = abstractRemotingChannel - .requestResponse(builder.build()); - System.err.println( - serviceInstance.getServiceId() + ":" + serviceInstance.getVersion() - + "; " + responsePayload.getPayload().toStringUtf8()); - } - - return abstractRemotingChannel; - } - - @Test - public void dataSyncWithGrpc() throws Exception { - AtomicLong syncCount = new AtomicLong(); - NodeInformation leader = new ClientTests().getLeaderGrpcRequest(); - System.err.println("get the leader is:" + leader.toString()); - AbstractRemotingChannel abstractRemotingChannel = addSomeDataWithGrpc(); - while (true) { - InteractivePayload.Builder builder = InteractivePayload.newBuilder(); - builder.setSink("dataSync"); - ServiceInstance serviceInstance = new ServiceInstance(); - serviceInstance.setServiceId("grpc.order.consumer"); - serviceInstance.setHost("127.0.0.1"); - serviceInstance.setPort(9090); - serviceInstance.setScheme("tcp://"); - serviceInstance.setSecure(true); - serviceInstance.setVersion(syncCount.incrementAndGet()); - builder.putHeaders(ClusterDataSyncManager.HEADER_KEY_REMOTING_DOMAIN_CLASS, - serviceInstance.getClass().getName()); - builder.setPayload(ByteString - .copyFrom(RemotingInteractiveConstants.OBJECT_ENCODING_HANDLER - .encodingResult(serviceInstance))); - - InteractivePayload responsePayload = abstractRemotingChannel - .requestResponse(builder.build()); - System.err.println( - syncCount.get() + "; " + responsePayload.getPayload().toStringUtf8()); - - TimeUnit.SECONDS.sleep(10); - } - } -} diff --git a/eureka-on-swift/src/test/java/com/ware/swift/eureka/tests/WareSwiftNodeStartTests.java b/eureka-on-swift/src/test/java/com/ware/swift/eureka/tests/WareSwiftNodeStartTests.java index 14d8489..6441375 100644 --- a/eureka-on-swift/src/test/java/com/ware/swift/eureka/tests/WareSwiftNodeStartTests.java +++ b/eureka-on-swift/src/test/java/com/ware/swift/eureka/tests/WareSwiftNodeStartTests.java @@ -15,11 +15,11 @@ */ package com.ware.swift.eureka.tests; -import org.junit.Test; - -import com.ware.swift.core.CoreSwiftManager; import com.ware.swift.core.NodeInformation; import com.ware.swift.core.WareCoreSwiftGlobalContext; +import com.ware.swift.core.WareSwiftDeveloperManager; +import org.junit.Test; + import java.util.concurrent.TimeUnit; /** @@ -30,14 +30,11 @@ public class WareSwiftNodeStartTests { @Test public void wareSwiftStartup() throws Exception { - WareCoreSwiftGlobalContext raftGlobalContext = new WareCoreSwiftGlobalContext(); - raftGlobalContext.setConfigPath("ware-swift.properties"); - CoreSwiftManager.getInstance().init(raftGlobalContext); - + WareCoreSwiftGlobalContext wareSwiftGlobalContext = WareSwiftDeveloperManager.wareSwiftInit("ware-swift.properties"); while (true) { TimeUnit.SECONDS.sleep(1); - NodeInformation leader = raftGlobalContext.getLeader(); + NodeInformation leader = wareSwiftGlobalContext.getLeader(); if (leader == null) { System.out.println("does not election..."); continue; diff --git a/eureka-on-swift/src/test/resources/META-INF/swift-plugins/com.ware.swift.core.remoting.ICapabilityModel b/eureka-on-swift/src/test/resources/META-INF/swift-plugins/com.ware.swift.core.remoting.ICapabilityModel index 5f8bc3f..5757610 100644 --- a/eureka-on-swift/src/test/resources/META-INF/swift-plugins/com.ware.swift.core.remoting.ICapabilityModel +++ b/eureka-on-swift/src/test/resources/META-INF/swift-plugins/com.ware.swift.core.remoting.ICapabilityModel @@ -1 +1 @@ -com.ware.swift.eureka.consistence.EurekaConsistenceCapabilityModel \ No newline at end of file +com.ware.swift.eureka.avaliable.EurekaAvailableCapabilityModel \ No newline at end of file diff --git a/eureka-on-swift/src/test/resources/META-INF/swift-plugins/com.ware.swift.core.remoting.IRemotingManager b/eureka-on-swift/src/test/resources/META-INF/swift-plugins/com.ware.swift.core.remoting.IRemotingManager index 7d8cdde..7aeebc8 100644 --- a/eureka-on-swift/src/test/resources/META-INF/swift-plugins/com.ware.swift.core.remoting.IRemotingManager +++ b/eureka-on-swift/src/test/resources/META-INF/swift-plugins/com.ware.swift.core.remoting.IRemotingManager @@ -1 +1 @@ -com.ware.swift.rsocket.DecentrationRSocketRemotingManager \ No newline at end of file +com.ware.swift.rsocket.LeaderFollowerRSocketRemotingManager \ No newline at end of file diff --git a/event-swift/src/main/java/com/ware/swift/event/BaseCondition.java b/event-swift/src/main/java/com/ware/swift/event/BaseCondition.java deleted file mode 100644 index dcb795a..0000000 --- a/event-swift/src/main/java/com/ware/swift/event/BaseCondition.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.ware.swift.event; - -/** - *

- * [说明:]
- * O: 就是被观察这对象,因为它承载了数据源,提供条件的判断。
- * V: 满足某个条件的固定值
- * 
- * 详细使用 - */ -public abstract class BaseCondition { - protected O observiable; - protected V value; - - public BaseCondition(O observiable, V v) { - this.observiable = observiable; - this.value = v; - } - - /** - * 业务触发的前提条件。需要业务人员根据自己的业务需求来实现 - * - * @return - */ - public abstract boolean isFinished(); - - /** - * 当 isFinished return true 时,handler 中的业务逻辑才会触发 - */ - public abstract void handler(); - - public O getObserviable() { - final O o = observiable; - return o; - } - - public V getValue() { - final V v = value; - return v; - } -} diff --git a/event-swift/src/main/java/com/ware/swift/event/BuilderStackTrace.java b/event-swift/src/main/java/com/ware/swift/event/BuilderStackTrace.java deleted file mode 100644 index 1bc67ea..0000000 --- a/event-swift/src/main/java/com/ware/swift/event/BuilderStackTrace.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.ware.swift.event; - -import java.util.ArrayList; -import java.util.List; - -/** - * 并行队列执行器 builder 时的堆栈跟踪 - * @author pengbingting - * - */ -public class BuilderStackTrace { - - private String threadPrefix ; - private int corePoolSize,maxPoolSize ; - private List stackInfo = new ArrayList<>(); - - public List getStackInfo() { - return stackInfo; - } - - public void addStackInfo(String stackInfo) { - this.stackInfo.add(stackInfo); - } - - public String getThreadPrefix() { - return threadPrefix; - } - - public void setThreadPrefix(String threadPrefix) { - this.threadPrefix = threadPrefix; - } - - public int getCorePoolSize() { - return corePoolSize; - } - - public void setCorePoolSize(int corePoolSize) { - this.corePoolSize = corePoolSize; - } - - public int getMaxPoolSize() { - return maxPoolSize; - } - - public void setMaxPoolSize(int maxPoolSize) { - this.maxPoolSize = maxPoolSize; - } - -} diff --git a/event-swift/src/main/java/com/ware/swift/event/ConditionSupport.java b/event-swift/src/main/java/com/ware/swift/event/ConditionSupport.java deleted file mode 100644 index 1bb93d7..0000000 --- a/event-swift/src/main/java/com/ware/swift/event/ConditionSupport.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.ware.swift.event; - -/** - * - * @param 条件满足后负责执行业务逻辑的执行器: executor - * @param 业务执行完后的返回值: result - * @param 产生 condtion 的事件源: source - */ -public abstract class ConditionSupport { - - protected E executor ; - public ConditionSupport(E executor){ - this.executor = executor; - } - - /** - * - * @param src - * @return - */ - protected abstract boolean accept(S src) ; - - /** - * - * @param src - * @return - */ - public R conditionPerform(S src){ - if (accept(src)){ - return onTrueExecute(this.executor,src); - } - - return onFalseExecute(this.executor,src); - } - - /** - * - * @param executor - * @param src - * @return - */ - protected abstract R onTrueExecute(E executor,S src); - - /** - * - * @param executor - * @param src - * @return - */ - protected abstract R onFalseExecute(E executor,S src); -} \ No newline at end of file diff --git a/event-swift/src/main/java/com/ware/swift/event/EventContext.java b/event-swift/src/main/java/com/ware/swift/event/EventContext.java index 4d642dc..11d3d18 100644 --- a/event-swift/src/main/java/com/ware/swift/event/EventContext.java +++ b/event-swift/src/main/java/com/ware/swift/event/EventContext.java @@ -5,62 +5,62 @@ import java.util.Hashtable; public class EventContext extends EventObject implements Serializable { - public final static String EVENT_TOPIC = "event.topic"; - public final static String EVENT_CALLBACK = "event.call.back"; + public final static String EVENT_TOPIC = "event.topic"; + public final static String EVENT_CALLBACK = "event.call.back"; - protected boolean isInterruptor; + protected boolean isInterrupt; - protected Hashtable eventContext = null; + protected Hashtable eventContext = null; - public EventContext(Object source) { - super(source); - } + public EventContext(Object source) { + super(source); + } - public boolean isInterruptor() { - return isInterruptor; - } + public boolean isInterrupt() { + return isInterrupt; + } - public void setInterruptor(boolean isBroken) { - this.isInterruptor = isBroken; - } + public void setInterrupt(boolean isBroken) { + this.isInterrupt = isBroken; + } - public void setParameter(String key, Object value) { - Hashtable tmpContext = getEventContext(); + public void setParameter(String key, Object value) { + Hashtable tmpContext = getEventContext(); - tmpContext.put(key, value); - } + tmpContext.put(key, value); + } - protected Hashtable getEventContext() { - Hashtable tmpContext = eventContext; - if (tmpContext == null) { - synchronized (this) { - tmpContext = eventContext; - if (tmpContext == null) { - eventContext = new Hashtable<>(); - tmpContext = eventContext; - } - } - } - return tmpContext; - } + protected Hashtable getEventContext() { + Hashtable tmpContext = eventContext; + if (tmpContext == null) { + synchronized (this) { + tmpContext = eventContext; + if (tmpContext == null) { + eventContext = new Hashtable<>(); + tmpContext = eventContext; + } + } + } + return tmpContext; + } - @SuppressWarnings("unchecked") - public T getParameter(String key) { + @SuppressWarnings("unchecked") + public T getParameter(String key) { - return eventContext == null ? null : (T) eventContext.get(key); - } + return eventContext == null ? null : (T) eventContext.get(key); + } - @SuppressWarnings("unchecked") - public T getParameter(String key, T defaultValue) { + @SuppressWarnings("unchecked") + public T getParameter(String key, T defaultValue) { - return eventContext == null ? null - : (T) eventContext.getOrDefault(key, defaultValue); - } + return eventContext == null ? null + : (T) eventContext.getOrDefault(key, defaultValue); + } - public void removeParameter(String key) { - if (eventContext == null) { - return; - } - eventContext.remove(key); - } + public void removeParameter(String key) { + if (eventContext == null) { + return; + } + eventContext.remove(key); + } } \ No newline at end of file diff --git a/event-swift/src/main/java/com/ware/swift/event/ICallbackHook.java b/event-swift/src/main/java/com/ware/swift/event/ICallbackHook.java new file mode 100644 index 0000000..16f9e32 --- /dev/null +++ b/event-swift/src/main/java/com/ware/swift/event/ICallbackHook.java @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2019 the original author or authors. + * + * 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 com.ware.swift.event; + +/** + * @author pbting + * @date 2019-05-19 10:29 AM + */ +public interface ICallbackHook { + + default void callback(V target) { + + } + + /** + * 提供一个空实现。 + */ + class EmptyCallbackHook implements ICallbackHook { + + @Override + public void callback(Object target) { + + // nothing to do + } + } +} diff --git a/event-swift/src/main/java/com/ware/swift/event/common/AtomicLongMap.java b/event-swift/src/main/java/com/ware/swift/event/common/AtomicLongMap.java index e645989..833b820 100644 --- a/event-swift/src/main/java/com/ware/swift/event/common/AtomicLongMap.java +++ b/event-swift/src/main/java/com/ware/swift/event/common/AtomicLongMap.java @@ -15,7 +15,7 @@ *

*

Most methods in this class treat absent values and zero values identically, as individually * documented. Exceptions to this are {@link #containsKey}, {@link #size}, {@link #isEmpty}, - * and {@link #toString}. + * and {@link #toString}. *

*

Instances of this class may be used by multiple threads concurrently. All operations are * atomic unless otherwise noted. @@ -24,6 +24,7 @@ * Warning: Unlike {@code Multiset}, entries whose values are zero are not automatically * removed from the map. Instead they must be removed manually with {@link #removeAllZeros}. * 适用于对某个 key 进行计数的场景。比如 word count 就比较适合 + * * @author Charles Fry * @since 11.0 */ @@ -38,7 +39,7 @@ private AtomicLongMap(ConcurrentHashMap map) { * Creates an {@code AtomicLongMap}. */ public static AtomicLongMap create() { - return new AtomicLongMap(new ConcurrentHashMap()); + return new AtomicLongMap(new ConcurrentHashMap<>()); } /** @@ -292,28 +293,28 @@ public String toString() { return map.toString(); } - /* - * ConcurrentMap operations which we may eventually add. - * - * The problem with these is that remove(K, long) has to be done in two phases by definition --- - * first decrementing to zero, and then removing. putIfAbsent or replace could observe the - * intermediate zero-state. Ways we could deal with this are: - * - * - Don't define any of the ConcurrentMap operations. This is the current state of affairs. - * - * - Define putIfAbsent and replace as treating zero and absent identically (as currently - * implemented below). This is a bit surprising with putIfAbsent, which really becomes - * putIfZero. - * - * - Allow putIfAbsent and replace to distinguish between zero and absent, but don't implement - * remove(K, long). Without any two-phase operations it becomes feasible for all remaining - * operations to distinguish between zero and absent. If we do this, then perhaps we should add - * replace(key, long). - * - * - Introduce a special-value private static final AtomicLong that would have the meaning of - * removal-in-progress, and rework all operations to properly distinguish between zero and - * absent. - */ + /* + * ConcurrentMap operations which we may eventually add. + * + * The problem with these is that remove(K, long) has to be done in two phases by definition --- + * first decrementing to zero, and then removing. putIfAbsent or replace could observe the + * intermediate zero-state. Ways we could deal with this are: + * + * - Don't define any of the ConcurrentMap operations. This is the current state of affairs. + * + * - Define putIfAbsent and replace as treating zero and absent identically (as currently + * implemented below). This is a bit surprising with putIfAbsent, which really becomes + * putIfZero. + * + * - Allow putIfAbsent and replace to distinguish between zero and absent, but don't implement + * remove(K, long). Without any two-phase operations it becomes feasible for all remaining + * operations to distinguish between zero and absent. If we do this, then perhaps we should add + * replace(key, long). + * + * - Introduce a special-value private static final AtomicLong that would have the meaning of + * removal-in-progress, and rework all operations to properly distinguish between zero and + * absent. + */ /** * If {@code key} is not already associated with a value or if {@code key} is associated with @@ -387,7 +388,7 @@ boolean remove(K key, long value) { // value changed return false; } - + // private transient Map asMap; /** @@ -396,13 +397,13 @@ boolean remove(K key, long value) { public Map asMap() { // Map result = asMap; // return (result == null) ? asMap = createAsMap() : result; - return createAsMap(); + return createAsMap(); } private Map createAsMap() { - Map resultMap = new LinkedHashMap(); - if(map != null && !map.isEmpty()){ - for(Entry entry : map.entrySet()){ + Map resultMap = new LinkedHashMap<>(); + if (map != null && !map.isEmpty()) { + for (Entry entry : map.entrySet()) { resultMap.put(entry.getKey(), entry.getValue().get()); } } diff --git a/event-swift/src/main/java/com/ware/swift/event/common/ConcurrentHashSet.java b/event-swift/src/main/java/com/ware/swift/event/common/ConcurrentHashSet.java deleted file mode 100644 index 876e02d..0000000 --- a/event-swift/src/main/java/com/ware/swift/event/common/ConcurrentHashSet.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.ware.swift.event.common; - -import java.util.Collection; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; - -public class ConcurrentHashSet extends MapBackedSet { - - private static final long serialVersionUID = 8518578988740277828L; - - public ConcurrentHashSet() { - super(new ConcurrentHashMap()); - } - - public ConcurrentHashSet(Collection c) { - super(new ConcurrentHashMap(), c); - } - - @Override - public boolean add(E o) { - Boolean answer = ((ConcurrentMap) map).putIfAbsent(o, - Boolean.TRUE); - return answer == null; - } -} diff --git a/event-swift/src/main/java/com/ware/swift/event/common/ConcurrentLinkedHashMap.java b/event-swift/src/main/java/com/ware/swift/event/common/ConcurrentLinkedHashMap.java index e620d4f..8de84c4 100644 --- a/event-swift/src/main/java/com/ware/swift/event/common/ConcurrentLinkedHashMap.java +++ b/event-swift/src/main/java/com/ware/swift/event/common/ConcurrentLinkedHashMap.java @@ -1,228 +1,214 @@ package com.ware.swift.event.common; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedHashMap; -import java.util.Set; +import java.util.*; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.ReentrantReadWriteLock; -public class ConcurrentLinkedHashMap extends LinkedHashMap{ - - /** - * - */ - private static final long serialVersionUID = 1L; - private static int SEGMENT_MASK = 16 ; - private final HashMap conditionMapping = new HashMap(); - - @SuppressWarnings("unchecked") - private LinkedHashMap[] linkedHashMapArray = new LinkedHashMap[SEGMENT_MASK+1]; - - private AtomicInteger size = new AtomicInteger(0); - public ConcurrentLinkedHashMap() { - this(SEGMENT_MASK); - } - - public ConcurrentLinkedHashMap(int segment) { - ConcurrentLinkedHashMap.SEGMENT_MASK = segment; - } - //初始化 16 个 - { - for(int i=0;i< SEGMENT_MASK;i++){ - linkedHashMapArray[i] = new LinkedHashMap(); - conditionMapping.put(i, new ReentrantReadWriteLock()); - } - } - - @Override - public V put(K key, V value) { - size.incrementAndGet(); - int index = getIndex(key); - - LinkedHashMap linkedHashMap = linkedHashMapArray[index]; - ReentrantReadWriteLock lock = conditionMapping.get(index); - V v = null ; - try { - lock.writeLock().tryLock(3, TimeUnit.SECONDS); - v = linkedHashMap.put(key, value); - lock.writeLock().unlock(); - } catch (InterruptedException e) { - } - - return v; - } - - @Override - public V get(Object key) { - int index = getIndex(key); - LinkedHashMap linkedHashMap = linkedHashMapArray[index]; - ReentrantReadWriteLock lock = conditionMapping.get(index); - V v = null ; - lock.readLock().lock(); - v = linkedHashMap.get(key); - lock.readLock().unlock(); - return v; - } - - @Override - public int size() { - - return size.get(); - } - - @Override - public boolean isEmpty() { - return size.get() == 0; - } - - @Override - public V remove(Object key) { - int index = getIndex(key); - LinkedHashMap linkedHashMap = linkedHashMapArray[index]; - ReentrantReadWriteLock lock = conditionMapping.get(index); - V v = null ; - lock.writeLock().lock(); - v = linkedHashMap.remove(key); - lock.writeLock().unlock(); - return v; - } - - @Override - public boolean containsKey(Object key) { - int index = getIndex(key); - LinkedHashMap linkedHashMap = linkedHashMapArray[index]; - ReentrantReadWriteLock lock = conditionMapping.get(index); - boolean v = false ; - lock.readLock().lock(); - v = linkedHashMap.containsKey(key); - lock.readLock().unlock(); - return v; - } - - @Override - public boolean containsValue(Object value) { - boolean result = false ; - for(int i=0;i<=SEGMENT_MASK;i++){ - LinkedHashMap linkedHashMap = linkedHashMapArray[i]; - ReentrantReadWriteLock lock = conditionMapping.get(i); - try{ - lock.readLock().lock(); - if(linkedHashMap.containsValue(value)){ - result = true ; - break; - } - }finally{ - lock.readLock().unlock(); - } - } - return result; - } - - @Override - protected void finalize() throws Throwable { - for(int i=0;i<=SEGMENT_MASK;i++){ - LinkedHashMap linkedHashMap = linkedHashMapArray[i]; - ReentrantReadWriteLock lock = conditionMapping.get(i); - try{ - lock.readLock().lock(); - linkedHashMap.clear(); - linkedHashMap = null ; - linkedHashMapArray[i] = null; - }finally{ - lock.readLock().unlock(); - } - } - } - - @Override - public Collection values() { - Collection values = new ArrayList(); - for(int i=0;i<=SEGMENT_MASK;i++){ - LinkedHashMap linkedHashMap = linkedHashMapArray[i]; - ReentrantReadWriteLock lock = conditionMapping.get(i); - try{ - lock.readLock().lock(); - values.addAll(linkedHashMap.values()); - }finally{ - lock.readLock().unlock(); - } - } - return values; - } - - @Override - public Set keySet() { - Set keySet = new HashSet(); - for(int i=0;i<=SEGMENT_MASK;i++){ - LinkedHashMap linkedHashMap = linkedHashMapArray[i]; - ReentrantReadWriteLock lock = conditionMapping.get(i); - try{ - lock.readLock().lock(); - keySet.addAll(linkedHashMap.keySet()); - }finally{ - lock.readLock().unlock(); - } - } - - return keySet; - } - - @Override - public void clear() { - for(int i=0;i<=SEGMENT_MASK;i++){ - LinkedHashMap linkedHashMap = linkedHashMapArray[i]; - ReentrantReadWriteLock lock = conditionMapping.get(i); - try{ - lock.readLock().lock(); - linkedHashMap.clear(); - }finally{ - lock.readLock().unlock(); - } - } - } - - @Override - public Set> entrySet() { - Set> entry = new HashSet>(); - for(int i=0;i<=SEGMENT_MASK;i++){ - LinkedHashMap linkedHashMap = linkedHashMapArray[i]; - ReentrantReadWriteLock lock = conditionMapping.get(i); - try{ - lock.readLock().lock(); - entry.addAll(linkedHashMap.entrySet()); - }finally{ - lock.readLock().unlock(); - } - } - - return entry; - } - - private int getIndex(Object key){ - - if(isPowerOfTwo(SEGMENT_MASK )){ - return key.hashCode() & SEGMENT_MASK - 1 ; - }else{ - return Math.abs(key.hashCode() % (SEGMENT_MASK )); - } - } - - private boolean isPowerOfTwo(int val) { - return (val & -val) == val; - } - - public static void main(String[] args) { - - ConcurrentLinkedHashMap h = new ConcurrentLinkedHashMap(4); - System.err.println(h.getIndex("A")); - System.err.println(h.getIndex("b")); - System.err.println(h.getIndex("b")); - System.err.println(h.getIndex("c")); - System.err.println(h.getIndex("d")); - } +public class ConcurrentLinkedHashMap extends LinkedHashMap { + + /** + * + */ + private static final long serialVersionUID = 1L; + private static int SEGMENT_MASK = 16; + private final HashMap conditionMapping = new HashMap<>(); + + @SuppressWarnings("unchecked") + private LinkedHashMap[] linkedHashMapArray = new LinkedHashMap[SEGMENT_MASK + 1]; + + private AtomicInteger size = new AtomicInteger(0); + + public ConcurrentLinkedHashMap() { + this(SEGMENT_MASK); + } + + public ConcurrentLinkedHashMap(int segment) { + ConcurrentLinkedHashMap.SEGMENT_MASK = segment; + } + + //初始化 16 个 + { + for (int i = 0; i < SEGMENT_MASK; i++) { + linkedHashMapArray[i] = new LinkedHashMap<>(); + conditionMapping.put(i, new ReentrantReadWriteLock()); + } + } + + @Override + public V put(K key, V value) { + size.incrementAndGet(); + int index = getIndex(key); + + LinkedHashMap linkedHashMap = linkedHashMapArray[index]; + ReentrantReadWriteLock lock = conditionMapping.get(index); + V v = null; + try { + lock.writeLock().tryLock(3, TimeUnit.SECONDS); + v = linkedHashMap.put(key, value); + lock.writeLock().unlock(); + } catch (InterruptedException e) { + } + + return v; + } + + @Override + public V get(Object key) { + int index = getIndex(key); + LinkedHashMap linkedHashMap = linkedHashMapArray[index]; + ReentrantReadWriteLock lock = conditionMapping.get(index); + V v = null; + lock.readLock().lock(); + v = linkedHashMap.get(key); + lock.readLock().unlock(); + return v; + } + + @Override + public int size() { + + return size.get(); + } + + @Override + public boolean isEmpty() { + return size.get() == 0; + } + + @Override + public V remove(Object key) { + int index = getIndex(key); + LinkedHashMap linkedHashMap = linkedHashMapArray[index]; + ReentrantReadWriteLock lock = conditionMapping.get(index); + lock.writeLock().lock(); + V v = linkedHashMap.remove(key); + lock.writeLock().unlock(); + return v; + } + + @Override + public boolean containsKey(Object key) { + int index = getIndex(key); + LinkedHashMap linkedHashMap = linkedHashMapArray[index]; + ReentrantReadWriteLock lock = conditionMapping.get(index); + boolean v = false; + lock.readLock().lock(); + v = linkedHashMap.containsKey(key); + lock.readLock().unlock(); + return v; + } + + @Override + public boolean containsValue(Object value) { + boolean result = false; + for (int i = 0; i <= SEGMENT_MASK; i++) { + LinkedHashMap linkedHashMap = linkedHashMapArray[i]; + ReentrantReadWriteLock lock = conditionMapping.get(i); + try { + lock.readLock().lock(); + if (linkedHashMap.containsValue(value)) { + result = true; + break; + } + } finally { + lock.readLock().unlock(); + } + } + return result; + } + + @Override + protected void finalize() throws Throwable { + for (int i = 0; i <= SEGMENT_MASK; i++) { + LinkedHashMap linkedHashMap = linkedHashMapArray[i]; + ReentrantReadWriteLock lock = conditionMapping.get(i); + try { + lock.readLock().lock(); + linkedHashMap.clear(); + linkedHashMapArray[i] = null; + } finally { + lock.readLock().unlock(); + } + } + } + + @Override + public Collection values() { + Collection values = new ArrayList(); + for (int i = 0; i <= SEGMENT_MASK; i++) { + LinkedHashMap linkedHashMap = linkedHashMapArray[i]; + ReentrantReadWriteLock lock = conditionMapping.get(i); + try { + lock.readLock().lock(); + values.addAll(linkedHashMap.values()); + } finally { + lock.readLock().unlock(); + } + } + return values; + } + + @Override + public Set keySet() { + Set keySet = new HashSet(); + for (int i = 0; i <= SEGMENT_MASK; i++) { + LinkedHashMap linkedHashMap = linkedHashMapArray[i]; + ReentrantReadWriteLock lock = conditionMapping.get(i); + try { + lock.readLock().lock(); + keySet.addAll(linkedHashMap.keySet()); + } finally { + lock.readLock().unlock(); + } + } + + return keySet; + } + + @Override + public void clear() { + for (int i = 0; i <= SEGMENT_MASK; i++) { + LinkedHashMap linkedHashMap = linkedHashMapArray[i]; + ReentrantReadWriteLock lock = conditionMapping.get(i); + try { + lock.readLock().lock(); + linkedHashMap.clear(); + } finally { + lock.readLock().unlock(); + } + } + } + + @Override + public Set> entrySet() { + Set> entry = new HashSet<>(); + for (int i = 0; i <= SEGMENT_MASK; i++) { + LinkedHashMap linkedHashMap = linkedHashMapArray[i]; + ReentrantReadWriteLock lock = conditionMapping.get(i); + try { + lock.readLock().lock(); + entry.addAll(linkedHashMap.entrySet()); + } finally { + lock.readLock().unlock(); + } + } + + return entry; + } + + private int getIndex(Object key) { + + if (isPowerOfTwo(SEGMENT_MASK)) { + return key.hashCode() & SEGMENT_MASK - 1; + } else { + return Math.abs(key.hashCode() % (SEGMENT_MASK)); + } + } + + private boolean isPowerOfTwo(int val) { + return (val & -val) == val; + } + } diff --git a/event-swift/src/main/java/com/ware/swift/event/graph/future/FutureResult.java b/event-swift/src/main/java/com/ware/swift/event/common/FutureResult.java similarity index 93% rename from event-swift/src/main/java/com/ware/swift/event/graph/future/FutureResult.java rename to event-swift/src/main/java/com/ware/swift/event/common/FutureResult.java index 386cffa..0249858 100644 --- a/event-swift/src/main/java/com/ware/swift/event/graph/future/FutureResult.java +++ b/event-swift/src/main/java/com/ware/swift/event/common/FutureResult.java @@ -1,4 +1,4 @@ -package com.ware.swift.event.graph.future; +package com.ware.swift.event.common; public class FutureResult { diff --git a/event-swift/src/main/java/com/ware/swift/event/common/IdempotentConfirmer.java b/event-swift/src/main/java/com/ware/swift/event/common/IdempotentConfirmer.java deleted file mode 100644 index 9c8c36d..0000000 --- a/event-swift/src/main/java/com/ware/swift/event/common/IdempotentConfirmer.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.ware.swift.event.common; - -/** - * 幂等操作器 - * Created by yijunzhang on 14-10-22. - */ -public abstract class IdempotentConfirmer { - private int retry = 3; - - protected IdempotentConfirmer(int retry) { - this.retry = retry; - } - - public IdempotentConfirmer() { - } - - public abstract boolean execute(); - - public boolean run() { - while (retry-- > 0) { - try { - boolean isOk = execute(); - if (isOk){ - return true; - } - } catch (Exception e) { - Log.error(e.getMessage(), e); - continue; - } - } - return false; - } -} - diff --git a/event-swift/src/main/java/com/ware/swift/event/common/MapBackedSet.java b/event-swift/src/main/java/com/ware/swift/event/common/MapBackedSet.java deleted file mode 100644 index c202485..0000000 --- a/event-swift/src/main/java/com/ware/swift/event/common/MapBackedSet.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.ware.swift.event.common; - -import java.io.Serializable; -import java.util.AbstractSet; -import java.util.Collection; -import java.util.Iterator; -import java.util.Map; - -public class MapBackedSet extends AbstractSet implements Serializable { - - private static final long serialVersionUID = -8347878570391674042L; - - protected final Map map; - - public MapBackedSet(Map map) { - this.map = map; - } - - public MapBackedSet(Map map, Collection c) { - this.map = map; - addAll(c); - } - - @Override - public int size() { - return map.size(); - } - - @Override - public boolean contains(Object o) { - return map.containsKey(o); - } - - @Override - public Iterator iterator() { - return map.keySet().iterator(); - } - - @Override - public boolean add(E o) { - return map.put(o, Boolean.TRUE) == null; - } - - @Override - public boolean remove(Object o) { - return map.remove(o) != null; - } - - @Override - public void clear() { - map.clear(); - } -} \ No newline at end of file diff --git a/event-swift/src/main/java/com/ware/swift/event/disruptor/DisruptorParallelActionExecutor.java b/event-swift/src/main/java/com/ware/swift/event/disruptor/DisruptorParallelActionExecutor.java index 1154b09..f4ea087 100644 --- a/event-swift/src/main/java/com/ware/swift/event/disruptor/DisruptorParallelActionExecutor.java +++ b/event-swift/src/main/java/com/ware/swift/event/disruptor/DisruptorParallelActionExecutor.java @@ -1,13 +1,13 @@ package com.ware.swift.event.disruptor; -import com.ware.swift.event.parallel.action.AbstractParallelActionExecutor; -import com.ware.swift.event.parallel.action.Action; -import com.ware.swift.event.parallel.action.ActionBridge; -import com.ware.swift.event.parallel.action.IActionQueue; import com.lmax.disruptor.*; import com.lmax.disruptor.dsl.Disruptor; import com.lmax.disruptor.dsl.ProducerType; import com.lmax.disruptor.util.DaemonThreadFactory; +import com.ware.swift.event.parallel.action.AbstractParallelActionExecutor; +import com.ware.swift.event.parallel.action.Action; +import com.ware.swift.event.parallel.action.ActionBridge; +import com.ware.swift.event.parallel.action.IActionQueue; import java.io.Serializable; import java.util.concurrent.ConcurrentHashMap; @@ -16,231 +16,222 @@ import java.util.concurrent.locks.ReentrantLock; /** - * * @author pengbingting - * */ public class DisruptorParallelActionExecutor extends AbstractParallelActionExecutor { - private ActionEventTranslator translator = new ActionEventTranslator(); - @SuppressWarnings("rawtypes") - private Disruptor[] disruptors = null; - private ThreadPoolExecutorIndexAllocator ringBufferExecutorIndexChooser; - private ReentrantLock lock = new ReentrantLock(true); - private ConcurrentMap> paralleleQueue = new ConcurrentHashMap>(); - private AtomicBoolean isOperating = new AtomicBoolean(false); - private AtomicBoolean isCronTrriger = new AtomicBoolean(false); - private Sequence actionQueueSize = new Sequence(0); - - public DisruptorParallelActionExecutor(int queueCount, int ringBufferSize) { - if (queueCount <= 0) { - queueCount = DEFAULT_QUEUE_SIZE; - } - - disruptors = new Disruptor[queueCount]; - for (int i = 0; i < queueCount; i++) { - // 鍗婁釜灏忔椂涔嬪悗杩樻病鏈変换鍔¤繃鏉ワ紝鍒欓攢姣佺嚎绋� - disruptors[i] = builderDisruptor(ringBufferSize); - } - - if (isPowerOfTwo(queueCount)) { - ringBufferExecutorIndexChooser = new PowerOfTwoExecutorIndexChooser( - queueCount); - } - else { - ringBufferExecutorIndexChooser = new GenericExecutorIndexChooser(queueCount); - } - - } - - @Override - public void execute(Runnable command) { - execute(command.getClass().getName(), command); - } - - /** - * 濡傛灉杩欓噷涓嶅寘瑁呮垚涓�涓狝ction锛屽垯璺烻uperFastParallelQueueExecutor 娌′粈涔堝澶х殑鍖哄埆 - */ - @Override - public void execute(String topic, Runnable command) { - if (!(command instanceof Action)) { - ActionBridge actionBridge = new ActionBridge(command); - enParallelAction(topic, actionBridge); - } - else { - Action action = (Action) command; - enParallelAction(topic, action); - } - } - - @SuppressWarnings("unchecked") - private Disruptor builderDisruptor(int ringBufferSize) { - Disruptor disruptor = new Disruptor( - new ActionEventFactory(), ringBufferSize, DaemonThreadFactory.INSTANCE, - ProducerType.SINGLE, new LiteBlockingWaitStrategy()); - disruptor.handleEventsWith(new ActionEventHandler()); - disruptor.start(); - return disruptor; - } - - public static class ActionEventTranslator - implements EventTranslatorOneArg { - - public void translateTo(ActionEvent event, long sequence, Action value) { - event.setValue(value); - } - } - - public static class ActionEventFactory implements EventFactory { - - public ActionEvent newInstance() { - - return new ActionEvent(); - } - } - - public static class ActionEvent implements Serializable { - - /** - * - */ - private static final long serialVersionUID = 1L; - - private Action value; - - public ActionEvent() { - } - - public Action getValue() { - return value; - } - - public void setValue(Action value) { - this.value = value; - } - } - - public static class ActionEventHandler implements EventHandler { - - public void onEvent(ActionEvent arg0, long arg1, boolean arg2) throws Exception { - Action action = arg0.getValue(); - action.run(); - arg0.setValue(action); - action = null; - } - } - - @SuppressWarnings("unchecked") - @Override - public void enParallelAction(String queueTopic, Action action) { - // 1、 - IActionQueue actionQueue = paralleleQueue.get(queueTopic); - // 2、 - if (actionQueue == null) { - lock.lock(); - try { - isOperating.set(true); - actionQueue = paralleleQueue.get(queueTopic); - if (actionQueue == null) { - int index = ringBufferExecutorIndexChooser.allocator(); - actionQueue = new DisruptorParallelActionQueue(disruptors[index], - this.translator); - paralleleQueue.put(queueTopic, actionQueue); - actionQueueSize.addAndGet(1); - } - } - finally { - isOperating.set(false); - lock.unlock(); - } - } - - // 3、 - action.setTopicName(queueTopic); - - // 4、 - trrigerWithRejectActionPolicy(actionQueue, action); - } - - @Override - public void removeParallelAction(String queueTopic) { - removeTopic(queueTopic); - } - - @Override - public void adjustPoolSize(int newCorePoolSize, int newMaxiPoolSize) { - - throw new UnsupportedOperationException(); - } - - @SuppressWarnings("unchecked") - @Override - public void stop() { - if (disruptors == null) { - return; - } - - for (Disruptor disruptor : disruptors) { - disruptor.shutdown(); - } - - disruptors = null; - super.stop(); - } - - @SuppressWarnings("unchecked") - @Override - public void registerTopics(String... topics) { - if (topics == null || topics.length == 0) { - return; - } - - try { - lock.lock(); - for (String queueTopic : topics) { - IActionQueue actionQueue = paralleleQueue.get(queueTopic); - if (actionQueue != null) { - continue; - } - - int index = ringBufferExecutorIndexChooser.allocator(); - actionQueue = new DisruptorParallelActionQueue(disruptors[index], - this.translator); - paralleleQueue.put(queueTopic, actionQueue); - } - } - finally { - lock.unlock(); - } - } - - @Override - public void removeTopic(String topic) { - - if (paralleleQueue.remove(topic) != null) { - actionQueueSize.addAndGet(-1); - } - } - - @Override - public void executeOneTime(Runnable command) { - - if (command instanceof Action) { - Action action = (Action) command; - executeOneTimeAction(action); - } - else { - ActionBridge actionBridge = new ActionBridge(command); - executeOneTimeAction(actionBridge); - } - } - - @Override - public void executeOneTimeAction(Action action) { - @SuppressWarnings("unchecked") - IActionQueue actionQueue = new DisruptorParallelActionQueue( - disruptors[ringBufferExecutorIndexChooser.allocator()], this.translator); - actionQueue.enqueue(action); - } + private ActionEventTranslator translator = new ActionEventTranslator(); + @SuppressWarnings("rawtypes") + private Disruptor[] disruptorArray; + private ThreadPoolExecutorIndexAllocator ringBufferExecutorIndexChooser; + private ReentrantLock lock = new ReentrantLock(true); + private ConcurrentMap> parallelQueue = new ConcurrentHashMap<>(); + private AtomicBoolean isOperating = new AtomicBoolean(false); + private Sequence actionQueueSize = new Sequence(0); + + public DisruptorParallelActionExecutor(int queueCount, int ringBufferSize) { + if (queueCount <= 0) { + queueCount = DEFAULT_QUEUE_SIZE; + } + + disruptorArray = new Disruptor[queueCount]; + for (int i = 0; i < queueCount; i++) { + disruptorArray[i] = builderDisruptor(ringBufferSize); + } + + if (isPowerOfTwo(queueCount)) { + ringBufferExecutorIndexChooser = new PowerOfTwoExecutorIndexChooser( + queueCount); + } else { + ringBufferExecutorIndexChooser = new GenericExecutorIndexChooser(queueCount); + } + + } + + @Override + public void execute(Runnable command) { + execute(command.getClass().getName(), command); + } + + /** + * 濡傛灉杩欓噷涓嶅寘瑁呮垚涓�涓狝ction锛屽垯璺烻uperFastParallelQueueExecutor 娌′粈涔堝澶х殑鍖哄埆 + */ + @Override + public void execute(String topic, Runnable command) { + if (!(command instanceof Action)) { + ActionBridge actionBridge = new ActionBridge(command); + enParallelAction(topic, actionBridge); + } else { + Action action = (Action) command; + enParallelAction(topic, action); + } + } + + @SuppressWarnings("unchecked") + private Disruptor builderDisruptor(int ringBufferSize) { + Disruptor disruptor = new Disruptor<>( + new ActionEventFactory(), ringBufferSize, DaemonThreadFactory.INSTANCE, + ProducerType.SINGLE, new LiteBlockingWaitStrategy()); + disruptor.handleEventsWith(new ActionEventHandler()); + disruptor.start(); + return disruptor; + } + + public static class ActionEventTranslator + implements EventTranslatorOneArg { + + public void translateTo(ActionEvent event, long sequence, Action value) { + event.setValue(value); + } + } + + public static class ActionEventFactory implements EventFactory { + + public ActionEvent newInstance() { + + return new ActionEvent(); + } + } + + public static class ActionEvent implements Serializable { + + /** + * + */ + private static final long serialVersionUID = 1L; + + private Action value; + + public ActionEvent() { + } + + public Action getValue() { + return value; + } + + public void setValue(Action value) { + this.value = value; + } + } + + public static class ActionEventHandler implements EventHandler { + + public void onEvent(ActionEvent arg0, long arg1, boolean arg2) throws Exception { + Action action = arg0.getValue(); + action.run(); + arg0.setValue(action); + action = null; + } + } + + @SuppressWarnings("unchecked") + @Override + public void enParallelAction(String queueTopic, Action action) { + // 1、 + IActionQueue actionQueue = parallelQueue.get(queueTopic); + // 2、 + if (actionQueue == null) { + lock.lock(); + try { + isOperating.set(true); + actionQueue = parallelQueue.get(queueTopic); + if (actionQueue == null) { + int index = ringBufferExecutorIndexChooser.allocator(); + actionQueue = new DisruptorParallelActionQueue(disruptorArray[index], + this.translator); + parallelQueue.put(queueTopic, actionQueue); + actionQueueSize.addAndGet(1); + } + } finally { + isOperating.set(false); + lock.unlock(); + } + } + + // 3、 + action.setTopicName(queueTopic); + + // 4、 + trrigerWithRejectActionPolicy(actionQueue, action); + } + + @Override + public void removeParallelAction(String queueTopic) { + removeTopic(queueTopic); + } + + @Override + public void adjustPoolSize(int newCorePoolSize, int newMaxiPoolSize) { + + throw new UnsupportedOperationException(); + } + + @SuppressWarnings("unchecked") + @Override + public void stop() { + if (disruptorArray == null) { + return; + } + + for (Disruptor disruptor : disruptorArray) { + disruptor.shutdown(); + } + + disruptorArray = null; + super.stop(); + } + + @SuppressWarnings("unchecked") + @Override + public void registerTopics(String... topics) { + if (topics == null || topics.length == 0) { + return; + } + + try { + lock.lock(); + for (String queueTopic : topics) { + IActionQueue actionQueue = parallelQueue.get(queueTopic); + if (actionQueue != null) { + continue; + } + + int index = ringBufferExecutorIndexChooser.allocator(); + actionQueue = new DisruptorParallelActionQueue(disruptorArray[index], + this.translator); + parallelQueue.put(queueTopic, actionQueue); + } + } finally { + lock.unlock(); + } + } + + @Override + public void removeTopic(String topic) { + + if (parallelQueue.remove(topic) != null) { + actionQueueSize.addAndGet(-1); + } + } + + @Override + public void executeOneTime(Runnable command) { + + if (command instanceof Action) { + Action action = (Action) command; + executeOneTimeAction(action); + } else { + ActionBridge actionBridge = new ActionBridge(command); + executeOneTimeAction(actionBridge); + } + } + + @Override + public void executeOneTimeAction(Action action) { + @SuppressWarnings("unchecked") + IActionQueue actionQueue = new DisruptorParallelActionQueue( + disruptorArray[ringBufferExecutorIndexChooser.allocator()], this.translator); + actionQueue.enqueue(action); + } } \ No newline at end of file diff --git a/event-swift/src/main/java/com/ware/swift/event/disruptor/DisruptorParallelActionQueue.java b/event-swift/src/main/java/com/ware/swift/event/disruptor/DisruptorParallelActionQueue.java index b692581..c2f387b 100644 --- a/event-swift/src/main/java/com/ware/swift/event/disruptor/DisruptorParallelActionQueue.java +++ b/event-swift/src/main/java/com/ware/swift/event/disruptor/DisruptorParallelActionQueue.java @@ -1,35 +1,35 @@ package com.ware.swift.event.disruptor; +import com.lmax.disruptor.EventTranslatorOneArg; +import com.lmax.disruptor.dsl.Disruptor; import com.ware.swift.event.parallel.action.AbstractActionQueue; import com.ware.swift.event.parallel.action.Action; + import java.util.LinkedList; import java.util.Queue; -import com.lmax.disruptor.EventTranslatorOneArg; -import com.lmax.disruptor.dsl.Disruptor; - class DisruptorParallelActionQueue extends AbstractActionQueue { - private Disruptor disruptor ; - private EventTranslatorOneArg translator; - - public DisruptorParallelActionQueue(Queue queue, Disruptor disruptor) { - super(queue); - this.disruptor = disruptor; - } + private Disruptor disruptor; + private EventTranslatorOneArg translator; + + public DisruptorParallelActionQueue(Queue queue, Disruptor disruptor) { + super(queue); + this.disruptor = disruptor; + } + + public DisruptorParallelActionQueue(Disruptor ringBuffer, EventTranslatorOneArg translator) { + super(new LinkedList<>()); + this.disruptor = ringBuffer; + this.translator = translator; + } - public DisruptorParallelActionQueue(Disruptor ringBuffer, EventTranslatorOneArg translator) { - super(new LinkedList()); - this.disruptor = ringBuffer; - this.translator = translator; - } - - public DisruptorParallelActionQueue(Queue queue) { - super(queue); - } + public DisruptorParallelActionQueue(Queue queue) { + super(queue); + } - @Override - public void doExecute(Runnable runnable) { - Action action = (Action) runnable; - disruptor.publishEvent(translator, action); - } + @Override + public void doExecute(Runnable runnable) { + Action action = (Action) runnable; + disruptor.publishEvent(translator, action); + } } diff --git a/event-swift/src/main/java/com/ware/swift/event/disruptor/DisruptorParallelQueueExecutor.java b/event-swift/src/main/java/com/ware/swift/event/disruptor/DisruptorParallelQueueExecutor.java index 7bb85b4..5836a48 100644 --- a/event-swift/src/main/java/com/ware/swift/event/disruptor/DisruptorParallelQueueExecutor.java +++ b/event-swift/src/main/java/com/ware/swift/event/disruptor/DisruptorParallelQueueExecutor.java @@ -1,6 +1,5 @@ package com.ware.swift.event.disruptor; -import com.ware.swift.event.common.AtomicLongMap; import com.lmax.disruptor.EventFactory; import com.lmax.disruptor.EventHandler; import com.lmax.disruptor.EventTranslatorOneArg; @@ -8,6 +7,7 @@ import com.lmax.disruptor.dsl.Disruptor; import com.lmax.disruptor.dsl.ProducerType; import com.lmax.disruptor.util.DaemonThreadFactory; +import com.ware.swift.event.common.AtomicLongMap; import com.ware.swift.event.parallel.AbstractParallelQueueExecutor; import java.io.Serializable; @@ -19,184 +19,174 @@ public class DisruptorParallelQueueExecutor extends AbstractParallelQueueExecutor { - private RunnableEventTranslator translator = new RunnableEventTranslator(); - - private Disruptor[] disruptors; - private ThreadPoolExecutorIndexAllocator ringBufferExecutorIndexChooser; - private ConcurrentMap queueExecutorMapping = new ConcurrentHashMap(); - private ReentrantLock lock = new ReentrantLock(true); - private AtomicLongMap topicLastExecuteTime = AtomicLongMap.create(); - private AtomicBoolean isOperating = new AtomicBoolean(false); - private AtomicBoolean isCronTrriger = new AtomicBoolean(false); - - @SuppressWarnings("unchecked") - public DisruptorParallelQueueExecutor(int queueCount, int ringBufferSize) { - if (queueCount <= 0) { - queueCount = DEFAULT_QUEUE_SIZE; - } - - disruptors = new Disruptor[queueCount]; - for (int i = 0; i < queueCount; i++) { - // 半个小时之后还没有任务过来,则销毁线程 - disruptors[i] = builderDisruptor(ringBufferSize); - } - - if (isPowerOfTwo(queueCount)) { - ringBufferExecutorIndexChooser = new PowerOfTwoExecutorIndexChooser( - queueCount); - } - else { - ringBufferExecutorIndexChooser = new GenericExecutorIndexChooser(queueCount); - } - - } - - @Override - public void execute(String topic, Runnable command) { - Integer index = queueExecutorMapping.get(topic); - if (index == null) { - try { - lock.tryLock(3, TimeUnit.SECONDS); - isOperating.set(true); - index = queueExecutorMapping.get(topic); - if (index == null) { - index = ringBufferExecutorIndexChooser.allocator(); - queueExecutorMapping.put(topic, index); - } - } - catch (Exception e) { - index = ringBufferExecutorIndexChooser.allocator(); - } - finally { - isOperating.set(false); - lock.unlock(); - } - } - Disruptor ringBuffer = disruptors[index]; - runing(ringBuffer, command); - topicLastExecuteTime.put(topic, System.currentTimeMillis()); - } - - @Override - public void execute(Runnable command) { - String topic = command.getClass().getName(); - execute(topic, command); - } - - private void runing(Disruptor disruptor, Runnable task) { - disruptor.publishEvent(translator, task); - } - - @SuppressWarnings("unchecked") - private Disruptor builderDisruptor(int ringBufferSize) { - Disruptor disruptor = new Disruptor( - new StringEventFactory(), ringBufferSize, DaemonThreadFactory.INSTANCE, - ProducerType.SINGLE, new LiteBlockingWaitStrategy()); - disruptor.handleEventsWith(new RunnableEventHandler()); - disruptor.start(); - return disruptor; - } - - public static class RunnableEventTranslator - implements EventTranslatorOneArg { - - public void translateTo(RunnableEvent event, long sequence, Runnable arg0) { - event.setValue(arg0); - } - } - - public static class StringEventFactory implements EventFactory { - - public RunnableEvent newInstance() { - - return new RunnableEvent(); - } - } - - public static class RunnableWapper { - - } - - public static class RunnableEvent implements Serializable { - - /** - * - */ - private static final long serialVersionUID = 1L; - - private Runnable value; - - public RunnableEvent() { - } - - public Runnable getValue() { - return value; - } - - public void setValue(Runnable value) { - this.value = value; - } - } - - public static class RunnableEventHandler implements EventHandler { - - public void onEvent(RunnableEvent arg0, long arg1, boolean arg2) - throws Exception { - Runnable runnable = arg0.getValue(); - runnable.run(); - arg0.setValue(null); - } - } - - @Override - public void stop() { - if (disruptors == null) { - return; - } - - for (Disruptor disruptor : disruptors) { - disruptor.shutdown(); - } - - disruptors = null; - super.stop(); - } - - @Override - public void registerTopics(String... topics) { - if (topics == null || topics.length == 0) { - return; - } - - try { - lock.tryLock(3, TimeUnit.SECONDS); - for (String topic : topics) { - Integer index = queueExecutorMapping.get(topic); - if (index != null) { - continue; - } - - index = ringBufferExecutorIndexChooser.allocator(); - queueExecutorMapping.put(topic, index); - } - } - catch (Exception e) { - } - finally { - lock.unlock(); - } - } - - @Override - public void removeTopic(String topic) { - - queueExecutorMapping.remove(topic); - } - - @Override - public void executeOneTime(Runnable command) { - Disruptor ringBuffer = disruptors[ringBufferExecutorIndexChooser - .allocator()]; - runing(ringBuffer, command); - } + private RunnableEventTranslator translator = new RunnableEventTranslator(); + + private Disruptor[] disruptorArray; + private ThreadPoolExecutorIndexAllocator ringBufferExecutorIndexChooser; + private ConcurrentMap queueExecutorMapping = new ConcurrentHashMap<>(); + private ReentrantLock lock = new ReentrantLock(true); + private AtomicLongMap topicLastExecuteTime = AtomicLongMap.create(); + private AtomicBoolean isOperating = new AtomicBoolean(false); + + @SuppressWarnings("unchecked") + public DisruptorParallelQueueExecutor(int queueCount, int ringBufferSize) { + if (queueCount <= 0) { + queueCount = DEFAULT_QUEUE_SIZE; + } + + disruptorArray = new Disruptor[queueCount]; + for (int i = 0; i < queueCount; i++) { + // 半个小时之后还没有任务过来,则销毁线程 + disruptorArray[i] = builderDisruptor(ringBufferSize); + } + + if (isPowerOfTwo(queueCount)) { + ringBufferExecutorIndexChooser = new PowerOfTwoExecutorIndexChooser( + queueCount); + } else { + ringBufferExecutorIndexChooser = new GenericExecutorIndexChooser(queueCount); + } + + } + + @Override + public void execute(String topic, Runnable command) { + Integer index = queueExecutorMapping.get(topic); + if (index == null) { + try { + lock.tryLock(3, TimeUnit.SECONDS); + isOperating.set(true); + index = queueExecutorMapping.get(topic); + if (index == null) { + index = ringBufferExecutorIndexChooser.allocator(); + queueExecutorMapping.put(topic, index); + } + } catch (Exception e) { + index = ringBufferExecutorIndexChooser.allocator(); + } finally { + isOperating.set(false); + lock.unlock(); + } + } + Disruptor ringBuffer = disruptorArray[index]; + runing(ringBuffer, command); + topicLastExecuteTime.put(topic, System.currentTimeMillis()); + } + + @Override + public void execute(Runnable command) { + String topic = command.getClass().getName(); + execute(topic, command); + } + + private void runing(Disruptor disruptor, Runnable task) { + disruptor.publishEvent(translator, task); + } + + @SuppressWarnings("unchecked") + private Disruptor builderDisruptor(int ringBufferSize) { + Disruptor disruptor = new Disruptor( + new StringEventFactory(), ringBufferSize, DaemonThreadFactory.INSTANCE, + ProducerType.SINGLE, new LiteBlockingWaitStrategy()); + disruptor.handleEventsWith(new RunnableEventHandler()); + disruptor.start(); + return disruptor; + } + + public static class RunnableEventTranslator + implements EventTranslatorOneArg { + + public void translateTo(RunnableEvent event, long sequence, Runnable arg0) { + event.setValue(arg0); + } + } + + public static class StringEventFactory implements EventFactory { + + public RunnableEvent newInstance() { + + return new RunnableEvent(); + } + } + + public static class RunnableEvent implements Serializable { + + /** + * + */ + private static final long serialVersionUID = 1L; + + private Runnable value; + + public RunnableEvent() { + } + + public Runnable getValue() { + return value; + } + + public void setValue(Runnable value) { + this.value = value; + } + } + + public static class RunnableEventHandler implements EventHandler { + + public void onEvent(RunnableEvent arg0, long arg1, boolean arg2) + throws Exception { + Runnable runnable = arg0.getValue(); + runnable.run(); + arg0.setValue(null); + } + } + + @Override + public void stop() { + if (disruptorArray == null) { + return; + } + + for (Disruptor disruptor : disruptorArray) { + disruptor.shutdown(); + } + + disruptorArray = null; + super.stop(); + } + + @Override + public void registerTopics(String... topics) { + if (topics == null || topics.length == 0) { + return; + } + + try { + lock.tryLock(3, TimeUnit.SECONDS); + for (String topic : topics) { + Integer index = queueExecutorMapping.get(topic); + if (index != null) { + continue; + } + + index = ringBufferExecutorIndexChooser.allocator(); + queueExecutorMapping.put(topic, index); + } + } catch (Exception e) { + } finally { + lock.unlock(); + } + } + + @Override + public void removeTopic(String topic) { + + queueExecutorMapping.remove(topic); + } + + @Override + public void executeOneTime(Runnable command) { + Disruptor ringBuffer = disruptorArray[ringBufferExecutorIndexChooser + .allocator()]; + runing(ringBuffer, command); + } } diff --git a/event-swift/src/main/java/com/ware/swift/event/graph/AbstractNodeCondition.java b/event-swift/src/main/java/com/ware/swift/event/graph/AbstractNodeCondition.java deleted file mode 100644 index 279388a..0000000 --- a/event-swift/src/main/java/com/ware/swift/event/graph/AbstractNodeCondition.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.ware.swift.event.graph; - -import java.util.Iterator; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.atomic.AtomicInteger; - -import com.ware.swift.event.BaseCondition; - -public abstract class AbstractNodeCondition extends BaseCondition, Set>> { - - protected GraphScheduler scheduler ; - protected ConcurrentHashMap,AtomicInteger> messageCountMap = null; - protected int messageTotal = 0; - - /** - * - * @param schedule - * @param observiable graph 中 当前的这个 node - * @param v - */ - public AbstractNodeCondition(GraphScheduler schedule, Node observiable, Set> v) { - super(observiable, v); - this.scheduler = schedule ; - if(v!=null){ - this.messageCountMap = new ConcurrentHashMap, AtomicInteger>(v.size()); - this.messageTotal = v.size(); - Iterator> iter = getValue().iterator(); - while(iter.hasNext()){ - Node tmp = iter.next(); - messageCountMap.put(tmp, new AtomicInteger(0)); - } - } - } - - @Override - public abstract boolean isFinished() ; - - public abstract void increMessageCount(Node targetJobNode); - - @Override - public void handler() { - if(this.isFinished()){ - //一次消息消费成功,对每一个前驱节点的消息数减一。 - for(AtomicInteger value : messageCountMap.values()){ - if(value.get()>=1){ - value.decrementAndGet(); - } - } - Node observiable = getObserviable(); - GraphUtils.submit(new GraphRunnable(observiable, scheduler)); - } - } -} diff --git a/event-swift/src/main/java/com/ware/swift/event/graph/Graph.java b/event-swift/src/main/java/com/ware/swift/event/graph/Graph.java deleted file mode 100644 index c1cbb9d..0000000 --- a/event-swift/src/main/java/com/ware/swift/event/graph/Graph.java +++ /dev/null @@ -1,108 +0,0 @@ -package com.ware.swift.event.graph; - -import java.util.Collections; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.locks.ReentrantLock; - -public class Graph { - - private Node startNode ; - // 图中节点的集合 - private Set> vertexSet = new HashSet>(); - // 相邻的节点,纪录边 start -> end - private Map, Set>> adjaNode = new ConcurrentHashMap, Set>>(); - // 记录: end -> start - private Map, Set>> reverseAdjaNode = new ConcurrentHashMap, Set>>(); - - private ReentrantLock lock = new ReentrantLock(); - - public Graph(Node startNode) { - super(); - this.startNode = startNode; - } - - // 将节点加入图中 - public boolean addNode(Node start, Node end) { - //1、save all of vertex - lock.lock(); - try{ - if (!vertexSet.contains(start)) { - vertexSet.add(start); - } - if (!vertexSet.contains(end)) { - vertexSet.add(end); - } - }finally{ - lock.unlock(); - } - - //2、save the relation of start -> end relation - if (adjaNode.containsKey(start) - && adjaNode.get(start).contains(end)) { - return false; - } - if (adjaNode.containsKey(start)) { - adjaNode.get(start).add(end); - } else { - Set> temp = new HashSet>(); - temp.add(end); - adjaNode.put(start, temp); - } - - //3、save the relation of end -> start relation - if(reverseAdjaNode.containsKey(end)&&reverseAdjaNode.get(end).contains(start)){ - return false; - } - if(reverseAdjaNode.containsKey(end)){ - reverseAdjaNode.get(end).add(start); - }else{ - Set> temp = new HashSet>(); - temp.add(start); - reverseAdjaNode.put(end, temp); - } - - end.setPathIn(end.getPathIn()+1); - return true; - } - - /** - * 得到所有的节点 - * @return - */ - public Set> getVertexSet() { - return Collections.unmodifiableSet(vertexSet); - } - /** - * 得到每一个节点的下一个节点 集合:forexample - * - * /B 一 D\ - * A F - * \C 一 E/ - * - * @return - */ - public Map, Set>> getAdjaNode() { - return Collections.unmodifiableMap(adjaNode); - } - - /** - * 得到每一个节点的前驱节点 集合。 - * 当一个节点收到一个消息时,判断这个节点是否能够触发具体的业务逻辑,需要判断是否全部收到他的前驱节点发过来的消息。这个时候是非常有用的。 - * @return - */ - public Map, Set>> getReverseAdjaNode() { - return Collections.unmodifiableMap(reverseAdjaNode); - } - - public Node getStartNode() { - return startNode; - } - - public void setStartNode(Node startNode) { - this.startNode = startNode; - } - -} diff --git a/event-swift/src/main/java/com/ware/swift/event/graph/GraphRunnable.java b/event-swift/src/main/java/com/ware/swift/event/graph/GraphRunnable.java deleted file mode 100644 index 1f1ab5c..0000000 --- a/event-swift/src/main/java/com/ware/swift/event/graph/GraphRunnable.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.ware.swift.event.graph; - -import com.ware.swift.event.graph.future.CallableNodeCmd; - - -public final class GraphRunnable implements Runnable{ - - private Node dependCommand ; - private GraphScheduler scheduler ; - public GraphRunnable(Node dependEJob, GraphScheduler scheduler) { - this.dependCommand = dependEJob ; - this.scheduler = scheduler ; - } - @SuppressWarnings("unchecked") - public void run() { - try { - if(dependCommand.getVal() instanceof RunnableNodeCmd){ - RunnableNodeCmd runnaNodeCmd = (RunnableNodeCmd) dependCommand.getVal(); - runnaNodeCmd.handler();; - }else if(dependCommand.getVal() instanceof CallableNodeCmd){ - CallableNodeCmd callableNodeCmd = (CallableNodeCmd) dependCommand.getVal(); - Object t = callableNodeCmd.call(); - if(callableNodeCmd.getFutureResult() == null){ - throw new IllegalArgumentException("callable node command must set a future node cmd."+ callableNodeCmd.toString()); - } - callableNodeCmd.getFutureResult().setResult(t); - } - this.scheduler.notify(dependCommand); - } catch (Exception e) { - e.printStackTrace(); - } - } -} diff --git a/event-swift/src/main/java/com/ware/swift/event/graph/GraphScheduler.java b/event-swift/src/main/java/com/ware/swift/event/graph/GraphScheduler.java deleted file mode 100644 index 5997521..0000000 --- a/event-swift/src/main/java/com/ware/swift/event/graph/GraphScheduler.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.ware.swift.event.graph; - -import com.ware.swift.event.ObjectEvent; -import com.ware.swift.event.object.AbstractEventObject; - -import java.util.Iterator; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; - -public class GraphScheduler extends AbstractEventObject> { - - private Graph graph; - private Map, NodeCondition> nodeConditionsMap = null; - - GraphScheduler(Graph graph) { - this.graph = graph; - this.initNodeCondition(); - } - - @Override - public void attachListener() { - this.addListener(event -> { - Node nodeCmd = event.getValue(); - // 得到当前这个节点的前驱节点[可能有多个] - Set> dependsNodes = (Set>) graph - .getAdjaNode().get(nodeCmd); - if (dependsNodes != null) { - // 如果不为null,则向他的后继节点广播一个消息。后继节点都知道自己将收到多少个消息后便可以开始执行 - Iterator> iter = dependsNodes.iterator(); - for (; iter.hasNext();) { - Node temp = (Node) iter.next(); - NodeCondition nodeCondition = nodeConditionsMap.get(temp); - nodeCondition.increMessageCount(nodeCmd);// 收到一个消息。进行加一操作。 - nodeCondition.handler(); - } - } - else { - if (nodeConditionsMap.get(nodeCmd) == null) {// 处理只有一个节点的情况 - return; - } - nodeConditionsMap.get(nodeCmd).handler();// 如果是最后一个节点每次执行完就直接看消息有没有收足够,收足够了,就执行,没有收足够,就直接丢弃 - } - }, 4); - } - - private void initNodeCondition() { - if (graph == null) { - return; - } - nodeConditionsMap = new ConcurrentHashMap, NodeCondition>( - graph.getVertexSet().size()); - Iterator> nodes = graph.getVertexSet().iterator(); - Map, Set>> reverseAdjaNode = graph - .getReverseAdjaNode(); - while (nodes.hasNext()) { - Node node = (Node) nodes.next(); - Set> reverseNode = (Set>) reverseAdjaNode - .get(node); - nodeConditionsMap.put(node, new NodeCondition(this, node, reverseNode)); - } - } - - public void notify(Node node) { - ObjectEvent> objectEvent = new ObjectEvent>( - this, node, 4); - this.notifyListeners(objectEvent); - } -} diff --git a/event-swift/src/main/java/com/ware/swift/event/graph/GraphUtils.java b/event-swift/src/main/java/com/ware/swift/event/graph/GraphUtils.java deleted file mode 100644 index be13dff..0000000 --- a/event-swift/src/main/java/com/ware/swift/event/graph/GraphUtils.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.ware.swift.event.graph; - -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - -public final class GraphUtils { - - private static ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()*4); - - /** - * if you want set the executor service then call this method - * @param executorService - */ - public static void setExecutorServer(ExecutorService executorService){ - GraphUtils.executorService = executorService; - } - - /** - * - * @param graphAction - */ - public static void submit(GraphRunnable graphAction){ - if(graphAction == null){ - return ; - } - - executorService.execute(graphAction); - } - - /** - * 如果是 开始节点,则调用 这个方法来触发.一次后继依赖的节点只要达到调教便可执行 - * - * @param startNode - * @param scheduler - */ - public static void triigerStartNode(Node startNode, GraphScheduler scheduler){ - GraphRunnable graphAction = new GraphRunnable(startNode, scheduler); - submit(graphAction); - } - - public static void triigerStartNode(Node startNode, Graph graph){ - GraphRunnable graphAction = new GraphRunnable(startNode, new GraphScheduler(graph)); - submit(graphAction); - } - - public static void scheduler(Graph graph){ - GraphRunnable graphAction = new GraphRunnable(graph.getStartNode(), new GraphScheduler(graph)); - submit(graphAction); - } - - public static void shutdown(){ - executorService.shutdown(); - } -} diff --git a/event-swift/src/main/java/com/ware/swift/event/graph/INodeCommand.java b/event-swift/src/main/java/com/ware/swift/event/graph/INodeCommand.java deleted file mode 100644 index 9bfb87b..0000000 --- a/event-swift/src/main/java/com/ware/swift/event/graph/INodeCommand.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.ware.swift.event.graph; -/** - * 触发节点执行的业务逻辑处理.如果某个节点达到执行的条件,就会触发这里的业务代码 - * @author pengbingting - * - */ -public interface INodeCommand { - -} diff --git a/event-swift/src/main/java/com/ware/swift/event/graph/Node.java b/event-swift/src/main/java/com/ware/swift/event/graph/Node.java deleted file mode 100644 index bd78230..0000000 --- a/event-swift/src/main/java/com/ware/swift/event/graph/Node.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.ware.swift.event.graph; - -import java.util.concurrent.locks.ReentrantLock; - -/** - * 拓扑排序节点类 - */ -public class Node { - private V val; - private int pathIn = 0; // 入链路数量 - private ReentrantLock lock = new ReentrantLock(); - public Node(V val) { - this.val = val; - } - public V getVal() { - return val; - } - public void setVal(V val) { - lock.lock(); - try{ - this.val = val; - }finally{ - lock.unlock(); - } - } - public int getPathIn() { - return pathIn; - } - public void setPathIn(int pathIn) { - lock.lock(); - try{ - this.pathIn = pathIn; - }finally{ - lock.unlock(); - } - } -} diff --git a/event-swift/src/main/java/com/ware/swift/event/graph/NodeCondition.java b/event-swift/src/main/java/com/ware/swift/event/graph/NodeCondition.java deleted file mode 100644 index 1a36389..0000000 --- a/event-swift/src/main/java/com/ware/swift/event/graph/NodeCondition.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.ware.swift.event.graph; - -import java.util.Set; -import java.util.concurrent.atomic.AtomicInteger; - -/** - * - * @author pengbingting - * - */ -public class NodeCondition extends AbstractNodeCondition { - - public NodeCondition(GraphScheduler schedule, Node observiable, Set> v) { - super(schedule,observiable, v); - } - - @Override - public boolean isFinished() { - //统计 收到 大于 1 的 node 的数量 - int tmpNum = 0 ; - for(AtomicInteger value : messageCountMap.values()){ - if(value.get()>=1){ - tmpNum++ ; - } - } - return tmpNum >= messageTotal; - } - - /** - * 收到 node 完成的消息数 - * @param targetJobNode - */ - public void increMessageCount(Node targetJobNode){ - - messageCountMap.get(targetJobNode).getAndIncrement(); - } -} diff --git a/event-swift/src/main/java/com/ware/swift/event/graph/RunnableNodeCmd.java b/event-swift/src/main/java/com/ware/swift/event/graph/RunnableNodeCmd.java deleted file mode 100644 index 9d6cda6..0000000 --- a/event-swift/src/main/java/com/ware/swift/event/graph/RunnableNodeCmd.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.ware.swift.event.graph; - -/** - * 如果这个节点仅仅是处理业务,不需要异步回调执行完后的结果,则实现该接口 - * @author pengbingting - * - */ -public interface RunnableNodeCmd extends INodeCommand { - - void handler(); -} diff --git a/event-swift/src/main/java/com/ware/swift/event/graph/future/AbstractCallableNodeCmd.java b/event-swift/src/main/java/com/ware/swift/event/graph/future/AbstractCallableNodeCmd.java deleted file mode 100644 index 5965407..0000000 --- a/event-swift/src/main/java/com/ware/swift/event/graph/future/AbstractCallableNodeCmd.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.ware.swift.event.graph.future; - -public abstract class AbstractCallableNodeCmd implements CallableNodeCmd { - - private FutureResult futureResult ; - public AbstractCallableNodeCmd() { - this.futureResult = new FutureResult(); - } - - public FutureResult getFutureResult() { - return this.futureResult; - } - - - public T get() { - return this.futureResult.getResult(); - } -} diff --git a/event-swift/src/main/java/com/ware/swift/event/graph/future/CallableNodeCmd.java b/event-swift/src/main/java/com/ware/swift/event/graph/future/CallableNodeCmd.java deleted file mode 100644 index 6631587..0000000 --- a/event-swift/src/main/java/com/ware/swift/event/graph/future/CallableNodeCmd.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.ware.swift.event.graph.future; - -import com.ware.swift.event.graph.INodeCommand; - -/** - * 提供异步可回调的 graph 节点 command.我们建议最好是在构造函数中就把 FutureNodeCmd 的 instance 给传递过来 - * @author pengbingting - * - */ -public interface CallableNodeCmd extends INodeCommand { - - T call() throws Exception; - - FutureResult getFutureResult(); - - T get(); -} diff --git a/event-swift/src/main/java/com/ware/swift/event/loop/AbstractAsyncEventLoopGroup.java b/event-swift/src/main/java/com/ware/swift/event/loop/AbstractAsyncEventLoopGroup.java index 1f83a17..b49ac42 100644 --- a/event-swift/src/main/java/com/ware/swift/event/loop/AbstractAsyncEventLoopGroup.java +++ b/event-swift/src/main/java/com/ware/swift/event/loop/AbstractAsyncEventLoopGroup.java @@ -5,11 +5,11 @@ import com.ware.swift.event.IEventPartitionerRegister; import com.ware.swift.event.ObjectEvent; import com.ware.swift.event.object.IEventCallBack; -import com.ware.swift.event.parallel.action.IParallelActionExecutor; import com.ware.swift.event.object.pipeline.AbstractPipelineEventObject; import com.ware.swift.event.object.pipeline.IPipelineEventListener; import com.ware.swift.event.parallel.IParallelQueueExecutor; import com.ware.swift.event.parallel.SuperFastParallelQueueExecutor; +import com.ware.swift.event.parallel.action.IParallelActionExecutor; import java.util.Deque; import java.util.concurrent.ConcurrentHashMap; @@ -26,7 +26,7 @@ public abstract class AbstractAsyncEventLoopGroup private IEventPartitioner iEventPartitioner; - protected ConcurrentHashMap> eventLoopQueueGroup = new ConcurrentHashMap>(); + protected ConcurrentHashMap> eventLoopQueueGroup = new ConcurrentHashMap<>(); protected IParallelQueueExecutor executor; protected long schedulerInterval;// 统一以毫秒为单位 @@ -127,7 +127,7 @@ public void registerEventPartitioner(IEventPartitioner eventPartitioner) { @Override public void enEmergencyQueue(Runnable runnable) { - executor.enEmergenceyQueue(runnable); + executor.enEmergencyQueue(runnable); } @Override diff --git a/event-swift/src/main/java/com/ware/swift/event/loop/EventLoopHandler.java b/event-swift/src/main/java/com/ware/swift/event/loop/EventLoopHandler.java index 87a75f5..20b3acf 100644 --- a/event-swift/src/main/java/com/ware/swift/event/loop/EventLoopHandler.java +++ b/event-swift/src/main/java/com/ware/swift/event/loop/EventLoopHandler.java @@ -6,109 +6,110 @@ import java.util.Collection; +/** + * @param + */ public class EventLoopHandler implements IEventLoopHandler { - protected IEventLoopQueue> eventLoopQueue; - protected Collection> pipelineListeners; - protected ObjectEvent event; - protected Long createTime; - protected AbstractAsyncEventLoopGroup asyncEventLoopGroup; + protected IEventLoopQueue> eventLoopQueue; + protected Collection> pipelineListeners; + protected ObjectEvent event; + protected Long createTime; + protected AbstractAsyncEventLoopGroup asyncEventLoopGroup; - public EventLoopHandler(IEventLoopQueue> eventLoopQueue, - AbstractAsyncEventLoopGroup asyncEventLoopGroup, - Collection> pipelineListeners, - ObjectEvent event) { - super(); - this.eventLoopQueue = eventLoopQueue; - this.pipelineListeners = pipelineListeners; - this.event = event; - this.createTime = System.currentTimeMillis(); - this.asyncEventLoopGroup = asyncEventLoopGroup; - } + public EventLoopHandler(IEventLoopQueue> eventLoopQueue, + AbstractAsyncEventLoopGroup asyncEventLoopGroup, + Collection> pipelineListeners, + ObjectEvent event) { + super(); + this.eventLoopQueue = eventLoopQueue; + this.pipelineListeners = pipelineListeners; + this.event = event; + this.createTime = System.currentTimeMillis(); + this.asyncEventLoopGroup = asyncEventLoopGroup; + } - public EventLoopHandler(AbstractAsyncEventLoopGroup asyncEventLoopGroup, - Collection> pipelineListeners, - ObjectEvent event) { - super(); - this.pipelineListeners = pipelineListeners; - this.event = event; - this.createTime = System.currentTimeMillis(); - } + public EventLoopHandler(AbstractAsyncEventLoopGroup asyncEventLoopGroup, + Collection> pipelineListeners, + ObjectEvent event) { + super(); + this.pipelineListeners = pipelineListeners; + this.event = event; + this.createTime = System.currentTimeMillis(); + } - public void setEventLoopQueueIfAbsent(IEventLoopQueue> queue) { - if (this.eventLoopQueue == null) { - // 两层判断的目的是 不需要每次过来都获取锁释放锁 - synchronized (this) { - if (this.eventLoopQueue == null) { - this.eventLoopQueue = queue; - } - } - } - } + public void setEventLoopQueueIfAbsent(IEventLoopQueue> queue) { + if (this.eventLoopQueue == null) { + // 两层判断的目的是 不需要每次过来都获取锁释放锁 + synchronized (this) { + if (this.eventLoopQueue == null) { + this.eventLoopQueue = queue; + } + } + } + } - public ObjectEvent getEvent() { - return event; - } + public ObjectEvent getEvent() { + return event; + } - public void setEvent(ObjectEvent event) { - this.event = event; - } + public void setEvent(ObjectEvent event) { + this.event = event; + } - @Override - public void run() { - if (eventLoopQueue == null) { - return; - } + @Override + public void run() { + if (eventLoopQueue == null) { + return; + } - boolean isStop = false; - long start = System.currentTimeMillis(); - try { - // 返回 false,表示当前这个event 还没有处理完,将等待interval时间,进入下一轮处理 - isStop = execute(); - long end = System.currentTimeMillis(); - long interval = end - start; - long leftTime = end - createTime; - if (interval >= 1000) { - Log.warn("execute action : " + this.toString() + ", interval : " - + interval + ", leftTime : " + leftTime + ", size : " - + eventLoopQueue.getQueue().size()); - } - } - catch (Exception e) { - e.printStackTrace(); - Log.error("run action execute exception. action : " + this.toString() - + e.getMessage()); - } - finally { - eventLoopQueue.dequeue(this); - if (!isStop) { - eventLoopQueue.schedulerdEventLoopHandler(this); - } - } - } + boolean isStop = false; + long start = System.currentTimeMillis(); + try { + // 返回 false,表示当前这个event 还没有处理完,将等待interval时间,进入下一轮处理 + isStop = execute(); + long end = System.currentTimeMillis(); + long interval = end - start; + long leftTime = end - createTime; + if (interval >= 1000) { + Log.warn("execute action : " + this.toString() + ", interval : " + + interval + ", leftTime : " + leftTime + ", size : " + + eventLoopQueue.getQueue().size()); + } + } catch (Exception e) { + e.printStackTrace(); + Log.error("run action execute exception. action : " + this.toString() + + e.getMessage()); + } finally { + eventLoopQueue.dequeue(this); + if (!isStop) { + eventLoopQueue.schedulerEventLoopHandler(this); + } + } + } - @Override - public boolean execute() { - boolean isStop = false; - int listenerIndex = 1; - for (IPipelineEventListener pipeline : pipelineListeners) { - // get the last handler to Decide to append event loop queue - isStop = pipeline.onEvent(event, listenerIndex); - if (event.isInterruptor()) { - // interruptor current pipeline object listener - break; - } - listenerIndex++; - } - return isStop; - } + @Override + public boolean execute() { + boolean isStop = false; + int listenerIndex = 1; + for (IPipelineEventListener pipeline : pipelineListeners) { + // get the last handler to Decide to append event loop queue + isStop = pipeline.onEvent(event, listenerIndex); + if (event.isInterrupt()) { + // interruptor current pipeline object listener + break; + } + listenerIndex++; + } + return isStop; + } - public AbstractAsyncEventLoopGroup getAsyncEventLoopGroup() { - return asyncEventLoopGroup; - } + public AbstractAsyncEventLoopGroup getAsyncEventLoopGroup() { + return asyncEventLoopGroup; + } - public void setAsyncEventLoopGroup( - AbstractAsyncEventLoopGroup asyncEventLoopGroup) { - this.asyncEventLoopGroup = asyncEventLoopGroup; - } + public void setAsyncEventLoopGroup( + AbstractAsyncEventLoopGroup asyncEventLoopGroup) { + this.asyncEventLoopGroup = asyncEventLoopGroup; + } } diff --git a/event-swift/src/main/java/com/ware/swift/event/loop/EventLoopQueue.java b/event-swift/src/main/java/com/ware/swift/event/loop/EventLoopQueue.java index f18fc63..0685dd2 100644 --- a/event-swift/src/main/java/com/ware/swift/event/loop/EventLoopQueue.java +++ b/event-swift/src/main/java/com/ware/swift/event/loop/EventLoopQueue.java @@ -1,8 +1,8 @@ package com.ware.swift.event.loop; +import com.lmax.disruptor.Sequence; import com.ware.swift.event.ObjectEvent; import com.ware.swift.event.common.Log; -import com.lmax.disruptor.Sequence; import com.ware.swift.event.parallel.IParallelQueueExecutor; import java.util.LinkedList; @@ -12,163 +12,158 @@ /** * the base implement of event loop queue. - * @author pengbingting * * @param + * @author pengbingting */ public class EventLoopQueue implements IEventLoopQueue> { - private Queue> queue; - private IParallelQueueExecutor executor; - private ReentrantLock queueLock = new ReentrantLock(); - protected AbstractAsyncEventLoopGroup asyncEventLoopGroup = null;// ms - protected Sequence lastActiveTime = new Sequence(System.currentTimeMillis()); - - public EventLoopQueue(IParallelQueueExecutor executor, - AbstractAsyncEventLoopGroup asyncEventLoopGroup) { - this.executor = executor; - queue = new LinkedList<>(); - this.asyncEventLoopGroup = asyncEventLoopGroup; - } - - public EventLoopQueue(IParallelQueueExecutor executor, - Queue> queue, - AbstractAsyncEventLoopGroup asyncEventLoopGroup) { - this.executor = executor; - this.queue = queue; - this.asyncEventLoopGroup = asyncEventLoopGroup; - } - - @SuppressWarnings({ "unchecked", "rawtypes" }) - @Override - public IEventLoopQueue getActionQueue() { - return this; - } - - @Override - public void clear() { - - } - - @SuppressWarnings({ "unchecked", "rawtypes" }) - @Override - public Queue getQueue() { - return queue; - } - - @Override - public void enqueue(EventLoopHandler eventHandler) { - int queueSize; - eventHandler.setEventLoopQueueIfAbsent(this); - queueLock.lock(); - try { - queue.add(eventHandler); - queueSize = queue.size(); - } - finally { - queueLock.unlock(); - } - if (queueSize == 1) { - // ObjectEvent objectEvent = eventHandler.getEvent(); - // executor.execute(eventHandler.getAsyncEventLoopGroup().partitioner(objectEvent), - // eventHandler); - doExecute(eventHandler); - } - if (queueSize > 4 * Runtime.getRuntime().availableProcessors()) { - Log.warn(eventHandler.getEvent().toString() + " queue size : " + queueSize); - } - - lastActiveTime.set(System.currentTimeMillis()); - } - - @Override - public void dequeue(EventLoopHandler eventHandler) { - EventLoopHandler nextEventHandler = null; - int queueSize; - String tmpString = null; - queueLock.lock(); - try { - queueSize = queue.size(); - EventLoopHandler temp = queue.remove(); - if (temp != eventHandler) { - tmpString = temp.getEvent().getValue().toString(); - } - if (queueSize != 0) { - nextEventHandler = queue.peek(); - } - } - finally { - queueLock.unlock(); - } - - if (nextEventHandler != null) { - ObjectEvent objectEvent = nextEventHandler.getEvent(); - executor.execute( - nextEventHandler.getAsyncEventLoopGroup().partitioner(objectEvent), - nextEventHandler); - } - if (queueSize == 0) { - Log.debug("queue.size() is 0."); - } - if (tmpString != null) { - Log.debug("action queue error. temp " + tmpString + ", action : " - + eventHandler.getEvent().getValue().toString()); - } - } - - @Override - public void schedulerdEventLoopHandler(final EventLoopHandler eventHandler) { - executor.getScheduledExecutorService().schedule(() -> enqueue(eventHandler), - getEventLoopInterval(eventHandler), TimeUnit.MILLISECONDS); - } - - @Override - public long getEventLoopInterval(EventLoopHandler eventHandler) { - ObjectEvent objectEvent = eventHandler.getEvent(); - long interval = asyncEventLoopGroup.getSchedulerInterval();// 公用的 - // 可根据实际的情况,动态调整每次 loop interval 的时间 间隔 - Object value = objectEvent - .getParameter(EventLoopConstants.EVENT_LOOP_INTERVAL_PARAM); - if (value != null) { - // 每个事件根据自己的需要可动态调整 event loop interval.即更加细粒度的控制 event interval 的时间 间隔 - if (value instanceof Integer) { - int tmpValue = (int) value; - interval = tmpValue; - } - else if (value instanceof Long) { - interval = (long) value; - } - else if (value instanceof String) { - try { - interval = Long.valueOf(value.toString()); - } - catch (NumberFormatException e) { - interval = TimeUnit.SECONDS.toMillis(1); - } - } - } - - return interval; - } - - @Override - public long getLastActiveTime() { - - return 0; - } - - @SuppressWarnings("unchecked") - @Override - public void doExecute(Runnable runnable) { - EventLoopHandler eventHandler = (EventLoopHandler) runnable; - ObjectEvent objectEvent = eventHandler.getEvent(); - executor.execute(eventHandler.getAsyncEventLoopGroup().partitioner(objectEvent), - eventHandler); - } - - @Override - public ReentrantLock getActionQueueLock() { - - return queueLock; - } + private Queue> queue; + private IParallelQueueExecutor executor; + private ReentrantLock queueLock = new ReentrantLock(); + protected AbstractAsyncEventLoopGroup asyncEventLoopGroup;// ms + protected Sequence lastActiveTime = new Sequence(System.currentTimeMillis()); + + public EventLoopQueue(IParallelQueueExecutor executor, + AbstractAsyncEventLoopGroup asyncEventLoopGroup) { + this.executor = executor; + queue = new LinkedList<>(); + this.asyncEventLoopGroup = asyncEventLoopGroup; + } + + public EventLoopQueue(IParallelQueueExecutor executor, + Queue> queue, + AbstractAsyncEventLoopGroup asyncEventLoopGroup) { + this.executor = executor; + this.queue = queue; + this.asyncEventLoopGroup = asyncEventLoopGroup; + } + + @SuppressWarnings({"unchecked", "rawtypes"}) + @Override + public IEventLoopQueue getActionQueue() { + return this; + } + + @Override + public void clear() { + + } + + @SuppressWarnings({"unchecked", "rawtypes"}) + @Override + public Queue getQueue() { + return queue; + } + + @Override + public void enqueue(EventLoopHandler eventHandler) { + int queueSize; + eventHandler.setEventLoopQueueIfAbsent(this); + queueLock.lock(); + try { + queue.add(eventHandler); + queueSize = queue.size(); + } finally { + queueLock.unlock(); + } + if (queueSize == 1) { + // ObjectEvent objectEvent = eventHandler.getEvent(); + // executor.execute(eventHandler.getAsyncEventLoopGroup().partitioner(objectEvent), + // eventHandler); + doExecute(eventHandler); + } + if (queueSize > 4 * Runtime.getRuntime().availableProcessors()) { + Log.warn(eventHandler.getEvent().toString() + " queue size : " + queueSize); + } + + lastActiveTime.set(System.currentTimeMillis()); + } + + @Override + public void dequeue(EventLoopHandler eventHandler) { + EventLoopHandler nextEventHandler = null; + int queueSize; + String tmpString = null; + queueLock.lock(); + try { + queueSize = queue.size(); + EventLoopHandler temp = queue.remove(); + if (temp != eventHandler) { + tmpString = temp.getEvent().getValue().toString(); + } + if (queueSize != 0) { + nextEventHandler = queue.peek(); + } + } finally { + queueLock.unlock(); + } + + if (nextEventHandler != null) { + ObjectEvent objectEvent = nextEventHandler.getEvent(); + executor.execute( + nextEventHandler.getAsyncEventLoopGroup().partitioner(objectEvent), + nextEventHandler); + } + if (queueSize == 0) { + Log.debug("queue.size() is 0."); + } + if (tmpString != null) { + Log.debug("action queue error. temp " + tmpString + ", action : " + + eventHandler.getEvent().getValue().toString()); + } + } + + @Override + public void schedulerEventLoopHandler(final EventLoopHandler eventHandler) { + executor.getScheduledExecutorService().schedule(() -> enqueue(eventHandler), + getEventLoopInterval(eventHandler), TimeUnit.MILLISECONDS); + } + + @Override + public long getEventLoopInterval(EventLoopHandler eventHandler) { + ObjectEvent objectEvent = eventHandler.getEvent(); + long interval = asyncEventLoopGroup.getSchedulerInterval();// 公用的 + // 可根据实际的情况,动态调整每次 loop interval 的时间 间隔 + Object value = objectEvent + .getParameter(EventLoopConstants.EVENT_LOOP_INTERVAL_PARAM); + if (value != null) { + // 每个事件根据自己的需要可动态调整 event loop interval.即更加细粒度的控制 event interval 的时间 间隔 + if (value instanceof Integer) { + int tmpValue = (int) value; + interval = tmpValue; + } else if (value instanceof Long) { + interval = (long) value; + } else if (value instanceof String) { + try { + interval = Long.valueOf(value.toString()); + } catch (NumberFormatException e) { + interval = TimeUnit.SECONDS.toMillis(1); + } + } + } + + return interval; + } + + @Override + public long getLastActiveTime() { + + return 0; + } + + @SuppressWarnings("unchecked") + @Override + public void doExecute(Runnable runnable) { + EventLoopHandler eventHandler = (EventLoopHandler) runnable; + ObjectEvent objectEvent = eventHandler.getEvent(); + executor.execute(eventHandler.getAsyncEventLoopGroup().partitioner(objectEvent), + eventHandler); + } + + @Override + public ReentrantLock getActionQueueLock() { + + return queueLock; + } } diff --git a/event-swift/src/main/java/com/ware/swift/event/loop/IEventLoopHandler.java b/event-swift/src/main/java/com/ware/swift/event/loop/IEventLoopHandler.java index 8e84f2a..cd244b5 100644 --- a/event-swift/src/main/java/com/ware/swift/event/loop/IEventLoopHandler.java +++ b/event-swift/src/main/java/com/ware/swift/event/loop/IEventLoopHandler.java @@ -1,10 +1,22 @@ package com.ware.swift.event.loop; +/** + * @param + */ interface IEventLoopHandler extends Runnable { - boolean execute(); + /** + * @return + */ + boolean execute(); - AbstractAsyncEventLoopGroup getAsyncEventLoopGroup(); + /** + * @return + */ + AbstractAsyncEventLoopGroup getAsyncEventLoopGroup(); - void setAsyncEventLoopGroup(AbstractAsyncEventLoopGroup asyncEventLoopGroup); + /** + * @param asyncEventLoopGroup + */ + void setAsyncEventLoopGroup(AbstractAsyncEventLoopGroup asyncEventLoopGroup); } diff --git a/event-swift/src/main/java/com/ware/swift/event/loop/IEventLoopQueue.java b/event-swift/src/main/java/com/ware/swift/event/loop/IEventLoopQueue.java index 0dc051e..52e3397 100644 --- a/event-swift/src/main/java/com/ware/swift/event/loop/IEventLoopQueue.java +++ b/event-swift/src/main/java/com/ware/swift/event/loop/IEventLoopQueue.java @@ -2,9 +2,19 @@ import com.ware.swift.event.parallel.action.IActionQueue; +/** + * @param + */ interface IEventLoopQueue extends IActionQueue { - void schedulerdEventLoopHandler(T event); + /** + * @param event + */ + void schedulerEventLoopHandler(T event); - long getEventLoopInterval(T eventHandler); + /** + * @param eventHandler + * @return + */ + long getEventLoopInterval(T eventHandler); } diff --git a/event-swift/src/main/java/com/ware/swift/event/mcache/AbstractConcurrentCache.java b/event-swift/src/main/java/com/ware/swift/event/mcache/AbstractConcurrentCache.java index 23b8767..43cf9ad 100644 --- a/event-swift/src/main/java/com/ware/swift/event/mcache/AbstractConcurrentCache.java +++ b/event-swift/src/main/java/com/ware/swift/event/mcache/AbstractConcurrentCache.java @@ -19,485 +19,464 @@ import java.util.concurrent.atomic.AtomicInteger; public abstract class AbstractConcurrentCache extends ConcurrentHashMap - implements IHighCacheMap { - - public final int ITEM_PUT_OP = 1 << 0; - public final int ITEM_RETRIVE_OP = 1 << 1; - public final int ITEM_REMOVE_OP = 1 << 2; - - private MCacheManager cacheManager; - private IExpireKeyHandler iExpireKeyAdaptor; - private IMissCacheHandler missCacheHandler = null; - /** - * - */ - private static final long serialVersionUID = 1L; - - private static final Logger log = LoggerFactory.getLogger(Log.class); - /** - * Use memory cache or not. - */ - protected boolean memoryCaching = true; - - /** - * Use unlimited disk caching. - */ - protected boolean unlimitedDiskCache = false; - - /** - * Default cache capacity (number of entries). - */ - protected final int DEFAULT_MAX_ENTRIES = 100; - - /** - * Max number of element in cache when considered unlimited. - */ - protected final int UNLIMITED = 2147483646; - - /** - * Cache capacity (number of entries). - */ - protected volatile int maxEntries = DEFAULT_MAX_ENTRIES; - /** - * The table is rehashed when its size exceeds this threshold. (The value of this - * field is always (int)(capacity * loadFactor).) - * - * @serial - */ - protected int threshold; - - /** - * to inspect the cache key expire - */ - protected ConcurrentMap keyExpireTimeMap = new ConcurrentHashMap<>(); - protected ConcurrentMap keyIdleTimeOutMap = new ConcurrentHashMap<>(); - - // 所有的缓存公用一个并行队列执行器 - public final static IParallelQueueExecutor DEFAULT_CACHE_QUEUE_EXECUTOR = new DisruptorParallelQueueExecutor( - Runtime.getRuntime().availableProcessors() * 2, 2 << 12); - // ParallelQueueExecutorBuilder.getInstance().builderSuperFastParallelQueueExecutor(4, - // "cache-event-loop-default"); - - private AtomicInteger epollCache = new AtomicInteger(0); - private AtomicBoolean isClearCache = new AtomicBoolean(false); - private AtomicBoolean isStartupCheckCapacity = new AtomicBoolean(false); - private IParallelQueueExecutor iParallelQueueExecutor = null; - private volatile String cacheTopic; - - public AbstractConcurrentCache(String cacheTopic, - IParallelQueueExecutor iParallelQueueExecutor, - IExpireKeyHandler iExpireKeyAdaptor, int initialCapacity, - float loadFactor) { - super(initialCapacity, loadFactor); - this.maxEntries = initialCapacity; - this.iExpireKeyAdaptor = iExpireKeyAdaptor; - this.iParallelQueueExecutor = iParallelQueueExecutor; - this.cacheTopic = cacheTopic; - this.cacheManager = new MCacheManager<>(this, iParallelQueueExecutor, true); - } - - public AbstractConcurrentCache(String cacheTopic, - IParallelQueueExecutor iParallelQueueExecutor, - IExpireKeyHandler iExpireKeyAdaptor, int initialCapacity) { - this(cacheTopic, iParallelQueueExecutor, iExpireKeyAdaptor, initialCapacity, - DEFAULT_LOAD_FACTOR); - } - - /** - * Constructs a new, empty map with a default initial capacity and load factor. - */ - public AbstractConcurrentCache(String cacheTopic, - IParallelQueueExecutor iParallelQueueExecutor, - IExpireKeyHandler iExpireKeyAdaptor) { - this(cacheTopic, iParallelQueueExecutor, iExpireKeyAdaptor, - DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR); - } - - public AbstractConcurrentCache(String cacheTopic, - IExpireKeyHandler iExpireKeyAdaptor) { - this(cacheTopic, DEFAULT_CACHE_QUEUE_EXECUTOR, iExpireKeyAdaptor, - DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR); - } - - public AbstractConcurrentCache(String cacheTopic, - IExpireKeyHandler iExpireKeyAdaptor, int initialCapacity) { - this(cacheTopic, DEFAULT_CACHE_QUEUE_EXECUTOR, iExpireKeyAdaptor, initialCapacity, - DEFAULT_LOAD_FACTOR); - } - - /** - * Returns true if this map contains no key-value mappings. - * - * @return true if this map contains no key-value mappings. - */ - public synchronized boolean isEmpty() { - return super.size() == 0; - } - - /** - * Set the cache capacity - */ - public void setMaxEntries(int newLimit) { - if (newLimit > 0) { - maxEntries = newLimit; - } - else { - // Capacity must be at least 1 - throw new IllegalArgumentException( - "Cache maximum number of entries must be at least 1"); - } - } - - /** - * Retrieve the cache capacity (number of entries). - */ - public int getMaxEntries() { - return maxEntries; - } - - /** - * Sets the memory caching flag. - */ - public void setMemoryCaching(boolean memoryCaching) { - this.memoryCaching = memoryCaching; - } - - /** - * Check if memory caching is used. - */ - public boolean isMemoryCaching() { - return memoryCaching; - } - - /** - * Sets the unlimited disk caching flag. - */ - public void setUnlimitedDiskCache(boolean unlimitedDiskCache) { - this.unlimitedDiskCache = unlimitedDiskCache; - } - - /** - * Check if we use unlimited disk cache. - */ - public boolean isUnlimitedDiskCache() { - return unlimitedDiskCache; - } - - /** - * Removes all mappings from this map. - */ - public void clear() { - isClearCache.set(true); - cacheManager.notifyListeners( - new ObjectEvent<>(this, "", MCacheManager.clearCacheEvent)); - } - - @SuppressWarnings("unchecked") - public V get(Object key) { - if (log.isDebugEnabled()) { - log.debug("get called (key=" + key + ")"); - } - // 一级缓存取 - V e = super.get(key); - if (e == null) {// 一级缓存取,如果一缓存没有,则到二级缓存取 - - return e; - } - else { - // checking for pointer equality first wins in most applications - notifyCacheRateUpdate((K) key, ITEM_RETRIVE_OP); - return e; // - } - - } - - @Override - public V get(K key, IMissCacheHandler missCacheHandler) { - - if (log.isDebugEnabled()) { - log.debug("get called (key=" + key + ")"); - } - // 一级缓存取 - V e = super.get(key); - if (e == null) {// 一级缓存取,如果一缓存没有,则到二级缓存取 - if (missCacheHandler != null) { - e = missCacheHandler.missCacheHandler(key); - } - return e; - } - else { - // checking for pointer equality first wins in most applications - notifyCacheRateUpdate(key, ITEM_RETRIVE_OP); - return e; // - } - } - - /** - * 这个 api 会自动将二级缓存中的数据存入一级缓存 - */ - @Override - public V getWithMissCacheHandler(K key) throws NoMissCacheHandlerException { - - if (log.isDebugEnabled()) { - log.debug("get called (key=" + key + ")"); - } - // 一级缓存取 - V e = super.get(key); - if (e == null) {// 一级缓存取,如果一缓存没有,则到二级缓存取 - if (missCacheHandler == null) { - throw new NoMissCacheHandlerException("the key[" + key - + "] does not IMissCacheHandler to operator,please implemate the interface for IMissCacheHandler."); - } - - e = missCacheHandler.missCacheHandler(key); - // 把他放到一级缓存 - this.put(key, e); - return e; - } - else { - // checking for pointer equality first wins in most applications - notifyCacheRateUpdate(key, ITEM_RETRIVE_OP); - return e; // - } - } - - @Override - public synchronized V putIfAbsent(K key, V value) { - if (super.containsKey(key)) { - // 这里访问 会更改他的缓存因子 - return this.get(key); - } - - return this.put(key, value); - } - - @Override - public void putAll(Map m) { - for (Map.Entry e : m.entrySet()) { - put(e.getKey(), e.getValue()); - } - } - - public class UpdateCacheRateEvent extends ObjectEvent { - /** - * - */ - private static final long serialVersionUID = 1L; - public K key; - public int operatorType; - - public UpdateCacheRateEvent(Object source, Object value, int eventType) { - super(source, value, eventType); - } - } - - public V put(K key, V value) { - if (value instanceof Collection) { - throw new UnsupportedOperationException( - "the cache in memory can not in collection "); - } - /** OpenSymphony END */ - if (value == null) { - throw new NullPointerException(); - } - - V oldValue = super.put(key, value); - if (this.size() - this.maxEntries > 10) { - // 加速回收过期的 cache entry - cacheManager.handlerExpireCacheEntry(this); - } - // 1、这也是同步处理 - notifyCacheRateUpdate(key, ITEM_PUT_OP); - return oldValue; - } - - @Override - public V remove(Object key) { - keyExpireTimeMap.remove(key); - keyIdleTimeOutMap.remove(key); - return remove(key, true); - } - - @SuppressWarnings("unchecked") - public V remove(Object key, boolean isnotify) { - V value = super.remove(key); - if (isnotify) { - notifyCacheRateUpdate((K) key, ITEM_REMOVE_OP); - } - return value; - } - - @SuppressWarnings("unchecked") - @Override - public boolean remove(Object key, Object value) { - if (key == null || value == null) { - return false; - } - // 1、先从内存中移除 - boolean e = super.remove(key, value); - if (e) { - // 如果移除成功,则将size 数减少,并移除 - K k = (K) key; - notifyCacheRateUpdate(k, ITEM_REMOVE_OP); - } - return e; - } - - @Override - public boolean replace(K key, V oldValue, V newValue) { - boolean flag = super.replace(key, oldValue, newValue); - if (flag) { - // replace 成功才算是一次真正的访问 - notifyCacheRateUpdate(key, ITEM_RETRIVE_OP); - } - return flag; - } - - @Override - public V replace(K key, V value) { - // 这里是强制行的replace - V oldValue = super.replace(key, value); - notifyCacheRateUpdate(key, ITEM_RETRIVE_OP); - return oldValue; - } - - /** - * Notify the underlying implementation that an item was put in the cache. - * - * @param key The cache key of the item that was put. - */ - public abstract void itemPut(K key); - - /** - * Notify any underlying algorithm that an item has been retrieved from the cache. - * - * @param key The cache key of the item that was retrieved. - */ - public abstract void itemRetrieved(K key); - - /** - * Notify the underlying implementation that an item was removed from the cache. - * - * @param key The cache key of the item that was removed. - */ - public abstract void itemRemoved(K key); - - /** - * The cache has reached its cacpacity and an item needs to be removed. (typically - * according to an algorithm such as LRU or FIFO). - * - * @return The key of whichever item was removed. - */ - public abstract K removeItem(boolean isRemove); - - @Override - public Set> getAllEntrySet() { - return super.entrySet(); - } - - @Override - public Set getAllKeySet() { - return super.keySet(); - } - - private void notifyCacheRateUpdate(final K key, final int operateType) { - epollCache.incrementAndGet(); - UpdateCacheRateEvent updateCacheRateEvent = new UpdateCacheRateEvent(this, "", - MCacheManager.cacheRateUpdateEvent); - updateCacheRateEvent.key = key; - updateCacheRateEvent.operatorType |= operateType; - cacheManager.updateCacheRate(updateCacheRateEvent); - - if (isStartupCheckCapacity.compareAndSet(false, true)) { - // 每个 topic cache 都有自己的一个 capacity check event - cacheManager.notifyListeners( - new ObjectEvent<>(this, "", MCacheManager.checkCapacityEvent)); - } - } - - public String getCacheTopic() { - return cacheTopic; - } - - public void setCacheTopic(String cacheTopic) { - this.cacheTopic = cacheTopic; - } - - public MCacheManager getCacheEventLoop() { - return cacheManager; - } - - public void setCacheEventLoop(MCacheManager cacheEventLoop) { - this.cacheManager = cacheEventLoop; - } - - public IExpireKeyHandler getiExpireKeyAdaptor() { - return iExpireKeyAdaptor; - } - - public void setiExpireKeyAdaptor(IExpireKeyHandler iExpireKeyAdaptor) { - this.iExpireKeyAdaptor = iExpireKeyAdaptor; - } - - public int getEpollCache() { - return epollCache.get(); - } - - public void decrementAndGetEpollCache() { - this.epollCache.decrementAndGet(); - } - - public boolean getIsClearCache() { - return isClearCache.get(); - } - - public void setClearFinish() { - isClearCache.set(false); - } - - public boolean getIsStartupCheckCapacity() { - return isStartupCheckCapacity.get(); - } - - public IParallelQueueExecutor getiParallelQueueExecutor() { - return iParallelQueueExecutor; - } - - public void setiParallelQueueExecutor(IParallelQueueExecutor iParallelQueueExecutor) { - this.iParallelQueueExecutor = iParallelQueueExecutor; - } - - public void setMissCacheHandler(IMissCacheHandler missCacheHandler) { - this.missCacheHandler = missCacheHandler; - } - - /** - * - * @param key - * @param value - * @param expireTime 过期时间,以毫秒为单位 - * @return - */ - @Override - public V put(K key, V value, long expireTime) { - keyExpireTimeMap.put(key, System.currentTimeMillis() + expireTime); - keyIdleTimeOutMap.put(key, expireTime); - return this.put(key, value); - } - - @Override - public void updateExpireTime(K key, long expireTime) { - keyExpireTimeMap.put(key, expireTime); - } - - public ConcurrentMap getKeyExpireTimeMap() { - return new ConcurrentHashMap<>(keyExpireTimeMap); - } - - @Override - public long getIdleTimeOut(K key) { - Long idleTimeOut = keyIdleTimeOutMap.get(key); - - return idleTimeOut == null ? -1 : idleTimeOut; - } + implements IHighCacheMap { + + public final int ITEM_PUT_OP = 1 << 0; + public final int ITEM_RETRIEVE_OP = 1 << 1; + public final int ITEM_REMOVE_OP = 1 << 2; + + private MCacheManager cacheManager; + private IExpireKeyHandler iExpireKeyAdaptor; + private IMissCacheHandler missCacheHandler = null; + /** + * + */ + private static final long serialVersionUID = 1L; + + private static final Logger log = LoggerFactory.getLogger(Log.class); + /** + * Use memory cache or not. + */ + protected boolean memoryCaching = true; + + /** + * Use unlimited disk caching. + */ + protected boolean unlimitedDiskCache = false; + + /** + * Default cache capacity (number of entries). + */ + protected final int DEFAULT_MAX_ENTRIES = 100; + + /** + * Cache capacity (number of entries). + */ + protected volatile int maxEntries = DEFAULT_MAX_ENTRIES; + /** + * to inspect the cache key expire + */ + protected ConcurrentMap keyExpireTimeMap = new ConcurrentHashMap<>(); + protected ConcurrentMap keyIdleTimeOutMap = new ConcurrentHashMap<>(); + + // 所有的缓存公用一个并行队列执行器 + public final static IParallelQueueExecutor DEFAULT_CACHE_QUEUE_EXECUTOR = new DisruptorParallelQueueExecutor( + Runtime.getRuntime().availableProcessors() * 2, 2 << 12); + private AtomicInteger epollCache = new AtomicInteger(0); + private AtomicBoolean isClearCache = new AtomicBoolean(false); + private AtomicBoolean isStartupCheckCapacity = new AtomicBoolean(false); + private IParallelQueueExecutor iParallelQueueExecutor; + private volatile String cacheTopic; + + public AbstractConcurrentCache(String cacheTopic, + IParallelQueueExecutor iParallelQueueExecutor, + IExpireKeyHandler iExpireKeyAdaptor, int initialCapacity, + float loadFactor) { + super(initialCapacity, loadFactor); + this.maxEntries = initialCapacity; + this.iExpireKeyAdaptor = iExpireKeyAdaptor; + this.iParallelQueueExecutor = iParallelQueueExecutor; + this.cacheTopic = cacheTopic; + this.cacheManager = new MCacheManager<>(this, iParallelQueueExecutor, true); + } + + public AbstractConcurrentCache(String cacheTopic, + IParallelQueueExecutor iParallelQueueExecutor, + IExpireKeyHandler iExpireKeyAdaptor, int initialCapacity) { + this(cacheTopic, iParallelQueueExecutor, iExpireKeyAdaptor, initialCapacity, + DEFAULT_LOAD_FACTOR); + } + + /** + * Constructs a new, empty map with a default initial capacity and load factor. + */ + public AbstractConcurrentCache(String cacheTopic, + IParallelQueueExecutor iParallelQueueExecutor, + IExpireKeyHandler iExpireKeyAdaptor) { + this(cacheTopic, iParallelQueueExecutor, iExpireKeyAdaptor, + DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR); + } + + public AbstractConcurrentCache(String cacheTopic, + IExpireKeyHandler iExpireKeyAdaptor) { + this(cacheTopic, DEFAULT_CACHE_QUEUE_EXECUTOR, iExpireKeyAdaptor, + DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR); + } + + public AbstractConcurrentCache(String cacheTopic, + IExpireKeyHandler iExpireKeyAdaptor, int initialCapacity) { + this(cacheTopic, DEFAULT_CACHE_QUEUE_EXECUTOR, iExpireKeyAdaptor, initialCapacity, + DEFAULT_LOAD_FACTOR); + } + + /** + * Returns true if this map contains no key-value mappings. + * + * @return true if this map contains no key-value mappings. + */ + public synchronized boolean isEmpty() { + return super.size() == 0; + } + + /** + * Set the cache capacity + */ + public void setMaxEntries(int newLimit) { + if (newLimit > 0) { + maxEntries = newLimit; + } else { + // Capacity must be at least 1 + throw new IllegalArgumentException( + "Cache maximum number of entries must be at least 1"); + } + } + + /** + * Retrieve the cache capacity (number of entries). + */ + public int getMaxEntries() { + return maxEntries; + } + + /** + * Sets the memory caching flag. + */ + public void setMemoryCaching(boolean memoryCaching) { + this.memoryCaching = memoryCaching; + } + + /** + * Check if memory caching is used. + */ + public boolean isMemoryCaching() { + return memoryCaching; + } + + /** + * Sets the unlimited disk caching flag. + */ + public void setUnlimitedDiskCache(boolean unlimitedDiskCache) { + this.unlimitedDiskCache = unlimitedDiskCache; + } + + /** + * Check if we use unlimited disk cache. + */ + public boolean isUnlimitedDiskCache() { + return unlimitedDiskCache; + } + + /** + * Removes all mappings from this map. + */ + public void clear() { + isClearCache.set(true); + cacheManager.notifyListeners( + new ObjectEvent<>(this, "", MCacheManager.clearCacheEvent)); + } + + @SuppressWarnings("unchecked") + public V get(Object key) { + if (log.isDebugEnabled()) { + log.debug("get called (key=" + key + ")"); + } + // 一级缓存取 + V e = super.get(key); + if (e == null) {// 一级缓存取,如果一缓存没有,则到二级缓存取 + + return e; + } else { + // checking for pointer equality first wins in most applications + notifyCacheRateUpdate((K) key, ITEM_RETRIEVE_OP); + return e; // + } + + } + + @Override + public V get(K key, IMissCacheHandler missCacheHandler) { + + if (log.isDebugEnabled()) { + log.debug("get called (key=" + key + ")"); + } + // 一级缓存取 + V e = super.get(key); + if (e == null) {// 一级缓存取,如果一缓存没有,则到二级缓存取 + if (missCacheHandler != null) { + e = missCacheHandler.missCacheHandler(key); + } + return e; + } else { + // checking for pointer equality first wins in most applications + notifyCacheRateUpdate(key, ITEM_RETRIEVE_OP); + return e; // + } + } + + /** + * 这个 api 会自动将二级缓存中的数据存入一级缓存 + */ + @Override + public V getWithMissCacheHandler(K key) throws NoMissCacheHandlerException { + + if (log.isDebugEnabled()) { + log.debug("get called (key=" + key + ")"); + } + // 一级缓存取 + V e = super.get(key); + if (e == null) {// 一级缓存取,如果一缓存没有,则到二级缓存取 + if (missCacheHandler == null) { + throw new NoMissCacheHandlerException("the key[" + key + + "] does not IMissCacheHandler to operator,please implemate the interface for IMissCacheHandler."); + } + + e = missCacheHandler.missCacheHandler(key); + // 把他放到一级缓存 + this.put(key, e); + return e; + } else { + // checking for pointer equality first wins in most applications + notifyCacheRateUpdate(key, ITEM_RETRIEVE_OP); + return e; // + } + } + + @Override + public synchronized V putIfAbsent(K key, V value) { + if (super.containsKey(key)) { + // 这里访问 会更改他的缓存因子 + return this.get(key); + } + + return this.put(key, value); + } + + @Override + public void putAll(Map m) { + for (Map.Entry e : m.entrySet()) { + put(e.getKey(), e.getValue()); + } + } + + public class UpdateCacheRateEvent extends ObjectEvent { + /** + * + */ + private static final long serialVersionUID = 1L; + public K key; + public int operatorType; + + public UpdateCacheRateEvent(Object source, Object value, int eventType) { + super(source, value, eventType); + } + } + + public V put(K key, V value) { + if (value instanceof Collection) { + throw new UnsupportedOperationException( + "the cache in memory can not in collection "); + } + /** OpenSymphony END */ + if (value == null) { + throw new NullPointerException(); + } + + V oldValue = super.put(key, value); + if (this.size() - this.maxEntries > 10) { + // 加速回收过期的 cache entry + cacheManager.handlerExpireCacheEntry(this); + } + // 1、这也是同步处理 + notifyCacheRateUpdate(key, ITEM_PUT_OP); + return oldValue; + } + + @Override + public V remove(Object key) { + keyExpireTimeMap.remove(key); + keyIdleTimeOutMap.remove(key); + return remove(key, true); + } + + @SuppressWarnings("unchecked") + public V remove(Object key, boolean isnotify) { + V value = super.remove(key); + if (isnotify) { + notifyCacheRateUpdate((K) key, ITEM_REMOVE_OP); + } + return value; + } + + @SuppressWarnings("unchecked") + @Override + public boolean remove(Object key, Object value) { + if (key == null || value == null) { + return false; + } + // 1、先从内存中移除 + boolean e = super.remove(key, value); + if (e) { + // 如果移除成功,则将size 数减少,并移除 + K k = (K) key; + notifyCacheRateUpdate(k, ITEM_REMOVE_OP); + } + return e; + } + + @Override + public boolean replace(K key, V oldValue, V newValue) { + boolean flag = super.replace(key, oldValue, newValue); + if (flag) { + // replace 成功才算是一次真正的访问 + notifyCacheRateUpdate(key, ITEM_RETRIEVE_OP); + } + return flag; + } + + @Override + public V replace(K key, V value) { + // 这里是强制行的replace + V oldValue = super.replace(key, value); + notifyCacheRateUpdate(key, ITEM_RETRIEVE_OP); + return oldValue; + } + + /** + * Notify the underlying implementation that an item was put in the cache. + * + * @param key The cache key of the item that was put. + */ + public abstract void itemPut(K key); + + /** + * Notify any underlying algorithm that an item has been retrieved from the cache. + * + * @param key The cache key of the item that was retrieved. + */ + public abstract void itemRetrieved(K key); + + /** + * Notify the underlying implementation that an item was removed from the cache. + * + * @param key The cache key of the item that was removed. + */ + public abstract void itemRemoved(K key); + + /** + * The cache has reached its cacpacity and an item needs to be removed. (typically + * according to an algorithm such as LRU or FIFO). + * + * @return The key of whichever item was removed. + */ + public abstract K removeItem(boolean isRemove); + + @Override + public Set> getAllEntrySet() { + return super.entrySet(); + } + + @Override + public Set getAllKeySet() { + return super.keySet(); + } + + private void notifyCacheRateUpdate(final K key, final int operateType) { + epollCache.incrementAndGet(); + UpdateCacheRateEvent updateCacheRateEvent = new UpdateCacheRateEvent(this, "", + MCacheManager.cacheRateUpdateEvent); + updateCacheRateEvent.key = key; + updateCacheRateEvent.operatorType |= operateType; + cacheManager.updateCacheRate(updateCacheRateEvent); + + if (isStartupCheckCapacity.compareAndSet(false, true)) { + // 每个 topic cache 都有自己的一个 capacity check event + cacheManager.notifyListeners( + new ObjectEvent<>(this, "", MCacheManager.checkCapacityEvent)); + } + } + + public String getCacheTopic() { + return cacheTopic; + } + + public void setCacheTopic(String cacheTopic) { + this.cacheTopic = cacheTopic; + } + + public MCacheManager getCacheEventLoop() { + return cacheManager; + } + + public void setCacheEventLoop(MCacheManager cacheEventLoop) { + this.cacheManager = cacheEventLoop; + } + + public IExpireKeyHandler getiExpireKeyAdaptor() { + return iExpireKeyAdaptor; + } + + public void setiExpireKeyAdaptor(IExpireKeyHandler iExpireKeyAdaptor) { + this.iExpireKeyAdaptor = iExpireKeyAdaptor; + } + + public int getEpollCache() { + return epollCache.get(); + } + + public void decrementAndGetEpollCache() { + this.epollCache.decrementAndGet(); + } + + public boolean getIsClearCache() { + return isClearCache.get(); + } + + public void setClearFinish() { + isClearCache.set(false); + } + + public boolean getIsStartupCheckCapacity() { + return isStartupCheckCapacity.get(); + } + + public IParallelQueueExecutor getiParallelQueueExecutor() { + return iParallelQueueExecutor; + } + + public void setiParallelQueueExecutor(IParallelQueueExecutor iParallelQueueExecutor) { + this.iParallelQueueExecutor = iParallelQueueExecutor; + } + + public void setMissCacheHandler(IMissCacheHandler missCacheHandler) { + this.missCacheHandler = missCacheHandler; + } + + /** + * @param key + * @param value + * @param expireTime 过期时间,以毫秒为单位 + * @return + */ + @Override + public V put(K key, V value, long expireTime) { + keyExpireTimeMap.put(key, System.currentTimeMillis() + expireTime); + keyIdleTimeOutMap.put(key, expireTime); + return this.put(key, value); + } + + @Override + public void updateExpireTime(K key, long expireTime) { + keyExpireTimeMap.put(key, expireTime); + } + + public ConcurrentMap getKeyExpireTimeMap() { + return new ConcurrentHashMap<>(keyExpireTimeMap); + } + + @Override + public long getIdleTimeOut(K key) { + Long idleTimeOut = keyIdleTimeOutMap.get(key); + + return idleTimeOut == null ? -1 : idleTimeOut; + } } diff --git a/event-swift/src/main/java/com/ware/swift/event/mcache/DefaultExpireKeyAdaptor.java b/event-swift/src/main/java/com/ware/swift/event/mcache/DefaultExpireKeyAdaptor.java index 0fd3ad3..0f5247b 100644 --- a/event-swift/src/main/java/com/ware/swift/event/mcache/DefaultExpireKeyAdaptor.java +++ b/event-swift/src/main/java/com/ware/swift/event/mcache/DefaultExpireKeyAdaptor.java @@ -1,10 +1,14 @@ package com.ware.swift.event.mcache; -public class DefaultExpireKeyAdaptor implements IExpireKeyHandler{ +/** + * @param + * @param + */ +public class DefaultExpireKeyAdaptor implements IExpireKeyHandler { - @Override - public void expire(K key, V value, AbstractConcurrentCache cache) { - - } + @Override + public void expire(K key, V value, AbstractConcurrentCache cache) { + + } } diff --git a/event-swift/src/main/java/com/ware/swift/event/mcache/FIFOConcurrentCache.java b/event-swift/src/main/java/com/ware/swift/event/mcache/FIFOConcurrentCache.java index 5967ea3..e51b27c 100644 --- a/event-swift/src/main/java/com/ware/swift/event/mcache/FIFOConcurrentCache.java +++ b/event-swift/src/main/java/com/ware/swift/event/mcache/FIFOConcurrentCache.java @@ -7,68 +7,68 @@ /** * 只关心key 的 put 操作。当元素满的时候将最先进入的元素给 remove 掉 - * 注意:get/replace 等操作是不会更改key 的进入先后顺序的 - * @author pengbingting + * 注意:get/replace 等操作是不会更改key 的进入先后顺序的 * * @param * @param + * @author pengbingting */ -public class FIFOConcurrentCache extends AbstractConcurrentCache { - - public FIFOConcurrentCache(String cacheTopic, IParallelQueueExecutor IParallelQueueExecutor, IExpireKeyHandler iExpireKeyAdaptor) { - super(cacheTopic,IParallelQueueExecutor, iExpireKeyAdaptor); - } - - public FIFOConcurrentCache(String cacheTopic,IParallelQueueExecutor IParallelQueueExecutor,int initialCapacity) { - super(cacheTopic,IParallelQueueExecutor, new DefaultExpireKeyAdaptor(),initialCapacity); - } - - public FIFOConcurrentCache(String cacheTopic,IParallelQueueExecutor IParallelQueueExecutor, IExpireKeyHandler iExpireKeyAdaptor, - int initialCapacity, float loadFactor) { - super(cacheTopic,IParallelQueueExecutor, iExpireKeyAdaptor, initialCapacity, loadFactor); - } - - public FIFOConcurrentCache(String cacheTopic,IParallelQueueExecutor IParallelQueueExecutor, IExpireKeyHandler iExpireKeyAdaptor, - int initialCapacity) { - super(cacheTopic,IParallelQueueExecutor, iExpireKeyAdaptor, initialCapacity); - } - - public FIFOConcurrentCache(String cacheTopic,IExpireKeyHandler iExpireKeyAdaptor,int initialCapacity) { - super(cacheTopic,DEFAULT_CACHE_QUEUE_EXECUTOR, iExpireKeyAdaptor, initialCapacity); - } - - private static final long serialVersionUID = -10333778645392679L; - - private LinkedBlockingQueue linkedQuence = new LinkedBlockingQueue(); - - public void itemPut(K key) { - if (linkedQuence.contains(key)){ - linkedQuence.remove(key); - } - - linkedQuence.add(key); - } - - @Override - public void itemRetrieved(K key) { - //get operation does not update the order - } - - @Override - public void itemRemoved(K key) { - linkedQuence.remove(key); - } - - /** - * 仅仅是提供找到合适 key 的算法,但是不做真正的remove - */ - @Override - public K removeItem(boolean isRemove) { - if(isRemove){ - return linkedQuence.poll(); - }else{ - return linkedQuence.peek(); - } - } +public class FIFOConcurrentCache extends AbstractConcurrentCache { + + public FIFOConcurrentCache(String cacheTopic, IParallelQueueExecutor IParallelQueueExecutor, IExpireKeyHandler iExpireKeyAdaptor) { + super(cacheTopic, IParallelQueueExecutor, iExpireKeyAdaptor); + } + + public FIFOConcurrentCache(String cacheTopic, IParallelQueueExecutor IParallelQueueExecutor, int initialCapacity) { + super(cacheTopic, IParallelQueueExecutor, new DefaultExpireKeyAdaptor<>(), initialCapacity); + } + + public FIFOConcurrentCache(String cacheTopic, IParallelQueueExecutor IParallelQueueExecutor, IExpireKeyHandler iExpireKeyAdaptor, + int initialCapacity, float loadFactor) { + super(cacheTopic, IParallelQueueExecutor, iExpireKeyAdaptor, initialCapacity, loadFactor); + } + + public FIFOConcurrentCache(String cacheTopic, IParallelQueueExecutor IParallelQueueExecutor, IExpireKeyHandler iExpireKeyAdaptor, + int initialCapacity) { + super(cacheTopic, IParallelQueueExecutor, iExpireKeyAdaptor, initialCapacity); + } + + public FIFOConcurrentCache(String cacheTopic, IExpireKeyHandler iExpireKeyAdaptor, int initialCapacity) { + super(cacheTopic, DEFAULT_CACHE_QUEUE_EXECUTOR, iExpireKeyAdaptor, initialCapacity); + } + + private static final long serialVersionUID = -10333778645392679L; + + private LinkedBlockingQueue linkedQuence = new LinkedBlockingQueue<>(); + + public void itemPut(K key) { + if (linkedQuence.contains(key)) { + linkedQuence.remove(key); + } + + linkedQuence.add(key); + } + + @Override + public void itemRetrieved(K key) { + //get operation does not update the order + } + + @Override + public void itemRemoved(K key) { + linkedQuence.remove(key); + } + + /** + * 仅仅是提供找到合适 key 的算法,但是不做真正的remove + */ + @Override + public K removeItem(boolean isRemove) { + if (isRemove) { + return linkedQuence.poll(); + } else { + return linkedQuence.peek(); + } + } } diff --git a/event-swift/src/main/java/com/ware/swift/event/mcache/IMissCacheHandler.java b/event-swift/src/main/java/com/ware/swift/event/mcache/IMissCacheHandler.java index 86aae31..67114ce 100644 --- a/event-swift/src/main/java/com/ware/swift/event/mcache/IMissCacheHandler.java +++ b/event-swift/src/main/java/com/ware/swift/event/mcache/IMissCacheHandler.java @@ -1,6 +1,10 @@ package com.ware.swift.event.mcache; +/** + * @param + * @param + */ public interface IMissCacheHandler { - V missCacheHandler(K key); + V missCacheHandler(K key); } diff --git a/event-swift/src/main/java/com/ware/swift/event/mcache/LFUConcurrentCache.java b/event-swift/src/main/java/com/ware/swift/event/mcache/LFUConcurrentCache.java index 89d58e4..7d9f421 100644 --- a/event-swift/src/main/java/com/ware/swift/event/mcache/LFUConcurrentCache.java +++ b/event-swift/src/main/java/com/ware/swift/event/mcache/LFUConcurrentCache.java @@ -2,187 +2,186 @@ import com.ware.swift.event.common.Log; import com.ware.swift.event.parallel.IParallelQueueExecutor; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.util.Comparator; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentSkipListSet; import java.util.concurrent.LinkedBlockingQueue; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - /** * 最少使用缓存置换算法的实现latest-seldom use cache - * @author pbting * + * @author pbting */ -public class LFUConcurrentCache extends AbstractConcurrentCache { - - /** - * - */ - private static final long serialVersionUID = 1L; - - public LFUConcurrentCache(String cacheTopic, IParallelQueueExecutor IParallelQueueExecutor, IExpireKeyHandler iExpireKeyAdaptor, - int initialCapacity, float loadFactor) { - super(cacheTopic,IParallelQueueExecutor, iExpireKeyAdaptor, initialCapacity, loadFactor); - } - - public LFUConcurrentCache(String cacheTopic,IParallelQueueExecutor IParallelQueueExecutor, IExpireKeyHandler iExpireKeyAdaptor, - int initialCapacity) { - super(cacheTopic,IParallelQueueExecutor, iExpireKeyAdaptor, initialCapacity); - } - - public LFUConcurrentCache(String cacheTopic,IParallelQueueExecutor IParallelQueueExecutor, - IExpireKeyHandler iExpireKeyAdaptor) { - super(cacheTopic,IParallelQueueExecutor, iExpireKeyAdaptor); - } - - - public LFUConcurrentCache(String cacheTopic,IExpireKeyHandler iExpireKeyAdaptor) { - super(cacheTopic,DEFAULT_CACHE_QUEUE_EXECUTOR, iExpireKeyAdaptor); - } - - private static final Logger log = LoggerFactory.getLogger(Log.class); - - //定义用来存放访问次数Set集合 - private Set accessCountSort = new ConcurrentSkipListSet(new Comparator() { - public int compare(Integer o1, Integer o2) { - - return o1.compareTo(o2); - }; - }); - - /** - * key和访问次数据映射的hashmap - */ - private ConcurrentHashMap keyAccessCount = new ConcurrentHashMap(); - - /** - * 访问映射字段:次数和这一类key的映射排序分类处理 - */ - private ConcurrentHashMap> accessCountQueue = new ConcurrentHashMap>(); - - @Override - public void itemPut(K key) { - - if(keyAccessCount.containsKey(key)){//已经包含该key,则该怎么办 - /** - * 先把这个key取出来,然后增加次数,放到适当的等级中 - *这个时候就成了一个获取时的操作了,改变其次序 - */ - log.info("[HighCache]-LFUConcurrentCache.itemPut and containsKey:"+key); - this.itemRetrieved(key); - }else{//不包含该怎么办 - //得到一次级别上的所有map对象,放到第一个级别上 - LinkedBlockingQueue accessCountQueue = this.accessCountQueue.get(1); - if(accessCountQueue == null){ - //表示系统的第一次 - accessCountQueue = new LinkedBlockingQueue(); - this.accessCountQueue.put(1, accessCountQueue); - } - - accessCountQueue.add(key); - if(!this.accessCountSort.contains(1)){ - this.accessCountSort.add(1); - } - - this.keyAccessCount.put(key,1); - log.debug("[HighCache]-LFUConcurrentCache.itemPut and first:"+key); - //一次处理处理完成 - } - } - - /** - * 当从缓存容器中get操作,促发该动作,目的就是改变key的一个排序 - */ - @Override - public void itemRetrieved(K key) { - //1、获取当前key的一个访问次数, - if(!keyAccessCount.containsKey(key)){ - return ; - } - - Integer accessCount = keyAccessCount.get(key); - //获取这个访问次次数的一批key - LinkedBlockingQueue accessCountQueue = this.accessCountQueue.get(accessCount); - //从当前这个等级remove 掉,往下一个等级放 - accessCountQueue.remove(key); - - if(accessCountQueue.isEmpty()){ - this.accessCountQueue.remove(accessCount); - accessCountQueue.clear(); - accessCountQueue = null ; - //不需要在排序列表里面 - accessCountSort.remove(accessCount); - } - //对这访问次数加一 - accessCount++; - log.debug("[HighCache]-LFUConcurrentCache.itemRetrieved and the access count is"+accessCount); - //获得下一个等级 - LinkedBlockingQueue nextLevelAccessCountQueue = this.accessCountQueue.get(accessCount); - - if(nextLevelAccessCountQueue == null){ - nextLevelAccessCountQueue = new LinkedBlockingQueue(); - this.accessCountQueue.put(accessCount, nextLevelAccessCountQueue); - } - //然后在这一个等级内添加key和他访问次数的一个映射 - nextLevelAccessCountQueue.add(key); - //更新当前 key的访问次数 - this.keyAccessCount.put(key, accessCount); - //必须的放进去 - log.debug("访问频率最高的次数为:"+accessCount+",and the key is:"+key); - if(!this.accessCountSort.contains(accessCount)){ - accessCountSort.add(accessCount); - } - //一次处理完成 - } - - /** - * 缓存容器中remove时,促发该动作,移除该key,重新保持最新的次序 - */ - @Override - public void itemRemoved(K key) { - - if(!this.keyAccessCount.containsKey(key)) - return ; - - Integer count = keyAccessCount.remove(key);//移除这个key所对应的访问次数 - //获取这个访问次次数的一批key - LinkedBlockingQueue accessCountQueue = this.accessCountQueue.get(count); - accessCountQueue.remove(key); - log.debug("the remove key is: "+key+" and the access count is: "+count); - } - - /** - * 一级缓存和二级缓存兑换时,自动替换一个缓存实体,这里是核心 - * - * 有个一次级别全部扫描的关系在里面,对相同的次数又该如何处理 - * ,这里一定要确保移除掉,不要就会影响命中率 - * - */ - @Override - public K removeItem(boolean isRemove) { - K key = null ; - //从低级别的开始扫描,也即排序迭代 - for(Integer count : this.accessCountSort){ - LinkedBlockingQueue accessCountQueue = this.accessCountQueue.get(count); - if(accessCountQueue.isEmpty())//这个时候表明这个访问层已经清空,则跳到下一层 - continue; - //移除这一类级别中的任何一个数,因为这些的访问次数是相同的 - else{ - //并将在HashMap中的已移除的 - if(isRemove){ - key = accessCountQueue.poll(); - keyAccessCount.remove(key); - }else{ - key = accessCountQueue.peek(); - } - break; - } - } - //如果正常,则应该替换村次数最少的 - - return key; - } +public class LFUConcurrentCache extends AbstractConcurrentCache { + + /** + * + */ + private static final long serialVersionUID = 1L; + + public LFUConcurrentCache(String cacheTopic, IParallelQueueExecutor IParallelQueueExecutor, IExpireKeyHandler iExpireKeyAdaptor, + int initialCapacity, float loadFactor) { + super(cacheTopic, IParallelQueueExecutor, iExpireKeyAdaptor, initialCapacity, loadFactor); + } + + public LFUConcurrentCache(String cacheTopic, IParallelQueueExecutor IParallelQueueExecutor, IExpireKeyHandler iExpireKeyAdaptor, + int initialCapacity) { + super(cacheTopic, IParallelQueueExecutor, iExpireKeyAdaptor, initialCapacity); + } + + public LFUConcurrentCache(String cacheTopic, IParallelQueueExecutor IParallelQueueExecutor, + IExpireKeyHandler iExpireKeyAdaptor) { + super(cacheTopic, IParallelQueueExecutor, iExpireKeyAdaptor); + } + + + public LFUConcurrentCache(String cacheTopic, IExpireKeyHandler iExpireKeyAdaptor) { + super(cacheTopic, DEFAULT_CACHE_QUEUE_EXECUTOR, iExpireKeyAdaptor); + } + + private static final Logger log = LoggerFactory.getLogger(Log.class); + + //定义用来存放访问次数Set集合 + private Set accessCountSort = new ConcurrentSkipListSet<>(new Comparator() { + public int compare(Integer o1, Integer o2) { + + return o1.compareTo(o2); + } + }); + + /** + * key和访问次数据映射的hashmap + */ + private ConcurrentHashMap keyAccessCount = new ConcurrentHashMap<>(); + + /** + * 访问映射字段:次数和这一类key的映射排序分类处理 + */ + private ConcurrentHashMap> accessCountQueue = new ConcurrentHashMap<>(); + + @Override + public void itemPut(K key) { + + if (keyAccessCount.containsKey(key)) {//已经包含该key,则该怎么办 + /** + * 先把这个key取出来,然后增加次数,放到适当的等级中 + *这个时候就成了一个获取时的操作了,改变其次序 + */ + log.info("[HighCache]-LFUConcurrentCache.itemPut and containsKey:" + key); + this.itemRetrieved(key); + } else {//不包含该怎么办 + //得到一次级别上的所有map对象,放到第一个级别上 + LinkedBlockingQueue accessCountQueue = this.accessCountQueue.get(1); + if (accessCountQueue == null) { + //表示系统的第一次 + accessCountQueue = new LinkedBlockingQueue(); + this.accessCountQueue.put(1, accessCountQueue); + } + + accessCountQueue.add(key); + if (!this.accessCountSort.contains(1)) { + this.accessCountSort.add(1); + } + + this.keyAccessCount.put(key, 1); + log.debug("[HighCache]-LFUConcurrentCache.itemPut and first:" + key); + //一次处理处理完成 + } + } + + /** + * 当从缓存容器中get操作,促发该动作,目的就是改变key的一个排序 + */ + @Override + public void itemRetrieved(K key) { + //1、获取当前key的一个访问次数, + if (!keyAccessCount.containsKey(key)) { + return; + } + + Integer accessCount = keyAccessCount.get(key); + //获取这个访问次次数的一批key + LinkedBlockingQueue accessCountQueue = this.accessCountQueue.get(accessCount); + //从当前这个等级remove 掉,往下一个等级放 + accessCountQueue.remove(key); + + if (accessCountQueue.isEmpty()) { + this.accessCountQueue.remove(accessCount); + accessCountQueue.clear(); + accessCountQueue = null; + //不需要在排序列表里面 + accessCountSort.remove(accessCount); + } + //对这访问次数加一 + accessCount++; + log.debug("[HighCache]-LFUConcurrentCache.itemRetrieved and the access count is" + accessCount); + //获得下一个等级 + LinkedBlockingQueue nextLevelAccessCountQueue = this.accessCountQueue.get(accessCount); + + if (nextLevelAccessCountQueue == null) { + nextLevelAccessCountQueue = new LinkedBlockingQueue(); + this.accessCountQueue.put(accessCount, nextLevelAccessCountQueue); + } + //然后在这一个等级内添加key和他访问次数的一个映射 + nextLevelAccessCountQueue.add(key); + //更新当前 key的访问次数 + this.keyAccessCount.put(key, accessCount); + //必须的放进去 + log.debug("访问频率最高的次数为:" + accessCount + ",and the key is:" + key); + if (!this.accessCountSort.contains(accessCount)) { + accessCountSort.add(accessCount); + } + //一次处理完成 + } + + /** + * 缓存容器中remove时,促发该动作,移除该key,重新保持最新的次序 + */ + @Override + public void itemRemoved(K key) { + + if (!this.keyAccessCount.containsKey(key)) + return; + + Integer count = keyAccessCount.remove(key);//移除这个key所对应的访问次数 + //获取这个访问次次数的一批key + LinkedBlockingQueue accessCountQueue = this.accessCountQueue.get(count); + accessCountQueue.remove(key); + log.debug("the remove key is: " + key + " and the access count is: " + count); + } + + /** + * 一级缓存和二级缓存兑换时,自动替换一个缓存实体,这里是核心 + *

+ * 有个一次级别全部扫描的关系在里面,对相同的次数又该如何处理 + * ,这里一定要确保移除掉,不要就会影响命中率 + */ + @Override + public K removeItem(boolean isRemove) { + K key = null; + //从低级别的开始扫描,也即排序迭代 + for (Integer count : this.accessCountSort) { + LinkedBlockingQueue accessCountQueue = this.accessCountQueue.get(count); + if (accessCountQueue.isEmpty())//这个时候表明这个访问层已经清空,则跳到下一层 + continue; + //移除这一类级别中的任何一个数,因为这些的访问次数是相同的 + else { + //并将在HashMap中的已移除的 + if (isRemove) { + key = accessCountQueue.poll(); + keyAccessCount.remove(key); + } else { + key = accessCountQueue.peek(); + } + break; + } + } + //如果正常,则应该替换村次数最少的 + + return key; + } } diff --git a/event-swift/src/main/java/com/ware/swift/event/mcache/LRFUAbstractConcurrentCache.java b/event-swift/src/main/java/com/ware/swift/event/mcache/LRFUAbstractConcurrentCache.java index 7568dbc..8c81e91 100644 --- a/event-swift/src/main/java/com/ware/swift/event/mcache/LRFUAbstractConcurrentCache.java +++ b/event-swift/src/main/java/com/ware/swift/event/mcache/LRFUAbstractConcurrentCache.java @@ -1,6 +1,9 @@ package com.ware.swift.event.mcache; import com.ware.swift.event.parallel.IParallelQueueExecutor; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.util.Comparator; import java.util.Iterator; import java.util.Map; @@ -8,252 +11,249 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentSkipListSet; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - /** * 最少使用缓存置换算法的实现latest-seldom use cache - * + * * @author pbting */ -public abstract class LRFUAbstractConcurrentCache extends AbstractConcurrentCache { - - /** - * - */ - private static final long serialVersionUID = 1L; - // 定义用来存放访问次数Set集合 - private Set cacheRateSort = new ConcurrentSkipListSet(new Comparator() { - public int compare(Float o1, Float o2) { - // 从小到大排序 - return o1.compareTo(o2); - }; - }); - - /** - * key和访问次数据映射的hashmap - */ - private ConcurrentHashMap keyCacheRataMap = new ConcurrentHashMap(); - - /** - * 访问映射字段:次数和这一类key的映射排序分类处理 - */ - private ConcurrentHashMap> cacheRateMapList = new ConcurrentHashMap>(); - - - public LRFUAbstractConcurrentCache(String cacheTopic, IParallelQueueExecutor IParallelActionExecutor, +public abstract class LRFUAbstractConcurrentCache extends AbstractConcurrentCache { + + /** + * + */ + private static final long serialVersionUID = 1L; + // 定义用来存放访问次数Set集合 + private Set cacheRateSort = new ConcurrentSkipListSet(new Comparator() { + public int compare(Float o1, Float o2) { + // 从小到大排序 + return o1.compareTo(o2); + } + + ; + }); + + /** + * key和访问次数据映射的hashmap + */ + private ConcurrentHashMap keyCacheRateMap = new ConcurrentHashMap<>(); + + /** + * 访问映射字段:次数和这一类key的映射排序分类处理 + */ + private ConcurrentHashMap> cacheRateMapList = new ConcurrentHashMap<>(); + + + public LRFUAbstractConcurrentCache(String cacheTopic, IParallelQueueExecutor IParallelActionExecutor, IExpireKeyHandler iExpireKeyAdaptor, int initialCapacity, float loadFactor) { - super(cacheTopic,IParallelActionExecutor, iExpireKeyAdaptor, initialCapacity, loadFactor); - } - - public LRFUAbstractConcurrentCache(String cacheTopic,IParallelQueueExecutor IParallelActionExecutor, - IExpireKeyHandler iExpireKeyAdaptor, int initialCapacity) { - super(cacheTopic,IParallelActionExecutor, iExpireKeyAdaptor, initialCapacity); - } - - public LRFUAbstractConcurrentCache(String cacheTopic,IParallelQueueExecutor IParallelActionExecutor, - IExpireKeyHandler iExpireKeyAdaptor) { - super(cacheTopic,IParallelActionExecutor, iExpireKeyAdaptor); - } - - public LRFUAbstractConcurrentCache(String cacheTopic,IExpireKeyHandler iExpireKeyAdaptor) { - super(cacheTopic,DEFAULT_CACHE_QUEUE_EXECUTOR, iExpireKeyAdaptor); - } - - private static final Logger log = LoggerFactory.getLogger(LRFUAbstractConcurrentCache.class); - - // 给一个增强因子,默认为10 - protected final static int EFACTOR = 2 << 4; - - @Override - public void itemPut(K key) { - if (keyCacheRataMap.containsKey(key)) {// 已经包含该key,则该怎么办 - /** - * 先把这个key取出来,然后增加次数,放到适当的等级中 这个时候就成了一个获取时的操作了,改变其次序 - */ - log.info("[HighCache]-LFUConcurrentCache.itemPut and containsKey:" + key); - this.itemRetrieved(key); - } else {// 不包含该怎么办 - SRUKey lfuKey = new SRUKey(key); - lfuKey.incrementCount();// 对次数进行加一,同时还有一些任务需要处理, - // 得到这个key的响应比 - float cacheRate = lfuKey.getCacheRate(); - // 得到一次级别上的所有map对象,放到第一个级别上 - Map ES = this.cacheRateMapList.get(cacheRate); - - if (ES == null) { - // 表示系统的第一次 - ES = new ConcurrentHashMap(); - this.cacheRateMapList.put(cacheRate, ES); - } - - ES.put(key, lfuKey); - // 如果为空,则表示第一次的,以后的第一次就可以不添加了,目的是排序 - if (!this.cacheRateSort.contains(cacheRate)){ - this.cacheRateSort.add(cacheRate); - } - - // 就算有一次也要记录下来 - this.keyCacheRataMap.put(key, cacheRate); - log.debug("[HighCache]-LFUConcurrentCache.itemPut and first:" + key); - // 一次处理处理完成 - } - } - - /** - * 当从缓存容器中get操作,促发该动作,目的就是改变key的一个排序 - */ - @Override - public void itemRetrieved(K key) { - // 如果这个key都没有响应的缓存响应因子,则直接返回 - if (!keyCacheRataMap.containsKey(key)){ - return; - } - - // 1、获取当前key的一个响应比, - Float cacheRate = keyCacheRataMap.get(key); - // 获取这个访问次次数的一批key - Map keyMap = this.cacheRateMapList.get(cacheRate); - // 这个时候存在再次放入,则提高一次级别,的从原来的级别中移除 - SRUKey lFUKey = keyMap.remove(key); - //防止内存持续增长,需要判断:如果当前这个 响应因子层级没有对应于其他的key了,则立即回收 - if(keyMap.isEmpty()){ - this.cacheRateMapList.remove(cacheRate); - keyMap.clear(); - keyMap = null ; - //同时,也不需要在排序列表里面了 - cacheRateSort.remove(cacheRate); - } - - // 对这访问次数加一,同时得到新的一个响应比 - lFUKey.incrementCount(); - cacheRate = lFUKey.getCacheRate(); - - log.debug("[HighCache]-LFUConcurrentCache.itemRetrieved and the access count is" + cacheRate); - // 根据新的响应比,获取该响应比相同的key-sets - Map nextLevelKeyMap = this.cacheRateMapList.get(cacheRate); - - if (nextLevelKeyMap == null) { - nextLevelKeyMap = new ConcurrentHashMap(); - this.cacheRateMapList.put(cacheRate, nextLevelKeyMap); - } - // 然后在这一个等级内添加key和他访问次数的一个映射 - nextLevelKeyMap.put(key, lFUKey); - //更新 - this.keyCacheRataMap.put(key, cacheRate); - // 必须的放进去 - // 对这个访问次数进行排序,重复的不会被添加,表明出现访问次数最高的 - log.debug("访问频率最高的次数为:" + lFUKey.count + ",and the key is:" + lFUKey.key); - - if (!this.cacheRateSort.contains(cacheRate)){ - cacheRateSort.add(cacheRate); - } - // 一次处理完成 - } - - /** - * 缓存容器中remove时,促发该动作,移除该key,重新保持最新的次序 - */ - @Override - public void itemRemoved(K key) { - // 如果这个key不存在响应的缓存相应因子。则直接返回 - if (!keyCacheRataMap.containsKey(key)){ - return; - } - - Float cacheRate = keyCacheRataMap.remove(key); - // 获取这个访问次次数的一批key - Map levelLFUKeys = this.cacheRateMapList.get(cacheRate); - // 这个时候存在再次放入,则提高一次级别,的从原来的级别中移除 - SRUKey removeKey = levelLFUKeys.remove(key); - //如果当前缓存因子没有对应的元素,则将在排序中的元素也 remove 掉 - if(levelLFUKeys.isEmpty()){ - cacheRateSort.remove(cacheRate); - } - log.debug("the remove key is" + removeKey.key + " and the access count is:" + removeKey.currentCacheRate); - } - - /** - * 一级缓存和二级缓存兑换时,自动替换一个缓存实体,这里是核心 有个一次级别全部扫描的关系在里面,对相同的次数又该如何处理 , - * 注意:这里仅仅是找一个 remove 的key,真正remove 的操作,是通过 itemRemoved 方法来移除的 - */ - @Override - public K removeItem(boolean isRemove) { - // 要记录哪一组哪一个key,然后才可以更好的移除 - K removeKey = null; - // 从低级别的开始扫描,也即排序迭代 - try { - for (Float cacheRate : this.cacheRateSort) { - Map levelLFUKeys = this.cacheRateMapList.get(cacheRate); - if (levelLFUKeys != null && levelLFUKeys.isEmpty()) {// 这个时候表明这个访问层已经清空,则跳到下一层 - continue; - // 移除这一类级别中的任何一个数,因为这些的访问次数是相同的 - } else { - // 得到key的集合,移除响应比最小的 - Iterator iter = levelLFUKeys.keySet().iterator(); - removeKey = iter.next(); - if(isRemove){ - iter.remove(); - keyCacheRataMap.remove(removeKey); - } - break; - } - } - } catch (Exception e) { - } - // 返回真正的 - return removeKey; - } - - protected volatile Long createTime = null; - - public class SRUKey { - protected K key; - protected int count = 0; - protected long lastAccessTime = 0; - long lastIntervalTime = 0; - float lastFactor = 0.0f; - float lastDx = 0.0f; - Float currentCacheRate = 0F; - - // 记下这个key的创建时间 - public SRUKey(K key) { - this.key = key; - if (createTime == null){ - createTime = System.nanoTime(); - } - - this.lastAccessTime = System.nanoTime(); - } - - // 每次设置这个count值时表示一次访问,则这个时候更改lastAccessTime 的时间 - public void incrementCount() { - this.count++; - // 记录遇上一次访问时间的一个间隔 - lastIntervalTime = System.nanoTime() - this.lastAccessTime + EFACTOR; - this.lastAccessTime = System.nanoTime(); - } - - /** - * 计算这个key的一个缓存响应比,如果这个缓存响应比越高,则越不应该留下,越小,则越应该留下 - */ - public Float getCacheRate() { - // 根据方差值来计算缓存响应比 - float factor = getFactor(this); - //注意区分这两种算法的实现 - if(LRFUAbstractConcurrentCache.this instanceof LRFUByDxConcurrentCache){ - factor = (float) Math.sqrt(factor); - factor = (float) Math.sqrt(factor); -// this.currentCacheRate = Math.abs(Float.valueOf(this.count * this.count) / (float) Math.sqrt(factor)); - this.currentCacheRate = Math.abs(Float.valueOf(this.count * this.count) / (float) factor); - }else if(LRFUAbstractConcurrentCache.this instanceof LRFUByExConcurrentCache){ - this.currentCacheRate = Math.abs((float) Math.sqrt(factor * this.count)); - } - this.currentCacheRate = (float) Math.sqrt(currentCacheRate); - this.currentCacheRate = (float) Math.sqrt(currentCacheRate); - return this.currentCacheRate; - } - } - - public abstract float getFactor(SRUKey sruKey); + super(cacheTopic, IParallelActionExecutor, iExpireKeyAdaptor, initialCapacity, loadFactor); + } + + public LRFUAbstractConcurrentCache(String cacheTopic, IParallelQueueExecutor IParallelActionExecutor, + IExpireKeyHandler iExpireKeyAdaptor, int initialCapacity) { + super(cacheTopic, IParallelActionExecutor, iExpireKeyAdaptor, initialCapacity); + } + + public LRFUAbstractConcurrentCache(String cacheTopic, IParallelQueueExecutor IParallelActionExecutor, + IExpireKeyHandler iExpireKeyAdaptor) { + super(cacheTopic, IParallelActionExecutor, iExpireKeyAdaptor); + } + + public LRFUAbstractConcurrentCache(String cacheTopic, IExpireKeyHandler iExpireKeyAdaptor) { + super(cacheTopic, DEFAULT_CACHE_QUEUE_EXECUTOR, iExpireKeyAdaptor); + } + + private static final Logger log = LoggerFactory.getLogger(LRFUAbstractConcurrentCache.class); + + // 给一个增强因子,默认为10 + protected final static int EFACTOR = 2 << 4; + + @Override + public void itemPut(K key) { + if (keyCacheRateMap.containsKey(key)) {// 已经包含该key,则该怎么办 + /** + * 先把这个key取出来,然后增加次数,放到适当的等级中 这个时候就成了一个获取时的操作了,改变其次序 + */ + log.info("[HighCache]-LFUConcurrentCache.itemPut and containsKey:" + key); + this.itemRetrieved(key); + } else {// 不包含该怎么办 + SRUKey lfuKey = new SRUKey(key); + lfuKey.incrementCount();// 对次数进行加一,同时还有一些任务需要处理, + // 得到这个key的响应比 + float cacheRate = lfuKey.getCacheRate(); + // 得到一次级别上的所有map对象,放到第一个级别上 + Map ES = this.cacheRateMapList.get(cacheRate); + + if (ES == null) { + // 表示系统的第一次 + ES = new ConcurrentHashMap<>(); + this.cacheRateMapList.put(cacheRate, ES); + } + + ES.put(key, lfuKey); + // 如果为空,则表示第一次的,以后的第一次就可以不添加了,目的是排序 + if (!this.cacheRateSort.contains(cacheRate)) { + this.cacheRateSort.add(cacheRate); + } + + // 就算有一次也要记录下来 + this.keyCacheRateMap.put(key, cacheRate); + log.debug("[HighCache]-LFUConcurrentCache.itemPut and first:" + key); + // 一次处理处理完成 + } + } + + /** + * 当从缓存容器中get操作,促发该动作,目的就是改变key的一个排序 + */ + @Override + public void itemRetrieved(K key) { + // 如果这个key都没有响应的缓存响应因子,则直接返回 + if (!keyCacheRateMap.containsKey(key)) { + return; + } + + // 1、获取当前key的一个响应比, + Float cacheRate = keyCacheRateMap.get(key); + // 获取这个访问次次数的一批key + Map keyMap = this.cacheRateMapList.get(cacheRate); + // 这个时候存在再次放入,则提高一次级别,的从原来的级别中移除 + SRUKey lFUKey = keyMap.remove(key); + //防止内存持续增长,需要判断:如果当前这个 响应因子层级没有对应于其他的key了,则立即回收 + if (keyMap.isEmpty()) { + this.cacheRateMapList.remove(cacheRate); + keyMap.clear(); + //同时,也不需要在排序列表里面了 + cacheRateSort.remove(cacheRate); + } + + // 对这访问次数加一,同时得到新的一个响应比 + lFUKey.incrementCount(); + cacheRate = lFUKey.getCacheRate(); + + log.debug("[HighCache]-LFUConcurrentCache.itemRetrieved and the access count is" + cacheRate); + // 根据新的响应比,获取该响应比相同的key-sets + Map nextLevelKeyMap = this.cacheRateMapList.get(cacheRate); + + if (nextLevelKeyMap == null) { + nextLevelKeyMap = new ConcurrentHashMap<>(); + this.cacheRateMapList.put(cacheRate, nextLevelKeyMap); + } + // 然后在这一个等级内添加key和他访问次数的一个映射 + nextLevelKeyMap.put(key, lFUKey); + //更新 + this.keyCacheRateMap.put(key, cacheRate); + // 必须的放进去 + // 对这个访问次数进行排序,重复的不会被添加,表明出现访问次数最高的 + log.debug("访问频率最高的次数为:" + lFUKey.count + ",and the key is:" + lFUKey.key); + + if (!this.cacheRateSort.contains(cacheRate)) { + cacheRateSort.add(cacheRate); + } + // 一次处理完成 + } + + /** + * 缓存容器中remove时,促发该动作,移除该key,重新保持最新的次序 + */ + @Override + public void itemRemoved(K key) { + // 如果这个key不存在响应的缓存相应因子。则直接返回 + if (!keyCacheRateMap.containsKey(key)) { + return; + } + + Float cacheRate = keyCacheRateMap.remove(key); + // 获取这个访问次次数的一批key + Map levelLFUKeys = this.cacheRateMapList.get(cacheRate); + // 这个时候存在再次放入,则提高一次级别,的从原来的级别中移除 + SRUKey removeKey = levelLFUKeys.remove(key); + //如果当前缓存因子没有对应的元素,则将在排序中的元素也 remove 掉 + if (levelLFUKeys.isEmpty()) { + cacheRateSort.remove(cacheRate); + } + log.debug("the remove key is" + removeKey.key + " and the access count is:" + removeKey.currentCacheRate); + } + + /** + * 一级缓存和二级缓存兑换时,自动替换一个缓存实体,这里是核心 有个一次级别全部扫描的关系在里面,对相同的次数又该如何处理 , + * 注意:这里仅仅是找一个 remove 的key,真正remove 的操作,是通过 itemRemoved 方法来移除的 + */ + @Override + public K removeItem(boolean isRemove) { + // 要记录哪一组哪一个key,然后才可以更好的移除 + K removeKey = null; + // 从低级别的开始扫描,也即排序迭代 + try { + for (Float cacheRate : this.cacheRateSort) { + Map levelLFUKeys = this.cacheRateMapList.get(cacheRate); + if (levelLFUKeys != null && levelLFUKeys.isEmpty()) {// 这个时候表明这个访问层已经清空,则跳到下一层 + continue; + // 移除这一类级别中的任何一个数,因为这些的访问次数是相同的 + } else { + // 得到key的集合,移除响应比最小的 + Iterator iter = levelLFUKeys.keySet().iterator(); + removeKey = iter.next(); + if (isRemove) { + iter.remove(); + keyCacheRateMap.remove(removeKey); + } + break; + } + } + } catch (Exception e) { + } + // 返回真正的 + return removeKey; + } + + protected volatile Long createTime = null; + + public class SRUKey { + protected K key; + protected int count = 0; + protected long lastAccessTime = 0; + long lastIntervalTime = 0; + float lastFactor = 0.0f; + float lastDx = 0.0f; + Float currentCacheRate = 0F; + + // 记下这个key的创建时间 + public SRUKey(K key) { + this.key = key; + if (createTime == null) { + createTime = System.nanoTime(); + } + + this.lastAccessTime = System.nanoTime(); + } + + // 每次设置这个count值时表示一次访问,则这个时候更改lastAccessTime 的时间 + public void incrementCount() { + this.count++; + // 记录遇上一次访问时间的一个间隔 + lastIntervalTime = System.nanoTime() - this.lastAccessTime + EFACTOR; + this.lastAccessTime = System.nanoTime(); + } + + /** + * 计算这个key的一个缓存响应比,如果这个缓存响应比越高,则越不应该留下,越小,则越应该留下 + */ + public Float getCacheRate() { + // 根据方差值来计算缓存响应比 + float factor = getFactor(this); + //注意区分这两种算法的实现 + if (LRFUAbstractConcurrentCache.this instanceof LRFUByDxConcurrentCache) { + factor = (float) Math.sqrt(factor); + factor = (float) Math.sqrt(factor); + this.currentCacheRate = Math.abs(Float.valueOf(this.count * this.count) / factor); + } else if (LRFUAbstractConcurrentCache.this instanceof LRFUByExConcurrentCache) { + this.currentCacheRate = Math.abs((float) Math.sqrt(factor * this.count)); + } + this.currentCacheRate = (float) Math.sqrt(currentCacheRate); + this.currentCacheRate = (float) Math.sqrt(currentCacheRate); + return this.currentCacheRate; + } + } + + public abstract float getFactor(SRUKey sruKey); } diff --git a/event-swift/src/main/java/com/ware/swift/event/mcache/LRFUByDxConcurrentCache.java b/event-swift/src/main/java/com/ware/swift/event/mcache/LRFUByDxConcurrentCache.java index f98ea43..65edbb8 100644 --- a/event-swift/src/main/java/com/ware/swift/event/mcache/LRFUByDxConcurrentCache.java +++ b/event-swift/src/main/java/com/ware/swift/event/mcache/LRFUByDxConcurrentCache.java @@ -4,49 +4,53 @@ /** * 最少使用缓存置换算法的实现latest-seldom use cache - * + * * @author pbting */ -public class LRFUByDxConcurrentCache extends LRFUAbstractConcurrentCache { - - /** - * - */ - private static final long serialVersionUID = 1L; - - public LRFUByDxConcurrentCache(String cacheTopic, IParallelQueueExecutor IParallelQueueExecutor, - IExpireKeyHandler iExpireKeyAdaptor, int initialCapacity, float loadFactor) { - super(cacheTopic, IParallelQueueExecutor, iExpireKeyAdaptor, initialCapacity, loadFactor); - } - - public LRFUByDxConcurrentCache(String cacheTopic, IParallelQueueExecutor IParallelQueueExecutor, - IExpireKeyHandler iExpireKeyAdaptor, int initialCapacity) { - super(cacheTopic, IParallelQueueExecutor, iExpireKeyAdaptor, initialCapacity); - } - - public LRFUByDxConcurrentCache(String cacheTopic, IParallelQueueExecutor IParallelQueueExecutor,int initialCapacity) { - super(cacheTopic, IParallelQueueExecutor, new DefaultExpireKeyAdaptor(), initialCapacity); - } - - public LRFUByDxConcurrentCache(String cacheTopic, IParallelQueueExecutor IParallelQueueExecutor, - IExpireKeyHandler iExpireKeyAdaptor) { - super(cacheTopic, IParallelQueueExecutor, iExpireKeyAdaptor); - } - - public LRFUByDxConcurrentCache(String cacheTopic,IExpireKeyHandler iExpireKeyAdaptor) { - super(cacheTopic,DEFAULT_CACHE_QUEUE_EXECUTOR, iExpireKeyAdaptor); - } - - @Override - public float getFactor(SRUKey sruKey) { - long currentTimeInterval = (System.nanoTime() - createTime) * 1000 + EFACTOR; - sruKey.lastFactor += (Float.valueOf(sruKey.lastIntervalTime) / currentTimeInterval) * sruKey.lastIntervalTime; - // 计算出来的结果太大,会影响均方差的计算导无穷大的情况,因此要进行一个降值的超过 - float Ex = (float) (Math.abs(sruKey.lastFactor * sruKey.lastFactor) / (Math.pow(3, 6))); - // 迭代计算计算均方差,即响应因子 - sruKey.lastDx += ((sruKey.lastIntervalTime - Ex) * (sruKey.lastIntervalTime - Ex) * (Float.valueOf(sruKey.lastIntervalTime) / currentTimeInterval)); - // 返回他的均方差 - return (float) Math.sqrt(sruKey.lastDx); - } +public class LRFUByDxConcurrentCache extends LRFUAbstractConcurrentCache { + + /** + * + */ + private static final long serialVersionUID = 1L; + + public LRFUByDxConcurrentCache(String cacheTopic, IParallelQueueExecutor IParallelQueueExecutor, + IExpireKeyHandler iExpireKeyAdaptor, int initialCapacity, float loadFactor) { + super(cacheTopic, IParallelQueueExecutor, iExpireKeyAdaptor, initialCapacity, loadFactor); + } + + public LRFUByDxConcurrentCache(String cacheTopic, IParallelQueueExecutor IParallelQueueExecutor, + IExpireKeyHandler iExpireKeyAdaptor, int initialCapacity) { + super(cacheTopic, IParallelQueueExecutor, iExpireKeyAdaptor, initialCapacity); + } + + public LRFUByDxConcurrentCache(String cacheTopic, IParallelQueueExecutor IParallelQueueExecutor, int initialCapacity) { + super(cacheTopic, IParallelQueueExecutor, new DefaultExpireKeyAdaptor<>(), initialCapacity); + } + + public LRFUByDxConcurrentCache(String cacheTopic, IParallelQueueExecutor IParallelQueueExecutor, + IExpireKeyHandler iExpireKeyAdaptor) { + super(cacheTopic, IParallelQueueExecutor, iExpireKeyAdaptor); + } + + public LRFUByDxConcurrentCache(String cacheTopic, IExpireKeyHandler iExpireKeyAdaptor) { + super(cacheTopic, DEFAULT_CACHE_QUEUE_EXECUTOR, iExpireKeyAdaptor); + } + + public LRFUByDxConcurrentCache(String cacheTopic, IExpireKeyHandler iExpireKeyAdaptor, int initialCapacity) { + super(cacheTopic, DEFAULT_CACHE_QUEUE_EXECUTOR, iExpireKeyAdaptor, initialCapacity); + } + + @Override + public float getFactor(SRUKey sruKey) { + long currentTimeInterval = (System.nanoTime() - createTime) * 1000 + EFACTOR; + sruKey.lastFactor += (Float.valueOf(sruKey.lastIntervalTime) / currentTimeInterval) * sruKey.lastIntervalTime; + // 计算出来的结果太大,会影响均方差的计算导无穷大的情况,因此要进行一个降值的超过 + float Ex = (float) (Math.abs(sruKey.lastFactor * sruKey.lastFactor) / (Math.pow(3, 6))); + // 迭代计算计算均方差,即响应因子 + sruKey.lastDx += ((sruKey.lastIntervalTime - Ex) * (sruKey.lastIntervalTime - Ex) * (Float.valueOf(sruKey.lastIntervalTime) / currentTimeInterval)); + // 返回他的均方差 + return (float) Math.sqrt(sruKey.lastDx); + } } diff --git a/event-swift/src/main/java/com/ware/swift/event/mcache/LRUConcurrentCache.java b/event-swift/src/main/java/com/ware/swift/event/mcache/LRUConcurrentCache.java index 689c14e..8f942a9 100644 --- a/event-swift/src/main/java/com/ware/swift/event/mcache/LRUConcurrentCache.java +++ b/event-swift/src/main/java/com/ware/swift/event/mcache/LRUConcurrentCache.java @@ -8,62 +8,60 @@ /** * LRU(long-recently) 最近最久未使用,是根据上一次访问时间到目前为止最长的时间进行替换,在这段时间内不考虑他的使用频率,是考虑他的 * 访问时间 - * - * @author pbting * + * @author pbting */ public class LRUConcurrentCache extends AbstractConcurrentCache { - private static final long serialVersionUID = -7379608101794788534L; + private static final long serialVersionUID = -7379608101794788534L; + + private LinkedBlockingQueue list = new LinkedBlockingQueue(); - private LinkedBlockingQueue list = new LinkedBlockingQueue(); + public LRUConcurrentCache(String cacheTopic, IParallelQueueExecutor IParallelQueueExecutor, + IExpireKeyHandler iExpireKeyAdaptor, int initialCapacity, float loadFactor) { + super(cacheTopic, IParallelQueueExecutor, iExpireKeyAdaptor, initialCapacity, loadFactor); + } - public LRUConcurrentCache(String cacheTopic, IParallelQueueExecutor IParallelQueueExecutor, - IExpireKeyHandler iExpireKeyAdaptor, int initialCapacity, float loadFactor) { - super(cacheTopic, IParallelQueueExecutor, iExpireKeyAdaptor, initialCapacity, loadFactor); - } + public LRUConcurrentCache(String cacheTopic, IParallelQueueExecutor IParallelQueueExecutor, + IExpireKeyHandler iExpireKeyAdaptor, int initialCapacity) { + super(cacheTopic, IParallelQueueExecutor, iExpireKeyAdaptor, initialCapacity); + } - public LRUConcurrentCache(String cacheTopic, IParallelQueueExecutor IParallelQueueExecutor, - IExpireKeyHandler iExpireKeyAdaptor, int initialCapacity) { - super(cacheTopic, IParallelQueueExecutor, iExpireKeyAdaptor, initialCapacity); - } + public LRUConcurrentCache(String cacheTopic, IParallelQueueExecutor IParallelQueueExecutor, + IExpireKeyHandler iExpireKeyAdaptor) { + super(cacheTopic, IParallelQueueExecutor, iExpireKeyAdaptor); + } - public LRUConcurrentCache(String cacheTopic, IParallelQueueExecutor IParallelQueueExecutor, - IExpireKeyHandler iExpireKeyAdaptor) { - super(cacheTopic, IParallelQueueExecutor, iExpireKeyAdaptor); - } + public LRUConcurrentCache(String cacheTopic, IExpireKeyHandler iExpireKeyAdaptor) { + super(cacheTopic, DEFAULT_CACHE_QUEUE_EXECUTOR, iExpireKeyAdaptor); + } - public LRUConcurrentCache(String cacheTopic,IExpireKeyHandler iExpireKeyAdaptor) { - super(cacheTopic,DEFAULT_CACHE_QUEUE_EXECUTOR, iExpireKeyAdaptor); - } - - // 这是为改变key的顺序而为子类留下的接口 - public void itemRetrieved(K key) { - // 原子操作需同步 - list.remove(key); - list.add(key); - } + // 这是为改变key的顺序而为子类留下的接口 + public void itemRetrieved(K key) { + // 原子操作需同步 + list.remove(key); + list.add(key); + } - /** - * 往里面放一个元素时,不管之前有没有,都remove掉,然后插入到 - */ - public void itemPut(K key) { - list.remove(key); - list.add(key); - } + /** + * 往里面放一个元素时,不管之前有没有,都remove掉,然后插入到 + */ + public void itemPut(K key) { + list.remove(key); + list.add(key); + } - public K removeItem(boolean isRemove) { - K toRemove = null; - Iterator it = list.iterator(); - toRemove = (K) it.next(); - if (isRemove) { - it.remove();// - } - return toRemove; - } + public K removeItem(boolean isRemove) { + Iterator it = list.iterator(); + K toRemove = it.next(); + if (isRemove) { + it.remove(); + } + return toRemove; + } - public void itemRemoved(K key) { + public void itemRemoved(K key) { - list.remove(key); - } + list.remove(key); + } } diff --git a/event-swift/src/main/java/com/ware/swift/event/mcache/MCacheManager.java b/event-swift/src/main/java/com/ware/swift/event/mcache/MCacheManager.java index 9599932..ae07c22 100644 --- a/event-swift/src/main/java/com/ware/swift/event/mcache/MCacheManager.java +++ b/event-swift/src/main/java/com/ware/swift/event/mcache/MCacheManager.java @@ -18,242 +18,242 @@ import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock; class MCacheManager extends AbstractAsyncEventLoopGroup { - public final static int checkCapacityEvent = 1;//单个事件循环处理的过程 - public final static int cacheRateUpdateEvent = 2;//多个事件异步串行处理,不需要多次执行 - public final static int clearCacheEvent = 3 ; - public final static int cacheExpireEvent = 4 ;//缓存过期检测 - - private AbstractConcurrentCache abstractConcurrentCache; - private DefaultPipelineEventObject defaultPipelineEventObject = new DefaultPipelineEventObject(true); - private ReentrantReadWriteLock reentrantLock = new ReentrantReadWriteLock(true);// new ReentrantLock(true); - - public MCacheManager(AbstractConcurrentCache abstractConcurrentCache,IParallelQueueExecutor executor, boolean isOptimism) { - super(executor, isOptimism); - this.abstractConcurrentCache = abstractConcurrentCache; - init(); - } - - private void init(){ - // 异步实时计算每个key 的响应因子。这里会有一个问题:在高并发的情况下,会产生很多这种访问日志,如果 cpu - // 处理能力不够,那么队列里面会积压未处理的 - defaultPipelineEventObject.addLast(new ItemPutCacheRateUpdateListener(), cacheRateUpdateEvent); - defaultPipelineEventObject.addLast(new ItemRetriveCacheRateUpdateListener(), cacheRateUpdateEvent); - defaultPipelineEventObject.addLast(new ItemRemoveCacheRateUpdateListener(), cacheRateUpdateEvent); - // - this.addLast(new ItemExpireCheckListener(), cacheExpireEvent); - this.publish(new Object(), cacheExpireEvent); - } - - @Override - public void attachListener() { - this.addLast(new IPipelineEventListener() { - @Override - public boolean onEvent(ObjectEvent event, int listenerIndex) { - int currentSize = abstractConcurrentCache.size(); - int maxEntries = abstractConcurrentCache.getMaxEntries(); - - if((abstractConcurrentCache.getEpollCache() > 0 && (currentSize - maxEntries < 50 )) || abstractConcurrentCache.getIsClearCache()){ - event.setParameter(EventLoopConstants.EVENT_LOOP_INTERVAL_PARAM, 1); - return false ; - } - - while(removeCheck()) { - final K key = abstractConcurrentCache.removeItem(true); - if(key == null){ - break; - } - final V value = abstractConcurrentCache.remove(key,false); - //异步触发 - abstractConcurrentCache.getiParallelQueueExecutor().execute(new Runnable() { - @Override - public void run() { - abstractConcurrentCache.getiExpireKeyAdaptor().expire(key, value,abstractConcurrentCache); - } - }); - } - event.setParameter(EventLoopConstants.EVENT_LOOP_INTERVAL_PARAM,TimeUnit.SECONDS.toMillis(1)); - //返回false 表示还没有结束,进行下一次 循环处理。单个事件循环处理的过程 - return false; - } - - }, checkCapacityEvent); - - this.addLast(new IPipelineEventListener() { - @Override - public boolean onEvent(ObjectEvent event, int listenerIndex) { - for (Iterator iter = abstractConcurrentCache.keySet().iterator(); iter.hasNext();) { - K key = (K) iter.next(); - abstractConcurrentCache.itemRemoved(key); - } - abstractConcurrentCache.clear(); - abstractConcurrentCache.setClearFinish(); - return false; - } - }, clearCacheEvent); - } - - public class ItemPutCacheRateUpdateListener implements IPipelineEventListener { - - @SuppressWarnings({ "unchecked", "rawtypes" }) - @Override - public boolean onEvent(ObjectEvent event, int listenerIndex) { - UpdateCacheRateEvent rateEvent = (UpdateCacheRateEvent) event; - if((rateEvent.operatorType & abstractConcurrentCache.ITEM_PUT_OP) > 0){ - K key = (K) rateEvent.key; - WriteLock writerock = reentrantLock.writeLock(); - try { - writerock.lock(); - abstractConcurrentCache.itemPut(key); - } finally { - writerock.unlock(); - } - abstractConcurrentCache.decrementAndGetEpollCache(); - - //更新他的一个 过期时间 - long idleTimeout = abstractConcurrentCache.getIdleTimeOut(key); - if (idleTimeout>0) { - abstractConcurrentCache.updateExpireTime(key,System.currentTimeMillis() + idleTimeout); - } - } - return true; - } - } - - public class ItemRetriveCacheRateUpdateListener implements IPipelineEventListener { - - @SuppressWarnings({ "unchecked", "rawtypes" }) - @Override - public boolean onEvent(ObjectEvent event, int listenerIndex) { - UpdateCacheRateEvent rateEvent = (UpdateCacheRateEvent) event; - if((rateEvent.operatorType & abstractConcurrentCache.ITEM_RETRIVE_OP) > 0){ - K key = (K) rateEvent.key; - WriteLock writerock = reentrantLock.writeLock(); - try { - writerock.lock(); - abstractConcurrentCache.itemRetrieved((K) rateEvent.key);// 使用缓存的缓存替换策略,改变其顺序 - } finally { - writerock.unlock(); - } - - abstractConcurrentCache.decrementAndGetEpollCache(); - //更新他的一个 过期时间 - long idleTimeout = abstractConcurrentCache.getIdleTimeOut(key); - if (idleTimeout>0) { - abstractConcurrentCache.updateExpireTime(key,System.currentTimeMillis() + idleTimeout); - } - } - - return true; - } - } - - public class ItemRemoveCacheRateUpdateListener implements IPipelineEventListener { - @SuppressWarnings({ "unchecked", "rawtypes" }) - @Override - public boolean onEvent(ObjectEvent event, int listenerIndex) { - UpdateCacheRateEvent rateEvent = (UpdateCacheRateEvent) event; - if((rateEvent.operatorType & abstractConcurrentCache.ITEM_REMOVE_OP) > 0){ - WriteLock writerock = reentrantLock.writeLock(); - try { - writerock.lock(); - abstractConcurrentCache.itemRemoved((K) rateEvent.key); - } finally { - writerock.unlock(); - } - - abstractConcurrentCache.decrementAndGetEpollCache(); - } - - return true; - } - } - - public class ItemExpireCheckListener implements IPipelineEventListener { - - @Override - public boolean onEvent(ObjectEvent event, int listenerIndex) { - System.out.println("check expire key------------>"); - if (abstractConcurrentCache == null){ - - return false ; - } - - ConcurrentMap expireKeysMap = abstractConcurrentCache.getKeyExpireTimeMap(); - if (expireKeysMap == null || expireKeysMap.isEmpty()){ - - return false ; - } - LinkedList expireKeyList = new LinkedList<>(); - for (Map.Entry entry : expireKeysMap.entrySet()){ - Long value = entry.getValue(); - if (System.currentTimeMillis() > value){ - //have expire,then remote the key - final K key = entry.getKey(); - final V cacheValue = abstractConcurrentCache.remove(key); - expireKeyList.add(key); - abstractConcurrentCache.getiParallelQueueExecutor().execute(new Runnable() { - @Override - public void run() { - abstractConcurrentCache.getiExpireKeyAdaptor().expire(key, cacheValue, abstractConcurrentCache); - - } - }); - } - } - return false ; - } - } - - @Override - public String partitioner(ObjectEvent objectEvent) { - if (abstractConcurrentCache == null){ - - return objectEvent.getEventTopic(); - } - String cacheTopic = abstractConcurrentCache.getCacheTopic(); - return cacheTopic; - } - - /** - * 是否需要检测 remove 的条件触发 - * 1、current cache entry > max entries - * 2、还没有其他操作cache 的线程 - * 3、没有触发 clear cache 的操作 - * @return - */ - public boolean removeCheck(){ - int currentSize = abstractConcurrentCache.size(); - int maxEntries = abstractConcurrentCache.getMaxEntries(); - - return currentSize > maxEntries && (abstractConcurrentCache.getEpollCache() <=0 || (currentSize - maxEntries > 50))&& !abstractConcurrentCache.getIsClearCache(); -// return abstractConcurrentCache.size() > abstractConcurrentCache.getMaxEntries() && !abstractConcurrentCache.getIsClearCache(); - } - - @SuppressWarnings({ "rawtypes", "unchecked" }) - public void updateCacheRate(UpdateCacheRateEvent rateEvent){ - - defaultPipelineEventObject.notifyListeners(rateEvent); - } - - public void handlerExpireCacheEntry(final AbstractConcurrentCache abstractConcurrentCache){ - ReadLock readLock = reentrantLock.readLock(); - try { - readLock.lock(); - final K key = abstractConcurrentCache.removeItem(true); - if(key == null){ - return ; - } - - final V value = abstractConcurrentCache.remove(key,false); - //异步触发 - abstractConcurrentCache.getiParallelQueueExecutor().execute(new Runnable() { - @Override - public void run() { - abstractConcurrentCache.getiExpireKeyAdaptor().expire(key, value,abstractConcurrentCache); - } - }); - } finally { - readLock.unlock(); - } - } + public final static int checkCapacityEvent = 1;//单个事件循环处理的过程 + public final static int cacheRateUpdateEvent = 2;//多个事件异步串行处理,不需要多次执行 + public final static int clearCacheEvent = 3; + public final static int cacheExpireEvent = 4;//缓存过期检测 + + private AbstractConcurrentCache abstractConcurrentCache; + private DefaultPipelineEventObject defaultPipelineEventObject = new DefaultPipelineEventObject<>(true); + private ReentrantReadWriteLock reentrantLock = new ReentrantReadWriteLock(true);// new ReentrantLock(true); + + public MCacheManager(AbstractConcurrentCache abstractConcurrentCache, IParallelQueueExecutor executor, boolean isOptimism) { + super(executor, isOptimism); + this.abstractConcurrentCache = abstractConcurrentCache; + init(); + } + + private void init() { + // 异步实时计算每个key 的响应因子。这里会有一个问题:在高并发的情况下,会产生很多这种访问日志,如果 cpu + // 处理能力不够,那么队列里面会积压未处理的 + defaultPipelineEventObject.addLast(new ItemPutCacheRateUpdateListener(), cacheRateUpdateEvent); + defaultPipelineEventObject.addLast(new ItemRetriveCacheRateUpdateListener(), cacheRateUpdateEvent); + defaultPipelineEventObject.addLast(new ItemRemoveCacheRateUpdateListener(), cacheRateUpdateEvent); + // + this.addLast(new ItemExpireCheckListener(), cacheExpireEvent); + this.publish(new Object(), cacheExpireEvent); + } + + @Override + public void attachListener() { + this.addLast(new IPipelineEventListener() { + @Override + public boolean onEvent(ObjectEvent event, int listenerIndex) { + int currentSize = abstractConcurrentCache.size(); + int maxEntries = abstractConcurrentCache.getMaxEntries(); + + if ((abstractConcurrentCache.getEpollCache() > 0 && (currentSize - maxEntries < 50)) || abstractConcurrentCache.getIsClearCache()) { + event.setParameter(EventLoopConstants.EVENT_LOOP_INTERVAL_PARAM, 1); + return false; + } + + while (removeCheck()) { + final K key = abstractConcurrentCache.removeItem(true); + if (key == null) { + break; + } + final V value = abstractConcurrentCache.remove(key, false); + //异步触发 + abstractConcurrentCache.getiParallelQueueExecutor().execute(new Runnable() { + @Override + public void run() { + abstractConcurrentCache.getiExpireKeyAdaptor().expire(key, value, abstractConcurrentCache); + } + }); + } + event.setParameter(EventLoopConstants.EVENT_LOOP_INTERVAL_PARAM, TimeUnit.SECONDS.toMillis(1)); + //返回false 表示还没有结束,进行下一次 循环处理。单个事件循环处理的过程 + return false; + } + + }, checkCapacityEvent); + + this.addLast(new IPipelineEventListener() { + @Override + public boolean onEvent(ObjectEvent event, int listenerIndex) { + for (Iterator iter = abstractConcurrentCache.keySet().iterator(); iter.hasNext(); ) { + K key = iter.next(); + abstractConcurrentCache.itemRemoved(key); + } + abstractConcurrentCache.clear(); + abstractConcurrentCache.setClearFinish(); + return false; + } + }, clearCacheEvent); + } + + public class ItemPutCacheRateUpdateListener implements IPipelineEventListener { + + @SuppressWarnings({"unchecked", "rawtypes"}) + @Override + public boolean onEvent(ObjectEvent event, int listenerIndex) { + UpdateCacheRateEvent rateEvent = (UpdateCacheRateEvent) event; + if ((rateEvent.operatorType & abstractConcurrentCache.ITEM_PUT_OP) > 0) { + K key = (K) rateEvent.key; + WriteLock writerock = reentrantLock.writeLock(); + try { + writerock.lock(); + abstractConcurrentCache.itemPut(key); + } finally { + writerock.unlock(); + } + abstractConcurrentCache.decrementAndGetEpollCache(); + + //更新他的一个 过期时间 + long idleTimeout = abstractConcurrentCache.getIdleTimeOut(key); + if (idleTimeout > 0) { + abstractConcurrentCache.updateExpireTime(key, System.currentTimeMillis() + idleTimeout); + } + } + return true; + } + } + + public class ItemRetriveCacheRateUpdateListener implements IPipelineEventListener { + + @SuppressWarnings({"unchecked", "rawtypes"}) + @Override + public boolean onEvent(ObjectEvent event, int listenerIndex) { + UpdateCacheRateEvent rateEvent = (UpdateCacheRateEvent) event; + if ((rateEvent.operatorType & abstractConcurrentCache.ITEM_RETRIEVE_OP) > 0) { + K key = (K) rateEvent.key; + WriteLock writerock = reentrantLock.writeLock(); + try { + writerock.lock(); + // 使用缓存的缓存替换策略,改变其顺序 + abstractConcurrentCache.itemRetrieved((K) rateEvent.key); + } finally { + writerock.unlock(); + } + + abstractConcurrentCache.decrementAndGetEpollCache(); + //更新他的一个 过期时间 + long idleTimeout = abstractConcurrentCache.getIdleTimeOut(key); + if (idleTimeout > 0) { + abstractConcurrentCache.updateExpireTime(key, System.currentTimeMillis() + idleTimeout); + } + } + + return true; + } + } + + public class ItemRemoveCacheRateUpdateListener implements IPipelineEventListener { + @SuppressWarnings({"unchecked", "rawtypes"}) + @Override + public boolean onEvent(ObjectEvent event, int listenerIndex) { + UpdateCacheRateEvent rateEvent = (UpdateCacheRateEvent) event; + if ((rateEvent.operatorType & abstractConcurrentCache.ITEM_REMOVE_OP) > 0) { + WriteLock writerock = reentrantLock.writeLock(); + try { + writerock.lock(); + abstractConcurrentCache.itemRemoved((K) rateEvent.key); + } finally { + writerock.unlock(); + } + + abstractConcurrentCache.decrementAndGetEpollCache(); + } + + return true; + } + } + + public class ItemExpireCheckListener implements IPipelineEventListener { + + @Override + public boolean onEvent(ObjectEvent event, int listenerIndex) { + if (abstractConcurrentCache == null) { + + return false; + } + + ConcurrentMap expireKeysMap = abstractConcurrentCache.getKeyExpireTimeMap(); + if (expireKeysMap == null || expireKeysMap.isEmpty()) { + + return false; + } + LinkedList expireKeyList = new LinkedList<>(); + for (Map.Entry entry : expireKeysMap.entrySet()) { + Long value = entry.getValue(); + if (System.currentTimeMillis() > value) { + //have expire,then remote the key + final K key = entry.getKey(); + final V cacheValue = abstractConcurrentCache.remove(key); + expireKeyList.add(key); + abstractConcurrentCache.getiParallelQueueExecutor().execute(new Runnable() { + @Override + public void run() { + abstractConcurrentCache.getiExpireKeyAdaptor().expire(key, cacheValue, abstractConcurrentCache); + + } + }); + } + } + return false; + } + } + + @Override + public String partitioner(ObjectEvent objectEvent) { + if (abstractConcurrentCache == null) { + + return objectEvent.getEventTopic(); + } + String cacheTopic = abstractConcurrentCache.getCacheTopic(); + return cacheTopic; + } + + /** + * 是否需要检测 remove 的条件触发 + * 1、current cache entry > max entries + * 2、还没有其他操作cache 的线程 + * 3、没有触发 clear cache 的操作 + * + * @return + */ + public boolean removeCheck() { + int currentSize = abstractConcurrentCache.size(); + int maxEntries = abstractConcurrentCache.getMaxEntries(); + + return currentSize > maxEntries && (abstractConcurrentCache.getEpollCache() <= 0 || (currentSize - maxEntries > 50)) && !abstractConcurrentCache.getIsClearCache(); + } + + @SuppressWarnings({"rawtypes", "unchecked"}) + public void updateCacheRate(UpdateCacheRateEvent rateEvent) { + + defaultPipelineEventObject.notifyListeners(rateEvent); + } + + public void handlerExpireCacheEntry(final AbstractConcurrentCache abstractConcurrentCache) { + ReadLock readLock = reentrantLock.readLock(); + try { + readLock.lock(); + final K key = abstractConcurrentCache.removeItem(true); + if (key == null) { + return; + } + + final V value = abstractConcurrentCache.remove(key, false); + //异步触发 + abstractConcurrentCache.getiParallelQueueExecutor().execute(new Runnable() { + @Override + public void run() { + abstractConcurrentCache.getiExpireKeyAdaptor().expire(key, value, abstractConcurrentCache); + } + }); + } finally { + readLock.unlock(); + } + } } diff --git a/event-swift/src/main/java/com/ware/swift/event/object/AbstractAsyncEventObject.java b/event-swift/src/main/java/com/ware/swift/event/object/AbstractAsyncEventObject.java index b144736..13923b0 100644 --- a/event-swift/src/main/java/com/ware/swift/event/object/AbstractAsyncEventObject.java +++ b/event-swift/src/main/java/com/ware/swift/event/object/AbstractAsyncEventObject.java @@ -81,7 +81,7 @@ public void shutdown() { @Override public void enEmergencyQueue(Runnable runnable) { - executor.enEmergenceyQueue(runnable); + executor.enEmergencyQueue(runnable); } @Override diff --git a/event-swift/src/main/java/com/ware/swift/event/object/DefaultAsyncEventObject.java b/event-swift/src/main/java/com/ware/swift/event/object/DefaultAsyncEventObject.java index 07ac820..5b62616 100644 --- a/event-swift/src/main/java/com/ware/swift/event/object/DefaultAsyncEventObject.java +++ b/event-swift/src/main/java/com/ware/swift/event/object/DefaultAsyncEventObject.java @@ -6,43 +6,39 @@ import com.ware.swift.event.parallel.action.ParallelActionExecutor; public class DefaultAsyncEventObject extends AbstractAsyncEventObject implements IEventPartitionerRegister { - - /** - * 这里放到子类里面来,目的就是为了区分父类partitioner 的方式。 - * 如果业务方自己继承AbstractAsyncEventObject,那么特定情况下自己重写 分类器 方法即可。 - * 如果业务方使用的是组合的方式,因为不能重写分类器,则需要借助接口适配的方式来实现 分类器 的具体实现。 - */ - protected IEventPartitioner iEventPartitioner; - public DefaultAsyncEventObject(ParallelActionExecutor executor, boolean isOptimism) { - super(executor, isOptimism); - } - - public DefaultAsyncEventObject(String executorName, boolean isOptimism) { - super(executorName, isOptimism); - } - - @Override - public void attachListener() { - //nothing to do - } - - public void subscriber(IEventObjectListener eventObjectListener,int eventType){ - - this.addListener(eventObjectListener, eventType); - } - - @Override - public String partitioner(ObjectEvent event) { - if(this.iEventPartitioner != null){ - - return iEventPartitioner.partitioner(event); - } - - return super.partitioner(event); - } - - @Override - public void registerEventPartitioner(IEventPartitioner eventPartitioner) { - this.iEventPartitioner = eventPartitioner; - } + + /** + * 这里放到子类里面来,目的就是为了区分父类partitioner 的方式。 + * 如果业务方自己继承AbstractAsyncEventObject,那么特定情况下自己重写 分类器 方法即可。 + * 如果业务方使用的是组合的方式,因为不能重写分类器,则需要借助接口适配的方式来实现 分类器 的具体实现。 + */ + protected IEventPartitioner iEventPartitioner; + + public DefaultAsyncEventObject(ParallelActionExecutor executor, boolean isOptimism) { + super(executor, isOptimism); + } + + public DefaultAsyncEventObject(String executorName, boolean isOptimism) { + super(executorName, isOptimism); + } + + @Override + public void attachListener() { + //nothing to do + } + + @Override + public String partitioner(ObjectEvent event) { + if (this.iEventPartitioner != null) { + + return iEventPartitioner.partitioner(event); + } + + return super.partitioner(event); + } + + @Override + public void registerEventPartitioner(IEventPartitioner eventPartitioner) { + this.iEventPartitioner = eventPartitioner; + } } diff --git a/event-swift/src/main/java/com/ware/swift/event/object/FutureObjectEvent.java b/event-swift/src/main/java/com/ware/swift/event/object/FutureObjectEvent.java index ffc9c61..c5205d9 100644 --- a/event-swift/src/main/java/com/ware/swift/event/object/FutureObjectEvent.java +++ b/event-swift/src/main/java/com/ware/swift/event/object/FutureObjectEvent.java @@ -1,49 +1,47 @@ package com.ware.swift.event.object; import com.ware.swift.event.ObjectEvent; -import com.ware.swift.event.graph.future.FutureResult; +import com.ware.swift.event.common.FutureResult; import java.util.concurrent.TimeUnit; /** - * - * @author pengbingting - * * @param + * @author pengbingting */ public class FutureObjectEvent extends ObjectEvent { - /** - * - */ - private static final long serialVersionUID = 1L; - private FutureResult futureResult; + /** + * + */ + private static final long serialVersionUID = 1L; + private FutureResult futureResult; - public FutureObjectEvent(Object source, V value, int eventType) { - super(source, value, eventType); - this.futureResult = new FutureResult<>(); - } + public FutureObjectEvent(Object source, V value, int eventType) { + super(source, value, eventType); + this.futureResult = new FutureResult<>(); + } - public void setResult(Object result) { - this.futureResult.setResult(result); - } + public void setResult(Object result) { + this.futureResult.setResult(result); + } - public Object getResult() { + public Object getResult() { - return this.futureResult.getResult(); - } + return this.futureResult.getResult(); + } - public Object getResult(int timeOut) { + public Object getResult(int timeOut) { - return this.futureResult.getResult(timeOut); - } + return this.futureResult.getResult(timeOut); + } - public Object getResult(int timeOut, TimeUnit timeUnit) { + public Object getResult(int timeOut, TimeUnit timeUnit) { - return this.futureResult.getResult(timeUnit.toMillis(timeOut)); - } + return this.futureResult.getResult(timeUnit.toMillis(timeOut)); + } - public static void main(String[] args) { - System.err.println(TimeUnit.SECONDS.toMillis(1)); - } + public static void main(String[] args) { + System.err.println(TimeUnit.SECONDS.toMillis(1)); + } } diff --git a/event-swift/src/main/java/com/ware/swift/event/object/IEventObjectListener.java b/event-swift/src/main/java/com/ware/swift/event/object/IEventObjectListener.java index e88501f..955e4dc 100644 --- a/event-swift/src/main/java/com/ware/swift/event/object/IEventObjectListener.java +++ b/event-swift/src/main/java/com/ware/swift/event/object/IEventObjectListener.java @@ -6,12 +6,11 @@ /** * 定义事件处理接口。由用户真正的实现 - * @author pengbingting * * @param + * @author pengbingting */ public interface IEventObjectListener extends EventListener { - - public void onEvent(ObjectEvent event) throws Throwable; + void onEvent(ObjectEvent event) throws Throwable; } diff --git a/event-swift/src/main/java/com/ware/swift/event/object/IFallBackEventObjectListener.java b/event-swift/src/main/java/com/ware/swift/event/object/IFallBackEventObjectListener.java index 2b265bb..5fa58e4 100644 --- a/event-swift/src/main/java/com/ware/swift/event/object/IFallBackEventObjectListener.java +++ b/event-swift/src/main/java/com/ware/swift/event/object/IFallBackEventObjectListener.java @@ -4,10 +4,10 @@ /** * 提供降级的接口实现 - * @author pengbingting * + * @author pengbingting */ -public interface IFallBackEventObjectListener extends IEventObjectListener{ +public interface IFallBackEventObjectListener extends IEventObjectListener { - public void fallBack(ObjectEvent event); + void fallBack(ObjectEvent event); } diff --git a/event-swift/src/main/java/com/ware/swift/event/object/fast/AbstractFastAsyncEventObject.java b/event-swift/src/main/java/com/ware/swift/event/object/fast/AbstractFastAsyncEventObject.java index 17ffea6..0ecd9f7 100644 --- a/event-swift/src/main/java/com/ware/swift/event/object/fast/AbstractFastAsyncEventObject.java +++ b/event-swift/src/main/java/com/ware/swift/event/object/fast/AbstractFastAsyncEventObject.java @@ -2,88 +2,88 @@ import com.ware.swift.event.IAsyncEventObject; import com.ware.swift.event.ObjectEvent; +import com.ware.swift.event.object.AbstractEventObject; import com.ware.swift.event.object.IEventCallBack; import com.ware.swift.event.object.IEventObjectListener; -import com.ware.swift.event.object.AbstractEventObject; import com.ware.swift.event.parallel.IParallelQueueExecutor; import java.util.Deque; public abstract class AbstractFastAsyncEventObject extends AbstractEventObject - implements IAsyncEventObject { + implements IAsyncEventObject { - protected DefaultAsyncEventObjectImpl defaultAsyncEventObject; + protected DefaultAsyncEventObjectImpl defaultAsyncEventObject; - public AbstractFastAsyncEventObject( - IParallelQueueExecutor superFastParallelQueueExecutor) { - this(superFastParallelQueueExecutor, true); - } + public AbstractFastAsyncEventObject( + IParallelQueueExecutor superFastParallelQueueExecutor) { + this(superFastParallelQueueExecutor, true); + } - public AbstractFastAsyncEventObject( - IParallelQueueExecutor superFastParallelQueueExecutor, boolean isOptimism) { - super(isOptimism); - this.defaultAsyncEventObject = new DefaultAsyncEventObjectImpl( - superFastParallelQueueExecutor, this); - } + public AbstractFastAsyncEventObject( + IParallelQueueExecutor superFastParallelQueueExecutor, boolean isOptimism) { + super(isOptimism); + this.defaultAsyncEventObject = new DefaultAsyncEventObjectImpl<>( + superFastParallelQueueExecutor, this); + } - @Override - public void shutdown() { + @Override + public void shutdown() { - defaultAsyncEventObject.shutdown(); - } + defaultAsyncEventObject.shutdown(); + } - @Override - public void adjustExecutor(int coreSize, int maxSize) { + @Override + public void adjustExecutor(int coreSize, int maxSize) { - defaultAsyncEventObject.adjustExecutor(coreSize, maxSize); - } + defaultAsyncEventObject.adjustExecutor(coreSize, maxSize); + } - @Override - public void enEmergencyQueue(Runnable runnable) { + @Override + public void enEmergencyQueue(Runnable runnable) { - defaultAsyncEventObject.enEmergencyQueue(runnable); - } + defaultAsyncEventObject.enEmergencyQueue(runnable); + } - /** - * 改造为异步 - */ - @Override - public void listenerHandler(final Deque> eventObjectListeners, - final ObjectEvent event) { - getParallelQueueExecutor().execute(partitioner(event), new Runnable() { - @Override - public void run() { + /** + * 改造为异步 + */ + @Override + public void listenerHandler(final Deque> eventObjectListeners, + final ObjectEvent event) { + getParallelQueueExecutor().execute(partitioner(event), new Runnable() { + @Override + public void run() { - doListenerHandler(eventObjectListeners, event); - } - }); - } + doListenerHandler(eventObjectListeners, event); + } + }); + } - @Override - public String partitioner(ObjectEvent event) { + @Override + public String partitioner(ObjectEvent event) { - return defaultAsyncEventObject.partitioner(event); - } + return defaultAsyncEventObject.partitioner(event); + } - @Override - public IParallelQueueExecutor getParallelQueueExecutor() { + @Override + public IParallelQueueExecutor getParallelQueueExecutor() { - return defaultAsyncEventObject.getParallelQueueExecutor(); - } + return defaultAsyncEventObject.getParallelQueueExecutor(); + } - @Override - public void publish(V value, Integer eventType, IEventCallBack iEventCallBack) { + @Override + public void publish(V value, Integer eventType, IEventCallBack iEventCallBack) { - defaultAsyncEventObject.publish(value, eventType, iEventCallBack); - } + defaultAsyncEventObject.publish(value, eventType, iEventCallBack); + } - @Override - public void notifyListeners(ObjectEvent objectEvent, - IEventCallBack iEventCallBack) { + @Override + public void notifyListeners(ObjectEvent objectEvent, + IEventCallBack iEventCallBack) { - defaultAsyncEventObject.notifyListeners(objectEvent, iEventCallBack); - } + defaultAsyncEventObject.notifyListeners(objectEvent, iEventCallBack); + } - @Override - public abstract void attachListener(); + @Override + public abstract void attachListener(); } diff --git a/event-swift/src/main/java/com/ware/swift/event/object/fast/AbstractFastAsyncPipelineEventObject.java b/event-swift/src/main/java/com/ware/swift/event/object/fast/AbstractFastAsyncPipelineEventObject.java index 488fdf1..5a4551d 100644 --- a/event-swift/src/main/java/com/ware/swift/event/object/fast/AbstractFastAsyncPipelineEventObject.java +++ b/event-swift/src/main/java/com/ware/swift/event/object/fast/AbstractFastAsyncPipelineEventObject.java @@ -10,77 +10,77 @@ import java.util.Deque; public abstract class AbstractFastAsyncPipelineEventObject - extends AbstractPipelineEventObject implements IAsyncEventObject { + extends AbstractPipelineEventObject implements IAsyncEventObject { - protected DefaultAsyncEventObjectImpl defaultAsyncEventObject = null; + protected DefaultAsyncEventObjectImpl defaultAsyncEventObject; - public AbstractFastAsyncPipelineEventObject( - IParallelQueueExecutor superFastParallelQueueExecutor) { - this(superFastParallelQueueExecutor, true); - } + public AbstractFastAsyncPipelineEventObject( + IParallelQueueExecutor superFastParallelQueueExecutor) { + this(superFastParallelQueueExecutor, true); + } - public AbstractFastAsyncPipelineEventObject( - IParallelQueueExecutor superFastParallelQueueExecutor, boolean isOptimism) { - super(isOptimism); - this.defaultAsyncEventObject = new DefaultAsyncEventObjectImpl<>( - superFastParallelQueueExecutor, this); - } + public AbstractFastAsyncPipelineEventObject( + IParallelQueueExecutor superFastParallelQueueExecutor, boolean isOptimism) { + super(isOptimism); + this.defaultAsyncEventObject = new DefaultAsyncEventObjectImpl<>( + superFastParallelQueueExecutor, this); + } - @Override - public void shutdown() { + @Override + public void shutdown() { - defaultAsyncEventObject.shutdown(); - } + defaultAsyncEventObject.shutdown(); + } - @Override - public void adjustExecutor(int coreSize, int maxSize) { + @Override + public void adjustExecutor(int coreSize, int maxSize) { - defaultAsyncEventObject.adjustExecutor(coreSize, maxSize); - } + defaultAsyncEventObject.adjustExecutor(coreSize, maxSize); + } - @Override - public void enEmergencyQueue(Runnable runnable) { + @Override + public void enEmergencyQueue(Runnable runnable) { - defaultAsyncEventObject.enEmergencyQueue(runnable); - } + defaultAsyncEventObject.enEmergencyQueue(runnable); + } - @Override - public void listenerHandler(final Deque> objectListeners, - final ObjectEvent event) { - getParallelQueueExecutor().execute(partitioner(event), new Runnable() { - @Override - public void run() { - doListenerHandler(objectListeners, event); - } - }); - } + @Override + public void listenerHandler(final Deque> objectListeners, + final ObjectEvent event) { + getParallelQueueExecutor().execute(partitioner(event), new Runnable() { + @Override + public void run() { + doListenerHandler(objectListeners, event); + } + }); + } - @Override - public String partitioner(ObjectEvent event) { + @Override + public String partitioner(ObjectEvent event) { - return defaultAsyncEventObject.partitioner(event); - } + return defaultAsyncEventObject.partitioner(event); + } - @Override - public IParallelQueueExecutor getParallelQueueExecutor() { + @Override + public IParallelQueueExecutor getParallelQueueExecutor() { - return defaultAsyncEventObject.getParallelQueueExecutor(); - } + return defaultAsyncEventObject.getParallelQueueExecutor(); + } - @Override - public void publish(V value, Integer eventType, IEventCallBack iEventCallBack) { + @Override + public void publish(V value, Integer eventType, IEventCallBack iEventCallBack) { - defaultAsyncEventObject.publish(value, eventType, iEventCallBack); - } + defaultAsyncEventObject.publish(value, eventType, iEventCallBack); + } - @Override - public void notifyListeners(ObjectEvent objectEvent, - IEventCallBack iEventCallBack) { + @Override + public void notifyListeners(ObjectEvent objectEvent, + IEventCallBack iEventCallBack) { - defaultAsyncEventObject.notifyListeners(objectEvent, iEventCallBack); - } + defaultAsyncEventObject.notifyListeners(objectEvent, iEventCallBack); + } - @Override - public abstract void attachListener(); + @Override + public abstract void attachListener(); } diff --git a/event-swift/src/main/java/com/ware/swift/event/object/fast/DefaultAsyncEventObjectImpl.java b/event-swift/src/main/java/com/ware/swift/event/object/fast/DefaultAsyncEventObjectImpl.java index 835af47..1beafe9 100644 --- a/event-swift/src/main/java/com/ware/swift/event/object/fast/DefaultAsyncEventObjectImpl.java +++ b/event-swift/src/main/java/com/ware/swift/event/object/fast/DefaultAsyncEventObjectImpl.java @@ -9,78 +9,78 @@ import com.ware.swift.event.parallel.IParallelQueueExecutor; class DefaultAsyncEventObjectImpl - implements IAsyncEventObject, IEventPartitionerRegister { + implements IAsyncEventObject, IEventPartitionerRegister { - private IParallelQueueExecutor parallelQueueExecutor; - private IEventPartitioner ieventPartitioner; - private IEventObject eventObject; + private IParallelQueueExecutor parallelQueueExecutor; + private IEventPartitioner ieventPartitioner; + private IEventObject eventObject; - public DefaultAsyncEventObjectImpl(IParallelQueueExecutor parallelQueueExecutor, - IEventObject eventObject) { - super(); - this.parallelQueueExecutor = parallelQueueExecutor; - this.eventObject = eventObject; - } + public DefaultAsyncEventObjectImpl(IParallelQueueExecutor parallelQueueExecutor, + IEventObject eventObject) { + super(); + this.parallelQueueExecutor = parallelQueueExecutor; + this.eventObject = eventObject; + } - @Override - public void shutdown() { + @Override + public void shutdown() { - parallelQueueExecutor.stop(); - } + parallelQueueExecutor.stop(); + } - @Override - public void adjustExecutor(int coreSize, int maxSize) { + @Override + public void adjustExecutor(int coreSize, int maxSize) { - throw new UnsupportedOperationException("does not adjuest the executor."); - } + throw new UnsupportedOperationException("does not adjust the executor."); + } - @Override - public void enEmergencyQueue(Runnable runnable) { + @Override + public void enEmergencyQueue(Runnable runnable) { - parallelQueueExecutor.enEmergenceyQueue(runnable); - } + parallelQueueExecutor.enEmergencyQueue(runnable); + } - @Override - public IParallelQueueExecutor getParallelQueueExecutor() { - final IParallelQueueExecutor tmpParallelQueueExecutor = parallelQueueExecutor; - return tmpParallelQueueExecutor; - } + @Override + public IParallelQueueExecutor getParallelQueueExecutor() { + final IParallelQueueExecutor tmpParallelQueueExecutor = parallelQueueExecutor; + return tmpParallelQueueExecutor; + } - @Override - public void publish(E value, Integer eventType, IEventCallBack iEventCallBack) { + @Override + public void publish(E value, Integer eventType, IEventCallBack iEventCallBack) { - notifyListeners(new ObjectEvent<>(this, value, eventType), iEventCallBack); - } + notifyListeners(new ObjectEvent<>(this, value, eventType), iEventCallBack); + } - @Override - public void notifyListeners(ObjectEvent objectEvent, - IEventCallBack iEventCallBack) { - objectEvent.setParameter(ObjectEvent.EVENT_CALLBACK, iEventCallBack); - eventObject.notifyListeners(objectEvent); - } + @Override + public void notifyListeners(ObjectEvent objectEvent, + IEventCallBack iEventCallBack) { + objectEvent.setParameter(ObjectEvent.EVENT_CALLBACK, iEventCallBack); + eventObject.notifyListeners(objectEvent); + } - @Override - public String partitioner(ObjectEvent event) { - if (ieventPartitioner != null) { + @Override + public String partitioner(ObjectEvent event) { + if (ieventPartitioner != null) { - return ieventPartitioner.partitioner(event); - } + return ieventPartitioner.partitioner(event); + } - return event.getEventTopic(); - } + return event.getEventTopic(); + } - public IEventObject getEventObject() { - return eventObject; - } + public IEventObject getEventObject() { + return eventObject; + } - public void setEventObject(IEventObject eventObject) { - this.eventObject = eventObject; - } + public void setEventObject(IEventObject eventObject) { + this.eventObject = eventObject; + } - @Override - public void registerEventPartitioner(IEventPartitioner eventPartitioner) { + @Override + public void registerEventPartitioner(IEventPartitioner eventPartitioner) { - this.ieventPartitioner = eventPartitioner; - } + this.ieventPartitioner = eventPartitioner; + } } diff --git a/event-swift/src/main/java/com/ware/swift/event/object/fast/DefaultFastAsyncPipelineEventObject.java b/event-swift/src/main/java/com/ware/swift/event/object/fast/DefaultFastAsyncPipelineEventObject.java index 013e2a7..ff532e7 100644 --- a/event-swift/src/main/java/com/ware/swift/event/object/fast/DefaultFastAsyncPipelineEventObject.java +++ b/event-swift/src/main/java/com/ware/swift/event/object/fast/DefaultFastAsyncPipelineEventObject.java @@ -2,40 +2,32 @@ import com.ware.swift.event.IEventPartitioner; import com.ware.swift.event.IEventPartitionerRegister; -import com.ware.swift.event.object.pipeline.IPipelineEventListener; import com.ware.swift.event.parallel.IParallelQueueExecutor; /** - * * @param */ public class DefaultFastAsyncPipelineEventObject extends - AbstractFastAsyncPipelineEventObject implements IEventPartitionerRegister { + AbstractFastAsyncPipelineEventObject implements IEventPartitionerRegister { - public DefaultFastAsyncPipelineEventObject( - IParallelQueueExecutor superFastParallelQueueExecutor, boolean isOptimism) { - super(superFastParallelQueueExecutor, isOptimism); - } + public DefaultFastAsyncPipelineEventObject( + IParallelQueueExecutor superFastParallelQueueExecutor, boolean isOptimism) { + super(superFastParallelQueueExecutor, isOptimism); + } - public DefaultFastAsyncPipelineEventObject( - IParallelQueueExecutor superFastParallelQueueExecutor) { - super(superFastParallelQueueExecutor); - } + public DefaultFastAsyncPipelineEventObject( + IParallelQueueExecutor superFastParallelQueueExecutor) { + super(superFastParallelQueueExecutor); + } - @Override - public void attachListener() { - // nothing to do - } + @Override + public void attachListener() { + // nothing to do + } - public void subscriber(IPipelineEventListener pipelineObjectListener, - int eventType) { + @Override + public void registerEventPartitioner(IEventPartitioner eventPartitioner) { - this.addLast(pipelineObjectListener, eventType); - } - - @Override - public void registerEventPartitioner(IEventPartitioner eventPartitioner) { - - defaultAsyncEventObject.registerEventPartitioner(eventPartitioner); - } + defaultAsyncEventObject.registerEventPartitioner(eventPartitioner); + } } diff --git a/event-swift/src/main/java/com/ware/swift/event/object/multi/DefaultMultiEventObject.java b/event-swift/src/main/java/com/ware/swift/event/object/multi/DefaultMultiEventObject.java index 0d147cd..2a8337d 100644 --- a/event-swift/src/main/java/com/ware/swift/event/object/multi/DefaultMultiEventObject.java +++ b/event-swift/src/main/java/com/ware/swift/event/object/multi/DefaultMultiEventObject.java @@ -2,20 +2,23 @@ import com.ware.swift.event.parallel.IParallelQueueExecutor; +/** + * @param + */ public class DefaultMultiEventObject extends AbstractMultiEventObject { - public DefaultMultiEventObject(IParallelQueueExecutor superFastParallelQueueExecutor, - boolean isOptimism) { - super(superFastParallelQueueExecutor, isOptimism); - } + public DefaultMultiEventObject(IParallelQueueExecutor superFastParallelQueueExecutor, + boolean isOptimism) { + super(superFastParallelQueueExecutor, isOptimism); + } - public DefaultMultiEventObject( - IParallelQueueExecutor superFastParallelQueueExecutor) { - super(superFastParallelQueueExecutor); - } + public DefaultMultiEventObject( + IParallelQueueExecutor superFastParallelQueueExecutor) { + super(superFastParallelQueueExecutor); + } - @Override - public void attachListener() { - // nothing to do - } + @Override + public void attachListener() { + // nothing to do + } } diff --git a/event-swift/src/main/java/com/ware/swift/event/object/multi/IDAGSequence.java b/event-swift/src/main/java/com/ware/swift/event/object/multi/IDAGSequence.java deleted file mode 100644 index 6c2c9f1..0000000 --- a/event-swift/src/main/java/com/ware/swift/event/object/multi/IDAGSequence.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.ware.swift.event.object.multi; - -public interface IDAGSequence { - - Integer getSequence(); -} diff --git a/event-swift/src/main/java/com/ware/swift/event/object/pipeline/AbstractAsyncPipelineEventObject.java b/event-swift/src/main/java/com/ware/swift/event/object/pipeline/AbstractAsyncPipelineEventObject.java index 0400db2..c2782e3 100644 --- a/event-swift/src/main/java/com/ware/swift/event/object/pipeline/AbstractAsyncPipelineEventObject.java +++ b/event-swift/src/main/java/com/ware/swift/event/object/pipeline/AbstractAsyncPipelineEventObject.java @@ -12,87 +12,91 @@ import java.util.Deque; +/** + * @param + */ public abstract class AbstractAsyncPipelineEventObject - extends AbstractPipelineEventObject implements IAsyncEventObject { - protected IParallelActionExecutor executor; - - public AbstractAsyncPipelineEventObject(IParallelActionExecutor executor) { - super(); - this.executor = executor; - } - - public AbstractAsyncPipelineEventObject(boolean isOptimism, - IParallelActionExecutor executor) { - super(isOptimism); - this.executor = executor; - } - - public AbstractAsyncPipelineEventObject(boolean isOptimism, String executorName) { - super(isOptimism); - int coreSize = Runtime.getRuntime().availableProcessors() * 4; - this.executor = new FastParallelActionExecutor(coreSize, executorName); - } - - /** - * 提供异步模式的事件 发布 - * @param value - * @param eventTopic - * @return - */ - public FutureObjectEvent publishWithFuture(V value, Integer eventTopic) { - FutureObjectEvent futureObjectEvent = new FutureObjectEvent<>(this, value, - eventTopic); - notifyListeners(futureObjectEvent); - return futureObjectEvent; - } - - @Override - public void listenerHandler(final Deque> objectListeners, - final ObjectEvent event) { - executor.enParallelAction(partitioner(event), new Action() { - @Override - public void execute() throws ActionExecuteException { - doListenerHandler(objectListeners, event); - } - }); - } - - @Override - public String partitioner(ObjectEvent event) { - - return event.getEventTopic(); - } - - @Override - public void adjustExecutor(int coreSize, int maxSize) { - executor.adjustPoolSize(coreSize, maxSize); - } - - @Override - public void enEmergencyQueue(Runnable runnable) { - executor.enEmergenceyQueue(runnable); - } - - public void shutdown() { - this.executor.stop(); - } - - @Override - public IParallelQueueExecutor getParallelQueueExecutor() { - - return this.executor; - } - - @Override - public void publish(V value, Integer eventType, IEventCallBack iEventCallBack) { - - notifyListeners(new ObjectEvent<>(this, value, eventType), iEventCallBack); - } - - @Override - public void notifyListeners(ObjectEvent objectEvent, - IEventCallBack iEventCallBack) { - objectEvent.setParameter(ObjectEvent.EVENT_CALLBACK, iEventCallBack); - notifyListeners(objectEvent); - } + extends AbstractPipelineEventObject implements IAsyncEventObject { + protected IParallelActionExecutor executor; + + public AbstractAsyncPipelineEventObject(IParallelActionExecutor executor) { + super(); + this.executor = executor; + } + + public AbstractAsyncPipelineEventObject(boolean isOptimism, + IParallelActionExecutor executor) { + super(isOptimism); + this.executor = executor; + } + + public AbstractAsyncPipelineEventObject(boolean isOptimism, String executorName) { + super(isOptimism); + int coreSize = Runtime.getRuntime().availableProcessors() * 4; + this.executor = new FastParallelActionExecutor(coreSize, executorName); + } + + /** + * 提供异步模式的事件 发布 + * + * @param value + * @param eventTopic + * @return + */ + public FutureObjectEvent publishWithFuture(V value, Integer eventTopic) { + FutureObjectEvent futureObjectEvent = new FutureObjectEvent<>(this, value, + eventTopic); + notifyListeners(futureObjectEvent); + return futureObjectEvent; + } + + @Override + public void listenerHandler(final Deque> objectListeners, + final ObjectEvent event) { + executor.enParallelAction(partitioner(event), new Action() { + @Override + public void execute() throws ActionExecuteException { + doListenerHandler(objectListeners, event); + } + }); + } + + @Override + public String partitioner(ObjectEvent event) { + + return event.getEventTopic(); + } + + @Override + public void adjustExecutor(int coreSize, int maxSize) { + executor.adjustPoolSize(coreSize, maxSize); + } + + @Override + public void enEmergencyQueue(Runnable runnable) { + executor.enEmergencyQueue(runnable); + } + + public void shutdown() { + this.executor.stop(); + } + + @Override + public IParallelQueueExecutor getParallelQueueExecutor() { + + return this.executor; + } + + @Override + public void publish(V value, Integer eventType, IEventCallBack iEventCallBack) { + + notifyListeners(new ObjectEvent<>(this, value, eventType), iEventCallBack); + } + + @Override + public void notifyListeners(ObjectEvent objectEvent, + IEventCallBack iEventCallBack) { + objectEvent.setParameter(ObjectEvent.EVENT_CALLBACK, iEventCallBack); + notifyListeners(objectEvent); + } } diff --git a/event-swift/src/main/java/com/ware/swift/event/object/pipeline/AbstractPipelineEventObject.java b/event-swift/src/main/java/com/ware/swift/event/object/pipeline/AbstractPipelineEventObject.java index 1f710a0..b8615da 100644 --- a/event-swift/src/main/java/com/ware/swift/event/object/pipeline/AbstractPipelineEventObject.java +++ b/event-swift/src/main/java/com/ware/swift/event/object/pipeline/AbstractPipelineEventObject.java @@ -178,7 +178,7 @@ protected final void doListenerHandler( for (IPipelineEventListener listener : objectListeners) { try { boolean isSuccessor = listener.onEvent(event, index); - if (!isSuccessor || event.isInterruptor()) { + if (!isSuccessor || event.isInterrupt()) { break; } } diff --git a/event-swift/src/main/java/com/ware/swift/event/object/pipeline/DefaultAsyncPipelineEventObject.java b/event-swift/src/main/java/com/ware/swift/event/object/pipeline/DefaultAsyncPipelineEventObject.java index a85d6f6..13299a5 100644 --- a/event-swift/src/main/java/com/ware/swift/event/object/pipeline/DefaultAsyncPipelineEventObject.java +++ b/event-swift/src/main/java/com/ware/swift/event/object/pipeline/DefaultAsyncPipelineEventObject.java @@ -7,43 +7,39 @@ public class DefaultAsyncPipelineEventObject extends AbstractAsyncPipelineEventObject implements IEventPartitionerRegister { - protected IEventPartitioner iEventPartitioner; - public DefaultAsyncPipelineEventObject(boolean isOptimism, IParallelActionExecutor executor) { - super(isOptimism, executor); - } - - public DefaultAsyncPipelineEventObject(boolean isOptimism, String executorName) { - super(isOptimism, executorName); - } - - public DefaultAsyncPipelineEventObject(IParallelActionExecutor executor) { - super(executor); - } - - @Override - public void attachListener() { - //nothing to do - } - - public void subscriber(IPipelineEventListener pipelineObjectListener, int eventType){ - - this.addLast(pipelineObjectListener, eventType); - } - - @Override - public String partitioner(ObjectEvent event) { - if(this.iEventPartitioner != null){ - - return this.iEventPartitioner.partitioner(event); - } - - return super.partitioner(event); - } - - @Override - public void registerEventPartitioner(IEventPartitioner eventPartitioner) { - - this.iEventPartitioner = eventPartitioner; - } - + protected IEventPartitioner iEventPartitioner; + + public DefaultAsyncPipelineEventObject(boolean isOptimism, IParallelActionExecutor executor) { + super(isOptimism, executor); + } + + public DefaultAsyncPipelineEventObject(boolean isOptimism, String executorName) { + super(isOptimism, executorName); + } + + public DefaultAsyncPipelineEventObject(IParallelActionExecutor executor) { + super(executor); + } + + @Override + public void attachListener() { + //nothing to do + } + + @Override + public String partitioner(ObjectEvent event) { + if (this.iEventPartitioner != null) { + + return this.iEventPartitioner.partitioner(event); + } + + return super.partitioner(event); + } + + @Override + public void registerEventPartitioner(IEventPartitioner eventPartitioner) { + + this.iEventPartitioner = eventPartitioner; + } + } diff --git a/event-swift/src/main/java/com/ware/swift/event/object/pipeline/DefaultPipelineEventObject.java b/event-swift/src/main/java/com/ware/swift/event/object/pipeline/DefaultPipelineEventObject.java index 6dc11df..fd019d6 100644 --- a/event-swift/src/main/java/com/ware/swift/event/object/pipeline/DefaultPipelineEventObject.java +++ b/event-swift/src/main/java/com/ware/swift/event/object/pipeline/DefaultPipelineEventObject.java @@ -2,23 +2,17 @@ public class DefaultPipelineEventObject extends AbstractPipelineEventObject { - public DefaultPipelineEventObject() { - super(); - } + public DefaultPipelineEventObject() { + super(); + } - public DefaultPipelineEventObject(boolean isOptimism) { - super(isOptimism); - } + public DefaultPipelineEventObject(boolean isOptimism) { + super(isOptimism); + } - @Override - public void attachListener() { - // nothing to do - } + @Override + public void attachListener() { + // nothing to do + } - public AbstractPipelineEventObject subscriber( - IPipelineEventListener pipelineObjectListener, int eventType) { - - this.addLast(pipelineObjectListener, eventType); - return this; - } } diff --git a/event-swift/src/main/java/com/ware/swift/event/parallel/AbstractParallelQueueExecutor.java b/event-swift/src/main/java/com/ware/swift/event/parallel/AbstractParallelQueueExecutor.java index ab2ab40..9af2522 100644 --- a/event-swift/src/main/java/com/ware/swift/event/parallel/AbstractParallelQueueExecutor.java +++ b/event-swift/src/main/java/com/ware/swift/event/parallel/AbstractParallelQueueExecutor.java @@ -42,7 +42,7 @@ public void run() { * */ @Override - public void enEmergenceyQueue(Runnable command) { + public void enEmergencyQueue(Runnable command) { if (command instanceof Action) { throw new IllegalArgumentException( diff --git a/event-swift/src/main/java/com/ware/swift/event/parallel/IParallelQueueExecutor.java b/event-swift/src/main/java/com/ware/swift/event/parallel/IParallelQueueExecutor.java index 83e7c72..a3ab8ee 100644 --- a/event-swift/src/main/java/com/ware/swift/event/parallel/IParallelQueueExecutor.java +++ b/event-swift/src/main/java/com/ware/swift/event/parallel/IParallelQueueExecutor.java @@ -5,51 +5,45 @@ public interface IParallelQueueExecutor extends Executor { - /** - * - * @param command - */ - void enEmergenceyQueue(Runnable command); - - /** - * - * @param topic - * @param command - */ - void execute(String topic, Runnable command); - - /** - * 执行一次性的 runnable - */ - void executeOneTime(Runnable command); - - /** - * - */ - void stop(); - - /** - * - * @return - */ - ScheduledExecutorService getScheduledExecutorService(); - - /** - * - * @param topics - */ - void registerTopics(String... topics); - - /** - * - * @param topic - */ - void removeTopic(String topic); - - /** - * - * @param isAuto 是否系统触发 - */ - void cronTrriger(boolean isAuto); + /** + * @param command + */ + void enEmergencyQueue(Runnable command); + + /** + * @param topic + * @param command + */ + void execute(String topic, Runnable command); + + /** + * 执行一次性的 runnable + */ + void executeOneTime(Runnable command); + + /** + * + */ + void stop(); + + /** + * @return + */ + ScheduledExecutorService getScheduledExecutorService(); + + /** + * @param topics + */ + void registerTopics(String... topics); + + /** + * @param topic + */ + void removeTopic(String topic); + + /** + * @param isAuto 是否系统触发 + */ + void cronTrriger(boolean isAuto); } diff --git a/event-swift/src/main/java/com/ware/swift/event/parallel/SuperFastParallelQueueExecutor.java b/event-swift/src/main/java/com/ware/swift/event/parallel/SuperFastParallelQueueExecutor.java index 28c63d0..c6d7eb4 100644 --- a/event-swift/src/main/java/com/ware/swift/event/parallel/SuperFastParallelQueueExecutor.java +++ b/event-swift/src/main/java/com/ware/swift/event/parallel/SuperFastParallelQueueExecutor.java @@ -8,127 +8,121 @@ /** * 此设计模拟高速公路上车道设计。 n-1 条车道为正常处理车道,1条车道为应急车道。 并行队列 执行器 - * + *

* 叫 SuperFastParallelQueueExecutor。是因为响应式优先。没有两层的并行队列,只有一层的并行执行队列 - * */ public class SuperFastParallelQueueExecutor extends AbstractParallelQueueExecutor { - private ThreadPoolExecutor[] pools; - // 维护一个 可以水平缩放的并行队列。并行队列的大小 n = paralleleQueue.size() + 1 - private ReentrantLock lock = new ReentrantLock(true); - private ThreadPoolExecutorIndexAllocator threadPoolExecutorIndexChooser; - private ConcurrentMap queueExecutorMapping = new ConcurrentHashMap<>(); - private AtomicLongMap topicLastExecuteTime = AtomicLongMap.create(); - private AtomicBoolean isOperating = new AtomicBoolean(false); - private AtomicBoolean isCronTrriger = new AtomicBoolean(false); - - /** - * @param prefix 线程enen池前缀名称 - */ - public SuperFastParallelQueueExecutor(int threads, String prefix) { - if (threads <= 0) { - threads = DEFAULT_QUEUE_SIZE; - } - - pools = new ThreadPoolExecutor[threads]; - for (int i = 0; i < threads; i++) { - // 半个小时之后还没有任务过来,则销毁线程 - pools[i] = new ThreadPoolExecutor(0, 1, 30L, TimeUnit.MINUTES, - new LinkedBlockingQueue(), - new DefaultThreadFactory(prefix)); - } - - if (isPowerOfTwo(threads)) { - threadPoolExecutorIndexChooser = new PowerOfTwoExecutorIndexChooser( - pools.length); - } - else { - threadPoolExecutorIndexChooser = new GenericExecutorIndexChooser( - pools.length); - } - } - - @Override - public void execute(String queueTopic, Runnable command) { - Integer index = queueExecutorMapping.get(queueTopic); - if (index == null) { - try { - lock.tryLock(3, TimeUnit.SECONDS); - isOperating.set(true); - index = queueExecutorMapping.get(queueTopic); - if (index == null) { - index = threadPoolExecutorIndexChooser.allocator(); - queueExecutorMapping.put(queueTopic, index); - } - } - catch (Exception e) { - if (index == null) { - index = threadPoolExecutorIndexChooser.allocator(); - } - } - finally { - isOperating.set(false); - lock.unlock(); - } - } - ThreadPoolExecutor executor = pools[index]; - runing(executor, command); - topicLastExecuteTime.put(queueTopic, System.currentTimeMillis()); - } - - public void stop() { - super.stop(); - for (int i = 0; i < pools.length; i++) { - pools[i].shutdown(); - } - } - - private void runing(ThreadPoolExecutor executor, Runnable task) { - if (executor.getQueue().isEmpty()) { - executor.execute(task); - } - else { - executor.getQueue().offer(task);// (action); - } - } - - public void execute(Runnable command) { - execute(command.getClass().getName(), command); - } - - @Override - public void registerTopics(String... topics) { - if (topics == null || topics.length == 0) { - return; - } - - lock.lock(); - try { - for (String queueTopic : topics) { - Integer index = queueExecutorMapping.get(queueTopic); - if (index != null) { - continue; - } - - index = threadPoolExecutorIndexChooser.allocator(); - queueExecutorMapping.put(queueTopic, index); - } - } - finally { - lock.unlock(); - } - } - - @Override - public void removeTopic(String topic) { - - queueExecutorMapping.remove(topic); - } - - @Override - public void executeOneTime(Runnable command) { - ThreadPoolExecutor executor = pools[threadPoolExecutorIndexChooser.allocator()]; - runing(executor, command); - } + private ThreadPoolExecutor[] pools; + // 维护一个 可以水平缩放的并行队列。并行队列的大小 n = parallel Queue.size() + 1 + private ReentrantLock lock = new ReentrantLock(true); + private ThreadPoolExecutorIndexAllocator threadPoolExecutorIndexChooser; + private ConcurrentMap queueExecutorMapping = new ConcurrentHashMap<>(); + private AtomicLongMap topicLastExecuteTime = AtomicLongMap.create(); + private AtomicBoolean isOperating = new AtomicBoolean(false); + private AtomicBoolean isCronTrriger = new AtomicBoolean(false); + + /** + * @param prefix 线程enen池前缀名称 + */ + public SuperFastParallelQueueExecutor(int threads, String prefix) { + if (threads <= 0) { + threads = DEFAULT_QUEUE_SIZE; + } + + pools = new ThreadPoolExecutor[threads]; + for (int i = 0; i < threads; i++) { + // 半个小时之后还没有任务过来,则销毁线程 + pools[i] = new ThreadPoolExecutor(0, 1, 30L, TimeUnit.MINUTES, + new LinkedBlockingQueue<>(), + new DefaultThreadFactory(prefix)); + } + + if (isPowerOfTwo(threads)) { + threadPoolExecutorIndexChooser = new PowerOfTwoExecutorIndexChooser( + pools.length); + } else { + threadPoolExecutorIndexChooser = new GenericExecutorIndexChooser( + pools.length); + } + } + + @Override + public void execute(String queueTopic, Runnable command) { + Integer index = queueExecutorMapping.get(queueTopic); + if (index == null) { + try { + lock.tryLock(3, TimeUnit.SECONDS); + isOperating.set(true); + index = queueExecutorMapping.get(queueTopic); + if (index == null) { + index = threadPoolExecutorIndexChooser.allocator(); + queueExecutorMapping.put(queueTopic, index); + } + } catch (Exception e) { + if (index == null) { + index = threadPoolExecutorIndexChooser.allocator(); + } + } finally { + isOperating.set(false); + lock.unlock(); + } + } + ThreadPoolExecutor executor = pools[index]; + runing(executor, command); + topicLastExecuteTime.put(queueTopic, System.currentTimeMillis()); + } + + public void stop() { + super.stop(); + for (int i = 0; i < pools.length; i++) { + pools[i].shutdown(); + } + } + + private void runing(ThreadPoolExecutor executor, Runnable task) { + if (executor.getQueue().isEmpty()) { + executor.execute(task); + } else { + executor.getQueue().offer(task);// (action); + } + } + + public void execute(Runnable command) { + execute(command.getClass().getName(), command); + } + + @Override + public void registerTopics(String... topics) { + if (topics == null || topics.length == 0) { + return; + } + + lock.lock(); + try { + for (String queueTopic : topics) { + Integer index = queueExecutorMapping.get(queueTopic); + if (index != null) { + continue; + } + + index = threadPoolExecutorIndexChooser.allocator(); + queueExecutorMapping.put(queueTopic, index); + } + } finally { + lock.unlock(); + } + } + + @Override + public void removeTopic(String topic) { + + queueExecutorMapping.remove(topic); + } + + @Override + public void executeOneTime(Runnable command) { + ThreadPoolExecutor executor = pools[threadPoolExecutorIndexChooser.allocator()]; + runing(executor, command); + } } diff --git a/event-swift/src/main/java/com/ware/swift/event/parallel/ThreadPoolContainer.java b/event-swift/src/main/java/com/ware/swift/event/parallel/ThreadPoolContainer.java deleted file mode 100644 index 23a5413..0000000 --- a/event-swift/src/main/java/com/ware/swift/event/parallel/ThreadPoolContainer.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.ware.swift.event.parallel; - -import java.io.Serializable; - - -public class ThreadPoolContainer implements Serializable{ - - /** - * - */ - private static final long serialVersionUID = 1L; - private int threadSize ; //线程池中活动线程池大小 - private int largestPoolSize; //有史以来的峰值大小 - private int activeTaskCount;// 正在执行的任务数 - private int waitTaskCount ; //在队列中等待执行的任务数 - private int completeTaskCount ;//已经调度完成的 task count - private int taskCount ;//总的被调度的task count = complete + working + in queue - public int getThreadSize() { - return threadSize; - } - public void setThreadSize(int threadSize) { - this.threadSize = threadSize; - } - public int getLargestPoolSize() { - return largestPoolSize; - } - public void setLargestPoolSize(int largestPoolSize) { - this.largestPoolSize = largestPoolSize; - } - public int getActiveTaskCount() { - return activeTaskCount; - } - public void setActiveTaskCount(int activeTaskCount) { - this.activeTaskCount = activeTaskCount; - } - public int getWaitTaskCount() { - return waitTaskCount; - } - public void setWaitTaskCount(int waitTaskCount) { - this.waitTaskCount = waitTaskCount; - } - public int getCompleteTaskCount() { - return completeTaskCount; - } - public void setCompleteTaskCount(int completeTaskCount) { - this.completeTaskCount = completeTaskCount; - } - public int getTaskCount() { - return taskCount; - } - public void setTaskCount(int taskCount) { - this.taskCount = taskCount; - } - @Override - public String toString() { - return "ThreadPoolContainer [threadSize=" + threadSize - + ", largestPoolSize=" + largestPoolSize + ", activeTaskCount=" - + activeTaskCount + ", waitTaskCount=" + waitTaskCount - + ", completeTaskCount=" + completeTaskCount + ", taskCount=" - + taskCount + "]"; - } - -} diff --git a/event-swift/src/main/java/com/ware/swift/event/parallel/ThreadPoolExecutorHook.java b/event-swift/src/main/java/com/ware/swift/event/parallel/ThreadPoolExecutorHook.java deleted file mode 100644 index 1b6f472..0000000 --- a/event-swift/src/main/java/com/ware/swift/event/parallel/ThreadPoolExecutorHook.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.ware.swift.event.parallel; - -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.RejectedExecutionHandler; -import java.util.concurrent.ThreadFactory; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; - -public class ThreadPoolExecutorHook extends ThreadPoolExecutor{ - - public ThreadPoolExecutorHook(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, - BlockingQueue workQueue, RejectedExecutionHandler handler) { - super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler); - } - - public ThreadPoolExecutorHook(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, - BlockingQueue workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) { - super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler); - } - - public ThreadPoolExecutorHook(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, - BlockingQueue workQueue, ThreadFactory threadFactory) { - super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory); - } - - public ThreadPoolExecutorHook(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, - BlockingQueue workQueue) { - super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue); - } - - /** - * 在这里可以跟踪每一个线程执行task 的时间,超过了指定的时间,则interuptor - */ - @Override - protected void beforeExecute(Thread t, Runnable r) { - super.beforeExecute(t, r); - } - - /** - * 在这里可以根据指定的运行超时触发报警。全面监控整个业务的异步task 的执行时间。 - * @param r 运行结束的 runnable - * @param t 如果运行过程中产生异常,则这里的 t 不为 null - */ - @Override - protected void afterExecute(Runnable r, Throwable t) { - super.afterExecute(r, t); - } - - @Override - protected void terminated() { - super.terminated(); - } -} diff --git a/event-swift/src/main/java/com/ware/swift/event/parallel/action/AbstractActionQueue.java b/event-swift/src/main/java/com/ware/swift/event/parallel/action/AbstractActionQueue.java index 8b2773f..634198e 100644 --- a/event-swift/src/main/java/com/ware/swift/event/parallel/action/AbstractActionQueue.java +++ b/event-swift/src/main/java/com/ware/swift/event/parallel/action/AbstractActionQueue.java @@ -1,111 +1,107 @@ package com.ware.swift.event.parallel.action; -import com.ware.swift.event.common.Log; import com.lmax.disruptor.Sequence; +import com.ware.swift.event.common.Log; import java.util.Queue; import java.util.concurrent.locks.ReentrantLock; /** * 基本队列的实现 - * @author pengbingting * + * @author pengbingting */ public abstract class AbstractActionQueue implements IActionQueue { - protected Queue queue; - protected ReentrantLock lock = new ReentrantLock(); - protected Sequence createTime = new Sequence(System.currentTimeMillis()); - protected Sequence lastActiveTime = new Sequence(System.currentTimeMillis()); - - public AbstractActionQueue(Queue queue) { - super(); - this.queue = queue; - } - - public IActionQueue getActionQueue() { - return this; - } - - public Queue getQueue() { - return queue; - } - - /** - * 进队列,这个 action 会被执行的条件是 队列中没有其他的 action 在执行。否则在队列等待执行 - */ - public void enqueue(Action action) { - int queueSize = 0; - if (action.getActionQueue() == null) { - action.setActionQueue(this); - } - lock.lock(); - try { - queue.add(action); - queueSize = queue.size(); - } - finally { - lock.unlock(); - } - if (queueSize == 1) { - doExecute(action); - } - } - - public void dequeue(Action action) { - lastActiveTime.set(System.currentTimeMillis()); - Action nextAction = null; - int queueSize = 0; - String tmpString = null; - lock.lock(); - try { - queueSize = queue.size(); - Action temp = queue.remove(); - if (temp != action) { - tmpString = temp.toString(); - } - if (queueSize != 0) { - nextAction = queue.peek(); - } - } - finally { - lock.unlock(); - } - - if (nextAction != null) { - doExecute(nextAction); - } - - if (queueSize == 0) { - Log.debug("queue.size() is 0."); - } - if (tmpString != null) { - Log.debug("action queue error. temp " + tmpString + ", action : " - + action.toString()); - } - } - - public abstract void doExecute(Runnable action); - - public void clear() { - lock.lock(); - try { - queue.clear(); - } - finally { - lock.unlock(); - } - } - - @Override - public long getLastActiveTime() { - - return lastActiveTime.get(); - } - - @Override - public ReentrantLock getActionQueueLock() { - - return this.lock; - } + protected Queue queue; + protected ReentrantLock lock = new ReentrantLock(); + protected Sequence lastActiveTime = new Sequence(System.currentTimeMillis()); + + public AbstractActionQueue(Queue queue) { + super(); + this.queue = queue; + } + + public IActionQueue getActionQueue() { + return this; + } + + public Queue getQueue() { + return queue; + } + + /** + * 进队列,这个 action 会被执行的条件是 队列中没有其他的 action 在执行。否则在队列等待执行 + */ + public void enqueue(Action action) { + int queueSize = 0; + if (action.getActionQueue() == null) { + action.setActionQueue(this); + } + lock.lock(); + try { + queue.add(action); + queueSize = queue.size(); + } finally { + lock.unlock(); + } + if (queueSize == 1) { + doExecute(action); + } + } + + public void dequeue(Action action) { + lastActiveTime.set(System.currentTimeMillis()); + Action nextAction = null; + int queueSize = 0; + String tmpString = null; + lock.lock(); + try { + queueSize = queue.size(); + Action temp = queue.remove(); + if (temp != action) { + tmpString = temp.toString(); + } + if (queueSize != 0) { + nextAction = queue.peek(); + } + } finally { + lock.unlock(); + } + + if (nextAction != null) { + doExecute(nextAction); + } + + if (queueSize == 0) { + Log.debug("queue.size() is 0."); + } + if (tmpString != null) { + Log.debug("action queue error. temp " + tmpString + ", action : " + + action.toString()); + } + } + + public abstract void doExecute(Runnable action); + + public void clear() { + lock.lock(); + try { + queue.clear(); + } finally { + lock.unlock(); + } + } + + @Override + public long getLastActiveTime() { + + return lastActiveTime.get(); + } + + @Override + public ReentrantLock getActionQueueLock() { + + return this.lock; + } } diff --git a/event-swift/src/main/java/com/ware/swift/event/parallel/action/Action.java b/event-swift/src/main/java/com/ware/swift/event/parallel/action/Action.java index d182c7b..d2ae5a4 100644 --- a/event-swift/src/main/java/com/ware/swift/event/parallel/action/Action.java +++ b/event-swift/src/main/java/com/ware/swift/event/parallel/action/Action.java @@ -4,63 +4,64 @@ /** * if current action must wait the prefer finished then extend this class. - * @author pengbingting * + * @author pengbingting */ public abstract class Action implements Runnable { - protected IActionQueue queue; - protected Long createTime; - protected String topicName ; - - public Action() { - createTime = System.currentTimeMillis(); - } - - public Action(IActionQueue queue) { - this.queue = queue; - createTime = System.currentTimeMillis(); - } - - public void setActionQueue(IActionQueue queue){ - this.queue = queue; - } - - public IActionQueue getActionQueue() { - return queue; - } + protected IActionQueue queue; + protected Long createTime; + protected String topicName; + + public Action() { + createTime = System.currentTimeMillis(); + } + + public Action(IActionQueue queue) { + this.queue = queue; + createTime = System.currentTimeMillis(); + } + + public void setActionQueue(IActionQueue queue) { + this.queue = queue; + } + + public IActionQueue getActionQueue() { + return queue; + } + + public void run() { + if (queue != null) { + long start = System.currentTimeMillis(); + try { + execute(); + long end = System.currentTimeMillis(); + long interval = end - start; + long leftTime = end - createTime; + if (interval >= 1000) { + Log.warn("topic anme=" + topicName + "; execute action : " + this.toString() + ", interval : " + interval + ", leftTime : " + leftTime + ", size : " + queue.getQueue().size()); + } + } catch (Exception e) { + e.printStackTrace(); + Log.error("run action execute exception. action : " + this.toString() + e.getMessage()); + } finally { + queue.dequeue(this); + } + } + } + + public abstract void execute() throws ActionExecuteException; - public void run() { - if (queue != null) { - long start = System.currentTimeMillis(); - try { - execute(); - long end = System.currentTimeMillis(); - long interval = end - start; - long leftTime = end - createTime; - if (interval >= 1000) { - Log.warn("topic anme="+topicName+"; execute action : " + this.toString() + ", interval : " + interval + ", leftTime : " + leftTime + ", size : " + queue.getQueue().size()); - } - } catch (Exception e) { - e.printStackTrace(); - Log.error("run action execute exception. action : " + this.toString()+e.getMessage()); - } finally { - queue.dequeue(this); - } - } - } + public String getTopicName() { + return topicName; + } - public abstract void execute() throws ActionExecuteException; - - public String getTopicName() { - return topicName; - } + public void setTopicName(String topicName) { + this.topicName = topicName; + } - public void setTopicName(String topicName) { - this.topicName = topicName; - } + public void execeptionCaught(Exception e) { - public void execeptionCaught(Exception e){ - - } + //nothing to do + } } diff --git a/event-swift/src/main/java/com/ware/swift/event/parallel/action/ActionBridge.java b/event-swift/src/main/java/com/ware/swift/event/parallel/action/ActionBridge.java index 0bf43a3..46a24c0 100644 --- a/event-swift/src/main/java/com/ware/swift/event/parallel/action/ActionBridge.java +++ b/event-swift/src/main/java/com/ware/swift/event/parallel/action/ActionBridge.java @@ -1,20 +1,21 @@ package com.ware.swift.event.parallel.action; -public class ActionBridge extends Action{ +public class ActionBridge extends Action { - private Runnable runnable; - public ActionBridge(Runnable runnable) { - super(); - this.runnable = runnable; - } + private Runnable runnable; + + public ActionBridge(Runnable runnable) { + super(); + this.runnable = runnable; + } + + @Override + public void execute() throws ActionExecuteException { + if (runnable == null) { + return; + } + + runnable.run(); + } - @Override - public void execute() throws ActionExecuteException { - if(runnable == null){ - return ; - } - - runnable.run(); - } - } diff --git a/event-swift/src/main/java/com/ware/swift/event/parallel/action/ActionExecuteException.java b/event-swift/src/main/java/com/ware/swift/event/parallel/action/ActionExecuteException.java index 6b69de2..fe431da 100644 --- a/event-swift/src/main/java/com/ware/swift/event/parallel/action/ActionExecuteException.java +++ b/event-swift/src/main/java/com/ware/swift/event/parallel/action/ActionExecuteException.java @@ -3,14 +3,15 @@ public class ActionExecuteException extends Exception { - /** - * - */ - private static final long serialVersionUID = 1L; + /** + * + */ + private static final long serialVersionUID = 1L; - public ActionExecuteException() { - } - public ActionExecuteException(String message){ - super(message); - } + public ActionExecuteException() { + } + + public ActionExecuteException(String message) { + super(message); + } } diff --git a/event-swift/src/main/java/com/ware/swift/event/parallel/action/EmptyActionQueue.java b/event-swift/src/main/java/com/ware/swift/event/parallel/action/EmptyActionQueue.java index bf24649..578b7e7 100644 --- a/event-swift/src/main/java/com/ware/swift/event/parallel/action/EmptyActionQueue.java +++ b/event-swift/src/main/java/com/ware/swift/event/parallel/action/EmptyActionQueue.java @@ -6,45 +6,45 @@ public class EmptyActionQueue implements IActionQueue { - @Override - public IActionQueue getActionQueue() { - return this; - } + @Override + public IActionQueue getActionQueue() { + return this; + } - @Override - public void enqueue(Action t) { - t.run(); - } + @Override + public void enqueue(Action t) { + t.run(); + } - @Override - public void dequeue(Action t) { - // nothing to do - } + @Override + public void dequeue(Action t) { + // nothing to do + } - @Override - public void clear() { + @Override + public void clear() { - } + } - @Override - public Queue getQueue() { - return new LinkedList(); - } + @Override + public Queue getQueue() { + return new LinkedList(); + } - @Override - public long getLastActiveTime() { - return System.currentTimeMillis(); - } + @Override + public long getLastActiveTime() { + return System.currentTimeMillis(); + } - @Override - public void doExecute(Runnable action) { - // TODO Auto-generated method stub + @Override + public void doExecute(Runnable action) { + // TODO Auto-generated method stub - } + } - @Override - public ReentrantLock getActionQueueLock() { + @Override + public ReentrantLock getActionQueueLock() { - return new ReentrantLock(); - } + return new ReentrantLock(); + } } diff --git a/event-swift/src/main/java/com/ware/swift/event/parallel/action/FastParallelActionExecutor.java b/event-swift/src/main/java/com/ware/swift/event/parallel/action/FastParallelActionExecutor.java index ffc8456..2bd0a9e 100644 --- a/event-swift/src/main/java/com/ware/swift/event/parallel/action/FastParallelActionExecutor.java +++ b/event-swift/src/main/java/com/ware/swift/event/parallel/action/FastParallelActionExecutor.java @@ -1,180 +1,171 @@ package com.ware.swift.event.parallel.action; +import com.ware.swift.event.parallel.AbstractParallelQueueExecutor; +import com.ware.swift.event.parallel.DefaultThreadFactory; + import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.locks.ReentrantLock; -import com.ware.swift.event.parallel.DefaultThreadFactory; -import com.ware.swift.event.parallel.AbstractParallelQueueExecutor; - /** * 此设计模拟高速公路上车道设计。 n-1 条车道为正常处理车道,1条车道为应急车道。 并行队列 执行器 这里弥补了ParallelQueueExecutor * 的缺点:可以让同一种类型的车子在指定车道上跑。(也即同一种类型的 action 会被指定的 thread 执行)。消除频繁的上下问切换。 因此叫 * FastParallelQueueExecutor。因此是吞吐量优先,所以相对于 SuperFastParallelQueueExecutor 来叫还是慢了半拍的 - * + *

* 【注意】适用于 有状态的Action instance - * @author pengbingting * + * @author pengbingting */ public class FastParallelActionExecutor extends AbstractParallelActionExecutor { - private ThreadPoolExecutor[] pools; - // 维护一个 可以水平缩放的并行队列。并行队列的大小 n = paralleleQueue.size() + 1 - // 其中paralleleQueue.size()为 n-1 条正常处理的车道, - private ConcurrentMap> paralleleQueue = new ConcurrentHashMap<>(); - private ReentrantLock lock = new ReentrantLock(); - private AbstractParallelQueueExecutor.ThreadPoolExecutorIndexAllocator indexAllocator; - private AtomicBoolean isOperating = new AtomicBoolean(false); - private AtomicBoolean isCronTrriger = new AtomicBoolean(false); - - /** - * 执行action队列的线程池 - * - * @param corePoolSize 最小线程数,包括空闲线程 - * @param maxPoolSize 最大线程数 - * @param keepAliveTime 当线程数大于核心时,终止多余的空闲线程等待新任务的最长时间 - * @param cacheSize 执行队列大小 - * @param prefix 线程池前缀名称 - */ - public FastParallelActionExecutor(int threads, String prefix) { - if (threads <= 0) { - threads = AbstractParallelQueueExecutor.DEFAULT_QUEUE_SIZE; - } - - pools = new ThreadPoolExecutor[threads]; - for (int i = 0; i < threads; i++) { - // 半个小时之后还没有任务过来,则销毁线程 - pools[i] = new ThreadPoolExecutor(0, 1, 30L, TimeUnit.MINUTES, - new LinkedBlockingQueue(), - new DefaultThreadFactory(prefix)); - } - - if (isPowerOfTwo(threads)) { - indexAllocator = new AbstractParallelQueueExecutor.PowerOfTwoExecutorIndexChooser(threads); - } - else { - indexAllocator = new AbstractParallelQueueExecutor.GenericExecutorIndexChooser(threads); - } - } - - /** - * 将一个 action 进入一个并行队列。可以水平缩放 - * @param queueTopic - * @param action - */ - @SuppressWarnings({ "rawtypes", "unchecked" }) - public void enParallelAction(String queueTopic, Action action) { - IActionQueue actionQueue = paralleleQueue.get(queueTopic); - if (actionQueue == null) { - try { - lock.tryLock(3, TimeUnit.SECONDS); - isOperating.set(true); - actionQueue = paralleleQueue.get(queueTopic); - if (actionQueue == null) { - actionQueue = new FastParallelActionQueue( - pools[indexAllocator.allocator()]); - paralleleQueue.put(queueTopic, actionQueue); - } - } - catch (Exception e) { - actionQueue = new FastParallelActionQueue( - pools[indexAllocator.allocator()]); - } - finally { - isOperating.set(false); - lock.unlock(); - } - } - action.setTopicName(queueTopic); - trrigerWithRejectActionPolicy(actionQueue, action); - } - - public void removeParallelAction(String queueTopic) { - - paralleleQueue.remove(queueTopic); - } - - public void stop() { - super.stop(); - for (int i = 0; i < pools.length; i++) { - pools[i].shutdown(); - } - paralleleQueue.clear(); - } - - @Override - public void execute(Runnable command) { - execute(command.getClass().getName(), command); - } - - /** - * 如果这里不包装成一个Action,则跟SuperFastParallelQueueExecutor 没什么多大的区别 - */ - @Override - public void execute(String topic, Runnable command) { - if (!(command instanceof Action)) { - ActionBridge actionBridge = new ActionBridge(command); - enParallelAction(topic, actionBridge); - } - else { - Action action = (Action) command; - enParallelAction(topic, action); - } - } - - @Override - public void adjustPoolSize(int newCorePoolSize, int newMaxiPoolSize) { - // nothing to do - } - - @Override - public void registerTopics(String... topics) { - if (topics == null || topics.length == 0) { - return; - } - - lock.lock(); - try { - for (String queueTopic : topics) { - IActionQueue actionQueue = paralleleQueue.get(queueTopic); - if (actionQueue != null) { - continue; - } - - actionQueue = new FastParallelActionQueue( - pools[indexAllocator.allocator()]); - paralleleQueue.put(queueTopic, actionQueue); - } - } - finally { - lock.unlock(); - } - } - - @Override - public void removeTopic(String topic) { - - paralleleQueue.remove(topic); - } - - @Override - public void executeOneTime(Runnable command) { - if (command instanceof Action) { - Action action = (Action) command; - executeOneTimeAction(action); - } - else { - ActionBridge actionBridge = new ActionBridge(command); - executeOneTimeAction(actionBridge); - } - } - - @Override - public void executeOneTimeAction(Action action) { - IActionQueue fastActionQueue = new FastParallelActionQueue( - pools[indexAllocator.allocator()]); - - fastActionQueue.enqueue(action); - } + private ThreadPoolExecutor[] pools; + // 维护一个 可以水平缩放的并行队列。并行队列的大小 n = parallelQueue.size() + 1 + // 其中 parallel Queue.size()为 n-1 条正常处理的车道, + private ConcurrentMap> parallelQueue = new ConcurrentHashMap<>(); + private ReentrantLock lock = new ReentrantLock(); + private AbstractParallelQueueExecutor.ThreadPoolExecutorIndexAllocator indexAllocator; + private AtomicBoolean isOperating = new AtomicBoolean(false); + + /** + * 执行action队列的线程池 + * + * @param threads 执行线程的大小 + * @param prefix 线程池前缀名称 + */ + public FastParallelActionExecutor(int threads, String prefix) { + if (threads <= 0) { + threads = AbstractParallelQueueExecutor.DEFAULT_QUEUE_SIZE; + } + + pools = new ThreadPoolExecutor[threads]; + for (int i = 0; i < threads; i++) { + // 半个小时之后还没有任务过来,则销毁线程 + pools[i] = new ThreadPoolExecutor(0, 1, 30L, TimeUnit.MINUTES, + new LinkedBlockingQueue<>(), + new DefaultThreadFactory(prefix)); + } + + if (isPowerOfTwo(threads)) { + indexAllocator = new AbstractParallelQueueExecutor.PowerOfTwoExecutorIndexChooser(threads); + } else { + indexAllocator = new AbstractParallelQueueExecutor.GenericExecutorIndexChooser(threads); + } + } + + /** + * 将一个 action 进入一个并行队列。可以水平缩放 + * + * @param queueTopic + * @param action + */ + @SuppressWarnings({"rawtypes", "unchecked"}) + public void enParallelAction(String queueTopic, Action action) { + IActionQueue actionQueue = parallelQueue.get(queueTopic); + if (actionQueue == null) { + try { + lock.tryLock(3, TimeUnit.SECONDS); + isOperating.set(true); + actionQueue = parallelQueue.get(queueTopic); + if (actionQueue == null) { + actionQueue = new FastParallelActionQueue( + pools[indexAllocator.allocator()]); + parallelQueue.put(queueTopic, actionQueue); + } + } catch (Exception e) { + actionQueue = new FastParallelActionQueue( + pools[indexAllocator.allocator()]); + } finally { + isOperating.set(false); + lock.unlock(); + } + } + action.setTopicName(queueTopic); + trrigerWithRejectActionPolicy(actionQueue, action); + } + + public void removeParallelAction(String queueTopic) { + + parallelQueue.remove(queueTopic); + } + + public void stop() { + super.stop(); + for (int i = 0; i < pools.length; i++) { + pools[i].shutdown(); + } + parallelQueue.clear(); + } + + @Override + public void execute(Runnable command) { + execute(command.getClass().getName(), command); + } + + /** + * 如果这里不包装成一个Action,则跟SuperFastParallelQueueExecutor 没什么多大的区别 + */ + @Override + public void execute(String topic, Runnable command) { + if (!(command instanceof Action)) { + ActionBridge actionBridge = new ActionBridge(command); + enParallelAction(topic, actionBridge); + } else { + Action action = (Action) command; + enParallelAction(topic, action); + } + } + + @Override + public void adjustPoolSize(int newCorePoolSize, int newMaxiPoolSize) { + // nothing to do + } + + @Override + public void registerTopics(String... topics) { + if (topics == null || topics.length == 0) { + return; + } + + lock.lock(); + try { + for (String queueTopic : topics) { + IActionQueue actionQueue = parallelQueue.get(queueTopic); + if (actionQueue != null) { + continue; + } + + actionQueue = new FastParallelActionQueue( + pools[indexAllocator.allocator()]); + parallelQueue.put(queueTopic, actionQueue); + } + } finally { + lock.unlock(); + } + } + + @Override + public void removeTopic(String topic) { + + parallelQueue.remove(topic); + } + + @Override + public void executeOneTime(Runnable command) { + if (command instanceof Action) { + Action action = (Action) command; + executeOneTimeAction(action); + } else { + ActionBridge actionBridge = new ActionBridge(command); + executeOneTimeAction(actionBridge); + } + } + + @Override + public void executeOneTimeAction(Action action) { + IActionQueue fastActionQueue = new FastParallelActionQueue( + pools[indexAllocator.allocator()]); + + fastActionQueue.enqueue(action); + } } diff --git a/event-swift/src/main/java/com/ware/swift/event/parallel/action/FastParallelActionQueue.java b/event-swift/src/main/java/com/ware/swift/event/parallel/action/FastParallelActionQueue.java index 4b747a5..98d7cbe 100644 --- a/event-swift/src/main/java/com/ware/swift/event/parallel/action/FastParallelActionQueue.java +++ b/event-swift/src/main/java/com/ware/swift/event/parallel/action/FastParallelActionQueue.java @@ -7,40 +7,41 @@ /** * 基于响应式优先的并行队列。相对于ParallelActionQueue 的主要区别就是减少数据在多个不同的线程之间的切换 - * @author pengbingting * + * @author pengbingting */ -public class FastParallelActionQueue extends AbstractActionQueue{ - - private ThreadPoolExecutor signelThreadExecutor ; - public FastParallelActionQueue(ThreadPoolExecutor signelThreadExecutor,Queue queue) { - super(queue); - if(signelThreadExecutor.getCorePoolSize() != 1){ - signelThreadExecutor.setCorePoolSize(1); - signelThreadExecutor.setMaximumPoolSize(1); - signelThreadExecutor.setKeepAliveTime(0, TimeUnit.MILLISECONDS); - } - - this.signelThreadExecutor = signelThreadExecutor; - } - - public FastParallelActionQueue(ThreadPoolExecutor signelThreadExecutor) { - super(new LinkedList()); - if(signelThreadExecutor.getCorePoolSize() != 1){ - signelThreadExecutor.setCorePoolSize(1); - signelThreadExecutor.setMaximumPoolSize(1); - signelThreadExecutor.setKeepAliveTime(0, TimeUnit.MILLISECONDS); - } - - this.signelThreadExecutor = signelThreadExecutor; - } - - @Override - public void doExecute(Runnable action) { - if(signelThreadExecutor.getQueue().isEmpty()){ - signelThreadExecutor.execute(action); - }else{ - signelThreadExecutor.getQueue().offer(action); - } - } +public class FastParallelActionQueue extends AbstractActionQueue { + + private ThreadPoolExecutor singleThreadExecutor; + + public FastParallelActionQueue(ThreadPoolExecutor singleThreadExecutor, Queue queue) { + super(queue); + if (singleThreadExecutor.getCorePoolSize() != 1) { + singleThreadExecutor.setCorePoolSize(1); + singleThreadExecutor.setMaximumPoolSize(1); + singleThreadExecutor.setKeepAliveTime(0, TimeUnit.MILLISECONDS); + } + + this.singleThreadExecutor = singleThreadExecutor; + } + + public FastParallelActionQueue(ThreadPoolExecutor singleThreadExecutor) { + super(new LinkedList<>()); + if (singleThreadExecutor.getCorePoolSize() != 1) { + singleThreadExecutor.setCorePoolSize(1); + singleThreadExecutor.setMaximumPoolSize(1); + singleThreadExecutor.setKeepAliveTime(0, TimeUnit.MILLISECONDS); + } + + this.singleThreadExecutor = singleThreadExecutor; + } + + @Override + public void doExecute(Runnable action) { + if (singleThreadExecutor.getQueue().isEmpty()) { + singleThreadExecutor.execute(action); + } else { + singleThreadExecutor.getQueue().offer(action); + } + } } diff --git a/event-swift/src/main/java/com/ware/swift/event/parallel/action/ParallelActionExecutor.java b/event-swift/src/main/java/com/ware/swift/event/parallel/action/ParallelActionExecutor.java index cb2905a..b21ba6f 100644 --- a/event-swift/src/main/java/com/ware/swift/event/parallel/action/ParallelActionExecutor.java +++ b/event-swift/src/main/java/com/ware/swift/event/parallel/action/ParallelActionExecutor.java @@ -1,253 +1,233 @@ package com.ware.swift.event.parallel.action; import com.ware.swift.event.common.Log; +import com.ware.swift.event.parallel.DefaultThreadFactory; import java.util.concurrent.*; import java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.locks.ReentrantLock; -import com.ware.swift.event.parallel.DefaultThreadFactory; -import com.ware.swift.event.parallel.ThreadPoolContainer; - /** - * * 此设计模拟高速公路上车道设计。 n-1 条车道为正常处理车道,1条车道为应急车道。 并行队列 执行器 - * + *

* 这里的主要缺点就是:同一种类型的车子可以在任何条车道上跑。(也即同一种类型的 action 会被多个不同的 thread 执行)。存在频繁的在多个线程间切换。 * 【注意】适合无状态的Action instance 因为可以动态扩展线程池数量,因此在突发流量的情况下可以适当的增大线程数量,提供并发处理的能力。 + * * @author pengbingting */ public class ParallelActionExecutor extends AbstractParallelActionExecutor { - private ThreadPoolExecutor pool; - private String prefix; - // 维护一个 可以水平缩放的并行队列。并行队列的大小 n = paralleleQueue.size() + 1 - // 其中paralleleQueue.size()为 n-1 条正常处理的车道, - private ConcurrentMap> paralleleQueue = new ConcurrentHashMap>(); - private ReentrantLock lock = new ReentrantLock(); - private AtomicBoolean isOperating = new AtomicBoolean(false); - private AtomicBoolean isCronTrriger = new AtomicBoolean(false); - - public ParallelActionExecutor(int corePoolSize, int maxPoolSize, int keepAliveTime, - String prefix) { - - this(corePoolSize, maxPoolSize, keepAliveTime, TimeUnit.MINUTES, prefix, - new DiscardOldestPolicy()); - } - - public ParallelActionExecutor(int corePoolSize, int maxPoolSize, int keepAliveTime, - TimeUnit unit, String prefix) { - - this(corePoolSize, maxPoolSize, keepAliveTime, unit, prefix, - new DiscardOldestPolicy()); - } - - /** - * 执行action队列的线程池 - * - * @param corePoolSize 最小线程数,包括空闲线程 - * @param maxPoolSize 最大线程数 - * @param keepAliveTime 当线程数大于核心时,终止多余的空闲线程等待新任务的最长时间 - * @param prefix 线程池前缀名称 - * @param rejectedExecutionHandler 拒绝策略 - */ - public ParallelActionExecutor(int corePoolSize, int maxPoolSize, int keepAliveTime, - String prefix, RejectedExecutionHandler rejectedExecutionHandler) { - - this(corePoolSize, maxPoolSize, keepAliveTime, TimeUnit.MINUTES, prefix, - rejectedExecutionHandler); - } - - public ParallelActionExecutor(int corePoolSize, int maxPoolSize, int keepAliveTime, - TimeUnit unit, String prefix, - RejectedExecutionHandler rejectedExecutionHandler) { - LinkedBlockingQueue workQueue = new LinkedBlockingQueue( - Integer.MAX_VALUE);// 无界队列,不需 处理策略 - if (prefix == null) { - prefix = ""; - } - this.prefix = prefix; - ThreadFactory threadFactory = new DefaultThreadFactory(prefix); - pool = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, unit, - workQueue, threadFactory, rejectedExecutionHandler); - } - - /** - * 定制化配置 - * @param pool - */ - public ParallelActionExecutor(ThreadPoolExecutor pool) { - this.pool = pool; - } - - public ThreadPoolContainer getThreadPollContainer() { - - ThreadPoolContainer threadPoolContainer = new ThreadPoolContainer(); - threadPoolContainer.setThreadSize(this.pool.getPoolSize()); - threadPoolContainer.setLargestPoolSize( - this.pool.getLargestPoolSize() + this.pool.getPoolSize()); - threadPoolContainer.setActiveTaskCount(this.pool.getActiveCount()); - threadPoolContainer.setCompleteTaskCount((int) this.pool.getCompletedTaskCount()); - threadPoolContainer.setWaitTaskCount(this.pool.getQueue().size()); - threadPoolContainer.setTaskCount((int) this.pool.getTaskCount()); - return threadPoolContainer; - } - - /** - * if you execute a temporary or disposable action then call this method,or normal and - * executor more times you should be call the enDefaultQueue method - * @param action - */ - public void execute(Runnable command) { - String topic = command.getClass().getName(); - execute(topic, command); - } - - @Override - public void execute(String topic, Runnable command) { - if (!(command instanceof Action)) { - // 如果不是 Action 的实例,则桥接一个 - enParallelAction(topic, new ActionBridge(command)); - } - else { - Action tmpAction = (Action) command; - enParallelAction(topic, tmpAction); - } - } - - /** - * @param newCorePoolSize - * @param newMaxiPoolSize - */ - public void adjustPoolSize(int newCorePoolSize, int newMaxiPoolSize) { - if (newCorePoolSize != pool.getCorePoolSize()) { - if (newCorePoolSize > 0 && newCorePoolSize < 357) { - pool.setCorePoolSize(newCorePoolSize); - } - - if (newMaxiPoolSize > 0 && newMaxiPoolSize < 357) { - pool.setMaximumPoolSize(newCorePoolSize); - } - } - } - - public boolean isShutdown() { - - return pool.isShutdown(); - } - - public String getPrefix() { - - return this.prefix; - } - - public T submit(Callable command) { - try { - return pool.submit(command).get(); - } - catch (InterruptedException e) { - Log.error(this.getClass().getName() + "; submit a task excetion.", e); - e.printStackTrace(); - } - catch (ExecutionException e) { - Log.error(this.getClass().getName() + "; submit a task excetion.", e); - e.printStackTrace(); - } - return null; - } - - public void stop() { - super.stop(); - if (!pool.isShutdown()) { - pool.shutdown(); - } - - for (IActionQueue actionQueue : paralleleQueue.values()) { - actionQueue.clear(); - } - } - - /** - * 将一个 action 进入一个并行队列。可以水平缩放 - * @param queueTopic - * @param action - */ - @Override - public void enParallelAction(String queueTopic, Action action) { - IActionQueue actionQueue = paralleleQueue.get(queueTopic); - if (actionQueue == null) { - try { - lock.tryLock(3, TimeUnit.SECONDS); - isOperating.set(true); - actionQueue = paralleleQueue.get(queueTopic); - if (actionQueue == null) { - actionQueue = new ParallelActionQueue(pool); - paralleleQueue.put(queueTopic, actionQueue); - } - } - catch (Exception e) { - // 给一个临时队列 - actionQueue = new ParallelActionQueue(pool); - } - finally { - isOperating.set(false); - lock.unlock(); - } - } - action.setTopicName(queueTopic); - trrigerWithRejectActionPolicy(actionQueue, action); - } - - @Override - public void removeParallelAction(String queueTopic) { - paralleleQueue.remove(queueTopic); - } - - @Override - public void registerTopics(String... topics) { - if (topics == null || topics.length == 0) { - return; - } - - lock.lock(); - try { - for (String queueTopic : topics) { - IActionQueue actionQueue = paralleleQueue.get(queueTopic); - if (actionQueue != null) { - continue; - } - - actionQueue = new ParallelActionQueue(pool); - paralleleQueue.put(queueTopic, actionQueue); - } - } - finally { - lock.unlock(); - } - } - - @Override - public void removeTopic(String topic) { - - paralleleQueue.remove(topic); - } - - @Override - public void executeOneTime(Runnable command) { - if (command instanceof Action) { - Action action = (Action) command; - executeOneTimeAction(action); - } - else { - ActionBridge actionBridge = new ActionBridge(command); - executeOneTimeAction(actionBridge); - } - } - - @Override - public void executeOneTimeAction(Action action) { - IActionQueue actionQueue = new ParallelActionQueue(pool); - actionQueue.enqueue(action); - } + private ThreadPoolExecutor pool; + private String prefix; + // 维护一个 可以水平缩放的并行队列。并行队列的大小 n = parallelQueue.size() + 1 + // 其中parallelQueue.size()为 n-1 条正常处理的车道, + private ConcurrentMap> parallelQueue = new ConcurrentHashMap<>(); + private ReentrantLock lock = new ReentrantLock(); + private AtomicBoolean isOperating = new AtomicBoolean(false); + + public ParallelActionExecutor(int corePoolSize, int maxPoolSize, int keepAliveTime, + String prefix) { + + this(corePoolSize, maxPoolSize, keepAliveTime, TimeUnit.MINUTES, prefix, + new DiscardOldestPolicy()); + } + + public ParallelActionExecutor(int corePoolSize, int maxPoolSize, int keepAliveTime, + TimeUnit unit, String prefix) { + + this(corePoolSize, maxPoolSize, keepAliveTime, unit, prefix, + new DiscardOldestPolicy()); + } + + /** + * 执行action队列的线程池 + * + * @param corePoolSize 最小线程数,包括空闲线程 + * @param maxPoolSize 最大线程数 + * @param keepAliveTime 当线程数大于核心时,终止多余的空闲线程等待新任务的最长时间 + * @param prefix 线程池前缀名称 + * @param rejectedExecutionHandler 拒绝策略 + */ + public ParallelActionExecutor(int corePoolSize, int maxPoolSize, int keepAliveTime, + String prefix, RejectedExecutionHandler rejectedExecutionHandler) { + + this(corePoolSize, maxPoolSize, keepAliveTime, TimeUnit.MINUTES, prefix, + rejectedExecutionHandler); + } + + public ParallelActionExecutor(int corePoolSize, int maxPoolSize, int keepAliveTime, + TimeUnit unit, String prefix, + RejectedExecutionHandler rejectedExecutionHandler) { + LinkedBlockingQueue workQueue = new LinkedBlockingQueue<>( + Integer.MAX_VALUE);// 无界队列,不需 处理策略 + if (prefix == null) { + prefix = ""; + } + this.prefix = prefix; + ThreadFactory threadFactory = new DefaultThreadFactory(prefix); + pool = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, unit, + workQueue, threadFactory, rejectedExecutionHandler); + } + + /** + * 定制化配置 + * + * @param pool + */ + public ParallelActionExecutor(ThreadPoolExecutor pool) { + this.pool = pool; + } + + /** + * if you execute a temporary or disposable action then call this method,or normal and + * executor more times you should be call the enDefaultQueue method + * + * @param command + */ + public void execute(Runnable command) { + String topic = command.getClass().getName(); + execute(topic, command); + } + + @Override + public void execute(String topic, Runnable command) { + if (!(command instanceof Action)) { + // 如果不是 Action 的实例,则桥接一个 + enParallelAction(topic, new ActionBridge(command)); + } else { + Action tmpAction = (Action) command; + enParallelAction(topic, tmpAction); + } + } + + /** + * @param newCorePoolSize + * @param newMaxiPoolSize + */ + public void adjustPoolSize(int newCorePoolSize, int newMaxiPoolSize) { + if (newCorePoolSize != pool.getCorePoolSize()) { + if (newCorePoolSize > 0 && newCorePoolSize < 357) { + pool.setCorePoolSize(newCorePoolSize); + } + + if (newMaxiPoolSize > 0 && newMaxiPoolSize < 357) { + pool.setMaximumPoolSize(newCorePoolSize); + } + } + } + + public boolean isShutdown() { + + return pool.isShutdown(); + } + + public String getPrefix() { + + return this.prefix; + } + + public T submit(Callable command) { + try { + return pool.submit(command).get(); + } catch (InterruptedException e) { + Log.error(this.getClass().getName() + "; submit a task excetion.", e); + e.printStackTrace(); + } catch (ExecutionException e) { + Log.error(this.getClass().getName() + "; submit a task excetion.", e); + e.printStackTrace(); + } + return null; + } + + public void stop() { + super.stop(); + if (!pool.isShutdown()) { + pool.shutdown(); + } + + for (IActionQueue actionQueue : parallelQueue.values()) { + actionQueue.clear(); + } + } + + /** + * 将一个 action 进入一个并行队列。可以水平缩放 + * + * @param queueTopic + * @param action + */ + @Override + public void enParallelAction(String queueTopic, Action action) { + IActionQueue actionQueue = parallelQueue.get(queueTopic); + if (actionQueue == null) { + try { + lock.tryLock(3, TimeUnit.SECONDS); + isOperating.set(true); + actionQueue = parallelQueue.get(queueTopic); + if (actionQueue == null) { + actionQueue = new ParallelActionQueue(pool); + parallelQueue.put(queueTopic, actionQueue); + } + } catch (Exception e) { + // 给一个临时队列 + actionQueue = new ParallelActionQueue(pool); + } finally { + isOperating.set(false); + lock.unlock(); + } + } + action.setTopicName(queueTopic); + trrigerWithRejectActionPolicy(actionQueue, action); + } + + @Override + public void removeParallelAction(String queueTopic) { + parallelQueue.remove(queueTopic); + } + + @Override + public void registerTopics(String... topics) { + if (topics == null || topics.length == 0) { + return; + } + + lock.lock(); + try { + for (String queueTopic : topics) { + IActionQueue actionQueue = parallelQueue.get(queueTopic); + if (actionQueue != null) { + continue; + } + + actionQueue = new ParallelActionQueue(pool); + parallelQueue.put(queueTopic, actionQueue); + } + } finally { + lock.unlock(); + } + } + + @Override + public void removeTopic(String topic) { + + parallelQueue.remove(topic); + } + + @Override + public void executeOneTime(Runnable command) { + if (command instanceof Action) { + Action action = (Action) command; + executeOneTimeAction(action); + } else { + ActionBridge actionBridge = new ActionBridge(command); + executeOneTimeAction(actionBridge); + } + } + + @Override + public void executeOneTimeAction(Action action) { + IActionQueue actionQueue = new ParallelActionQueue(pool); + actionQueue.enqueue(action); + } } diff --git a/event-swift/src/main/java/com/ware/swift/event/parallel/action/ParallelActionQueue.java b/event-swift/src/main/java/com/ware/swift/event/parallel/action/ParallelActionQueue.java index 8deb055..3912c76 100644 --- a/event-swift/src/main/java/com/ware/swift/event/parallel/action/ParallelActionQueue.java +++ b/event-swift/src/main/java/com/ware/swift/event/parallel/action/ParallelActionQueue.java @@ -8,31 +8,31 @@ * 吞吐量优先的行为队列 * 说明:这里的队列是严格的无阻塞队列。如果这个队列中某个业务室阻塞式的,那么将会导致一个严重的问题就是后面的action 就无法保证顺利执行。 * 使用场景:无阻塞式行为队列 - * @author pengbingting * + * @author pengbingting */ -public class ParallelActionQueue extends AbstractActionQueue{ - - //------------------------------- - private ThreadPoolExecutor executor; - - public ParallelActionQueue(ThreadPoolExecutor executor) { - super(new LinkedList()); - this.executor = executor; - } +public class ParallelActionQueue extends AbstractActionQueue { + + //------------------------------- + private ThreadPoolExecutor executor; + + public ParallelActionQueue(ThreadPoolExecutor executor) { + super(new LinkedList<>()); + this.executor = executor; + } + + public ParallelActionQueue(ThreadPoolExecutor executor, Queue queue) { + super(queue); + this.executor = executor; + } - public ParallelActionQueue(ThreadPoolExecutor executor, Queue queue) { - super(queue); - this.executor = executor; - } + @Override + public void doExecute(Runnable action) { + executor.execute(action); + } - @Override - public void doExecute(Runnable action) { - executor.execute(action); - } - - @Override - public void clear() { - super.clear(); - } + @Override + public void clear() { + super.clear(); + } } diff --git a/event-swift/src/test/java/com/ware/swift/event/tests.java b/event-swift/src/test/java/com/ware/swift/event/tests.java new file mode 100644 index 0000000..c679e39 --- /dev/null +++ b/event-swift/src/test/java/com/ware/swift/event/tests.java @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2019 the original author or authors. + * + * 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 com.ware.swift.event; + +import org.junit.Test; + +import java.util.HashMap; + +/** + * @author pbting + * @date 2019-05-20 11:32 PM + */ +public class tests { + + @Test + public void evenTypeTest() { + + HashMap values = new HashMap<>(); + values.put(1, "111"); + System.err.println(values.get(1)); + } +} \ No newline at end of file diff --git a/grpc-swift/src/main/java/com/ware/swift/grpc/AbstractCallStreamObserver.java b/grpc-swift/src/main/java/com/ware/swift/grpc/AbstractCallStreamObserver.java index a762e9d..62dafb1 100644 --- a/grpc-swift/src/main/java/com/ware/swift/grpc/AbstractCallStreamObserver.java +++ b/grpc-swift/src/main/java/com/ware/swift/grpc/AbstractCallStreamObserver.java @@ -11,7 +11,7 @@ public abstract class AbstractCallStreamObserver extends CallStreamObserver implements IInteractive, IIdentifyStreamObserver { - private String indentifyStream; + private String identifyStream; /** * if you have some payload to send other nodes,it very useful. */ @@ -55,11 +55,11 @@ public void onNext(InteractivePayload value) { request(new GrpcRequestStreamInteractive(value, interactiveStream)); } - public abstract void request(GrpcRequestStreamInteractive raftInteractive); + public abstract void request(GrpcRequestStreamInteractive wareSwiftInteractive); - public boolean sendPayload(InteractivePayload raftInteractivePayload) { + public boolean sendPayload(InteractivePayload wareSwiftInteractivePayload) { if (interactiveStream.isReady()) { - interactiveStream.onNext(raftInteractivePayload); + interactiveStream.onNext(wareSwiftInteractivePayload); return true; } @@ -76,12 +76,12 @@ public void onError(Throwable t) { } @Override - public String getIndendity() { - return indentifyStream; + public String getIdentify() { + return identifyStream; } @Override - public void setIndendity(String indendity) { - this.indentifyStream = indendity; + public void setIdentify(String identify) { + this.identifyStream = identify; } } diff --git a/grpc-swift/src/main/java/com/ware/swift/grpc/ClientRequestStreamObserver.java b/grpc-swift/src/main/java/com/ware/swift/grpc/ClientRequestStreamObserver.java index 30ddb1b..c18f820 100644 --- a/grpc-swift/src/main/java/com/ware/swift/grpc/ClientRequestStreamObserver.java +++ b/grpc-swift/src/main/java/com/ware/swift/grpc/ClientRequestStreamObserver.java @@ -24,14 +24,14 @@ public ClientRequestStreamObserver( /** * The Entry of Client Request * - * @param raftInteractive + * @param wareSwiftInteractive */ @Override - public void request(GrpcRequestStreamInteractive raftInteractive) { - int eventType = raftInteractive.getInteractivePayload().getEventType(); + public void request(GrpcRequestStreamInteractive wareSwiftInteractive) { + int eventType = wareSwiftInteractive.getInteractivePayload().getEventType(); logger.debug("receive client request with the event type :" + eventType); RemotingEventDispatcher.getInstance() - .dispatcher(new ObjectEvent(this, raftInteractive, eventType)); + .dispatcher(new ObjectEvent(this, wareSwiftInteractive, eventType)); } @Override diff --git a/grpc-swift/src/main/java/com/ware/swift/grpc/DecentrationGrpcRemotingManager.java b/grpc-swift/src/main/java/com/ware/swift/grpc/DecentrationGrpcRemotingManager.java index e0d70f0..b141cce 100644 --- a/grpc-swift/src/main/java/com/ware/swift/grpc/DecentrationGrpcRemotingManager.java +++ b/grpc-swift/src/main/java/com/ware/swift/grpc/DecentrationGrpcRemotingManager.java @@ -2,11 +2,11 @@ import com.ware.swift.core.NodeState; import com.ware.swift.core.remoting.HeartbeatMultiMailbox; -import com.ware.swift.core.remoting.IDecentrationRemotingManager; +import com.ware.swift.core.remoting.IDecentralizeRemotingManager; import com.ware.swift.core.remoting.IMailbox; import com.ware.swift.core.remoting.RemotingInteractiveConstants; -import com.ware.swift.core.remoting.event.local.DecentrationInitRemotingChannelEventListener; -import com.ware.swift.core.remoting.event.local.DecentrationNodeMeetEventListener; +import com.ware.swift.core.remoting.event.local.DecentralizeInitRemotingChannelEventListener; +import com.ware.swift.core.remoting.event.local.DecentralizeNodeMeetEventListener; import com.ware.swift.core.remoting.event.local.InitRemotingChannelEventListener; import com.ware.swift.proto.InteractivePayload; @@ -16,7 +16,7 @@ * 提供去中心化能力的 grpc remoting manager */ public class DecentrationGrpcRemotingManager extends GrpcRemotingManagerSupport - implements IDecentrationRemotingManager { + implements IDecentralizeRemotingManager { private IMailbox mailbox = new HeartbeatMultiMailbox( this.getClass().getName()); @@ -45,14 +45,14 @@ public void initEventListener() { @Override public void initNodeMeetEventListener() { - remotingEventLoopGroup.addLast(new DecentrationNodeMeetEventListener(this), + remotingEventLoopGroup.addLast(new DecentralizeNodeMeetEventListener(this), NODE_MEET_EVENT_TYPE); } @Override public InitRemotingChannelEventListener initRemotingChannelEventListener() { - return new DecentrationInitRemotingChannelEventListener(this); + return new DecentralizeInitRemotingChannelEventListener(this); } @Override @@ -69,7 +69,7 @@ public void setServer(T server) { @Override public String getChannelIdentify(String key) { - return RemotingInteractiveConstants.PTP_ROLE_PRIFIX + "@" + key; + return RemotingInteractiveConstants.PTP_ROLE_PREFIX + "@" + key; } @Override @@ -91,7 +91,7 @@ public void initTimeoutCheckEventListener() { @Override public void producer(InteractivePayload value) { - DecentrationRemotingManager.DEFAULT_IMPL.producer(value, nodesHeartbeatMailboxRegistry); + DecentralizeRemotingManager.DEFAULT_IMPL.producer(value, nodesHeartbeatMailboxRegistry); } @Override @@ -103,7 +103,7 @@ public void addHeartbeatTimeoutCheckMailbox(IMailbox mailbox @Override public void processClusterNodes(String nodes) { - DecentrationRemotingManager.DEFAULT_IMPL.processClusterNodes(this, nodes); + DecentralizeRemotingManager.DEFAULT_IMPL.processClusterNodes(this, nodes); } } diff --git a/grpc-swift/src/main/java/com/ware/swift/grpc/GrpcRequestResponseInteractive.java b/grpc-swift/src/main/java/com/ware/swift/grpc/GrpcRequestResponseInteractive.java index db0b53f..b6b5246 100644 --- a/grpc-swift/src/main/java/com/ware/swift/grpc/GrpcRequestResponseInteractive.java +++ b/grpc-swift/src/main/java/com/ware/swift/grpc/GrpcRequestResponseInteractive.java @@ -8,9 +8,9 @@ */ public class GrpcRequestResponseInteractive extends AbstractGrpcInteractive { - public GrpcRequestResponseInteractive(InteractivePayload raftInteractivePayload, + public GrpcRequestResponseInteractive(InteractivePayload wareSwiftInteractivePayload, CallStreamObserver responseStream) { - super(raftInteractivePayload, responseStream); + super(wareSwiftInteractivePayload, responseStream); } @Override diff --git a/grpc-swift/src/main/java/com/ware/swift/grpc/IIdentifyStreamObserver.java b/grpc-swift/src/main/java/com/ware/swift/grpc/IIdentifyStreamObserver.java index b579911..66aba52 100644 --- a/grpc-swift/src/main/java/com/ware/swift/grpc/IIdentifyStreamObserver.java +++ b/grpc-swift/src/main/java/com/ware/swift/grpc/IIdentifyStreamObserver.java @@ -1,10 +1,13 @@ package com.ware.swift.grpc; +/** + * + */ public interface IIdentifyStreamObserver { - String getIndendity(); + String getIdentify(); - void setIndendity(String indendity); + void setIdentify(String indendity); - AbstractCallStreamObserver getAbstractCallStreamObserver(); + AbstractCallStreamObserver getAbstractCallStreamObserver(); } diff --git a/grpc-swift/src/main/java/com/ware/swift/grpc/LeaderFollowerGrpcRemotingManager.java b/grpc-swift/src/main/java/com/ware/swift/grpc/LeaderFollowerGrpcRemotingManager.java index de7db75..59f811a 100644 --- a/grpc-swift/src/main/java/com/ware/swift/grpc/LeaderFollowerGrpcRemotingManager.java +++ b/grpc-swift/src/main/java/com/ware/swift/grpc/LeaderFollowerGrpcRemotingManager.java @@ -5,13 +5,12 @@ import com.ware.swift.core.remoting.IMailbox; import com.ware.swift.core.remoting.RemotingInteractiveConstants; import com.ware.swift.core.remoting.event.local.*; - import com.ware.swift.proto.InteractivePayload; /** * 需要明确知道以下三件事: *

- * 1. raft 节点之间的通信入口是哪一个: GrpcRemotingChannel 实例 + * 1. wareSwift 节点之间的通信入口是哪一个: GrpcRemotingChannel 实例 *

* 2. 服务端有数据 response 的客户端入口在哪里: ServerResponseStreamObserver *

@@ -20,7 +19,7 @@ public class LeaderFollowerGrpcRemotingManager extends GrpcRemotingManagerSupport implements ILeaderFollowerRemotingManager { - private IMailbox signelMailbox = new HeartbeatSingleMailbox( + private IMailbox singleMailbox = new HeartbeatSingleMailbox( this.getClass().getName()); @Override @@ -71,12 +70,12 @@ public void initSendHeartbeatEventListener() { @Override public String getChannelIdentify(String key) { - return RemotingInteractiveConstants.RAFT_ROLE_PRIFIX + "@" + key; + return RemotingInteractiveConstants.RAFT_ROLE_PREFIX + "@" + key; } @Override public IMailbox getMailbox() { - return this.signelMailbox; + return this.singleMailbox; } @Override diff --git a/grpc-swift/src/main/java/com/ware/swift/grpc/RaftInteractiveServiceGrpcImpl.java b/grpc-swift/src/main/java/com/ware/swift/grpc/RaftInteractiveServiceGrpcImpl.java index e2f3aef..6a0c8d0 100644 --- a/grpc-swift/src/main/java/com/ware/swift/grpc/RaftInteractiveServiceGrpcImpl.java +++ b/grpc-swift/src/main/java/com/ware/swift/grpc/RaftInteractiveServiceGrpcImpl.java @@ -36,10 +36,10 @@ public void requestResponse(InteractivePayload request, int eventType = request.getEventType(); logger.debug("receive client request with the event type :" + eventType); - AbstractGrpcInteractive raftInteractive = new GrpcRequestResponseInteractive( + AbstractGrpcInteractive wareSwiftInteractive = new GrpcRequestResponseInteractive( request, (CallStreamObserver) responseObserver); RemotingEventDispatcher.getInstance() - .dispatcher(new ObjectEvent(this, raftInteractive, eventType)); + .dispatcher(new ObjectEvent(this, wareSwiftInteractive, eventType)); } @Override @@ -47,9 +47,9 @@ public void requestStream(InteractivePayload request, StreamObserver responseObserver) { int eventType = request.getEventType(); logger.debug("receive client request with the event type :" + eventType); - AbstractGrpcInteractive raftInteractive = new GrpcRequestStreamInteractive( + AbstractGrpcInteractive wareSwiftInteractive = new GrpcRequestStreamInteractive( request, (CallStreamObserver) responseObserver); RemotingEventDispatcher.getInstance() - .dispatcher(new ObjectEvent(this, raftInteractive, eventType)); + .dispatcher(new ObjectEvent(this, wareSwiftInteractive, eventType)); } } diff --git a/hot-data-on-swift/pom.xml b/hot-data-on-swift/pom.xml index bd6ffb8..e6b1545 100644 --- a/hot-data-on-swift/pom.xml +++ b/hot-data-on-swift/pom.xml @@ -11,5 +11,11 @@ hot-data-on-swift jar - + + + ${project.groupId} + rsocket-swift + ${project.version} + + \ No newline at end of file diff --git a/eureka-on-swift/src/test/java/com/ware/swift/eureka/tests/DataSyncTests.java b/hot-data-on-swift/src/main/java/com/ware/swift/hot/data/HotDataApplication.java similarity index 69% rename from eureka-on-swift/src/test/java/com/ware/swift/eureka/tests/DataSyncTests.java rename to hot-data-on-swift/src/main/java/com/ware/swift/hot/data/HotDataApplication.java index 4df897c..53110ca 100644 --- a/eureka-on-swift/src/test/java/com/ware/swift/eureka/tests/DataSyncTests.java +++ b/hot-data-on-swift/src/main/java/com/ware/swift/hot/data/HotDataApplication.java @@ -13,11 +13,18 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.ware.swift.eureka.tests; +package com.ware.swift.hot.data; + +import com.ware.swift.core.WareSwiftDeveloperManager; /** * @author pbting - * @date 2019-05-18 3:28 AM + * @date 2019-05-19 10:10 PM */ -public class DataSyncTests { +public class HotDataApplication { + + public static void main(String[] args) { + + WareSwiftDeveloperManager.wareSwiftInit("ware-swift.properties"); + } } \ No newline at end of file diff --git a/hot-data-on-swift/src/main/java/com/ware/swift/hot/data/HotDataAvailableCapabilityModel.java b/hot-data-on-swift/src/main/java/com/ware/swift/hot/data/HotDataAvailableCapabilityModel.java new file mode 100644 index 0000000..02619a2 --- /dev/null +++ b/hot-data-on-swift/src/main/java/com/ware/swift/hot/data/HotDataAvailableCapabilityModel.java @@ -0,0 +1,193 @@ +/* + * Copyright (C) 2019 the original author or authors. + * + * 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 com.ware.swift.hot.data; + +import com.ware.swift.core.WareSwiftDeveloperManager; +import com.ware.swift.core.remoting.*; +import com.ware.swift.core.remoting.avalispart.AbstractAvailableCapabilityModel; +import com.ware.swift.event.mcache.AbstractConcurrentCache; +import com.ware.swift.event.mcache.IExpireKeyHandler; +import com.ware.swift.event.mcache.LRFUByDxConcurrentCache; +import com.ware.swift.proto.InteractivePayload; + +import java.util.Collection; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.logging.Logger; + +/** + * @author pbting + * @date 2019-05-19 10:18 AM + */ +public class HotDataAvailableCapabilityModel extends AbstractAvailableCapabilityModel { + + private static final Logger logger = Logger.getLogger(HotDataRemotingDomain.class.getCanonicalName()); + /** + * + */ + private final ConcurrentHashMap> hotDataRegistry = new ConcurrentHashMap<>(); + + /** + * 1. 当有新的节点加入或者节点重启时,需要对数据进行同步时,可在这里添加您的代码 + * + * @param syncDataEmitter + */ + @Override + + public void onDataStreamSyncing(DataSyncEmitter syncDataEmitter) { + + hotDataRegistry.values().forEach(stringHotDataRemotingDomainConcurrentMap -> stringHotDataRemotingDomainConcurrentMap.values().forEach(hotDataRemotingDomain -> syncDataEmitter.onEmit(hotDataRemotingDomain))); + syncDataEmitter.onEmitFinish(); + } + + /** + * 2. 当一次性同步失败时,系统会用一定的内存空间来持续进行失败节点的重试同步。 + * 当同步超过一定时间后,系统会自动的触发这里的回调,业务层给出具体的处理策略。 + * + * @param outOfSyncingWaterMarker + */ + @Override + public void onOutOfSyncingWaterMarker(Collection outOfSyncingWaterMarker) { + + } + + /** + * @param remotingDomain + */ + @Override + public void onAfterOutboundDataSet(RemotingDomainSupport remotingDomain) { + + processHotDataRemotingDomain((HotDataRemotingDomain) remotingDomain); + } + + /** + * @param remotingDomain + */ + private void processHotDataRemotingDomain(HotDataRemotingDomain remotingDomain) { + logger.info("process hot data remoting domain:" + remotingDomain.toString()); + final HotDataRemotingDomain hotDataRemotingDomain = remotingDomain; + + if (WareSwiftDeveloperManager.isDebug()) { + HotDataClientRequestHandler hotDataClientRequestHandler = WareSwiftDeveloperManager.getClientPayloadHandler(); + hotDataClientRequestHandler.processSubscriberPush(remotingDomain); + } + + AbstractConcurrentCache abstractConcurrentCache = new LRFUByDxConcurrentCache<>(hotDataRemotingDomain.getTopic(), new HotDataExpireKeyHandler(), 1024); + ConcurrentMap concurrentMap = hotDataRegistry.putIfAbsent(hotDataRemotingDomain.getTopic(), abstractConcurrentCache); + + if (concurrentMap == null) { + concurrentMap = abstractConcurrentCache; + } + + if (RemotingDomainManager.isAddOperator(remotingDomain) || RemotingDomainManager.isUpdateOperator(remotingDomain)) { + HotDataRemotingDomain preHotDataRemotingDomain = concurrentMap.putIfAbsent(hotDataRemotingDomain.getKey(), hotDataRemotingDomain); + boolean isPush = true; + if (preHotDataRemotingDomain != null) { + if (preHotDataRemotingDomain.getReVersion() < hotDataRemotingDomain.getReVersion()) { + concurrentMap.put(hotDataRemotingDomain.getKey(), hotDataRemotingDomain); + isPush = true; + } else { + isPush = false; + } + } + + if (isPush) { + HotDataClientRequestHandler hotDataClientRequestHandler = WareSwiftDeveloperManager.getClientPayloadHandler(); + hotDataClientRequestHandler.processSubscriberPush(hotDataRemotingDomain); + } + return; + } + + if (RemotingDomainManager.isDeleteOperator(remotingDomain)) { + + concurrentMap.remove(remotingDomain.getKey()); + // 数据发生变更,推送到订阅的客户端。 + HotDataClientRequestHandler hotDataClientRequestHandler = WareSwiftDeveloperManager.getClientPayloadHandler(); + hotDataClientRequestHandler.processSubscriberPush(remotingDomain); + return; + } + } + + /** + * 3. 集群节点间数据同步时, + * + * @param remotingDomain + * @param headsMap + * @throws RemotingInteractiveException + */ + @Override + public void onInboundDataSet(RemotingDomainSupport remotingDomain, Map headsMap) throws RemotingInteractiveException { + + processHotDataRemotingDomain((HotDataRemotingDomain) remotingDomain); + } + + /** + * 4. 集群节点进行数据同步时,如果构建自定义的 InteractivePayload。 + *

+ * 一般来讲,调用{@link com.ware.swift.core.remoting.ClusterDataSyncManager#newSyncInteractivePayload(RemotingDomainSupport, com.ware.swift.core.remoting.IClusterSyncCallbackHoot)} 就够用了。 + *

+ * + * @param remotingDomain + * @return + */ + @Override + public InteractivePayload buildSyncInteractivePayload(RemotingDomainSupport remotingDomain) { + + return ClusterDataSyncManager.newSyncInteractivePayload(remotingDomain, new IClusterSyncCallbackHoot() { + @Override + public void callback(InteractivePayload.Builder target) { + /** + * 一般情况下,需要自定义自己的 head,可以在这里进行添加. + * head 提供统一类型的 int 类型而不是字符串,目的是从某种层度来讲统一使用一个 8 字节的类型来表示其具体的 head。 + * + * 而避免当使用字符串是使用参差不齐的长度带来一定内存上的碎片化。 + */ + target.putHeaders(1001, String.valueOf(System.currentTimeMillis())); + } + }); + } + + /** + * + */ + private class HotDataExpireKeyHandler implements IExpireKeyHandler { + + @Override + public void expire(String key, HotDataRemotingDomain value, AbstractConcurrentCache cache) { + value.setDomainOperator(RemotingDomainManager.DOMAIN_OPERATOR_DELETE); + HotDataClientRequestHandler hotDataClientRequestHandler = WareSwiftDeveloperManager.getClientPayloadHandler(); + hotDataClientRequestHandler.processSubscriberPush(value); + } + } + + /** + * 如果没有具体对应的数据,则返回 null . + * + * @param hotDataTopic + * @param key + * @return + */ + public HotDataRemotingDomain getHotDataRemotingDomain(String hotDataTopic, String key) { + + ConcurrentMap keyMap = hotDataRegistry.get(hotDataTopic); + if (keyMap == null) { + return null; + } + + return keyMap.get(key); + } +} \ No newline at end of file diff --git a/hot-data-on-swift/src/main/java/com/ware/swift/hot/data/HotDataClientRequestHandler.java b/hot-data-on-swift/src/main/java/com/ware/swift/hot/data/HotDataClientRequestHandler.java new file mode 100644 index 0000000..09fbf30 --- /dev/null +++ b/hot-data-on-swift/src/main/java/com/ware/swift/hot/data/HotDataClientRequestHandler.java @@ -0,0 +1,296 @@ +/* + * Copyright (C) 2019 the original author or authors. + * + * 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 com.ware.swift.hot.data; + +import com.google.protobuf.ByteString; +import com.ware.swift.core.WareSwiftDeveloperManager; +import com.ware.swift.core.remoting.DefaultClientInteractivePayloadHandler; +import com.ware.swift.core.remoting.IInteractive; +import com.ware.swift.core.remoting.OutboundCallback; +import com.ware.swift.core.remoting.RemotingDomainManager; +import com.ware.swift.event.ICallbackHook; +import com.ware.swift.proto.InteractivePayload; +import io.netty.util.CharsetUtil; +import io.netty.util.internal.StringUtil; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.logging.Logger; + +/** + * @author pbting + * @date 2019-05-19 10:49 AM + */ +public class HotDataClientRequestHandler extends DefaultClientInteractivePayloadHandler { + + private static final Logger log = Logger.getLogger(HotDataRemotingDomain.class.getCanonicalName()); + private static final Integer HOT_DATA_TOPIC_HEAD_KEY = 1000; + private static final Integer HOT_DATA_KEY_HEAD_KEY = 1001; + private final ConcurrentHashMap subscriberInteractiveRegistry = new ConcurrentHashMap<>(); + private HashMap sinkOperatorMethodMapping = new HashMap<>(); + + public static final String SINK_ADD_REMOTING_DOMAIN_OP = "add/remoting/domain.op"; + + public static final String SINK_SUBSCRIBE_REMOTING_DOMAIN = "subscribe/remoting/domain.op"; + + public static final String SINK_HOT_DATA_PUSH_OP = "hot/data/push.op"; + + /** + * + */ + @Override + public void onAfterConstructInstance() { + + Method[] methods = this.getClass().getDeclaredMethods(); + for (Method method : methods) { + if (!method.isAnnotationPresent(Sink.class)) { + continue; + } + try { + method.setAccessible(true); + Sink sink = method.getAnnotation(Sink.class); + String sinkName = sink.name(); + sinkOperatorMethodMapping.put(sinkName, method); + } catch (SecurityException e) { + e.printStackTrace(); + } + } + } + + /** + * @param sink + * @param interactive + */ + @Override + public void processCustomSink(String sink, IInteractive interactive) { + + Method sinkOperator = sinkOperatorMethodMapping.get(sink); + if (sinkOperator == null) { + return; + } + + try { + sinkOperator.invoke(this, interactive); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } + + + @Sink(name = SINK_ADD_REMOTING_DOMAIN_OP, desc = "添加一个热点数据 remoting domain") + public void processAddRemotingDomain(final IInteractive interactive) { + + byte[] remotingDomains = interactive.getInteractivePayload().getPayload().toByteArray(); + + final HotDataRemotingDomain hotDataRemotingDomain = WareSwiftDeveloperManager.deserialize(remotingDomains, HotDataRemotingDomain.class); + hotDataRemotingDomain.setDomainOperator(RemotingDomainManager.DOMAIN_OPERATOR_ADD); + /** + * 集群节点间同步 + */ + WareSwiftDeveloperManager.getCapabilityModel().onOutboundDataSet(hotDataRemotingDomain, new OutboundCallback() { + @Override + public void onSyncingSuccess(Set channelIdentifies) { + + interactive.sendPayload(WareSwiftDeveloperManager.buildSuccessInteractivePayload(channelIdentifies.toString(), "[ ADD ] hot data [ SUCCESS ]。")); + } + + @Override + public void onSyncingFailed(Set successChannelIdentifies, Set failedChannelIdentifies) { + + log.info("success channel identifies=" + successChannelIdentifies + "; failed =" + failedChannelIdentifies.toString()); + + InteractivePayload response = + WareSwiftDeveloperManager.buildResponseInteractivePayload(("success node size =" + successChannelIdentifies.size() + "; " + " failed is =" + failedChannelIdentifies.size()), "[ ADD ] hot data result", new ICallbackHook() { + @Override + public void callback(InteractivePayload.Builder target) { + target.putHeaders(WareSwiftDeveloperManager.RESPONSE_STATUS_CODE_HEAD_KEY, String.valueOf(WareSwiftDeveloperManager.EXCEPTION_STATUS_CODE)); + } + }); + interactive.sendPayload(response); + } + + @Override + public void onMinRequestRequiredAcks(int requestRequiredAcks, int realActiveChannels) { + + InteractivePayload response = + WareSwiftDeveloperManager.buildResponseInteractivePayload(("request require ack=" + realActiveChannels + "; " + " real is =" + realActiveChannels), "[ ADD ] hot data failed", new ICallbackHook() { + @Override + public void callback(InteractivePayload.Builder target) { + target.putHeaders(WareSwiftDeveloperManager.RESPONSE_STATUS_CODE_HEAD_KEY, String.valueOf(WareSwiftDeveloperManager.ATTENTION_ACK_STATUS_CODE)); + } + }); + interactive.sendPayload(response); + } + }); + } + + /** + * @param interactive + */ + @Sink(name = "update/remoting/domain.op") + public void processUpdateRemotingDomain(IInteractive interactive) { + Map headsMap = interactive.getInteractivePayload().getHeadersMap(); + String hotDataTopic = headsMap.get(HOT_DATA_TOPIC_HEAD_KEY); + String hotDataKey = headsMap.get(HOT_DATA_KEY_HEAD_KEY); + + HotDataAvailableCapabilityModel hotDataAvailableCapabilityModel = + (HotDataAvailableCapabilityModel) WareSwiftDeveloperManager.getCapabilityModel(); + + + if (hotDataAvailableCapabilityModel.getHotDataRemotingDomain(hotDataTopic, hotDataKey) == null) { + + InteractivePayload.Builder builder = InteractivePayload.newBuilder(); + builder.setPayload(ByteString.copyFrom((String.format("[ UPDATE ] No corresponding data for [%s] [%s]", hotDataTopic, hotDataKey).getBytes(CharsetUtil.UTF_8)))); + interactive.sendPayload(builder.build()); + return; + } + + // + String updateValue = interactive.getInteractivePayload().getPayload().toStringUtf8(); + final HotDataRemotingDomain hotDataRemotingDomain = + new HotDataRemotingDomain(RemotingDomainManager.DOMAIN_OPERATOR_UPDATE); + hotDataRemotingDomain.setTopic(hotDataTopic); + hotDataRemotingDomain.setKey(hotDataKey); + hotDataRemotingDomain.setValue(updateValue); + hotDataAvailableCapabilityModel.onOutboundDataSet(hotDataRemotingDomain, new OutboundCallback() { + @Override + public void onSyncingSuccess(Set channelIdentifies) { + + interactive.sendPayload(WareSwiftDeveloperManager.buildSuccessInteractivePayload(channelIdentifies.toString(), "[ UPDATE ] hot data [ SUCCESS ]。")); + } + + @Override + public void onSyncingFailed(Set successChannelIdentifies, Set failedChannelIdentifies) { + + log.info("success channel identifies=" + successChannelIdentifies + "; failed =" + failedChannelIdentifies.toString()); + + InteractivePayload response = + WareSwiftDeveloperManager.buildResponseInteractivePayload(("success node size =" + successChannelIdentifies.size() + "; " + " failed is =" + failedChannelIdentifies.size()), "[ UPDATE ] hot data result", new ICallbackHook() { + @Override + public void callback(InteractivePayload.Builder target) { + target.putHeaders(WareSwiftDeveloperManager.RESPONSE_STATUS_CODE_HEAD_KEY, String.valueOf(WareSwiftDeveloperManager.EXCEPTION_STATUS_CODE)); + } + }); + interactive.sendPayload(response); + } + + @Override + public void onMinRequestRequiredAcks(int requestRequiredAcks, int realActiveChannels) { + + InteractivePayload response = + WareSwiftDeveloperManager.buildResponseInteractivePayload(("request require ack=" + realActiveChannels + "; " + " real is =" + realActiveChannels), "[ UPDATE ] hot data failed", new ICallbackHook() { + @Override + public void callback(InteractivePayload.Builder target) { + target.putHeaders(WareSwiftDeveloperManager.RESPONSE_STATUS_CODE_HEAD_KEY, String.valueOf(WareSwiftDeveloperManager.ATTENTION_ACK_STATUS_CODE)); + } + }); + interactive.sendPayload(response); + } + }); + } + + /** + * @param interactive + */ + @Sink(name = "delete/remoting/domain.op") + public void deleteRemotingDomain(IInteractive interactive) { + HotDataAvailableCapabilityModel hotDataAvailableCapabilityModel = + (HotDataAvailableCapabilityModel) WareSwiftDeveloperManager.getCapabilityModel(); + + java.util.Map headersMap = interactive.getInteractivePayload().getHeadersMap(); + String hotDataTopic = headersMap.get(HOT_DATA_TOPIC_HEAD_KEY); + String hotDataKey = headersMap.get(HOT_DATA_KEY_HEAD_KEY); + final HotDataRemotingDomain hotDataRemotingDomain = hotDataAvailableCapabilityModel.getHotDataRemotingDomain(hotDataTopic, hotDataKey); + hotDataRemotingDomain.setDomainOperator(RemotingDomainManager.DOMAIN_OPERATOR_DELETE); + if (hotDataRemotingDomain == null) { + + InteractivePayload.Builder builder = InteractivePayload.newBuilder(); + builder.setPayload(ByteString.copyFrom((String.format("[ DELETE ] No corresponding data for [%s] [%s]", hotDataTopic, hotDataKey).getBytes(CharsetUtil.UTF_8)))); + interactive.sendPayload(builder.build()); + return; + } + + hotDataAvailableCapabilityModel.onOutboundDataSet(hotDataRemotingDomain, new OutboundCallback() { + @Override + public void onSyncingSuccess(Set channelIdentifies) { + + interactive.sendPayload(WareSwiftDeveloperManager.buildSuccessInteractivePayload(channelIdentifies.toString(), "[ DELETE ] hot data [ SUCCESS ]。")); + } + + @Override + public void onSyncingFailed(Set successChannelIdentifies, Set failedChannelIdentifies) { + + log.info("success channel identifies=" + successChannelIdentifies + "; failed =" + failedChannelIdentifies.toString()); + InteractivePayload response = + WareSwiftDeveloperManager.buildResponseInteractivePayload(("success node size =" + successChannelIdentifies.size() + "; " + " failed is =" + failedChannelIdentifies.size()), "[ DELETE ] hot data result", new ICallbackHook() { + @Override + public void callback(InteractivePayload.Builder target) { + target.putHeaders(WareSwiftDeveloperManager.RESPONSE_STATUS_CODE_HEAD_KEY, String.valueOf(WareSwiftDeveloperManager.EXCEPTION_STATUS_CODE)); + } + }); + interactive.sendPayload(response); + } + + @Override + public void onMinRequestRequiredAcks(int requestRequiredAcks, int realActiveChannels) { + + InteractivePayload response = + WareSwiftDeveloperManager.buildResponseInteractivePayload(("request require ack=" + realActiveChannels + "; " + " real is =" + realActiveChannels), "[ DELETE ] hot data failed", new ICallbackHook() { + @Override + public void callback(InteractivePayload.Builder target) { + target.putHeaders(WareSwiftDeveloperManager.RESPONSE_STATUS_CODE_HEAD_KEY, String.valueOf(WareSwiftDeveloperManager.ATTENTION_ACK_STATUS_CODE)); + } + }); + interactive.sendPayload(response); + } + }); + + } + + /** + * @param interactive + */ + @Sink(name = SINK_SUBSCRIBE_REMOTING_DOMAIN) + public void subscribeRemotingDomain(IInteractive interactive) { + + String hotDataTopic = interactive.getInteractivePayload().getPayload().toStringUtf8(); + if (StringUtil.isNullOrEmpty(hotDataTopic)) { + Map headsMap = interactive.getInteractivePayload().getHeadersMap(); + hotDataTopic = headsMap.get(HOT_DATA_TOPIC_HEAD_KEY); + } + subscriberInteractiveRegistry.put(hotDataTopic, interactive); + interactive.sendPayload(InteractivePayload.newBuilder().setSink(SINK_SUBSCRIBE_REMOTING_DOMAIN).setPayload(ByteString.copyFrom("subscriber success!".getBytes(CharsetUtil.UTF_8))).build()); + log.info("add new hot data topic=" + hotDataTopic + "; subscribe size =" + subscriberInteractiveRegistry.size()); + } + + public void processSubscriberPush(final HotDataRemotingDomain hotDataRemotingDomain) { + IInteractive subInteractive = subscriberInteractiveRegistry.get(hotDataRemotingDomain.getTopic()); + if (subInteractive == null) { + return; + } + + InteractivePayload.Builder builder = InteractivePayload.newBuilder(); + builder.setPayload(ByteString.copyFrom(WareSwiftDeveloperManager.serialize(hotDataRemotingDomain))); + builder.setSink(SINK_HOT_DATA_PUSH_OP); + subInteractive.sendPayload(builder.build()); + } +} \ No newline at end of file diff --git a/hot-data-on-swift/src/main/java/com/ware/swift/hot/data/HotDataRemotingDomain.java b/hot-data-on-swift/src/main/java/com/ware/swift/hot/data/HotDataRemotingDomain.java new file mode 100644 index 0000000..7ac926d --- /dev/null +++ b/hot-data-on-swift/src/main/java/com/ware/swift/hot/data/HotDataRemotingDomain.java @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2019 the original author or authors. + * + * 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 com.ware.swift.hot.data; + +import com.ware.swift.core.remoting.RemotingDomainSupport; + +/** + * @author pbting + * @date 2019-05-19 10:57 AM + */ +public class HotDataRemotingDomain extends RemotingDomainSupport { + + private String topic; + private String key; + private String value; + private long reVersion; + + + public HotDataRemotingDomain(byte domainOperator) { + setDomainOperator(domainOperator); + } + + public HotDataRemotingDomain(String topic, String key, String value) { + this.topic = topic; + this.key = key; + this.value = value; + + } + + public String getTopic() { + return topic; + } + + public void setTopic(String topic) { + this.topic = topic; + } + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + public long getReVersion() { + return reVersion; + } + + public void setReVersion(long reVersion) { + this.reVersion = reVersion; + } + + @Override + public String toString() { + return "HotDataRemotingDomain{" + + "topic='" + topic + '\'' + + ", key='" + key + '\'' + + ", value='" + value + '\'' + + ", reVersion=" + reVersion + + '}'; + } +} \ No newline at end of file diff --git a/hot-data-on-swift/src/main/java/com/ware/swift/hot/data/Sink.java b/hot-data-on-swift/src/main/java/com/ware/swift/hot/data/Sink.java new file mode 100644 index 0000000..a4825e4 --- /dev/null +++ b/hot-data-on-swift/src/main/java/com/ware/swift/hot/data/Sink.java @@ -0,0 +1,19 @@ +package com.ware.swift.hot.data; + +import java.lang.annotation.*; + +@Target({ElementType.METHOD, ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface Sink { + + /** + * @return + */ + String name() default ""; + + /** + * @return + */ + String desc() default ""; +} diff --git a/hot-data-on-swift/src/main/resources/META-INF/swift-plugins/com.ware.swift.core.remoting.ICapabilityModel b/hot-data-on-swift/src/main/resources/META-INF/swift-plugins/com.ware.swift.core.remoting.ICapabilityModel new file mode 100644 index 0000000..899e5f4 --- /dev/null +++ b/hot-data-on-swift/src/main/resources/META-INF/swift-plugins/com.ware.swift.core.remoting.ICapabilityModel @@ -0,0 +1 @@ +com.ware.swift.hot.data.HotDataAvailableCapabilityModel \ No newline at end of file diff --git a/hot-data-on-swift/src/main/resources/META-INF/swift-plugins/com.ware.swift.core.remoting.IClientInteractivePayloadHandler b/hot-data-on-swift/src/main/resources/META-INF/swift-plugins/com.ware.swift.core.remoting.IClientInteractivePayloadHandler new file mode 100644 index 0000000..1c70298 --- /dev/null +++ b/hot-data-on-swift/src/main/resources/META-INF/swift-plugins/com.ware.swift.core.remoting.IClientInteractivePayloadHandler @@ -0,0 +1 @@ +com.ware.swift.hot.data.HotDataClientRequestHandler \ No newline at end of file diff --git a/hot-data-on-swift/src/main/resources/META-INF/swift-plugins/com.ware.swift.core.remoting.IRemotingManager b/hot-data-on-swift/src/main/resources/META-INF/swift-plugins/com.ware.swift.core.remoting.IRemotingManager new file mode 100644 index 0000000..7aeebc8 --- /dev/null +++ b/hot-data-on-swift/src/main/resources/META-INF/swift-plugins/com.ware.swift.core.remoting.IRemotingManager @@ -0,0 +1 @@ +com.ware.swift.rsocket.LeaderFollowerRSocketRemotingManager \ No newline at end of file diff --git a/hot-data-on-swift/src/main/resources/ware-swift.properties b/hot-data-on-swift/src/main/resources/ware-swift.properties new file mode 100644 index 0000000..614dafe --- /dev/null +++ b/hot-data-on-swift/src/main/resources/ware-swift.properties @@ -0,0 +1,7 @@ +#cluster.nodes=127.0.0.1:19091 +cluster.nodes=127.0.0.1:19091;127.0.0.1:19092;127.0.0.1:19093 +bind.address=127.0.0.1 +bind.port=19093 +cluster.name=default +send.heartbeat.interval=1000 +ware.swift.debug=true \ No newline at end of file diff --git a/hot-data-on-swift/src/test/java/com/ware/swift/hot/data/tests/HotDataPublisher.java b/hot-data-on-swift/src/test/java/com/ware/swift/hot/data/tests/HotDataPublisher.java new file mode 100644 index 0000000..43f2284 --- /dev/null +++ b/hot-data-on-swift/src/test/java/com/ware/swift/hot/data/tests/HotDataPublisher.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2019 the original author or authors. + * + * 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 com.ware.swift.hot.data.tests; + +import com.ware.swift.core.WareSwiftDeveloperManager; +import com.ware.swift.core.remoting.channel.AbstractRemotingChannel; +import com.ware.swift.event.ICallbackHook; +import com.ware.swift.hot.data.HotDataRemotingDomain; +import com.ware.swift.proto.InteractivePayload; +import com.ware.swift.rsocket.RSockeetRemotingChannelBuilder; +import org.junit.Test; + +import java.util.Random; +import java.util.concurrent.TimeUnit; + +/** + * @author pbting + * @date 2019-05-21 10:38 PM + */ +public class HotDataPublisher { + + @Test + public void publisherHotData() throws Exception { + + final AbstractRemotingChannel remotingChannel = + RSockeetRemotingChannelBuilder.newBuilder() + .setAddressPort("127.0.0.1:19091") + .setClusterName("default").build(); + + while (true) { + HotDataRemotingDomain hotDataRemotingDomain = new HotDataRemotingDomain("service.registry", "consumer.order.services", "com.ware.swift.order.service.BuyGoods"); + hotDataRemotingDomain.setReVersion(Math.abs(new Random().nextLong())); + InteractivePayload interactivePayload = WareSwiftDeveloperManager.buildInteractivePayload("add/remoting/domain.op", hotDataRemotingDomain, new ICallbackHook() { + @Override + public void callback(InteractivePayload.Builder target) { + target.setSource(remotingChannel.identify()); + } + }); + InteractivePayload response = remotingChannel.requestResponse(interactivePayload); + System.err.println(response.getPayload().toStringUtf8()); + + TimeUnit.SECONDS.sleep(1); + } + } + +} \ No newline at end of file diff --git a/hot-data-on-swift/src/test/java/com/ware/swift/hot/data/tests/HotDataSubscriber.java b/hot-data-on-swift/src/test/java/com/ware/swift/hot/data/tests/HotDataSubscriber.java new file mode 100644 index 0000000..eee0ac8 --- /dev/null +++ b/hot-data-on-swift/src/test/java/com/ware/swift/hot/data/tests/HotDataSubscriber.java @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2019 the original author or authors. + * + * 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 com.ware.swift.hot.data.tests; + +import com.google.protobuf.ByteString; +import com.ware.swift.core.remoting.IRemotingCallStreamObserver; +import com.ware.swift.core.remoting.RemotingInteractiveConstants; +import com.ware.swift.core.remoting.channel.AbstractRemotingChannel; +import com.ware.swift.hot.data.HotDataClientRequestHandler; +import com.ware.swift.hot.data.HotDataRemotingDomain; +import com.ware.swift.proto.InteractivePayload; +import com.ware.swift.rsocket.RSockeetRemotingChannelBuilder; +import io.netty.util.CharsetUtil; +import org.junit.Test; + +/** + * @author pbting + * @date 2019-05-21 11:10 PM + */ +public class HotDataSubscriber { + + @Test + public void subscriber() throws Exception { + final AbstractRemotingChannel remotingChannel = + RSockeetRemotingChannelBuilder.newBuilder() + .setAddressPort("127.0.0.1:19091") + .setClusterName("default").build(); + + InteractivePayload.Builder builder = InteractivePayload.newBuilder(); + builder.setSink(HotDataClientRequestHandler.SINK_SUBSCRIBE_REMOTING_DOMAIN); + builder.setPayload(ByteString.copyFrom("service.registry".getBytes(CharsetUtil.UTF_8))); + builder.setSource(remotingChannel.identify()); + // 使用服务端推送。 + remotingChannel.requestStream(builder.build()).registryCallback(new IRemotingCallStreamObserver() { + @Override + public void onNext(InteractivePayload interactivePayload) { + String sink = interactivePayload.getSink(); + if (HotDataClientRequestHandler.SINK_SUBSCRIBE_REMOTING_DOMAIN.equals(sink)) { + + System.out.println("\t subscribe success."); + } else if (HotDataClientRequestHandler.SINK_HOT_DATA_PUSH_OP.equals(sink)) { + HotDataRemotingDomain remotingDomain = + (HotDataRemotingDomain) RemotingInteractiveConstants.OBJECT_ENCODING_HANDLER.decodeResult(interactivePayload.getPayload().toByteArray(), HotDataRemotingDomain.class); + System.err.println("\t\t stream push: " + remotingDomain.toString()); + } + } + }); + System.in.read(); + } +} \ No newline at end of file diff --git a/rsocket-swift/src/main/java/com/ware/swift/rsocket/DecentrationRSocketRemotingManager.java b/rsocket-swift/src/main/java/com/ware/swift/rsocket/DecentrationRSocketRemotingManager.java index 377260f..1b8b54c 100644 --- a/rsocket-swift/src/main/java/com/ware/swift/rsocket/DecentrationRSocketRemotingManager.java +++ b/rsocket-swift/src/main/java/com/ware/swift/rsocket/DecentrationRSocketRemotingManager.java @@ -2,11 +2,11 @@ import com.ware.swift.core.NodeState; import com.ware.swift.core.remoting.HeartbeatMultiMailbox; -import com.ware.swift.core.remoting.IDecentrationRemotingManager; +import com.ware.swift.core.remoting.IDecentralizeRemotingManager; import com.ware.swift.core.remoting.IMailbox; import com.ware.swift.core.remoting.RemotingInteractiveConstants; -import com.ware.swift.core.remoting.event.local.DecentrationInitRemotingChannelEventListener; -import com.ware.swift.core.remoting.event.local.DecentrationNodeMeetEventListener; +import com.ware.swift.core.remoting.event.local.DecentralizeInitRemotingChannelEventListener; +import com.ware.swift.core.remoting.event.local.DecentralizeNodeMeetEventListener; import com.ware.swift.core.remoting.event.local.InitRemotingChannelEventListener; import com.ware.swift.proto.InteractivePayload; @@ -16,7 +16,7 @@ * 提供去中心化能力的 rsocket remoting manager */ public class DecentrationRSocketRemotingManager extends RSocketRemotingManagerSupport - implements IDecentrationRemotingManager { + implements IDecentralizeRemotingManager { private IMailbox mailbox = new HeartbeatMultiMailbox( this.getClass().getName()); @@ -43,14 +43,14 @@ public void initEventListener() { @Override public void initNodeMeetEventListener() { - remotingEventLoopGroup.addLast(new DecentrationNodeMeetEventListener(this), + remotingEventLoopGroup.addLast(new DecentralizeNodeMeetEventListener(this), NODE_MEET_EVENT_TYPE); } @Override public InitRemotingChannelEventListener initRemotingChannelEventListener() { - return new DecentrationInitRemotingChannelEventListener(this); + return new DecentralizeInitRemotingChannelEventListener(this); } /** @@ -83,7 +83,7 @@ public void setServer(T server) { @Override public String getChannelIdentify(String key) { - return RemotingInteractiveConstants.PTP_ROLE_PRIFIX + "@" + key; + return RemotingInteractiveConstants.PTP_ROLE_PREFIX + "@" + key; } @Override @@ -99,7 +99,7 @@ public IMailbox getMailbox() { @Override public void producer(InteractivePayload value) { - DecentrationRemotingManager.DEFAULT_IMPL.producer(value, nodesHeartbeatMailboxRegistry); + DecentralizeRemotingManager.DEFAULT_IMPL.producer(value, nodesHeartbeatMailboxRegistry); } @Override @@ -111,7 +111,7 @@ public void addHeartbeatTimeoutCheckMailbox(IMailbox mailbox @Override public void processClusterNodes(String nodes) { - DecentrationRemotingManager.DEFAULT_IMPL.processClusterNodes(this, nodes); + DecentralizeRemotingManager.DEFAULT_IMPL.processClusterNodes(this, nodes); } } diff --git a/rsocket-swift/src/main/java/com/ware/swift/rsocket/LeaderFollowerRSocketRemotingManager.java b/rsocket-swift/src/main/java/com/ware/swift/rsocket/LeaderFollowerRSocketRemotingManager.java index b8a3db7..0d1ae8f 100644 --- a/rsocket-swift/src/main/java/com/ware/swift/rsocket/LeaderFollowerRSocketRemotingManager.java +++ b/rsocket-swift/src/main/java/com/ware/swift/rsocket/LeaderFollowerRSocketRemotingManager.java @@ -16,7 +16,7 @@ public class LeaderFollowerRSocketRemotingManager extends RSocketRemotingManagerSupport implements ILeaderFollowerRemotingManager { - private IMailbox signelMailbox = new HeartbeatSingleMailbox( + private IMailbox singleMailbox = new HeartbeatSingleMailbox( this.getClass().getName()); public LeaderFollowerRSocketRemotingManager() { @@ -68,12 +68,12 @@ public void initSendHeartbeatEventListener() { @Override public String getChannelIdentify(String key) { - return RemotingInteractiveConstants.RAFT_ROLE_PRIFIX + "@" + key; + return RemotingInteractiveConstants.RAFT_ROLE_PREFIX + "@" + key; } @Override public IMailbox getMailbox() { - return signelMailbox; + return singleMailbox; } @Override diff --git a/rsocket-swift/src/main/java/com/ware/swift/rsocket/RSockeetRemotingChannelBuilder.java b/rsocket-swift/src/main/java/com/ware/swift/rsocket/RSockeetRemotingChannelBuilder.java new file mode 100644 index 0000000..407dbb9 --- /dev/null +++ b/rsocket-swift/src/main/java/com/ware/swift/rsocket/RSockeetRemotingChannelBuilder.java @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2019 the original author or authors. + * + * 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 com.ware.swift.rsocket; + +import com.ware.swift.core.remoting.channel.AbstractRemotingChannel; + +/** + * @author pbting + * @date 2019-05-21 10:43 PM + */ +public final class RSockeetRemotingChannelBuilder { + + private String addressPort; + private String cluterName = "default"; + + public static RSockeetRemotingChannelBuilder newBuilder() { + return new RSockeetRemotingChannelBuilder(); + } + + public RSockeetRemotingChannelBuilder setAddressPort(String addressPort) { + + this.addressPort = addressPort; + return this; + } + + public RSockeetRemotingChannelBuilder setClusterName(String clusterName) { + this.cluterName = clusterName; + return this; + } + + public AbstractRemotingChannel build() { + String[] addressPortArr = addressPort.split("[:]"); + + RSocketClientBootstrap clientBootstrap = new RSocketClientBootstrap( + addressPortArr[0], Integer.valueOf(addressPortArr[1])); + + AbstractRemotingChannel remotingChannel = new RSocketRemotingChannel( + addressPort, clientBootstrap); + remotingChannel.setIdentify("CLIENT" + addressPort + "@" + this.cluterName); + return remotingChannel; + } +} \ No newline at end of file diff --git a/rsocket-swift/src/main/java/com/ware/swift/rsocket/RSocketInteractiveImpl.java b/rsocket-swift/src/main/java/com/ware/swift/rsocket/RSocketInteractiveImpl.java index 5b76545..d27d074 100644 --- a/rsocket-swift/src/main/java/com/ware/swift/rsocket/RSocketInteractiveImpl.java +++ b/rsocket-swift/src/main/java/com/ware/swift/rsocket/RSocketInteractiveImpl.java @@ -12,7 +12,7 @@ public class RSocketInteractiveImpl implements IInteractive { private InteractivePayload interactivePayload; private ReplayProcessor replayProcessor; - private volatile boolean isDroppted = false; + private volatile boolean isDropted = false; public RSocketInteractiveImpl(InteractivePayload interactivePayload, ReplayProcessor replayProcessor) { @@ -32,6 +32,6 @@ public InteractivePayload getInteractivePayload() { @Override public boolean sendPayload(InteractivePayload interactivePayload) { replayProcessor.onNext(interactivePayload.toByteArray()); - return !isDroppted; + return !isDropted; } } diff --git a/rsocket-swift/src/main/java/com/ware/swift/rsocket/RSocketRemotingChannel.java b/rsocket-swift/src/main/java/com/ware/swift/rsocket/RSocketRemotingChannel.java index 9242ec6..8030272 100644 --- a/rsocket-swift/src/main/java/com/ware/swift/rsocket/RSocketRemotingChannel.java +++ b/rsocket-swift/src/main/java/com/ware/swift/rsocket/RSocketRemotingChannel.java @@ -82,8 +82,6 @@ public IRequestStreamCallbackRegistrator requestStream(InteractivePayload inBoun byte[] data = byteBuffer.array(); return data; }).doOnNext(responseData -> { - System.err.println("\treceive sync data size[request/stream]:" - + syncDataSize.incrementAndGet()); if (callStreamObserver == null) { log.warn(IRemotingCallStreamObserver.class.getName() + " is not set."); diff --git a/rsocket-swift/src/main/java/com/ware/swift/rsocket/RSocketRemotingChannelFactory.java b/rsocket-swift/src/main/java/com/ware/swift/rsocket/RSocketRemotingChannelFactory.java index d5b6ede..aa916bd 100644 --- a/rsocket-swift/src/main/java/com/ware/swift/rsocket/RSocketRemotingChannelFactory.java +++ b/rsocket-swift/src/main/java/com/ware/swift/rsocket/RSocketRemotingChannelFactory.java @@ -1,56 +1,56 @@ package com.ware.swift.rsocket; -import com.ware.swift.rsocket.handler.RSocketRequestHandlerSupport; import com.ware.swift.core.remoting.IRemotingManager; import com.ware.swift.core.remoting.channel.AbstractRemotingChannel; import com.ware.swift.core.remoting.channel.IRemotingChannelFactory; +import com.ware.swift.rsocket.handler.RSocketRequestHandlerSupport; import java.util.concurrent.TimeUnit; +import java.util.logging.Logger; /** * 这里初始化于 Server 端建立连接 */ public class RSocketRemotingChannelFactory implements IRemotingChannelFactory { - private IRemotingManager remotingManager; - - public RSocketRemotingChannelFactory(IRemotingManager remotingManager) { - this.remotingManager = remotingManager; - } - - @Override - public AbstractRemotingChannel newRemotingChannel(String addressPort, - String clusterName) { - String[] addressPortArr = addressPort.split("[:]"); - - RSocketClientBootstrap clientBootstrap = new RSocketClientBootstrap( - addressPortArr[0], Integer.valueOf(addressPortArr[1])); - - clientBootstrap - .setSocketRequestHandlerFactory(new IRSocketRequestHandlerFactory() { - @Override - public RSocketRequestHandlerSupport createWithClient() { - return new RSocketClientSideInboundHandler( - RSocketRequestHandlerSupport.RSocketRequestHandlerRole.CLIENT); - } - }); - while (true) { - try { - AbstractRemotingChannel remotingChannel = new RSocketRemotingChannel( - addressPort, clientBootstrap); - remotingChannel.setIdentify(remotingManager - .getChannelIdentify(addressPort + "@" + clusterName)); - return remotingChannel; - } - catch (Exception e) { - System.err.println(addressPort + " is unavliable..."); - try { - TimeUnit.SECONDS.sleep(1); - } - catch (InterruptedException e1) { - } - } - } - } + private static final Logger logger = Logger.getLogger(RSocketRemotingChannelFactory.class.getCanonicalName()); + private IRemotingManager remotingManager; + + public RSocketRemotingChannelFactory(IRemotingManager remotingManager) { + this.remotingManager = remotingManager; + } + + @Override + public AbstractRemotingChannel newRemotingChannel(String addressPort, + String clusterName) { + String[] addressPortArr = addressPort.split("[:]"); + + RSocketClientBootstrap clientBootstrap = new RSocketClientBootstrap( + addressPortArr[0], Integer.valueOf(addressPortArr[1])); + + clientBootstrap + .setSocketRequestHandlerFactory(new IRSocketRequestHandlerFactory() { + @Override + public RSocketRequestHandlerSupport createWithClient() { + return new RSocketClientSideInboundHandler( + RSocketRequestHandlerSupport.RSocketRequestHandlerRole.CLIENT); + } + }); + while (true) { + try { + AbstractRemotingChannel remotingChannel = new RSocketRemotingChannel( + addressPort, clientBootstrap); + remotingChannel.setIdentify(remotingManager + .getChannelIdentify(addressPort + "@" + clusterName)); + return remotingChannel; + } catch (Exception e) { + logger.warning(addressPort + " is Unavailable..."); + try { + TimeUnit.SECONDS.sleep(1); + } catch (InterruptedException e1) { + } + } + } + } } diff --git a/rsocket-swift/src/main/java/com/ware/swift/rsocket/RSocketRemotingManagerSupport.java b/rsocket-swift/src/main/java/com/ware/swift/rsocket/RSocketRemotingManagerSupport.java index 1143a39..ba501c9 100644 --- a/rsocket-swift/src/main/java/com/ware/swift/rsocket/RSocketRemotingManagerSupport.java +++ b/rsocket-swift/src/main/java/com/ware/swift/rsocket/RSocketRemotingManagerSupport.java @@ -1,49 +1,49 @@ package com.ware.swift.rsocket; -import reactor.core.publisher.ReplayProcessor; - import com.ware.swift.core.remoting.AbstractRemotingManager; import com.ware.swift.core.remoting.channel.IRemotingChannelFactory; import com.ware.swift.core.remoting.event.local.StartupServerEventListener; +import reactor.core.publisher.ReplayProcessor; + import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /** - * + * */ public abstract class RSocketRemotingManagerSupport extends AbstractRemotingManager { - private IRemotingChannelFactory remotingChannelFactory = new RSocketRemotingChannelFactory( - this); + private IRemotingChannelFactory remotingChannelFactory = new RSocketRemotingChannelFactory( + this); - private RSocketServerBootstrap serverBootstrap; + private RSocketServerBootstrap serverBootstrap; - private Map watchNotification = new ConcurrentHashMap<>(); + private Map watchNotification = new ConcurrentHashMap<>(); - @Override - public IRemotingChannelFactory getRemotingChannelFactory() { + @Override + public IRemotingChannelFactory getRemotingChannelFactory() { - return remotingChannelFactory; - } + return remotingChannelFactory; + } - @Override - public void setServer(T server) { - this.serverBootstrap = (RSocketServerBootstrap) server; - } + @Override + public void setServer(T server) { + this.serverBootstrap = (RSocketServerBootstrap) server; + } - @Override - public StartupServerEventListener initStartupServerEventListener() { - return new RSocketStartupServerEventListener(this); - } + @Override + public StartupServerEventListener initStartupServerEventListener() { + return new RSocketStartupServerEventListener(this); + } - @Override - public void putIfAbsentStreamReplayProcessor( - String streamTopicIdentify, ReplayProcessor streamReplayProcessor) { - if (watchNotification.containsKey(streamTopicIdentify)) { - return; - } + @Override + public void putIfAbsentStreamReplayProcessor( + String streamTopicIdentify, ReplayProcessor streamReplayProcessor) { + if (watchNotification.containsKey(streamTopicIdentify)) { + return; + } - watchNotification.put(streamTopicIdentify, - (reactor.core.publisher.ReplayProcessor) streamReplayProcessor); - } + watchNotification.put(streamTopicIdentify, + (reactor.core.publisher.ReplayProcessor) streamReplayProcessor); + } } diff --git a/rsocket-swift/src/main/java/com/ware/swift/rsocket/RSocketServerSideInboundHandler.java b/rsocket-swift/src/main/java/com/ware/swift/rsocket/RSocketServerSideInboundHandler.java index b32dcc5..8c76c81 100644 --- a/rsocket-swift/src/main/java/com/ware/swift/rsocket/RSocketServerSideInboundHandler.java +++ b/rsocket-swift/src/main/java/com/ware/swift/rsocket/RSocketServerSideInboundHandler.java @@ -1,11 +1,10 @@ package com.ware.swift.rsocket; +import com.google.protobuf.InvalidProtocolBufferException; import com.ware.swift.core.remoting.IInteractive; -import com.ware.swift.core.remoting.ClusterDataSyncManager; import com.ware.swift.core.remoting.event.remoting.RemotingEventDispatcher; -import com.ware.swift.rsocket.handler.RSocketRequestHandlerSupport; import com.ware.swift.proto.InteractivePayload; -import com.google.protobuf.InvalidProtocolBufferException; +import com.ware.swift.rsocket.handler.RSocketRequestHandlerSupport; import io.rsocket.ConnectionSetupPayload; import io.rsocket.Payload; import io.rsocket.RSocket; @@ -86,11 +85,6 @@ public Flux requestStream(Payload payload) { } } - private String getStreamTopic(String source, String tmpTopic) { - - return source + ClusterDataSyncManager.STREAM_TOPIC_SEPARATOR + tmpTopic; - } - @Override public Mono fireAndForget(Payload payload) { return super.fireAndForget(payload); diff --git a/rsocket-swift/src/main/java/com/ware/swift/rsocket/RSocketStartupServerEventListener.java b/rsocket-swift/src/main/java/com/ware/swift/rsocket/RSocketStartupServerEventListener.java index 958b01c..fc201e8 100644 --- a/rsocket-swift/src/main/java/com/ware/swift/rsocket/RSocketStartupServerEventListener.java +++ b/rsocket-swift/src/main/java/com/ware/swift/rsocket/RSocketStartupServerEventListener.java @@ -18,12 +18,12 @@ public RSocketStartupServerEventListener(IRemotingManager remotingManager) { } @Override - public void startupServer(WareCoreSwiftConfig raftConfig) { + public void startupServer(WareCoreSwiftConfig wareSwiftConfig) { RSocketServerBootstrap serverBootstrap = new RSocketServerBootstrap( - raftConfig.getNodeInformation().getBindAddress(), - Integer.valueOf(raftConfig.getNodeInformation().getBindPort())); - remotingManager.setRaftConfig(raftConfig); + wareSwiftConfig.getNodeInformation().getBindAddress(), + Integer.valueOf(wareSwiftConfig.getNodeInformation().getBindPort())); + remotingManager.setRaftConfig(wareSwiftConfig); remotingManager.setServer(serverBootstrap); serverBootstrap .setSocketRequestHandlerFactory(new IRSocketRequestHandlerFactory() { diff --git a/rsocket-swift/src/main/java/com/ware/swift/rsocket/SocketAcceptSupport.java b/rsocket-swift/src/main/java/com/ware/swift/rsocket/SocketAcceptSupport.java index 9f80de6..2db118e 100644 --- a/rsocket-swift/src/main/java/com/ware/swift/rsocket/SocketAcceptSupport.java +++ b/rsocket-swift/src/main/java/com/ware/swift/rsocket/SocketAcceptSupport.java @@ -7,7 +7,6 @@ import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -import java.util.ArrayList; import java.util.List; /** @@ -15,34 +14,34 @@ */ public class SocketAcceptSupport implements SocketAcceptor { - private List acceptorFilters = new ArrayList<>(); - private IRSocketRequestHandlerFactory rSocketRequestHandlerFactory; - - public SocketAcceptSupport(List acceptorFilters) { - this(acceptorFilters, - new IRSocketRequestHandlerFactory.DefaultRSocketRequestHandlerFactory()); - } - - public SocketAcceptSupport(List acceptorFilters, - IRSocketRequestHandlerFactory rSocketRequestHandlerFactory) { - this.acceptorFilters = acceptorFilters; - this.rSocketRequestHandlerFactory = rSocketRequestHandlerFactory; - } - - @Override - public Mono accept(final ConnectionSetupPayload setup, - final RSocket sendingSocket) { - // if Successful authentication - return Flux.fromIterable(acceptorFilters) - .all(acceptorFilters -> acceptorFilters.accept(setup, sendingSocket)) - .defaultIfEmpty(true).map(filterResult -> { - if (!filterResult) { - return null; - } - RSocketRequestHandlerSupport rSocketRequestHandler = rSocketRequestHandlerFactory - .createWithServer(setup, sendingSocket); - return rSocketRequestHandler; - }); - } + private List acceptorFilters; + private IRSocketRequestHandlerFactory rSocketRequestHandlerFactory; + + public SocketAcceptSupport(List acceptorFilters) { + this(acceptorFilters, + new IRSocketRequestHandlerFactory.DefaultRSocketRequestHandlerFactory()); + } + + public SocketAcceptSupport(List acceptorFilters, + IRSocketRequestHandlerFactory rSocketRequestHandlerFactory) { + this.acceptorFilters = acceptorFilters; + this.rSocketRequestHandlerFactory = rSocketRequestHandlerFactory; + } + + @Override + public Mono accept(final ConnectionSetupPayload setup, + final RSocket sendingSocket) { + // if Successful authentication + return Flux.fromIterable(acceptorFilters) + .all(acceptorFilters -> acceptorFilters.accept(setup, sendingSocket)) + .defaultIfEmpty(true).map(filterResult -> { + if (!filterResult) { + return null; + } + RSocketRequestHandlerSupport rSocketRequestHandler = rSocketRequestHandlerFactory + .createWithServer(setup, sendingSocket); + return rSocketRequestHandler; + }); + } } \ No newline at end of file