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