Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,87 @@ public void schedule_retry_should_not_turn_a_transient_wait_into_failure_at_the_
Assert.That(item.OverallStatus, Is.EqualTo(PendingImportStatus.Retrying));
Assert.That(item.AudiobookStatus, Is.EqualTo(PendingImportStatus.Pending));
Assert.That(item.EbookStatus, Is.EqualTo(PendingImportStatus.NotRequested));
Assert.That(item.NextAttemptAt, Is.InRange(before.AddMinutes(3.9), DateTime.UtcNow.AddMinutes(6.1)));

// A long-running wait stays Retrying forever, but at the capped backoff rather than every five minutes.
Assert.That(item.NextAttemptAt, Is.InRange(before.AddHours(4.79), DateTime.UtcNow.AddHours(7.21)));
Assert.That(repositoryProxy.Updated, Is.SameAs(item));
Assert.That(events.Events, Has.None.InstanceOf<PendingAuthorImportFailedEvent>());
}

[Test]
public void schedule_retry_should_not_push_back_a_request_merged_in_after_the_worker_read_it()
{
var repository = DispatchProxy.Create<IPendingAuthorImportRepository, RepositoryProxy>();
var repositoryProxy = (RepositoryProxy)repository;
var subject = new PendingAuthorImportService(repository, DispatchProxy.Create<IAuthorService, AuthorServiceProxy>(), new RecordingEventAggregator(), LogManager.GetCurrentClassLogger());

// The worker processed version 5 at attempt 99; meanwhile a new request was merged
// in (version 6) and made due straight away.
var processed = new PendingAuthorImport
{
Id = 42, ProviderId = "gr:42", AttemptCount = 99, Version = 5,
AudiobookStatus = PendingImportStatus.Pending, EbookStatus = PendingImportStatus.NotRequested,
OverallStatus = PendingImportStatus.Retrying
};
var dueNow = DateTime.UtcNow;
repositoryProxy.Active = new PendingAuthorImport
{
Id = 42, ProviderId = "gr:42", AttemptCount = 99, Version = 6, NextAttemptAt = dueNow,
AudiobookStatus = PendingImportStatus.Pending, EbookStatus = PendingImportStatus.NotRequested,
OverallStatus = PendingImportStatus.Pending
};

subject.ScheduleRetry(processed, PendingAuthorImportRetryReason.AuthorNotYetAvailable);

Assert.That(repositoryProxy.Updated.NextAttemptAt, Is.EqualTo(dueNow));
}

[Test]
public void schedule_retry_should_back_off_the_request_the_worker_processed()
{
var repository = DispatchProxy.Create<IPendingAuthorImportRepository, RepositoryProxy>();
var repositoryProxy = (RepositoryProxy)repository;
var subject = new PendingAuthorImportService(repository, DispatchProxy.Create<IAuthorService, AuthorServiceProxy>(), new RecordingEventAggregator(), LogManager.GetCurrentClassLogger());
var item = new PendingAuthorImport
{
Id = 42, ProviderId = "gr:42", AttemptCount = 99, Version = 5, NextAttemptAt = DateTime.UtcNow,
AudiobookStatus = PendingImportStatus.Pending, EbookStatus = PendingImportStatus.NotRequested,
OverallStatus = PendingImportStatus.Retrying
};
repositoryProxy.Active = item;
var before = DateTime.UtcNow;

subject.ScheduleRetry(item, PendingAuthorImportRetryReason.AuthorNotYetAvailable);

Assert.That(repositoryProxy.Updated.NextAttemptAt, Is.GreaterThan(before.AddHours(4.79)));
}

[TestCase(1, 60)]
[TestCase(3, 60)]
[TestCase(4, 300)]
[TestCase(15, 300)]
[TestCase(16, 600)]
[TestCase(17, 1200)]
[TestCase(21, 19200)]
[TestCase(22, 21600)]
[TestCase(8727, 21600)]
[TestCase(int.MaxValue, 21600)]
public void pending_import_retry_should_keep_the_first_hour_fast_then_back_off_to_a_ceiling(int attemptCount, double expectedSeconds)
{
Assert.That(AuthorRetrySchedule.PendingImportDelaySeconds(attemptCount), Is.EqualTo(expectedSeconds));
}

