-
Notifications
You must be signed in to change notification settings - Fork 146
/
GrpcOrchestratorClient.cs
174 lines (148 loc) · 7.02 KB
/
GrpcOrchestratorClient.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Diagnostics.ContractsLight;
using System.Linq;
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;
using BuildXL.Cache.ContentStore.Interfaces.Results;
using static BuildXL.Distribution.Grpc.HelloResponse.Types;
namespace BuildXL.Engine.Distribution.Grpc
{
/// <nodoc/>
internal sealed class GrpcOrchestratorClient : IOrchestratorClient
{
private readonly DistributedInvocationId m_invocationId;
private Orchestrator.OrchestratorClient m_client;
private ClientConnectionManager m_connectionManager;
private readonly LoggingContext m_loggingContext;
private volatile bool m_initialized;
private AsyncClientStreamingCall<ExecutionLogInfo, RpcResponse> m_executionLogStream;
private AsyncClientStreamingCall<PipResultsInfo, RpcResponse> m_pipResultsStream;
private readonly CounterCollection<DistributionCounter> m_counters;
public GrpcOrchestratorClient(LoggingContext loggingContext, DistributedInvocationId invocationId, CounterCollection<DistributionCounter> counters)
{
m_invocationId = invocationId;
m_loggingContext = loggingContext;
m_counters = counters;
}
public async Task<Possible<HelloResponseType>> SayHelloAsync(HelloRequest helloRequest, CancellationToken cancellationToken = default)
{
HelloResponseType result = HelloResponseType.Ok; // Need to initialize to please the compiler
var callResult = await m_connectionManager.CallAsync(
async (callOptions) =>
{
result = (await m_client.HelloAsync(helloRequest, options: callOptions)).Message;
},
"Hello",
cancellationToken: cancellationToken,
waitForConnection: true);
if (!callResult.Succeeded)
{
return new Failure<string>("Communication error: Hello call could not be sent to the orchestrator.");
}
return result;
}
public void Initialize(string ipAddress,
int port,
EventHandler<ConnectionFailureEventArgs> onConnectionFailureAsync)
{
m_connectionManager = new ClientConnectionManager(
m_loggingContext,
ipAddress,
port,
m_invocationId,
m_counters,
async (callOptions) => await m_client.HeartbeatAsync(GrpcUtils.EmptyResponse, callOptions));
m_connectionManager.OnConnectionFailureAsync += onConnectionFailureAsync;
#if NET6_0_OR_GREATER
m_client = new Orchestrator.OrchestratorClient(m_connectionManager.Channel);
#else
m_client = null;
#endif
m_initialized = true;
}
public Task CloseAsync()
{
if (!m_initialized)
{
return Task.CompletedTask;
}
return m_connectionManager.CloseAsync();
}
public async Task<RpcCallResult<Unit>> AttachCompletedAsync(AttachCompletionInfo message, CancellationToken cancellationToken = default)
{
Contract.Assert(m_initialized);
var attachmentCompletion = await m_connectionManager.CallAsync(
async (callOptions) => await m_client.AttachCompletedAsync(message, options: callOptions),
"AttachCompleted",
cancellationToken: cancellationToken,
waitForConnection: true);
if (attachmentCompletion.Succeeded)
{
m_connectionManager.OnAttachmentCompleted();
}
return attachmentCompletion;
}
public Task<RpcCallResult<Unit>> ReportPipResultsAsync(PipResultsInfo message, string description, CancellationToken cancellationToken = default)
{
Contract.Assert(m_initialized);
Func<CallOptions, Task> func;
if (EngineEnvironmentSettings.GrpcStreamingEnabled)
{
if (m_pipResultsStream == null)
{
var headerResult = GrpcUtils.InitializeHeaders(m_invocationId);
m_pipResultsStream = m_client.StreamPipResults(headers: headerResult.headers, cancellationToken: cancellationToken);
}
func = async (callOptions) => await m_pipResultsStream.RequestStream.WriteAsync(message);
}
else
{
func = async (callOptions) => await m_client.ReportPipResultsAsync(message, options: callOptions);
}
return m_connectionManager.CallAsync(func, description, cancellationToken: cancellationToken);
}
public Task<RpcCallResult<Unit>> ReportExecutionLogAsync(ExecutionLogInfo message, CancellationToken cancellationToken = default)
{
Contract.Assert(m_initialized);
Func<CallOptions, Task> func;
if (EngineEnvironmentSettings.GrpcStreamingEnabled)
{
if (m_executionLogStream == null)
{
var headerResult = GrpcUtils.InitializeHeaders(m_invocationId);
m_executionLogStream = m_client.StreamExecutionLog(headers: headerResult.headers, cancellationToken: cancellationToken);
}
func = async (callOptions) => await m_executionLogStream.RequestStream.WriteAsync(message);
}
else
{
func = async (callOptions) => await m_client.ReportExecutionLogAsync(message, options: callOptions);
}
return m_connectionManager.CallAsync(func, $" ReportExecutionLog: Size={message.Events.DataBlob.Count()}, SequenceNumber={message.Events.SequenceNumber}", cancellationToken: cancellationToken);
}
public bool TryFinalizeStreaming()
{
RpcCallResult<Unit> executionLogResponse = new RpcCallResult<Unit>();
RpcCallResult<Unit> pipResultsResponse = new RpcCallResult<Unit>();
if (m_executionLogStream != null)
{
executionLogResponse = m_connectionManager.FinalizeStreamAsync(m_executionLogStream).GetAwaiter().GetResult();
}
if (m_pipResultsStream != null)
{
pipResultsResponse = m_connectionManager.FinalizeStreamAsync(m_pipResultsStream).GetAwaiter().GetResult();
}
return executionLogResponse.Succeeded && pipResultsResponse.Succeeded;
}
}
}