private async Task <long> GetRemainingWorkAsync(ILease existingLease) { ChangeFeedOptions options = new ChangeFeedOptions { MaxItemCount = 1, PartitionKeyRangeId = existingLease.PartitionId, RequestContinuation = existingLease.ContinuationToken, StartFromBeginning = string.IsNullOrEmpty(existingLease.ContinuationToken), }; IChangeFeedDocumentQuery <Document> query = this.feedDocumentClient.CreateDocumentChangeFeedQuery(this.collectionSelfLink, options); IFeedResponse <Document> response = null; try { response = await query.ExecuteNextAsync <Document>().ConfigureAwait(false); long parsedLSNFromSessionToken = TryConvertToNumber(ExtractLsnFromSessionToken(response.SessionToken)); long lastQueryLSN = response.Count > 0 ? TryConvertToNumber(GetFirstDocument(response).GetPropertyValue <string>(LSNPropertyName)) - 1 : parsedLSNFromSessionToken; if (lastQueryLSN < 0) { return(1); } long partitionRemainingWork = parsedLSNFromSessionToken - lastQueryLSN; return(partitionRemainingWork < 0 ? 0 : partitionRemainingWork); } catch (Exception clientException) { Logger.WarnException($"GetEstimateWork > exception: partition '{existingLease.PartitionId}'", clientException); throw; } }
public Task <IFeedResponse <TResult> > ExecuteNextAsync <TResult>(CancellationToken token = new CancellationToken()) { return(_meter.MeasureAsync( "ExecuteNextAsync", _partitionRangeId, async() => await _inner.ExecuteNextAsync <TResult>(token) )); }
public async Task <long> GetEstimatedRemainingWork() { long remainingWork = 0; bool hasAtLeastOneLease = false; ChangeFeedOptions options = new ChangeFeedOptions { MaxItemCount = 1, }; foreach (ILease existingLease in await this.leaseManager.ListAllLeasesAsync().ConfigureAwait(false)) { hasAtLeastOneLease = true; options.PartitionKeyRangeId = existingLease.PartitionId; options.RequestContinuation = existingLease.ContinuationToken; options.StartFromBeginning = string.IsNullOrEmpty(existingLease.ContinuationToken); IChangeFeedDocumentQuery <Document> query = this.feedDocumentClient.CreateDocumentChangeFeedQuery(this.collectionSelfLink, options); IFeedResponse <Document> response = null; try { response = await query.ExecuteNextAsync <Document>().ConfigureAwait(false); long parsedLSNFromSessionToken = TryConvertToNumber(ExtractLSNFromSessionToken(response.SessionToken)); long lastQueryLSN = response.Count > 0 ? TryConvertToNumber(GetFirstDocument(response).GetPropertyValue <string>(LSNPropertyName)) - 1 : parsedLSNFromSessionToken; if (lastQueryLSN < 0) { // Could not parse LSN from document, we cannot determine the amount of changes but since the query returned 1 document, we know it's at least 1 remainingWork += 1; continue; } long partitionRemainingWork = parsedLSNFromSessionToken - lastQueryLSN; remainingWork += partitionRemainingWork < 0 ? 0 : partitionRemainingWork; } catch (DocumentClientException clientException) { Logger.WarnException("GetEstimateWork > exception: partition '{0}'", clientException, existingLease.PartitionId); } } if (!hasAtLeastOneLease) { return(1); } return(remainingWork); }