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