Yuta HIGUCHI | b5df76d | 2014-09-27 20:54:00 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.common; |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 2 | |
| 3 | import com.google.common.base.Optional; |
| 4 | import com.google.common.cache.LoadingCache; |
| 5 | import com.hazelcast.core.EntryAdapter; |
| 6 | import com.hazelcast.core.EntryEvent; |
| 7 | import com.hazelcast.core.HazelcastInstance; |
| 8 | import com.hazelcast.core.MapEvent; |
Yuta HIGUCHI | fec9e19 | 2014-09-28 14:58:02 -0700 | [diff] [blame] | 9 | import com.hazelcast.core.Member; |
Yuta HIGUCHI | b5df76d | 2014-09-27 20:54:00 -0700 | [diff] [blame] | 10 | |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 11 | import org.apache.felix.scr.annotations.Activate; |
| 12 | import org.apache.felix.scr.annotations.Component; |
| 13 | import org.apache.felix.scr.annotations.Reference; |
| 14 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 15 | import org.onlab.onos.event.Event; |
| 16 | import org.onlab.onos.store.AbstractStore; |
| 17 | import org.onlab.onos.store.StoreDelegate; |
Yuta HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 18 | import org.onlab.onos.store.serializers.KryoSerializationService; |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 19 | import org.slf4j.Logger; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static org.slf4j.LoggerFactory.getLogger; |
| 23 | |
| 24 | /** |
| 25 | * Abstraction of a distributed store based on Hazelcast. |
| 26 | */ |
| 27 | @Component(componentAbstract = true) |
Yuta HIGUCHI | 2e96389 | 2014-09-27 13:00:39 -0700 | [diff] [blame] | 28 | public abstract class AbstractHazelcastStore<E extends Event, D extends StoreDelegate<E>> |
tom | ca55e64 | 2014-09-24 18:28:38 -0700 | [diff] [blame] | 29 | extends AbstractStore<E, D> { |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 30 | |
| 31 | protected final Logger log = getLogger(getClass()); |
| 32 | |
| 33 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 34 | protected StoreService storeService; |
| 35 | |
Yuta HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 36 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 37 | protected KryoSerializationService kryoSerializationService; |
| 38 | |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 39 | protected HazelcastInstance theInstance; |
| 40 | |
| 41 | @Activate |
| 42 | public void activate() { |
| 43 | theInstance = storeService.getHazelcastInstance(); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Serializes the specified object using the backing store service. |
| 48 | * |
| 49 | * @param obj object to be serialized |
| 50 | * @return serialized object |
| 51 | */ |
| 52 | protected byte[] serialize(Object obj) { |
Yuta HIGUCHI | 53a285d | 2014-10-06 23:58:01 -0700 | [diff] [blame^] | 53 | return kryoSerializationService.encode(obj); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Deserializes the specified object using the backing store service. |
| 58 | * |
| 59 | * @param bytes bytes to be deserialized |
| 60 | * @param <T> type of object |
| 61 | * @return deserialized object |
| 62 | */ |
| 63 | protected <T> T deserialize(byte[] bytes) { |
Yuta HIGUCHI | 53a285d | 2014-10-06 23:58:01 -0700 | [diff] [blame^] | 64 | return kryoSerializationService.decode(bytes); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | |
| 68 | /** |
| 69 | * An IMap entry listener, which reflects each remote event to the cache. |
| 70 | * |
| 71 | * @param <K> IMap key type after deserialization |
| 72 | * @param <V> IMap value type after deserialization |
| 73 | */ |
Yuta HIGUCHI | fec9e19 | 2014-09-28 14:58:02 -0700 | [diff] [blame] | 74 | public class RemoteCacheEventHandler<K, V> extends EntryAdapter<byte[], byte[]> { |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 75 | |
Yuta HIGUCHI | fec9e19 | 2014-09-28 14:58:02 -0700 | [diff] [blame] | 76 | private final Member localMember; |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 77 | private LoadingCache<K, Optional<V>> cache; |
| 78 | |
| 79 | /** |
| 80 | * Constructor. |
| 81 | * |
| 82 | * @param cache cache to update |
| 83 | */ |
Yuta HIGUCHI | fec9e19 | 2014-09-28 14:58:02 -0700 | [diff] [blame] | 84 | public RemoteCacheEventHandler(LoadingCache<K, Optional<V>> cache) { |
| 85 | this.localMember = theInstance.getCluster().getLocalMember(); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 86 | this.cache = checkNotNull(cache); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public void mapCleared(MapEvent event) { |
Yuta HIGUCHI | fec9e19 | 2014-09-28 14:58:02 -0700 | [diff] [blame] | 91 | if (localMember.equals(event.getMember())) { |
| 92 | // ignore locally triggered event |
| 93 | return; |
| 94 | } |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 95 | cache.invalidateAll(); |
| 96 | } |
| 97 | |
| 98 | @Override |
tom | ca55e64 | 2014-09-24 18:28:38 -0700 | [diff] [blame] | 99 | public void entryAdded(EntryEvent<byte[], byte[]> event) { |
Yuta HIGUCHI | fec9e19 | 2014-09-28 14:58:02 -0700 | [diff] [blame] | 100 | if (localMember.equals(event.getMember())) { |
| 101 | // ignore locally triggered event |
| 102 | return; |
| 103 | } |
tom | ca55e64 | 2014-09-24 18:28:38 -0700 | [diff] [blame] | 104 | K key = deserialize(event.getKey()); |
| 105 | V newVal = deserialize(event.getValue()); |
| 106 | Optional<V> newValue = Optional.of(newVal); |
| 107 | cache.asMap().putIfAbsent(key, newValue); |
| 108 | onAdd(key, newVal); |
| 109 | } |
| 110 | |
| 111 | @Override |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 112 | public void entryUpdated(EntryEvent<byte[], byte[]> event) { |
Yuta HIGUCHI | fec9e19 | 2014-09-28 14:58:02 -0700 | [diff] [blame] | 113 | if (localMember.equals(event.getMember())) { |
| 114 | // ignore locally triggered event |
| 115 | return; |
| 116 | } |
tom | ca55e64 | 2014-09-24 18:28:38 -0700 | [diff] [blame] | 117 | K key = deserialize(event.getKey()); |
| 118 | V oldVal = deserialize(event.getOldValue()); |
Yuta HIGUCHI | bb1fc72 | 2014-09-24 00:00:13 -0700 | [diff] [blame] | 119 | Optional<V> oldValue = Optional.fromNullable(oldVal); |
tom | ca55e64 | 2014-09-24 18:28:38 -0700 | [diff] [blame] | 120 | V newVal = deserialize(event.getValue()); |
Yuta HIGUCHI | bb1fc72 | 2014-09-24 00:00:13 -0700 | [diff] [blame] | 121 | Optional<V> newValue = Optional.of(newVal); |
| 122 | cache.asMap().replace(key, oldValue, newValue); |
Yuta HIGUCHI | a8a53eb | 2014-09-25 17:47:55 -0700 | [diff] [blame] | 123 | onUpdate(key, oldVal, newVal); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public void entryRemoved(EntryEvent<byte[], byte[]> event) { |
Yuta HIGUCHI | fec9e19 | 2014-09-28 14:58:02 -0700 | [diff] [blame] | 128 | if (localMember.equals(event.getMember())) { |
| 129 | // ignore locally triggered event |
| 130 | return; |
| 131 | } |
tom | ca55e64 | 2014-09-24 18:28:38 -0700 | [diff] [blame] | 132 | K key = deserialize(event.getKey()); |
Yuta HIGUCHI | f547970 | 2014-09-25 00:09:24 -0700 | [diff] [blame] | 133 | V val = deserialize(event.getOldValue()); |
tom | ca55e64 | 2014-09-24 18:28:38 -0700 | [diff] [blame] | 134 | cache.invalidate(key); |
| 135 | onRemove(key, val); |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 136 | } |
| 137 | |
tom | ca55e64 | 2014-09-24 18:28:38 -0700 | [diff] [blame] | 138 | /** |
| 139 | * Cache entry addition hook. |
| 140 | * |
| 141 | * @param key new key |
| 142 | * @param newVal new value |
| 143 | */ |
| 144 | protected void onAdd(K key, V newVal) { |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Cache entry update hook. |
| 149 | * |
| 150 | * @param key new key |
Yuta HIGUCHI | a8a53eb | 2014-09-25 17:47:55 -0700 | [diff] [blame] | 151 | * @param oldValue old value |
tom | ca55e64 | 2014-09-24 18:28:38 -0700 | [diff] [blame] | 152 | * @param newVal new value |
| 153 | */ |
Yuta HIGUCHI | a8a53eb | 2014-09-25 17:47:55 -0700 | [diff] [blame] | 154 | protected void onUpdate(K key, V oldValue, V newVal) { |
tom | ca55e64 | 2014-09-24 18:28:38 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Cache entry remove hook. |
| 159 | * |
| 160 | * @param key new key |
| 161 | * @param val old value |
| 162 | */ |
| 163 | protected void onRemove(K key, V val) { |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
Yuta HIGUCHI | fec9e19 | 2014-09-28 14:58:02 -0700 | [diff] [blame] | 167 | /** |
| 168 | * Distributed object remote event entry listener. |
| 169 | * |
| 170 | * @param <K> Entry key type after deserialization |
| 171 | * @param <V> Entry value type after deserialization |
| 172 | */ |
| 173 | public class RemoteEventHandler<K, V> extends EntryAdapter<byte[], byte[]> { |
| 174 | |
| 175 | private final Member localMember; |
| 176 | |
| 177 | public RemoteEventHandler() { |
| 178 | this.localMember = theInstance.getCluster().getLocalMember(); |
| 179 | } |
| 180 | @Override |
| 181 | public void entryAdded(EntryEvent<byte[], byte[]> event) { |
| 182 | if (localMember.equals(event.getMember())) { |
| 183 | // ignore locally triggered event |
| 184 | return; |
| 185 | } |
| 186 | K key = deserialize(event.getKey()); |
| 187 | V newVal = deserialize(event.getValue()); |
| 188 | onAdd(key, newVal); |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public void entryRemoved(EntryEvent<byte[], byte[]> event) { |
| 193 | if (localMember.equals(event.getMember())) { |
| 194 | // ignore locally triggered event |
| 195 | return; |
| 196 | } |
| 197 | K key = deserialize(event.getKey()); |
| 198 | V val = deserialize(event.getValue()); |
| 199 | onRemove(key, val); |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public void entryUpdated(EntryEvent<byte[], byte[]> event) { |
| 204 | if (localMember.equals(event.getMember())) { |
| 205 | // ignore locally triggered event |
| 206 | return; |
| 207 | } |
| 208 | K key = deserialize(event.getKey()); |
| 209 | V oldVal = deserialize(event.getOldValue()); |
| 210 | V newVal = deserialize(event.getValue()); |
| 211 | onUpdate(key, oldVal, newVal); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Remote entry addition hook. |
| 216 | * |
| 217 | * @param key new key |
| 218 | * @param newVal new value |
| 219 | */ |
| 220 | protected void onAdd(K key, V newVal) { |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Remote entry update hook. |
| 225 | * |
| 226 | * @param key new key |
| 227 | * @param oldValue old value |
| 228 | * @param newVal new value |
| 229 | */ |
| 230 | protected void onUpdate(K key, V oldValue, V newVal) { |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Remote entry remove hook. |
| 235 | * |
| 236 | * @param key new key |
| 237 | * @param val old value |
| 238 | */ |
| 239 | protected void onRemove(K key, V val) { |
| 240 | } |
| 241 | } |
| 242 | |
tom | b41d1ac | 2014-09-24 01:51:24 -0700 | [diff] [blame] | 243 | } |