/
ConsistentMapService.java
1151 lines (1052 loc) · 40.7 KB
/
ConsistentMapService.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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2016-present Open Networking Foundation
*
* 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.atomix.core.map.impl;
import com.google.common.base.Throwables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import io.atomix.core.map.MapEvent;
import io.atomix.core.map.impl.ConsistentMapOperations.ContainsKey;
import io.atomix.core.map.impl.ConsistentMapOperations.ContainsValue;
import io.atomix.core.map.impl.ConsistentMapOperations.Get;
import io.atomix.core.map.impl.ConsistentMapOperations.GetAllPresent;
import io.atomix.core.map.impl.ConsistentMapOperations.GetOrDefault;
import io.atomix.core.map.impl.ConsistentMapOperations.Put;
import io.atomix.core.map.impl.ConsistentMapOperations.Remove;
import io.atomix.core.map.impl.ConsistentMapOperations.RemoveValue;
import io.atomix.core.map.impl.ConsistentMapOperations.RemoveVersion;
import io.atomix.core.map.impl.ConsistentMapOperations.Replace;
import io.atomix.core.map.impl.ConsistentMapOperations.ReplaceValue;
import io.atomix.core.map.impl.ConsistentMapOperations.ReplaceVersion;
import io.atomix.core.map.impl.ConsistentMapOperations.TransactionBegin;
import io.atomix.core.map.impl.ConsistentMapOperations.TransactionCommit;
import io.atomix.core.map.impl.ConsistentMapOperations.TransactionPrepare;
import io.atomix.core.map.impl.ConsistentMapOperations.TransactionPrepareAndCommit;
import io.atomix.core.map.impl.ConsistentMapOperations.TransactionRollback;
import io.atomix.core.map.impl.MapUpdate.Type;
import io.atomix.core.transaction.TransactionId;
import io.atomix.core.transaction.TransactionLog;
import io.atomix.primitive.service.AbstractPrimitiveService;
import io.atomix.primitive.service.Commit;
import io.atomix.primitive.service.ServiceExecutor;
import io.atomix.primitive.session.Session;
import io.atomix.storage.buffer.BufferInput;
import io.atomix.storage.buffer.BufferOutput;
import io.atomix.utils.concurrent.Scheduled;
import io.atomix.utils.serializer.KryoNamespace;
import io.atomix.utils.serializer.KryoNamespaces;
import io.atomix.utils.serializer.Serializer;
import io.atomix.utils.time.Versioned;
import java.time.Duration;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import static com.google.common.base.Preconditions.checkState;
import static io.atomix.core.map.impl.ConsistentMapEvents.CHANGE;
import static io.atomix.core.map.impl.ConsistentMapOperations.ADD_LISTENER;
import static io.atomix.core.map.impl.ConsistentMapOperations.BEGIN;
import static io.atomix.core.map.impl.ConsistentMapOperations.CLEAR;
import static io.atomix.core.map.impl.ConsistentMapOperations.COMMIT;
import static io.atomix.core.map.impl.ConsistentMapOperations.CONTAINS_KEY;
import static io.atomix.core.map.impl.ConsistentMapOperations.CONTAINS_VALUE;
import static io.atomix.core.map.impl.ConsistentMapOperations.ENTRY_SET;
import static io.atomix.core.map.impl.ConsistentMapOperations.GET;
import static io.atomix.core.map.impl.ConsistentMapOperations.GET_ALL_PRESENT;
import static io.atomix.core.map.impl.ConsistentMapOperations.GET_OR_DEFAULT;
import static io.atomix.core.map.impl.ConsistentMapOperations.IS_EMPTY;
import static io.atomix.core.map.impl.ConsistentMapOperations.KEY_SET;
import static io.atomix.core.map.impl.ConsistentMapOperations.PREPARE;
import static io.atomix.core.map.impl.ConsistentMapOperations.PREPARE_AND_COMMIT;
import static io.atomix.core.map.impl.ConsistentMapOperations.PUT;
import static io.atomix.core.map.impl.ConsistentMapOperations.PUT_AND_GET;
import static io.atomix.core.map.impl.ConsistentMapOperations.PUT_IF_ABSENT;
import static io.atomix.core.map.impl.ConsistentMapOperations.REMOVE;
import static io.atomix.core.map.impl.ConsistentMapOperations.REMOVE_LISTENER;
import static io.atomix.core.map.impl.ConsistentMapOperations.REMOVE_VALUE;
import static io.atomix.core.map.impl.ConsistentMapOperations.REMOVE_VERSION;
import static io.atomix.core.map.impl.ConsistentMapOperations.REPLACE;
import static io.atomix.core.map.impl.ConsistentMapOperations.REPLACE_VALUE;
import static io.atomix.core.map.impl.ConsistentMapOperations.REPLACE_VERSION;
import static io.atomix.core.map.impl.ConsistentMapOperations.ROLLBACK;
import static io.atomix.core.map.impl.ConsistentMapOperations.SIZE;
import static io.atomix.core.map.impl.ConsistentMapOperations.VALUES;
/**
* State Machine for {@link ConsistentMapProxy} resource.
*/
public class ConsistentMapService extends AbstractPrimitiveService {
private static final Serializer SERIALIZER = Serializer.using(KryoNamespace.builder()
.register(KryoNamespaces.BASIC)
.register(ConsistentMapOperations.NAMESPACE)
.register(ConsistentMapEvents.NAMESPACE)
.nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID + 100)
.register(TransactionScope.class)
.register(TransactionLog.class)
.register(TransactionId.class)
.register(MapEntryValue.class)
.register(MapEntryValue.Type.class)
.register(new HashMap().keySet().getClass())
.build());
protected Map<Long, Session> listeners = new LinkedHashMap<>();
private Map<String, MapEntryValue> map;
protected Set<String> preparedKeys = Sets.newHashSet();
protected Map<TransactionId, TransactionScope> activeTransactions = Maps.newHashMap();
protected long currentVersion;
public ConsistentMapService() {
map = createMap();
}
protected Map<String, MapEntryValue> createMap() {
return Maps.newHashMap();
}
protected Map<String, MapEntryValue> entries() {
return map;
}
protected Serializer serializer() {
return SERIALIZER;
}
@Override
public void backup(BufferOutput<?> writer) {
writer.writeObject(Sets.newHashSet(listeners.keySet()), serializer()::encode);
writer.writeObject(preparedKeys, serializer()::encode);
writer.writeObject(entries(), serializer()::encode);
writer.writeObject(activeTransactions, serializer()::encode);
writer.writeLong(currentVersion);
}
@Override
public void restore(BufferInput<?> reader) {
listeners = new LinkedHashMap<>();
for (Long sessionId : reader.<Set<Long>>readObject(serializer()::decode)) {
listeners.put(sessionId, getSessions().getSession(sessionId));
}
preparedKeys = reader.readObject(serializer()::decode);
map = reader.readObject(serializer()::decode);
activeTransactions = reader.readObject(serializer()::decode);
currentVersion = reader.readLong();
map.forEach((key, value) -> {
if (value.ttl() > 0) {
value.timer = getScheduler().schedule(Duration.ofMillis(value.ttl() - (getWallClock().getTime().unixTimestamp() - value.created())), () -> {
entries().remove(key, value);
publish(new MapEvent<>(MapEvent.Type.REMOVE, "", key, null, toVersioned(value)));
});
}
});
}
@Override
protected void configure(ServiceExecutor executor) {
// Listeners
executor.register(ADD_LISTENER, (Commit<Void> c) -> listen(c.session()));
executor.register(REMOVE_LISTENER, (Commit<Void> c) -> unlisten(c.session()));
// Queries
executor.register(CONTAINS_KEY, serializer()::decode, this::containsKey, serializer()::encode);
executor.register(CONTAINS_VALUE, serializer()::decode, this::containsValue, serializer()::encode);
executor.register(ENTRY_SET, (Commit<Void> c) -> entrySet(), serializer()::encode);
executor.register(GET, serializer()::decode, this::get, serializer()::encode);
executor.register(GET_ALL_PRESENT, serializer()::decode, this::getAllPresent, serializer()::encode);
executor.register(GET_OR_DEFAULT, serializer()::decode, this::getOrDefault, serializer()::encode);
executor.register(IS_EMPTY, (Commit<Void> c) -> isEmpty(), serializer()::encode);
executor.register(KEY_SET, (Commit<Void> c) -> keySet(), serializer()::encode);
executor.register(SIZE, (Commit<Void> c) -> size(), serializer()::encode);
executor.register(VALUES, (Commit<Void> c) -> values(), serializer()::encode);
// Commands
executor.register(PUT, serializer()::decode, this::put, serializer()::encode);
executor.register(PUT_IF_ABSENT, serializer()::decode, this::putIfAbsent, serializer()::encode);
executor.register(PUT_AND_GET, serializer()::decode, this::putAndGet, serializer()::encode);
executor.register(REMOVE, serializer()::decode, this::remove, serializer()::encode);
executor.register(REMOVE_VALUE, serializer()::decode, this::removeValue, serializer()::encode);
executor.register(REMOVE_VERSION, serializer()::decode, this::removeVersion, serializer()::encode);
executor.register(REPLACE, serializer()::decode, this::replace, serializer()::encode);
executor.register(REPLACE_VALUE, serializer()::decode, this::replaceValue, serializer()::encode);
executor.register(REPLACE_VERSION, serializer()::decode, this::replaceVersion, serializer()::encode);
executor.register(CLEAR, (Commit<Void> c) -> clear(), serializer()::encode);
executor.register(BEGIN, serializer()::decode, this::begin, serializer()::encode);
executor.register(PREPARE, serializer()::decode, this::prepare, serializer()::encode);
executor.register(PREPARE_AND_COMMIT, serializer()::decode, this::prepareAndCommit, serializer()::encode);
executor.register(COMMIT, serializer()::decode, this::commit, serializer()::encode);
executor.register(ROLLBACK, serializer()::decode, this::rollback, serializer()::encode);
}
/**
* Handles a contains key commit.
*
* @param commit containsKey commit
* @return {@code true} if map contains key
*/
protected boolean containsKey(Commit<? extends ContainsKey> commit) {
MapEntryValue value = entries().get(commit.value().key());
return value != null && value.type() != MapEntryValue.Type.TOMBSTONE;
}
/**
* Handles a contains value commit.
*
* @param commit containsValue commit
* @return {@code true} if map contains value
*/
protected boolean containsValue(Commit<? extends ContainsValue> commit) {
return entries().values().stream()
.filter(value -> value.type() != MapEntryValue.Type.TOMBSTONE)
.anyMatch(value -> Arrays.equals(value.value, commit.value().value()));
}
/**
* Handles a get commit.
*
* @param commit get commit
* @return value mapped to key
*/
protected Versioned<byte[]> get(Commit<? extends Get> commit) {
return toVersioned(entries().get(commit.value().key()));
}
/**
* Handles a get all present commit.
*
* @param commit get all present commit
* @return keys present in map
*/
protected Map<String, Versioned<byte[]>> getAllPresent(Commit<? extends GetAllPresent> commit) {
return entries().entrySet().stream()
.filter(entry -> entry.getValue().type() != MapEntryValue.Type.TOMBSTONE
&& commit.value().keys().contains(entry.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, o -> toVersioned(o.getValue())));
}
/**
* Handles a get or default commit.
*
* @param commit get or default commit
* @return value mapped to key
*/
protected Versioned<byte[]> getOrDefault(Commit<? extends GetOrDefault> commit) {
MapEntryValue value = entries().get(commit.value().key());
if (value == null) {
return new Versioned<>(commit.value().defaultValue(), 0);
} else if (value.type() == MapEntryValue.Type.TOMBSTONE) {
return new Versioned<>(commit.value().defaultValue(), value.version);
} else {
return new Versioned<>(value.value(), value.version);
}
}
/**
* Handles a size commit.
*
* @return number of entries in map
*/
protected int size() {
return (int) entries().values().stream()
.filter(value -> value.type() != MapEntryValue.Type.TOMBSTONE)
.count();
}
/**
* Handles an is empty commit.
*
* @return {@code true} if map is empty
*/
protected boolean isEmpty() {
return entries().values().stream()
.noneMatch(value -> value.type() != MapEntryValue.Type.TOMBSTONE);
}
/**
* Handles a keySet commit.
*
* @return set of keys in map
*/
protected Set<String> keySet() {
return entries().entrySet().stream()
.filter(entry -> entry.getValue().type() != MapEntryValue.Type.TOMBSTONE)
.map(Map.Entry::getKey)
.collect(Collectors.toSet());
}
/**
* Handles a values commit.
*
* @return collection of values in map
*/
protected Collection<Versioned<byte[]>> values() {
return entries().entrySet().stream()
.filter(entry -> entry.getValue().type() != MapEntryValue.Type.TOMBSTONE)
.map(entry -> toVersioned(entry.getValue()))
.collect(Collectors.toList());
}
/**
* Handles a entry set commit.
*
* @return set of map entries
*/
protected Set<Map.Entry<String, Versioned<byte[]>>> entrySet() {
return entries().entrySet().stream()
.filter(entry -> entry.getValue().type() != MapEntryValue.Type.TOMBSTONE)
.map(e -> Maps.immutableEntry(e.getKey(), toVersioned(e.getValue())))
.collect(Collectors.toSet());
}
/**
* Returns a boolean indicating whether the given MapEntryValues are equal.
*
* @param oldValue the first value to compare
* @param newValue the second value to compare
* @return indicates whether the two values are equal
*/
protected boolean valuesEqual(MapEntryValue oldValue, MapEntryValue newValue) {
return (oldValue == null && newValue == null)
|| (oldValue != null && newValue != null && valuesEqual(oldValue.value(), newValue.value()));
}
/**
* Returns a boolean indicating whether the given entry values are equal.
*
* @param oldValue the first value to compare
* @param newValue the second value to compare
* @return indicates whether the two values are equal
*/
protected boolean valuesEqual(byte[] oldValue, byte[] newValue) {
return (oldValue == null && newValue == null)
|| (oldValue != null && newValue != null && Arrays.equals(oldValue, newValue));
}
/**
* Returns a boolean indicating whether the given MapEntryValue is null or a tombstone.
*
* @param value the value to check
* @return indicates whether the given value is null or is a tombstone
*/
protected boolean valueIsNull(MapEntryValue value) {
return value == null || value.type() == MapEntryValue.Type.TOMBSTONE;
}
/**
* Updates the given value.
*
* @param key the key to update
* @param value the value to update
*/
protected void putValue(String key, MapEntryValue value) {
MapEntryValue oldValue = entries().put(key, value);
cancelTtl(oldValue);
scheduleTtl(key, value);
}
/**
* Schedules the TTL for the given value.
*
* @param value the value for which to schedule the TTL
*/
protected void scheduleTtl(String key, MapEntryValue value) {
if (value.ttl() > 0) {
value.timer = getScheduler().schedule(Duration.ofMillis(value.ttl()), () -> {
entries().remove(key, value);
publish(new MapEvent<>(MapEvent.Type.REMOVE, "", key, null, toVersioned(value)));
});
}
}
/**
* Cancels the TTL for the given value.
*
* @param value the value for which to cancel the TTL
*/
protected void cancelTtl(MapEntryValue value) {
if (value != null && value.timer != null) {
value.timer.cancel();
}
}
/**
* Handles a put commit.
*
* @param commit put commit
* @return map entry update result
*/
protected MapEntryUpdateResult<String, byte[]> put(Commit<? extends Put> commit) {
String key = commit.value().key();
MapEntryValue oldValue = entries().get(key);
MapEntryValue newValue = new MapEntryValue(
MapEntryValue.Type.VALUE,
commit.index(),
commit.value().value(),
commit.wallClockTime().unixTimestamp(),
commit.value().ttl());
// If the value is null or a tombstone, this is an insert.
// Otherwise, only update the value if it has changed to reduce the number of events.
if (valueIsNull(oldValue)) {
// If the key has been locked by a transaction, return a WRITE_LOCK error.
if (preparedKeys.contains(key)) {
return new MapEntryUpdateResult<>(
MapEntryUpdateResult.Status.WRITE_LOCK,
commit.index(),
key,
toVersioned(oldValue));
}
putValue(commit.value().key(), newValue);
Versioned<byte[]> result = toVersioned(oldValue);
publish(new MapEvent<>(MapEvent.Type.INSERT, "", key, toVersioned(newValue), result));
return new MapEntryUpdateResult<>(MapEntryUpdateResult.Status.OK, commit.index(), key, result);
} else if (!valuesEqual(oldValue, newValue)) {
// If the key has been locked by a transaction, return a WRITE_LOCK error.
if (preparedKeys.contains(key)) {
return new MapEntryUpdateResult<>(
MapEntryUpdateResult.Status.WRITE_LOCK,
commit.index(),
key,
toVersioned(oldValue));
}
putValue(commit.value().key(), newValue);
Versioned<byte[]> result = toVersioned(oldValue);
publish(new MapEvent<>(MapEvent.Type.UPDATE, "", key, toVersioned(newValue), result));
return new MapEntryUpdateResult<>(MapEntryUpdateResult.Status.OK, commit.index(), key, result);
}
// If the value hasn't changed, return a NOOP result.
return new MapEntryUpdateResult<>(MapEntryUpdateResult.Status.NOOP, commit.index(), key, toVersioned(oldValue));
}
/**
* Handles a putIfAbsent commit.
*
* @param commit putIfAbsent commit
* @return map entry update result
*/
protected MapEntryUpdateResult<String, byte[]> putIfAbsent(Commit<? extends Put> commit) {
String key = commit.value().key();
MapEntryValue oldValue = entries().get(key);
// If the value is null, this is an INSERT.
if (valueIsNull(oldValue)) {
// If the key has been locked by a transaction, return a WRITE_LOCK error.
if (preparedKeys.contains(key)) {
return new MapEntryUpdateResult<>(
MapEntryUpdateResult.Status.WRITE_LOCK,
commit.index(),
key,
toVersioned(oldValue));
}
MapEntryValue newValue = new MapEntryValue(
MapEntryValue.Type.VALUE,
commit.index(),
commit.value().value(),
commit.wallClockTime().unixTimestamp(),
commit.value().ttl());
putValue(commit.value().key(), newValue);
Versioned<byte[]> result = toVersioned(newValue);
publish(new MapEvent<>(MapEvent.Type.INSERT, "", key, result, null));
return new MapEntryUpdateResult<>(MapEntryUpdateResult.Status.OK, commit.index(), key, null);
}
return new MapEntryUpdateResult<>(
MapEntryUpdateResult.Status.PRECONDITION_FAILED,
commit.index(),
key,
toVersioned(oldValue));
}
/**
* Handles a putAndGet commit.
*
* @param commit putAndGet commit
* @return map entry update result
*/
protected MapEntryUpdateResult<String, byte[]> putAndGet(Commit<? extends Put> commit) {
String key = commit.value().key();
MapEntryValue oldValue = entries().get(key);
MapEntryValue newValue = new MapEntryValue(MapEntryValue.Type.VALUE, commit.index(), commit.value().value(), commit.wallClockTime().unixTimestamp(), commit.value().ttl());
// If the value is null or a tombstone, this is an insert.
// Otherwise, only update the value if it has changed to reduce the number of events.
if (valueIsNull(oldValue)) {
// If the key has been locked by a transaction, return a WRITE_LOCK error.
if (preparedKeys.contains(key)) {
return new MapEntryUpdateResult<>(
MapEntryUpdateResult.Status.WRITE_LOCK,
commit.index(),
key,
toVersioned(oldValue));
}
putValue(commit.value().key(), newValue);
Versioned<byte[]> result = toVersioned(newValue);
publish(new MapEvent<>(MapEvent.Type.INSERT, "", key, result, null));
return new MapEntryUpdateResult<>(MapEntryUpdateResult.Status.OK, commit.index(), key, result);
} else if (!valuesEqual(oldValue, newValue)) {
// If the key has been locked by a transaction, return a WRITE_LOCK error.
if (preparedKeys.contains(key)) {
return new MapEntryUpdateResult<>(
MapEntryUpdateResult.Status.WRITE_LOCK,
commit.index(),
key,
toVersioned(oldValue));
}
putValue(commit.value().key(), newValue);
Versioned<byte[]> result = toVersioned(newValue);
publish(new MapEvent<>(MapEvent.Type.UPDATE, "", key, result, toVersioned(oldValue)));
return new MapEntryUpdateResult<>(MapEntryUpdateResult.Status.OK, commit.index(), key, result);
}
return new MapEntryUpdateResult<>(MapEntryUpdateResult.Status.NOOP, commit.index(), key, toVersioned(oldValue));
}
/**
* Handles a remove commit.
*
* @param index the commit index
* @param key the key to remove
* @param predicate predicate to determine whether to remove the entry
* @return map entry update result
*/
private MapEntryUpdateResult<String, byte[]> removeIf(long index, String key, Predicate<MapEntryValue> predicate) {
MapEntryValue value = entries().get(key);
// If the value does not exist or doesn't match the predicate, return a PRECONDITION_FAILED error.
if (valueIsNull(value) || !predicate.test(value)) {
return new MapEntryUpdateResult<>(MapEntryUpdateResult.Status.PRECONDITION_FAILED, index, key, null);
}
// If the key has been locked by a transaction, return a WRITE_LOCK error.
if (preparedKeys.contains(key)) {
return new MapEntryUpdateResult<>(MapEntryUpdateResult.Status.WRITE_LOCK, index, key, null);
}
// If no transactions are active, remove the key. Otherwise, replace it with a tombstone.
if (activeTransactions.isEmpty()) {
entries().remove(key);
} else {
entries().put(key, new MapEntryValue(MapEntryValue.Type.TOMBSTONE, index, null, 0, 0));
}
// Cancel the timer if one is scheduled.
cancelTtl(value);
Versioned<byte[]> result = toVersioned(value);
publish(new MapEvent<>(MapEvent.Type.REMOVE, "", key, null, result));
return new MapEntryUpdateResult<>(MapEntryUpdateResult.Status.OK, index, key, result);
}
/**
* Handles a remove commit.
*
* @param commit remove commit
* @return map entry update result
*/
protected MapEntryUpdateResult<String, byte[]> remove(Commit<? extends Remove> commit) {
return removeIf(commit.index(), commit.value().key(), v -> true);
}
/**
* Handles a removeValue commit.
*
* @param commit removeValue commit
* @return map entry update result
*/
protected MapEntryUpdateResult<String, byte[]> removeValue(Commit<? extends RemoveValue> commit) {
return removeIf(commit.index(), commit.value().key(), v ->
valuesEqual(v, new MapEntryValue(MapEntryValue.Type.VALUE, commit.index(), commit.value().value(), commit.wallClockTime().unixTimestamp(), 0)));
}
/**
* Handles a removeVersion commit.
*
* @param commit removeVersion commit
* @return map entry update result
*/
protected MapEntryUpdateResult<String, byte[]> removeVersion(Commit<? extends RemoveVersion> commit) {
return removeIf(commit.index(), commit.value().key(), v -> v.version() == commit.value().version());
}
/**
* Handles a replace commit.
*
* @param index the commit index
* @param key the key to replace
* @param newValue the value with which to replace the key
* @param predicate a predicate to determine whether to replace the key
* @return map entry update result
*/
private MapEntryUpdateResult<String, byte[]> replaceIf(
long index, String key, MapEntryValue newValue, Predicate<MapEntryValue> predicate) {
MapEntryValue oldValue = entries().get(key);
// If the key is not set or the current value doesn't match the predicate, return a PRECONDITION_FAILED error.
if (valueIsNull(oldValue) || !predicate.test(oldValue)) {
return new MapEntryUpdateResult<>(
MapEntryUpdateResult.Status.PRECONDITION_FAILED,
index,
key,
toVersioned(oldValue));
}
// If the key has been locked by a transaction, return a WRITE_LOCK error.
if (preparedKeys.contains(key)) {
return new MapEntryUpdateResult<>(MapEntryUpdateResult.Status.WRITE_LOCK, index, key, null);
}
putValue(key, newValue);
Versioned<byte[]> result = toVersioned(oldValue);
publish(new MapEvent<>(MapEvent.Type.UPDATE, "", key, toVersioned(newValue), result));
return new MapEntryUpdateResult<>(MapEntryUpdateResult.Status.OK, index, key, result);
}
/**
* Handles a replace commit.
*
* @param commit replace commit
* @return map entry update result
*/
protected MapEntryUpdateResult<String, byte[]> replace(Commit<? extends Replace> commit) {
MapEntryValue value = new MapEntryValue(
MapEntryValue.Type.VALUE,
commit.index(),
commit.value().value(),
commit.wallClockTime().unixTimestamp(),
0);
return replaceIf(commit.index(), commit.value().key(), value, v -> true);
}
/**
* Handles a replaceValue commit.
*
* @param commit replaceValue commit
* @return map entry update result
*/
protected MapEntryUpdateResult<String, byte[]> replaceValue(Commit<? extends ReplaceValue> commit) {
MapEntryValue value = new MapEntryValue(
MapEntryValue.Type.VALUE,
commit.index(),
commit.value().newValue(),
commit.wallClockTime().unixTimestamp(),
0);
return replaceIf(commit.index(), commit.value().key(), value,
v -> valuesEqual(v.value(), commit.value().oldValue()));
}
/**
* Handles a replaceVersion commit.
*
* @param commit replaceVersion commit
* @return map entry update result
*/
protected MapEntryUpdateResult<String, byte[]> replaceVersion(Commit<? extends ReplaceVersion> commit) {
MapEntryValue value = new MapEntryValue(
MapEntryValue.Type.VALUE,
commit.index(),
commit.value().newValue(),
commit.wallClockTime().unixTimestamp(),
0);
return replaceIf(commit.index(), commit.value().key(), value,
v -> v.version() == commit.value().oldVersion());
}
/**
* Handles a clear commit.
*
* @return clear result
*/
protected MapEntryUpdateResult.Status clear() {
Iterator<Map.Entry<String, MapEntryValue>> iterator = entries().entrySet().iterator();
Map<String, MapEntryValue> entriesToAdd = new HashMap<>();
while (iterator.hasNext()) {
Map.Entry<String, MapEntryValue> entry = iterator.next();
String key = entry.getKey();
MapEntryValue value = entry.getValue();
if (!valueIsNull(value)) {
Versioned<byte[]> removedValue = new Versioned<>(value.value(), value.version());
publish(new MapEvent<>(MapEvent.Type.REMOVE, "", key, null, removedValue));
cancelTtl(value);
if (activeTransactions.isEmpty()) {
iterator.remove();
} else {
entriesToAdd.put(key, new MapEntryValue(MapEntryValue.Type.TOMBSTONE, value.version, null, 0, 0));
}
}
}
entries().putAll(entriesToAdd);
return MapEntryUpdateResult.Status.OK;
}
/**
* Handles a listen commit.
*
* @param session listen session
*/
protected void listen(Session session) {
listeners.put(session.sessionId().id(), session);
}
/**
* Handles an unlisten commit.
*
* @param session unlisten session
*/
protected void unlisten(Session session) {
listeners.remove(session.sessionId().id());
}
/**
* Handles a begin commit.
*
* @param commit transaction begin commit
* @return transaction state version
*/
protected long begin(Commit<? extends TransactionBegin> commit) {
long version = commit.index();
activeTransactions.put(commit.value().transactionId(), new TransactionScope(version));
return version;
}
/**
* Handles an prepare and commit commit.
*
* @param commit transaction prepare and commit commit
* @return prepare result
*/
protected PrepareResult prepareAndCommit(Commit<? extends TransactionPrepareAndCommit> commit) {
TransactionId transactionId = commit.value().transactionLog().transactionId();
PrepareResult prepareResult = prepare(commit);
TransactionScope transactionScope = activeTransactions.remove(transactionId);
if (prepareResult == PrepareResult.OK) {
this.currentVersion = commit.index();
transactionScope = transactionScope.prepared(commit);
commitTransaction(transactionScope);
}
discardTombstones();
return prepareResult;
}
/**
* Handles an prepare commit.
*
* @param commit transaction prepare commit
* @return prepare result
*/
protected PrepareResult prepare(Commit<? extends TransactionPrepare> commit) {
try {
TransactionLog<MapUpdate<String, byte[]>> transactionLog = commit.value().transactionLog();
// Iterate through records in the transaction log and perform isolation checks.
for (MapUpdate<String, byte[]> record : transactionLog.records()) {
String key = record.key();
// If the record is a VERSION_MATCH then check that the record's version matches the current
// version of the state machine.
if (record.type() == MapUpdate.Type.VERSION_MATCH && key == null) {
if (record.version() > currentVersion) {
return PrepareResult.OPTIMISTIC_LOCK_FAILURE;
} else {
continue;
}
}
// If the prepared keys already contains the key contained within the record, that indicates a
// conflict with a concurrent transaction.
if (preparedKeys.contains(key)) {
return PrepareResult.CONCURRENT_TRANSACTION;
}
// Read the existing value from the map.
MapEntryValue existingValue = entries().get(key);
// Note: if the existing value is null, that means the key has not changed during the transaction,
// otherwise a tombstone would have been retained.
if (existingValue == null) {
// If the value is null, ensure the version is equal to the transaction version.
if (record.type() != Type.PUT_IF_ABSENT && record.version() != transactionLog.version()) {
return PrepareResult.OPTIMISTIC_LOCK_FAILURE;
}
} else {
// If the value is non-null, compare the current version with the record version.
if (existingValue.version() > record.version()) {
return PrepareResult.OPTIMISTIC_LOCK_FAILURE;
}
}
}
// No violations detected. Mark modified keys locked for transactions.
transactionLog.records().forEach(record -> {
if (record.type() != MapUpdate.Type.VERSION_MATCH) {
preparedKeys.add(record.key());
}
});
// Update the transaction scope. If the transaction scope is not set on this node, that indicates the
// coordinator is communicating with another node. Transactions assume that the client is communicating
// with a single leader in order to limit the overhead of retaining tombstones.
TransactionScope transactionScope = activeTransactions.get(transactionLog.transactionId());
if (transactionScope == null) {
activeTransactions.put(
transactionLog.transactionId(),
new TransactionScope(transactionLog.version(), commit.value().transactionLog()));
return PrepareResult.PARTIAL_FAILURE;
} else {
activeTransactions.put(
transactionLog.transactionId(),
transactionScope.prepared(commit));
return PrepareResult.OK;
}
} catch (Exception e) {
getLogger().warn("Failure applying {}", commit, e);
throw Throwables.propagate(e);
}
}
/**
* Handles an commit commit (ha!).
*
* @param commit transaction commit commit
* @return commit result
*/
protected CommitResult commit(Commit<? extends TransactionCommit> commit) {
TransactionId transactionId = commit.value().transactionId();
TransactionScope transactionScope = activeTransactions.remove(transactionId);
if (transactionScope == null) {
return CommitResult.UNKNOWN_TRANSACTION_ID;
}
try {
this.currentVersion = commit.index();
return commitTransaction(transactionScope);
} catch (Exception e) {
getLogger().warn("Failure applying {}", commit, e);
throw Throwables.propagate(e);
} finally {
discardTombstones();
}
}
/**
* Applies committed operations to the state machine.
*/
private CommitResult commitTransaction(TransactionScope transactionScope) {
TransactionLog<MapUpdate<String, byte[]>> transactionLog = transactionScope.transactionLog();
boolean retainTombstones = !activeTransactions.isEmpty();
List<MapEvent<String, byte[]>> eventsToPublish = Lists.newArrayList();
for (MapUpdate<String, byte[]> record : transactionLog.records()) {
if (record.type() == MapUpdate.Type.VERSION_MATCH) {
continue;
}
String key = record.key();
checkState(preparedKeys.remove(key), "key is not prepared");
if (record.type() == MapUpdate.Type.LOCK) {
continue;
}
MapEntryValue previousValue = entries().remove(key);
// Cancel the previous timer if set.
cancelTtl(previousValue);
MapEntryValue newValue = null;
// If the record is not a delete, create a transactional commit.
if (record.type() != MapUpdate.Type.REMOVE_IF_VERSION_MATCH) {
newValue = new MapEntryValue(MapEntryValue.Type.VALUE, currentVersion, record.value(), 0, 0);
} else if (retainTombstones) {
// For deletes, if tombstones need to be retained then create and store a tombstone commit.
newValue = new MapEntryValue(MapEntryValue.Type.TOMBSTONE, currentVersion, null, 0, 0);
}
MapEvent<String, byte[]> event;
if (newValue != null) {
entries().put(key, newValue);
if (!valueIsNull(newValue)) {
if (!valueIsNull(previousValue)) {
event = new MapEvent<>(
MapEvent.Type.UPDATE,
"",
key,
toVersioned(newValue),
toVersioned(previousValue));
} else {
event = new MapEvent<>(
MapEvent.Type.INSERT,
"",
key,
toVersioned(newValue),
null);
}
} else {
event = new MapEvent<>(
MapEvent.Type.REMOVE,
"",
key,
null,
toVersioned(previousValue));
}
} else {
event = new MapEvent<>(
MapEvent.Type.REMOVE,
"",
key,
null,
toVersioned(previousValue));
}
eventsToPublish.add(event);
}
publish(eventsToPublish);
return CommitResult.OK;
}
/**
* Handles an rollback commit (ha!).
*
* @param commit transaction rollback commit
* @return rollback result
*/
protected RollbackResult rollback(Commit<? extends TransactionRollback> commit) {
TransactionId transactionId = commit.value().transactionId();
TransactionScope transactionScope = activeTransactions.remove(transactionId);
if (transactionScope == null) {
return RollbackResult.UNKNOWN_TRANSACTION_ID;
} else if (!transactionScope.isPrepared()) {
discardTombstones();
return RollbackResult.OK;
} else {
try {
transactionScope.transactionLog().records()
.forEach(record -> {
if (record.type() != MapUpdate.Type.VERSION_MATCH) {
preparedKeys.remove(record.key());
}
});
return RollbackResult.OK;
} finally {
discardTombstones();
}
}
}
/**
* Discards tombstones no longer needed by active transactions.
*/
private void discardTombstones() {
if (activeTransactions.isEmpty()) {
Iterator<Map.Entry<String, MapEntryValue>> iterator = entries().entrySet().iterator();
while (iterator.hasNext()) {
MapEntryValue value = iterator.next().getValue();
if (value.type() == MapEntryValue.Type.TOMBSTONE) {
iterator.remove();
}
}
} else {
long lowWaterMark = activeTransactions.values().stream()
.mapToLong(TransactionScope::version)
.min().getAsLong();
Iterator<Map.Entry<String, MapEntryValue>> iterator = entries().entrySet().iterator();
while (iterator.hasNext()) {
MapEntryValue value = iterator.next().getValue();
if (value.type() == MapEntryValue.Type.TOMBSTONE && value.version < lowWaterMark) {
iterator.remove();
}
}
}
}
/**
* Utility for turning a {@code MapEntryValue} to {@code Versioned}.
*
* @param value map entry value
* @return versioned instance
*/
protected Versioned<byte[]> toVersioned(MapEntryValue value) {
return value != null && value.type() != MapEntryValue.Type.TOMBSTONE
? new Versioned<>(value.value(), value.version()) : null;
}
/**
* Publishes an event to listeners.
*
* @param event event to publish
*/
private void publish(MapEvent<String, byte[]> event) {
publish(Lists.newArrayList(event));
}
/**
* Publishes events to listeners.