Further simplified the store & connection manager relationship.
diff --git a/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/ClusterCommunicationAdminService.java b/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/ClusterCommunicationAdminService.java
new file mode 100644
index 0000000..76e4167
--- /dev/null
+++ b/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/ClusterCommunicationAdminService.java
@@ -0,0 +1,32 @@
+package org.onlab.onos.store.cluster.impl;
+
+import org.onlab.onos.cluster.DefaultControllerNode;
+
+/**
+ * Service for administering communications manager.
+ */
+public interface ClusterCommunicationAdminService {
+
+ /**
+ * Adds the node to the list of monitored nodes.
+ *
+ * @param node node to be added
+ */
+ void addNode(DefaultControllerNode node);
+
+ /**
+ * Removes the node from the list of monitored nodes.
+ *
+ * @param node node to be removed
+ */
+ void removeNode(DefaultControllerNode node);
+
+ /**
+ * Starts-up the communications engine.
+ *
+ * @param localNode local controller node
+ * @param delegate nodes delegate
+ */
+ void startUp(DefaultControllerNode localNode, ClusterNodesDelegate delegate);
+
+}
diff --git a/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/ClusterCommunicationManager.java b/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/ClusterCommunicationManager.java
new file mode 100644
index 0000000..492afbc
--- /dev/null
+++ b/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/ClusterCommunicationManager.java
@@ -0,0 +1,308 @@
+package org.onlab.onos.store.cluster.impl;
+
+import com.google.common.collect.HashMultimap;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Multimap;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.apache.felix.scr.annotations.Service;
+import org.onlab.onos.cluster.DefaultControllerNode;
+import org.onlab.onos.cluster.NodeId;
+import org.onlab.onos.store.cluster.messaging.ClusterCommunicationService;
+import org.onlab.onos.store.cluster.messaging.ClusterMessage;
+import org.onlab.onos.store.cluster.messaging.ClusterMessageStream;
+import org.onlab.onos.store.cluster.messaging.HelloMessage;
+import org.onlab.onos.store.cluster.messaging.MessageSubject;
+import org.onlab.onos.store.cluster.messaging.MessageSubscriber;
+import org.onlab.onos.store.cluster.messaging.SerializationService;
+import org.onlab.packet.IpPrefix;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.net.SocketAddress;
+import java.nio.channels.SocketChannel;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.Timer;
+import java.util.TimerTask;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+import static java.net.InetAddress.getByAddress;
+import static org.onlab.util.Tools.namedThreads;
+
+/**
+ * Implements the cluster communication services to use by other stores.
+ */
+@Component(immediate = true)
+@Service
+public class ClusterCommunicationManager
+ implements ClusterCommunicationService, ClusterCommunicationAdminService {
+
+ private final Logger log = LoggerFactory.getLogger(getClass());
+
+ private static final long CONNECTION_CUSTODIAN_DELAY = 1000L;
+ private static final long CONNECTION_CUSTODIAN_FREQUENCY = 5000;
+
+ private static final long START_TIMEOUT = 1000;
+ private static final int WORKERS = 3;
+
+ private ClusterConnectionListener connectionListener;
+ private List<ClusterIOWorker> workers = new ArrayList<>(WORKERS);
+
+ private DefaultControllerNode localNode;
+ private ClusterNodesDelegate nodesDelegate;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected SerializationService serializationService;
+
+ // Nodes to be monitored to make sure they have a connection.
+ private final Set<DefaultControllerNode> nodes = new HashSet<>();
+
+ // Means to track message streams to other nodes.
+ private final Map<NodeId, ClusterMessageStream> streams = new ConcurrentHashMap<>();
+
+ // TODO: use something different that won't require synchronization
+ private Multimap<MessageSubject, MessageSubscriber> subscribers = HashMultimap.create();
+
+ // Executor pools for listening and managing connections to other nodes.
+ private final ExecutorService listenExecutor =
+ Executors.newSingleThreadExecutor(namedThreads("onos-comm-listen"));
+ private final ExecutorService commExecutors =
+ Executors.newFixedThreadPool(WORKERS, namedThreads("onos-comm-cluster"));
+ private final ExecutorService heartbeatExecutor =
+ Executors.newSingleThreadExecutor(namedThreads("onos-comm-heartbeat"));
+
+ private final Timer timer = new Timer("onos-comm-initiator");
+ private final TimerTask connectionCustodian = new ConnectionCustodian();
+
+ @Activate
+ public void activate() {
+ log.info("Activated but waiting for delegate");
+ }
+
+ @Deactivate
+ public void deactivate() {
+ connectionCustodian.cancel();
+ if (connectionListener != null) {
+ connectionListener.shutdown();
+ for (ClusterIOWorker worker : workers) {
+ worker.shutdown();
+ }
+ }
+ log.info("Stopped");
+ }
+
+ @Override
+ public boolean send(ClusterMessage message, NodeId toNodeId) {
+ ClusterMessageStream stream = streams.get(toNodeId);
+ if (stream != null) {
+ try {
+ stream.write(message);
+ return true;
+ } catch (IOException e) {
+ log.warn("Unable to send message {} to node {}",
+ message.subject(), toNodeId);
+ }
+ }
+ return false;
+ }
+
+ @Override
+ public synchronized void addSubscriber(MessageSubject subject,
+ MessageSubscriber subscriber) {
+ subscribers.put(subject, subscriber);
+ }
+
+ @Override
+ public synchronized void removeSubscriber(MessageSubject subject,
+ MessageSubscriber subscriber) {
+ subscribers.remove(subject, subscriber);
+ }
+
+ @Override
+ public Set<MessageSubscriber> getSubscribers(MessageSubject subject) {
+ return ImmutableSet.copyOf(subscribers.get(subject));
+ }
+
+ @Override
+ public void addNode(DefaultControllerNode node) {
+ nodes.add(node);
+ }
+
+ @Override
+ public void removeNode(DefaultControllerNode node) {
+ nodes.remove(node);
+ ClusterMessageStream stream = streams.remove(node.id());
+ if (stream != null) {
+ stream.close();
+ }
+ }
+
+ @Override
+ public void startUp(DefaultControllerNode localNode,
+ ClusterNodesDelegate delegate) {
+ this.localNode = localNode;
+ this.nodesDelegate = delegate;
+
+ startCommunications();
+ startListening();
+ startInitiatingConnections();
+ log.info("Started");
+ }
+
+ /**
+ * Dispatches the specified message to all subscribers to its subject.
+ *
+ * @param message message to dispatch
+ * @param fromNodeId node from which the message was received
+ */
+ void dispatch(ClusterMessage message, NodeId fromNodeId) {
+ Set<MessageSubscriber> set = getSubscribers(message.subject());
+ if (set != null) {
+ for (MessageSubscriber subscriber : set) {
+ subscriber.receive(message, fromNodeId);
+ }
+ }
+ }
+
+ /**
+ * Removes the stream associated with the specified node.
+ *
+ * @param nodeId newly detected cluster node id
+ * @param ip node IP listen address
+ * @param tcpPort node TCP listen port
+ * @return controller node bound to the stream
+ */
+ DefaultControllerNode addNodeStream(NodeId nodeId, IpPrefix ip, int tcpPort,
+ ClusterMessageStream stream) {
+ DefaultControllerNode node = nodesDelegate.nodeDetected(nodeId, ip, tcpPort);
+ stream.setNode(node);
+ streams.put(node.id(), stream);
+ return node;
+ }
+
+ /**
+ * Removes the stream associated with the specified node.
+ *
+ * @param node node whose stream to remove
+ */
+ void removeNodeStream(DefaultControllerNode node) {
+ nodesDelegate.nodeVanished(node.id());
+ streams.remove(node.id());
+ }
+
+ /**
+ * Finds the least utilized IO worker.
+ *
+ * @return IO worker
+ */
+ ClusterIOWorker findWorker() {
+ ClusterIOWorker leastUtilized = null;
+ int minCount = Integer.MAX_VALUE;
+ for (ClusterIOWorker worker : workers) {
+ int count = worker.streamCount();
+ if (count == 0) {
+ return worker;
+ }
+
+ if (count < minCount) {
+ leastUtilized = worker;
+ minCount = count;
+ }
+ }
+ return leastUtilized;
+ }
+
+ /**
+ * Kicks off the IO loops and waits for them to startup.
+ */
+ private void startCommunications() {
+ HelloMessage hello = new HelloMessage(localNode.id(), localNode.ip(),
+ localNode.tcpPort());
+ for (int i = 0; i < WORKERS; i++) {
+ try {
+ ClusterIOWorker worker =
+ new ClusterIOWorker(this, serializationService, hello);
+ workers.add(worker);
+ commExecutors.execute(worker);
+ } catch (IOException e) {
+ log.warn("Unable to start communication worker", e);
+ }
+ }
+
+ // Wait for the IO loops to start
+ for (ClusterIOWorker loop : workers) {
+ if (!loop.awaitStart(START_TIMEOUT)) {
+ log.warn("Comm loop did not start on-time; moving on...");
+ }
+ }
+ }
+
+ /**
+ * Starts listening for connections from peer cluster members.
+ */
+ private void startListening() {
+ try {
+ connectionListener =
+ new ClusterConnectionListener(this, localNode.ip(), localNode.tcpPort());
+ listenExecutor.execute(connectionListener);
+ if (!connectionListener.awaitStart(START_TIMEOUT)) {
+ log.warn("Listener did not start on-time; moving on...");
+ }
+ } catch (IOException e) {
+ log.error("Unable to listen for cluster connections", e);
+ }
+ }
+
+ /**
+ * Attempts to connect to any nodes that do not have an associated connection.
+ */
+ private void startInitiatingConnections() {
+ timer.schedule(connectionCustodian, CONNECTION_CUSTODIAN_DELAY,
+ CONNECTION_CUSTODIAN_FREQUENCY);
+ }
+
+ /**
+ * Initiates open connection request and registers the pending socket
+ * channel with the given IO worker.
+ *
+ * @param worker loop with which the channel should be registered
+ * @throws java.io.IOException if the socket could not be open or connected
+ */
+ private void initiateConnection(DefaultControllerNode node,
+ ClusterIOWorker worker) throws IOException {
+ SocketAddress sa = new InetSocketAddress(getByAddress(node.ip().toOctets()), node.tcpPort());
+ SocketChannel ch = SocketChannel.open();
+ ch.configureBlocking(false);
+ ch.connect(sa);
+ worker.connectStream(ch);
+ }
+
+ // Sweeps through all controller nodes and attempts to open connection to
+ // those that presently do not have one.
+ private class ConnectionCustodian extends TimerTask {
+ @Override
+ public void run() {
+ for (DefaultControllerNode node : nodes) {
+ if (!node.id().equals(localNode.id()) && !streams.containsKey(node.id())) {
+ try {
+ initiateConnection(node, findWorker());
+ } catch (IOException e) {
+ log.debug("Unable to connect", e);
+ }
+ }
+ }
+ }
+ }
+
+}
diff --git a/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/ClusterConnectionListener.java b/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/ClusterConnectionListener.java
index ae4a76f..36d5ab4 100644
--- a/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/ClusterConnectionListener.java
+++ b/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/ClusterConnectionListener.java
@@ -23,12 +23,12 @@
private static final int SO_SEND_BUFFER_SIZE = COMM_BUFFER_SIZE;
private static final int SO_RCV_BUFFER_SIZE = COMM_BUFFER_SIZE;
- private final WorkerFinder workerFinder;
+ private final ClusterCommunicationManager manager;
- ClusterConnectionListener(IpPrefix ip, int tcpPort,
- WorkerFinder workerFinder) throws IOException {
+ ClusterConnectionListener(ClusterCommunicationManager manager,
+ IpPrefix ip, int tcpPort) throws IOException {
super(SELECT_TIMEOUT, new InetSocketAddress(getByAddress(ip.toOctets()), tcpPort));
- this.workerFinder = workerFinder;
+ this.manager = manager;
}
@Override
@@ -41,7 +41,7 @@
so.setReceiveBufferSize(SO_RCV_BUFFER_SIZE);
so.setSendBufferSize(SO_SEND_BUFFER_SIZE);
- workerFinder.findWorker().acceptStream(sc);
+ manager.findWorker().acceptStream(sc);
}
}
diff --git a/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/ClusterIOWorker.java b/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/ClusterIOWorker.java
index 0e93985..c096a7d 100644
--- a/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/ClusterIOWorker.java
+++ b/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/ClusterIOWorker.java
@@ -3,8 +3,10 @@
import org.onlab.nio.IOLoop;
import org.onlab.nio.MessageStream;
import org.onlab.onos.cluster.DefaultControllerNode;
+import org.onlab.onos.cluster.NodeId;
import org.onlab.onos.store.cluster.messaging.ClusterMessage;
import org.onlab.onos.store.cluster.messaging.ClusterMessageStream;
+import org.onlab.onos.store.cluster.messaging.HelloMessage;
import org.onlab.onos.store.cluster.messaging.SerializationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -29,27 +31,23 @@
private static final long SELECT_TIMEOUT = 50;
- private final ConnectionManager connectionManager;
- private final CommunicationsDelegate commsDelegate;
+ private final ClusterCommunicationManager manager;
private final SerializationService serializationService;
private final ClusterMessage helloMessage;
/**
* Creates a new cluster IO worker.
*
- * @param connectionManager parent connection manager
- * @param commsDelegate communications delegate for dispatching
+ * @param manager parent comms manager
* @param serializationService serialization service for encode/decode
* @param helloMessage hello message for greeting peers
* @throws IOException if errors occur during IO loop ignition
*/
- ClusterIOWorker(ConnectionManager connectionManager,
- CommunicationsDelegate commsDelegate,
+ ClusterIOWorker(ClusterCommunicationManager manager,
SerializationService serializationService,
ClusterMessage helloMessage) throws IOException {
super(SELECT_TIMEOUT);
- this.connectionManager = connectionManager;
- this.commsDelegate = commsDelegate;
+ this.manager = manager;
this.serializationService = serializationService;
this.helloMessage = helloMessage;
}
@@ -61,11 +59,27 @@
@Override
protected void processMessages(List<ClusterMessage> messages, MessageStream<ClusterMessage> stream) {
+ NodeId nodeId = getNodeId(messages, (ClusterMessageStream) stream);
for (ClusterMessage message : messages) {
- commsDelegate.dispatch(message);
+ manager.dispatch(message, nodeId);
}
}
+ // Retrieves the node from the stream. If one is not bound, it attempts
+ // to bind it using the knowledge that the first message must be a hello.
+ private NodeId getNodeId(List<ClusterMessage> messages, ClusterMessageStream stream) {
+ DefaultControllerNode node = stream.node();
+ if (node == null && !messages.isEmpty()) {
+ ClusterMessage firstMessage = messages.get(0);
+ if (firstMessage instanceof HelloMessage) {
+ HelloMessage hello = (HelloMessage) firstMessage;
+ node = manager.addNodeStream(hello.nodeId(), hello.ipAddress(),
+ hello.tcpPort(), stream);
+ }
+ }
+ return node != null ? node.id() : null;
+ }
+
@Override
public ClusterMessageStream acceptStream(SocketChannel channel) {
ClusterMessageStream stream = super.acceptStream(channel);
@@ -99,7 +113,7 @@
DefaultControllerNode node = ((ClusterMessageStream) stream).node();
if (node != null) {
log.info("Closed connection to node {}", node.id());
- connectionManager.removeNodeStream(node);
+ manager.removeNodeStream(node);
}
super.removeStream(stream);
}
diff --git a/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/ClusterNodesDelegate.java b/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/ClusterNodesDelegate.java
index b822304..0c47d18 100644
--- a/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/ClusterNodesDelegate.java
+++ b/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/ClusterNodesDelegate.java
@@ -1,6 +1,8 @@
package org.onlab.onos.store.cluster.impl;
import org.onlab.onos.cluster.DefaultControllerNode;
+import org.onlab.onos.cluster.NodeId;
+import org.onlab.packet.IpPrefix;
/**
* Simple back interface through which connection manager can interact with
@@ -9,17 +11,20 @@
public interface ClusterNodesDelegate {
/**
- * Notifies about a new cluster node being detected.
+ * Notifies about cluster node coming online.
*
- * @param node newly detected cluster node
+ * @param nodeId newly detected cluster node id
+ * @param ip node IP listen address
+ * @param tcpPort node TCP listen port
+ * @return the controller node
*/
- void nodeDetected(DefaultControllerNode node);
+ DefaultControllerNode nodeDetected(NodeId nodeId, IpPrefix ip, int tcpPort);
/**
* Notifies about cluster node going offline.
*
- * @param node cluster node that vanished
+ * @param nodeId identifier of the cluster node that vanished
*/
- void nodeVanished(DefaultControllerNode node);
+ void nodeVanished(NodeId nodeId);
}
diff --git a/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/CommunicationsDelegate.java b/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/CommunicationsDelegate.java
deleted file mode 100644
index e74d14b..0000000
--- a/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/CommunicationsDelegate.java
+++ /dev/null
@@ -1,24 +0,0 @@
-package org.onlab.onos.store.cluster.impl;
-
-import org.onlab.onos.store.cluster.messaging.ClusterMessage;
-
-/**
- * Simple back interface for interacting with the communications service.
- */
-public interface CommunicationsDelegate {
-
- /**
- * Dispatches the specified message to all registered subscribers.
- *
- * @param message message to be dispatched
- */
- void dispatch(ClusterMessage message);
-
- /**
- * Sets the sender.
- *
- * @param messageSender message sender
- */
- void setSender(MessageSender messageSender);
-
-}
diff --git a/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/ConnectionManager.java b/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/ConnectionManager.java
deleted file mode 100644
index fac3c21..0000000
--- a/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/ConnectionManager.java
+++ /dev/null
@@ -1,255 +0,0 @@
-package org.onlab.onos.store.cluster.impl;
-
-import org.onlab.onos.cluster.DefaultControllerNode;
-import org.onlab.onos.cluster.NodeId;
-import org.onlab.onos.store.cluster.messaging.ClusterMessage;
-import org.onlab.onos.store.cluster.messaging.ClusterMessageStream;
-import org.onlab.onos.store.cluster.messaging.HelloMessage;
-import org.onlab.onos.store.cluster.messaging.SerializationService;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.io.IOException;
-import java.net.InetSocketAddress;
-import java.net.SocketAddress;
-import java.nio.channels.SocketChannel;
-import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.Timer;
-import java.util.TimerTask;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-
-import static java.net.InetAddress.getByAddress;
-import static org.onlab.util.Tools.namedThreads;
-
-/**
- * Manages connections to other controller cluster nodes.
- */
-public class ConnectionManager implements MessageSender {
-
- private final Logger log = LoggerFactory.getLogger(getClass());
-
- private static final long CONNECTION_CUSTODIAN_DELAY = 1000L;
- private static final long CONNECTION_CUSTODIAN_FREQUENCY = 5000;
-
- private static final long START_TIMEOUT = 1000;
- private static final int WORKERS = 3;
-
- private ClusterConnectionListener connectionListener;
- private List<ClusterIOWorker> workers = new ArrayList<>(WORKERS);
-
- private final DefaultControllerNode localNode;
- private final ClusterNodesDelegate nodesDelegate;
- private final CommunicationsDelegate commsDelegate;
- private final SerializationService serializationService;
-
- // Nodes to be monitored to make sure they have a connection.
- private final Set<DefaultControllerNode> nodes = new HashSet<>();
-
- // Means to track message streams to other nodes.
- private final Map<NodeId, ClusterMessageStream> streams = new ConcurrentHashMap<>();
-
- // Executor pools for listening and managing connections to other nodes.
- private final ExecutorService listenExecutor =
- Executors.newSingleThreadExecutor(namedThreads("onos-comm-listen"));
- private final ExecutorService commExecutors =
- Executors.newFixedThreadPool(WORKERS, namedThreads("onos-comm-cluster"));
- private final ExecutorService heartbeatExecutor =
- Executors.newSingleThreadExecutor(namedThreads("onos-comm-heartbeat"));
-
- private final Timer timer = new Timer("onos-comm-initiator");
- private final TimerTask connectionCustodian = new ConnectionCustodian();
-
- private final WorkerFinder workerFinder = new LeastUtilitiedWorkerFinder();
-
-
- /**
- * Creates a new connection manager.
- */
- ConnectionManager(DefaultControllerNode localNode,
- ClusterNodesDelegate nodesDelegate,
- CommunicationsDelegate commsDelegate,
- SerializationService serializationService) {
- this.localNode = localNode;
- this.nodesDelegate = nodesDelegate;
- this.commsDelegate = commsDelegate;
- this.serializationService = serializationService;
-
- commsDelegate.setSender(this);
- startCommunications();
- startListening();
- startInitiating();
- log.info("Started");
- }
-
- /**
- * Shuts down the connection manager.
- */
- void shutdown() {
- connectionListener.shutdown();
- for (ClusterIOWorker worker : workers) {
- worker.shutdown();
- }
- log.info("Stopped");
- }
-
- /**
- * Adds the node to the list of monitored nodes.
- *
- * @param node node to be added
- */
- void addNode(DefaultControllerNode node) {
- nodes.add(node);
- }
-
- /**
- * Removes the node from the list of monitored nodes.
- *
- * @param node node to be removed
- */
- void removeNode(DefaultControllerNode node) {
- nodes.remove(node);
- ClusterMessageStream stream = streams.remove(node.id());
- if (stream != null) {
- stream.close();
- }
- }
-
- /**
- * Removes the stream associated with the specified node.
- *
- * @param node node whose stream to remove
- */
- void removeNodeStream(DefaultControllerNode node) {
- nodesDelegate.nodeVanished(node);
- streams.remove(node.id());
- }
-
- @Override
- public boolean send(NodeId nodeId, ClusterMessage message) {
- ClusterMessageStream stream = streams.get(nodeId);
- if (stream != null) {
- try {
- stream.write(message);
- return true;
- } catch (IOException e) {
- log.warn("Unable to send a message about {} to node {}",
- message.subject(), nodeId);
- }
- }
- return false;
- }
-
- /**
- * Kicks off the IO loops and waits for them to startup.
- */
- private void startCommunications() {
- HelloMessage hello = new HelloMessage(localNode.id(), localNode.ip(),
- localNode.tcpPort());
- for (int i = 0; i < WORKERS; i++) {
- try {
- ClusterIOWorker worker =
- new ClusterIOWorker(this, commsDelegate,
- serializationService, hello);
- workers.add(worker);
- commExecutors.execute(worker);
- } catch (IOException e) {
- log.warn("Unable to start communication worker", e);
- }
- }
-
- // Wait for the IO loops to start
- for (ClusterIOWorker loop : workers) {
- if (!loop.awaitStart(START_TIMEOUT)) {
- log.warn("Comm loop did not start on-time; moving on...");
- }
- }
- }
-
- /**
- * Starts listening for connections from peer cluster members.
- */
- private void startListening() {
- try {
- connectionListener =
- new ClusterConnectionListener(localNode.ip(), localNode.tcpPort(),
- workerFinder);
- listenExecutor.execute(connectionListener);
- if (!connectionListener.awaitStart(START_TIMEOUT)) {
- log.warn("Listener did not start on-time; moving on...");
- }
- } catch (IOException e) {
- log.error("Unable to listen for cluster connections", e);
- }
- }
-
- /**
- * Initiates open connection request and registers the pending socket
- * channel with the given IO loop.
- *
- * @param loop loop with which the channel should be registered
- * @throws java.io.IOException if the socket could not be open or connected
- */
- private void initiateConnection(DefaultControllerNode node,
- ClusterIOWorker loop) throws IOException {
- SocketAddress sa = new InetSocketAddress(getByAddress(node.ip().toOctets()), node.tcpPort());
- SocketChannel ch = SocketChannel.open();
- ch.configureBlocking(false);
- ch.connect(sa);
- loop.connectStream(ch);
- }
-
-
- /**
- * Attempts to connect to any nodes that do not have an associated connection.
- */
- private void startInitiating() {
- timer.schedule(connectionCustodian, CONNECTION_CUSTODIAN_DELAY,
- CONNECTION_CUSTODIAN_FREQUENCY);
- }
-
- // Sweeps through all controller nodes and attempts to open connection to
- // those that presently do not have one.
- private class ConnectionCustodian extends TimerTask {
- @Override
- public void run() {
- for (DefaultControllerNode node : nodes) {
- if (node != localNode && !streams.containsKey(node.id())) {
- try {
- initiateConnection(node, workerFinder.findWorker());
- } catch (IOException e) {
- log.debug("Unable to connect", e);
- }
- }
- }
- }
- }
-
- // Finds the least utilitied IO loop.
- private class LeastUtilitiedWorkerFinder implements WorkerFinder {
-
- @Override
- public ClusterIOWorker findWorker() {
- ClusterIOWorker leastUtilized = null;
- int minCount = Integer.MAX_VALUE;
- for (ClusterIOWorker worker : workers) {
- int count = worker.streamCount();
- if (count == 0) {
- return worker;
- }
-
- if (count < minCount) {
- leastUtilized = worker;
- minCount = count;
- }
- }
- return leastUtilized;
- }
- }
-
-}
diff --git a/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/DistributedClusterStore.java b/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/DistributedClusterStore.java
index ae04226..025804a 100644
--- a/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/DistributedClusterStore.java
+++ b/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/DistributedClusterStore.java
@@ -14,7 +14,6 @@
import org.onlab.onos.cluster.DefaultControllerNode;
import org.onlab.onos.cluster.NodeId;
import org.onlab.onos.store.AbstractStore;
-import org.onlab.onos.store.cluster.messaging.SerializationService;
import org.onlab.packet.IpPrefix;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -43,20 +42,20 @@
private final Map<NodeId, State> states = new ConcurrentHashMap<>();
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
- private CommunicationsDelegate commsDelegate;
-
- @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
- private SerializationService serializationService;
+ private ClusterCommunicationAdminService communicationAdminService;
private final ClusterNodesDelegate nodesDelegate = new InnerNodesDelegate();
- private ConnectionManager connectionManager;
@Activate
public void activate() {
loadClusterDefinition();
establishSelfIdentity();
- connectionManager = new ConnectionManager(localNode, nodesDelegate,
- commsDelegate, serializationService);
+
+ // Start-up the comm service and prime it with the loaded nodes.
+ communicationAdminService.startUp(localNode, nodesDelegate);
+ for (DefaultControllerNode node : nodes.values()) {
+ communicationAdminService.addNode(node);
+ }
log.info("Started");
}
@@ -92,8 +91,8 @@
if (localNode == null) {
localNode = new DefaultControllerNode(new NodeId(ip.toString()), ip);
nodes.put(localNode.id(), localNode);
- states.put(localNode.id(), State.ACTIVE);
}
+ states.put(localNode.id(), State.ACTIVE);
}
@Override
@@ -122,7 +121,7 @@
public ControllerNode addNode(NodeId nodeId, IpPrefix ip, int tcpPort) {
DefaultControllerNode node = new DefaultControllerNode(nodeId, ip, tcpPort);
nodes.put(nodeId, node);
- connectionManager.addNode(node);
+ communicationAdminService.addNode(node);
return node;
}
@@ -130,21 +129,25 @@
public void removeNode(NodeId nodeId) {
DefaultControllerNode node = nodes.remove(nodeId);
if (node != null) {
- connectionManager.removeNode(node);
+ communicationAdminService.removeNode(node);
}
}
// Entity to handle back calls from the connection manager.
private class InnerNodesDelegate implements ClusterNodesDelegate {
@Override
- public void nodeDetected(DefaultControllerNode node) {
- nodes.put(node.id(), node);
- states.put(node.id(), State.ACTIVE);
+ public DefaultControllerNode nodeDetected(NodeId nodeId, IpPrefix ip, int tcpPort) {
+ DefaultControllerNode node = nodes.get(nodeId);
+ if (node == null) {
+ node = (DefaultControllerNode) addNode(nodeId, ip, tcpPort);
+ }
+ states.put(nodeId, State.ACTIVE);
+ return node;
}
-
@Override
- public void nodeVanished(DefaultControllerNode node) {
- states.put(node.id(), State.INACTIVE);
+ public void nodeVanished(NodeId nodeId) {
+ states.put(nodeId, State.INACTIVE);
}
}
+
}
diff --git a/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/MessageSender.java b/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/MessageSender.java
deleted file mode 100644
index 55f868c..0000000
--- a/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/MessageSender.java
+++ /dev/null
@@ -1,21 +0,0 @@
-package org.onlab.onos.store.cluster.impl;
-
-import org.onlab.onos.cluster.NodeId;
-import org.onlab.onos.store.cluster.messaging.ClusterMessage;
-
-/**
- * Created by tom on 9/29/14.
- */
-public interface MessageSender {
-
- /**
- * Sends the specified message to the given cluster node.
- *
- * @param nodeId node identifier
- * @param message mesage to send
- * @return true if the message was sent sucessfully; false if there is
- * no stream or if there was an error
- */
- boolean send(NodeId nodeId, ClusterMessage message);
-
-}
diff --git a/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/WorkerFinder.java b/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/WorkerFinder.java
deleted file mode 100644
index 06f4f8a..0000000
--- a/core/store/dist/src/main/java/org/onlab/onos/store/cluster/impl/WorkerFinder.java
+++ /dev/null
@@ -1,14 +0,0 @@
-package org.onlab.onos.store.cluster.impl;
-
-/**
- * Provides means to find a worker IO loop.
- */
-public interface WorkerFinder {
-
- /**
- * Finds a suitable worker.
- *
- * @return available worker
- */
- ClusterIOWorker findWorker();
-}
diff --git a/core/store/dist/src/main/java/org/onlab/onos/store/cluster/messaging/HelloMessage.java b/core/store/dist/src/main/java/org/onlab/onos/store/cluster/messaging/HelloMessage.java
index ddc79d3..923e21e 100644
--- a/core/store/dist/src/main/java/org/onlab/onos/store/cluster/messaging/HelloMessage.java
+++ b/core/store/dist/src/main/java/org/onlab/onos/store/cluster/messaging/HelloMessage.java
@@ -29,9 +29,9 @@
*/
public HelloMessage(NodeId nodeId, IpPrefix ipAddress, int tcpPort) {
super(MessageSubject.HELLO);
- nodeId = nodeId;
- ipAddress = ipAddress;
- tcpPort = tcpPort;
+ this.nodeId = nodeId;
+ this.ipAddress = ipAddress;
+ this.tcpPort = tcpPort;
}
/**
@@ -60,4 +60,5 @@
public int tcpPort() {
return tcpPort;
}
+
}
diff --git a/core/store/dist/src/main/java/org/onlab/onos/store/cluster/messaging/MessageSubscriber.java b/core/store/dist/src/main/java/org/onlab/onos/store/cluster/messaging/MessageSubscriber.java
index 6b78fec..68cd83c 100644
--- a/core/store/dist/src/main/java/org/onlab/onos/store/cluster/messaging/MessageSubscriber.java
+++ b/core/store/dist/src/main/java/org/onlab/onos/store/cluster/messaging/MessageSubscriber.java
@@ -1,5 +1,7 @@
package org.onlab.onos.store.cluster.messaging;
+import org.onlab.onos.cluster.NodeId;
+
/**
* Represents a message consumer.
*/
@@ -8,8 +10,9 @@
/**
* Receives the specified cluster message.
*
- * @param message message to be received
+ * @param message message to be received
+ * @param fromNodeId node from which the message was received
*/
- void receive(ClusterMessage message);
+ void receive(ClusterMessage message, NodeId fromNodeId);
}
diff --git a/core/store/dist/src/main/java/org/onlab/onos/store/cluster/messaging/SerializationService.java b/core/store/dist/src/main/java/org/onlab/onos/store/cluster/messaging/SerializationService.java
index 79e054b..7521630 100644
--- a/core/store/dist/src/main/java/org/onlab/onos/store/cluster/messaging/SerializationService.java
+++ b/core/store/dist/src/main/java/org/onlab/onos/store/cluster/messaging/SerializationService.java
@@ -3,12 +3,12 @@
import java.nio.ByteBuffer;
/**
- * Service for serializing/deserializing intra-cluster messages.
+ * Service for encoding & decoding intra-cluster messages.
*/
public interface SerializationService {
/**
- * Decodes the specified byte buffer to obtain a message within.
+ * Decodes the specified byte buffer to obtain the message within.
*
* @param buffer byte buffer with message(s)
* @return parsed message
diff --git a/core/store/dist/src/main/java/org/onlab/onos/store/cluster/messaging/impl/ClusterCommunicationManager.java b/core/store/dist/src/main/java/org/onlab/onos/store/cluster/messaging/impl/ClusterCommunicationManager.java
deleted file mode 100644
index bafb2c3..0000000
--- a/core/store/dist/src/main/java/org/onlab/onos/store/cluster/messaging/impl/ClusterCommunicationManager.java
+++ /dev/null
@@ -1,64 +0,0 @@
-package org.onlab.onos.store.cluster.messaging.impl;
-
-import com.google.common.collect.HashMultimap;
-import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.Multimap;
-import org.apache.felix.scr.annotations.Component;
-import org.apache.felix.scr.annotations.Service;
-import org.onlab.onos.cluster.NodeId;
-import org.onlab.onos.store.cluster.impl.CommunicationsDelegate;
-import org.onlab.onos.store.cluster.impl.MessageSender;
-import org.onlab.onos.store.cluster.messaging.ClusterCommunicationService;
-import org.onlab.onos.store.cluster.messaging.ClusterMessage;
-import org.onlab.onos.store.cluster.messaging.MessageSubject;
-import org.onlab.onos.store.cluster.messaging.MessageSubscriber;
-
-import java.util.Set;
-
-/**
- * Implements the cluster communication services to use by other stores.
- */
-@Component(immediate = true)
-@Service
-public class ClusterCommunicationManager
- implements ClusterCommunicationService, CommunicationsDelegate {
-
- // TODO: use something different that won't require synchronization
- private Multimap<MessageSubject, MessageSubscriber> subscribers = HashMultimap.create();
- private MessageSender messageSender;
-
- @Override
- public boolean send(ClusterMessage message, NodeId toNodeId) {
- return messageSender.send(toNodeId, message);
- }
-
- @Override
- public synchronized void addSubscriber(MessageSubject subject, MessageSubscriber subscriber) {
- subscribers.put(subject, subscriber);
- }
-
- @Override
- public synchronized void removeSubscriber(MessageSubject subject, MessageSubscriber subscriber) {
- subscribers.remove(subject, subscriber);
- }
-
- @Override
- public Set<MessageSubscriber> getSubscribers(MessageSubject subject) {
- return ImmutableSet.copyOf(subscribers.get(subject));
- }
-
- @Override
- public void dispatch(ClusterMessage message) {
- Set<MessageSubscriber> set = getSubscribers(message.subject());
- if (set != null) {
- for (MessageSubscriber subscriber : set) {
- subscriber.receive(message);
- }
- }
- }
-
- @Override
- public void setSender(MessageSender messageSender) {
- this.messageSender = messageSender;
- }
-}
diff --git a/core/store/dist/src/main/java/org/onlab/onos/store/cluster/messaging/impl/MessageSerializer.java b/core/store/dist/src/main/java/org/onlab/onos/store/cluster/messaging/impl/MessageSerializer.java
index 93c8310..7064801 100644
--- a/core/store/dist/src/main/java/org/onlab/onos/store/cluster/messaging/impl/MessageSerializer.java
+++ b/core/store/dist/src/main/java/org/onlab/onos/store/cluster/messaging/impl/MessageSerializer.java
@@ -1,8 +1,15 @@
package org.onlab.onos.store.cluster.messaging.impl;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Service;
+import org.onlab.onos.cluster.NodeId;
import org.onlab.onos.store.cluster.messaging.ClusterMessage;
+import org.onlab.onos.store.cluster.messaging.HelloMessage;
import org.onlab.onos.store.cluster.messaging.MessageSubject;
import org.onlab.onos.store.cluster.messaging.SerializationService;
+import org.onlab.packet.IpPrefix;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import java.nio.ByteBuffer;
@@ -11,8 +18,12 @@
/**
* Factory for parsing messages sent between cluster members.
*/
+@Component(immediate = true)
+@Service
public class MessageSerializer implements SerializationService {
+ private final Logger log = LoggerFactory.getLogger(getClass());
+
private static final int METADATA_LENGTH = 16; // 8 + 4 + 4
private static final int LENGTH_OFFSET = 12;
@@ -46,11 +57,12 @@
buffer.get(data);
// TODO: add deserialization hook here; for now this hack
- return null; // actually deserialize
+ String[] fields = new String(data).split(":");
+ return new HelloMessage(new NodeId(fields[0]), IpPrefix.valueOf(fields[1]), Integer.parseInt(fields[2]));
} catch (Exception e) {
// TODO: recover from exceptions by forwarding stream to next marker
- e.printStackTrace();
+ log.warn("Unable to decode message due to: " + e);
}
return null;
}
@@ -58,11 +70,18 @@
@Override
public void encode(ClusterMessage message, ByteBuffer buffer) {
try {
- int i = 0;
- // Type based lookup for proper encoder
+ HelloMessage helloMessage = (HelloMessage) message;
+ buffer.putLong(MARKER);
+ buffer.putInt(message.subject().ordinal());
+
+ String str = helloMessage.nodeId() + ":" + helloMessage.ipAddress() + ":" + helloMessage.tcpPort();
+ byte[] data = str.getBytes();
+ buffer.putInt(data.length + METADATA_LENGTH);
+ buffer.put(data);
+
} catch (Exception e) {
// TODO: recover from exceptions by forwarding stream to next marker
- e.printStackTrace();
+ log.warn("Unable to encode message due to: " + e);
}
}