tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.cluster.impl; |
| 2 | |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 3 | import com.google.common.cache.Cache; |
| 4 | import com.google.common.cache.CacheBuilder; |
| 5 | import com.google.common.cache.RemovalListener; |
| 6 | import com.google.common.cache.RemovalNotification; |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 7 | import com.google.common.collect.ImmutableSet; |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 8 | |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 9 | import org.apache.felix.scr.annotations.Activate; |
| 10 | import org.apache.felix.scr.annotations.Component; |
| 11 | import org.apache.felix.scr.annotations.Deactivate; |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 12 | import org.apache.felix.scr.annotations.Reference; |
| 13 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 14 | import org.apache.felix.scr.annotations.Service; |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 15 | import org.onlab.onos.cluster.ClusterEvent; |
| 16 | import org.onlab.onos.cluster.ClusterStore; |
| 17 | import org.onlab.onos.cluster.ClusterStoreDelegate; |
| 18 | import org.onlab.onos.cluster.ControllerNode; |
| 19 | import org.onlab.onos.cluster.DefaultControllerNode; |
| 20 | import org.onlab.onos.cluster.NodeId; |
| 21 | import org.onlab.onos.store.AbstractStore; |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 22 | import org.onlab.onos.store.cluster.messaging.ClusterCommunicationAdminService; |
| 23 | import org.onlab.onos.store.cluster.messaging.impl.OnosClusterCommunicationManager; |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 24 | import org.onlab.packet.IpPrefix; |
| 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
| 28 | import java.io.IOException; |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 29 | import java.util.Map; |
| 30 | import java.util.Set; |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 31 | import java.util.concurrent.ConcurrentHashMap; |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 32 | import java.util.concurrent.TimeUnit; |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 33 | |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 34 | import static org.onlab.onos.cluster.ControllerNode.State; |
| 35 | import static org.onlab.packet.IpPrefix.valueOf; |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Distributed implementation of the cluster nodes store. |
| 39 | */ |
| 40 | @Component(immediate = true) |
| 41 | @Service |
| 42 | public class DistributedClusterStore |
| 43 | extends AbstractStore<ClusterEvent, ClusterStoreDelegate> |
| 44 | implements ClusterStore { |
| 45 | |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 46 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 47 | |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 48 | private DefaultControllerNode localNode; |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 49 | private final Map<NodeId, DefaultControllerNode> nodes = new ConcurrentHashMap<>(); |
| 50 | private final Map<NodeId, State> states = new ConcurrentHashMap<>(); |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 51 | private final Cache<NodeId, ControllerNode> livenessCache = CacheBuilder.newBuilder() |
| 52 | .maximumSize(1000) |
| 53 | .expireAfterWrite(OnosClusterCommunicationManager.HEART_BEAT_INTERVAL_MILLIS * 3, TimeUnit.MILLISECONDS) |
| 54 | .removalListener(new LivenessCacheRemovalListener()).build(); |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 55 | |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 56 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 57 | private ClusterCommunicationAdminService clusterCommunicationAdminService; |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 58 | |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 59 | private final ClusterNodesDelegate nodesDelegate = new InnerNodesDelegate(); |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 60 | |
| 61 | @Activate |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 62 | public void activate() throws IOException { |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 63 | loadClusterDefinition(); |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 64 | establishSelfIdentity(); |
tom | 8158314 | 2014-09-30 01:40:29 -0700 | [diff] [blame] | 65 | |
| 66 | // Start-up the comm service and prime it with the loaded nodes. |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 67 | clusterCommunicationAdminService.initialize(localNode, nodesDelegate); |
tom | 8158314 | 2014-09-30 01:40:29 -0700 | [diff] [blame] | 68 | for (DefaultControllerNode node : nodes.values()) { |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 69 | clusterCommunicationAdminService.addNode(node); |
tom | 8158314 | 2014-09-30 01:40:29 -0700 | [diff] [blame] | 70 | } |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 71 | log.info("Started"); |
| 72 | } |
| 73 | |
| 74 | @Deactivate |
| 75 | public void deactivate() { |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 76 | log.info("Stopped"); |
| 77 | } |
| 78 | |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 79 | /** |
| 80 | * Loads the cluster definition file. |
| 81 | */ |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 82 | private void loadClusterDefinition() { |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 83 | ClusterDefinitionStore cds = new ClusterDefinitionStore("../config/cluster.json"); |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 84 | try { |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 85 | Set<DefaultControllerNode> storedNodes = cds.read(); |
| 86 | for (DefaultControllerNode node : storedNodes) { |
| 87 | nodes.put(node.id(), node); |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 88 | } |
| 89 | } catch (IOException e) { |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 90 | log.error("Unable to read cluster definitions", e); |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | /** |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 95 | * Determines who the local controller node is. |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 96 | */ |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 97 | private void establishSelfIdentity() { |
| 98 | // Establishes the controller's own identity. |
| 99 | IpPrefix ip = valueOf(System.getProperty("onos.ip", "127.0.1.1")); |
| 100 | localNode = nodes.get(new NodeId(ip.toString())); |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 101 | |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 102 | // As a fall-back, let's make sure we at least know who we are. |
| 103 | if (localNode == null) { |
| 104 | localNode = new DefaultControllerNode(new NodeId(ip.toString()), ip); |
| 105 | nodes.put(localNode.id(), localNode); |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 106 | } |
tom | 8158314 | 2014-09-30 01:40:29 -0700 | [diff] [blame] | 107 | states.put(localNode.id(), State.ACTIVE); |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public ControllerNode getLocalNode() { |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 112 | return localNode; |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public Set<ControllerNode> getNodes() { |
| 117 | ImmutableSet.Builder<ControllerNode> builder = ImmutableSet.builder(); |
| 118 | return builder.addAll(nodes.values()).build(); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public ControllerNode getNode(NodeId nodeId) { |
| 123 | return nodes.get(nodeId); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public State getState(NodeId nodeId) { |
| 128 | State state = states.get(nodeId); |
| 129 | return state == null ? State.INACTIVE : state; |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public ControllerNode addNode(NodeId nodeId, IpPrefix ip, int tcpPort) { |
| 134 | DefaultControllerNode node = new DefaultControllerNode(nodeId, ip, tcpPort); |
| 135 | nodes.put(nodeId, node); |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 136 | clusterCommunicationAdminService.addNode(node); |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 137 | return node; |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public void removeNode(NodeId nodeId) { |
tom | d33e640 | 2014-09-30 03:14:43 -0700 | [diff] [blame] | 142 | if (nodeId.equals(localNode.id())) { |
tom | d33e640 | 2014-09-30 03:14:43 -0700 | [diff] [blame] | 143 | nodes.clear(); |
tom | 28e1fa2 | 2014-09-30 10:38:21 -0700 | [diff] [blame] | 144 | nodes.put(localNode.id(), localNode); |
| 145 | |
tom | d33e640 | 2014-09-30 03:14:43 -0700 | [diff] [blame] | 146 | } else { |
| 147 | // Remove the other node. |
| 148 | DefaultControllerNode node = nodes.remove(nodeId); |
| 149 | if (node != null) { |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 150 | clusterCommunicationAdminService.removeNode(node); |
tom | d33e640 | 2014-09-30 03:14:43 -0700 | [diff] [blame] | 151 | } |
tom | 5a8779c | 2014-09-29 14:48:43 -0700 | [diff] [blame] | 152 | } |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 153 | } |
| 154 | |
tom | 1d416c5 | 2014-09-29 20:55:24 -0700 | [diff] [blame] | 155 | // Entity to handle back calls from the connection manager. |
| 156 | private class InnerNodesDelegate implements ClusterNodesDelegate { |
| 157 | @Override |
tom | 8158314 | 2014-09-30 01:40:29 -0700 | [diff] [blame] | 158 | public DefaultControllerNode nodeDetected(NodeId nodeId, IpPrefix ip, int tcpPort) { |
| 159 | DefaultControllerNode node = nodes.get(nodeId); |
| 160 | if (node == null) { |
| 161 | node = (DefaultControllerNode) addNode(nodeId, ip, tcpPort); |
| 162 | } |
| 163 | states.put(nodeId, State.ACTIVE); |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 164 | livenessCache.put(nodeId, node); |
tom | 8158314 | 2014-09-30 01:40:29 -0700 | [diff] [blame] | 165 | return node; |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 166 | } |
tom | 28e1fa2 | 2014-09-30 10:38:21 -0700 | [diff] [blame] | 167 | |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 168 | @Override |
tom | 8158314 | 2014-09-30 01:40:29 -0700 | [diff] [blame] | 169 | public void nodeVanished(NodeId nodeId) { |
| 170 | states.put(nodeId, State.INACTIVE); |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 171 | } |
tom | d33e640 | 2014-09-30 03:14:43 -0700 | [diff] [blame] | 172 | |
| 173 | @Override |
| 174 | public void nodeRemoved(NodeId nodeId) { |
| 175 | removeNode(nodeId); |
| 176 | } |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 177 | } |
tom | 8158314 | 2014-09-30 01:40:29 -0700 | [diff] [blame] | 178 | |
Madan Jampani | 890bc35 | 2014-10-01 22:35:29 -0700 | [diff] [blame] | 179 | private class LivenessCacheRemovalListener implements RemovalListener<NodeId, ControllerNode> { |
| 180 | |
| 181 | @Override |
| 182 | public void onRemoval(RemovalNotification<NodeId, ControllerNode> entry) { |
| 183 | NodeId nodeId = entry.getKey(); |
| 184 | log.warn("Failed to receive heartbeats from controller: " + nodeId); |
| 185 | nodesDelegate.nodeVanished(nodeId); |
| 186 | } |
| 187 | } |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame] | 188 | } |