tom | cb43d60 | 2014-09-28 22:46:16 -0700 | [diff] [blame] | 1 | package org.onlab.onos.ccc; |
| 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; |
tom | 1655562 | 2014-09-29 08:49:27 -0700 | [diff] [blame^] | 24 | import java.net.Socket; |
| 25 | import java.net.SocketAddress; |
tom | cb43d60 | 2014-09-28 22:46:16 -0700 | [diff] [blame] | 26 | import java.nio.channels.ByteChannel; |
| 27 | import java.nio.channels.ServerSocketChannel; |
tom | 1655562 | 2014-09-29 08:49:27 -0700 | [diff] [blame^] | 28 | import java.nio.channels.SocketChannel; |
tom | cb43d60 | 2014-09-28 22:46:16 -0700 | [diff] [blame] | 29 | import java.util.ArrayList; |
| 30 | import java.util.List; |
| 31 | import java.util.Map; |
| 32 | import java.util.Set; |
tom | 1655562 | 2014-09-29 08:49:27 -0700 | [diff] [blame^] | 33 | import java.util.Timer; |
| 34 | import java.util.TimerTask; |
tom | cb43d60 | 2014-09-28 22:46:16 -0700 | [diff] [blame] | 35 | import java.util.concurrent.ConcurrentHashMap; |
| 36 | import java.util.concurrent.ExecutorService; |
| 37 | import java.util.concurrent.Executors; |
| 38 | |
| 39 | import static java.net.InetAddress.getByAddress; |
| 40 | import static org.onlab.onos.cluster.ControllerNode.State; |
| 41 | import static org.onlab.packet.IpPrefix.valueOf; |
| 42 | import static org.onlab.util.Tools.namedThreads; |
| 43 | |
| 44 | /** |
| 45 | * Distributed implementation of the cluster nodes store. |
| 46 | */ |
| 47 | @Component(immediate = true) |
| 48 | @Service |
| 49 | public class DistributedClusterStore |
| 50 | extends AbstractStore<ClusterEvent, ClusterStoreDelegate> |
| 51 | implements ClusterStore { |
| 52 | |
| 53 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 54 | |
tom | 1655562 | 2014-09-29 08:49:27 -0700 | [diff] [blame^] | 55 | private static final long CONNECTION_CUSTODIAN_DELAY = 100L; |
| 56 | private static final long CONNECTION_CUSTODIAN_FREQUENCY = 5000; |
| 57 | |
tom | cb43d60 | 2014-09-28 22:46:16 -0700 | [diff] [blame] | 58 | private static final long SELECT_TIMEOUT = 50; |
| 59 | private static final int WORKERS = 3; |
tom | 1655562 | 2014-09-29 08:49:27 -0700 | [diff] [blame^] | 60 | private static final int INITIATORS = 2; |
tom | cb43d60 | 2014-09-28 22:46:16 -0700 | [diff] [blame] | 61 | private static final int COMM_BUFFER_SIZE = 16 * 1024; |
| 62 | private static final int COMM_IDLE_TIME = 500; |
| 63 | |
tom | 1655562 | 2014-09-29 08:49:27 -0700 | [diff] [blame^] | 64 | private static final boolean SO_NO_DELAY = false; |
| 65 | private static final int SO_SEND_BUFFER_SIZE = 128 * 1024; |
| 66 | private static final int SO_RCV_BUFFER_SIZE = 128 * 1024; |
| 67 | |
tom | cb43d60 | 2014-09-28 22:46:16 -0700 | [diff] [blame] | 68 | private DefaultControllerNode self; |
| 69 | private final Map<NodeId, DefaultControllerNode> nodes = new ConcurrentHashMap<>(); |
| 70 | private final Map<NodeId, State> states = new ConcurrentHashMap<>(); |
tom | 1655562 | 2014-09-29 08:49:27 -0700 | [diff] [blame^] | 71 | private final Map<NodeId, TLVMessageStream> streams = new ConcurrentHashMap<>(); |
| 72 | private final Map<SocketChannel, DefaultControllerNode> nodesByChannel = new ConcurrentHashMap<>(); |
tom | cb43d60 | 2014-09-28 22:46:16 -0700 | [diff] [blame] | 73 | |
| 74 | private final ExecutorService listenExecutor = |
tom | 1655562 | 2014-09-29 08:49:27 -0700 | [diff] [blame^] | 75 | Executors.newSingleThreadExecutor(namedThreads("onos-comm-listen")); |
tom | cb43d60 | 2014-09-28 22:46:16 -0700 | [diff] [blame] | 76 | private final ExecutorService commExecutors = |
tom | 1655562 | 2014-09-29 08:49:27 -0700 | [diff] [blame^] | 77 | Executors.newFixedThreadPool(WORKERS, namedThreads("onos-comm-cluster")); |
tom | cb43d60 | 2014-09-28 22:46:16 -0700 | [diff] [blame] | 78 | private final ExecutorService heartbeatExecutor = |
tom | 1655562 | 2014-09-29 08:49:27 -0700 | [diff] [blame^] | 79 | Executors.newSingleThreadExecutor(namedThreads("onos-comm-heartbeat")); |
| 80 | private final ExecutorService initiatorExecutors = |
| 81 | Executors.newFixedThreadPool(INITIATORS, namedThreads("onos-comm-initiator")); |
| 82 | |
| 83 | private final Timer timer = new Timer(); |
| 84 | private final TimerTask connectionCustodian = new ConnectionCustodian(); |
tom | cb43d60 | 2014-09-28 22:46:16 -0700 | [diff] [blame] | 85 | |
| 86 | private ListenLoop listenLoop; |
| 87 | private List<CommLoop> commLoops = new ArrayList<>(WORKERS); |
| 88 | |
| 89 | @Activate |
| 90 | public void activate() { |
| 91 | establishIdentity(); |
| 92 | startCommunications(); |
| 93 | startListening(); |
tom | 1655562 | 2014-09-29 08:49:27 -0700 | [diff] [blame^] | 94 | startInitiating(); |
tom | cb43d60 | 2014-09-28 22:46:16 -0700 | [diff] [blame] | 95 | log.info("Started"); |
| 96 | } |
| 97 | |
tom | 1655562 | 2014-09-29 08:49:27 -0700 | [diff] [blame^] | 98 | @Deactivate |
| 99 | public void deactivate() { |
| 100 | listenLoop.shutdown(); |
| 101 | for (CommLoop loop : commLoops) { |
| 102 | loop.shutdown(); |
| 103 | } |
| 104 | log.info("Stopped"); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | // Establishes the controller's own identity. |
| 109 | private void establishIdentity() { |
| 110 | IpPrefix ip = valueOf(System.getProperty("onos.ip", "127.0.1.1")); |
| 111 | self = new DefaultControllerNode(new NodeId(ip.toString()), ip); |
| 112 | nodes.put(self.id(), self); |
| 113 | } |
| 114 | |
| 115 | // Kicks off the IO loops. |
tom | cb43d60 | 2014-09-28 22:46:16 -0700 | [diff] [blame] | 116 | private void startCommunications() { |
| 117 | for (int i = 0; i < WORKERS; i++) { |
| 118 | try { |
| 119 | CommLoop loop = new CommLoop(); |
| 120 | commLoops.add(loop); |
| 121 | commExecutors.execute(loop); |
| 122 | } catch (IOException e) { |
| 123 | log.warn("Unable to start comm IO loop", e); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // Starts listening for connections from peer cluster members. |
| 129 | private void startListening() { |
| 130 | try { |
| 131 | listenLoop = new ListenLoop(self.ip(), self.tcpPort()); |
| 132 | listenExecutor.execute(listenLoop); |
| 133 | } catch (IOException e) { |
| 134 | log.error("Unable to listen for cluster connections", e); |
| 135 | } |
| 136 | } |
| 137 | |
tom | 1655562 | 2014-09-29 08:49:27 -0700 | [diff] [blame^] | 138 | /** |
| 139 | * Initiates open connection request and registers the pending socket |
| 140 | * channel with the given IO loop. |
| 141 | * |
| 142 | * @param loop loop with which the channel should be registered |
| 143 | * @throws java.io.IOException if the socket could not be open or connected |
| 144 | */ |
| 145 | private void openConnection(DefaultControllerNode node, CommLoop loop) throws IOException { |
| 146 | SocketAddress sa = new InetSocketAddress(getByAddress(node.ip().toOctets()), node.tcpPort()); |
| 147 | SocketChannel ch = SocketChannel.open(); |
| 148 | nodesByChannel.put(ch, node); |
| 149 | ch.configureBlocking(false); |
| 150 | loop.connectStream(ch); |
| 151 | ch.connect(sa); |
tom | cb43d60 | 2014-09-28 22:46:16 -0700 | [diff] [blame] | 152 | } |
| 153 | |
tom | 1655562 | 2014-09-29 08:49:27 -0700 | [diff] [blame^] | 154 | |
| 155 | // Attempts to connect to any nodes that do not have an associated connection. |
| 156 | private void startInitiating() { |
| 157 | timer.schedule(connectionCustodian, CONNECTION_CUSTODIAN_DELAY, CONNECTION_CUSTODIAN_FREQUENCY); |
tom | cb43d60 | 2014-09-28 22:46:16 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public ControllerNode getLocalNode() { |
| 162 | return self; |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public Set<ControllerNode> getNodes() { |
| 167 | ImmutableSet.Builder<ControllerNode> builder = ImmutableSet.builder(); |
| 168 | return builder.addAll(nodes.values()).build(); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public ControllerNode getNode(NodeId nodeId) { |
| 173 | return nodes.get(nodeId); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public State getState(NodeId nodeId) { |
| 178 | State state = states.get(nodeId); |
| 179 | return state == null ? State.INACTIVE : state; |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public ControllerNode addNode(NodeId nodeId, IpPrefix ip, int tcpPort) { |
| 184 | DefaultControllerNode node = new DefaultControllerNode(nodeId, ip, tcpPort); |
| 185 | nodes.put(nodeId, node); |
| 186 | return node; |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public void removeNode(NodeId nodeId) { |
| 191 | nodes.remove(nodeId); |
| 192 | } |
| 193 | |
| 194 | // Listens and accepts inbound connections from other cluster nodes. |
| 195 | private class ListenLoop extends AcceptorLoop { |
| 196 | ListenLoop(IpPrefix ip, int tcpPort) throws IOException { |
| 197 | super(SELECT_TIMEOUT, new InetSocketAddress(getByAddress(ip.toOctets()), tcpPort)); |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | protected void acceptConnection(ServerSocketChannel channel) throws IOException { |
tom | 1655562 | 2014-09-29 08:49:27 -0700 | [diff] [blame^] | 202 | SocketChannel sc = channel.accept(); |
| 203 | sc.configureBlocking(false); |
tom | cb43d60 | 2014-09-28 22:46:16 -0700 | [diff] [blame] | 204 | |
tom | 1655562 | 2014-09-29 08:49:27 -0700 | [diff] [blame^] | 205 | Socket so = sc.socket(); |
| 206 | so.setTcpNoDelay(SO_NO_DELAY); |
| 207 | so.setReceiveBufferSize(SO_RCV_BUFFER_SIZE); |
| 208 | so.setSendBufferSize(SO_SEND_BUFFER_SIZE); |
| 209 | |
| 210 | findLeastUtilizedLoop().acceptStream(sc); |
| 211 | log.info("Connected client"); |
tom | cb43d60 | 2014-09-28 22:46:16 -0700 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
| 215 | private class CommLoop extends IOLoop<TLVMessage, TLVMessageStream> { |
| 216 | CommLoop() throws IOException { |
| 217 | super(SELECT_TIMEOUT); |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | protected TLVMessageStream createStream(ByteChannel byteChannel) { |
| 222 | return new TLVMessageStream(this, byteChannel, COMM_BUFFER_SIZE, COMM_IDLE_TIME); |
| 223 | } |
| 224 | |
| 225 | @Override |
| 226 | protected void processMessages(List<TLVMessage> messages, MessageStream<TLVMessage> stream) { |
| 227 | |
| 228 | } |
tom | 1655562 | 2014-09-29 08:49:27 -0700 | [diff] [blame^] | 229 | |
| 230 | @Override |
| 231 | public TLVMessageStream acceptStream(SocketChannel channel) { |
| 232 | TLVMessageStream stream = super.acceptStream(channel); |
| 233 | try { |
| 234 | InetSocketAddress sa = (InetSocketAddress) channel.getRemoteAddress(); |
| 235 | log.info("Accepted a new connection from {}", IpPrefix.valueOf(sa.getAddress().getAddress())); |
| 236 | } catch (IOException e) { |
| 237 | log.warn("Unable to accept connection from an unknown end-point", e); |
| 238 | } |
| 239 | return stream; |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public TLVMessageStream connectStream(SocketChannel channel) { |
| 244 | TLVMessageStream stream = super.connectStream(channel); |
| 245 | DefaultControllerNode node = nodesByChannel.get(channel); |
| 246 | if (node != null) { |
| 247 | log.info("Opened connection to {}", node.id()); |
| 248 | streams.put(node.id(), stream); |
| 249 | } |
| 250 | return stream; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | |
| 255 | // Sweeps through all controller nodes and attempts to open connection to |
| 256 | // those that presently do not have one. |
| 257 | private class ConnectionCustodian extends TimerTask { |
| 258 | @Override |
| 259 | public void run() { |
| 260 | for (DefaultControllerNode node : nodes.values()) { |
| 261 | if (node != self && !streams.containsKey(node.id())) { |
| 262 | try { |
| 263 | openConnection(node, findLeastUtilizedLoop()); |
| 264 | } catch (IOException e) { |
| 265 | log.warn("Unable to connect", e); |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | // Finds the least utilities IO loop. |
| 273 | private CommLoop findLeastUtilizedLoop() { |
| 274 | CommLoop leastUtilized = null; |
| 275 | int minCount = Integer.MAX_VALUE; |
| 276 | for (CommLoop loop : commLoops) { |
| 277 | int count = loop.streamCount(); |
| 278 | if (count == 0) { |
| 279 | return loop; |
| 280 | } |
| 281 | |
| 282 | if (count < minCount) { |
| 283 | leastUtilized = loop; |
| 284 | minCount = count; |
| 285 | } |
| 286 | } |
| 287 | return leastUtilized; |
tom | cb43d60 | 2014-09-28 22:46:16 -0700 | [diff] [blame] | 288 | } |
| 289 | } |