Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.datagrid; |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 2 | |
Yuta HIGUCHI | 62d8637 | 2014-07-03 12:04:13 -0700 | [diff] [blame] | 3 | import java.util.Arrays; |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 4 | import java.util.Collection; |
Yuta HIGUCHI | a5dcd19 | 2014-06-06 18:00:35 -0700 | [diff] [blame] | 5 | import java.util.Iterator; |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 6 | import java.util.LinkedList; |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 7 | import java.util.Set; |
Pavlin Radoslavov | 1c8d809 | 2014-02-14 15:47:24 -0800 | [diff] [blame] | 8 | import java.util.concurrent.CopyOnWriteArrayList; |
Pavlin Radoslavov | bcf1433 | 2014-03-27 18:15:30 -0700 | [diff] [blame] | 9 | import java.util.concurrent.TimeUnit; |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 10 | |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 11 | import net.onrc.onos.core.util.serializers.KryoFactory; |
| 12 | |
Jonathan Hart | 14c4a76 | 2014-04-15 10:35:57 -0700 | [diff] [blame] | 13 | import org.slf4j.Logger; |
| 14 | import org.slf4j.LoggerFactory; |
| 15 | |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 16 | import com.esotericsoftware.kryo.Kryo; |
| 17 | import com.esotericsoftware.kryo.io.Input; |
| 18 | import com.esotericsoftware.kryo.io.Output; |
| 19 | import com.hazelcast.core.EntryEvent; |
| 20 | import com.hazelcast.core.EntryListener; |
| 21 | import com.hazelcast.core.HazelcastInstance; |
| 22 | import com.hazelcast.core.IMap; |
| 23 | |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 24 | /** |
| 25 | * A datagrid event channel that uses Hazelcast as a datagrid. |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 26 | * |
| 27 | * @param <K> The class type of the key. |
| 28 | * @param <V> The class type of the value. |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 29 | */ |
| 30 | public class HazelcastEventChannel<K, V> implements IEventChannel<K, V> { |
Jonathan Hart | 14c4a76 | 2014-04-15 10:35:57 -0700 | [diff] [blame] | 31 | private static final Logger log = |
| 32 | LoggerFactory.getLogger(HazelcastEventChannel.class); |
| 33 | |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 34 | private final HazelcastInstance hazelcastInstance; // The Hazelcast instance |
| 35 | private final String channelName; // The event channel name |
Pavlin Radoslavov | e561a4c | 2014-04-01 14:10:55 -0700 | [diff] [blame] | 36 | private final Class<K> typeK; // The class type of the key |
| 37 | private final Class<V> typeV; // The class type of the value |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 38 | private IMap<K, byte[]> channelMap; // The Hazelcast channel map |
Pavlin Radoslavov | 1c8d809 | 2014-02-14 15:47:24 -0800 | [diff] [blame] | 39 | // The channel listeners |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 40 | private final CopyOnWriteArrayList<IEventChannelListener<K, V>> listeners = |
| 41 | new CopyOnWriteArrayList<>(); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 42 | |
| 43 | // The map entry listener |
Pavlin Radoslavov | e561a4c | 2014-04-01 14:10:55 -0700 | [diff] [blame] | 44 | private final EntryListener<K, byte[]> mapEntryListener = new MapEntryListener(); |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 45 | private String mapListenerId; // The map listener ID |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 46 | |
| 47 | // TODO: We should use a single global KryoFactory instance |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 48 | private final KryoFactory kryoFactory = new KryoFactory(); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 49 | |
| 50 | // Maximum serialized event size |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 51 | private static final int MAX_BUFFER_SIZE = 64 * 1024; |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * Constructor for a given event channel name. |
| 55 | * |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 56 | * @param newHazelcastInstance the Hazelcast instance to use. |
| 57 | * @param newChannelName the event channel name. |
| 58 | * @param newTypeK the type of the Key in the Key-Value store. |
| 59 | * @param newTypeV the type of the Value in the Key-Value store. |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 60 | */ |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 61 | public HazelcastEventChannel(HazelcastInstance newHazelcastInstance, |
| 62 | String newChannelName, Class<K> newTypeK, |
| 63 | Class<V> newTypeV) { |
| 64 | hazelcastInstance = newHazelcastInstance; |
| 65 | channelName = newChannelName; |
| 66 | typeK = newTypeK; |
| 67 | typeV = newTypeV; |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Verify the key and value types of a channel. |
| 72 | * |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 73 | * @param typeKToVerify the type of the key to verify. |
| 74 | * @param typeVToVerify the type of the value to verify. |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 75 | * @return true if the key and value types of the channel match, |
| 76 | * otherwise false. |
| 77 | */ |
| 78 | @Override |
Jonathan Hart | 14c4a76 | 2014-04-15 10:35:57 -0700 | [diff] [blame] | 79 | public boolean verifyKeyValueTypes(Class<?> typeKToVerify, |
| 80 | Class<?> typeVToVerify) { |
Ray Milkey | 7301965 | 2014-03-24 11:12:44 -0700 | [diff] [blame] | 81 | return (typeK.equals(typeKToVerify)) && (typeV.equals(typeVToVerify)); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Cleanup and destroy the channel. |
| 86 | */ |
| 87 | @Override |
| 88 | protected void finalize() { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 89 | shutdown(); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Startup the channel operation. |
| 94 | */ |
| 95 | @Override |
| 96 | public void startup() { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 97 | if (channelMap == null) { |
| 98 | channelMap = hazelcastInstance.getMap(channelName); |
| 99 | mapListenerId = channelMap.addEntryListener(mapEntryListener, |
| 100 | true); |
| 101 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Shutdown the channel operation. |
| 106 | */ |
| 107 | @Override |
| 108 | public void shutdown() { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 109 | if (channelMap != null) { |
| 110 | channelMap.removeEntryListener(mapListenerId); |
| 111 | channelMap = null; |
| 112 | mapListenerId = null; |
| 113 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Add event channel listener. |
| 118 | * |
| 119 | * @param listener the listener to add. |
| 120 | */ |
| 121 | @Override |
| 122 | public void addListener(IEventChannelListener<K, V> listener) { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 123 | if (listeners.contains(listener)) { |
| 124 | return; // Nothing to do: already a listener |
| 125 | } |
| 126 | listeners.add(listener); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Remove event channel listener. |
| 131 | * |
| 132 | * @param listener the listener to remove. |
| 133 | */ |
| 134 | @Override |
| 135 | public void removeListener(IEventChannelListener<K, V> listener) { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 136 | listeners.remove(listener); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Add an entry to the channel. |
| 141 | * |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 142 | * @param key the key of the entry to add. |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 143 | * @param value the value of the entry to add. |
| 144 | */ |
| 145 | @Override |
| 146 | public void addEntry(K key, V value) { |
Pavlin Radoslavov | bcf1433 | 2014-03-27 18:15:30 -0700 | [diff] [blame] | 147 | byte[] valueBytes = serializeValue(value); |
| 148 | // |
| 149 | // Put the entry in the map: |
| 150 | // - Key : Type <K> |
| 151 | // - Value : Serialized Value (byte[]) |
| 152 | // |
| 153 | channelMap.putAsync(key, valueBytes); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Add a transient entry to the channel. |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 158 | * <p/> |
Pavlin Radoslavov | bcf1433 | 2014-03-27 18:15:30 -0700 | [diff] [blame] | 159 | * The added entry is transient and will automatically timeout after 1ms. |
| 160 | * |
| 161 | * @param key the key of the entry to add. |
| 162 | * @param value the value of the entry to add. |
| 163 | */ |
| 164 | @Override |
| 165 | public void addTransientEntry(K key, V value) { |
| 166 | byte[] valueBytes = serializeValue(value); |
| 167 | // |
| 168 | // Put the entry in the map: |
| 169 | // - Key : Type <K> |
| 170 | // - Value : Serialized Value (byte[]) |
| 171 | // - Timeout: 1ms |
| 172 | // |
| 173 | channelMap.putAsync(key, valueBytes, 1L, TimeUnit.MILLISECONDS); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Serialize the value. |
| 178 | * |
| 179 | * @param value the value to serialize. |
| 180 | * @return the serialized value. |
| 181 | */ |
| 182 | private byte[] serializeValue(V value) { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 183 | // |
| 184 | // Encode the value |
| 185 | // |
| 186 | byte[] buffer = new byte[MAX_BUFFER_SIZE]; |
| 187 | Kryo kryo = kryoFactory.newKryo(); |
Yuta HIGUCHI | e8bfe61 | 2014-06-08 14:20:51 -0700 | [diff] [blame] | 188 | try { |
| 189 | Output output = new Output(buffer, -1); |
| 190 | kryo.writeClassAndObject(output, value); |
| 191 | byte[] valueBytes = output.toBytes(); |
| 192 | return valueBytes; |
| 193 | } finally { |
| 194 | kryoFactory.deleteKryo(kryo); |
| 195 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /** |
Pavlin Radoslavov | 7439db1 | 2014-04-17 13:50:29 -0700 | [diff] [blame] | 199 | * Deserialize the value. |
| 200 | * |
| 201 | * @param kryo the Kryo instance to use for the deserialization. |
| 202 | * @param valueBytes the buffer with the serialized value. |
| 203 | * @return the deserialized value. |
| 204 | */ |
| 205 | private V deserializeValue(Kryo kryo, byte[] valueBytes) { |
| 206 | V value; |
| 207 | |
| 208 | // |
| 209 | // Decode the value |
| 210 | // |
| 211 | Input input = new Input(valueBytes); |
| 212 | Object objValue = kryo.readClassAndObject(input); |
| 213 | try { |
| 214 | value = typeV.cast(objValue); |
| 215 | } catch (ClassCastException e) { |
| 216 | log.error("Received notification value cast failed", e); |
| 217 | return null; |
| 218 | } |
| 219 | |
| 220 | return value; |
| 221 | } |
| 222 | |
| 223 | /** |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 224 | * Remove an entry from the channel. |
| 225 | * |
| 226 | * @param key the key of the entry to remove. |
| 227 | */ |
| 228 | @Override |
| 229 | public void removeEntry(K key) { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 230 | // |
| 231 | // Remove the entry: |
| 232 | // - Key : Type <K> |
| 233 | // - Value : Serialized Value (byte[]) |
| 234 | // |
| 235 | channelMap.removeAsync(key); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Update an entry in the channel. |
| 240 | * |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 241 | * @param key the key of the entry to update. |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 242 | * @param value the value of the entry to update. |
| 243 | */ |
| 244 | @Override |
| 245 | public void updateEntry(K key, V value) { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 246 | // NOTE: Adding an entry with an existing key automatically updates it |
| 247 | addEntry(key, value); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Get an entry from the channel. |
| 252 | * |
| 253 | * @param key the key of the entry to get. |
| 254 | * @return the entry if found, otherwise null. |
| 255 | */ |
| 256 | @Override |
| 257 | @Deprecated |
| 258 | public V getEntry(K key) { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 259 | byte[] valueBytes = channelMap.get(key); |
| 260 | if (valueBytes == null) { |
| 261 | return null; |
| 262 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 263 | |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 264 | // |
| 265 | // Decode the value |
| 266 | // |
Pavlin Radoslavov | 7439db1 | 2014-04-17 13:50:29 -0700 | [diff] [blame] | 267 | Kryo kryo = kryoFactory.newKryo(); |
Yuta HIGUCHI | e8bfe61 | 2014-06-08 14:20:51 -0700 | [diff] [blame] | 268 | try { |
| 269 | V value = deserializeValue(kryo, valueBytes); |
| 270 | return value; |
| 271 | } finally { |
| 272 | kryoFactory.deleteKryo(kryo); |
| 273 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Get all entries in the channel. |
| 278 | * |
| 279 | * @return all entries that are currently in the channel. |
| 280 | */ |
| 281 | @Override |
| 282 | @Deprecated |
| 283 | public Collection<V> getAllEntries() { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 284 | Collection<V> allEntries = new LinkedList<V>(); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 285 | |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 286 | if (channelMap == null) { |
| 287 | return allEntries; // Nothing found |
| 288 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 289 | |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 290 | // |
| 291 | // Get all entries |
| 292 | // |
| 293 | Collection<byte[]> values = channelMap.values(); |
| 294 | Kryo kryo = kryoFactory.newKryo(); |
Yuta HIGUCHI | e8bfe61 | 2014-06-08 14:20:51 -0700 | [diff] [blame] | 295 | try { |
| 296 | for (byte[] valueBytes : values) { |
| 297 | // |
| 298 | // Decode the value |
| 299 | // |
| 300 | V value = deserializeValue(kryo, valueBytes); |
| 301 | allEntries.add(value); |
| 302 | } |
| 303 | } finally { |
| 304 | kryoFactory.deleteKryo(kryo); |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 305 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 306 | |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 307 | return allEntries; |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Remove all entries in the channel. |
| 312 | */ |
| 313 | @Override |
| 314 | @Deprecated |
| 315 | public void removeAllEntries() { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 316 | // |
| 317 | // Remove all entries |
| 318 | // |
| 319 | // NOTE: We remove the entries one-by-one so the per-entry |
| 320 | // notifications will be delivered. |
| 321 | // |
| 322 | // channelMap.clear(); |
| 323 | Set<K> keySet = channelMap.keySet(); |
| 324 | for (K key : keySet) { |
| 325 | channelMap.removeAsync(key); |
| 326 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Class for receiving event notifications for the channel. |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 331 | * <p/> |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 332 | * The datagrid map is: |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 333 | * - Key: Type K |
| 334 | * - Value: Serialized V (byte[]) |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 335 | */ |
Pavlin Radoslavov | e561a4c | 2014-04-01 14:10:55 -0700 | [diff] [blame] | 336 | private class MapEntryListener implements EntryListener<K, byte[]> { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 337 | /** |
| 338 | * Receive a notification that an entry is added. |
| 339 | * |
| 340 | * @param event the notification event for the entry. |
| 341 | */ |
| 342 | @Override |
| 343 | public void entryAdded(EntryEvent<K, byte[]> event) { |
| 344 | // |
| 345 | // Decode the value |
| 346 | // |
Yuta HIGUCHI | 62d8637 | 2014-07-03 12:04:13 -0700 | [diff] [blame] | 347 | byte[] original = event.getValue(); |
| 348 | // Copying byte[], to see if it resolves ONOS-1343. |
| 349 | byte[] valueBytes = Arrays.copyOf(original, original.length); |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 350 | Kryo kryo = kryoFactory.newKryo(); |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 351 | try { |
| 352 | V value = deserializeValue(kryo, valueBytes); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 353 | |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 354 | // |
| 355 | // Deliver the notification |
| 356 | // |
Yuta HIGUCHI | a5dcd19 | 2014-06-06 18:00:35 -0700 | [diff] [blame] | 357 | final Iterator<IEventChannelListener<K, V>> it = listeners.iterator(); |
| 358 | while (it.hasNext()) { |
| 359 | final IEventChannelListener<K, V> listener = it.next(); |
| 360 | if (it.hasNext()) { |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 361 | // Each listener should get a deep copy of the value |
Yuta HIGUCHI | a5dcd19 | 2014-06-06 18:00:35 -0700 | [diff] [blame] | 362 | // TODO: compare which is faster |
| 363 | // - kryo.copy(value) |
| 364 | // - deserializeValue(kryo, valueBytes) |
| 365 | listener.entryAdded(kryo.copy(value)); |
| 366 | } else { |
| 367 | // Last listener can use the value |
| 368 | listener.entryAdded(value); |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 369 | } |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 370 | } |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 371 | } finally { |
| 372 | kryoFactory.deleteKryo(kryo); |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 373 | } |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 374 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 375 | |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 376 | /** |
| 377 | * Receive a notification that an entry is removed. |
| 378 | * |
| 379 | * @param event the notification event for the entry. |
| 380 | */ |
| 381 | @Override |
| 382 | public void entryRemoved(EntryEvent<K, byte[]> event) { |
| 383 | // |
| 384 | // Decode the value |
| 385 | // |
Yuta HIGUCHI | 62d8637 | 2014-07-03 12:04:13 -0700 | [diff] [blame] | 386 | byte[] original = event.getValue(); |
| 387 | // Copying byte[], to see if it resolves ONOS-1343. |
| 388 | byte[] valueBytes = Arrays.copyOf(original, original.length); |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 389 | Kryo kryo = kryoFactory.newKryo(); |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 390 | try { |
| 391 | V value = deserializeValue(kryo, valueBytes); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 392 | |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 393 | // |
| 394 | // Deliver the notification |
| 395 | // |
Yuta HIGUCHI | a5dcd19 | 2014-06-06 18:00:35 -0700 | [diff] [blame] | 396 | final Iterator<IEventChannelListener<K, V>> it = listeners.iterator(); |
| 397 | while (it.hasNext()) { |
| 398 | final IEventChannelListener<K, V> listener = it.next(); |
| 399 | if (it.hasNext()) { |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 400 | // Each listener should get a deep copy of the value |
Yuta HIGUCHI | a5dcd19 | 2014-06-06 18:00:35 -0700 | [diff] [blame] | 401 | listener.entryRemoved(kryo.copy(value)); |
| 402 | } else { |
| 403 | // Last listener can use the value |
| 404 | listener.entryRemoved(value); |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 405 | } |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 406 | } |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 407 | } finally { |
| 408 | kryoFactory.deleteKryo(kryo); |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 409 | } |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 410 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 411 | |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 412 | /** |
| 413 | * Receive a notification that an entry is updated. |
| 414 | * |
| 415 | * @param event the notification event for the entry. |
| 416 | */ |
| 417 | @Override |
| 418 | public void entryUpdated(EntryEvent<K, byte[]> event) { |
| 419 | // |
| 420 | // Decode the value |
| 421 | // |
Yuta HIGUCHI | 62d8637 | 2014-07-03 12:04:13 -0700 | [diff] [blame] | 422 | byte[] original = event.getValue(); |
| 423 | // Copying byte[], to see if it resolves ONOS-1343. |
| 424 | byte[] valueBytes = Arrays.copyOf(original, original.length); |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 425 | Kryo kryo = kryoFactory.newKryo(); |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 426 | try { |
| 427 | V value = deserializeValue(kryo, valueBytes); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 428 | |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 429 | // |
| 430 | // Deliver the notification |
| 431 | // |
Yuta HIGUCHI | a5dcd19 | 2014-06-06 18:00:35 -0700 | [diff] [blame] | 432 | final Iterator<IEventChannelListener<K, V>> it = listeners.iterator(); |
| 433 | while (it.hasNext()) { |
| 434 | final IEventChannelListener<K, V> listener = it.next(); |
| 435 | if (it.hasNext()) { |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 436 | // Each listener should get a deep copy of the value |
Yuta HIGUCHI | a5dcd19 | 2014-06-06 18:00:35 -0700 | [diff] [blame] | 437 | listener.entryUpdated(kryo.copy(value)); |
| 438 | } else { |
| 439 | // Last listener can use the value |
| 440 | listener.entryUpdated(value); |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 441 | } |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 442 | } |
Yuta HIGUCHI | 57701af | 2014-06-06 17:56:28 -0700 | [diff] [blame] | 443 | } finally { |
| 444 | kryoFactory.deleteKryo(kryo); |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 445 | } |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 446 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 447 | |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 448 | /** |
| 449 | * Receive a notification that an entry is evicted. |
| 450 | * |
| 451 | * @param event the notification event for the entry. |
| 452 | */ |
| 453 | @Override |
| 454 | public void entryEvicted(EntryEvent<K, byte[]> event) { |
| 455 | // NOTE: We don't use eviction for this map |
| 456 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 457 | } |
| 458 | } |