forked from pravega/pravega-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSharedMap.java
479 lines (416 loc) · 16.1 KB
/
SharedMap.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*
* Copyright (c) 2017 Dell Inc., or its subsidiaries. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*/
package io.pravega.example.statesynchronizer;
import java.io.Serializable;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import io.pravega.client.ClientFactory;
import io.pravega.client.admin.StreamManager;
import io.pravega.client.state.InitialUpdate;
import io.pravega.client.state.Revision;
import io.pravega.client.state.Revisioned;
import io.pravega.client.state.StateSynchronizer;
import io.pravega.client.state.SynchronizerConfig;
import io.pravega.client.state.Update;
import io.pravega.client.stream.ScalingPolicy;
import io.pravega.client.stream.StreamConfiguration;
import io.pravega.client.stream.impl.JavaSerializer;
/**
* A Map<K, V> that uses Pravega StateSynchronizer to maintain a shared, consistent, optimistically locked, concurrently
* accessible representation.
*
* @param <K> - the type of key used in the map.
* @param <V> - the value type used in the map.
*
*/
public class SharedMap<K extends Serializable, V extends Serializable> {
/**
* The internal Map representation that gets synchronized. This is the "shared state" object.
*
*/
private static class SharedStateMap<K, V> implements Revisioned, Serializable {
private static final long serialVersionUID = 1L;
private final String scopedStreamName;
private final ConcurrentHashMap<K, V> impl;
private final Revision currentRevision;
public SharedStateMap(String scopedStreamName, ConcurrentHashMap<K,V> impl, Revision revision){
this.scopedStreamName = scopedStreamName;
this.impl = impl;
this.currentRevision = revision;
}
@Override
public Revision getRevision() {
return currentRevision;
}
@Override
public String getScopedStreamName() {
return scopedStreamName;
}
public int size() {
return impl.size();
}
public boolean containsKey(K key) {
return impl.containsKey(key);
}
public boolean containsValue(V value) {
return impl.containsValue(value);
}
public Set<Entry<K, V>> entrySet() {
return impl.entrySet();
}
public V get(K key) {
return impl.get(key);
}
public Collection<V> values() {
return impl.values();
}
public Map<K,V> clone() {
return new ConcurrentHashMap<K,V>(impl);
}
}
/**
* Create a Map. This is used by StateSynchronizer to initialize shared state.
*/
private static class CreateState<K, V> implements InitialUpdate<SharedStateMap<K,V>>, Serializable {
private static final long serialVersionUID = 1L;
private final ConcurrentHashMap<K, V> impl;
public CreateState(ConcurrentHashMap<K, V> impl) {
this.impl = impl;
}
@Override
public SharedStateMap<K, V> create(String scopedStreamName, Revision revision) {
return new SharedStateMap<K, V>(scopedStreamName, impl, revision);
}
}
/**
* A base class for all updates to the shared state. This allows for several different types of updates.
*/
private static abstract class StateUpdate<K,V> implements Update<SharedStateMap<K,V>>, Serializable {
private static final long serialVersionUID = 1L;
@Override
public SharedStateMap<K,V> applyTo(SharedStateMap<K,V> oldState, Revision newRevision) {
ConcurrentHashMap<K, V> newState = new ConcurrentHashMap<K, V>(oldState.impl);
process(newState);
return new SharedStateMap<K,V>(oldState.getScopedStreamName(), newState, newRevision);
}
public abstract void process(ConcurrentHashMap<K, V> updatableList);
}
/**
* Clear the State.
*/
private static class Clear<K, V> extends StateUpdate<K,V> {
private static final long serialVersionUID = 1L;
public Clear() {
}
@Override
public void process(ConcurrentHashMap<K, V> impl) {
impl.clear();
}
}
/**
* Add a key/value pair to the State.
*/
private static class Put<K,V> extends StateUpdate<K,V> {
private static final long serialVersionUID = 1L;
private final K key;
private final V value;
public Put(K key, V value) {
this.key = key;
this.value = value;
}
@Override
public void process(ConcurrentHashMap<K, V> impl) {
impl.put(key, value);
}
}
/**
* Add a number of key/value pairs to the State.
*/
private static class PutAll<K,V> extends StateUpdate<K,V> {
private static final long serialVersionUID = 1L;
private final Map<? extends K, ? extends V> map;
public PutAll(Map<? extends K, ? extends V> map) {
this.map = map;
}
@Override
public void process(ConcurrentHashMap<K, V> impl) {
impl.putAll(map);
}
}
/**
* Remove a key/value pair from the State.
*/
private static class Remove<K,V> extends StateUpdate<K,V> {
private static final long serialVersionUID = 1L;
private final K key;
public Remove(K key) {
this.key = key;
}
@Override
public void process(ConcurrentHashMap<K, V> impl) {
impl.remove(key);
}
}
//+++++++++++++++++++++++++++++++++++ SharedMap Behavior +++++++++++++++++++++++++++++++++++
private static final int REMOVALS_BEFORE_COMPACTION = 5;
private final StateSynchronizer<SharedStateMap<K,V>> stateSynchronizer;
private final AtomicInteger countdownToCompaction = new AtomicInteger(REMOVALS_BEFORE_COMPACTION);
/**
* Creates the shared state using a synchronizer based on the given stream name.
*
* @param clientFactory - the Pravega ClientFactory to use to create the StateSynchronizer.
* @param streamManager - the Pravega StreamManager to use to create the Scope and the Stream used by the StateSynchronizer
* @param scope - the Scope to use to create the Stream used by the StateSynchronizer.
* @param name - the name of the Stream to be used by the StateSynchronizer.
*/
public SharedMap(ClientFactory clientFactory, StreamManager streamManager, String scope, String name){
streamManager.createScope(scope);
StreamConfiguration streamConfig = StreamConfiguration.builder()
.scalingPolicy(ScalingPolicy.fixed(1))
.build();
streamManager.createStream(scope, name, streamConfig);
this.stateSynchronizer = clientFactory.createStateSynchronizer(name,
new JavaSerializer<StateUpdate<K,V>>(),
new JavaSerializer<CreateState<K,V>>(),
SynchronizerConfig.builder().build());
stateSynchronizer.initialize(new CreateState<K,V>(new ConcurrentHashMap<K,V>()));
}
//++++++++++++++++++++++++++++++++ Synchronizer-oriented API ++++++++++++++++++++++++++++++++
/**
* Make sure the internal representation of the state is current with respect to the stateSynchronizer.
*/
public void refresh() {
stateSynchronizer.fetchUpdates();
}
/**
* Use the StateSynchronizer to compact the shared state after some number of removal operations.
*/
private void compact() {
countdownToCompaction.set(REMOVALS_BEFORE_COMPACTION);
stateSynchronizer.compact(state -> new CreateState<K,V>(state.impl));
}
//+++++++++++++++++++++++++++++++++++ Map-oriented API ++++++++++++++++++++++++++++++++++++
/**
* Removes all of the mappings from this map.
* Use the StateSynchronizer to compact the shared state.
*/
public void clear(){
stateSynchronizer.updateState((state, updates) -> {
if (state.size() > 0) {
updates.add(new Clear<K,V>());
}
});
compact();
}
/**
* Create a copy of the map.
*
* @return - the copy of the shared state map.
*/
public Map<K,V> clone() {
return stateSynchronizer.getState().clone();
}
/**
* Determine if the given key appears in the map.
*
* @param key - the key to search for.
* @return - true if this map contains a mapping for the specified key.
*/
public boolean containsKey(K key){
return stateSynchronizer.getState().containsKey(key);
}
/**
* Determine if the given value appears in the map.
*
* @param value - the value to search for.
* @returns - true if this map maps one or more keys to the specified value.
*/
public boolean containsValue(V value){
return stateSynchronizer.getState().containsValue(value);
}
/**
* Render the map as a Set of entries.
*
* @return - a Set view of the mappings contained in this map.
*/
public Set<Map.Entry<K,V>> entrySet(){
return stateSynchronizer.getState().entrySet();
}
/**
* Return the value for the given key.
*
* @param key - the key to search for.
* @returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
*/
public V get(K key){
return stateSynchronizer.getState().get(key);
}
/**
* Associates the specified value with the specified key in this map.
*
* @param key - the key at which the value should be found.
* @param value - the value to be entered into the map.
* @return - the previous value (if it existed) for the given key or null if the key did not exist before this operation.
*/
public V put(K key, V value){
final AtomicReference<V> oldValue = new AtomicReference<V>(null);
stateSynchronizer.updateState((state, updates) -> {
oldValue.set(state.get(key));
updates.add(new Put<K,V>(key,value));
});
return oldValue.get();
}
/**
* Copies all of the mappings from the specified map to this map.
*
* @param map - the map to copy.
*/
public void putAll(Map<K,V> map){
stateSynchronizer.updateState((state, updates) -> {
updates.add(new PutAll<K,V>(map));
});
}
/**
* If the specified key is not already associated with a value (or is mapped to null) associates it with the given
* value and returns null, else returns the current value.
*
* @param key - the key at which the value should be found.
* @param value - the value to be entered into the map.
* @return - the previous value (if it existed) for the given key or null if the key did not exist before this operation.
*/
public V putIfAbsent(K key, V value){
final AtomicReference<V> ret = new AtomicReference<V>();
refresh(); //this is a conditional modifying operation, need to update local state with current shared state before checking the condition
stateSynchronizer.updateState((state, updates) -> {
if (state.containsKey(key) && state.get(key) != null) {
ret.set(state.get(key));
} else {
ret.set(null);
updates.add(new Put<K,V>(key,value));
}
});
return ret.get();
}
/**
* Removes the mapping for the specified key from this map if present.
* Uses the countDown to determine if it is also time to compact the SharedState after removal.
*
* @param key - the key to be removed.
* @return - the previous value (if it existed) for the key to be removed or null if that key does not exist in the map.
*/
public V remove(K key) {
final AtomicReference<V> oldValue = new AtomicReference<V>(null);
stateSynchronizer.updateState((state, updates) -> {
if (state.impl.containsKey(key)) {
oldValue.set(state.get(key));
updates.add(new Remove<K,V>(key));
} else {
oldValue.set(null);
}
});
if (countdownToCompaction.decrementAndGet() <= 0) {
compact();
}
return oldValue.get();
}
/**
* Removes the entry for the specified key only if it is currently mapped to the specified value.
* Uses the countDown to determine if it is also time to compact the SharedState after removal
*
* @param - the key to be removed.
* @param - the expected value of the key to be removed.
* @return - true if the key was removed, false otherwise.
*/
public boolean remove(K key, V value){
AtomicBoolean ret = new AtomicBoolean(false);
stateSynchronizer.updateState((state, updates) -> {
if (state.impl.containsKey(key) && state.impl.get(key).equals(value)) {
ret.set(true);
updates.add(new Remove<K,V>(key));
} else {
ret.set(false);
}
});
if (countdownToCompaction.decrementAndGet() <= 0) {
compact();
}
return ret.get();
}
/**
* Replaces the entry for the specified key only if it is currently mapped to some value.
*
* @param key - the key whose value is to be replaced.
* @param value - the value to assign to the key.
* @return - the previous value associated with the key, or null if the key does not exist in the map.
*/
public V replace(K key, V value){
final AtomicReference<V> oldValue = new AtomicReference<V>(null);
stateSynchronizer.updateState((state, updates) -> {
if (state.impl.containsKey(key)) {
oldValue.set(state.get(key));
updates.add(new Put<K,V>(key,value));
} else {
oldValue.set(null);
}
});
return oldValue.get();
}
/**
* Replaces the entry for the specified key only if currently mapped to the specified value.
*
* @param key - the key whose value is to be replaced.
* @param oldValue - the expected value of the key prior to replacement.
* @param newValue - the value to be assigned to the key.
* @return - true if the current value of the key matches oldvalue and therefore the key is updated with newValue. False otherwise.
*/
public boolean replace(K key, V oldValue, V newValue){
AtomicBoolean ret = new AtomicBoolean(false);
stateSynchronizer.updateState((state, updates) -> {
if (state.impl.containsKey(key) && state.impl.get(key).equals(oldValue)) {
ret.set(true);
updates.add(new Put<K,V>(key, newValue));
} else {
ret.set(false);
}
});
return ret.get();
}
/**
* Determine the number of key-value mappings in the map.
*
* @return - the number of key-value mappings in the map.
*/
public int size(){
return stateSynchronizer.getState().size();
}
/**
* Render the set of values as a Collection.
*
* @return - a Collection view of the values contained in this map.
*/
public Collection<V> values(){
return stateSynchronizer.getState().values();
}
/**
* Close the SharedMap.
*/
public void close() {
if( stateSynchronizer != null) {
stateSynchronizer.close();
}
}
}