| | 1 | | // Copyright (c) Microsoft. All rights reserved. |
| | 2 | | // Licensed under the MIT license. See LICENSE file in the project root for full license information.using System; |
| | 3 | |
|
| | 4 | | namespace Microsoft.Azure.EventHubs.ServiceFabricProcessor |
| | 5 | | { |
| | 6 | | using Microsoft.ServiceFabric.Data; |
| | 7 | | using Microsoft.ServiceFabric.Data.Collections; |
| | 8 | | using System; |
| | 9 | | using System.Collections.Generic; |
| | 10 | | using System.Threading; |
| | 11 | | using System.Threading.Tasks; |
| | 12 | |
|
| | 13 | | class ReliableDictionaryCheckpointMananger : ICheckpointMananger |
| | 14 | | { |
| | 15 | | private IReliableStateManager reliableStateManager = null; |
| | 16 | | private IReliableDictionary<string, Dictionary<string, object>> store = null; |
| | 17 | |
|
| 12 | 18 | | internal ReliableDictionaryCheckpointMananger(IReliableStateManager rsm) |
| | 19 | | { |
| 12 | 20 | | this.reliableStateManager = rsm; |
| 12 | 21 | | } |
| | 22 | |
|
| | 23 | | public async Task<bool> CheckpointStoreExistsAsync(CancellationToken cancellationToken) |
| | 24 | | { |
| 0 | 25 | | ConditionalValue<IReliableDictionary<string, Dictionary<string, object>>> tryStore = await |
| 0 | 26 | | this.reliableStateManager.TryGetAsync<IReliableDictionary<string, Dictionary<string, object>>>(Constants |
| 0 | 27 | | EventProcessorEventSource.Current.Message($"CheckpointStoreExistsAsync = {tryStore.HasValue}"); |
| 0 | 28 | | return tryStore.HasValue; |
| 0 | 29 | | } |
| | 30 | |
|
| | 31 | | public async Task<bool> CreateCheckpointStoreIfNotExistsAsync(CancellationToken cancellationToken) |
| | 32 | | { |
| | 33 | | // Create or get access to the dictionary. |
| 8 | 34 | | this.store = await reliableStateManager.GetOrAddAsync<IReliableDictionary<string, Dictionary<string, object> |
| 8 | 35 | | EventProcessorEventSource.Current.Message("CreateCheckpointStoreIfNotExistsAsync OK"); |
| 8 | 36 | | return true; |
| 8 | 37 | | } |
| | 38 | |
|
| | 39 | | public async Task<Checkpoint> CreateCheckpointIfNotExistsAsync(string partitionId, CancellationToken cancellatio |
| | 40 | | { |
| 8 | 41 | | Checkpoint existingCheckpoint = await GetWithRetry(partitionId, cancellationToken).ConfigureAwait(false); |
| | 42 | |
|
| 8 | 43 | | if (existingCheckpoint == null) |
| | 44 | | { |
| 6 | 45 | | existingCheckpoint = new Checkpoint(1); |
| 6 | 46 | | await PutWithRetry(partitionId, existingCheckpoint, cancellationToken).ConfigureAwait(false); |
| | 47 | | } |
| 8 | 48 | | EventProcessorEventSource.Current.Message("CreateCheckpointIfNotExists OK"); |
| | 49 | |
|
| 8 | 50 | | return existingCheckpoint; |
| 8 | 51 | | } |
| | 52 | |
|
| | 53 | | public async Task<Checkpoint> GetCheckpointAsync(string partitionId, CancellationToken cancellationToken) |
| | 54 | | { |
| 0 | 55 | | return await GetWithRetry(partitionId, cancellationToken).ConfigureAwait(false); |
| 0 | 56 | | } |
| | 57 | |
|
| | 58 | | public async Task UpdateCheckpointAsync(string partitionId, Checkpoint checkpoint, CancellationToken cancellatio |
| | 59 | | { |
| 0 | 60 | | await PutWithRetry(partitionId, checkpoint, cancellationToken).ConfigureAwait(false); |
| 0 | 61 | | } |
| | 62 | |
|
| | 63 | | // Throws on error or if cancelled. |
| | 64 | | // Returns null if there is no entry for the given partition. |
| | 65 | | private async Task<Checkpoint> GetWithRetry(string partitionId, CancellationToken cancellationToken) |
| | 66 | | { |
| 8 | 67 | | EventProcessorEventSource.Current.Message($"Getting checkpoint for {partitionId}"); |
| | 68 | |
|
| 8 | 69 | | Checkpoint result = null; |
| 8 | 70 | | Exception lastException = null; |
| 0 | 71 | | for (int i = 0; i < Constants.RetryCount; i++) |
| | 72 | | { |
| 8 | 73 | | cancellationToken.ThrowIfCancellationRequested(); |
| 8 | 74 | | lastException = null; |
| | 75 | |
|
| | 76 | | try |
| | 77 | | { |
| 8 | 78 | | using (ITransaction tx = this.reliableStateManager.CreateTransaction()) |
| | 79 | | { |
| 8 | 80 | | ConditionalValue<Dictionary<string, object>> rawCheckpoint = await |
| 8 | 81 | | this.store.TryGetValueAsync(tx, partitionId, Constants.ReliableDictionaryTimeout, cancellati |
| | 82 | |
|
| 8 | 83 | | await tx.CommitAsync().ConfigureAwait(false); |
| | 84 | |
|
| | 85 | | // Success! Save the result, if any, and break out of the retry loop. |
| 8 | 86 | | if (rawCheckpoint.HasValue) |
| | 87 | | { |
| 2 | 88 | | result = Checkpoint.CreateFromDictionary(rawCheckpoint.Value); |
| | 89 | | } |
| | 90 | | else |
| | 91 | | { |
| 6 | 92 | | result = null; |
| | 93 | | } |
| 8 | 94 | | break; |
| | 95 | | } |
| | 96 | | } |
| 0 | 97 | | catch (TimeoutException e) |
| | 98 | | { |
| 0 | 99 | | lastException = e; |
| 0 | 100 | | } |
| | 101 | | } |
| | 102 | |
|
| 8 | 103 | | if (lastException != null) |
| | 104 | | { |
| | 105 | | // Ran out of retries, throw. |
| 0 | 106 | | throw new Exception("Ran out of retries creating checkpoint", lastException); |
| | 107 | | } |
| | 108 | |
|
| 8 | 109 | | if (result != null) |
| | 110 | | { |
| 2 | 111 | | EventProcessorEventSource.Current.Message($"Got checkpoint for {partitionId}: {result.Offset}//{result.S |
| | 112 | | } |
| | 113 | | else |
| | 114 | | { |
| 6 | 115 | | EventProcessorEventSource.Current.Message($"No checkpoint found for {partitionId}: returning null"); |
| | 116 | | } |
| | 117 | |
|
| 8 | 118 | | return result; |
| 8 | 119 | | } |
| | 120 | |
|
| | 121 | | private async Task PutWithRetry(string partitionId, Checkpoint checkpoint, CancellationToken cancellationToken) |
| | 122 | | { |
| 6 | 123 | | EventProcessorEventSource.Current.Message($"Setting checkpoint for {partitionId}: {checkpoint.Offset}//{chec |
| | 124 | |
|
| 6 | 125 | | Exception lastException = null; |
| 0 | 126 | | for (int i = 0; i < Constants.RetryCount; i++) |
| | 127 | | { |
| 6 | 128 | | cancellationToken.ThrowIfCancellationRequested(); |
| 6 | 129 | | lastException = null; |
| | 130 | |
|
| 6 | 131 | | Dictionary<string, object> putThis = checkpoint.ToDictionary(); |
| | 132 | |
|
| | 133 | | try |
| | 134 | | { |
| 6 | 135 | | using (ITransaction tx = this.reliableStateManager.CreateTransaction()) |
| | 136 | | { |
| 6 | 137 | | await this.store.SetAsync(tx, partitionId, putThis, Constants.ReliableDictionaryTimeout, cancell |
| 6 | 138 | | await tx.CommitAsync().ConfigureAwait(false); |
| | 139 | |
|
| | 140 | | // Success! Break out of the retry loop. |
| 6 | 141 | | break; |
| | 142 | | } |
| | 143 | | } |
| 0 | 144 | | catch (TimeoutException e) |
| | 145 | | { |
| 0 | 146 | | lastException = e; |
| 0 | 147 | | } |
| | 148 | | } |
| | 149 | |
|
| 6 | 150 | | if (lastException != null) |
| | 151 | | { |
| | 152 | | // Ran out of retries, throw. |
| 0 | 153 | | throw new Exception("Ran out of retries creating checkpoint", lastException); |
| | 154 | | } |
| | 155 | |
|
| 6 | 156 | | EventProcessorEventSource.Current.Message($"Set checkpoint for {partitionId} OK"); |
| 6 | 157 | | } |
| | 158 | | } |
| | 159 | | } |