Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 1 | package net.onrc.onos.datagrid; |
| 2 | |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 3 | import java.util.Collection; |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 4 | import java.util.LinkedList; |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 5 | import java.util.Set; |
Pavlin Radoslavov | 1c8d809 | 2014-02-14 15:47:24 -0800 | [diff] [blame] | 6 | import java.util.concurrent.CopyOnWriteArrayList; |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 7 | |
| 8 | import com.esotericsoftware.kryo.Kryo; |
| 9 | import com.esotericsoftware.kryo.io.Input; |
| 10 | import com.esotericsoftware.kryo.io.Output; |
| 11 | import com.hazelcast.core.EntryEvent; |
| 12 | import com.hazelcast.core.EntryListener; |
| 13 | import com.hazelcast.core.HazelcastInstance; |
| 14 | import com.hazelcast.core.IMap; |
| 15 | |
| 16 | import net.onrc.onos.ofcontroller.util.serializers.KryoFactory; |
| 17 | |
| 18 | /** |
| 19 | * A datagrid event channel that uses Hazelcast as a datagrid. |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 20 | * |
| 21 | * @param <K> The class type of the key. |
| 22 | * @param <V> The class type of the value. |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 23 | */ |
| 24 | public class HazelcastEventChannel<K, V> implements IEventChannel<K, V> { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 25 | private final HazelcastInstance hazelcastInstance; // The Hazelcast instance |
| 26 | private final String channelName; // The event channel name |
| 27 | private final Class<?> typeK; // The class type of the key |
| 28 | private final Class<?> typeV; // The class type of the value |
| 29 | private IMap<K, byte[]> channelMap; // The Hazelcast channel map |
Pavlin Radoslavov | 1c8d809 | 2014-02-14 15:47:24 -0800 | [diff] [blame] | 30 | // The channel listeners |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 31 | private final CopyOnWriteArrayList<IEventChannelListener<K, V>> listeners = |
| 32 | new CopyOnWriteArrayList<>(); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 33 | |
| 34 | // The map entry listener |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 35 | private final MapEntryListener mapEntryListener = new MapEntryListener<K>(); |
| 36 | private String mapListenerId; // The map listener ID |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 37 | |
| 38 | // TODO: We should use a single global KryoFactory instance |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 39 | private final KryoFactory kryoFactory = new KryoFactory(); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 40 | |
| 41 | // Maximum serialized event size |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 42 | private static final int MAX_BUFFER_SIZE = 64 * 1024; |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Constructor for a given event channel name. |
| 46 | * |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 47 | * @param newHazelcastInstance the Hazelcast instance to use. |
| 48 | * @param newChannelName the event channel name. |
| 49 | * @param newTypeK the type of the Key in the Key-Value store. |
| 50 | * @param newTypeV the type of the Value in the Key-Value store. |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 51 | */ |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 52 | public HazelcastEventChannel(HazelcastInstance newHazelcastInstance, |
| 53 | String newChannelName, Class<K> newTypeK, |
| 54 | Class<V> newTypeV) { |
| 55 | hazelcastInstance = newHazelcastInstance; |
| 56 | channelName = newChannelName; |
| 57 | typeK = newTypeK; |
| 58 | typeV = newTypeV; |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Verify the key and value types of a channel. |
| 63 | * |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 64 | * @param typeKToVerify the type of the key to verify. |
| 65 | * @param typeVToVerify the type of the value to verify. |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 66 | * @return true if the key and value types of the channel match, |
| 67 | * otherwise false. |
| 68 | */ |
| 69 | @Override |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 70 | public boolean verifyKeyValueTypes(Class typeKToVerify, |
| 71 | Class typeVToVerify) { |
Ray Milkey | 7301965 | 2014-03-24 11:12:44 -0700 | [diff] [blame] | 72 | return (typeK.equals(typeKToVerify)) && (typeV.equals(typeVToVerify)); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Cleanup and destroy the channel. |
| 77 | */ |
| 78 | @Override |
| 79 | protected void finalize() { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 80 | shutdown(); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Startup the channel operation. |
| 85 | */ |
| 86 | @Override |
| 87 | public void startup() { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 88 | if (channelMap == null) { |
| 89 | channelMap = hazelcastInstance.getMap(channelName); |
| 90 | mapListenerId = channelMap.addEntryListener(mapEntryListener, |
| 91 | true); |
| 92 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Shutdown the channel operation. |
| 97 | */ |
| 98 | @Override |
| 99 | public void shutdown() { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 100 | if (channelMap != null) { |
| 101 | channelMap.removeEntryListener(mapListenerId); |
| 102 | channelMap = null; |
| 103 | mapListenerId = null; |
| 104 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Add event channel listener. |
| 109 | * |
| 110 | * @param listener the listener to add. |
| 111 | */ |
| 112 | @Override |
| 113 | public void addListener(IEventChannelListener<K, V> listener) { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 114 | if (listeners.contains(listener)) { |
| 115 | return; // Nothing to do: already a listener |
| 116 | } |
| 117 | listeners.add(listener); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Remove event channel listener. |
| 122 | * |
| 123 | * @param listener the listener to remove. |
| 124 | */ |
| 125 | @Override |
| 126 | public void removeListener(IEventChannelListener<K, V> listener) { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 127 | listeners.remove(listener); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Add an entry to the channel. |
| 132 | * |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 133 | * @param key the key of the entry to add. |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 134 | * @param value the value of the entry to add. |
| 135 | */ |
| 136 | @Override |
| 137 | public void addEntry(K key, V value) { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 138 | // |
| 139 | // Encode the value |
| 140 | // |
| 141 | byte[] buffer = new byte[MAX_BUFFER_SIZE]; |
| 142 | Kryo kryo = kryoFactory.newKryo(); |
| 143 | Output output = new Output(buffer, -1); |
| 144 | kryo.writeObject(output, value); |
| 145 | byte[] valueBytes = output.toBytes(); |
| 146 | kryoFactory.deleteKryo(kryo); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 147 | |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 148 | // |
| 149 | // Put the entry in the map: |
| 150 | // - Key : Type <K> |
| 151 | // - Value : Serialized Value (byte[]) |
| 152 | // |
| 153 | channelMap.putAsync(key, valueBytes); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Remove an entry from the channel. |
| 158 | * |
| 159 | * @param key the key of the entry to remove. |
| 160 | */ |
| 161 | @Override |
| 162 | public void removeEntry(K key) { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 163 | // |
| 164 | // Remove the entry: |
| 165 | // - Key : Type <K> |
| 166 | // - Value : Serialized Value (byte[]) |
| 167 | // |
| 168 | channelMap.removeAsync(key); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Update an entry in the channel. |
| 173 | * |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 174 | * @param key the key of the entry to update. |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 175 | * @param value the value of the entry to update. |
| 176 | */ |
| 177 | @Override |
| 178 | public void updateEntry(K key, V value) { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 179 | // NOTE: Adding an entry with an existing key automatically updates it |
| 180 | addEntry(key, value); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Get an entry from the channel. |
| 185 | * |
| 186 | * @param key the key of the entry to get. |
| 187 | * @return the entry if found, otherwise null. |
| 188 | */ |
| 189 | @Override |
| 190 | @Deprecated |
| 191 | public V getEntry(K key) { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 192 | byte[] valueBytes = channelMap.get(key); |
| 193 | if (valueBytes == null) { |
| 194 | return null; |
| 195 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 196 | |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 197 | Kryo kryo = kryoFactory.newKryo(); |
| 198 | // |
| 199 | // Decode the value |
| 200 | // |
| 201 | Input input = new Input(valueBytes); |
| 202 | V value = (V) kryo.readObject(input, typeV); |
| 203 | kryoFactory.deleteKryo(kryo); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 204 | |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 205 | return value; |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Get all entries in the channel. |
| 210 | * |
| 211 | * @return all entries that are currently in the channel. |
| 212 | */ |
| 213 | @Override |
| 214 | @Deprecated |
| 215 | public Collection<V> getAllEntries() { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 216 | Collection<V> allEntries = new LinkedList<V>(); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 217 | |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 218 | if (channelMap == null) { |
| 219 | return allEntries; // Nothing found |
| 220 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 221 | |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 222 | // |
| 223 | // Get all entries |
| 224 | // |
| 225 | Collection<byte[]> values = channelMap.values(); |
| 226 | Kryo kryo = kryoFactory.newKryo(); |
| 227 | for (byte[] valueBytes : values) { |
| 228 | // |
| 229 | // Decode the value |
| 230 | // |
| 231 | Input input = new Input(valueBytes); |
| 232 | V value = (V) kryo.readObject(input, typeV); |
| 233 | allEntries.add(value); |
| 234 | } |
| 235 | kryoFactory.deleteKryo(kryo); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 236 | |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 237 | return allEntries; |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Remove all entries in the channel. |
| 242 | */ |
| 243 | @Override |
| 244 | @Deprecated |
| 245 | public void removeAllEntries() { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 246 | // |
| 247 | // Remove all entries |
| 248 | // |
| 249 | // NOTE: We remove the entries one-by-one so the per-entry |
| 250 | // notifications will be delivered. |
| 251 | // |
| 252 | // channelMap.clear(); |
| 253 | Set<K> keySet = channelMap.keySet(); |
| 254 | for (K key : keySet) { |
| 255 | channelMap.removeAsync(key); |
| 256 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Class for receiving event notifications for the channel. |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 261 | * <p/> |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 262 | * The datagrid map is: |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 263 | * - Key: Type K |
| 264 | * - Value: Serialized V (byte[]) |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 265 | */ |
| 266 | private class MapEntryListener<K> implements EntryListener<K, byte[]> { |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 267 | /** |
| 268 | * Receive a notification that an entry is added. |
| 269 | * |
| 270 | * @param event the notification event for the entry. |
| 271 | */ |
| 272 | @Override |
| 273 | public void entryAdded(EntryEvent<K, byte[]> event) { |
| 274 | // |
| 275 | // Decode the value |
| 276 | // |
| 277 | byte[] valueBytes = event.getValue(); |
| 278 | Kryo kryo = kryoFactory.newKryo(); |
| 279 | Input input = new Input(valueBytes); |
| 280 | V value = (V) kryo.readObject(input, typeV); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 281 | |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 282 | // |
| 283 | // Deliver the notification |
| 284 | // |
| 285 | int index = 0; |
| 286 | for (IEventChannelListener listener : listeners) { |
| 287 | V copyValue = value; |
| 288 | if (index++ > 0) { |
| 289 | // Each listener should get a deep copy of the value |
| 290 | copyValue = kryo.copy(value); |
| 291 | } |
| 292 | listener.entryAdded(copyValue); |
| 293 | } |
| 294 | kryoFactory.deleteKryo(kryo); |
| 295 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 296 | |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 297 | /** |
| 298 | * Receive a notification that an entry is removed. |
| 299 | * |
| 300 | * @param event the notification event for the entry. |
| 301 | */ |
| 302 | @Override |
| 303 | public void entryRemoved(EntryEvent<K, byte[]> event) { |
| 304 | // |
| 305 | // Decode the value |
| 306 | // |
| 307 | byte[] valueBytes = event.getValue(); |
| 308 | Kryo kryo = kryoFactory.newKryo(); |
| 309 | Input input = new Input(valueBytes); |
| 310 | V value = (V) kryo.readObject(input, typeV); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 311 | |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 312 | // |
| 313 | // Deliver the notification |
| 314 | // |
| 315 | int index = 0; |
| 316 | for (IEventChannelListener listener : listeners) { |
| 317 | V copyValue = value; |
| 318 | if (index++ > 0) { |
| 319 | // Each listener should get a deep copy of the value |
| 320 | copyValue = kryo.copy(value); |
| 321 | } |
| 322 | listener.entryRemoved(copyValue); |
| 323 | } |
| 324 | kryoFactory.deleteKryo(kryo); |
| 325 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 326 | |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 327 | /** |
| 328 | * Receive a notification that an entry is updated. |
| 329 | * |
| 330 | * @param event the notification event for the entry. |
| 331 | */ |
| 332 | @Override |
| 333 | public void entryUpdated(EntryEvent<K, byte[]> event) { |
| 334 | // |
| 335 | // Decode the value |
| 336 | // |
| 337 | byte[] valueBytes = event.getValue(); |
| 338 | Kryo kryo = kryoFactory.newKryo(); |
| 339 | Input input = new Input(valueBytes); |
| 340 | V value = (V) kryo.readObject(input, typeV); |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 341 | |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 342 | // |
| 343 | // Deliver the notification |
| 344 | // |
| 345 | int index = 0; |
| 346 | for (IEventChannelListener listener : listeners) { |
| 347 | V copyValue = value; |
| 348 | if (index++ > 0) { |
| 349 | // Each listener should get a deep copy of the value |
| 350 | copyValue = kryo.copy(value); |
| 351 | } |
| 352 | listener.entryUpdated(copyValue); |
| 353 | } |
| 354 | kryoFactory.deleteKryo(kryo); |
| 355 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 356 | |
Ray Milkey | e88b2b8 | 2014-03-21 11:40:04 -0700 | [diff] [blame] | 357 | /** |
| 358 | * Receive a notification that an entry is evicted. |
| 359 | * |
| 360 | * @param event the notification event for the entry. |
| 361 | */ |
| 362 | @Override |
| 363 | public void entryEvicted(EntryEvent<K, byte[]> event) { |
| 364 | // NOTE: We don't use eviction for this map |
| 365 | } |
Pavlin Radoslavov | 7940b65 | 2014-02-13 19:42:05 -0800 | [diff] [blame] | 366 | } |
| 367 | } |