This repository has been archived by the owner on Aug 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathSyncContext.cs
1104 lines (1006 loc) · 54.9 KB
/
SyncContext.cs
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 (c) Microsoft Corporation. All Rights Reserved.
// Licensed under the MIT License.
using Microsoft.Datasync.Client.Extensions;
using Microsoft.Datasync.Client.Http;
using Microsoft.Datasync.Client.Offline.Queue;
using Microsoft.Datasync.Client.Query;
using Microsoft.Datasync.Client.Query.Linq.Nodes;
using Microsoft.Datasync.Client.Query.OData;
using Microsoft.Datasync.Client.Serialization;
using Microsoft.Datasync.Client.Table;
using Microsoft.Datasync.Client.Utils;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.Datasync.Client.Offline
{
/// <summary>
/// The push context - this is used during testing.
/// </summary>
internal interface IPushContext
{
/// <summary>
/// Pushes items in the operations queue for the list of tables to the remote service.
/// </summary>
/// <param name="tableNames">The list of table names to push.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param>
/// <returns>A task that completes when the push operation has finished.</returns>
/// <exception cref="PushFailedException">if the push operation failed.</exception>
Task PushItemsAsync(string[] tableNames, CancellationToken cancellationToken = default);
}
/// <summary>
/// The synchronization context, used for coordinating requests between the online and
/// offline stores.
/// </summary>
internal class SyncContext : IPushContext, IDisposable
{
private readonly AsyncLock initializationLock = new();
private readonly AsyncReaderWriterLock queueLock = new();
private readonly AsyncLockDictionary tableLock = new();
private readonly AsyncLockDictionary itemLock = new();
/// <summary>
/// The Id generator to use for item.
/// </summary>
private readonly Func<string, string> IdGenerator;
/// <summary>
/// Coordinates all the requests for offline operations.
/// </summary>
internal SyncContext(DatasyncClient client, IOfflineStore store)
{
Arguments.IsNotNull(client, nameof(client));
Arguments.IsNotNull(store, nameof(store));
ServiceClient = client;
OfflineStore = store;
PushContext = this;
IdGenerator = client.ClientOptions.IdGenerator;
}
/// <summary>
/// <c>true</c> when the <see cref="SyncContext"/> can be used.
/// </summary>
internal bool IsInitialized { get; private set; }
/// <summary>
/// The offline store to use for persistent storage.
/// </summary>
internal IOfflineStore OfflineStore { get; }
/// <summary>
/// The persistent operations queue.
/// </summary>
internal OperationsQueue OperationsQueue { get; private set; }
/// <summary>
/// Persistent storage for delta-tokens.
/// </summary>
internal IDeltaTokenStore DeltaTokenStore { get; private set; }
/// <summary>
/// The service client to use for communicating with the back end service.
/// </summary>
internal DatasyncClient ServiceClient { get; }
/// <summary>
/// The context to use when doing an automatic push.
/// </summary>
/// <remarks>
/// This is only really used during testing.
/// </remarks>
internal IPushContext PushContext { get; set; }
/// <summary>
/// The number of pending operations in the operations queue.
/// </summary>
internal long PendingOperations { get => OperationsQueue.PendingOperations; }
/// <summary>
/// Initialize the synchronization context for this offline store.
/// </summary>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param>
/// <returns>A task that completes when initialization is complete.</returns>
public async Task InitializeAsync(CancellationToken cancellationToken = default)
{
using (await initializationLock.AcquireAsync(cancellationToken).ConfigureAwait(false))
{
if (IsInitialized)
{
return;
}
// Initialize the store
await OfflineStore.InitializeAsync(cancellationToken).ConfigureAwait(false);
// Initialize the operations queue.
OperationsQueue = new OperationsQueue(OfflineStore);
await OperationsQueue.InitializeAsync(cancellationToken).ConfigureAwait(false);
// Initialize the delta token store.
if (OfflineStore is IDeltaTokenStoreProvider provider)
{
DeltaTokenStore = await provider.GetDeltaTokenStoreAsync(cancellationToken).ConfigureAwait(false);
}
else
{
DeltaTokenStore = new DeltaToken.DefaultDeltaTokenStore(OfflineStore);
}
// Clear the errors in the store.
var errorsEnumerable = new FuncAsyncPageable<TableOperationError>(nextLink => GetPageOfErrorsAsync("", nextLink, cancellationToken));
var errors = await errorsEnumerable.ToZumoListAsync(cancellationToken).ConfigureAwait(false);
if (errors.Any())
{
await RemoveErrorsAsync(errors, cancellationToken).ConfigureAwait(false);
}
// We are now initialized.
IsInitialized = true;
}
}
/// <summary>
/// Loads a single page from the SyncErrors table into memory.
/// </summary>
/// <param name="query">The query to execute.</param>
/// <param name="nextLink">The next link.</param>
/// <returns>A page of operation error objects.</returns>
private async Task<Page<TableOperationError>> GetPageOfErrorsAsync(string query, string nextLink, CancellationToken cancellationToken)
{
if (nextLink != null)
{
var requestUri = new Uri(nextLink);
query = requestUri.Query.TrimStart('?');
}
var queryDescription = QueryDescription.Parse(SystemTables.SyncErrors, query);
Page<JObject> sourcePage = await OfflineStore.GetPageAsync(queryDescription, cancellationToken).ConfigureAwait(false);
return new Page<TableOperationError>()
{
Count = sourcePage.Count,
NextLink = sourcePage.NextLink,
Items = sourcePage.Items?.Select(err => TableOperationError.Deserialize(err, ServiceClient.Serializer.SerializerSettings))
};
}
#region Store Operations initiated from an OfflineTable
/// <summary>
/// Count the number of items that would be returned by the provided query, without returning
/// all the values.
/// </summary>
/// <param name="tableName">The name of the offline table.</param>
/// <param name="query">The query to execute.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param>
/// <returns>A task that returns the number of items that will be in the result set when the query finishes.</returns>
public async Task<long> CountItemsAsync(string tableName, string query, CancellationToken cancellationToken = default)
{
await EnsureContextIsInitializedAsync(cancellationToken).ConfigureAwait(false);
var queryDescription = QueryDescription.Parse(tableName, query);
queryDescription.IncludeTotalCount = true;
queryDescription.Skip = null;
queryDescription.Top = 1;
var page = await OfflineStore.GetPageAsync(queryDescription, cancellationToken).ConfigureAwait(false);
return page.Count ?? -1;
}
/// <summary>
/// Deletes an item from the offline table.
/// </summary>
/// <param name="tableName">The name of the offline table.</param>
/// <param name="instance">The instance to delete from the table.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param>
/// <returns>A task that returns the response when complete.</returns>
public async Task DeleteItemAsync(string tableName, JObject instance, CancellationToken cancellationToken = default)
{
await EnsureContextIsInitializedAsync(cancellationToken).ConfigureAwait(false);
string itemId = ServiceSerializer.GetId(instance);
using IDisposable itemLock = await this.itemLock.AcquireAsync(itemId, cancellationToken).ConfigureAwait(false);
JObject originalInstance = await GetItemAsync(tableName, itemId, cancellationToken).ConfigureAwait(false)
?? throw new InvalidOperationException($"The item with ID '{itemId}' is not in the offline store.");
DeleteOperation operation = new(tableName, itemId) { Item = originalInstance };
await EnqueueOperationAsync(operation, originalInstance, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Retrieve an item from the offline table.
/// </summary>
/// <param name="tableName">The name of the offline table.</param>
/// <param name="id">The ID of the item to retrieve.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param>
/// <returns>A task that returns the item when complete.</returns>
public async Task<JObject> GetItemAsync(string tableName, string id, CancellationToken cancellationToken = default)
{
await EnsureContextIsInitializedAsync(cancellationToken).ConfigureAwait(false);
return await OfflineStore.GetItemAsync(tableName, id, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Gets the next page of items from a query.
/// </summary>
/// <param name="query">The query.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param>
/// <returns>A task that returns a page of items when complete.</returns>
public async Task<Page<JObject>> GetNextPageAsync(string tableName, string query, CancellationToken cancellationToken = default)
{
await EnsureContextIsInitializedAsync(cancellationToken).ConfigureAwait(false);
var queryDescription = QueryDescription.Parse(tableName, query);
return await OfflineStore.GetPageAsync(queryDescription, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Inserts an item into the offline table.
/// </summary>
/// <param name="tableName">The name of the offline table.</param>
/// <param name="instance">The instance to insert into the table.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param>
/// <returns>A task that returns the inserted data when complete.</returns>
public async Task InsertItemAsync(string tableName, JObject instance, CancellationToken cancellationToken = default)
{
await EnsureContextIsInitializedAsync(cancellationToken).ConfigureAwait(false);
string itemId = ServiceSerializer.GetId(instance, allowDefault: true);
if (itemId == null)
{
itemId = IdGenerator?.Invoke(tableName) ?? Guid.NewGuid().ToString("N");
instance = (JObject)instance.DeepClone();
instance[SystemProperties.JsonIdProperty] = itemId;
}
using IDisposable itemLock = await this.itemLock.AcquireAsync(itemId, cancellationToken).ConfigureAwait(false);
var operation = new InsertOperation(tableName, itemId);
await EnqueueOperationAsync(operation, instance, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Replaces an item into the remote table.
/// </summary>
/// <param name="tableName">The name of the offline table.</param>
/// <param name="instance">The instance to replace into the table.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param>
/// <returns>A task that returns the replaced data when complete.</returns>
public async Task ReplaceItemAsync(string tableName, JObject instance, CancellationToken cancellationToken = default)
{
await EnsureContextIsInitializedAsync(cancellationToken).ConfigureAwait(false);
string itemId = ServiceSerializer.GetId(instance);
instance = ServiceSerializer.RemoveSystemProperties(instance, out string version);
if (version != null)
{
instance[SystemProperties.JsonVersionProperty] = version;
}
using IDisposable itemLock = await this.itemLock.AcquireAsync(itemId, cancellationToken).ConfigureAwait(false);
var operation = new UpdateOperation(tableName, itemId);
await EnqueueOperationAsync(operation, instance, cancellationToken).ConfigureAwait(false);
}
#endregion
#region Data synchronization methods
/// <summary>
/// Pulls the items matching the provided query from the remote table.
/// </summary>
/// <param name="tableName">The name of the offline table.</param>
/// <param name="query">The OData query that determines which items to pull from the remote table.</param>
/// <param name="options">The options used to configure the pull operation.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param>
/// <returns>A task that completes when the pull operation has finished.</returns>
public async Task PullItemsAsync(string tableName, string query, PullOptions options, CancellationToken cancellationToken = default)
{
Arguments.IsValidTableName(tableName, nameof(tableName));
Arguments.IsNotNull(query, nameof(query));
Arguments.IsNotNull(options, nameof(options));
await EnsureContextIsInitializedAsync(cancellationToken).ConfigureAwait(false);
var table = new RemoteTable(tableName, ServiceClient); // We need the RemoteTable, not the IRemoteTable here.
var queryId = options.QueryId ?? GetQueryIdFromQuery(tableName, query);
var queryDescription = QueryDescription.Parse(tableName, query);
string[] relatedTables = options.PushOtherTables ? null : new string[] { tableName };
if (queryDescription.Selection.Count > 0 || queryDescription.Projections.Count > 0)
{
throw new ArgumentException("Pull query with select clause is not supported.", nameof(query));
}
queryDescription.Ordering.Clear();
queryDescription.Top = null;
queryDescription.Skip = null;
queryDescription.IncludeTotalCount = false;
if (await TableIsDirtyAsync(tableName, cancellationToken).ConfigureAwait(false))
{
await PushContext.PushItemsAsync(relatedTables, cancellationToken).ConfigureAwait(false);
// If the table is still dirty, then throw an error.
if (await TableIsDirtyAsync(tableName, cancellationToken).ConfigureAwait(false))
{
throw new DatasyncInvalidOperationException($"There are still pending operations for table '{tableName}' after a push");
}
}
var deltaToken = await DeltaTokenStore.GetDeltaTokenAsync(tableName, queryId, cancellationToken).ConfigureAwait(false);
// Issue 601 - if the delta-token is 0L (in Unix ms), then it's a "new pull", so we don't need the "updatedAt" filter nor
// do we need the include deleted flag.
Dictionary<string, string> parameters = new();
if (options.AlwaysPullWithDeltaToken || deltaToken.ToUnixTimeMilliseconds() > 0L)
{
var deltaTokenFilter = new BinaryOperatorNode(BinaryOperatorKind.GreaterThan, new MemberAccessNode(null, SystemProperties.JsonUpdatedAtProperty), new ConstantNode(deltaToken));
queryDescription.Filter = queryDescription.Filter == null ? deltaTokenFilter : new BinaryOperatorNode(BinaryOperatorKind.And, queryDescription.Filter, deltaTokenFilter);
parameters.Add(ODataOptions.IncludeDeleted, "true");
}
queryDescription.IncludeTotalCount = true;
queryDescription.Ordering.Add(new OrderByNode(new MemberAccessNode(null, SystemProperties.JsonUpdatedAtProperty), true));
var odataString = queryDescription.ToODataString(parameters);
SendPullStartedEvent(tableName);
long itemCount = 0;
long expectedItems = -1;
DateTimeOffset? updatedAt = null;
var enumerable = table.GetAsyncItems(odataString);
var deletedIds = new List<string>();
var upsertList = new Dictionary<string, JObject>();
try
{
await foreach (var instance in enumerable)
{
cancellationToken.ThrowIfCancellationRequested();
// If we have not read the expectedItem count yet, then read it from the pageable.
if (expectedItems == -1 && enumerable is FuncAsyncPageable<JToken> pageable)
{
expectedItems = pageable.Count ?? -1;
}
if (instance is not JObject item)
{
SendPullFinishedEvent(tableName, itemCount, false);
throw new DatasyncInvalidOperationException("Received item is not an object");
}
string itemId = ServiceSerializer.GetId(item);
if (itemId == null)
{
SendPullFinishedEvent(tableName, itemCount, false);
throw new DatasyncInvalidOperationException("Received an item without an ID");
}
var pendingOperation = await OperationsQueue.GetOperationByItemIdAsync(tableName, itemId, cancellationToken).ConfigureAwait(false);
if (pendingOperation != null)
{
SendPullFinishedEvent(tableName, itemCount, false);
throw new InvalidOperationException("Received an item for which there is a pending operation.");
}
SendItemWillBeStoredEvent(tableName, itemId, itemCount, expectedItems);
if (ServiceSerializer.IsDeleted(item))
{
deletedIds.Add(itemId);
}
else
{
upsertList.Add(itemId, item);
}
itemCount++;
updatedAt = ServiceSerializer.GetUpdatedAt(item)?.ToUniversalTime();
if (itemCount % options.WriteDeltaTokenInterval == 0)
{
await OfflineStore.DeleteAsync(tableName, deletedIds, cancellationToken).ConfigureAwait(false);
deletedIds.ForEach(x => SendItemWasStoredEvent(tableName, x, itemCount, expectedItems));
deletedIds.Clear();
await OfflineStore.UpsertAsync(tableName, upsertList.Values, ignoreMissingColumns: true, cancellationToken).ConfigureAwait(false);
upsertList.Keys.ToList().ForEach(x => SendItemWasStoredEvent(tableName, x, itemCount, expectedItems));
upsertList.Clear();
if (updatedAt.HasValue)
{
await DeltaTokenStore.SetDeltaTokenAsync(tableName, queryId, updatedAt.Value, cancellationToken).ConfigureAwait(false);
updatedAt = null;
}
}
}
}
finally
{
if (deletedIds.Count > 0)
{
await OfflineStore.DeleteAsync(tableName, deletedIds, cancellationToken).ConfigureAwait(false);
deletedIds.ForEach(x => SendItemWasStoredEvent(tableName, x, itemCount, expectedItems));
}
if (upsertList.Count > 0)
{
await OfflineStore.UpsertAsync(tableName, upsertList.Values, ignoreMissingColumns: true, cancellationToken).ConfigureAwait(false);
upsertList.Keys.ToList().ForEach(x => SendItemWasStoredEvent(tableName, x, itemCount, expectedItems));
}
if (updatedAt.HasValue)
{
await DeltaTokenStore.SetDeltaTokenAsync(tableName, queryId, updatedAt.Value, cancellationToken).ConfigureAwait(false);
}
SendPullFinishedEvent(tableName, itemCount, true);
}
}
/// <summary>
/// Deletes all the items in the offline table that match the query.
/// </summary>
/// <param name="tableName">The name of the offline table.</param>
/// <param name="query">An OData query that determines which items to delete.</param>
/// <param name="options">The options used to configure the purge operation.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param>
/// <returns>A task that completes when the purge operation has finished.</returns>
public async Task PurgeItemsAsync(string tableName, string query, PurgeOptions options, CancellationToken cancellationToken = default)
{
Arguments.IsValidTableName(tableName, nameof(tableName));
Arguments.IsNotNull(query, nameof(query));
Arguments.IsNotNull(options, nameof(options));
await EnsureContextIsInitializedAsync(cancellationToken).ConfigureAwait(false);
var queryId = options.QueryId ?? GetQueryIdFromQuery(tableName, query);
var queryDescription = QueryDescription.Parse(tableName, query);
using (await tableLock.AcquireAsync(tableName, cancellationToken).ConfigureAwait(false))
{
if (await TableIsDirtyAsync(tableName, cancellationToken).ConfigureAwait(false))
{
if (options.DiscardPendingOperations)
{
await DiscardTableOperationsAsync(tableName, cancellationToken).ConfigureAwait(false);
}
else
{
throw new InvalidOperationException("The table cannot be purged because it has pending operations.");
}
}
// Execute the purge
using (await queueLock.WriterLockAsync(cancellationToken).ConfigureAwait(false))
{
await OfflineStore.DeleteAsync(queryDescription, cancellationToken).ConfigureAwait(false);
await SetDeltaTokenAsync(tableName, queryId, options, cancellationToken).ConfigureAwait(false);
}
}
}
/// <summary>
/// Pushes items in the operations queue for the named table to the remote service.
/// </summary>
/// <param name="tableName">The name of the offline table.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/></param>
/// <returns>A task that completes when the push operation has finished.</returns>
public Task PushItemsAsync(string tableName, CancellationToken cancellationToken = default)
=> PushItemsAsync(tableName, null, cancellationToken);
/// <summary>
/// Pushes items in the operations queue for the named table to the remote service.
/// </summary>
/// <param name="tableName">The name of the offline table.</param>
/// <param name="options">The push options to use for this operation.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/></param>
/// <returns>A task that completes when the push operation has finished.</returns>
public Task PushItemsAsync(string tableName, PushOptions options, CancellationToken cancellationToken = default)
=> PushItemsAsync(new string[] { tableName }, options, cancellationToken);
/// <summary>
/// Pushes items in the operations queue for the list of tables to the remote service.
/// </summary>
/// <param name="tableNames">The list of table names to push.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param>
/// <returns>A task that completes when the push operation has finished.</returns>
/// <exception cref="PushFailedException">if the push operation failed.</exception>
public Task PushItemsAsync(string[] tableNames, CancellationToken cancellationToken = default)
=> PushItemsAsync(tableNames, null, cancellationToken);
/// <summary>
/// Pushes items in the operations queue for the list of tables to the remote service.
/// </summary>
/// <param name="tableNames">The list of table names to push.</param>
/// <param name="options">The push options to use for this operation.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param>
/// <returns>A task that completes when the push operation has finished.</returns>
/// <exception cref="PushFailedException">if the push operation failed.</exception>
public async Task PushItemsAsync(string[] tableNames, PushOptions options, CancellationToken cancellationToken = default)
{
await EnsureContextIsInitializedAsync(cancellationToken).ConfigureAwait(false);
var batch = new OperationBatch(this);
cancellationToken.Register(() => batch.Abort(PushStatus.CancelledByToken));
// Process the queue - only use the QueueHandler (new logic) if maxThreads > 1
SendPushStartedEvent();
int maxThreads = GetMaximumParallelOperations(options);
long itemCount = 0;
if (maxThreads == 1)
{
try
{
TableOperation operation = await OperationsQueue.PeekAsync(0, tableNames, cancellationToken).ConfigureAwait(false);
while (operation != null)
{
using (IDisposable itemLock = await this.itemLock.AcquireAsync(operation.ItemId, cancellationToken).ConfigureAwait(false))
{
// Get the operation again in case it changed while waiting for the lock.
operation = await OperationsQueue.GetOperationByItemIdAsync(operation.TableName, operation.ItemId, cancellationToken).ConfigureAwait(false);
SendItemWillBePushedEvent(operation.TableName, operation.ItemId, itemCount);
bool isSuccessful = await ExecutePushOperationAsync(operation, batch, true, cancellationToken).ConfigureAwait(false);
Interlocked.Increment(ref itemCount);
SendItemWasPushedEvent(operation.TableName, operation.ItemId, itemCount, isSuccessful);
}
if (batch.AbortReason.HasValue)
{
break;
}
operation = await OperationsQueue.PeekAsync(operation.Sequence, tableNames, cancellationToken).ConfigureAwait(false);
}
}
catch (Exception ex)
{
batch.OtherErrors.Add(ex);
}
}
else
{
QueueHandler queueHandler = new(maxThreads, async (operation) =>
{
using IDisposable itemLock = await this.itemLock.AcquireAsync(operation.ItemId, cancellationToken).ConfigureAwait(false);
// Get the operation again in case it changed while waiting for the lock.
operation = await OperationsQueue.GetOperationByItemIdAsync(operation.TableName, operation.ItemId, cancellationToken).ConfigureAwait(false);
SendItemWillBePushedEvent(operation.TableName, operation.ItemId, itemCount);
bool isSuccessful = await ExecutePushOperationAsync(operation, batch, true, cancellationToken).ConfigureAwait(false);
Interlocked.Increment(ref itemCount);
SendItemWasPushedEvent(operation.TableName, operation.ItemId, itemCount, isSuccessful);
});
try
{
TableOperation operation = await OperationsQueue.PeekAsync(0, tableNames, cancellationToken).ConfigureAwait(false);
while (operation != null)
{
queueHandler.Enqueue(operation);
operation = await OperationsQueue.PeekAsync(operation.Sequence, tableNames, cancellationToken).ConfigureAwait(false);
}
await queueHandler.WhenComplete();
}
catch (Exception ex)
{
batch.OtherErrors.Add(ex);
}
}
// If there were errors, then create a push failed exception.
List<TableOperationError> errors = new();
PushStatus batchStatus = batch.AbortReason ?? PushStatus.Complete;
try
{
errors.AddRange(await batch.LoadErrorsAsync(tableNames, cancellationToken).ConfigureAwait(false));
}
catch (Exception ex)
{
batch.OtherErrors.Add(new OfflineStoreException("Failed to read errors from the local store.", ex));
}
SendPushFinishedEvent(batchStatus != PushStatus.Complete);
// If the push did not complete successfully, then throw a PushFailedException.
if (batchStatus != PushStatus.Complete || batch.HasErrors(errors))
{
List<TableOperationError> unhandledErrors = errors.Where(error => !error.Handled).ToList();
Exception innerException = batch.OtherErrors.Count > 0 ? new AggregateException(batch.OtherErrors) : null;
await RemoveErrorsAsync(errors.Where(e => e.Status != HttpStatusCode.Conflict && e.Status != HttpStatusCode.PreconditionFailed), cancellationToken).ConfigureAwait(false);
throw new PushFailedException(new PushCompletionResult(unhandledErrors, batchStatus), innerException);
}
}
/// <summary>
/// Removes a list of errors from the errors table.
/// </summary>
/// <param name="errors">The list of errors to remove.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param>
/// <returns>A task that completes when the errors are removed.</returns>
internal async Task RemoveErrorsAsync(IEnumerable<TableOperationError> errors, CancellationToken cancellationToken = default)
{
List<string> ids = errors.Select(e => e.Id).Where(e => !string.IsNullOrWhiteSpace(e)).ToList();
if (ids.Any())
{
await OfflineStore.DeleteAsync(SystemTables.SyncErrors, ids, cancellationToken).ConfigureAwait(false);
}
}
#endregion
#region Callbacks for handling conflicts
/// <summary>
/// Cancels the table operation, discarding the item.
/// </summary>
/// <param name="error">The <see cref="TableOperationError"/> being processed.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param>
/// <returns>A task that completes when the operation is finished.</returns>
public async Task CancelAndDiscardItemAsync(TableOperationError error, CancellationToken cancellationToken = default)
{
Arguments.IsNotNull(error, nameof(error));
if (error.Item == null)
{
throw new ArgumentException("Operation error must contain an item", nameof(error));
}
string itemId = error.Item.Value<string>(SystemProperties.JsonIdProperty);
await TryCancelOperationAsync(error, cancellationToken).ConfigureAwait(false);
await OfflineStore.DeleteAsync(error.TableName, new[] { itemId }, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Cancels an operation and updated the item with the version from the service.
/// </summary>
/// <param name="error">The <see cref="TableOperationError"/> that is being processed.</param>
/// <param name="item">The item to use for updating.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param>
/// <returns>A task that completes when the update is finished.</returns>
public async Task CancelAndUpdateItemAsync(TableOperationError error, JObject item, CancellationToken cancellationToken = default)
{
Arguments.IsNotNull(error, nameof(error));
Arguments.IsNotNull(item, nameof(item));
if (error.Item == null)
{
throw new ArgumentException("Operation error must contain an item", nameof(error));
}
await TryCancelOperationAsync(error, cancellationToken).ConfigureAwait(false);
await OfflineStore.UpsertAsync(error.TableName, new[] { item }, true, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Updates the item within an operation for retry.
/// </summary>
/// <param name="error">The <see cref="TableOperationError"/> that is being processed.</param>
/// <param name="item">The item to use for updating.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param>
/// <returns>A task that completes when the update is finished.</returns>
public async Task UpdateOperationAsync(TableOperationError error, JObject item, CancellationToken cancellationToken = default)
{
Arguments.IsNotNull(error, nameof(error));
Arguments.IsNotNull(item, nameof(item));
if (!await OperationsQueue.UpdateOperationAsync(error.Id, error.OperationVersion, item, cancellationToken).ConfigureAwait(false))
{
throw new InvalidOperationException("The operation has been updated and cannot be updated again.");
}
await OfflineStore.DeleteAsync(SystemTables.SyncErrors, new[] { error.Id }, cancellationToken).ConfigureAwait(false);
if (error.OperationKind != TableOperationKind.Delete)
{
await OfflineStore.UpsertAsync(error.TableName, new[] { item }, true, cancellationToken).ConfigureAwait(false);
}
}
/// <summary>
/// Tries to cancel an operation in the operations queue.
/// </summary>
/// <param name="error">The table operation to cancel.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param>
/// <returns></returns>
private async Task TryCancelOperationAsync(TableOperationError error, CancellationToken cancellationToken = default)
{
if (!await OperationsQueue.DeleteOperationByIdAsync(error.Id, error.OperationVersion, cancellationToken).ConfigureAwait(false))
{
throw new InvalidOperationException("The operation has been updated and cannot be cancelled.");
}
await OfflineStore.DeleteAsync(SystemTables.SyncErrors, new[] { error.Id }, cancellationToken).ConfigureAwait(false);
}
#endregion
/// <summary>
/// Discards operations within the operations queue for a specific table.
/// </summary>
/// <param name="tableName">The name of the table.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param>
internal async Task DiscardTableOperationsAsync(string tableName, CancellationToken cancellationToken)
{
Arguments.IsValidTableName(tableName, nameof(tableName));
await EnsureContextIsInitializedAsync(cancellationToken).ConfigureAwait(false);
var query = QueryDescription.Parse(SystemTables.OperationsQueue, $"$filter=(tableName eq '{tableName}')");
await OperationsQueue.DeleteOperationsAsync(query, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Enqueues an operation and updates the offline store.
/// </summary>
/// <param name="operation">The table operation to enqueue.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param>
/// <returns>A task that completes when the operation is enqueued on the operations queue.</returns>
private async Task EnqueueOperationAsync(TableOperation operation, JObject instance, CancellationToken cancellationToken)
{
using (await queueLock.WriterLockAsync(cancellationToken).ConfigureAwait(false))
{
// See if there is an existing operation. If there is, then validate that it can be collapsed.
TableOperation existingOperation = await OperationsQueue.GetOperationByItemIdAsync(operation.TableName, operation.ItemId, cancellationToken).ConfigureAwait(false);
existingOperation?.ValidateOperationCanCollapse(operation);
// Execute the operation on the local store.
try
{
await operation.ExecuteOperationOnOfflineStoreAsync(OfflineStore, instance, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex) when (ex is not OfflineStoreException)
{
throw new OfflineStoreException("Failed to perform operation on local store.", ex);
}
// If there is an existing operation, then collapse the operation with the previous one.
if (existingOperation != null)
{
existingOperation.CollapseOperation(operation);
if (existingOperation.IsCancelled)
{
await OperationsQueue.DeleteOperationByIdAsync(existingOperation.Id, existingOperation.Version, cancellationToken).ConfigureAwait(false);
}
else if (existingOperation.IsUpdated)
{
await OperationsQueue.UpdateOperationAsync(existingOperation, cancellationToken).ConfigureAwait(false);
}
}
if (!operation.IsCancelled)
{
await OperationsQueue.EnqueueOperationAsync(operation, cancellationToken).ConfigureAwait(false);
}
}
}
/// <summary>
/// Ensures that the <see cref="SyncContext"/> has been initialized before use.
/// </summary>
/// <param name="cancellationToken">A cancellation token to use.</param>
/// <returns>A task that completes when the context is initialized</returns>
private async Task EnsureContextIsInitializedAsync(CancellationToken cancellationToken = default)
{
await InitializeAsync(cancellationToken).ConfigureAwait(false);
if (!IsInitialized)
{
throw new InvalidOperationException("Store is not initialized");
}
}
/// <summary>
/// Executes a single operation within the queue.
/// </summary>
/// <param name="operation">The operation to execute.</param>
/// <param name="batch">The operation batch being executed.</param>
/// <param name="removeFromQueueOnSuccess">If <c>true</c>, remove the operation from the queue when successful.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param>
/// <returns>A task that returns <c>true</c> if the execution was successful when complete.</returns>
protected virtual async Task<bool> ExecutePushOperationAsync(TableOperation operation, OperationBatch batch, bool removeFromQueueOnSuccess, CancellationToken cancellationToken)
{
if (operation.IsCancelled || cancellationToken.IsCancellationRequested)
{
return false;
}
if (operation.Item == null)
{
try
{
operation.Item = await OfflineStore.GetItemAsync(operation.TableName, operation.ItemId, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
batch.Abort(PushStatus.CancelledByOfflineStoreError);
throw new OfflineStoreException($"Failed to read Item '{operation.ItemId}' from table '{operation.TableName}' in the offline store.", ex);
}
if (operation.Item == null)
{
var item = new JObject(new JProperty(SystemProperties.JsonIdProperty, operation.ItemId));
await batch.AddErrorAsync(operation, null, null, item, cancellationToken).ConfigureAwait(false);
return false;
}
}
await OperationsQueue.TryUpdateOperationStateAsync(operation, TableOperationState.Attempted, batch, cancellationToken).ConfigureAwait(false);
operation.Item = ServiceSerializer.RemoveSystemProperties(operation.Item, out string version);
if (version != null)
{
operation.Item[SystemProperties.JsonVersionProperty] = version;
}
JObject result = null;
Exception error = null;
try
{
result = await operation.ExecuteOperationOnRemoteServiceAsync(ServiceClient, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
await OperationsQueue.TryUpdateOperationStateAsync(operation, TableOperationState.Failed, batch, cancellationToken).ConfigureAwait(false);
if (ex is HttpRequestException || ex is TimeoutException)
{
batch.Abort(PushStatus.CancelledByNetworkError);
return false;
}
else if (ex is DatasyncInvalidOperationException ios && ios.Response?.StatusCode == HttpStatusCode.Unauthorized)
{
batch.Abort(PushStatus.CancelledByAuthenticationError);
return false;
}
else if (ex is PushAbortedException)
{
batch.Abort(PushStatus.CancelledByOperation);
return false;
}
error = ex;
}
if (error == null && operation.CanWriteResultToStore && result?.Value<string>(SystemProperties.JsonIdProperty) != null)
{
try
{
await OfflineStore.UpsertAsync(operation.TableName, new[] { result }, true, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
batch.Abort(PushStatus.CancelledByOfflineStoreError);
throw new OfflineStoreException($"Failed to update item '{operation.ItemId}' in table '{operation.TableName}' of the offline store.", ex);
}
}
else if (error != null)
{
string rawResult = null;
if (error is DatasyncInvalidOperationException iox && iox.Response != null)
{
HttpStatusCode? statusCode = iox.Response.StatusCode;
try
{
rawResult = await iox.Response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
result = ServiceClient.Serializer.DeserializeObjectOrDefault(rawResult);
}
catch { /* Deliberately ignore JSON parsing errors */ }
await batch.AddErrorAsync(operation, statusCode, rawResult, result, cancellationToken).ConfigureAwait(false);
}
}
if (error == null)
{
if (removeFromQueueOnSuccess)
{
await OperationsQueue.DeleteOperationByIdAsync(operation.Id, operation.Version, cancellationToken).ConfigureAwait(false);
}
IList<TableOperationError> errors = (await batch.LoadErrorsAsync(new string[] { operation.TableName }, cancellationToken).ConfigureAwait(false))
.Where(e => (string)e.Item["id"] == operation.Id).ToList();
if (errors.Count > 0)
{
await RemoveErrorsAsync(errors, cancellationToken).ConfigureAwait(false);
}
}
return error == null;
}
/// <summary>
/// Obtains a query ID from a query and table name. This is used
/// when the dev doesn't specify a query ID.
/// </summary>
/// <param name="tableName">The name of the table.</param>
/// <param name="query">The query string.</param>
/// <returns>A query ID.</returns>
internal static string GetQueryIdFromQuery(string tableName, string query)
{
string hashKey = $"q|{tableName}|{query}";
byte[] bytes = Encoding.UTF8.GetBytes(hashKey);
using MD5 md5 = MD5.Create();
byte[] hashBytes = md5.ComputeHash(bytes);
return BitConverter.ToString(hashBytes).Replace("-", string.Empty).ToLower();
}
/// <summary>
/// Obtains the maximum number of parallel operations allowed, based on the passed in
/// <see cref="PushOptions"/> and/or the global options.
/// </summary>
/// <param name="options">The <see cref="PushOptions"/> for this operation, or null.</param>
/// <returns>The maximum number of parallel operations allowed.</returns>
internal int GetMaximumParallelOperations(PushOptions options)
=> (options == null || options.ParallelOperations == 0)
? ServiceClient.ClientOptions.ParallelOperations
: options.ParallelOperations;
/// <summary>
/// Sets the delta token for a query. If the resetAssociatedQueries flag is set, then all the
/// queries for a specific table are reset in the same way.
/// </summary>
/// <param name="tableName">The name of the table</param>
/// <param name="queryId">The ID of the query</param>
/// <param name="options">A options for the purge operation.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param>
/// <returns>A task that completes when the operation is finished.</returns>
internal async Task SetDeltaTokenAsync(string tableName, string queryId, PurgeOptions options, CancellationToken cancellationToken = default)
{
DateTimeOffset? timestamp = null;
switch (options.TimestampUpdatePolicy)
{
case TimestampUpdatePolicy.NoUpdate:
// Do not update the timestamp - this whole method becomes a no-op
return;
case TimestampUpdatePolicy.UpdateToLastEntity:
// Get the last entity (max UpdatedAt) in the table and update the timestamp to that value.
var tsq = new QueryDescription(tableName) { IncludeTotalCount = false, Top = 1 };
tsq.Ordering.Add(new OrderByNode(new MemberAccessNode(null, "updatedAt"), OrderByDirection.Descending));
Page<JObject> page = await OfflineStore.GetPageAsync(tsq, cancellationToken).ConfigureAwait(false);
if (page?.Items?.LastOrDefault() != null)
{
timestamp = DateTimeOffset.Parse(page?.Items?.LastOrDefault()?.Value<string>("updatedAt"));
}
break;
case TimestampUpdatePolicy.UpdateToNow:
timestamp = DateTimeOffset.UtcNow;
break;
// Anything else resets the timestamp to the epoch.
}
if (timestamp.HasValue)
{
await DeltaTokenStore.SetDeltaTokenAsync(tableName, queryId, timestamp.Value, cancellationToken).ConfigureAwait(false);
}
else
{
await DeltaTokenStore.ResetDeltaTokenAsync(tableName, queryId, cancellationToken).ConfigureAwait(false);
}
if (options.ResetAssociatedQueries)
{
var queryIds = (await DeltaTokenStore.GetDeltaTokenQueryIdsForTableAsync(tableName, cancellationToken).ConfigureAwait(false)).ToList();
foreach (string qid in queryIds)
{
if (qid != queryId)
{
if (timestamp.HasValue)
{
await DeltaTokenStore.SetDeltaTokenAsync(tableName, qid, timestamp.Value, cancellationToken).ConfigureAwait(false);
}
else
{
await DeltaTokenStore.ResetDeltaTokenAsync(tableName, qid, cancellationToken).ConfigureAwait(false);
}
}
}
}
}
/// <summary>
/// Determines if the table is dirty (i.e. has pending operations)
/// </summary>
/// <param name="tableName">Name of the table.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param>
/// <returns>A task that returns <c>true</c> if the table is dirty, and <c>false</c> otherwise when complete.</returns>
internal async Task<bool> TableIsDirtyAsync(string tableName, CancellationToken cancellationToken)
{
await EnsureContextIsInitializedAsync(cancellationToken).ConfigureAwait(false);
long pendingOperations = await OperationsQueue.CountPendingOperationsAsync(tableName, cancellationToken).ConfigureAwait(false);
return pendingOperations > 0;
}
#region Eventing Support
private void SendPullStartedEvent(string tableName)
{
ServiceClient.SendSynchronizationEvent(new SynchronizationEventArgs
{
EventType = SynchronizationEventType.PullStarted,
TableName = tableName,
IsSuccessful = null
});
}
private void SendPullFinishedEvent(string tableName, long itemsReceived, bool isSuccessful)
{
ServiceClient.SendSynchronizationEvent(new SynchronizationEventArgs
{