-
Notifications
You must be signed in to change notification settings - Fork 25
/
ReplicaEngine.tla
972 lines (883 loc) · 55.2 KB
/
ReplicaEngine.tla
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
-------------------------- MODULE ReplicaEngine --------------------------
EXTENDS Naturals, FiniteSets, Sequences, TLC
(* Actions on the Lucene index *)
CONSTANTS Lucene_addDocuments, Lucene_updateDocuments, Lucene_deleteDocuments
CONSTANTS ADD, RETRY_ADD, UPDATE, DELETE, NULL
CONSTANTS DocContent
CONSTANTS DocAutoIdTimestamp
CONSTANTS DuplicationLimit
(* We model the activity of a single document, since distinct documents
(according to their IDs) are independent. Also each indexing operation
occurs under a lock for that document ID, so there is not much concurrency
to consider. *)
(* The set of individual requests that can occur on the document *)
Request(request_count)
(* ADD: An optimised append-only write can only occur as the first operation
on the document ID in seqno order. Any subsequent attempts to ADD the
document have the retry flag set and modelled as a RETRY_ADD. Other operations
on the document are also possible. *)
== [type : {ADD}, seqno : {1}, content : DocContent, autoIdTimeStamp : {DocAutoIdTimestamp}]
(* RETRY_ADD: A retry of a write that does involve an internally-generated
document ID. *)
\cup [type : {RETRY_ADD}, seqno : 1..request_count, content : DocContent, autoIdTimeStamp : {DocAutoIdTimestamp}]
(* UPDATE: A write that does not involve an internally-generated document ID. *)
\cup [type : {UPDATE}, seqno : 1..request_count, content : DocContent]
(* DELETE *)
\cup [type : {DELETE}, seqno : 1..request_count]
(* The set of sets of requests, which have distinct seqnos *)
RequestSet(request_count)
== { rs \in SUBSET Request(request_count):
/\ Cardinality(rs) = request_count
/\ Cardinality({r.seqno : r \in rs}) = request_count
/\ (* Also ADDs and RETRY_ADDs should have the same content *)
Cardinality({r.content: r \in { r \in rs: r.type \in {ADD, RETRY_ADD}}}) <= 1
}
(* Apply a set of operations to a document in seqno order *)
RECURSIVE ApplyOps(_, _, _)
ApplyOps(requests, nextSeqno, currentContent)
== IF \A r \in requests: r.seqno < nextSeqno
THEN currentContent
ELSE LET r == CHOOSE r \in requests: r.seqno = nextSeqno
IN IF r \in requests /\ r.seqno = nextSeqno
THEN ApplyOps(requests, nextSeqno + 1,
CASE r.type = DELETE -> NULL
[] r.type = ADD -> r.content
[] r.type = RETRY_ADD -> r.content
[] r.type = UPDATE -> r.content)
ELSE Assert(FALSE, "Bad sequence")
(* Calculate the final doc by applying all the requests in order *)
FinalDoc(requests) == ApplyOps(requests, 1, NULL)
(* Apply each the operation in the Lucene buffer, rejecting an
addDocuments when there is already a document present as this
would lead to duplication. *)
RECURSIVE ApplyBufferedOperations(_, _)
ApplyBufferedOperations(buffer, origDoc)
== IF buffer = <<>>
THEN origDoc
ELSE LET nextOp == Head(buffer)
IN ApplyBufferedOperations(Tail(buffer),
CASE nextOp.type = Lucene_deleteDocuments -> NULL
[] \/ nextOp.type = Lucene_updateDocuments
\/ /\ nextOp.type = Lucene_addDocuments
/\ origDoc = NULL -> [content |-> nextOp.content, seqno |-> nextOp.seqno]
[] OTHER -> Assert(FALSE, "Error: Lucene_addDocuments when origDoc /= NULL"))
Max(a,b) == IF a <= b THEN b ELSE a
(* --algorithm basic
variables
request_count \in 1..4,
replication_requests \in RequestSet(request_count),
expected_doc = FinalDoc(replication_requests),
versionMap_needsSafeAccess = FALSE,
versionMap_isUnsafe = FALSE,
versionMap_entry = NULL,
(* Other concurrent activity can flag that the version map needs to be safely accessed *)
process SafeAccessEnablerProcess = "SafeAccessEnabler"
begin
SafeAccessEnablerLoop:
while pc["Consumer"] /= "Done" do
versionMap_needsSafeAccess := (versionMap_needsSafeAccess = FALSE);
(* Technically the only way this can go back to FALSE is via a refresh, but
we should not need this fact, so model both kinds of change. *)
end while;
end process;
(* Other concurrent activity can make the version map become unsafe, if safe access mode is disabled *)
process UnsafePutterProcess = "UnsafePutter"
begin
UnsafePutterLoop:
while pc["Consumer"] /= "Done" do
await versionMap_needsSafeAccess = FALSE;
versionMap_isUnsafe := TRUE;
end while;
end process;
(* Other concurrent activity can increase the maxUnsafeAutoIdTimestamp *)
process MaxUnsafeAutoIdTimestampIncreaserProcess = "MaxUnsafeAutoIdTimestampIncreaser"
begin
MaxUnsafeAutoIdTimestampIncreaserLoop:
while pc["Consumer"] /= "Done" do
with newTimestamp \in {DocAutoIdTimestamp - 1, DocAutoIdTimestamp, DocAutoIdTimestamp + 1} do
await maxUnsafeAutoIdTimestamp < newTimestamp;
maxUnsafeAutoIdTimestamp := newTimestamp;
end with;
end while;
end process;
(* Lucene refreshes can happen at any time *)
process LuceneProcess = "ReplicaLucene"
variables
lucene_document = NULL,
lucene_buffer = <<>>,
begin
LuceneLoop:
while pc["Consumer"] /= "Done" \/ lucene_buffer /= <<>> do
lucene_document := ApplyBufferedOperations(lucene_buffer, lucene_document);
lucene_buffer := <<>>;
(* TODO Model the inner structure of the version map so this refresh can be
broken into the individual steps that occur concurrently with ongoing indexing. *)
versionMap_isUnsafe := FALSE;
versionMap_needsSafeAccess := FALSE;
if versionMap_entry /= NULL
then
if versionMap_entry.type = UPDATE
then
versionMap_entry := NULL;
else
assert versionMap_entry.type = DELETE;
versionMap_entry := [ versionMap_entry EXCEPT !.flushed = TRUE ];
end if;
end if;
end while;
end process;
(* Flushed deletes expire after a time and are cleaned up *)
process DeleteCollectorProcess = "DeleteCollector"
begin
DeleteCollectorLoop:
while pc["Consumer"] /= "Done" do
await /\ versionMap_entry /= NULL
/\ versionMap_entry.type = DELETE
/\ versionMap_entry.seqno <= localCheckPoint \* PR #28790
/\ versionMap_entry.flushed = TRUE;
versionMap_entry := NULL;
end while;
end process;
(* Local checkpoint advances as each operation is marked as completed *)
process LocalCheckpointTrackerProcess = "LocalCheckpointTracker"
variables
localCheckPoint = 0,
completedSeqnos = {}
begin
LocalCheckpointTrackerLoop:
while pc["Consumer"] /= "Done" do
await localCheckPoint + 1 \in completedSeqnos;
localCheckPoint := localCheckPoint + 1;
end while;
end process
process UnsafeSeqnoIncreaserProcess = "UnsafeSeqnoIncreaserProcess"
variables
maxSeqNoOfNonAppendOnlyOperations = 0,
begin
UnsafeSeqnoIncreaserProcessLoop:
while pc["Consumer"] /= "Done" /\ maxSeqNoOfNonAppendOnlyOperations < request_count + 1 do
maxSeqNoOfNonAppendOnlyOperations := maxSeqNoOfNonAppendOnlyOperations + 1;
end while;
end process
(* The process that consumes replication requests for a particular document ID, which
are processed in series because of the lock in the version map. *)
process ConsumerProcess = "Consumer"
variables
duplicationCount = 0,
maxUnsafeAutoIdTimestamp \in {0, DocAutoIdTimestamp - 1, DocAutoIdTimestamp, DocAutoIdTimestamp + 1},
req, plan,
deleteFromLucene, currentlyDeleted,
currentNotFoundOrDeleted, useLuceneUpdateDocument, indexIntoLucene,
begin
ConsumerLoop:
while replication_requests /= {} do
with replication_request \in replication_requests do
if replication_request.type = ADD
then
(* Never see two ADDs - if duplicated, one of them is a RETRY_ADD *)
either
(* Process ADD without duplication *)
replication_requests := replication_requests \ {replication_request};
req := replication_request;
or
await duplicationCount < DuplicationLimit;
duplicationCount := duplicationCount + 1;
(* Process ADD and leave a duplicate RETRY_ADD for later *)
replication_requests := (replication_requests \ {replication_request})
\cup {[replication_request EXCEPT !.type = RETRY_ADD]};
req := replication_request;
or
await duplicationCount < DuplicationLimit;
duplicationCount := duplicationCount + 1;
(* Process duplicate RETRY_ADD and leave the original ADD *)
req := [replication_request EXCEPT !.type = RETRY_ADD];
end either;
else
req := replication_request;
either
await duplicationCount < DuplicationLimit;
duplicationCount := duplicationCount + 1;
or
replication_requests := replication_requests \ {replication_request};
end either;
end if;
end with;
if req.type = DELETE
then
versionMap_needsSafeAccess := TRUE;
(* planDeletionAsNonPrimary *)
maxSeqNoOfNonAppendOnlyOperations := Max(maxSeqNoOfNonAppendOnlyOperations, req.seqno);
if req.seqno <= localCheckPoint
then
(* OP_STALE_OR_EQUAL *)
plan := "processButSkipLucene";
deleteFromLucene := FALSE;
currentlyDeleted := FALSE;
else
if versionMap_isUnsafe
then
(* Perform a Lucene refresh *)
AwaitRefreshOnDelete: \* Label here to allow for other concurrent activity
await lucene_buffer = <<>>;
versionMap_needsSafeAccess := TRUE;
end if;
compareDeleteOpToLuceneDocBasedOnSeqNo: \* Label needed because of AwaitRefreshOnDelete label
if versionMap_entry /= NULL
then
(* Doc is in version map *)
if req.seqno > versionMap_entry.seqno
then
(* OP_NEWER *)
plan := "processNormally";
deleteFromLucene := TRUE;
currentlyDeleted := FALSE;
else
(* OP_STALE_OR_EQUAL *)
plan := "processButSkipLucene";
deleteFromLucene := FALSE;
currentlyDeleted := FALSE;
end if;
else
(* Doc is not in version map - check Lucene *)
if lucene_document = NULL
then
(* LUCENE_DOC_NOT_FOUND *)
plan := "processNormallyExceptNotFound";
deleteFromLucene := TRUE;
currentlyDeleted := TRUE;
else
if req.seqno > lucene_document.seqno
then
(* OP_NEWER *)
plan := "processNormally";
deleteFromLucene := TRUE;
currentlyDeleted := FALSE;
else
(* OP_STALE_OR_EQUAL *)
plan := "processButSkipLucene";
deleteFromLucene := FALSE;
currentlyDeleted := FALSE;
end if;
end if;
end if;
end if;
ExecuteDeletePlan: \* Label needed because of AwaitRefreshOnDelete label
if deleteFromLucene
then
if currentlyDeleted = FALSE
then
lucene_buffer := Append(lucene_buffer, [ type |-> Lucene_deleteDocuments ]);
end if;
versionMap_entry := [ type |-> DELETE, seqno |-> req.seqno, flushed |-> FALSE ];
end if;
completedSeqnos := completedSeqnos \cup {req.seqno};
else
(* planIndexingAsNonPrimary *)
(* A RETRY_ADD has canOptimiseAddDocument = TRUE and
mayHaveBeenIndexedBefore = TRUE so is planned normally,
but also updates maxUnsafeAutoIdTimestamp within
mayHaveBeenIndexedBefore() *)
if req.type = RETRY_ADD
then
maxUnsafeAutoIdTimestamp := Max(maxUnsafeAutoIdTimestamp, req.autoIdTimeStamp);
end if;
(* An ADD can be optimized if mayHaveBeenIndexedBefore = FALSE
which is calculated by comparing timestamps. *)
if /\ req.type = ADD
/\ maxUnsafeAutoIdTimestamp < req.autoIdTimeStamp
/\ maxSeqNoOfNonAppendOnlyOperations < req.seqno \* PR #28787
then
plan := "optimisedAppendOnly";
currentNotFoundOrDeleted := TRUE;
useLuceneUpdateDocument := FALSE;
indexIntoLucene := TRUE;
else
if req.type \notin {ADD, RETRY_ADD}
then
maxSeqNoOfNonAppendOnlyOperations := Max(maxSeqNoOfNonAppendOnlyOperations, req.seqno);
end if;
(* All other operations are planned normally *)
versionMap_needsSafeAccess := TRUE;
if req.seqno <= localCheckPoint
then
(* OP_STALE_OR_EQUAL *)
plan := "processButSkipLucene";
currentNotFoundOrDeleted := FALSE;
useLuceneUpdateDocument := FALSE;
indexIntoLucene := FALSE;
else
if versionMap_isUnsafe
then
(* Perform a Lucene refresh *)
AwaitRefreshOnIndex: \* Label here to allow for other concurrent activity
await lucene_buffer = <<>>;
versionMap_needsSafeAccess := TRUE;
end if;
compareIndexOpToLuceneDocBasedOnSeqNo: \* Label needed because of AwaitRefreshOnIndex label
if req.seqno <= localCheckPoint \* PR #29276
then \* PR #29276
(* OP_STALE_OR_EQUAL *) \* PR #29276
plan := "processButSkipLucene"; \* PR #29276
currentNotFoundOrDeleted := FALSE; \* PR #29276
useLuceneUpdateDocument := FALSE; \* PR #29276
indexIntoLucene := FALSE; \* PR #29276
elsif versionMap_entry /= NULL
then
(* Doc is in version map *)
if req.seqno > versionMap_entry.seqno
then
(* OP_NEWER *)
plan := "processNormally";
currentNotFoundOrDeleted := FALSE;
useLuceneUpdateDocument := TRUE;
indexIntoLucene := TRUE;
else
(* OP_STALE_OR_EQUAL *)
plan := "processButSkipLucene";
currentNotFoundOrDeleted := FALSE;
useLuceneUpdateDocument := FALSE;
indexIntoLucene := FALSE;
end if;
else
(* Doc is not in version map - check Lucene *)
if lucene_document = NULL
then
(* LUCENE_DOC_NOT_FOUND *)
plan := "processNormallyExceptNotFound";
currentNotFoundOrDeleted := TRUE;
useLuceneUpdateDocument := FALSE;
indexIntoLucene := TRUE;
else
if req.seqno > lucene_document.seqno
then
(* OP_NEWER *)
plan := "processNormally";
currentNotFoundOrDeleted := FALSE;
useLuceneUpdateDocument := TRUE;
indexIntoLucene := TRUE;
else
(* OP_STALE_OR_EQUAL *)
plan := "processButSkipLucene";
currentNotFoundOrDeleted := FALSE;
useLuceneUpdateDocument := FALSE;
indexIntoLucene := FALSE;
end if;
end if;
end if;
end if;
end if;
(* planIndexingAsNonPrimary finished - now time to execute the plan *)
ExecuteIndexPlan: \* Label needed because of AwaitRefreshOnIndex label
if indexIntoLucene
then
lucene_buffer := Append(lucene_buffer,
[ type |-> IF useLuceneUpdateDocument THEN Lucene_updateDocuments ELSE Lucene_addDocuments
, seqno |-> req.seqno
, content |-> req.content
]);
if versionMap_needsSafeAccess
then
versionMap_entry := [ type |-> UPDATE, seqno |-> req.seqno ];
else
versionMap_isUnsafe := TRUE;
if /\ versionMap_entry /= NULL
/\ versionMap_entry.type = DELETE
/\ versionMap_entry.seqno < req.seqno
then
versionMap_entry := NULL; \* Desync bug #3 (no PR number yet)
end if;
end if;
end if;
completedSeqnos := completedSeqnos \cup {req.seqno}
end if;
end while;
end process
end algorithm
*)
\* BEGIN TRANSLATION
CONSTANT defaultInitValue
VARIABLES request_count, replication_requests, expected_doc,
versionMap_needsSafeAccess, versionMap_isUnsafe, versionMap_entry,
pc, lucene_document, lucene_buffer, localCheckPoint,
completedSeqnos, maxSeqNoOfNonAppendOnlyOperations,
duplicationCount, maxUnsafeAutoIdTimestamp, req, plan,
deleteFromLucene, currentlyDeleted, currentNotFoundOrDeleted,
useLuceneUpdateDocument, indexIntoLucene
vars == << request_count, replication_requests, expected_doc,
versionMap_needsSafeAccess, versionMap_isUnsafe, versionMap_entry,
pc, lucene_document, lucene_buffer, localCheckPoint,
completedSeqnos, maxSeqNoOfNonAppendOnlyOperations,
duplicationCount, maxUnsafeAutoIdTimestamp, req, plan,
deleteFromLucene, currentlyDeleted, currentNotFoundOrDeleted,
useLuceneUpdateDocument, indexIntoLucene >>
ProcSet == {"SafeAccessEnabler"} \cup {"UnsafePutter"} \cup {"MaxUnsafeAutoIdTimestampIncreaser"} \cup {"ReplicaLucene"} \cup {"DeleteCollector"} \cup {"LocalCheckpointTracker"} \cup {"UnsafeSeqnoIncreaserProcess"} \cup {"Consumer"}
Init == (* Global variables *)
/\ request_count \in 1..4
/\ replication_requests \in RequestSet(request_count)
/\ expected_doc = FinalDoc(replication_requests)
/\ versionMap_needsSafeAccess = FALSE
/\ versionMap_isUnsafe = FALSE
/\ versionMap_entry = NULL
(* Process LuceneProcess *)
/\ lucene_document = NULL
/\ lucene_buffer = <<>>
(* Process LocalCheckpointTrackerProcess *)
/\ localCheckPoint = 0
/\ completedSeqnos = {}
(* Process UnsafeSeqnoIncreaserProcess *)
/\ maxSeqNoOfNonAppendOnlyOperations = 0
(* Process ConsumerProcess *)
/\ duplicationCount = 0
/\ maxUnsafeAutoIdTimestamp \in {0, DocAutoIdTimestamp - 1, DocAutoIdTimestamp, DocAutoIdTimestamp + 1}
/\ req = defaultInitValue
/\ plan = defaultInitValue
/\ deleteFromLucene = defaultInitValue
/\ currentlyDeleted = defaultInitValue
/\ currentNotFoundOrDeleted = defaultInitValue
/\ useLuceneUpdateDocument = defaultInitValue
/\ indexIntoLucene = defaultInitValue
/\ pc = [self \in ProcSet |-> CASE self = "SafeAccessEnabler" -> "SafeAccessEnablerLoop"
[] self = "UnsafePutter" -> "UnsafePutterLoop"
[] self = "MaxUnsafeAutoIdTimestampIncreaser" -> "MaxUnsafeAutoIdTimestampIncreaserLoop"
[] self = "ReplicaLucene" -> "LuceneLoop"
[] self = "DeleteCollector" -> "DeleteCollectorLoop"
[] self = "LocalCheckpointTracker" -> "LocalCheckpointTrackerLoop"
[] self = "UnsafeSeqnoIncreaserProcess" -> "UnsafeSeqnoIncreaserProcessLoop"
[] self = "Consumer" -> "ConsumerLoop"]
SafeAccessEnablerLoop == /\ pc["SafeAccessEnabler"] = "SafeAccessEnablerLoop"
/\ IF pc["Consumer"] /= "Done"
THEN /\ versionMap_needsSafeAccess' = (versionMap_needsSafeAccess = FALSE)
/\ pc' = [pc EXCEPT !["SafeAccessEnabler"] = "SafeAccessEnablerLoop"]
ELSE /\ pc' = [pc EXCEPT !["SafeAccessEnabler"] = "Done"]
/\ UNCHANGED versionMap_needsSafeAccess
/\ UNCHANGED << request_count, replication_requests,
expected_doc, versionMap_isUnsafe,
versionMap_entry, lucene_document,
lucene_buffer, localCheckPoint,
completedSeqnos,
maxSeqNoOfNonAppendOnlyOperations,
duplicationCount,
maxUnsafeAutoIdTimestamp, req, plan,
deleteFromLucene, currentlyDeleted,
currentNotFoundOrDeleted,
useLuceneUpdateDocument,
indexIntoLucene >>
SafeAccessEnablerProcess == SafeAccessEnablerLoop
UnsafePutterLoop == /\ pc["UnsafePutter"] = "UnsafePutterLoop"
/\ IF pc["Consumer"] /= "Done"
THEN /\ versionMap_needsSafeAccess = FALSE
/\ versionMap_isUnsafe' = TRUE
/\ pc' = [pc EXCEPT !["UnsafePutter"] = "UnsafePutterLoop"]
ELSE /\ pc' = [pc EXCEPT !["UnsafePutter"] = "Done"]
/\ UNCHANGED versionMap_isUnsafe
/\ UNCHANGED << request_count, replication_requests,
expected_doc, versionMap_needsSafeAccess,
versionMap_entry, lucene_document,
lucene_buffer, localCheckPoint,
completedSeqnos,
maxSeqNoOfNonAppendOnlyOperations,
duplicationCount, maxUnsafeAutoIdTimestamp,
req, plan, deleteFromLucene,
currentlyDeleted, currentNotFoundOrDeleted,
useLuceneUpdateDocument, indexIntoLucene >>
UnsafePutterProcess == UnsafePutterLoop
MaxUnsafeAutoIdTimestampIncreaserLoop == /\ pc["MaxUnsafeAutoIdTimestampIncreaser"] = "MaxUnsafeAutoIdTimestampIncreaserLoop"
/\ IF pc["Consumer"] /= "Done"
THEN /\ \E newTimestamp \in {DocAutoIdTimestamp - 1, DocAutoIdTimestamp, DocAutoIdTimestamp + 1}:
/\ maxUnsafeAutoIdTimestamp < newTimestamp
/\ maxUnsafeAutoIdTimestamp' = newTimestamp
/\ pc' = [pc EXCEPT !["MaxUnsafeAutoIdTimestampIncreaser"] = "MaxUnsafeAutoIdTimestampIncreaserLoop"]
ELSE /\ pc' = [pc EXCEPT !["MaxUnsafeAutoIdTimestampIncreaser"] = "Done"]
/\ UNCHANGED maxUnsafeAutoIdTimestamp
/\ UNCHANGED << request_count,
replication_requests,
expected_doc,
versionMap_needsSafeAccess,
versionMap_isUnsafe,
versionMap_entry,
lucene_document,
lucene_buffer,
localCheckPoint,
completedSeqnos,
maxSeqNoOfNonAppendOnlyOperations,
duplicationCount, req,
plan,
deleteFromLucene,
currentlyDeleted,
currentNotFoundOrDeleted,
useLuceneUpdateDocument,
indexIntoLucene >>
MaxUnsafeAutoIdTimestampIncreaserProcess == MaxUnsafeAutoIdTimestampIncreaserLoop
LuceneLoop == /\ pc["ReplicaLucene"] = "LuceneLoop"
/\ IF pc["Consumer"] /= "Done" \/ lucene_buffer /= <<>>
THEN /\ lucene_document' = ApplyBufferedOperations(lucene_buffer, lucene_document)
/\ lucene_buffer' = <<>>
/\ versionMap_isUnsafe' = FALSE
/\ versionMap_needsSafeAccess' = FALSE
/\ IF versionMap_entry /= NULL
THEN /\ IF versionMap_entry.type = UPDATE
THEN /\ versionMap_entry' = NULL
ELSE /\ Assert(versionMap_entry.type = DELETE,
"Failure of assertion at line 147, column 17.")
/\ versionMap_entry' = [ versionMap_entry EXCEPT !.flushed = TRUE ]
ELSE /\ TRUE
/\ UNCHANGED versionMap_entry
/\ pc' = [pc EXCEPT !["ReplicaLucene"] = "LuceneLoop"]
ELSE /\ pc' = [pc EXCEPT !["ReplicaLucene"] = "Done"]
/\ UNCHANGED << versionMap_needsSafeAccess,
versionMap_isUnsafe, versionMap_entry,
lucene_document, lucene_buffer >>
/\ UNCHANGED << request_count, replication_requests,
expected_doc, localCheckPoint, completedSeqnos,
maxSeqNoOfNonAppendOnlyOperations,
duplicationCount, maxUnsafeAutoIdTimestamp, req,
plan, deleteFromLucene, currentlyDeleted,
currentNotFoundOrDeleted,
useLuceneUpdateDocument, indexIntoLucene >>
LuceneProcess == LuceneLoop
DeleteCollectorLoop == /\ pc["DeleteCollector"] = "DeleteCollectorLoop"
/\ IF pc["Consumer"] /= "Done"
THEN /\ /\ versionMap_entry /= NULL
/\ versionMap_entry.type = DELETE
/\ versionMap_entry.seqno <= localCheckPoint
/\ versionMap_entry.flushed = TRUE
/\ versionMap_entry' = NULL
/\ pc' = [pc EXCEPT !["DeleteCollector"] = "DeleteCollectorLoop"]
ELSE /\ pc' = [pc EXCEPT !["DeleteCollector"] = "Done"]
/\ UNCHANGED versionMap_entry
/\ UNCHANGED << request_count, replication_requests,
expected_doc,
versionMap_needsSafeAccess,
versionMap_isUnsafe, lucene_document,
lucene_buffer, localCheckPoint,
completedSeqnos,
maxSeqNoOfNonAppendOnlyOperations,
duplicationCount,
maxUnsafeAutoIdTimestamp, req, plan,
deleteFromLucene, currentlyDeleted,
currentNotFoundOrDeleted,
useLuceneUpdateDocument,
indexIntoLucene >>
DeleteCollectorProcess == DeleteCollectorLoop
LocalCheckpointTrackerLoop == /\ pc["LocalCheckpointTracker"] = "LocalCheckpointTrackerLoop"
/\ IF pc["Consumer"] /= "Done"
THEN /\ localCheckPoint + 1 \in completedSeqnos
/\ localCheckPoint' = localCheckPoint + 1
/\ pc' = [pc EXCEPT !["LocalCheckpointTracker"] = "LocalCheckpointTrackerLoop"]
ELSE /\ pc' = [pc EXCEPT !["LocalCheckpointTracker"] = "Done"]
/\ UNCHANGED localCheckPoint
/\ UNCHANGED << request_count,
replication_requests,
expected_doc,
versionMap_needsSafeAccess,
versionMap_isUnsafe,
versionMap_entry,
lucene_document, lucene_buffer,
completedSeqnos,
maxSeqNoOfNonAppendOnlyOperations,
duplicationCount,
maxUnsafeAutoIdTimestamp, req,
plan, deleteFromLucene,
currentlyDeleted,
currentNotFoundOrDeleted,
useLuceneUpdateDocument,
indexIntoLucene >>
LocalCheckpointTrackerProcess == LocalCheckpointTrackerLoop
UnsafeSeqnoIncreaserProcessLoop == /\ pc["UnsafeSeqnoIncreaserProcess"] = "UnsafeSeqnoIncreaserProcessLoop"
/\ IF pc["Consumer"] /= "Done" /\ maxSeqNoOfNonAppendOnlyOperations < request_count + 1
THEN /\ maxSeqNoOfNonAppendOnlyOperations' = maxSeqNoOfNonAppendOnlyOperations + 1
/\ pc' = [pc EXCEPT !["UnsafeSeqnoIncreaserProcess"] = "UnsafeSeqnoIncreaserProcessLoop"]
ELSE /\ pc' = [pc EXCEPT !["UnsafeSeqnoIncreaserProcess"] = "Done"]
/\ UNCHANGED maxSeqNoOfNonAppendOnlyOperations
/\ UNCHANGED << request_count,
replication_requests,
expected_doc,
versionMap_needsSafeAccess,
versionMap_isUnsafe,
versionMap_entry,
lucene_document,
lucene_buffer,
localCheckPoint,
completedSeqnos,
duplicationCount,
maxUnsafeAutoIdTimestamp,
req, plan, deleteFromLucene,
currentlyDeleted,
currentNotFoundOrDeleted,
useLuceneUpdateDocument,
indexIntoLucene >>
UnsafeSeqnoIncreaserProcess == UnsafeSeqnoIncreaserProcessLoop
ConsumerLoop == /\ pc["Consumer"] = "ConsumerLoop"
/\ IF replication_requests /= {}
THEN /\ \E replication_request \in replication_requests:
IF replication_request.type = ADD
THEN /\ \/ /\ replication_requests' = replication_requests \ {replication_request}
/\ req' = replication_request
/\ UNCHANGED duplicationCount
\/ /\ duplicationCount < DuplicationLimit
/\ duplicationCount' = duplicationCount + 1
/\ replication_requests' = (replication_requests \ {replication_request})
\cup {[replication_request EXCEPT !.type = RETRY_ADD]}
/\ req' = replication_request
\/ /\ duplicationCount < DuplicationLimit
/\ duplicationCount' = duplicationCount + 1
/\ req' = [replication_request EXCEPT !.type = RETRY_ADD]
/\ UNCHANGED replication_requests
ELSE /\ req' = replication_request
/\ \/ /\ duplicationCount < DuplicationLimit
/\ duplicationCount' = duplicationCount + 1
/\ UNCHANGED replication_requests
\/ /\ replication_requests' = replication_requests \ {replication_request}
/\ UNCHANGED duplicationCount
/\ IF req'.type = DELETE
THEN /\ versionMap_needsSafeAccess' = TRUE
/\ maxSeqNoOfNonAppendOnlyOperations' = Max(maxSeqNoOfNonAppendOnlyOperations, req'.seqno)
/\ IF req'.seqno <= localCheckPoint
THEN /\ plan' = "processButSkipLucene"
/\ deleteFromLucene' = FALSE
/\ currentlyDeleted' = FALSE
/\ pc' = [pc EXCEPT !["Consumer"] = "ExecuteDeletePlan"]
ELSE /\ IF versionMap_isUnsafe
THEN /\ pc' = [pc EXCEPT !["Consumer"] = "AwaitRefreshOnDelete"]
ELSE /\ pc' = [pc EXCEPT !["Consumer"] = "compareDeleteOpToLuceneDocBasedOnSeqNo"]
/\ UNCHANGED << plan,
deleteFromLucene,
currentlyDeleted >>
/\ UNCHANGED << maxUnsafeAutoIdTimestamp,
currentNotFoundOrDeleted,
useLuceneUpdateDocument,
indexIntoLucene >>
ELSE /\ IF req'.type = RETRY_ADD
THEN /\ maxUnsafeAutoIdTimestamp' = Max(maxUnsafeAutoIdTimestamp, req'.autoIdTimeStamp)
ELSE /\ TRUE
/\ UNCHANGED maxUnsafeAutoIdTimestamp
/\ IF /\ req'.type = ADD
/\ maxUnsafeAutoIdTimestamp' < req'.autoIdTimeStamp
/\ maxSeqNoOfNonAppendOnlyOperations < req'.seqno
THEN /\ plan' = "optimisedAppendOnly"
/\ currentNotFoundOrDeleted' = TRUE
/\ useLuceneUpdateDocument' = FALSE
/\ indexIntoLucene' = TRUE
/\ pc' = [pc EXCEPT !["Consumer"] = "ExecuteIndexPlan"]
/\ UNCHANGED << versionMap_needsSafeAccess,
maxSeqNoOfNonAppendOnlyOperations >>
ELSE /\ IF req'.type \notin {ADD, RETRY_ADD}
THEN /\ maxSeqNoOfNonAppendOnlyOperations' = Max(maxSeqNoOfNonAppendOnlyOperations, req'.seqno)
ELSE /\ TRUE
/\ UNCHANGED maxSeqNoOfNonAppendOnlyOperations
/\ versionMap_needsSafeAccess' = TRUE
/\ IF req'.seqno <= localCheckPoint
THEN /\ plan' = "processButSkipLucene"
/\ currentNotFoundOrDeleted' = FALSE
/\ useLuceneUpdateDocument' = FALSE
/\ indexIntoLucene' = FALSE
/\ pc' = [pc EXCEPT !["Consumer"] = "ExecuteIndexPlan"]
ELSE /\ IF versionMap_isUnsafe
THEN /\ pc' = [pc EXCEPT !["Consumer"] = "AwaitRefreshOnIndex"]
ELSE /\ pc' = [pc EXCEPT !["Consumer"] = "compareIndexOpToLuceneDocBasedOnSeqNo"]
/\ UNCHANGED << plan,
currentNotFoundOrDeleted,
useLuceneUpdateDocument,
indexIntoLucene >>
/\ UNCHANGED << deleteFromLucene,
currentlyDeleted >>
ELSE /\ pc' = [pc EXCEPT !["Consumer"] = "Done"]
/\ UNCHANGED << replication_requests,
versionMap_needsSafeAccess,
maxSeqNoOfNonAppendOnlyOperations,
duplicationCount,
maxUnsafeAutoIdTimestamp, req, plan,
deleteFromLucene, currentlyDeleted,
currentNotFoundOrDeleted,
useLuceneUpdateDocument,
indexIntoLucene >>
/\ UNCHANGED << request_count, expected_doc,
versionMap_isUnsafe, versionMap_entry,
lucene_document, lucene_buffer,
localCheckPoint, completedSeqnos >>
ExecuteDeletePlan == /\ pc["Consumer"] = "ExecuteDeletePlan"
/\ IF deleteFromLucene
THEN /\ IF currentlyDeleted = FALSE
THEN /\ lucene_buffer' = Append(lucene_buffer, [ type |-> Lucene_deleteDocuments ])
ELSE /\ TRUE
/\ UNCHANGED lucene_buffer
/\ versionMap_entry' = [ type |-> DELETE, seqno |-> req.seqno, flushed |-> FALSE ]
ELSE /\ TRUE
/\ UNCHANGED << versionMap_entry,
lucene_buffer >>
/\ completedSeqnos' = (completedSeqnos \cup {req.seqno})
/\ pc' = [pc EXCEPT !["Consumer"] = "ConsumerLoop"]
/\ UNCHANGED << request_count, replication_requests,
expected_doc, versionMap_needsSafeAccess,
versionMap_isUnsafe, lucene_document,
localCheckPoint,
maxSeqNoOfNonAppendOnlyOperations,
duplicationCount,
maxUnsafeAutoIdTimestamp, req, plan,
deleteFromLucene, currentlyDeleted,
currentNotFoundOrDeleted,
useLuceneUpdateDocument, indexIntoLucene >>
ExecuteIndexPlan == /\ pc["Consumer"] = "ExecuteIndexPlan"
/\ IF indexIntoLucene
THEN /\ lucene_buffer' = Append(lucene_buffer,
[ type |-> IF useLuceneUpdateDocument THEN Lucene_updateDocuments ELSE Lucene_addDocuments
, seqno |-> req.seqno
, content |-> req.content
])
/\ IF versionMap_needsSafeAccess
THEN /\ versionMap_entry' = [ type |-> UPDATE, seqno |-> req.seqno ]
/\ UNCHANGED versionMap_isUnsafe
ELSE /\ versionMap_isUnsafe' = TRUE
/\ IF /\ versionMap_entry /= NULL
/\ versionMap_entry.type = DELETE
/\ versionMap_entry.seqno < req.seqno
THEN /\ versionMap_entry' = NULL
ELSE /\ TRUE
/\ UNCHANGED versionMap_entry
ELSE /\ TRUE
/\ UNCHANGED << versionMap_isUnsafe,
versionMap_entry, lucene_buffer >>
/\ completedSeqnos' = (completedSeqnos \cup {req.seqno})
/\ pc' = [pc EXCEPT !["Consumer"] = "ConsumerLoop"]
/\ UNCHANGED << request_count, replication_requests,
expected_doc, versionMap_needsSafeAccess,
lucene_document, localCheckPoint,
maxSeqNoOfNonAppendOnlyOperations,
duplicationCount, maxUnsafeAutoIdTimestamp,
req, plan, deleteFromLucene,
currentlyDeleted, currentNotFoundOrDeleted,
useLuceneUpdateDocument, indexIntoLucene >>
compareDeleteOpToLuceneDocBasedOnSeqNo == /\ pc["Consumer"] = "compareDeleteOpToLuceneDocBasedOnSeqNo"
/\ IF versionMap_entry /= NULL
THEN /\ IF req.seqno > versionMap_entry.seqno
THEN /\ plan' = "processNormally"
/\ deleteFromLucene' = TRUE
/\ currentlyDeleted' = FALSE
ELSE /\ plan' = "processButSkipLucene"
/\ deleteFromLucene' = FALSE
/\ currentlyDeleted' = FALSE
ELSE /\ IF lucene_document = NULL
THEN /\ plan' = "processNormallyExceptNotFound"
/\ deleteFromLucene' = TRUE
/\ currentlyDeleted' = TRUE
ELSE /\ IF req.seqno > lucene_document.seqno
THEN /\ plan' = "processNormally"
/\ deleteFromLucene' = TRUE
/\ currentlyDeleted' = FALSE
ELSE /\ plan' = "processButSkipLucene"
/\ deleteFromLucene' = FALSE
/\ currentlyDeleted' = FALSE
/\ pc' = [pc EXCEPT !["Consumer"] = "ExecuteDeletePlan"]
/\ UNCHANGED << request_count,
replication_requests,
expected_doc,
versionMap_needsSafeAccess,
versionMap_isUnsafe,
versionMap_entry,
lucene_document,
lucene_buffer,
localCheckPoint,
completedSeqnos,
maxSeqNoOfNonAppendOnlyOperations,
duplicationCount,
maxUnsafeAutoIdTimestamp,
req,
currentNotFoundOrDeleted,
useLuceneUpdateDocument,
indexIntoLucene >>
AwaitRefreshOnDelete == /\ pc["Consumer"] = "AwaitRefreshOnDelete"
/\ lucene_buffer = <<>>
/\ versionMap_needsSafeAccess' = TRUE
/\ pc' = [pc EXCEPT !["Consumer"] = "compareDeleteOpToLuceneDocBasedOnSeqNo"]
/\ UNCHANGED << request_count, replication_requests,
expected_doc, versionMap_isUnsafe,
versionMap_entry, lucene_document,
lucene_buffer, localCheckPoint,
completedSeqnos,
maxSeqNoOfNonAppendOnlyOperations,
duplicationCount,
maxUnsafeAutoIdTimestamp, req, plan,
deleteFromLucene, currentlyDeleted,
currentNotFoundOrDeleted,
useLuceneUpdateDocument,
indexIntoLucene >>
compareIndexOpToLuceneDocBasedOnSeqNo == /\ pc["Consumer"] = "compareIndexOpToLuceneDocBasedOnSeqNo"
/\ IF req.seqno <= localCheckPoint
THEN /\ plan' = "processButSkipLucene"
/\ currentNotFoundOrDeleted' = FALSE
/\ useLuceneUpdateDocument' = FALSE
/\ indexIntoLucene' = FALSE
ELSE /\ IF versionMap_entry /= NULL
THEN /\ IF req.seqno > versionMap_entry.seqno
THEN /\ plan' = "processNormally"
/\ currentNotFoundOrDeleted' = FALSE
/\ useLuceneUpdateDocument' = TRUE
/\ indexIntoLucene' = TRUE
ELSE /\ plan' = "processButSkipLucene"
/\ currentNotFoundOrDeleted' = FALSE
/\ useLuceneUpdateDocument' = FALSE
/\ indexIntoLucene' = FALSE
ELSE /\ IF lucene_document = NULL
THEN /\ plan' = "processNormallyExceptNotFound"
/\ currentNotFoundOrDeleted' = TRUE
/\ useLuceneUpdateDocument' = FALSE
/\ indexIntoLucene' = TRUE
ELSE /\ IF req.seqno > lucene_document.seqno
THEN /\ plan' = "processNormally"
/\ currentNotFoundOrDeleted' = FALSE
/\ useLuceneUpdateDocument' = TRUE
/\ indexIntoLucene' = TRUE
ELSE /\ plan' = "processButSkipLucene"
/\ currentNotFoundOrDeleted' = FALSE
/\ useLuceneUpdateDocument' = FALSE
/\ indexIntoLucene' = FALSE
/\ pc' = [pc EXCEPT !["Consumer"] = "ExecuteIndexPlan"]
/\ UNCHANGED << request_count,
replication_requests,
expected_doc,
versionMap_needsSafeAccess,
versionMap_isUnsafe,
versionMap_entry,
lucene_document,
lucene_buffer,
localCheckPoint,
completedSeqnos,
maxSeqNoOfNonAppendOnlyOperations,
duplicationCount,
maxUnsafeAutoIdTimestamp,
req, deleteFromLucene,
currentlyDeleted >>
AwaitRefreshOnIndex == /\ pc["Consumer"] = "AwaitRefreshOnIndex"
/\ lucene_buffer = <<>>
/\ versionMap_needsSafeAccess' = TRUE
/\ pc' = [pc EXCEPT !["Consumer"] = "compareIndexOpToLuceneDocBasedOnSeqNo"]
/\ UNCHANGED << request_count, replication_requests,
expected_doc, versionMap_isUnsafe,
versionMap_entry, lucene_document,
lucene_buffer, localCheckPoint,
completedSeqnos,
maxSeqNoOfNonAppendOnlyOperations,
duplicationCount,
maxUnsafeAutoIdTimestamp, req, plan,
deleteFromLucene, currentlyDeleted,
currentNotFoundOrDeleted,
useLuceneUpdateDocument,
indexIntoLucene >>
ConsumerProcess == ConsumerLoop \/ ExecuteDeletePlan \/ ExecuteIndexPlan
\/ compareDeleteOpToLuceneDocBasedOnSeqNo
\/ AwaitRefreshOnDelete
\/ compareIndexOpToLuceneDocBasedOnSeqNo
\/ AwaitRefreshOnIndex
Next == SafeAccessEnablerProcess \/ UnsafePutterProcess
\/ MaxUnsafeAutoIdTimestampIncreaserProcess \/ LuceneProcess
\/ DeleteCollectorProcess \/ LocalCheckpointTrackerProcess
\/ UnsafeSeqnoIncreaserProcess \/ ConsumerProcess
\/ (* Disjunct to prevent deadlock on termination *)
((\A self \in ProcSet: pc[self] = "Done") /\ UNCHANGED vars)
Spec == Init /\ [][Next]_vars
Termination == <>(\A self \in ProcSet: pc[self] = "Done")
\* END TRANSLATION
Terminated == \A self \in ProcSet: pc[self] = "Done"
Invariant == Terminated => /\ expected_doc = NULL => lucene_document = NULL
/\ expected_doc /= NULL => lucene_document.content = expected_doc
=============================================================================