-
Notifications
You must be signed in to change notification settings - Fork 146
/
GrpcWorkerClient.cs
132 lines (111 loc) · 5.26 KB
/
GrpcWorkerClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Diagnostics.ContractsLight;
using System.Threading;
using System.Threading.Tasks;
using BuildXL.Distribution.Grpc;
using BuildXL.Utilities.Configuration;
using BuildXL.Utilities.Instrumentation.Common;
using BuildXL.Utilities.Core;
using BuildXL.Utilities.Core.Tasks;
using Grpc.Core;
using static BuildXL.Engine.Distribution.Grpc.ClientConnectionManager;
namespace BuildXL.Engine.Distribution.Grpc
{
/// <nodoc/>
internal sealed class GrpcWorkerClient : IWorkerClient
{
private readonly LoggingContext m_loggingContext;
private readonly EventHandler<ConnectionFailureEventArgs> m_onConnectionFailureAsync;
private readonly DistributedInvocationId m_invocationId;
private ClientConnectionManager m_connectionManager;
private Worker.WorkerClient m_client;
private AsyncClientStreamingCall<PipBuildRequest, RpcResponse> m_pipBuildRequestStream;
private readonly CounterCollection<DistributionCounter> m_counters;
public GrpcWorkerClient(LoggingContext loggingContext, DistributedInvocationId invocationId, EventHandler<ConnectionFailureEventArgs> onConnectionFailureAsync, CounterCollection<DistributionCounter> counters)
{
m_loggingContext = loggingContext;
m_onConnectionFailureAsync = onConnectionFailureAsync;
m_invocationId = invocationId;
m_counters = counters;
}
/// <summary>
/// Receives a worker location and starts the connection management to that location
/// </summary>
public void SetWorkerLocation(ServiceLocation serviceLocation)
{
Contract.Assert(m_connectionManager == null, "The worker location can only be set once");
Contract.Assert(serviceLocation != null);
m_connectionManager = new ClientConnectionManager(
m_loggingContext,
serviceLocation.IpAddress,
serviceLocation.Port,
m_invocationId,
m_counters,
async (callOptions) => await m_client.HeartbeatAsync(GrpcUtils.EmptyResponse, callOptions));
m_connectionManager.OnConnectionFailureAsync += m_onConnectionFailureAsync;
#if NET6_0_OR_GREATER
m_client = new Worker.WorkerClient(m_connectionManager.Channel);
#else
m_client = null;
#endif
}
public Task CloseAsync()
{
return m_connectionManager?.CloseAsync();
}
public void Dispose()
{ }
public async Task<RpcCallResult<Unit>> AttachAsync(BuildStartData message, CancellationToken cancellationToken)
{
Contract.Assert(m_connectionManager != null, "The worker location should be known before attaching");
var attachment = await m_connectionManager.CallAsync(
async (callOptions) => await m_client.AttachAsync(message, options: callOptions),
"Attach",
cancellationToken,
waitForConnection: true);
if (attachment.Succeeded)
{
m_connectionManager.OnAttachmentCompleted();
}
return attachment;
}
public Task<RpcCallResult<Unit>> ExecutePipsAsync(PipBuildRequest message, string description, CancellationToken cancellationToken = default)
{
Contract.Assert(m_connectionManager != null, "The worker location should be known if calling ExecutePips");
Func<CallOptions, Task> func;
if (EngineEnvironmentSettings.GrpcStreamingEnabled)
{
if (m_pipBuildRequestStream == null)
{
var headerResult = GrpcUtils.InitializeHeaders(m_invocationId);
m_pipBuildRequestStream = m_client.StreamExecutePips(headers: headerResult.headers, cancellationToken: cancellationToken);
}
func = async (callOptions) => await m_pipBuildRequestStream.RequestStream.WriteAsync(message);
}
else
{
func = async (callOptions) => await m_client.ExecutePipsAsync(message, options: callOptions);
}
return m_connectionManager.CallAsync(func, description);
}
public Task<RpcCallResult<WorkerExitResponse>> ExitAsync(BuildEndData message, CancellationToken cancellationToken = default)
{
Contract.Assert(m_connectionManager != null, "The worker location should be known if calling Exit");
m_connectionManager.ReadyForExit();
return m_connectionManager.CallAsyncWithResult<WorkerExitResponse>(
async (callOptions) => await m_client.ExitAsync(message, options: callOptions),
"Exit",
cancellationToken);
}
public bool TryFinalizeStreaming()
{
if (m_pipBuildRequestStream == null)
{
return true;
}
return m_connectionManager.FinalizeStreamAsync(m_pipBuildRequestStream).GetAwaiter().GetResult().Succeeded;
}
}
}