Skip to content

Commit 044f780

Browse files
author
Sophia Tevosyan
committed
added a wrapper ExtendedSessionsCache object
1 parent 2162650 commit 044f780

File tree

2 files changed

+40
-19
lines changed

2 files changed

+40
-19
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using Microsoft.Extensions.Caching.Memory;
5+
6+
namespace Microsoft.DurableTask.Worker.Grpc;
7+
8+
public class ExtendedSessionsCache
9+
{
10+
private IMemoryCache? extendedSessions;
11+
12+
internal IMemoryCache GetOrInitializeCache(double extendedSessionIdleTimeoutInSeconds)
13+
{
14+
this.extendedSessions ??= new MemoryCache(new MemoryCacheOptions
15+
{
16+
ExpirationScanFrequency = TimeSpan.FromSeconds(extendedSessionIdleTimeoutInSeconds),
17+
});
18+
19+
return this.extendedSessions;
20+
}
21+
}

src/Worker/Grpc/GrpcOrchestrationRunner.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public static string LoadAndRun(
136136
/// <param name="implementation">
137137
/// An <see cref="ITaskOrchestrator"/> implementation that defines the orchestrator logic.
138138
/// </param>
139-
/// <param name="extendedSessions">
139+
/// <param name="extendedSessionsCache">
140140
/// The cache of extended sessions which can be used to retrieve the <see cref="ExtendedSessionState"/> if this orchestration request is from within an extended session.
141141
/// </param>
142142
/// <param name="services">
@@ -154,7 +154,7 @@ public static string LoadAndRun(
154154
public static string LoadAndRun(
155155
string encodedOrchestratorRequest,
156156
ITaskOrchestrator implementation,
157-
IMemoryCache extendedSessions,
157+
ExtendedSessionsCache extendedSessionsCache,
158158
IServiceProvider? services = null)
159159
{
160160
Check.NotNullOrEmpty(encodedOrchestratorRequest);
@@ -171,9 +171,20 @@ public static string LoadAndRun(
171171

172172
OrchestratorExecutionResult? result = null;
173173
bool addToExtendedSessions = false;
174-
bool requiresHistory = false;
175-
if (properties.TryGetValue("ExtendedSession", out object? isExtendedSession) && (bool)isExtendedSession!)
176-
{
174+
bool requiresHistory = false;
175+
double extendedSessionIdleTimeoutInSeconds = 0;
176+
IMemoryCache? extendedSessions = null;
177+
178+
if (properties.TryGetValue("ExtendedSession", out object? isExtendedSessionObj)
179+
&& isExtendedSessionObj is bool isExtendedSession
180+
&& isExtendedSession
181+
&& properties.TryGetValue("ExtendedSessionIdleTimeoutInSeconds", out object? extendedSessionIdleTimeoutObj)
182+
&& extendedSessionIdleTimeoutObj is double extendedSessionIdleTimeout
183+
&& extendedSessionIdleTimeout >= 0)
184+
{
185+
extendedSessionIdleTimeoutInSeconds = extendedSessionIdleTimeout;
186+
extendedSessions = extendedSessionsCache.GetOrInitializeCache(extendedSessionIdleTimeoutInSeconds);
187+
177188
if (extendedSessions.TryGetValue(request.InstanceId, out ExtendedSessionState? extendedSessionState))
178189
{
179190
OrchestrationRuntimeState runtimeState = extendedSessionState!.RuntimeState;
@@ -192,22 +203,11 @@ public static string LoadAndRun(
192203
else
193204
{
194205
addToExtendedSessions = true;
195-
}
206+
}
196207
}
197208

198209
if (result == null)
199210
{
200-
double extendedSessionIdleTimeoutInSeconds = 0;
201-
if (properties.TryGetValue("ExtendedSessionIdleTimeoutInSeconds", out object? extendedSessionIdleTimeout)
202-
&& (double)extendedSessionIdleTimeout! >= 0)
203-
{
204-
extendedSessionIdleTimeoutInSeconds = (double)extendedSessionIdleTimeout!;
205-
}
206-
else
207-
{
208-
addToExtendedSessions = false;
209-
}
210-
211211
// If this is the first orchestration execution, then the past events count will be 0 but includePastEvents will be true (there are just none to include).
212212
// Otherwise, there is an orchestration history but DurableTask.Core did not attach it since the extended session is still active on its end, but we have since evicted the
213213
// session and lost the orchestration history so we cannot replay the orchestration.
@@ -240,14 +240,14 @@ public static string LoadAndRun(
240240

241241
if (addToExtendedSessions && !executor.IsCompleted)
242242
{
243-
extendedSessions.Set<ExtendedSessionState>(
243+
extendedSessions!.Set<ExtendedSessionState>(
244244
request.InstanceId,
245245
new(runtimeState, shim, executor),
246246
new MemoryCacheEntryOptions { SlidingExpiration = TimeSpan.FromSeconds(extendedSessionIdleTimeoutInSeconds) });
247247
}
248248
else
249249
{
250-
extendedSessions.Remove(request.InstanceId);
250+
extendedSessions?.Remove(request.InstanceId);
251251
}
252252
}
253253
}

0 commit comments

Comments
 (0)