tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.cluster.impl; |
| 2 | |
| 3 | import com.google.common.base.Optional; |
| 4 | import com.google.common.cache.LoadingCache; |
| 5 | import com.google.common.collect.ImmutableSet; |
| 6 | import com.hazelcast.core.IMap; |
| 7 | import org.apache.felix.scr.annotations.Activate; |
| 8 | import org.apache.felix.scr.annotations.Component; |
| 9 | import org.apache.felix.scr.annotations.Deactivate; |
| 10 | import org.apache.felix.scr.annotations.Reference; |
| 11 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 12 | import org.apache.felix.scr.annotations.Service; |
| 13 | import org.onlab.onos.cluster.ClusterService; |
| 14 | import org.onlab.onos.cluster.MastershipEvent; |
| 15 | import org.onlab.onos.cluster.MastershipStore; |
| 16 | import org.onlab.onos.cluster.NodeId; |
| 17 | import org.onlab.onos.net.DeviceId; |
| 18 | import org.onlab.onos.net.MastershipRole; |
| 19 | import org.onlab.onos.store.impl.AbsentInvalidatingLoadingCache; |
| 20 | import org.onlab.onos.store.impl.AbstractDistributedStore; |
| 21 | import org.onlab.onos.store.impl.OptionalCacheLoader; |
| 22 | |
| 23 | import java.util.Map; |
| 24 | import java.util.Objects; |
| 25 | import java.util.Set; |
| 26 | |
| 27 | import static com.google.common.cache.CacheBuilder.newBuilder; |
| 28 | |
| 29 | /** |
| 30 | * Distributed implementation of the cluster nodes store. |
| 31 | */ |
| 32 | @Component(immediate = true) |
| 33 | @Service |
| 34 | public class DistributedMastershipStore extends AbstractDistributedStore |
| 35 | implements MastershipStore { |
| 36 | |
| 37 | private IMap<byte[], byte[]> rawMasters; |
| 38 | private LoadingCache<DeviceId, Optional<NodeId>> masters; |
| 39 | |
| 40 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 41 | protected ClusterService clusterService; |
| 42 | |
| 43 | @Activate |
| 44 | public void activate() { |
| 45 | super.activate(); |
| 46 | |
| 47 | rawMasters = theInstance.getMap("masters"); |
| 48 | OptionalCacheLoader<DeviceId, NodeId> nodeLoader |
| 49 | = new OptionalCacheLoader<>(storeService, rawMasters); |
| 50 | masters = new AbsentInvalidatingLoadingCache<>(newBuilder().build(nodeLoader)); |
| 51 | rawMasters.addEntryListener(new RemoteEventHandler<>(masters), true); |
| 52 | |
| 53 | log.info("Started"); |
| 54 | } |
| 55 | |
| 56 | @Deactivate |
| 57 | public void deactivate() { |
| 58 | log.info("Stopped"); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public MastershipEvent setRole(NodeId nodeId, DeviceId deviceId, MastershipRole role) { |
| 63 | synchronized (this) { |
| 64 | NodeId currentMaster = getMaster(deviceId); |
| 65 | if (role == MastershipRole.MASTER && Objects.equals(currentMaster, nodeId)) { |
| 66 | return null; |
| 67 | } |
| 68 | |
| 69 | // FIXME: for now implementing semantics of setMaster |
| 70 | rawMasters.put(serialize(deviceId), serialize(nodeId)); |
| 71 | masters.put(deviceId, Optional.of(nodeId)); |
| 72 | return new MastershipEvent(MastershipEvent.Type.MASTER_CHANGED, deviceId, nodeId); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public NodeId getMaster(DeviceId deviceId) { |
| 78 | return masters.getUnchecked(deviceId).orNull(); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public Set<DeviceId> getDevices(NodeId nodeId) { |
| 83 | ImmutableSet.Builder<DeviceId> builder = ImmutableSet.builder(); |
| 84 | for (Map.Entry<DeviceId, Optional<NodeId>> entry : masters.asMap().entrySet()) { |
| 85 | if (nodeId.equals(entry.getValue().get())) { |
| 86 | builder.add(entry.getKey()); |
| 87 | } |
| 88 | } |
| 89 | return builder.build(); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public MastershipRole requestRole(DeviceId deviceId) { |
| 94 | // FIXME: for now we are 'selecting' as master whoever asks |
| 95 | setRole(clusterService.getLocalNode().id(), deviceId, MastershipRole.MASTER); |
| 96 | return MastershipRole.MASTER; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public MastershipRole getRole(NodeId nodeId, DeviceId deviceId) { |
| 101 | NodeId master = masters.getUnchecked(deviceId).orNull(); |
| 102 | return nodeId.equals(master) ? MastershipRole.MASTER : MastershipRole.STANDBY; |
| 103 | } |
| 104 | |
| 105 | } |