Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Internal] Change Feed: Fixes estimator diagnostics #1930

Merged
merged 2 commits into from
Oct 13, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Microsoft.Azure.Cosmos.ChangeFeed
{
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
Expand Down Expand Up @@ -168,39 +167,32 @@ private async Task<FeedResponse<ChangeFeedProcessorState>> ReadNextInternalAsync
}

IEnumerable<DocumentServiceLease> leasesForCurrentPage = this.lazyLeaseDocuments.Result.Result.Skip(this.currentPage * this.pageSize).Take(this.pageSize);
IEnumerable<Task<List<(ChangeFeedProcessorState, double)>>> tasks = Partitioner.Create(leasesForCurrentPage)
.GetPartitions(this.pageSize)
.Select(partition => Task.Run(async () =>
IEnumerable<Task<(ChangeFeedProcessorState, ResponseMessage)>> tasks = leasesForCurrentPage
.Select(lease => Task.Run(async () =>
{
List<(ChangeFeedProcessorState, double)> partialResults = new List<(ChangeFeedProcessorState, double)>();
using (partition)
{
while (!cancellationToken.IsCancellationRequested && partition.MoveNext())
{
DocumentServiceLease item = partition.Current;
if (item?.CurrentLeaseToken == null) continue;
(long estimation, ResponseMessage responseMessage) = await this.GetRemainingWorkAsync(item, cancellationToken).ConfigureAwait(false);

// Attach each diagnostics
diagnosticsContext.AddDiagnosticsInternal(responseMessage.DiagnosticsContext);
partialResults.Add((new ChangeFeedProcessorState(item.CurrentLeaseToken, estimation, item.Owner), responseMessage.Headers.RequestCharge));
}
}
(long estimation, ResponseMessage responseMessage) = await this.GetRemainingWorkAsync(lease, cancellationToken);

return partialResults;
return (new ChangeFeedProcessorState(lease.CurrentLeaseToken, estimation, lease.Owner), responseMessage);
})).ToArray();

IEnumerable<List<(ChangeFeedProcessorState, double)>> partitionResults = await Task.WhenAll(tasks);
IEnumerable<(ChangeFeedProcessorState, ResponseMessage)> results = await Task.WhenAll(tasks);

IEnumerable<(ChangeFeedProcessorState, double)> unifiedResults = partitionResults.SelectMany(r => r);

ReadOnlyCollection<ChangeFeedProcessorState> estimations = unifiedResults.Select(r => r.Item1).ToList().AsReadOnly();
List<ChangeFeedProcessorState> estimations = new List<ChangeFeedProcessorState>();
double totalRUCost = 0;
foreach ((ChangeFeedProcessorState, ResponseMessage) result in results)
{
using (result.Item2)
{
totalRUCost += result.Item2.Headers.RequestCharge;
diagnosticsContext.AddDiagnosticsInternal(result.Item2.DiagnosticsContext);
}

double totalRUCost = unifiedResults.Sum(r => r.Item2);
estimations.Add(result.Item1);
}

this.hasMoreResults = ++this.currentPage != this.maxPage;

return new ChangeFeedEstimatorFeedResponse(diagnosticsContext.Diagnostics, estimations, totalRUCost);
return new ChangeFeedEstimatorFeedResponse(diagnosticsContext.Diagnostics, estimations.AsReadOnly(), totalRUCost);
}

/// <summary>
Expand Down