tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.cluster.impl; |
| 2 | |
| 3 | import com.google.common.collect.ImmutableSet; |
| 4 | import org.apache.felix.scr.annotations.Activate; |
| 5 | import org.apache.felix.scr.annotations.Component; |
| 6 | import org.apache.felix.scr.annotations.Deactivate; |
| 7 | import org.apache.felix.scr.annotations.Service; |
| 8 | import org.onlab.nio.AcceptorLoop; |
| 9 | import org.onlab.nio.IOLoop; |
| 10 | import org.onlab.nio.MessageStream; |
| 11 | import org.onlab.onos.cluster.ClusterEvent; |
| 12 | import org.onlab.onos.cluster.ClusterStore; |
| 13 | import org.onlab.onos.cluster.ClusterStoreDelegate; |
| 14 | import org.onlab.onos.cluster.ControllerNode; |
| 15 | import org.onlab.onos.cluster.DefaultControllerNode; |
| 16 | import org.onlab.onos.cluster.NodeId; |
| 17 | import org.onlab.onos.store.AbstractStore; |
| 18 | import org.onlab.packet.IpPrefix; |
| 19 | import org.slf4j.Logger; |
| 20 | import org.slf4j.LoggerFactory; |
| 21 | |
| 22 | import java.io.IOException; |
| 23 | import java.net.InetSocketAddress; |
| 24 | import java.net.Socket; |
| 25 | import java.net.SocketAddress; |
| 26 | import java.nio.channels.ByteChannel; |
| 27 | import java.nio.channels.SelectionKey; |
| 28 | import java.nio.channels.ServerSocketChannel; |
| 29 | import java.nio.channels.SocketChannel; |
| 30 | import java.util.ArrayList; |
| 31 | import java.util.List; |
| 32 | import java.util.Map; |
tom | 5a8779c | 2014-09-29 14:48:43 -0700 | [diff] [blame^] | 33 | import java.util.Objects; |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 34 | import java.util.Set; |
| 35 | import java.util.Timer; |
| 36 | import java.util.TimerTask; |
| 37 | import java.util.concurrent.ConcurrentHashMap; |
| 38 | import java.util.concurrent.ExecutorService; |
| 39 | import java.util.concurrent.Executors; |
| 40 | |
| 41 | import static java.net.InetAddress.getByAddress; |
| 42 | import static org.onlab.onos.cluster.ControllerNode.State; |
| 43 | import static org.onlab.packet.IpPrefix.valueOf; |
| 44 | import static org.onlab.util.Tools.namedThreads; |
| 45 | |
| 46 | /** |
| 47 | * Distributed implementation of the cluster nodes store. |
| 48 | */ |
| 49 | @Component(immediate = true) |
| 50 | @Service |
| 51 | public class DistributedClusterStore |
| 52 | extends AbstractStore<ClusterEvent, ClusterStoreDelegate> |
| 53 | implements ClusterStore { |
| 54 | |
| 55 | private static final int HELLO_MSG = 1; |
| 56 | private static final int ECHO_MSG = 2; |
| 57 | |
| 58 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 59 | |
| 60 | private static final long CONNECTION_CUSTODIAN_DELAY = 1000L; |
| 61 | private static final long CONNECTION_CUSTODIAN_FREQUENCY = 5000; |
| 62 | |
| 63 | private static final long START_TIMEOUT = 1000; |
| 64 | private static final long SELECT_TIMEOUT = 50; |
| 65 | private static final int WORKERS = 3; |
| 66 | private static final int COMM_BUFFER_SIZE = 32 * 1024; |
| 67 | private static final int COMM_IDLE_TIME = 500; |
| 68 | |
| 69 | private static final boolean SO_NO_DELAY = false; |
| 70 | private static final int SO_SEND_BUFFER_SIZE = COMM_BUFFER_SIZE; |
| 71 | private static final int SO_RCV_BUFFER_SIZE = COMM_BUFFER_SIZE; |
| 72 | |
| 73 | private DefaultControllerNode self; |
| 74 | private final Map<NodeId, DefaultControllerNode> nodes = new ConcurrentHashMap<>(); |
| 75 | private final Map<NodeId, State> states = new ConcurrentHashMap<>(); |
| 76 | |
| 77 | // Means to track message streams to other nodes. |
| 78 | private final Map<NodeId, TLVMessageStream> streams = new ConcurrentHashMap<>(); |
| 79 | private final Map<SocketChannel, DefaultControllerNode> nodesByChannel = new ConcurrentHashMap<>(); |
| 80 | |
| 81 | // Executor pools for listening and managing connections to other nodes. |
| 82 | private final ExecutorService listenExecutor = |
| 83 | Executors.newSingleThreadExecutor(namedThreads("onos-comm-listen")); |
| 84 | private final ExecutorService commExecutors = |
| 85 | Executors.newFixedThreadPool(WORKERS, namedThreads("onos-comm-cluster")); |
| 86 | private final ExecutorService heartbeatExecutor = |
| 87 | Executors.newSingleThreadExecutor(namedThreads("onos-comm-heartbeat")); |
| 88 | |
| 89 | private final Timer timer = new Timer("onos-comm-initiator"); |
| 90 | private final TimerTask connectionCustodian = new ConnectionCustodian(); |
| 91 | |
| 92 | private ListenLoop listenLoop; |
| 93 | private List<CommLoop> commLoops = new ArrayList<>(WORKERS); |
| 94 | |
| 95 | @Activate |
| 96 | public void activate() { |
| 97 | loadClusterDefinition(); |
| 98 | startCommunications(); |
| 99 | startListening(); |
| 100 | startInitiating(); |
| 101 | log.info("Started"); |
| 102 | } |
| 103 | |
| 104 | @Deactivate |
| 105 | public void deactivate() { |
| 106 | listenLoop.shutdown(); |
| 107 | for (CommLoop loop : commLoops) { |
| 108 | loop.shutdown(); |
| 109 | } |
| 110 | log.info("Stopped"); |
| 111 | } |
| 112 | |
| 113 | // Loads the cluster definition file |
| 114 | private void loadClusterDefinition() { |
| 115 | // ClusterDefinitionStore cds = new ClusterDefinitionStore("../config/cluster.json"); |
| 116 | // try { |
| 117 | // Set<DefaultControllerNode> storedNodes = cds.read(); |
| 118 | // for (DefaultControllerNode node : storedNodes) { |
| 119 | // nodes.put(node.id(), node); |
| 120 | // } |
| 121 | // } catch (IOException e) { |
| 122 | // log.error("Unable to read cluster definitions", e); |
| 123 | // } |
| 124 | |
| 125 | // Establishes the controller's own identity. |
| 126 | IpPrefix ip = valueOf(System.getProperty("onos.ip", "127.0.1.1")); |
| 127 | self = nodes.get(new NodeId(ip.toString())); |
| 128 | |
| 129 | // As a fall-back, let's make sure we at least know who we are. |
| 130 | if (self == null) { |
| 131 | self = new DefaultControllerNode(new NodeId(ip.toString()), ip); |
| 132 | nodes.put(self.id(), self); |
tom | 5a8779c | 2014-09-29 14:48:43 -0700 | [diff] [blame^] | 133 | states.put(self.id(), State.ACTIVE); |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
| 137 | // Kicks off the IO loops. |
| 138 | private void startCommunications() { |
| 139 | for (int i = 0; i < WORKERS; i++) { |
| 140 | try { |
| 141 | CommLoop loop = new CommLoop(); |
| 142 | commLoops.add(loop); |
| 143 | commExecutors.execute(loop); |
| 144 | } catch (IOException e) { |
| 145 | log.warn("Unable to start comm IO loop", e); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // Wait for the IO loops to start |
| 150 | for (CommLoop loop : commLoops) { |
| 151 | if (!loop.awaitStart(START_TIMEOUT)) { |
| 152 | log.warn("Comm loop did not start on-time; moving on..."); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // Starts listening for connections from peer cluster members. |
| 158 | private void startListening() { |
| 159 | try { |
| 160 | listenLoop = new ListenLoop(self.ip(), self.tcpPort()); |
| 161 | listenExecutor.execute(listenLoop); |
| 162 | if (!listenLoop.awaitStart(START_TIMEOUT)) { |
| 163 | log.warn("Listen loop did not start on-time; moving on..."); |
| 164 | } |
| 165 | } catch (IOException e) { |
| 166 | log.error("Unable to listen for cluster connections", e); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Initiates open connection request and registers the pending socket |
| 172 | * channel with the given IO loop. |
| 173 | * |
| 174 | * @param loop loop with which the channel should be registered |
| 175 | * @throws java.io.IOException if the socket could not be open or connected |
| 176 | */ |
| 177 | private void openConnection(DefaultControllerNode node, CommLoop loop) throws IOException { |
| 178 | SocketAddress sa = new InetSocketAddress(getByAddress(node.ip().toOctets()), node.tcpPort()); |
| 179 | SocketChannel ch = SocketChannel.open(); |
| 180 | nodesByChannel.put(ch, node); |
| 181 | ch.configureBlocking(false); |
| 182 | ch.connect(sa); |
| 183 | loop.connectStream(ch); |
| 184 | } |
| 185 | |
| 186 | |
| 187 | // Attempts to connect to any nodes that do not have an associated connection. |
| 188 | private void startInitiating() { |
| 189 | timer.schedule(connectionCustodian, CONNECTION_CUSTODIAN_DELAY, CONNECTION_CUSTODIAN_FREQUENCY); |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public ControllerNode getLocalNode() { |
| 194 | return self; |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public Set<ControllerNode> getNodes() { |
| 199 | ImmutableSet.Builder<ControllerNode> builder = ImmutableSet.builder(); |
| 200 | return builder.addAll(nodes.values()).build(); |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public ControllerNode getNode(NodeId nodeId) { |
| 205 | return nodes.get(nodeId); |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public State getState(NodeId nodeId) { |
| 210 | State state = states.get(nodeId); |
| 211 | return state == null ? State.INACTIVE : state; |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public ControllerNode addNode(NodeId nodeId, IpPrefix ip, int tcpPort) { |
| 216 | DefaultControllerNode node = new DefaultControllerNode(nodeId, ip, tcpPort); |
| 217 | nodes.put(nodeId, node); |
| 218 | return node; |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public void removeNode(NodeId nodeId) { |
| 223 | nodes.remove(nodeId); |
tom | 5a8779c | 2014-09-29 14:48:43 -0700 | [diff] [blame^] | 224 | TLVMessageStream stream = streams.remove(nodeId); |
| 225 | if (stream != null) { |
| 226 | stream.close(); |
| 227 | } |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | // Listens and accepts inbound connections from other cluster nodes. |
| 231 | private class ListenLoop extends AcceptorLoop { |
| 232 | ListenLoop(IpPrefix ip, int tcpPort) throws IOException { |
| 233 | super(SELECT_TIMEOUT, new InetSocketAddress(getByAddress(ip.toOctets()), tcpPort)); |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | protected void acceptConnection(ServerSocketChannel channel) throws IOException { |
| 238 | SocketChannel sc = channel.accept(); |
| 239 | sc.configureBlocking(false); |
| 240 | |
| 241 | Socket so = sc.socket(); |
| 242 | so.setTcpNoDelay(SO_NO_DELAY); |
| 243 | so.setReceiveBufferSize(SO_RCV_BUFFER_SIZE); |
| 244 | so.setSendBufferSize(SO_SEND_BUFFER_SIZE); |
| 245 | |
| 246 | findLeastUtilizedLoop().acceptStream(sc); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | private class CommLoop extends IOLoop<TLVMessage, TLVMessageStream> { |
| 251 | CommLoop() throws IOException { |
| 252 | super(SELECT_TIMEOUT); |
| 253 | } |
| 254 | |
| 255 | @Override |
| 256 | protected TLVMessageStream createStream(ByteChannel byteChannel) { |
| 257 | return new TLVMessageStream(this, byteChannel, COMM_BUFFER_SIZE, COMM_IDLE_TIME); |
| 258 | } |
| 259 | |
| 260 | @Override |
| 261 | protected void processMessages(List<TLVMessage> messages, MessageStream<TLVMessage> stream) { |
| 262 | TLVMessageStream tlvStream = (TLVMessageStream) stream; |
| 263 | for (TLVMessage message : messages) { |
tom | 5a8779c | 2014-09-29 14:48:43 -0700 | [diff] [blame^] | 264 | // TODO: add type-based dispatching here... this is just a hack to get going |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 265 | if (message.type() == HELLO_MSG) { |
| 266 | processHello(message, tlvStream); |
tom | 5a8779c | 2014-09-29 14:48:43 -0700 | [diff] [blame^] | 267 | } else if (message.type() == ECHO_MSG) { |
| 268 | processEcho(message, tlvStream); |
| 269 | } else { |
| 270 | log.info("Deal with other messages"); |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | @Override |
| 276 | public TLVMessageStream acceptStream(SocketChannel channel) { |
| 277 | TLVMessageStream stream = super.acceptStream(channel); |
| 278 | try { |
| 279 | InetSocketAddress sa = (InetSocketAddress) channel.getRemoteAddress(); |
tom | 5a8779c | 2014-09-29 14:48:43 -0700 | [diff] [blame^] | 280 | log.info("Accepted connection from node {}", valueOf(sa.getAddress().getAddress())); |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 281 | stream.write(createHello(self)); |
| 282 | |
| 283 | } catch (IOException e) { |
| 284 | log.warn("Unable to accept connection from an unknown end-point", e); |
| 285 | } |
| 286 | return stream; |
| 287 | } |
| 288 | |
| 289 | @Override |
| 290 | public TLVMessageStream connectStream(SocketChannel channel) { |
| 291 | TLVMessageStream stream = super.connectStream(channel); |
| 292 | DefaultControllerNode node = nodesByChannel.get(channel); |
| 293 | if (node != null) { |
tom | 5a8779c | 2014-09-29 14:48:43 -0700 | [diff] [blame^] | 294 | log.debug("Opened connection to node {}", node.id()); |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 295 | nodesByChannel.remove(channel); |
| 296 | } |
| 297 | return stream; |
| 298 | } |
| 299 | |
| 300 | @Override |
tom | 5a8779c | 2014-09-29 14:48:43 -0700 | [diff] [blame^] | 301 | protected void connect(SelectionKey key) throws IOException { |
| 302 | try { |
| 303 | super.connect(key); |
| 304 | TLVMessageStream stream = (TLVMessageStream) key.attachment(); |
| 305 | send(stream, createHello(self)); |
| 306 | } catch (IOException e) { |
| 307 | if (!Objects.equals(e.getMessage(), "Connection refused")) { |
| 308 | throw e; |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | @Override |
| 314 | protected void removeStream(MessageStream<TLVMessage> stream) { |
| 315 | DefaultControllerNode node = ((TLVMessageStream) stream).node(); |
| 316 | if (node != null) { |
| 317 | log.info("Closed connection to node {}", node.id()); |
| 318 | states.put(node.id(), State.INACTIVE); |
| 319 | streams.remove(node.id()); |
| 320 | } |
| 321 | super.removeStream(stream); |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
tom | 5a8779c | 2014-09-29 14:48:43 -0700 | [diff] [blame^] | 325 | // Processes a HELLO message from a peer controller node. |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 326 | private void processHello(TLVMessage message, TLVMessageStream stream) { |
tom | 5a8779c | 2014-09-29 14:48:43 -0700 | [diff] [blame^] | 327 | // FIXME: pure hack for now |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 328 | String data = new String(message.data()); |
tom | 5a8779c | 2014-09-29 14:48:43 -0700 | [diff] [blame^] | 329 | String[] fields = data.split(":"); |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 330 | DefaultControllerNode node = new DefaultControllerNode(new NodeId(fields[0]), |
tom | 5a8779c | 2014-09-29 14:48:43 -0700 | [diff] [blame^] | 331 | valueOf(fields[1]), |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 332 | Integer.parseInt(fields[2])); |
| 333 | stream.setNode(node); |
| 334 | nodes.put(node.id(), node); |
| 335 | streams.put(node.id(), stream); |
tom | 5a8779c | 2014-09-29 14:48:43 -0700 | [diff] [blame^] | 336 | states.put(node.id(), State.ACTIVE); |
| 337 | } |
| 338 | |
| 339 | // Processes an ECHO message from a peer controller node. |
| 340 | private void processEcho(TLVMessage message, TLVMessageStream tlvStream) { |
| 341 | // TODO: implement heart-beat refresh |
| 342 | log.info("Dealing with echoes..."); |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | // Sends message to the specified stream. |
| 346 | private void send(TLVMessageStream stream, TLVMessage message) { |
| 347 | try { |
| 348 | stream.write(message); |
| 349 | } catch (IOException e) { |
| 350 | log.warn("Unable to send message to {}", stream.node().id()); |
| 351 | } |
| 352 | } |
| 353 | |
tom | 5a8779c | 2014-09-29 14:48:43 -0700 | [diff] [blame^] | 354 | // Creates a hello message to be sent to a peer controller node. |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 355 | private TLVMessage createHello(DefaultControllerNode self) { |
| 356 | return new TLVMessage(HELLO_MSG, (self.id() + ":" + self.ip() + ":" + self.tcpPort()).getBytes()); |
| 357 | } |
| 358 | |
| 359 | // Sweeps through all controller nodes and attempts to open connection to |
| 360 | // those that presently do not have one. |
| 361 | private class ConnectionCustodian extends TimerTask { |
| 362 | @Override |
| 363 | public void run() { |
| 364 | for (DefaultControllerNode node : nodes.values()) { |
| 365 | if (node != self && !streams.containsKey(node.id())) { |
| 366 | try { |
| 367 | openConnection(node, findLeastUtilizedLoop()); |
| 368 | } catch (IOException e) { |
tom | 5a8779c | 2014-09-29 14:48:43 -0700 | [diff] [blame^] | 369 | log.debug("Unable to connect", e); |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | // Finds the least utilities IO loop. |
| 377 | private CommLoop findLeastUtilizedLoop() { |
| 378 | CommLoop leastUtilized = null; |
| 379 | int minCount = Integer.MAX_VALUE; |
| 380 | for (CommLoop loop : commLoops) { |
| 381 | int count = loop.streamCount(); |
| 382 | if (count == 0) { |
| 383 | return loop; |
| 384 | } |
| 385 | |
| 386 | if (count < minCount) { |
| 387 | leastUtilized = loop; |
| 388 | minCount = count; |
| 389 | } |
| 390 | } |
| 391 | return leastUtilized; |
| 392 | } |
| 393 | } |