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