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