[Test]
public void shared_author_retry_schedule_should_keep_its_five_minute_ceiling()
{
// Metadata sync and format sync share NextAttempt; only pending imports back off further.
var before = DateTime.UtcNow;

var next = AuthorRetrySchedule.NextAttempt(500);

Assert.That(next, Is.InRange(before.AddSeconds(239), DateTime.UtcNow.AddSeconds(361)));
}

[Test]
public void expected_version_status_update_should_leave_a_newer_merged_request_unchanged()
{
Expand Down
35 changes: 35 additions & 0 deletions src/NzbDrone.Core/Books/AuthorRetrySchedule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,48 @@ namespace NzbDrone.Core.Books
{
internal static class AuthorRetrySchedule
{
// Pending author imports keep the original cadence for roughly the first hour
// (three one-minute retries, then five-minute retries), then double the delay up
// to this ceiling. A request the metadata server never fulfils still retries
// forever, but a few times a day instead of 288 attempts (each a full author
// refresh) a day.
internal const int PendingImportFastAttempts = 15;
internal const double PendingImportMaxDelaySeconds = 6 * 60 * 60;

public static bool ShouldWarn(int attemptCount) => attemptCount == 1 || attemptCount == 4 || attemptCount % 10 == 0;

public static DateTime NextAttempt(int attemptCount)
{
// Shared with pending author requests: three short retries, then a bounded
// five-minute delay. Jitter keeps a recovered server from receiving a burst.
var seconds = attemptCount <= 3 ? 60 : 300;
return WithJitter(seconds);
}

public static DateTime NextPendingImportAttempt(int attemptCount)
{
return WithJitter(PendingImportDelaySeconds(attemptCount));
}

internal static double PendingImportDelaySeconds(int attemptCount)
{
if (attemptCount <= 3)
{
return 60;
}

if (attemptCount <= PendingImportFastAttempts)
{
return 300;
}

// Cap the exponent so very large legacy attempt counts cannot overflow.
var doublings = Math.Min(attemptCount - PendingImportFastAttempts, 16);
return Math.Min(300 * Math.Pow(2, doublings), PendingImportMaxDelaySeconds);
}

private static DateTime WithJitter(double seconds)
{
return DateTime.UtcNow.AddSeconds(seconds * (0.8 + Random.Shared.NextDouble() * 0.4));
}
}
Expand Down
11 changes: 9 additions & 2 deletions src/NzbDrone.Core/Books/Services/PendingAuthorImportService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,13 @@ public void ScheduleRetry(PendingAuthorImport item, string error)
current.MaxAttempts = 0;
current.LastAttemptAt = DateTime.UtcNow;
current.LastError = error;
current.NextAttemptAt = AuthorRetrySchedule.NextAttempt(current.AttemptCount);
var backoff = AuthorRetrySchedule.NextPendingImportAttempt(current.AttemptCount);

// A request merged in after this worker read the row may be due sooner; the backoff
// is for the request this worker processed, so it must not push the newer one back.
current.NextAttemptAt = current.Version != item.Version && current.NextAttemptAt < backoff
? current.NextAttemptAt
: backoff;
current.OverallStatus = PendingImportStatus.Retrying;
current.UpdatedAt = DateTime.UtcNow;

Expand All @@ -1042,7 +1048,8 @@ public void ScheduleRetry(PendingAuthorImport item, string error)
}

CopyMutableState(current, item);
_logger.Warn("Pending import {0} attempt {1} waiting until {2}: {3}", current.Id, current.AttemptCount, current.NextAttemptAt, error);
var level = AuthorRetrySchedule.ShouldWarn(current.AttemptCount) ? LogLevel.Warn : LogLevel.Debug;
_logger.Log(level, "Pending import {0} attempt {1} waiting until {2}: {3}", current.Id, current.AttemptCount, current.NextAttemptAt, error);
return;
}
}
Expand Down
Loading