-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnalyticsPerformerService.cs
More file actions
238 lines (206 loc) · 9.5 KB
/
AnalyticsPerformerService.cs
File metadata and controls
238 lines (206 loc) · 9.5 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
using System.Collections.Concurrent;
using Couchbase.Analytics.Performer.Internal.Connections;
using Couchbase.Analytics.Performer.Internal.Exceptions;
using Couchbase.Analytics.Performer.Internal.Modes;
using Couchbase.Analytics.Performer.Internal.Utils;
using Couchbase.AnalyticsClient;
using Couchbase.Core.Utils;
using Couchbase.Grpc.Protocol.Columnar;
using Couchbase.Grpc.Protocol.Shared;
using Google.Protobuf.WellKnownTypes;
using Grpc.Core;
using Exception = System.Exception;
namespace Couchbase.Analytics.Performer.Internal.Services;
internal class AnalyticsPerformerService : ColumnarService.ColumnarServiceBase
{
public AnalyticsPerformerService(ConcurrentDictionary<string, ClusterConnection> clusters)
{
Clusters = clusters;
}
private ConcurrentDictionary<string, ClusterConnection> Clusters { get; }
public override Task<EmptyResultOrFailureResponse> ClusterNewInstance(ClusterNewInstanceRequest request,
ServerCallContext context)
{
var stopWatch = LightweightStopwatch.StartNew();
var initiated = Timestamp.FromDateTime(DateTime.UtcNow);
Serilog.Log.Information("Creating new cluster instance: {ConnectionString}", request.ConnectionString);
var response = new EmptyResultOrFailureResponse();
try
{
//how to make this work in C#? or does this work?
foreach (var tunable in request.Tunables)
{
Environment.SetEnvironmentVariable(tunable.Key, tunable.Value);
}
var sdkCluster = Clusters.GetOrAdd(request.ClusterConnectionId,
(_) =>
{
Serilog.Log.Information(
"No cached cluster found creating new cluster: {ConnectionString}",
request.ConnectionString);
var cluster = Cluster.Create(request.ConnectionString,
request.ToSdkCredential(), request.ToSdkQueryOptions());
return new(request, cluster);
});
/* context.GetHttpContext().Response.OnCompleted(() =>
{
foreach (var tunable in request.Tunables)
{
Environment.SetEnvironmentVariable(tunable.Key, null);
}
return Task.CompletedTask;
});*/
Serilog.Log.Information(
"Created or using new cluster instance in {Seconds}: {ConnectionString}",
stopWatch.Elapsed, request.ConnectionString);
response.GetResponseMetaData(stopWatch, initiated);
}
catch (Exception ex)
{
response.GetResponseMetaData(stopWatch, initiated, ex);
}
return Task.FromResult(response);
}
public override Task<FetchPerformerCapsResponse> FetchPerformerCaps(FetchPerformerCapsRequest request,
ServerCallContext context)
{
var stopWatch = LightweightStopwatch.StartNew();
Serilog.Log.Information("Calling FetchPerformerCaps");
var response = new FetchPerformerCapsResponse
{
AnalyticsProduct = AnalyticsProduct.Analytics,
Sdk = SDK.Net
};
try
{
response.ClusterNewInstance.Add((int)Mode.PushBasedStreaming, new PerApiElementClusterNewInstance { SupportsDispatchTimeout = true });
response.ClusterNewInstance.Add((int)Mode.Buffered, new PerApiElementClusterNewInstance { SupportsDispatchTimeout = true });
response.ClusterClose.Add((int)Mode.PushBasedStreaming, new PerApiElementClusterClose());
response.ClusterClose.Add((int)Mode.Buffered, new PerApiElementClusterClose());
var executeQueryBuffered = new PerApiElementExecuteQuery
{
ExecuteQueryReturns = PerApiElementExecuteQuery
.Types.ExecuteQueryReturns.QueryResult,
RowIteration = PerApiElementExecuteQuery
.Types.RowIteration.Buffered,
RowDeserialization = PerApiElementExecuteQuery
.Types.RowDeserialization.StaticRowTypingIndividual,
SupportsCustomDeserializer = false
};
var executeQueryStreaming = new PerApiElementExecuteQuery
{
ExecuteQueryReturns = PerApiElementExecuteQuery.Types
.ExecuteQueryReturns.QueryMetadata,
RowIteration = PerApiElementExecuteQuery.Types.RowIteration
.StreamingPushBased,
RowDeserialization = PerApiElementExecuteQuery.Types
.RowDeserialization.StaticRowTypingIndividual,
SupportsCustomDeserializer = false
};
response.ClusterExecuteQuery.Add((int)Mode.Buffered,
executeQueryBuffered);
response.ClusterExecuteQuery.Add((int)Mode.PushBasedStreaming,
executeQueryStreaming);
response.ScopeExecuteQuery.Add((int)Mode.Buffered,
executeQueryBuffered);
response.ScopeExecuteQuery.Add((int)Mode.PushBasedStreaming,
executeQueryStreaming);
response.SdkConnectionError.Add((int)Mode.Buffered,
new SdkConnectionError
{
InvalidCredErrorType = SdkConnectionError.Types
.InvalidCredentialErrorType
.AsInvalidCredentialException,
BootstrapErrorType = SdkConnectionError.Types
.BootstrapErrorType
.AsColumnarError
});
response.SdkConnectionError.Add((int)Mode.PushBasedStreaming,
new SdkConnectionError
{
InvalidCredErrorType = SdkConnectionError.Types
.InvalidCredentialErrorType
.AsInvalidCredentialException,
BootstrapErrorType = SdkConnectionError.Types
.BootstrapErrorType
.AsColumnarError
});
Serilog.Log.Information(
"FetchPerformerCaps fetched {Seconds} seconds",
stopWatch.Elapsed.TotalMilliseconds);
}
catch (Exception exception)
{
var statusCode = StatusCode.Aborted;
if (exception is GrpcUnimplementedException)
{
statusCode = StatusCode.Unimplemented;
}
response.SdkConnectionError.Add((int)statusCode, new SdkConnectionError());// ¯\_(ツ)_/¯
}
//java has observer.onNext and observer.onCompleted here
//C# has context.GetHttpContext().Response.OnCompleted and context.GetHttpContext().Response.OnStarting
//Do we have to provide a func here for each or just use the defaults?
return Task.FromResult(response);
}
public override Task<EmptyResultOrFailureResponse> ClusterClose(ClusterCloseRequest request, ServerCallContext context)
{
Serilog.Log.Information("Calling ClusterClose for {ClusterId}", request.ExecutionContext.ClusterId);
var stopWatch = LightweightStopwatch.StartNew();
var initiated = Timestamp.FromDateTime(DateTime.UtcNow);
var response = new EmptyResultOrFailureResponse();
try
{
if (Clusters.TryRemove(request.ExecutionContext.ClusterId, out var connection))
{
connection.Dispose();
}
response.GetResponseMetaData(stopWatch, initiated);
}
catch (Exception ex)
{
response.GetResponseMetaData(stopWatch, initiated, ex);
Serilog.Log.Error(ex, "Could not close cluster {ConnectionId}", request.ExecutionContext.ClusterId);
}
Serilog.Log.Information("Closing cluster {ClusterId} in {Milliseconds}ms", request.ExecutionContext.ClusterId, stopWatch.Elapsed.TotalMilliseconds);
return Task.FromResult(response);
}
public override Task<EmptyResultOrFailureResponse> CloseAllClusters(CloseAllColumnarClustersRequest request,
ServerCallContext context)
{
Serilog.Log.Information("Calling CloseAllClusters");
var stopWatch = LightweightStopwatch.StartNew();
var initiated = Timestamp.FromDateTime(DateTime.UtcNow);
var response = new EmptyResultOrFailureResponse();
var connectionIds = Clusters.Keys;
try
{
foreach (var connection in connectionIds)
{
if (Clusters.TryRemove(connection, out var sdkCluster))
{
Serilog.Log.Information("Closing cluster {ConnectionId}",
connection);
sdkCluster.Dispose();
}
else
{
Serilog.Log.Information(
"Failed to remove cluster {ConnectionId}", connection);
}
}
response.GetResponseMetaData(stopWatch, initiated);
}
catch (Exception ex)
{
response.GetResponseMetaData(stopWatch, initiated, ex);
}
Serilog.Log.Information("Closed all clusters in {Milliseconds}ms", stopWatch.Elapsed.TotalMilliseconds);
return Task.FromResult(response);
}
public override Task<EchoResponse> Echo(EchoRequest request, ServerCallContext context)
{
Serilog.Log.Information("Calling Echo - {TestName} | {Message}", request.TestName, request.Message);
return Task.FromResult(new EchoResponse());
}
}