-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove RX as the primary pipe for gRPC communications (#8281)
### Issue describing the changes in this PR resolves #8280; performance locally is a 5x+ speedup in throughput the main crux of the new flow is described in src/WebJobs.Script.Grpc/Eventing/GrpcEventExtensions.cs, but in short: we establish a per-worker `Channel<InboundGrpcEvent>` and `Channel<OutboundGrpcEvent>` to handle the backlog; instead of publishing to RX, we publish to the writer of the relevant queue. Separately, we have an async worker deque data from the queues, and process accordingly. This is much more direct, avoids a *lot* of RX machinery, and creates isolation between workers. ### Pull request checklist * [ ] My changes **do not** require documentation changes * [ ] Otherwise: Documentation issue linked to PR * [ ] My changes **should not** be added to the release notes for the next release * [ ] Otherwise: I've added my notes to `release_notes.md` * [ ] My changes **do not** need to be backported to a previous version * [ ] Otherwise: Backport tracked by issue/PR #issue_or_pr * [ ] I have added all required tests (Unit tests, E2E tests) <!-- Optional: delete if not applicable --> ### Additional information Additional PR information Co-authored-by: Lilian Kasem <likasem@microsoft.com> Co-authored-by: Marc Gravell <marcgravell@microsoft.com> Co-authored-by: Nick Craver <nickcraver@microsoft.com> Co-authored-by: Nick Craver <nrcraver@gmail.com>
- Loading branch information
1 parent
5d4e651
commit c112509
Showing
18 changed files
with
967 additions
and
411 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Threading.Channels; | ||
using Microsoft.Azure.WebJobs.Script.Eventing; | ||
|
||
namespace Microsoft.Azure.WebJobs.Script.Grpc.Eventing; | ||
|
||
internal static class GrpcEventExtensions | ||
{ | ||
// flow here is: | ||
// 1) external request is proxied to the the GrpcWorkerChannel via one of the many Send* APIs, which writes | ||
// to outbound-writer; this means we can have concurrent writes to outbound | ||
// 2) if an out-of-process function is connected, a FunctionRpcService-EventStream will consume | ||
// from outbound-reader (we'll allow for the multi-stream possibility, hence concurrent), and push it via gRPC | ||
// 3) when the out-of-process function provides a response to FunctionRpcService-EventStream, it is written to | ||
// inbound-writer (note we will allow for multi-stream possibility) | ||
// 4) the GrpcWorkerChannel has a single dedicated consumer of inbound-reader, which it then marries to | ||
// in-flight operations | ||
internal static readonly UnboundedChannelOptions InboundOptions = new UnboundedChannelOptions | ||
{ | ||
SingleReader = true, // see 4 | ||
SingleWriter = false, // see 3 | ||
AllowSynchronousContinuations = false, | ||
}; | ||
|
||
internal static readonly UnboundedChannelOptions OutboundOptions = new UnboundedChannelOptions | ||
{ | ||
SingleReader = false, // see 2 | ||
SingleWriter = false, // see 1 | ||
AllowSynchronousContinuations = false, | ||
}; | ||
|
||
public static void AddGrpcChannels(this IScriptEventManager manager, string workerId) | ||
{ | ||
var inbound = Channel.CreateUnbounded<InboundGrpcEvent>(InboundOptions); | ||
if (manager.TryAddWorkerState(workerId, inbound)) | ||
{ | ||
var outbound = Channel.CreateUnbounded<OutboundGrpcEvent>(OutboundOptions); | ||
if (manager.TryAddWorkerState(workerId, outbound)) | ||
{ | ||
return; // successfully added both | ||
} | ||
// we added the inbound but not the outbound; revert | ||
manager.TryRemoveWorkerState(workerId, out inbound); | ||
} | ||
// this is not anticipated, so don't panic abount the allocs above | ||
throw new ArgumentException("Duplicate worker id: " + workerId, nameof(workerId)); | ||
} | ||
|
||
public static bool TryGetGrpcChannels(this IScriptEventManager manager, string workerId, out Channel<InboundGrpcEvent> inbound, out Channel<OutboundGrpcEvent> outbound) | ||
=> manager.TryGetWorkerState(workerId, out inbound) & manager.TryGetWorkerState(workerId, out outbound); | ||
|
||
public static void RemoveGrpcChannels(this IScriptEventManager manager, string workerId) | ||
{ | ||
// remove any channels, and shut them down | ||
if (manager.TryGetWorkerState<Channel<InboundGrpcEvent>>(workerId, out var inbound)) | ||
{ | ||
inbound.Writer.TryComplete(); | ||
} | ||
if (manager.TryGetWorkerState<Channel<OutboundGrpcEvent>>(workerId, out var outbound)) | ||
{ | ||
outbound.Writer.TryComplete(); | ||
} | ||
} | ||
} |
22 changes: 0 additions & 22 deletions
22
src/WebJobs.Script.Grpc/Extensions/InboundGrpcEventExtensions.cs
This file was deleted.
Oops, something went wrong.
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
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
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.