Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2014-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.cluster.impl; |
| 17 | |
| 18 | import java.util.ArrayList; |
| 19 | import java.util.Collection; |
| 20 | import java.util.Collections; |
| 21 | import java.util.HashSet; |
| 22 | import java.util.List; |
| 23 | import java.util.Set; |
| 24 | import java.util.concurrent.atomic.AtomicReference; |
| 25 | |
| 26 | import com.google.common.collect.Sets; |
| 27 | import org.apache.felix.scr.annotations.Activate; |
| 28 | import org.apache.felix.scr.annotations.Component; |
| 29 | import org.apache.felix.scr.annotations.Deactivate; |
| 30 | import org.apache.felix.scr.annotations.Reference; |
| 31 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 32 | import org.apache.felix.scr.annotations.Service; |
| 33 | import org.apache.karaf.system.SystemService; |
| 34 | import org.joda.time.DateTime; |
| 35 | import org.onlab.packet.IpAddress; |
| 36 | import org.onlab.util.Tools; |
| 37 | import org.onosproject.cluster.ClusterEvent; |
| 38 | import org.onosproject.cluster.ClusterEventListener; |
| 39 | import org.onosproject.cluster.ClusterMetadata; |
| 40 | import org.onosproject.cluster.ClusterMetadataAdminService; |
| 41 | import org.onosproject.cluster.ClusterMetadataDiff; |
| 42 | import org.onosproject.cluster.ClusterMetadataEvent; |
| 43 | import org.onosproject.cluster.ClusterMetadataEventListener; |
| 44 | import org.onosproject.cluster.ClusterMetadataService; |
| 45 | import org.onosproject.cluster.ClusterStore; |
| 46 | import org.onosproject.cluster.ClusterStoreDelegate; |
| 47 | import org.onosproject.cluster.ControllerNode; |
| 48 | import org.onosproject.cluster.DefaultPartition; |
| 49 | import org.onosproject.cluster.UnifiedClusterAdminService; |
| 50 | import org.onosproject.cluster.UnifiedClusterService; |
| 51 | import org.onosproject.cluster.NodeId; |
| 52 | import org.onosproject.cluster.Partition; |
| 53 | import org.onosproject.cluster.PartitionId; |
| 54 | import org.onosproject.core.Version; |
| 55 | import org.onosproject.event.AbstractListenerManager; |
| 56 | import org.slf4j.Logger; |
| 57 | |
| 58 | import static com.google.common.base.Preconditions.checkArgument; |
| 59 | import static com.google.common.base.Preconditions.checkNotNull; |
| 60 | import static org.onosproject.security.AppGuard.checkPermission; |
| 61 | import static org.onosproject.security.AppPermission.Type.CLUSTER_READ; |
| 62 | import static org.slf4j.LoggerFactory.getLogger; |
| 63 | |
| 64 | /** |
| 65 | * Implementation of the cluster service. |
| 66 | */ |
| 67 | @Component(immediate = true) |
| 68 | @Service |
| 69 | public class UnifiedClusterManager |
| 70 | extends AbstractListenerManager<ClusterEvent, ClusterEventListener> |
| 71 | implements UnifiedClusterService, UnifiedClusterAdminService { |
| 72 | |
| 73 | public static final String INSTANCE_ID_NULL = "Instance ID cannot be null"; |
| 74 | private static final int DEFAULT_PARTITION_SIZE = 3; |
| 75 | private final Logger log = getLogger(getClass()); |
| 76 | |
| 77 | private ClusterStoreDelegate delegate = new InternalStoreDelegate(); |
| 78 | |
| 79 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 80 | protected ClusterMetadataService clusterMetadataService; |
| 81 | |
| 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 83 | protected ClusterMetadataAdminService clusterMetadataAdminService; |
| 84 | |
| 85 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 86 | protected ClusterStore store; |
| 87 | |
| 88 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 89 | protected SystemService systemService; |
| 90 | |
| 91 | private final AtomicReference<ClusterMetadata> currentMetadata = new AtomicReference<>(); |
| 92 | private final InternalClusterMetadataListener metadataListener = new InternalClusterMetadataListener(); |
| 93 | |
| 94 | @Activate |
| 95 | public void activate() { |
| 96 | store.setDelegate(delegate); |
| 97 | eventDispatcher.addSink(ClusterEvent.class, listenerRegistry); |
| 98 | clusterMetadataService.addListener(metadataListener); |
| 99 | processMetadata(clusterMetadataService.getClusterMetadata()); |
| 100 | log.info("Started"); |
| 101 | } |
| 102 | |
| 103 | @Deactivate |
| 104 | public void deactivate() { |
| 105 | clusterMetadataService.removeListener(metadataListener); |
| 106 | store.unsetDelegate(delegate); |
| 107 | eventDispatcher.removeSink(ClusterEvent.class); |
| 108 | log.info("Stopped"); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public ControllerNode getLocalNode() { |
| 113 | checkPermission(CLUSTER_READ); |
| 114 | return store.getLocalNode(); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public Set<ControllerNode> getNodes() { |
| 119 | checkPermission(CLUSTER_READ); |
| 120 | return store.getNodes(); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public ControllerNode getNode(NodeId nodeId) { |
| 125 | checkPermission(CLUSTER_READ); |
| 126 | checkNotNull(nodeId, INSTANCE_ID_NULL); |
| 127 | return store.getNode(nodeId); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public ControllerNode.State getState(NodeId nodeId) { |
| 132 | checkPermission(CLUSTER_READ); |
| 133 | checkNotNull(nodeId, INSTANCE_ID_NULL); |
| 134 | return store.getState(nodeId); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public Version getVersion(NodeId nodeId) { |
| 139 | checkPermission(CLUSTER_READ); |
| 140 | checkNotNull(nodeId, INSTANCE_ID_NULL); |
| 141 | return store.getVersion(nodeId); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public void markFullyStarted(boolean started) { |
| 146 | store.markFullyStarted(started); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public DateTime getLastUpdated(NodeId nodeId) { |
| 151 | checkPermission(CLUSTER_READ); |
| 152 | return store.getLastUpdated(nodeId); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public void formCluster(Set<ControllerNode> nodes) { |
| 157 | formCluster(nodes, DEFAULT_PARTITION_SIZE); |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public void formCluster(Set<ControllerNode> nodes, int partitionSize) { |
| 162 | checkNotNull(nodes, "Nodes cannot be null"); |
| 163 | checkArgument(!nodes.isEmpty(), "Nodes cannot be empty"); |
| 164 | |
| 165 | ClusterMetadata metadata = new ClusterMetadata("default", nodes, buildDefaultPartitions(nodes, partitionSize)); |
| 166 | clusterMetadataAdminService.setClusterMetadata(metadata); |
| 167 | try { |
| 168 | log.warn("Shutting down container for cluster reconfiguration!"); |
| 169 | // Clean up persistent state associated with previous cluster configuration. |
| 170 | Tools.removeDirectory(System.getProperty("karaf.data") + "/partitions"); |
| 171 | systemService.reboot("now", SystemService.Swipe.NONE); |
| 172 | } catch (Exception e) { |
| 173 | log.error("Unable to reboot container", e); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public ControllerNode addNode(NodeId nodeId, IpAddress ip, int tcpPort) { |
| 179 | checkNotNull(nodeId, INSTANCE_ID_NULL); |
| 180 | checkNotNull(ip, "IP address cannot be null"); |
| 181 | checkArgument(tcpPort > 5000, "TCP port must be > 5000"); |
| 182 | return store.addNode(nodeId, ip, tcpPort); |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public void removeNode(NodeId nodeId) { |
| 187 | checkNotNull(nodeId, INSTANCE_ID_NULL); |
| 188 | store.removeNode(nodeId); |
| 189 | } |
| 190 | |
| 191 | // Store delegate to re-post events emitted from the store. |
| 192 | private class InternalStoreDelegate implements ClusterStoreDelegate { |
| 193 | @Override |
| 194 | public void notify(ClusterEvent event) { |
| 195 | post(event); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | private static Set<Partition> buildDefaultPartitions(Collection<ControllerNode> nodes, int partitionSize) { |
| 200 | List<ControllerNode> sorted = new ArrayList<>(nodes); |
| 201 | Collections.sort(sorted, (o1, o2) -> o1.id().toString().compareTo(o2.id().toString())); |
| 202 | Set<Partition> partitions = Sets.newHashSet(); |
| 203 | // add partitions |
| 204 | int length = nodes.size(); |
| 205 | int count = Math.min(partitionSize, length); |
| 206 | for (int i = 0; i < length; i++) { |
| 207 | int index = i; |
| 208 | Set<NodeId> set = new HashSet<>(count); |
| 209 | for (int j = 0; j < count; j++) { |
| 210 | set.add(sorted.get((i + j) % length).id()); |
| 211 | } |
| 212 | partitions.add(new DefaultPartition(PartitionId.from((index + 1)), set)); |
| 213 | } |
| 214 | return partitions; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Processes metadata by adding and removing nodes from the cluster. |
| 219 | */ |
| 220 | private synchronized void processMetadata(ClusterMetadata metadata) { |
| 221 | try { |
| 222 | ClusterMetadataDiff examiner = |
| 223 | new ClusterMetadataDiff(currentMetadata.get(), metadata); |
| 224 | examiner.nodesAdded().forEach(node -> addNode(node.id(), node.ip(), node.tcpPort())); |
| 225 | examiner.nodesRemoved().forEach(this::removeNode); |
| 226 | } finally { |
| 227 | currentMetadata.set(metadata); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | private class InternalClusterMetadataListener implements ClusterMetadataEventListener { |
| 232 | @Override |
| 233 | public void event(ClusterMetadataEvent event) { |
| 234 | processMetadata(event.subject()); |
| 235 | } |
| 236 | } |
| 237 | } |