-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathEntitySqlBackendQueries.cs
More file actions
227 lines (199 loc) · 9.86 KB
/
EntitySqlBackendQueries.cs
File metadata and controls
227 lines (199 loc) · 9.86 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
namespace DurableTask.SqlServer
{
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
using DurableTask.Core;
using DurableTask.Core.Entities;
using System.Collections.Generic;
using System.Diagnostics;
class EntitySqlBackendQueries : EntityBackendQueries
{
readonly SqlOrchestrationService orchestrationService;
static TimeSpan timeLimitForCleanEntityStorageLoop = TimeSpan.FromSeconds(5);
public EntitySqlBackendQueries(
SqlOrchestrationService orchestrationService)
{
this.orchestrationService = orchestrationService;
}
public async override Task<EntityMetadata?> GetEntityAsync(
EntityId id,
bool includeState = false,
bool includeStateless = false,
CancellationToken cancellation = default)
{
OrchestrationState? state = (await this.orchestrationService.GetOrchestrationStateAsync(id.ToString(), allExecutions: false)).FirstOrDefault();
return this.GetEntityMetadata(state, includeStateless, includeState);
}
public async override Task<EntityQueryResult> QueryEntitiesAsync(EntityQuery filter, CancellationToken cancellation)
{
int pageNumber = 0;
if (!string.IsNullOrEmpty(filter.ContinuationToken) && !int.TryParse(filter.ContinuationToken, out pageNumber))
{
throw new ArgumentException($"Invalid continuation token {filter.ContinuationToken}");
}
int retrievedResults = 0;
IEnumerable<OrchestrationState> allResults = Array.Empty<OrchestrationState>();
var stopwatch = new Stopwatch();
stopwatch.Start();
do
{
SqlOrchestrationQuery entityInstancesQuery = new SqlOrchestrationQuery()
{
PageSize = filter.PageSize.GetValueOrDefault(100),
PageNumber = pageNumber,
InstanceIdPrefix = filter.InstanceIdStartsWith,
CreatedTimeFrom = filter.LastModifiedFrom.GetValueOrDefault(DateTime.MinValue),
CreatedTimeTo = filter.LastModifiedTo.GetValueOrDefault(DateTime.MaxValue),
FetchInput = filter.IncludeState,
};
IReadOnlyCollection<OrchestrationState> results = await this.orchestrationService.GetManyOrchestrationsAsync(entityInstancesQuery, cancellation);
allResults = allResults.Concat(results);
pageNumber++;
retrievedResults = results.Count;
if (retrievedResults == 0)
{
pageNumber = -1;
}
} while (retrievedResults > 0 && stopwatch.ElapsedMilliseconds <= 100);
IEnumerable<EntityMetadata> entities = allResults.Select(result => this.GetEntityMetadata(result, filter.IncludeTransient, filter.IncludeState))
.OfType<EntityMetadata>();
return new EntityQueryResult()
{
Results = entities,
ContinuationToken = pageNumber < 0 ? null : pageNumber.ToString()
};
}
public async override Task<CleanEntityStorageResult> CleanEntityStorageAsync(CleanEntityStorageRequest request = default, CancellationToken cancellation = default)
{
DateTime now = DateTime.UtcNow;
int emptyEntitiesRemoved = 0;
int orphanedLocksReleased = 0;
int pageNumber = 0;
if (!string.IsNullOrEmpty(request.ContinuationToken) && !int.TryParse(request.ContinuationToken, out pageNumber))
{
throw new ArgumentException($"Invalid continuation token {request.ContinuationToken}");
}
int retrievedResults = 0;
IEnumerable<OrchestrationState> allResults = Array.Empty<OrchestrationState>();
var stopwatch = new Stopwatch();
stopwatch.Start();
do
{
SqlOrchestrationQuery entityInstancesQuery = new SqlOrchestrationQuery()
{
PageSize = 100,
PageNumber = pageNumber,
InstanceIdPrefix = "@",
CreatedTimeFrom = DateTime.MinValue,
CreatedTimeTo = DateTime.MaxValue,
FetchInput = true,
};
IReadOnlyCollection<OrchestrationState> page = await this.orchestrationService.GetManyOrchestrationsAsync(entityInstancesQuery, cancellation);
pageNumber++;
retrievedResults = page.Count;
if (retrievedResults == 0)
{
pageNumber = -1;
break;
}
var tasks = new List<Task>();
IEnumerable<string> emptyEntityIds = new List<string>();
foreach (OrchestrationState state in page)
{
EntityStatus? status = ClientEntityHelpers.GetEntityStatus(state.Status);
if (status != null)
{
if (request.ReleaseOrphanedLocks && status.LockedBy != null)
{
tasks.Add(CheckForOrphanedLockAndFixIt(state, status.LockedBy));
}
if (request.RemoveEmptyEntities)
{
bool isEmptyEntity = !status.EntityExists && status.LockedBy == null && status.BacklogQueueSize == 0;
bool safeToRemoveWithoutBreakingMessageSorterLogic =
now - state.LastUpdatedTime > this.orchestrationService.EntityBackendProperties.EntityMessageReorderWindow;
if (isEmptyEntity && safeToRemoveWithoutBreakingMessageSorterLogic)
{
emptyEntityIds.Append(state.OrchestrationInstance.InstanceId);
orphanedLocksReleased++;
}
}
}
}
async Task CheckForOrphanedLockAndFixIt(OrchestrationState state, string lockOwner)
{
OrchestrationState? ownerState
= (await this.orchestrationService.GetOrchestrationStateAsync(lockOwner, allExecutions: false)).FirstOrDefault();
bool OrchestrationIsRunning(OrchestrationStatus? status)
=> status != null && (status == OrchestrationStatus.Running || status == OrchestrationStatus.Suspended);
if (!OrchestrationIsRunning(ownerState?.OrchestrationStatus))
{
// the owner is not a running orchestration. Send a lock release.
EntityMessageEvent eventToSend = ClientEntityHelpers.EmitUnlockForOrphanedLock(state.OrchestrationInstance, lockOwner);
await this.orchestrationService.SendTaskOrchestrationMessageAsync(eventToSend.AsTaskMessage());
Interlocked.Increment(ref orphanedLocksReleased);
}
}
await this.orchestrationService.PurgeOrchestrationHistoryAsync(emptyEntityIds);
} while (retrievedResults > 0 && stopwatch.Elapsed <= timeLimitForCleanEntityStorageLoop);
return new CleanEntityStorageResult()
{
EmptyEntitiesRemoved = emptyEntitiesRemoved,
OrphanedLocksReleased = orphanedLocksReleased,
ContinuationToken = pageNumber < 0 ? null : pageNumber.ToString()
};
}
EntityMetadata? GetEntityMetadata(OrchestrationState? state, bool includeTransient, bool includeState)
{
if (state == null)
{
return null;
}
if (!includeState)
{
if (!includeTransient)
{
// it is possible that this entity was logically deleted even though its orchestration was not purged yet.
// we can check this efficiently (i.e. without deserializing anything) by looking at just the custom status
if (!EntityStatus.TestEntityExists(state.Status))
{
return null;
}
}
EntityStatus? status = ClientEntityHelpers.GetEntityStatus(state.Status);
return new EntityMetadata()
{
EntityId = EntityId.FromString(state.OrchestrationInstance.InstanceId),
LastModifiedTime = state.CreatedTime,
BacklogQueueSize = status?.BacklogQueueSize ?? 0,
LockedBy = status?.LockedBy,
SerializedState = null, // we were instructed to not include the state
};
}
else
{
// return the result to the user
if (!includeTransient && state.Input == null)
{
return null;
}
else
{
EntityStatus? status = ClientEntityHelpers.GetEntityStatus(state.Status);
return new EntityMetadata()
{
EntityId = EntityId.FromString(state.OrchestrationInstance.InstanceId),
LastModifiedTime = state.CreatedTime,
BacklogQueueSize = status?.BacklogQueueSize ?? 0,
LockedBy = status?.LockedBy,
SerializedState = state.Input,
};
}
}
}
}
}