Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: async results #275

Merged
merged 2 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
225 changes: 126 additions & 99 deletions Client/src/Unified/Services/Submitter/Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public Service(Properties properties,
}));


HandlerResponse = Task.Run(ResultTask,
HandlerResponse = Task.Run(() => ResultTask(CancellationResultTaskSource.Token),
CancellationResultTaskSource.Token);
}

Expand Down Expand Up @@ -222,13 +222,16 @@ public override void Dispose()
try
{
awaitable.WaitSync();
awaitable.Dispose();
}
catch (OperationCanceledException)
{
}

awaitable.Dispose();
}

CancellationResultTaskSource.Dispose();

GC.SuppressFinalize(this);
}

Expand Down Expand Up @@ -451,10 +454,12 @@ private ServiceResult Execute(string methodName,
/// <param name="responseHandler">The action to take when a response is received.</param>
/// <param name="errorHandler">The action to take when an error occurs.</param>
/// <param name="chunkResultSize">The size of the chunk to retrieve results in.</param>
private void ProxyTryGetResults(IEnumerable<string> taskIds,
Action<string, byte[]> responseHandler,
Action<string, TaskStatus, string> errorHandler,
int chunkResultSize = 200)
/// <param name="cancellationToken"></param>
private async Task ProxyTryGetResults(IEnumerable<string> taskIds,
Action<string, byte[]> responseHandler,
Action<string, TaskStatus, string> errorHandler,
int chunkResultSize = 200,
CancellationToken cancellationToken = default)
{
var missing = new HashSet<string>(taskIds);
var holdPrev = missing.Count;
Expand All @@ -474,38 +479,46 @@ private void ProxyTryGetResults(IEnumerable<string> taskIds,
foreach (var bucket in missing.ToList()
.ToChunks(chunkResultSize))
{
var resultStatusCollection = SessionService.GetResultStatus(bucket);
var resultStatusCollection = await SessionService.GetResultStatusAsync(bucket,
cancellationToken)
.ConfigureAwait(false);

foreach (var resultStatusData in resultStatusCollection.IdsReady)
{
cancellationToken.ThrowIfCancellationRequested();
try
{
Logger.LogTrace("Response handler for {taskId}",
resultStatusData.TaskId);
responseHandler(resultStatusData.TaskId,
Retry.WhileException(5,
2000,
retry =>
{
if (retry > 1)
{
Logger.LogWarning("Try {try} for {funcName}",
retry,
nameof(SessionService.TryGetResultAsync));
}

return SessionService.TryGetResultAsync(new ResultRequest
{
ResultId = resultStatusData.ResultId,
Session = SessionId,
},
CancellationToken.None)
.Result;
},
true,
Logger,
typeof(IOException),
typeof(RpcException))!);
await Retry.WhileException(5,
2000,
retry =>
{
if (retry > 1)
{
Logger.LogWarning("Try {try} for {funcName}",
retry,
nameof(SessionService.TryGetResultAsync));
}

return SessionService.TryGetResultAsync(new ResultRequest
dbrasseur-aneo marked this conversation as resolved.
Show resolved Hide resolved
{
ResultId = resultStatusData.ResultId,
Session = SessionId,
},
cancellationToken);
},
true,
Logger,
cancellationToken,
typeof(IOException),
typeof(RpcException))
.ConfigureAwait(false)!);
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
throw;
}
catch (Exception e)
{
Expand Down Expand Up @@ -533,7 +546,9 @@ private void ProxyTryGetResults(IEnumerable<string> taskIds,
{
string details;

var taskStatus = SessionService.GetTaskStatus(resultStatusData.TaskId);
var taskStatus = await SessionService.GetTaskStatusAsync(resultStatusData.TaskId,
cancellationToken)
.ConfigureAwait(false);

switch (taskStatus)
{
Expand All @@ -542,7 +557,9 @@ private void ProxyTryGetResults(IEnumerable<string> taskIds,
details = $"Task {resultStatusData.TaskId} was canceled";
break;
default:
var outputInfo = SessionService.GetTaskOutputInfo(resultStatusData.TaskId);
var outputInfo = await SessionService.GetTaskOutputInfoAsync(resultStatusData.TaskId,
cancellationToken)
.ConfigureAwait(false);
details = outputInfo.TypeCase == Output.TypeOneofCase.Error
? outputInfo.Error.Details
: "Result is in status : " + resultStatusData.Status + ", look for task in error in logs.";
Expand Down Expand Up @@ -572,6 +589,7 @@ private void ProxyTryGetResults(IEnumerable<string> taskIds,

foreach (var resultStatusData in resultStatusCollection.Canceled)
{
cancellationToken.ThrowIfCancellationRequested();
try
{
errorHandler(resultStatusData.TaskId,
Expand Down Expand Up @@ -605,89 +623,98 @@ private void ProxyTryGetResults(IEnumerable<string> taskIds,

holdPrev = missing.Count;

Thread.Sleep(waitInSeconds[idx]);
await Task.Delay(waitInSeconds[idx],
cancellationToken)
.ConfigureAwait(false);
}
}
}

private void ResultTask()
private async Task ResultTask(CancellationToken cancellationToken)
{
while (!(CancellationResultTaskSource.Token.IsCancellationRequested && ResultHandlerDictionary.IsEmpty))
{
try
{
if (!ResultHandlerDictionary.IsEmpty)
{
ProxyTryGetResults(ResultHandlerDictionary.Keys.ToList(),
(taskId,
byteResult) =>
{
try
{
var result = ProtoSerializer.Deserialize<object?[]>(byteResult);
ResultHandlerDictionary[taskId]
.HandleResponse(result![0],
taskId);
}
catch (Exception e)
{
const ArmonikStatusCode statusCode = ArmonikStatusCode.Unknown;

ServiceInvocationException ex;

var ae = e as AggregateException;

if (ae is not null && ae.InnerExceptions.Count > 1)
{
ex = new ServiceInvocationException(ae,
statusCode);
}
else if (ae is not null)
{
ex = new ServiceInvocationException(ae.InnerException ?? ae,
statusCode);
}
else
{
ex = new ServiceInvocationException(e,
statusCode);
}

ResultHandlerDictionary[taskId]
.HandleError(ex,
taskId);
}
finally
{
ResultHandlerDictionary.TryRemove(taskId,
out _);
}
},
(taskId,
taskStatus,
ex) =>
{
try
{
var statusCode = taskStatus.ToArmonikStatusCode();

ResultHandlerDictionary[taskId]
.HandleError(new ServiceInvocationException(ex,
statusCode),
taskId);
}
finally
{
ResultHandlerDictionary.TryRemove(taskId,
out _);
}
});
await ProxyTryGetResults(ResultHandlerDictionary.Keys.ToList(),
(taskId,
byteResult) =>
{
try
{
var result = ProtoSerializer.Deserialize<object?[]>(byteResult);
ResultHandlerDictionary[taskId]
.HandleResponse(result![0],
taskId);
}
catch (Exception e)
{
const ArmonikStatusCode statusCode = ArmonikStatusCode.Unknown;

ServiceInvocationException ex;

var ae = e as AggregateException;

if (ae is not null && ae.InnerExceptions.Count > 1)
{
ex = new ServiceInvocationException(ae,
statusCode);
}
else if (ae is not null)
{
ex = new ServiceInvocationException(ae.InnerException ?? ae,
statusCode);
}
else
{
ex = new ServiceInvocationException(e,
statusCode);
}

ResultHandlerDictionary[taskId]
.HandleError(ex,
taskId);
}
finally
{
ResultHandlerDictionary.TryRemove(taskId,
out _);
}
},
(taskId,
taskStatus,
ex) =>
{
try
{
var statusCode = taskStatus.ToArmonikStatusCode();

ResultHandlerDictionary[taskId]
.HandleError(new ServiceInvocationException(ex,
statusCode),
taskId);
}
finally
{
ResultHandlerDictionary.TryRemove(taskId,
out _);
}
},
cancellationToken: cancellationToken);
}
else
{
Thread.Sleep(100);
await Task.Delay(100,
cancellationToken)
.ConfigureAwait(false);
}
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
break;
}
catch (Exception e)
{
Logger.LogError("An error occurred while fetching results: {e}",
Expand Down
Loading
Loading