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

fix: resubmit task when agent tries to acquire a task in Retried #762

Merged
merged 4 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion Adaptors/Memory/src/PushQueueStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private Task PushMessagesAsync(IEnumerable<MessageData> messages,
if (!id2Handlers_.TryAdd(messageHandler.TaskId!,
messageHandler))
{
throw new InvalidOperationException("Duplicate messageId found.");
throw new InvalidOperationException($"Duplicate messageId: {messageHandler.TaskId} found.");
}
}

Expand Down
2 changes: 1 addition & 1 deletion Adaptors/Memory/src/TaskTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public Task CreateTasks(IEnumerable<TaskData> tasks,
if (!taskId2TaskData_.TryAdd(taskData.TaskId,
taskData))
{
throw new ArmoniKException($"Tasks '{taskData.TaskId}' already exists");
throw new TaskAlreadyExistsException($"Tasks '{taskData.TaskId}' already exists");
}

var session = session2TaskIds_.GetOrAdd(taskData.SessionId,
Expand Down
14 changes: 11 additions & 3 deletions Adaptors/MongoDB/src/TaskTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,17 @@ public async Task CreateTasks(IEnumerable<TaskData> tasks,

var taskCollection = taskCollectionProvider_.Get();

await taskCollection.InsertManyAsync(tasks.Select(taskData => taskData),
cancellationToken: cancellationToken)
.ConfigureAwait(false);
try
{
await taskCollection.InsertManyAsync(tasks.Select(taskData => taskData),
cancellationToken: cancellationToken)
.ConfigureAwait(false);
}
catch (MongoBulkWriteException<TaskData> e)
aneojgurhem marked this conversation as resolved.
Show resolved Hide resolved
{
throw new TaskAlreadyExistsException("Task already exists",
e);
}
}

/// <inheritdoc />
Expand Down
40 changes: 40 additions & 0 deletions Common/src/Exceptions/TaskAlreadyExistsException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// This file is part of the ArmoniK project
//
// Copyright (C) ANEO, 2021-2024. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY, without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

using System;

namespace ArmoniK.Core.Common.Exceptions;

[Serializable]
public class TaskAlreadyExistsException : ArmoniKException
{
public TaskAlreadyExistsException()
{
}

public TaskAlreadyExistsException(string message)
: base(message)
{
}

public TaskAlreadyExistsException(string message,
Exception innerException)
: base(message,
innerException)
{
}
}
lemaitre-aneo marked this conversation as resolved.
Show resolved Hide resolved
21 changes: 21 additions & 0 deletions Common/src/Pollster/AcquisitionStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,27 @@ public enum AcquisitionStatus
/// </summary>
TaskIsRetried,

/// <summary>
/// Task not acquired because its status is <see cref="TaskStatus.Retried" />. Moreover, the retried task is
/// <see cref="TaskStatus.Creating" />
/// Retried task finalization is required.
/// </summary>
TaskIsRetriedAndRetryIsCreating,

/// <summary>
/// Task not acquired because its status is <see cref="TaskStatus.Retried" />. Moreover, the retried task was not found
/// in the database
/// Retried task creation and submission is required.
/// </summary>
TaskIsRetriedAndRetryIsNotFound,

/// <summary>
/// Task not acquired because its status is <see cref="TaskStatus.Retried" />. Moreover, the retried task is
/// <see cref="TaskStatus.Submitted" />
/// Reinsertion in the queue may be required.
/// </summary>
TaskIsRetriedAndRetryIsSubmitted,

/// <summary>
/// Task not acquired because its status is <see cref="TaskStatus.Processing" /> but the other pod does not seem to be
/// processing it
Expand Down
74 changes: 74 additions & 0 deletions Common/src/Pollster/TaskHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
Expand Down Expand Up @@ -445,6 +446,79 @@ await submitter_.CompleteTaskAsync(taskData_,
case TaskStatus.Retried:
logger_.LogInformation("Task is in retry ; retry task should be executed");
messageHandler_.Status = QueueMessageStatus.Poisonous;
var retryId = taskData_.RetryId();

try
{
var retryData = await taskTable_.ReadTaskAsync(retryId,
lateCts_.Token)
.ConfigureAwait(false);
if (retryData.Status is TaskStatus.Creating or TaskStatus.Submitted)
{
logger_.LogWarning("Retried task {task} is in {status}; trying to finalize task creation",
retryId,
retryData.Status);
await submitter_.FinalizeTaskCreation(new List<TaskCreationRequest>
{
new(retryId,
retryData.PayloadId,
retryData.Options,
retryData.ExpectedOutputIds,
retryData.DataDependencies),
},
sessionData_,
taskData_.TaskId,
CancellationToken.None)
.ConfigureAwait(false);
return retryData.Status == TaskStatus.Submitted
? AcquisitionStatus.TaskIsRetriedAndRetryIsSubmitted
: AcquisitionStatus.TaskIsRetriedAndRetryIsCreating;
}
}
catch (TaskNotFoundException)
{
logger_.LogWarning("Retried task {task} was not found in the database; resubmit it",
retryId);

try
{
await taskTable_.RetryTask(taskData_,
CancellationToken.None)
.ConfigureAwait(false);
}
catch (TaskAlreadyExistsException)
{
logger_.LogWarning("Retried task {task} already exists; finalize creation if needed",
retryId);
}
finally
{
var retryData = await taskTable_.ReadTaskAsync(retryId,
lateCts_.Token)
.ConfigureAwait(false);
if (retryData.Status is TaskStatus.Creating or TaskStatus.Submitted)
{
logger_.LogWarning("Retried task {task} is in {status}; trying to finalize task creation",
retryId,
retryData.Status);
await submitter_.FinalizeTaskCreation(new List<TaskCreationRequest>
{
new(retryId,
retryData.PayloadId,
retryData.Options,
retryData.ExpectedOutputIds,
retryData.DataDependencies),
},
sessionData_,
taskData_.TaskId,
CancellationToken.None)
.ConfigureAwait(false);
}
}

return AcquisitionStatus.TaskIsRetriedAndRetryIsNotFound;
}
aneojgurhem marked this conversation as resolved.
Show resolved Hide resolved

return AcquisitionStatus.TaskIsRetried;
case TaskStatus.Unspecified:
default:
Expand Down
10 changes: 10 additions & 0 deletions Common/src/Storage/TaskData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,14 @@ public static implicit operator Application(TaskData taskData)
taskData.Options.ApplicationNamespace,
taskData.Options.ApplicationVersion,
taskData.Options.ApplicationService);

/// <summary>
/// Compute the Id of the new task if this task should be retried
/// Should be deterministic as it can be called several time
/// </summary>
/// <returns>
/// Id of the retried task
/// </returns>
public string RetryId()
=> InitialTaskId + $"###{RetryOfIds.Count + 1}";
}
2 changes: 1 addition & 1 deletion Common/src/Storage/TaskTableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ public static async Task<string> RetryTask(this ITaskTable taskTable,
TaskData taskData,
CancellationToken cancellationToken = default)
{
var newTaskId = taskData.InitialTaskId + $"###{taskData.RetryOfIds.Count + 1}";
var newTaskId = taskData.RetryId();

var newTaskRetryOfIds = new List<string>(taskData.RetryOfIds)
{
Expand Down
50 changes: 27 additions & 23 deletions Common/tests/Helpers/TestTaskHandlerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
using System.Threading;

using ArmoniK.Api.Common.Options;
using ArmoniK.Core.Adapters.Memory;
using ArmoniK.Core.Adapters.MongoDB;
using ArmoniK.Core.Base;
using ArmoniK.Core.Common.gRPC.Services;
Expand Down Expand Up @@ -50,19 +49,22 @@ namespace ArmoniK.Core.Common.Tests.Helpers;

public class TestTaskHandlerProvider : IDisposable
{
private const string DatabaseName = "ArmoniK_TestDB";
private static readonly ActivitySource ActivitySource = new("ArmoniK.Core.Common.Tests.TestTaskHandlerProvider");
private readonly WebApplication app_;
private readonly IMongoClient client_;
private readonly LoggerFactory loggerFactory_;
private readonly IObjectStorage objectStorage_;
public readonly IPartitionTable PartitionTable;
public readonly IResultTable ResultTable;
private readonly IMongoRunner runner_;
public readonly ISessionTable SessionTable;
public readonly ISubmitter Submitter;
public readonly TaskHandler TaskHandler;
public readonly ITaskTable TaskTable;
private const string DatabaseName = "ArmoniK_TestDB";
private static readonly ActivitySource ActivitySource = new("ArmoniK.Core.Common.Tests.TestTaskHandlerProvider");
private readonly WebApplication app_;
private readonly IMongoClient client_;
public readonly ILogger Logger;
private readonly LoggerFactory loggerFactory_;
private readonly IObjectStorage objectStorage_;
public readonly IPartitionTable PartitionTable;
public readonly IPushQueueStorage PushQueueStorage;
public readonly IResultTable ResultTable;
private readonly IMongoRunner runner_;
public readonly ISessionTable SessionTable;
public readonly ISubmitter Submitter;
public readonly TaskHandler TaskHandler;
public readonly ITaskTable TaskTable;


public IHostApplicationLifetime Lifetime;

Expand Down Expand Up @@ -139,6 +141,7 @@ public TestTaskHandlerProvider(IWorkerStreamHandler workerStreamHandler,

loggerFactory_ = new LoggerFactory();
loggerFactory_.AddProvider(new ConsoleForwardingLoggerProvider());
Logger = loggerFactory_.CreateLogger("root");

var builder = WebApplication.CreateBuilder();

Expand All @@ -155,7 +158,7 @@ public TestTaskHandlerProvider(IWorkerStreamHandler workerStreamHandler,
Injection.Options.Submitter.SettingSection)
.AddOption<Injection.Options.Pollster>(builder.Configuration,
Injection.Options.Pollster.SettingSection)
.AddSingleton<IPushQueueStorage, PushQueueStorage>()
.AddSingleton<IPushQueueStorage, SimplePushQueueStorage>()
.AddSingleton<MeterHolder>()
.AddSingleton<AgentIdentifier>()
.AddSingleton<ExceptionManager.Options>()
Expand Down Expand Up @@ -206,14 +209,15 @@ public TestTaskHandlerProvider(IWorkerStreamHandler workerStreamHandler,

app_ = builder.Build();

ResultTable = app_.Services.GetRequiredService<IResultTable>();
TaskTable = app_.Services.GetRequiredService<ITaskTable>();
PartitionTable = app_.Services.GetRequiredService<IPartitionTable>();
SessionTable = app_.Services.GetRequiredService<ISessionTable>();
Submitter = app_.Services.GetRequiredService<ISubmitter>();
TaskHandler = app_.Services.GetRequiredService<TaskHandler>();
Lifetime = app_.Services.GetRequiredService<IHostApplicationLifetime>();
objectStorage_ = app_.Services.GetRequiredService<IObjectStorage>();
ResultTable = app_.Services.GetRequiredService<IResultTable>();
TaskTable = app_.Services.GetRequiredService<ITaskTable>();
PartitionTable = app_.Services.GetRequiredService<IPartitionTable>();
SessionTable = app_.Services.GetRequiredService<ISessionTable>();
Submitter = app_.Services.GetRequiredService<ISubmitter>();
TaskHandler = app_.Services.GetRequiredService<TaskHandler>();
Lifetime = app_.Services.GetRequiredService<IHostApplicationLifetime>();
objectStorage_ = app_.Services.GetRequiredService<IObjectStorage>();
PushQueueStorage = app_.Services.GetRequiredService<IPushQueueStorage>();

ResultTable.Init(CancellationToken.None)
.Wait();
Expand Down
Loading