| | | 1 | | #nullable enable |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Concurrent; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Text; |
| | | 6 | | using System.Threading; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using dotnet_etcd.interfaces; |
| | | 9 | | using Etcdserverpb; |
| | | 10 | | using Google.Protobuf; |
| | | 11 | | using Grpc.Core; |
| | | 12 | | using Mvccpb; |
| | | 13 | | |
| | | 14 | | namespace dotnet_etcd; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Manages watch streams and provides a way to cancel watches |
| | | 18 | | /// </summary> |
| | | 19 | | public class WatchManager : IWatchManager |
| | | 20 | | { |
| | 63 | 21 | | private readonly object _lockObject = new(); |
| | 63 | 22 | | private readonly ConcurrentDictionary<long, WatchCancellation> _watches = new(); |
| | 63 | 23 | | private readonly ConcurrentDictionary<long, long> _watchIdMapping = new(); |
| | | 24 | | |
| | | 25 | | private readonly |
| | | 26 | | Func<Metadata?, DateTime?, CancellationToken, IAsyncDuplexStreamingCall<WatchRequest, WatchResponse>> |
| | | 27 | | _watchStreamFactory; |
| | | 28 | | |
| | | 29 | | private bool _disposed; |
| | 63 | 30 | | private long _nextWatchId = 1; |
| | | 31 | | private Watcher? _watchStream; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Creates a new WatchManager |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="watchStreamFactory">A factory function that creates a new watch stream</param> |
| | 63 | 37 | | public WatchManager( |
| | 63 | 38 | | Func<Metadata?, DateTime?, CancellationToken, IAsyncDuplexStreamingCall<WatchRequest, WatchResponse>> |
| | 126 | 39 | | watchStreamFactory) => _watchStreamFactory = |
| | 63 | 40 | | watchStreamFactory ?? throw new ArgumentNullException(nameof(watchStreamFactory)); |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Creates a new watch request |
| | | 44 | | /// </summary> |
| | | 45 | | /// <param name="request">The watch requests to create</param> |
| | | 46 | | /// <param name="callback">The callback to invoke when a watch event is received</param> |
| | | 47 | | /// <param name="headers">The initial metadata to send with the call</param> |
| | | 48 | | /// <param name="deadline">An optional deadline for the call</param> |
| | | 49 | | /// <param name="cancellationToken">An optional token for canceling the call</param> |
| | | 50 | | /// <returns>A watch ID that can be used to cancel the watch</returns> |
| | | 51 | | public async Task<long> WatchAsync(WatchRequest request, Action<WatchResponse> callback, Metadata? headers = null, |
| | | 52 | | DateTime? deadline = null, CancellationToken cancellationToken = default) |
| | 0 | 53 | | { |
| | 0 | 54 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | | 55 | | |
| | | 56 | | // Create a new watch stream if needed |
| | 0 | 57 | | EnsureWatchStream(headers, deadline, cancellationToken); |
| | | 58 | | |
| | | 59 | | // Generate a new watch ID |
| | 0 | 60 | | long watchId = Interlocked.Increment(ref _nextWatchId); |
| | | 61 | | |
| | | 62 | | // Create a cancellation token source that can be used to cancel the watch |
| | 0 | 63 | | CancellationTokenSource cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); |
| | | 64 | | |
| | | 65 | | // Create a watch cancellation object |
| | 0 | 66 | | WatchCancellation watchCancellation = new() |
| | 0 | 67 | | { |
| | 0 | 68 | | WatchId = watchId, |
| | 0 | 69 | | CancellationTokenSource = cts, |
| | 0 | 70 | | Request = request, |
| | 0 | 71 | | Callback = WrappedCallback |
| | 0 | 72 | | }; |
| | | 73 | | |
| | 0 | 74 | | request.CreateRequest.WatchId = watchId; |
| | | 75 | | // Create the watch |
| | 0 | 76 | | await _watchStream!.CreateWatchAsync(request, WrappedCallback).ConfigureAwait(false); |
| | | 77 | | |
| | | 78 | | // Add the watch cancellation to the dictionary |
| | 0 | 79 | | _watches[watchId] = watchCancellation; |
| | | 80 | | |
| | | 81 | | // Since we don't get a server watch ID from CreateWatchAsync, we can't map it |
| | | 82 | | // The server will assign a watch ID and include it in the watch response |
| | | 83 | | // Our wrappedCallback will handle this mapping when it receives the response |
| | | 84 | | |
| | 0 | 85 | | return watchId; |
| | | 86 | | |
| | | 87 | | // Create a wrapper callback that checks if the watch has been canceled |
| | | 88 | | void WrappedCallback(WatchResponse response) |
| | 0 | 89 | | { |
| | 0 | 90 | | if (cts.IsCancellationRequested) |
| | 0 | 91 | | { |
| | 0 | 92 | | return; |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | // If this is a creation response, update our watch ID mapping |
| | 0 | 96 | | if (response.Created) |
| | 0 | 97 | | { |
| | | 98 | | // Map the server-assigned watch ID to our client-generated watch ID |
| | 0 | 99 | | _watchIdMapping[response.WatchId] = watchId; |
| | 0 | 100 | | } |
| | | 101 | | |
| | 0 | 102 | | callback(response); |
| | 0 | 103 | | } |
| | 0 | 104 | | } |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// Creates a new watch request |
| | | 108 | | /// </summary> |
| | | 109 | | /// <param name="request">The watch request to create</param> |
| | | 110 | | /// <param name="callback">The callback to invoke when a watch event is received</param> |
| | | 111 | | /// <param name="headers">The initial metadata to send with the call</param> |
| | | 112 | | /// <param name="deadline">An optional deadline for the call</param> |
| | | 113 | | /// <param name="cancellationToken">An optional token for canceling the call</param> |
| | | 114 | | /// <returns>A watch ID that can be used to cancel the watch</returns> |
| | | 115 | | public long Watch(WatchRequest request, Action<WatchResponse> callback, Metadata? headers = null, |
| | | 116 | | DateTime? deadline = null, CancellationToken cancellationToken = default) |
| | 0 | 117 | | { |
| | | 118 | | // Run the async method synchronously |
| | 0 | 119 | | Task<long> task = Task.Run(() => WatchAsync(request, callback, headers, deadline, cancellationToken), cancellati |
| | 0 | 120 | | task.Wait(cancellationToken); |
| | 0 | 121 | | return task.Result; |
| | 0 | 122 | | } |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// Watches a specific key |
| | | 126 | | /// </summary> |
| | | 127 | | /// <param name="key">Key to watch</param> |
| | | 128 | | /// <param name="action">Action to be executed when watch event is triggered</param> |
| | | 129 | | /// <param name="headers">The initial metadata to send with the call</param> |
| | | 130 | | /// <param name="deadline">An optional deadline for the call</param> |
| | | 131 | | /// <param name="cancellationToken">An optional token for canceling the call</param> |
| | | 132 | | /// <returns>Watch ID</returns> |
| | | 133 | | public long Watch(string key, Action<WatchEvent> action, Metadata? headers = null, DateTime? deadline = null, |
| | | 134 | | CancellationToken cancellationToken = default) |
| | 0 | 135 | | { |
| | 0 | 136 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | | 137 | | |
| | | 138 | | // Create a watch request for the key |
| | 0 | 139 | | WatchRequest request = new() |
| | 0 | 140 | | { |
| | 0 | 141 | | CreateRequest = new WatchCreateRequest |
| | 0 | 142 | | { |
| | 0 | 143 | | Key = ByteString.CopyFromUtf8(key), ProgressNotify = true, PrevKv = true |
| | 0 | 144 | | } |
| | 0 | 145 | | }; |
| | | 146 | | |
| | | 147 | | // Call the Watch method with the request |
| | 0 | 148 | | return Watch(request, Callback, headers, deadline, cancellationToken); |
| | | 149 | | |
| | | 150 | | // Create a wrapper callback that converts the WatchResponse to a WatchEvent |
| | | 151 | | void Callback(WatchResponse response) |
| | 0 | 152 | | { |
| | 0 | 153 | | if (response.Events == null) |
| | 0 | 154 | | { |
| | 0 | 155 | | return; |
| | | 156 | | } |
| | | 157 | | |
| | 0 | 158 | | foreach (Event evt in response.Events) |
| | 0 | 159 | | { |
| | 0 | 160 | | WatchEvent watchEvent = new() { Key = evt.Kv.Key.ToStringUtf8(), Value = evt.Kv.Value.ToStringUtf8(), Ty |
| | 0 | 161 | | action(watchEvent); |
| | 0 | 162 | | } |
| | 0 | 163 | | } |
| | 0 | 164 | | } |
| | | 165 | | |
| | | 166 | | /// <summary> |
| | | 167 | | /// Watches a range of keys with a prefix |
| | | 168 | | /// </summary> |
| | | 169 | | /// <param name="prefixKey">Prefix key to watch</param> |
| | | 170 | | /// <param name="action">Action to be executed when watch event is triggered</param> |
| | | 171 | | /// <param name="headers">The initial metadata to send with the call</param> |
| | | 172 | | /// <param name="deadline">An optional deadline for the call</param> |
| | | 173 | | /// <param name="cancellationToken">An optional token for canceling the call</param> |
| | | 174 | | /// <returns>Watch ID</returns> |
| | | 175 | | public long WatchRange(string prefixKey, Action<WatchEvent> action, Metadata? headers = null, |
| | | 176 | | DateTime? deadline = null, CancellationToken cancellationToken = default) |
| | 0 | 177 | | { |
| | 0 | 178 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | | 179 | | |
| | | 180 | | // Create a watch request for the range |
| | 0 | 181 | | WatchRequest request = new() |
| | 0 | 182 | | { |
| | 0 | 183 | | CreateRequest = new WatchCreateRequest |
| | 0 | 184 | | { |
| | 0 | 185 | | Key = ByteString.CopyFromUtf8(prefixKey), |
| | 0 | 186 | | RangeEnd = ByteString.CopyFromUtf8(GetRangeEnd(prefixKey)), |
| | 0 | 187 | | ProgressNotify = true, |
| | 0 | 188 | | PrevKv = true |
| | 0 | 189 | | } |
| | 0 | 190 | | }; |
| | | 191 | | |
| | | 192 | | // Create a wrapper callback that converts the WatchResponse to a WatchEvent |
| | 0 | 193 | | Action<WatchResponse> callback = response => |
| | 0 | 194 | | { |
| | 0 | 195 | | if (response.Events == null) |
| | 0 | 196 | | { |
| | 0 | 197 | | return; |
| | 0 | 198 | | } |
| | 0 | 199 | | |
| | 0 | 200 | | foreach (Event evt in response.Events) |
| | 0 | 201 | | { |
| | 0 | 202 | | WatchEvent watchEvent = new() |
| | 0 | 203 | | { |
| | 0 | 204 | | Key = evt.Kv.Key.ToStringUtf8(), Value = evt.Kv.Value.ToStringUtf8(), Type = evt.Type |
| | 0 | 205 | | }; |
| | 0 | 206 | | action(watchEvent); |
| | 0 | 207 | | } |
| | 0 | 208 | | }; |
| | | 209 | | |
| | | 210 | | // Call the Watch method with the request |
| | 0 | 211 | | return Watch(request, callback, headers, deadline, cancellationToken); |
| | 0 | 212 | | } |
| | | 213 | | |
| | | 214 | | /// <summary> |
| | | 215 | | /// Watches a specific key with start revision |
| | | 216 | | /// </summary> |
| | | 217 | | /// <param name="key">Key to watch</param> |
| | | 218 | | /// <param name="startRevision">Start revision</param> |
| | | 219 | | /// <param name="action">Action to be executed when watch event is triggered</param> |
| | | 220 | | /// <param name="headers">The initial metadata to send with the call</param> |
| | | 221 | | /// <param name="deadline">An optional deadline for the call</param> |
| | | 222 | | /// <param name="cancellationToken">An optional token for canceling the call</param> |
| | | 223 | | /// <returns>Watch ID</returns> |
| | | 224 | | public long Watch(string key, long startRevision, Action<WatchEvent> action, Metadata? headers = null, |
| | | 225 | | DateTime? deadline = null, CancellationToken cancellationToken = default) |
| | 0 | 226 | | { |
| | 0 | 227 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | | 228 | | |
| | | 229 | | // Create a watch request for the key with start revision |
| | 0 | 230 | | WatchRequest request = new() |
| | 0 | 231 | | { |
| | 0 | 232 | | CreateRequest = new WatchCreateRequest |
| | 0 | 233 | | { |
| | 0 | 234 | | Key = ByteString.CopyFromUtf8(key), |
| | 0 | 235 | | StartRevision = startRevision, |
| | 0 | 236 | | ProgressNotify = true, |
| | 0 | 237 | | PrevKv = true |
| | 0 | 238 | | } |
| | 0 | 239 | | }; |
| | | 240 | | |
| | | 241 | | // Create a wrapper callback that converts the WatchResponse to a WatchEvent |
| | 0 | 242 | | Action<WatchResponse> callback = response => |
| | 0 | 243 | | { |
| | 0 | 244 | | if (response.Events == null) |
| | 0 | 245 | | { |
| | 0 | 246 | | return; |
| | 0 | 247 | | } |
| | 0 | 248 | | |
| | 0 | 249 | | foreach (Event evt in response.Events) |
| | 0 | 250 | | { |
| | 0 | 251 | | WatchEvent watchEvent = new() |
| | 0 | 252 | | { |
| | 0 | 253 | | Key = evt.Kv.Key.ToStringUtf8(), Value = evt.Kv.Value.ToStringUtf8(), Type = evt.Type |
| | 0 | 254 | | }; |
| | 0 | 255 | | action(watchEvent); |
| | 0 | 256 | | } |
| | 0 | 257 | | }; |
| | | 258 | | |
| | | 259 | | // Call the Watch method with the request |
| | 0 | 260 | | return Watch(request, callback, headers, deadline, cancellationToken); |
| | 0 | 261 | | } |
| | | 262 | | |
| | | 263 | | /// <summary> |
| | | 264 | | /// Watches a range of keys with a prefix and start revision |
| | | 265 | | /// </summary> |
| | | 266 | | /// <param name="prefixKey">Prefix key to watch</param> |
| | | 267 | | /// <param name="startRevision">Start revision</param> |
| | | 268 | | /// <param name="action">Action to be executed when watch event is triggered</param> |
| | | 269 | | /// <param name="headers">The initial metadata to send with the call</param> |
| | | 270 | | /// <param name="deadline">An optional deadline for the call</param> |
| | | 271 | | /// <param name="cancellationToken">An optional token for canceling the call</param> |
| | | 272 | | /// <returns>Watch ID</returns> |
| | | 273 | | public long WatchRange(string prefixKey, long startRevision, Action<WatchEvent> action, Metadata? headers = null, |
| | | 274 | | DateTime? deadline = null, CancellationToken cancellationToken = default) |
| | 0 | 275 | | { |
| | 0 | 276 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | | 277 | | |
| | | 278 | | // Create a watch request for the range with start revision |
| | 0 | 279 | | WatchRequest request = new() |
| | 0 | 280 | | { |
| | 0 | 281 | | CreateRequest = new WatchCreateRequest |
| | 0 | 282 | | { |
| | 0 | 283 | | Key = ByteString.CopyFromUtf8(prefixKey), |
| | 0 | 284 | | RangeEnd = ByteString.CopyFromUtf8(GetRangeEnd(prefixKey)), |
| | 0 | 285 | | StartRevision = startRevision, |
| | 0 | 286 | | ProgressNotify = true, |
| | 0 | 287 | | PrevKv = true |
| | 0 | 288 | | } |
| | 0 | 289 | | }; |
| | | 290 | | |
| | | 291 | | // Create a wrapper callback that converts the WatchResponse to a WatchEvent |
| | 0 | 292 | | Action<WatchResponse> callback = response => |
| | 0 | 293 | | { |
| | 0 | 294 | | if (response.Events == null) |
| | 0 | 295 | | { |
| | 0 | 296 | | return; |
| | 0 | 297 | | } |
| | 0 | 298 | | |
| | 0 | 299 | | foreach (Event evt in response.Events) |
| | 0 | 300 | | { |
| | 0 | 301 | | WatchEvent watchEvent = new() |
| | 0 | 302 | | { |
| | 0 | 303 | | Key = evt.Kv.Key.ToStringUtf8(), Value = evt.Kv.Value.ToStringUtf8(), Type = evt.Type |
| | 0 | 304 | | }; |
| | 0 | 305 | | action(watchEvent); |
| | 0 | 306 | | } |
| | 0 | 307 | | }; |
| | | 308 | | |
| | | 309 | | // Call the Watch method with the request |
| | 0 | 310 | | return Watch(request, callback, headers, deadline, cancellationToken); |
| | 0 | 311 | | } |
| | | 312 | | |
| | | 313 | | /// <summary> |
| | | 314 | | /// Watches a specific key asynchronously |
| | | 315 | | /// </summary> |
| | | 316 | | /// <param name="key">Key to watch</param> |
| | | 317 | | /// <param name="action">Action to be executed when watch event is triggered</param> |
| | | 318 | | /// <param name="headers">The initial metadata to send with the call</param> |
| | | 319 | | /// <param name="deadline">An optional deadline for the call</param> |
| | | 320 | | /// <param name="cancellationToken">An optional token for canceling the call</param> |
| | | 321 | | /// <returns>Watch ID</returns> |
| | | 322 | | public async Task<long> WatchAsync(string key, Action<WatchEvent> action, Metadata? headers = null, |
| | | 323 | | DateTime? deadline = null, CancellationToken cancellationToken = default) |
| | 0 | 324 | | { |
| | 0 | 325 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | | 326 | | |
| | | 327 | | // Create a watch request for the key |
| | 0 | 328 | | WatchRequest request = new() |
| | 0 | 329 | | { |
| | 0 | 330 | | CreateRequest = new WatchCreateRequest |
| | 0 | 331 | | { |
| | 0 | 332 | | Key = ByteString.CopyFromUtf8(key), ProgressNotify = true, PrevKv = true |
| | 0 | 333 | | } |
| | 0 | 334 | | }; |
| | | 335 | | |
| | | 336 | | // Create a wrapper callback that converts the WatchResponse to a WatchEvent |
| | 0 | 337 | | Action<WatchResponse> callback = response => |
| | 0 | 338 | | { |
| | 0 | 339 | | if (response.Events == null) |
| | 0 | 340 | | { |
| | 0 | 341 | | return; |
| | 0 | 342 | | } |
| | 0 | 343 | | |
| | 0 | 344 | | foreach (Event evt in response.Events) |
| | 0 | 345 | | { |
| | 0 | 346 | | WatchEvent watchEvent = new() |
| | 0 | 347 | | { |
| | 0 | 348 | | Key = evt.Kv.Key.ToStringUtf8(), Value = evt.Kv.Value.ToStringUtf8(), Type = evt.Type |
| | 0 | 349 | | }; |
| | 0 | 350 | | action(watchEvent); |
| | 0 | 351 | | } |
| | 0 | 352 | | }; |
| | | 353 | | |
| | | 354 | | // Call the WatchAsync method with the request |
| | 0 | 355 | | return await WatchAsync(request, callback, headers, deadline, cancellationToken).ConfigureAwait(false); |
| | 0 | 356 | | } |
| | | 357 | | |
| | | 358 | | /// <summary> |
| | | 359 | | /// Watches a range of keys with a prefix asynchronously |
| | | 360 | | /// </summary> |
| | | 361 | | /// <param name="prefixKey">Prefix key to watch</param> |
| | | 362 | | /// <param name="action">Action to be executed when watch event is triggered</param> |
| | | 363 | | /// <param name="headers">The initial metadata to send with the call</param> |
| | | 364 | | /// <param name="deadline">An optional deadline for the call</param> |
| | | 365 | | /// <param name="cancellationToken">An optional token for canceling the call</param> |
| | | 366 | | /// <returns>Watch ID</returns> |
| | | 367 | | public async Task<long> WatchRangeAsync(string prefixKey, Action<WatchEvent> action, Metadata? headers = null, |
| | | 368 | | DateTime? deadline = null, CancellationToken cancellationToken = default) |
| | 0 | 369 | | { |
| | 0 | 370 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | | 371 | | |
| | | 372 | | // Create a watch request for the range |
| | 0 | 373 | | WatchRequest request = new() |
| | 0 | 374 | | { |
| | 0 | 375 | | CreateRequest = new WatchCreateRequest |
| | 0 | 376 | | { |
| | 0 | 377 | | Key = ByteString.CopyFromUtf8(prefixKey), |
| | 0 | 378 | | RangeEnd = ByteString.CopyFromUtf8(GetRangeEnd(prefixKey)), |
| | 0 | 379 | | ProgressNotify = true, |
| | 0 | 380 | | PrevKv = true |
| | 0 | 381 | | } |
| | 0 | 382 | | }; |
| | | 383 | | |
| | | 384 | | // Create a wrapper callback that converts the WatchResponse to a WatchEvent |
| | 0 | 385 | | Action<WatchResponse> callback = response => |
| | 0 | 386 | | { |
| | 0 | 387 | | if (response.Events == null) |
| | 0 | 388 | | { |
| | 0 | 389 | | return; |
| | 0 | 390 | | } |
| | 0 | 391 | | |
| | 0 | 392 | | foreach (Event evt in response.Events) |
| | 0 | 393 | | { |
| | 0 | 394 | | WatchEvent watchEvent = new() |
| | 0 | 395 | | { |
| | 0 | 396 | | Key = evt.Kv.Key.ToStringUtf8(), Value = evt.Kv.Value.ToStringUtf8(), Type = evt.Type |
| | 0 | 397 | | }; |
| | 0 | 398 | | action(watchEvent); |
| | 0 | 399 | | } |
| | 0 | 400 | | }; |
| | | 401 | | |
| | | 402 | | // Call the WatchAsync method with the request |
| | 0 | 403 | | return await WatchAsync(request, callback, headers, deadline, cancellationToken).ConfigureAwait(false); |
| | 0 | 404 | | } |
| | | 405 | | |
| | | 406 | | /// <summary> |
| | | 407 | | /// Watches a specific key with start revision asynchronously |
| | | 408 | | /// </summary> |
| | | 409 | | /// <param name="key">Key to watch</param> |
| | | 410 | | /// <param name="startRevision">Start revision</param> |
| | | 411 | | /// <param name="action">Action to be executed when watch event is triggered</param> |
| | | 412 | | /// <param name="headers">The initial metadata to send with the call</param> |
| | | 413 | | /// <param name="deadline">An optional deadline for the call</param> |
| | | 414 | | /// <param name="cancellationToken">An optional token for canceling the call</param> |
| | | 415 | | /// <returns>Watch ID</returns> |
| | | 416 | | public async Task<long> WatchAsync(string key, long startRevision, Action<WatchEvent> action, |
| | | 417 | | Metadata? headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default) |
| | 0 | 418 | | { |
| | 0 | 419 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | | 420 | | |
| | | 421 | | // Create a watch request for the key with start revision |
| | 0 | 422 | | WatchRequest request = new() |
| | 0 | 423 | | { |
| | 0 | 424 | | CreateRequest = new WatchCreateRequest |
| | 0 | 425 | | { |
| | 0 | 426 | | Key = ByteString.CopyFromUtf8(key), |
| | 0 | 427 | | StartRevision = startRevision, |
| | 0 | 428 | | ProgressNotify = true, |
| | 0 | 429 | | PrevKv = true |
| | 0 | 430 | | } |
| | 0 | 431 | | }; |
| | | 432 | | |
| | | 433 | | // Create a wrapper callback that converts the WatchResponse to a WatchEvent |
| | 0 | 434 | | Action<WatchResponse> callback = response => |
| | 0 | 435 | | { |
| | 0 | 436 | | if (response.Events == null) |
| | 0 | 437 | | { |
| | 0 | 438 | | return; |
| | 0 | 439 | | } |
| | 0 | 440 | | |
| | 0 | 441 | | foreach (Event evt in response.Events) |
| | 0 | 442 | | { |
| | 0 | 443 | | WatchEvent watchEvent = new() |
| | 0 | 444 | | { |
| | 0 | 445 | | Key = evt.Kv.Key.ToStringUtf8(), Value = evt.Kv.Value.ToStringUtf8(), Type = evt.Type |
| | 0 | 446 | | }; |
| | 0 | 447 | | action(watchEvent); |
| | 0 | 448 | | } |
| | 0 | 449 | | }; |
| | | 450 | | |
| | | 451 | | // Call the WatchAsync method with the request |
| | 0 | 452 | | return await WatchAsync(request, callback, headers, deadline, cancellationToken).ConfigureAwait(false); |
| | 0 | 453 | | } |
| | | 454 | | |
| | | 455 | | /// <summary> |
| | | 456 | | /// Watches a range of keys with a prefix and start revision asynchronously |
| | | 457 | | /// </summary> |
| | | 458 | | /// <param name="prefixKey">Prefix key to watch</param> |
| | | 459 | | /// <param name="startRevision">Start revision</param> |
| | | 460 | | /// <param name="action">Action to be executed when watch event is triggered</param> |
| | | 461 | | /// <param name="headers">The initial metadata to send with the call</param> |
| | | 462 | | /// <param name="deadline">An optional deadline for the call</param> |
| | | 463 | | /// <param name="cancellationToken">An optional token for canceling the call</param> |
| | | 464 | | /// <returns>Watch ID</returns> |
| | | 465 | | public async Task<long> WatchRangeAsync(string prefixKey, long startRevision, Action<WatchEvent> action, |
| | | 466 | | Metadata? headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default) |
| | 0 | 467 | | { |
| | 0 | 468 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | | 469 | | |
| | | 470 | | // Create a watch request for the range with start revision |
| | 0 | 471 | | WatchRequest request = new() |
| | 0 | 472 | | { |
| | 0 | 473 | | CreateRequest = new WatchCreateRequest |
| | 0 | 474 | | { |
| | 0 | 475 | | Key = ByteString.CopyFromUtf8(prefixKey), |
| | 0 | 476 | | RangeEnd = ByteString.CopyFromUtf8(GetRangeEnd(prefixKey)), |
| | 0 | 477 | | StartRevision = startRevision, |
| | 0 | 478 | | ProgressNotify = true, |
| | 0 | 479 | | PrevKv = true |
| | 0 | 480 | | } |
| | 0 | 481 | | }; |
| | | 482 | | |
| | | 483 | | // Create a wrapper callback that converts the WatchResponse to a WatchEvent |
| | 0 | 484 | | Action<WatchResponse> callback = response => |
| | 0 | 485 | | { |
| | 0 | 486 | | if (response.Events == null) |
| | 0 | 487 | | { |
| | 0 | 488 | | return; |
| | 0 | 489 | | } |
| | 0 | 490 | | |
| | 0 | 491 | | foreach (Event evt in response.Events) |
| | 0 | 492 | | { |
| | 0 | 493 | | WatchEvent watchEvent = new() |
| | 0 | 494 | | { |
| | 0 | 495 | | Key = evt.Kv.Key.ToStringUtf8(), Value = evt.Kv.Value.ToStringUtf8(), Type = evt.Type |
| | 0 | 496 | | }; |
| | 0 | 497 | | action(watchEvent); |
| | 0 | 498 | | } |
| | 0 | 499 | | }; |
| | | 500 | | |
| | | 501 | | // Call the WatchAsync method with the request |
| | 0 | 502 | | return await WatchAsync(request, callback, headers, deadline, cancellationToken).ConfigureAwait(false); |
| | 0 | 503 | | } |
| | | 504 | | |
| | | 505 | | /// <summary> |
| | | 506 | | /// Watches a key range |
| | | 507 | | /// </summary> |
| | | 508 | | /// <param name="path">The path to watch</param> |
| | | 509 | | /// <param name="callback">The callback to invoke when a watch event is received</param> |
| | | 510 | | /// <param name="headers">The initial metadata to send with the call</param> |
| | | 511 | | /// <param name="deadline">An optional deadline for the call</param> |
| | | 512 | | /// <param name="cancellationToken">An optional token for canceling the call</param> |
| | | 513 | | /// <returns>A watch ID that can be used to cancel the watch</returns> |
| | | 514 | | public long WatchRange(string path, Action<WatchResponse> callback, Metadata? headers = null, |
| | | 515 | | DateTime? deadline = null, CancellationToken cancellationToken = default) |
| | 0 | 516 | | { |
| | 0 | 517 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | | 518 | | |
| | | 519 | | // Create a watch request for the range |
| | 0 | 520 | | WatchRequest request = new() |
| | 0 | 521 | | { |
| | 0 | 522 | | CreateRequest = new WatchCreateRequest |
| | 0 | 523 | | { |
| | 0 | 524 | | Key = ByteString.CopyFromUtf8(path), |
| | 0 | 525 | | RangeEnd = ByteString.CopyFromUtf8(GetRangeEnd(path)), |
| | 0 | 526 | | ProgressNotify = true, |
| | 0 | 527 | | PrevKv = true |
| | 0 | 528 | | } |
| | 0 | 529 | | }; |
| | | 530 | | |
| | | 531 | | // Call the Watch method with the request |
| | 0 | 532 | | return Watch(request, callback, headers, deadline, cancellationToken); |
| | 0 | 533 | | } |
| | | 534 | | |
| | | 535 | | /// <summary> |
| | | 536 | | /// Cancels a watch request |
| | | 537 | | /// </summary> |
| | | 538 | | /// <param name="watchId">The ID of the watch to cancel</param> |
| | | 539 | | public void CancelWatch(long watchId) |
| | 0 | 540 | | { |
| | 0 | 541 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | | 542 | | |
| | 0 | 543 | | if (!_watches.TryRemove(watchId, out WatchCancellation? watchCancellation)) |
| | 0 | 544 | | { |
| | 0 | 545 | | return; |
| | | 546 | | } |
| | | 547 | | |
| | | 548 | | // Cancel the watch |
| | 0 | 549 | | watchCancellation.CancellationTokenSource.Cancel(); |
| | | 550 | | |
| | | 551 | | // Find the server watch ID that corresponds to our client watch ID |
| | 0 | 552 | | long serverWatchId = GetServerWatchId(watchId); |
| | | 553 | | |
| | | 554 | | // Cancel the watch on the server if we found a mapping |
| | 0 | 555 | | if (serverWatchId != -1 && _watchStream != null) |
| | 0 | 556 | | { |
| | 0 | 557 | | _watchIdMapping.TryRemove(serverWatchId, out _); |
| | 0 | 558 | | _watchStream.CancelWatchAsync(serverWatchId).ContinueWith(_ => |
| | 0 | 559 | | { |
| | 0 | 560 | | // Ignore exceptions |
| | 0 | 561 | | }); |
| | 0 | 562 | | } |
| | | 563 | | |
| | | 564 | | // Dispose the cancellation token source |
| | 0 | 565 | | watchCancellation.CancellationTokenSource.Dispose(); |
| | 0 | 566 | | } |
| | | 567 | | |
| | | 568 | | /// <summary> |
| | | 569 | | /// Disposes the watch manager |
| | | 570 | | /// </summary> |
| | | 571 | | public void Dispose() |
| | 1 | 572 | | { |
| | 1 | 573 | | if (_disposed) |
| | 0 | 574 | | { |
| | 0 | 575 | | return; |
| | | 576 | | } |
| | | 577 | | |
| | 1 | 578 | | _disposed = true; |
| | | 579 | | |
| | | 580 | | // Cancel all watches |
| | 3 | 581 | | foreach (WatchCancellation watchCancellation in _watches.Values) |
| | 0 | 582 | | { |
| | 0 | 583 | | watchCancellation.CancellationTokenSource.Cancel(); |
| | 0 | 584 | | watchCancellation.CancellationTokenSource.Dispose(); |
| | 0 | 585 | | } |
| | | 586 | | |
| | 1 | 587 | | _watches.Clear(); |
| | | 588 | | |
| | | 589 | | // Dispose the watch stream |
| | 1 | 590 | | if (_watchStream != null) |
| | 0 | 591 | | { |
| | 0 | 592 | | _watchStream.Dispose(); |
| | 0 | 593 | | _watchStream = null; |
| | 0 | 594 | | } |
| | | 595 | | |
| | 1 | 596 | | GC.SuppressFinalize(this); |
| | 1 | 597 | | } |
| | | 598 | | |
| | | 599 | | private static string GetRangeEnd(string path) |
| | 0 | 600 | | { |
| | | 601 | | // Calculate the range end for the given path |
| | | 602 | | // This is the same logic used in EtcdClient.GetRangeEnd |
| | 0 | 603 | | byte[] bytes = Encoding.UTF8.GetBytes(path); |
| | 0 | 604 | | for (int i = bytes.Length - 1; i >= 0; i--) |
| | 0 | 605 | | { |
| | 0 | 606 | | if (bytes[i] >= 0xff) |
| | 0 | 607 | | { |
| | 0 | 608 | | continue; |
| | | 609 | | } |
| | | 610 | | |
| | 0 | 611 | | bytes[i]++; |
| | 0 | 612 | | return Encoding.UTF8.GetString(bytes, 0, i + 1); |
| | | 613 | | } |
| | | 614 | | |
| | 0 | 615 | | return string.Empty; |
| | 0 | 616 | | } |
| | | 617 | | |
| | | 618 | | /// <summary> |
| | | 619 | | /// Gets the server watch ID for a client watch ID |
| | | 620 | | /// </summary> |
| | | 621 | | /// <param name="clientWatchId">The client watch ID</param> |
| | | 622 | | /// <returns>The server watch ID, or -1 if not found</returns> |
| | | 623 | | private long GetServerWatchId(long clientWatchId) |
| | 0 | 624 | | { |
| | 0 | 625 | | foreach (KeyValuePair<long, long> kvp in _watchIdMapping) |
| | 0 | 626 | | { |
| | 0 | 627 | | if (kvp.Value == clientWatchId) |
| | 0 | 628 | | { |
| | 0 | 629 | | return kvp.Key; |
| | | 630 | | } |
| | 0 | 631 | | } |
| | | 632 | | |
| | 0 | 633 | | return -1; |
| | 0 | 634 | | } |
| | | 635 | | |
| | | 636 | | /// <param name="cancellationToken">An optional token for canceling the call</param> |
| | | 637 | | private void EnsureWatchStream(Metadata? headers, DateTime? deadline, CancellationToken cancellationToken) |
| | 0 | 638 | | { |
| | 0 | 639 | | lock (_lockObject) |
| | 0 | 640 | | { |
| | 0 | 641 | | if (_watchStream != null) |
| | 0 | 642 | | { |
| | 0 | 643 | | return; |
| | | 644 | | } |
| | | 645 | | |
| | | 646 | | // Create a new watch stream |
| | 0 | 647 | | IAsyncDuplexStreamingCall<WatchRequest, WatchResponse> watchStreamCall = |
| | 0 | 648 | | _watchStreamFactory(headers, deadline, cancellationToken); |
| | 0 | 649 | | _watchStream = new Watcher(watchStreamCall, HandleConnectionFailure); |
| | 0 | 650 | | } |
| | 0 | 651 | | } |
| | | 652 | | |
| | | 653 | | private void HandleConnectionFailure() |
| | 0 | 654 | | { |
| | 0 | 655 | | lock (_lockObject) |
| | 0 | 656 | | { |
| | 0 | 657 | | _watchStream = null; |
| | 0 | 658 | | _watchIdMapping.Clear(); |
| | 0 | 659 | | } |
| | | 660 | | |
| | | 661 | | // Must run async to avoid blocking the caller (which might be the dead stream loop) |
| | 0 | 662 | | Task.Run(async () => |
| | 0 | 663 | | { |
| | 0 | 664 | | try |
| | 0 | 665 | | { |
| | 0 | 666 | | // Wait small delay to allow network to stabilize |
| | 0 | 667 | | await Task.Delay(500); |
| | 0 | 668 | | |
| | 0 | 669 | | lock (_lockObject) |
| | 0 | 670 | | { |
| | 0 | 671 | | if (_disposed) return; |
| | 0 | 672 | | EnsureWatchStream(null, null, default); |
| | 0 | 673 | | } |
| | 0 | 674 | | |
| | 0 | 675 | | foreach (var watch in _watches.Values) |
| | 0 | 676 | | { |
| | 0 | 677 | | await _watchStream!.CreateWatchAsync(watch.Request, watch.Callback).ConfigureAwait(false); |
| | 0 | 678 | | } |
| | 0 | 679 | | } |
| | 0 | 680 | | catch (Exception ex) |
| | 0 | 681 | | { |
| | 0 | 682 | | Console.Error.WriteLine($"Watch reconnection failed: {ex.Message}"); |
| | 0 | 683 | | // Retry in 5s |
| | 0 | 684 | | _ = Task.Delay(5000).ContinueWith(_ => HandleConnectionFailure()); |
| | 0 | 685 | | } |
| | 0 | 686 | | }); |
| | 0 | 687 | | } |
| | | 688 | | |
| | | 689 | | private class WatchCancellation |
| | | 690 | | { |
| | 0 | 691 | | public long WatchId { get; set; } |
| | 0 | 692 | | public required CancellationTokenSource CancellationTokenSource { get; set; } |
| | 0 | 693 | | public required WatchRequest Request { get; set; } |
| | 0 | 694 | | public required Action<WatchResponse> Callback { get; set; } |
| | | 695 | | } |
| | | 696 | | } |