-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathGrpcDurableEntityClient.cs
More file actions
264 lines (235 loc) · 10.5 KB
/
GrpcDurableEntityClient.cs
File metadata and controls
264 lines (235 loc) · 10.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Diagnostics;
using Microsoft.DurableTask.Client.Entities;
using Microsoft.DurableTask.Entities;
using Microsoft.Extensions.Logging;
using static Microsoft.DurableTask.Protobuf.TaskHubSidecarService;
using DTCore = DurableTask.Core;
using P = Microsoft.DurableTask.Protobuf;
namespace Microsoft.DurableTask.Client.Grpc;
/// <summary>
/// The client for entities.
/// </summary>
class GrpcDurableEntityClient : DurableEntityClient
{
readonly TaskHubSidecarServiceClient sidecarClient;
readonly DataConverter dataConverter;
readonly ILogger logger;
/// <summary>
/// Initializes a new instance of the <see cref="GrpcDurableEntityClient"/> class.
/// </summary>
/// <param name="name">The name of the client.</param>
/// <param name="dataConverter">The data converter.</param>
/// <param name="sidecarClient">The client for the GRPC connection to the sidecar.</param>
/// <param name="logger">The logger for logging client requests.</param>
public GrpcDurableEntityClient(
string name, DataConverter dataConverter, TaskHubSidecarServiceClient sidecarClient, ILogger logger)
: base(name)
{
this.dataConverter = dataConverter;
this.sidecarClient = sidecarClient;
this.logger = logger;
}
/// <inheritdoc/>
public override async Task SignalEntityAsync(
EntityInstanceId id,
string operationName,
object? input = null,
SignalEntityOptions? options = null,
CancellationToken cancellation = default)
{
Check.NotNullOrEmpty(id.Name);
Check.NotNull(id.Key);
Guid requestId = Guid.NewGuid();
DateTimeOffset? scheduledTime = options?.SignalTime;
P.SignalEntityRequest request = new()
{
InstanceId = id.ToString(),
RequestId = requestId.ToString(),
Name = operationName,
Input = this.dataConverter.Serialize(input),
ScheduledTime = scheduledTime?.ToTimestamp(),
RequestTime = DateTimeOffset.UtcNow.ToTimestamp(),
};
if (Activity.Current is { } activity)
{
request.ParentTraceContext ??= new P.TraceContext();
request.ParentTraceContext.TraceParent = activity.Id;
request.ParentTraceContext.TraceState = activity.TraceStateString;
}
this.logger.SignalingEntity(id.ToString(), operationName);
try
{
await this.sidecarClient.SignalEntityAsync(request, cancellationToken: cancellation);
}
catch (RpcException e) when (e.StatusCode == StatusCode.Cancelled)
{
throw new OperationCanceledException(
$"The {nameof(this.SignalEntityAsync)} operation was canceled.", e, cancellation);
}
}
/// <inheritdoc/>
public override Task<EntityMetadata?> GetEntityAsync(
EntityInstanceId id, bool includeState = false, CancellationToken cancellation = default)
=> this.GetEntityCoreAsync(id, includeState, (e, s) => this.ToEntityMetadata(e, s), cancellation);
/// <inheritdoc/>
public override Task<EntityMetadata<TState>?> GetEntityAsync<TState>(
EntityInstanceId id, bool includeState = false, CancellationToken cancellation = default)
=> this.GetEntityCoreAsync(id, includeState, (e, s) => this.ToEntityMetadata<TState>(e, s), cancellation);
/// <inheritdoc/>
public override AsyncPageable<EntityMetadata> GetAllEntitiesAsync(EntityQuery? filter = null)
=> this.GetAllEntitiesCoreAsync(filter, (x, s) => this.ToEntityMetadata(x, s));
/// <inheritdoc/>
public override AsyncPageable<EntityMetadata<TState>> GetAllEntitiesAsync<TState>(EntityQuery? filter = null)
=> this.GetAllEntitiesCoreAsync(filter, (x, s) => this.ToEntityMetadata<TState>(x, s));
/// <inheritdoc/>
public override async Task<CleanEntityStorageResult> CleanEntityStorageAsync(
CleanEntityStorageRequest? request = null,
bool continueUntilComplete = true,
CancellationToken cancellation = default)
{
CleanEntityStorageRequest req = request ?? CleanEntityStorageRequest.Default;
string? continuationToken = req.ContinuationToken;
int emptyEntitiesRemoved = 0;
int orphanedLocksReleased = 0;
this.logger.CleaningEntityStorage();
try
{
do
{
P.CleanEntityStorageResponse response = await this.sidecarClient.CleanEntityStorageAsync(
new P.CleanEntityStorageRequest
{
RemoveEmptyEntities = req.RemoveEmptyEntities,
ReleaseOrphanedLocks = req.ReleaseOrphanedLocks,
ContinuationToken = continuationToken,
},
cancellationToken: cancellation);
continuationToken = response.ContinuationToken;
emptyEntitiesRemoved += response.EmptyEntitiesRemoved;
orphanedLocksReleased += response.OrphanedLocksReleased;
}
while (continueUntilComplete && continuationToken != null);
return new CleanEntityStorageResult
{
ContinuationToken = continuationToken,
EmptyEntitiesRemoved = emptyEntitiesRemoved,
OrphanedLocksReleased = orphanedLocksReleased,
};
}
catch (RpcException e) when (e.StatusCode == StatusCode.Cancelled)
{
throw new OperationCanceledException(
$"The {nameof(this.CleanEntityStorageAsync)} operation was canceled.", e, cancellation);
}
}
async Task<TMetadata?> GetEntityCoreAsync<TMetadata>(
EntityInstanceId id,
bool includeState,
Func<P.EntityMetadata, bool, TMetadata> select,
CancellationToken cancellation)
where TMetadata : class
{
Check.NotNullOrEmpty(id.Name);
Check.NotNull(id.Key);
P.GetEntityRequest request = new()
{
InstanceId = id.ToString(),
IncludeState = includeState,
};
this.logger.GettingEntity(id.ToString());
try
{
P.GetEntityResponse response = await this.sidecarClient
.GetEntityAsync(request, cancellationToken: cancellation);
return response.Exists ? select(response.Entity, includeState) : null;
}
catch (RpcException e) when (e.StatusCode == StatusCode.Cancelled)
{
throw new OperationCanceledException(
$"The {nameof(this.GetEntityAsync)} operation was canceled.", e, cancellation);
}
}
AsyncPageable<TMetadata> GetAllEntitiesCoreAsync<TMetadata>(
EntityQuery? filter, Func<P.EntityMetadata, bool, TMetadata> select)
where TMetadata : class
{
bool includeState = filter?.IncludeState ?? true;
bool includeTransient = filter?.IncludeTransient ?? false;
string startsWith = filter?.InstanceIdStartsWith ?? string.Empty;
DateTimeOffset? lastModifiedFrom = filter?.LastModifiedFrom;
DateTimeOffset? lastModifiedTo = filter?.LastModifiedTo;
this.logger.QueryingEntities(startsWith, lastModifiedFrom, lastModifiedTo);
return Pageable.Create(async (continuation, pageSize, cancellation) =>
{
pageSize ??= filter?.PageSize;
try
{
P.QueryEntitiesResponse response = await this.sidecarClient.QueryEntitiesAsync(
new P.QueryEntitiesRequest
{
Query = new P.EntityQuery
{
InstanceIdStartsWith = startsWith,
LastModifiedFrom = lastModifiedFrom?.ToTimestamp(),
LastModifiedTo = lastModifiedTo?.ToTimestamp(),
IncludeState = includeState,
IncludeTransient = includeTransient,
PageSize = pageSize,
ContinuationToken = continuation ?? filter?.ContinuationToken,
},
},
cancellationToken: cancellation);
IReadOnlyList<TMetadata> values = response.Entities
.Select(x => select(x, includeState))
.ToList();
return new Page<TMetadata>(values, response.ContinuationToken);
}
catch (RpcException e) when (e.StatusCode == StatusCode.Cancelled)
{
throw new OperationCanceledException(
$"The {nameof(this.GetAllEntitiesAsync)} operation was canceled.", e, cancellation);
}
});
}
EntityMetadata ToEntityMetadata(P.EntityMetadata metadata, bool includeState)
{
var coreEntityId = DTCore.Entities.EntityId.FromString(metadata.InstanceId);
EntityInstanceId entityId = new(coreEntityId.Name, coreEntityId.Key);
bool hasState = metadata.SerializedState != null;
SerializedData? data = (includeState && hasState) ? new(metadata.SerializedState!, this.dataConverter) : null;
return new EntityMetadata(entityId, data)
{
LastModifiedTime = metadata.LastModifiedTime.ToDateTimeOffset(),
BacklogQueueSize = metadata.BacklogQueueSize,
LockedBy = metadata.LockedBy,
};
}
EntityMetadata<T> ToEntityMetadata<T>(P.EntityMetadata metadata, bool includeState)
{
var coreEntityId = DTCore.Entities.EntityId.FromString(metadata.InstanceId);
EntityInstanceId entityId = new(coreEntityId.Name, coreEntityId.Key);
DateTimeOffset lastModified = metadata.LastModifiedTime.ToDateTimeOffset();
bool hasState = metadata.SerializedState != null;
if (includeState && hasState)
{
T? data = includeState ? this.dataConverter.Deserialize<T>(metadata.SerializedState) : default;
return new EntityMetadata<T>(entityId, data)
{
LastModifiedTime = lastModified,
BacklogQueueSize = metadata.BacklogQueueSize,
LockedBy = metadata.LockedBy,
};
}
else
{
return new EntityMetadata<T>(entityId)
{
LastModifiedTime = lastModified,
BacklogQueueSize = metadata.BacklogQueueSize,
LockedBy = metadata.LockedBy,
};
}
}
}