-
Notifications
You must be signed in to change notification settings - Fork 504
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Query: Adds aggressive prefetching for scalar aggregates (#3168)
* Add an aggressive prefetch policy and a fully buffered partition range page enumerator to match * Start setting up test scaffolding * Fix up some broken unit tests * Instantiate the correct type of buffered enumerator in cross partition enumerator * add some UTs for aggressive prefetching * Add unit tests for aggressive prefetching * Add some more unit test coverage * Incorporate code review feedback and add more test coverage Co-authored-by: j82w <j82w@users.noreply.github.com>
- Loading branch information
Showing
22 changed files
with
841 additions
and
236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
Microsoft.Azure.Cosmos/src/Pagination/BufferedPartitionRangePageAsyncEnumeratorBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// ------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// ------------------------------------------------------------ | ||
|
||
namespace Microsoft.Azure.Cosmos.Pagination | ||
{ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.Cosmos.Tracing; | ||
|
||
internal abstract class BufferedPartitionRangePageAsyncEnumeratorBase<TPage, TState> : PartitionRangePageAsyncEnumerator<TPage, TState>, IPrefetcher | ||
where TPage : Page<TState> | ||
where TState : State | ||
{ | ||
protected BufferedPartitionRangePageAsyncEnumeratorBase(FeedRangeState<TState> feedRangeState, CancellationToken cancellationToken) | ||
: base(feedRangeState, cancellationToken) | ||
{ | ||
} | ||
|
||
public abstract ValueTask PrefetchAsync(ITrace trace, CancellationToken cancellationToken); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
Microsoft.Azure.Cosmos/src/Pagination/FullyBufferedPartitionRangeAsyncEnumerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// ------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// ------------------------------------------------------------ | ||
namespace Microsoft.Azure.Cosmos.Pagination | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.Cosmos.Query.Core.Monads; | ||
using Microsoft.Azure.Cosmos.Tracing; | ||
|
||
internal sealed class FullyBufferedPartitionRangeAsyncEnumerator<TPage, TState> : BufferedPartitionRangePageAsyncEnumeratorBase<TPage, TState> | ||
where TPage : Page<TState> | ||
where TState : State | ||
{ | ||
private readonly PartitionRangePageAsyncEnumerator<TPage, TState> enumerator; | ||
private readonly List<TPage> bufferedPages; | ||
private int currentIndex; | ||
private Exception exception; | ||
|
||
private bool HasPrefetched => (this.exception != null) || (this.bufferedPages.Count > 0); | ||
|
||
public FullyBufferedPartitionRangeAsyncEnumerator(PartitionRangePageAsyncEnumerator<TPage, TState> enumerator, CancellationToken cancellationToken) | ||
: base(enumerator.FeedRangeState, cancellationToken) | ||
{ | ||
this.enumerator = enumerator ?? throw new ArgumentNullException(nameof(enumerator)); | ||
this.bufferedPages = new List<TPage>(); | ||
} | ||
|
||
public override ValueTask DisposeAsync() | ||
{ | ||
return this.enumerator.DisposeAsync(); | ||
} | ||
|
||
public override async ValueTask PrefetchAsync(ITrace trace, CancellationToken cancellationToken) | ||
{ | ||
if (trace == null) | ||
{ | ||
throw new ArgumentNullException(nameof(trace)); | ||
} | ||
|
||
if (this.HasPrefetched) | ||
{ | ||
return; | ||
} | ||
|
||
cancellationToken.ThrowIfCancellationRequested(); | ||
|
||
using (ITrace prefetchTrace = trace.StartChild("Prefetch", TraceComponent.Pagination, TraceLevel.Info)) | ||
{ | ||
while (await this.enumerator.MoveNextAsync(prefetchTrace)) | ||
{ | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
TryCatch<TPage> current = this.enumerator.Current; | ||
if (current.Succeeded) | ||
{ | ||
this.bufferedPages.Add(current.Result); | ||
} | ||
else | ||
{ | ||
this.exception = current.Exception; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
protected override async Task<TryCatch<TPage>> GetNextPageAsync(ITrace trace, CancellationToken cancellationToken) | ||
{ | ||
TryCatch<TPage> result; | ||
if (this.currentIndex < this.bufferedPages.Count) | ||
{ | ||
result = TryCatch<TPage>.FromResult(this.bufferedPages[this.currentIndex]); | ||
} | ||
else if (this.currentIndex == this.bufferedPages.Count && this.exception != null) | ||
{ | ||
result = TryCatch<TPage>.FromException(this.exception); | ||
} | ||
else | ||
{ | ||
await this.enumerator.MoveNextAsync(trace); | ||
result = this.enumerator.Current; | ||
} | ||
|
||
++this.currentIndex; | ||
return result; | ||
} | ||
|
||
public override void SetCancellationToken(CancellationToken cancellationToken) | ||
{ | ||
base.SetCancellationToken(cancellationToken); | ||
this.enumerator.SetCancellationToken(cancellationToken); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// ------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// ------------------------------------------------------------ | ||
namespace Microsoft.Azure.Cosmos.Pagination | ||
{ | ||
internal enum PrefetchPolicy | ||
{ | ||
PrefetchSinglePage, | ||
PrefetchAll | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.