forked from adaloveless/commonx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArcLogShipper.pas
2637 lines (2315 loc) · 73.8 KB
/
ArcLogShipper.pas
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
unit ArcLogShipper;
{x$DEFINE DISABLE_SHIPPER}
{x$DEFINE USE_TCP}
{x$DEFINE CONNECTIONLESS_ROLLBACK}
{$DEFINE SINGLE_SHIPPER}
interface
{$DEFINE LAME_POOL}
{x$DEFINE LOCAL_TEMP}
{$DEFINE SUCCESS_HOOK}
{$DEFINE COMBINE_RING_LOGS}
{x$DEFINE USE_RING_FILE}
{x$DEFINE USE_NEW_ARC_ZONE_CHECKSUMS}
{x$DEFINE POST_BY_COMMAND}
{x$DEFINE VERIFY}
//truth
//MODE - Real-time
// Mainthread
// -- If there's room in the ring put it. Update local arcmap right away
// --else switch modes, flag zone in arcmap with newer rev, but don't add anything to ring
// ShipperThread
// -- Get stuff from ring
// -- If the stuff is >= arcmap rev level the push it
// -- If the stuff is > arcmap rev level then update arcmap
//MODO - Pseudo-real-time
// Mainthread
// -- If there's room in the ring put it. Update Local arcmap right away
// -- flag zone in arcmap with newer rev, but don't add anything to ring
// -- if zoneidx < validationidx then flag that we need to make another pass
// ShipperThread
// -- Get stuff from ring
// -- If the stuff is >= arcmap rev level the push it
// -- If the stuff is > arcmap rev level then update arcmap
// -- CheckRev level At validationidx
// -- if arcmap rev > remote rev then
// -- fetch zone, push zone
// 1. LOCKED - Gather DAta - localrev, remoterev
// 2. serialized - Get Data from Disk
// 2. UNLOCKED - Push Data - remoterev is changed to remoterev + 1
// 3. LOCKED - Commit - localrev is updated to
//Race #1
// 3 STATES to worry about - LocalRev (target) RemoteRev, RRC (RemoteRevCache)
// ------ Start Local [0] RRRC [0] Remote[0] --------------------
// [THREAD 1 -----------] [THREAD 2 ----------]
// Gather [0][0][0] T=1
// Gather [0][0][0] T=1
// Push 0->1
// Push 0->1
// ^ rslt = TRUE ^result = FALSE (backend rejected it under lock)
// State is [0][0][1]
// Commit OK! [1][1][1]
// Rollback Check/Verify that backend is [1] and force change to RRC
// Gather [1][1][1] T=2
// Push 1->2
// ^result = TRUE
// State is [1][1][2]
// Commit OK! [2][2][2]
// ------ End Local [2] RRC[2] Remote [2] --------------------
//Race #2
// 3 STATES to worry about - LocalRev (target) RemoteRev, RRC (RemoteRevCache)
// ------ Start Local [0] RRC[0] Remote [0] --------------------
// [THREAD 1 -----------] [THREAD 2 ----------]
// Start [0][0][0]
// Gather [0][0][0]
// Push 0->1 (OK!)
// STate is [0][0][1]
// Gather[0][0][0] (since there's a change gather to reach backend, state might be [0][1][1])
// Commit OK! [1][1][1]
//
// Push 0->1 (FAIL!)
// Rollback Check/Verify that backend is [1]
// Gather[1] Target [2]
// Push 1->2
// ^result = TRUE
// Commit OK! [2][2][2]
// ------ End Local [2] Remote [2] RRC[2] --------------------
//Race #2a gather takes state from backend
// 3 STATES to worry about - LocalRev (target) RemoteRev, RRC (RemoteRevCache)
// ------ Start Local [0] RRC[0] Remote [0] --------------------
// [THREAD 1 -----------] [THREAD 2 ----------]
// Start [0][0][0]
// Gather [0][0][0]
// Push 0->1 (OK!)
// STate is [0][0][1]
// Gather[0][1][1] (alternate result fetched from back-end
// Commit OK! [1][1][1]
// Push 1->2
// ^result = TRUE
// Commit OK! [2][2][2]
// ------ End Local [2] Remote [2] RRC[2] --------------------
//Race #3 (Validate/refresh vs. Ring Push)
// 3 STATES to worry about - LocalRev (target) RRC (RemoteRevCache) RemoteRev
// ------ Start Local [0] RRC[0] Remote [0] --------------------
// [THREAD 1 (VR)---------] [THREAD 2 -------------] [THREAD 3 -------------]
// Start [0][0][0]
// Gather [0][0][0] target [1]
// Push 0->1 (OK!)
// Commit OK! [1][1][1]
// L Examine localrev [1]
// L Add Data to Ring [1->2]
// L Update localrev [2][1][1]
// Get DAta From Ring [1->2]
// FALSE [1]>=[2] //if RRC >= localrev then data irrelevant
// Push [1->2] OK! [2][1][2]
// Commit! OK! [2][2][2]
// L Examine localrev [2]
// L Add Data to Ring [2->3]
// L Update localrev [3][2][2]
// Get DAta From Ring [2->3]
// FALSE [2]>=[3] //if RRC >= localrev then data irrelevant
// Push [2->3] OK! [3][2][3]
// Commit! OK! [3][3][3]
//Race #3A (Validate/refresh vs. Ring Push)
// 3 STATES to worry about - LocalRev (target) RRC (RemoteRevCache) RemoteRev
// ------ Start Local [0] RRC[0] Remote [0] --------------------
// [THREAD 1 (VR)---------] [THREAD 2 -------------] [THREAD 3 -------------]
// Start [0][0][0]
// Gather [0][0][0] target [1]
// Push 0->1 (OK!)
// Commit OK! [1][1][1]
// L Examine localrev [1]
// L Add Data to Ring [1->2]
// L Update localrev [2][1][1]
// Get DAta From Ring [1->2]
// L Examine localrev [2]
// L Add Data to Ring [2->3]
// L Update localrev [3][1][1]
// FALSE [1]>=[2] //if RRC >= localrev then data irrelevant
// Push [1->2] OK! [3][1][2]
// Commit! OK! [3][2][2]
// //
// Get DAta From Ring [2->3]
// FALSE [2]>=[3] //if RRC >= localrev then data irrelevant
// Push [2->3] OK! [3][2][3]
// Commit! OK! [3][3][3]
// Final STate [3] [3] [3] OK!
//Race #3B (Validate/refresh vs. Ring Push)
// 3 STATES to worry about - LocalRev (target) RRC (RemoteRevCache) RemoteRev
// ------ Start Local [0] RRC[0] Remote [0] --------------------
// [THREAD 1 (VR)---------] [THREAD 2 -------------] [THREAD 3 -------------]
// Start [0][0][0]
// Gather [0][0][0] target [1]
// Push 0->1 (OK!)
// Commit OK! [1][1][1]
// L Examine localrev [1]
// L Add Data to Ring [1->2]
// L Update localrev [2][1][1]
// Get DAta From Ring [1->2]
// FALSE [1]>=[2] //if RRC >= localrev then data irrelevant
// Push [1->2] OK! [2][1][1->2]
// L Examine localrev [2]
// L Add Data to Ring [2->3]
// L Update localrev [2->3][1][2]
// Commit! OK! [3][1->2][2]
// //
// Get DAta From Ring [2->3]
// FALSE [2]>=[3] //if RRC >= localrev then data irrelevant
// Push [2->3] OK! [3][2][2->3]
// Commit! OK! [3][2->3][3]
// Final STate [3] [3] [3] OK!
//Race #3C (Validate/refresh vs. Ring Push)
// 3 STATES to worry about - LocalRev (target) RRC (RemoteRevCache) RemoteRev
// ------ Start Local [0] RRC[0] Remote [0] --------------------
// [THREAD 1 (VR)---------] [THREAD 2 -------------] [THREAD 3 -------------]
// Start [0][0][0]
// Gather [0][0][0] target [1]
// Push 0->1 (OK!)
// Commit OK! [1][1][1]
// L Examine localrev [1]
// L Add Data to Ring [1->2]
// L Update localrev [2][1][1]
// Get DAta From Ring [1->2]
// FALSE [1]>=[2] //if RRC >= localrev then data irrelevant
// Push [1->2] OK! [2][1][1->2]
// L Examine localrev [2]
// L Add Data to Ring [2->3]
// L Update localrev [2->3][1][2]
// Start [3][1][2]
// Gather [3][1][2] target [2]
// Push [1->2] (FAIL! Backend is at 2!}
// Rollback! Update Local State to match backend [3][1->2][2]
// RETRY!
// Gather [3][2][2] target [3]
// Push [3][2][2->3] ok!
// Commit OK! [3][2->3][3]
// Commit! OK! [3][3(nc)][3]
// //
// Get DAta From Ring [2->3]
// TRUE [3]>=[3] //if RRC >= localrev then data irrelevant
// No Push Since Equal!
// Commit! OK! [3][3(nc)][3]
// Final STate [3] [3] [3] OK!
//Race #3C (Validate/refresh vs. Ring Push)
// 3 STATES to worry about - LocalRev (target) RRC (RemoteRevCache) RemoteRev
// ------ Start Local [0] RRC[0] Remote [0] --------------------
// [THREAD 1 (VR)---------] [THREAD 2 -------------] [THREAD 3 -------------]
// Start [0][0][0]
// Gather [0][0][0] target [1]
// Push 0->1 (OK!)
// Commit OK! [1][1][1]
// L Examine localrev [1]
// L Add Data to Ring [1->2]
// L Update localrev [2][1][1]
// Get DAta From Ring [1->2]
// FALSE [1]>=[2] //if RRC >= localrev then data irrelevant
// Push [1->2] OK! [2][1][1->2]
// L Examine localrev [2]
// L Add Data to Ring [2->3]
// L Update localrev [2->3][1][2]
// Start [3][1][2]
// Gather [3][1][2] target [2]
// Push [1->2] (FAIL! Backend is at 2!}
// Rollback! Update Local State to match backend [3][1->2][2]
// RETRY!
// Gather [3][2][2] target [3]
// Push [3][2][2->3] ok!
// Commit! OK! [3][1->2)][3]
// Commit OK! [3][2->3][3]
// //
// Get DAta From Ring [2->3]
// TRUE [3]>=[3] //if RRC >= localrev then data irrelevant
// No Push Since Equal!
// Commit! OK! [3][3(nc)][3]
// Final STate [3] [3] [3] OK!
//Race #3C (Validate/refresh vs. Ring Push)
// 3 STATES to worry about - LocalRev (target) RRC (RemoteRevCache) RemoteRev
// ------ Start Local [0] RRC[0] Remote [0] --------------------
// [THREAD 1 (VR)---------] [THREAD 2 -------------] [THREAD 3 -------------]
// Start [0][0][0]
// Gather [0][0][0] target [1]
// Push 0->1 (OK!)
// Commit OK! [1][1][1]
// L Examine localrev [1]
// L Add Data to Ring [1->2]
// L Update localrev [2][1][1]
// Get DAta From Ring [1->2]
// FALSE [1]>=[2] //if RRC >= localrev then data irrelevant
// Start [2][1][1]
// Gather [2][1][1] target [2]
// Push [2][1][1->2] OK!
// Push [1->2] FAIL! [2][1][!!! is already 2]
// Rollback refresh state [2][1->2][2]
// TRUE [2]>=[2] //if RRC >= localrev then data is irrelevant
// No push Since Equal!
// L Examine localrev [2]
// L Add Data to Ring [2->3]
// L Update localrev [2->3][1][2]
// Commit OK! [3][2(nc)][2]
// Commit! OK! [3][2nc)][2]
// //
// Get DAta From Ring [2->3]
// FALSE [2]>=[3] //if RRC >= localrev then data irrelevant
// Push [3][2][2->3]
// Commit! OK! [3][2->3][3]
// Final STate [3] [3] [3] OK!
//TODO 1: Make sure that on transaction commit, localrev only rolls FORWARD
uses
perfmessage,perfmessageclient,numbers, typex, systemx, stringx, RDTPArchiveClient, LockQueue, managedthread, sysutils, ringfile, virtualdiskconstants, signals, arcmap, debug, tickcount, commandprocessor, simplequeue, ringbuffer, transaction, generics.collections, better_collections, commandicons;
const
RESERVE_ID_COUNT = 1000000;
MAX_UNIQUE_CLIENTS = 32;
RESERVE_UNIQUE_CLIENTS = 24;
ZONES_TO_BACKUP = { $200} MAX_ZONES;
ARC_ZONE_CHECKSUM_SIZE = $10000;
type
{$IFDEF USE_RING_FILE}
TLocalRing = TRingFile;
{$ELSE}
TLocalRing = TDoubleRingBuffer;
{$ENDIF}
TArcMode = (arcValidateRefresh, arcRealTime);
TArcLogShipper = class;//forward
Tcmd_ContinueValidateLogRefresh = class;//forward
Ttrans_LogThis = class;//forward
TArcTransactionalCommand = class;//forward
TArcLogShippingthread = class(TManagedThread)
public
shipper: TArcLogShipper;
procedure DoExecute;override;
end;
TLogShipHeader = packed record
public
logid: int64;
blockstart: int64;
blocklength: int64;
bytelength: int64;
headercheck: int64;
function ComputeCheck: int64;inline;
procedure SetCheck;inline;
function IsCheckValid: boolean;inline;
procedure Init;inline;
end;
TArcLogShipper = class(TFakeLockQueue)
private
FTargetArchive: string;
FReadyForRealtime: boolean;
FSourceArchive: string;
FClient, FQUickClient: TRDTPArchiveClient;
FActive: boolean;
FArchive: string;
ring: TLocalRing;
FStatus: string;
FThr: TArcLogShippingThread;
[volatile] FMode: TArcMode;
temp: TDynByteArray;
function GetEndpoint: string;
function GetHost: string;
procedure SetEndPOint(value: string);
procedure SetHost(value: string);
procedure SetArchive(value: string);
function GetRingFileName: string;
procedure SetRingFileName(value: string);
procedure SetMode(const Value: TArcMode);
procedure CancelValBAtches;
function NoneUsingClient(cli: TRDTPArchiveClient): boolean;
procedure FinishValBatches(bAll: boolean; bCancel: boolean = false);
procedure CollapseValbatches;
function SearchValBatchForZone(zidx: int64; bCheckOnlyTransLogThis: boolean): TArcTransactionalCommand;
function GetArcZonesInDisk: int64;
function GetdiskSize: int64;
function GEtQuickClient: TRDTPArchiveClient;
protected
function CanActivate: boolean;
procedure Activate;
procedure DestroyClientPool;
public
phIO, phRingR, phRingW, phNet: TPerfHandle;
FClients: TSharedList<TRDTPArchiveClient>;
disk: TObject;
// arcmap: TArcMap;
validateidx: int64;
error_time: ticker;
ValidateRefreshRepeat: boolean;
Enabled: boolean;
remote_block_start:int64;
remote_rev_cache: TDynInt64Array;
ids_reserved_to: int64;
next_id: int64;
bPosting: boolean;
bJammed: boolean;
pauseforfetch: boolean;
arcmap_validated: boolean;
FBackgroundCommands: TSimpleQueue;
tmLastDiskBusyTime: ticker;
ArcTargetSafePin: TDateTime;
csPrep, csClient, csQuickClient: TCLXCriticalSection;
{$IFNDEF DYNAMIC_VALBATCH}
valbatchcount: ni;
{$ENDIF}
valbatch: array[0..1023] of TArcTransactionalCommand;
newbatch: array[0..1023] of TTrans_LogThis;
{$IFDEF LAME_POOL}
unique: array[0..1023] of TRDTPArchiveClient;
{$ELSE}
unique: array[0..1023] of int64;
{$ENDIF}
// scratchpad: array [0.._BIG_BLOCK_SIZE_IN_BLOCKS div ARC_ZONE_SIZE_IN_BLOCKS] of int64;
function HasRemoveRevCached(idx: int64): boolean;
procedure Detach;override;
constructor Create;override;
destructor Destroy;override;
procedure StopThread;
procedure StartThread;
function NeedClient: TRDTPArchiveClient;
procedure NoNeedClient(cli: TRDTPArchiveClient);
function LogThis(const blockstart, blocklength: int64; const data: Pbyte): boolean;
function LogThis_OLD(const blockstart, blocklength: int64; const data: Pbyte): boolean;
function FetchLog(const blockstart, blocklength: int64; const outdata: PByte): boolean;
procedure RefreshRingStats;
// property TargetArchive: string read FTargetArchive write FTargetArchive;
// property SourceArchive: string read FSourceArchive write FSourceArchive;
property ReadyForRealTime: boolean read FReadyForRealtime;
property Client: TRDTPArchiveClient read FClient;
property QuickClient: TRDTPArchiveClient read GEtQuickClient;
property Host: string read GetHost write SetHost;
property Endpoint: string read GetEndpoint write SetEndPOint;
property Archive: string read FArchive write SetArchive;
property Active: boolean read FActive;
property RingfileName: string read GetRingFileName write SetRingFileName;
property Status: string read FStatus;
procedure Alert(s: string);
procedure DoThreadStuff;
procedure ShipLogsToServer;
procedure AddVAlBatch(c: TArcTransactionalCommand);
procedure ContinueLogValidateRefresh;
property Mode: TArcMode read FMode write SetMode;
procedure ModeChanged;
procedure Shovel;
procedure SetRemoteLogRevCache(idx: int64; logid: int64);
function GetREmoteLogRev(idx: int64): int64;
procedure PutRemoteLogRevInCache(idx: int64;logid: int64);
function GetNextLogId(zoneidx:int64): int64;
function ArcMapValid: boolean;
function ArcZoneValidateOrReset(zonebase: int64): boolean;
procedure SaveARcmapChecksum;
procedure LogSuccessX(const zoneidx, logid: int64);
procedure FInishBackGroundCommand;
function LogThis_Best(logid, startblock, blocklength: int64; data: TDynByteArray): boolean;
function CountUniqueClients(bOnlyValRefresh: boolean): ni;
property DiskSize: int64 read GetdiskSize;
property ArcZonesInDisk: int64 read GetArcZonesInDisk;
function Defined: boolean;
end;
TArcTransactionalCommand = class(TTransactionalCommand)
strict
private
function GetClient: TRDTPARchiveClient;
protected
procedure ExceptionRecover; override;
{$IFNDEF LAME_POOL}
Fclient: TRDTPArchiveClient;
property xclient: TRDTPARchiveClient read GetClient;
{$ENDIF}
public
{$IFDEF LAME_POOL}
xclient: TRDTPArchiveClient;
{$ENDIF}
zidx: int64;
shipper: TArcLogShipper;
procedure Detach; override;
procedure tallyerror;
end;
Ttrans_LogThis = class(TArcTransactionalCommand)
private
procedure SetStartBlock(const Value: int64);
protected
success: boolean;
Fstartblock: int64;
function LockState: Boolean; override;
procedure UnlockState; override;
procedure GatherStartSTate; override;
function ReEvalAgainstStartState: TTransactionResult; override;
procedure DoCommit; override;
function Perform: TTransactionResult; override;
procedure DoRollback; override;
public
known_fromid_at_creation: int64;
fromid: int64;
this_logid: int64;
archive: string;
data: TDynByteArray;
blocklength: int64;
procedure Init; override;
property StartBlock: int64 read FStartBlock write SetStartBlock;
procedure InitExpense; override;
function CanCombine(h: TLogShipHeader;indata:TDynByteArray): boolean;
function DoCombine(h: TLogShipHeader;indata: TDynByteArray): boolean;
function TryCombine(h: TLogShipHeader; indata: TDynByteArray): boolean;
end;
Tcmd_ContinueVAlidateLogRefresh = class(TArcTransactionalCommand)
protected
original_local, fromid, this_logid: int64;
entryvalidate: TArcMapEntry;
{$IFNDEF LOCAL_TEMP}
temp: TDynByteArray;
{$ENDIF}
pass: boolean;
checksumpass: boolean;
function LockState: Boolean; override;
procedure UnlockState; override;
procedure GatherStartSTate; override;
function ReEvalAgainstStartState: TTransactionResult; override;
procedure DoCommit; override;
procedure DoRollback;override;
function Perform: TTransactionResult; override;
public
disk: TObject;
success: boolean;
archive: string;
procedure InitExpense; override;
procedure Init; override;
end;
var
rebuildbootcount: int64 = 0;
implementation
{ TArcLogShipper }
uses
virtualdisk_advanced;
{ TArcLogShipper }
procedure TArcLogShipper.Activate;
begin
FACtive := true;
end;
procedure TArcLogShipper.AddVAlBatch(c: TArcTransactionalCommand);
var
t: ni;
begin
for t:= 0 to high(valbatch) do begin
if valbatch[t] = nil then begin
valbatch[t] := c;
inc(valbatchcount);
exit;
end;
end;
{$IFDEF DYNAMIC_VALBATCH}
setlength(valbatch, length(valbatch)+1);
valbatch[high(valbatch)] := c;
{$ELSE}
raise ECritical.create('no more slots');
{$ENDIF}
end;
procedure TArcLogShipper.Alert(s: string);
begin
FStatus := s;
Debug.Log(s);
end;
function TArcLogShipper.ArcMapValid: boolean;
var
cs: int64;
s: string;
z: int64;
csLocal: int64;
begin
result := false;
ECS(csClient);
try
try
result := arcmap_validated;
if not result then begin
if (GetTimeSince(FClient.LastConnectionAttempt) > 30000) or FClient.Connected then
begin
{$IFNDEF USE_NEW_ARC_ZONE_CHECKSUMS}
FClient.GetStoredParam_Async(self.Archive, 'arcmap_checksum', '0');
csLocal := TVirtualDisk_Advanced(self.disk).zonerevs.CalculateChecksum;
s := FClient.GetStoredParam_Response;
cs := 0;
if s <> '' then begin
cs := strtoint64(s);
end;
result := (s<>'') and (cs = csLocal);
{$ELSE}
result := false;
{$ENDIF}
if not result then begin
z := 0;
while z < self.ArcZonesInDisk do begin
if assigned(FThr) then begin
Fthr.Step := z;
FThr.stepcount := self.ArcZonesInDisk;
end;
ArcZonevalidateOrReset(z);
inc(z, ARC_ZONE_CHECKSUM_SIZE);
end;
result := true;
end;
arcmap_validated := true;
end;
end;
except
FClient.Disconnect;
end;
finally
LCS(csclient);
end;
end;
function TArcLogShipper.ArcZoneValidateOrReset(zonebase: int64): boolean;
var
cs: int64;
s: string;
csLocal: int64;
begin
ECS(csClient);
try
result := arcmap_validated;
if not result then begin
if (GetTimeSince(FClient.LastConnectionAttempt) > 30000) or FClient.Connected then
begin
{$IFNDEF USE_NEW_ARC_ZONE_CHECKSUMS}
FClient.GetStoredParam_ASYNC(self.Archive, 'arcmap_checksum_zone_'+inttohex(zonebase,8), '0');
csLocal := TVirtualDisk_Advanced(self.disk).zonerevs.CalculateZoneChecksum(zonebase, ARC_ZONE_CHECKSUM_SIZE);
s := FClient.GetStoredParam_Response;
cs := 0;
if s <> '' then begin
cs := strtoint64(s);
end;
{$ELSE}
FClient.GetArcVatCheckSum_Async(self.Archive, zonebase, ARC_ZONE_CHECKSUM_SIZE);
csLocal := arcmap.CalculateZoneChecksum2(zonebase, ARC_ZONE_CHECKSUM_SIZE);
cs := FClient.GetArcVatCheckSum_Response;
{$ENDIF}
result := (s<>'') and (cs = csLocal);
if not result then begin
TVirtualDisk_Advanced(self.disk).zonerevs.ResetZone(zonebase, ARC_ZONE_CHECKSUM_SIZE,true);
result := true;
end;
end;
end;
finally
LCS(csclient);
end;
end;
function TArcLogShipper.CanActivate: boolean;
begin
result := (Archive <> '') and (FCLient.Host <> '') and (FClient.Endpoint <> '');
// and (FQuickCLient.Host <> '') and (FQuickCLient.Endpoint <> '');
end;
procedure TArcLogShipper.CancelValBAtches;
var
t: ni;
cmd: TArcTransactionalCommand;
begin
exit;
for t := 0 to high(valbatch) do begin
cmd := valbatch[t];
if cmd <> nil then begin
try
cmd.Cancel;
except
end;
end;
end;
end;
function TArcLogShipper.CountUniqueClients(bOnlyValRefresh: boolean): ni;
{$IFDEF LAME_POOL}
var
t: ni;
uidx: ni;
function Has(c: TRDTPArchiveClient): boolean;
var
t: ni;
begin
for t:= 0 to uidx-1 do begin
if unique[t]=c then
exit(true);
end;
exit(false);
end;
begin
uidx := 0;
for t:= 0 to high(valbatch) do begin
if valbatch[t] <> nil then begin
if not has(valbatch[t].xclient) then begin
unique[uidx] := valbatch[t].xclient;
if (not bOnlyValRefresh) or (valbatch[t] is Tcmd_ContinueValidateLogRefresh) then
inc(uidx);
end;
end;
end;
exit(uidx);
end;
{$ELSE}
var
t: ni;
uidx: ni;
function Has(zidx: int64): boolean;
var
t: ni;
begin
for t:= 0 to uidx-1 do begin
if unique[t]=zidx then
exit(true);
end;
exit(false);
end;
begin
uidx := 0;
for t:= 0 to high(valbatch) do begin
if valbatch[t] <> nil then begin
if not has(valbatch[t].zidx) then begin
unique[uidx] := valbatch[t].zidx;
if (not bOnlyValRefresh) or (valbatch[t] is Tcmd_ContinueValidateLogRefresh) then
inc(uidx);
end;
end;
end;
exit(uidx);
end;
{$ENDIF}
procedure TArcLogShipper.FinishValBatches(bAll: boolean; bCancel: boolean = false);
var
t,u: ni;
cmd: TArcTransactionalCommand;
iFound: ni;
begin
if bAll then begin
for t := 0 to high(valbatch) do begin
cmd := valbatch[t];
if cmd <> nil then begin
try
cmd.Cancel;
cmd.WaitFor;
except
cmd.tallyerror;
end;
valbatch[t] := nil;
dec(valbatchcount);
{$IFDEF LAME_POOL}
if NoneUsingClient(cmd.xclient) then
NoNeedClient(cmd.xclient);
{$EndIF}
cmd.SelfDestruct(2000);
// cmd.free;
cmd := nil;
end;
end;
end else begin
iFound := 0;
for t := 0 to high(valbatch) do begin
cmd := valbatch[t];
if (cmd <> nil) and (cmd.IsComplete) then begin
try
cmd.WaitFor;
except
cmd.tallyerror;
end;
valbatch[t] := nil;
{$IFDEF LAME_POOL}
if NoneUsingClient(cmd.xclient) then
NoNeedClient(cmd.xclient);
{$ENDIF}
cmd.SelfDestruct(2000);
// cmd.free;
cmd := nil;
for u := t to high(valbatch)-1 do begin
valbatch[u] := valbatch[u+1];
end;
valbatch[high(valbatch)] := nil;
dec(valbatchcount);
inc(iFound);
end;
end;
if iFound = 0 then
sleep(10);
end;
end;
procedure TArcLogShipper.CollapseValbatches;
begin
raise ECritical.create('unimplemented');
//TODO -cunimplemented: unimplemented block
end;
procedure TArcLogShipper.ContinueLogValidateRefresh;
var
cmd: Tcmd_ContinueVAlidateLogRefresh;
t: ni;
vidx: ni;
lastc: TArcTransactionalCommand;
bFinish: boolean;
bRestart: boolean;
a,b: int64;
bfound: boolean;
tmStart: ticker;
begin
tmStart := getTicker;
FinishValBatches(false);
bFinish := false;
bRestart := false;
//exit if we have too many clients opened for validate/refresh
if CountUniqueClients(true) >= RESERVE_UNIQUE_CLIENTS then
exit;
//exit if we have too many clients opened for all purposes
if CountUniqueClients(false) >= MAX_UNIQUE_CLIENTS then
exit;
//check if any valbatches are done
// if valbatchcount > 0 then
repeat
bFound := false;
for t:= low(valbatch) to high(valbatch) do begin
if valbatch[t] = nil then begin
vidx := validateidx;//current index...
if vidx >= ZONES_To_BACKUP then
break;
ecs(csPrep);
try
//GET our local log rev
b := TVirtualDisk_Advanced(self.disk).zonerevs.GetEntry(validateidx).logid;
if b > 0 then begin
//get remote rev from cache (should be equal or less than our upgraded target regardless of race condition)
a := GetREmoteLogRev(validateidx);
if (a <> b) {or (b=0)} then begin
bFound := true;
cmd := Tcmd_ContinueVAlidateLogRefresh.create;
cmd.archive := self.Archive;
cmd.shipper := self;
cmd.zidx := vidx;
// FThr.Status := 'VR '+vidx.tohexstring;
cmd.disk := self.disk;
lastc := SearchValBatchForZone(vidx, false);
if lastc <> nil then begin
cmd.AddDependency(lastc);
{$IFDEF LAME_POOL}
cmd.xclient := lastc.xclient;
{$ENDIF}
end else begin
{$IFDEF LAME_POOL}
cmd.xclient := NeedClient;
{$ENDIF}
end;
cmd.Start;
valbatch[t] := cmd;
end;
end;
validateidx := validateidx + 1;
self.FThr.stepcount := ZONES_TO_BACKUP;
self.FThr.step := validateidx;
if validateidx = ZONES_To_BACKUP then
begin
validateidx := 0;
if not ValidateRefreshRepeat then
begin
bFinish := true;
ValidateRefreshRepeat := false;
mode := arcRealTime;
exit;
end else begin
ValidateRefreshRepeat := false;
end;
end;
finally
lcs(csPrep);
end;
if bFInish then
FinishValBatches(true);
if CountUniqueClients(false) >= MAX_UNIQUE_CLIENTS then
break;
end;
end;
until bFound or (GetTimeSince(tmStart) > 1000);
end;
constructor TArcLogShipper.Create;
var
t: ni;
begin
inherited;
phIO := PMC.GetPerfhandle;
phIO.desc.desc := 'ArcRing';
phRingW := PMC.GetPerfhandle;
phRingW.node.typ := NT_BUCKETFILL;
phRingW.desc.desc := 'WriteBuf';
phRingW.desc.above := phIO.id;
phRingR := PMC.GetPerfhandle;
phRingR.node.typ := NT_BUCKETFILL;
phRingR.desc.desc := 'ReadBuf';
phRingR.desc.above := phRingW.id;
phNet := PMC.GetPerfHandle;
phNet.desc.desc :='TargetArchive';
phNet.desc.left := phIO.id;
//create a command to handle the refresh
for t:= low(valbatch) to high(valbatch) do begin
valbatch[t] := nil;
end;
FClient := TRDTPARchiveClient.create('','');
FClients := TSharedList<TRDTPARchiveClient>.create;
{$IFDEF USE_TCP}
FClient.UseTCP := true;
{$ENDIF}
FQuickclient := nil;
ring := TLocalRing.create;
setlength(temp, ARC_ZONE_SIZE_IN_BYTES);
remote_block_start := -1;
ICS(csClient, 'shipperClient');
ICS(csPrep,'shipperPrep');
ICS(csQuickClient, 'shipperQuickClient');
FBackgroundCommands := tpm.Needthread<TSimpleQueue>(nil);
FBackGRoundCommands.start;
end;
function TArcLogShipper.Defined: boolean;
begin
result := ARchive <> '';
end;
destructor TArcLogShipper.Destroy;
begin
CAncelValBatches;
StopThread;
FBackGroundCOmmands.WAitForAll;
FBackGroundCommands.Stop;
FBackGroundCommands.SafeWaitFor;
TPM.NoNeedThread(FBackGroundCommands);
FClient.free;
FClient := nil;
FQuickClient.free;
FQuickClient := nil;
ring.free;
ring := nil;
DCS(csPrep);
DCS(csClient);
DCS(csQuickClient);
FClients.free;
PMC.ReleasePerfHandle(phRingR);
PMC.ReleasePerfHandle(phRingW);
PMC.ReleasePerfHandle(phNet);
PMC.ReleasePerfHandle(phIO);
inherited;
end;
procedure TArcLogShipper.DestroyClientPool;
var
cli: TRDTPArchiveClient;
begin
FClients.lock;
try
while FClients.count > 0 do begin