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