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

DBP: Implement exponential backoff for optout retries #2815

Merged
merged 4 commits into from
Jun 5, 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
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ struct OperationPreferredDateCalculator {
return currentPreferredRunDate
}
case .error:
return date.now.addingTimeInterval(schedulingConfig.retryError.hoursToSeconds)
return date.now.addingTimeInterval(calculateNextRunDate(schedulingConfig: schedulingConfig, historyEvents: historyEvents))
case .optOutStarted, .scanStarted, .noMatchFound:
return currentPreferredRunDate
case .optOutConfirmed, .optOutRequested:
Expand All @@ -97,4 +97,10 @@ struct OperationPreferredDateCalculator {
let lastRemovalEventDate = lastRemovalEvent.date.addingTimeInterval(schedulingConfig.maintenanceScan.hoursToSeconds)
return lastRemovalEventDate < Date()
}

private func calculateNextRunDate(schedulingConfig: DataBrokerScheduleConfig,
historyEvents: [HistoryEvent]) -> TimeInterval {
let pastTries = historyEvents.filter { $0.isError }.count
return min(Int(pow(2.0, Double(pastTries))), schedulingConfig.retryError).hoursToSeconds
jotaemepereira marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import XCTest
final class OperationPreferredDateCalculatorTests: XCTestCase {

private let schedulingConfig = DataBrokerScheduleConfig(
retryError: 1000,
retryError: 48,
confirmOptOutScan: 2000,
maintenanceScan: 3000
)
Expand Down Expand Up @@ -322,17 +322,89 @@ final class OperationPreferredDateCalculatorTests: XCTestCase {
XCTAssertTrue(areDatesEqualIgnoringSeconds(date1: expectedOptOutDate, date2: actualOptOutDate))
}

func testError_thenOptOutDateIsRetry() throws {
let expectedOptOutDate = Date().addingTimeInterval(schedulingConfig.retryError.hoursToSeconds)

func testWhenOptOutFailedOnce_thenWeRetryInTwoHours() throws {
let expectedOptOutDate = Calendar.current.date(byAdding: .hour, value: 2, to: Date())!
let historyEvents = [
HistoryEvent(extractedProfileId: 1,
brokerId: 1,
profileQueryId: 1,
type: .error(error: DataBrokerProtectionError.malformedURL))]
let calculator = OperationPreferredDateCalculator()
let actualOptOutDate = try calculator.dateForOptOutOperation(currentPreferredRunDate: nil,
historyEvents: historyEvents,
extractedProfileID: nil,
schedulingConfig: schedulingConfig)

XCTAssertTrue(areDatesEqualIgnoringSeconds(date1: expectedOptOutDate, date2: actualOptOutDate))
}

func testWhenOptOutFailedTwice_thenWeRetryInFourHours() throws {
let expectedOptOutDate = Calendar.current.date(byAdding: .hour, value: 4, to: Date())!
let historyEvents: [HistoryEvent] = [
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL))
]
let calculator = OperationPreferredDateCalculator()
let actualOptOutDate = try calculator.dateForOptOutOperation(currentPreferredRunDate: nil,
historyEvents: historyEvents,
extractedProfileID: nil,
schedulingConfig: schedulingConfig)

XCTAssertTrue(areDatesEqualIgnoringSeconds(date1: expectedOptOutDate, date2: actualOptOutDate))
}

func testWhenOptOutFailedThreeTimes_thenWeRetryInEightHours() throws {
let expectedOptOutDate = Calendar.current.date(byAdding: .hour, value: 8, to: Date())!
let historyEvents: [HistoryEvent] = [
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL))
]
let calculator = OperationPreferredDateCalculator()
let actualOptOutDate = try calculator.dateForOptOutOperation(currentPreferredRunDate: nil,
historyEvents: historyEvents,
extractedProfileID: nil,
schedulingConfig: schedulingConfig)

XCTAssertTrue(areDatesEqualIgnoringSeconds(date1: expectedOptOutDate, date2: actualOptOutDate))
}
jotaemepereira marked this conversation as resolved.
Show resolved Hide resolved

func testWhenOptOutFailedSixTimes_thenWeRetryInTwoDays() throws {
let expectedOptOutDate = Calendar.current.date(byAdding: .day, value: 2, to: Date())!
let historyEvents: [HistoryEvent] = [
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL))
]
let calculator = OperationPreferredDateCalculator()
let actualOptOutDate = try calculator.dateForOptOutOperation(currentPreferredRunDate: nil,
historyEvents: historyEvents,
extractedProfileID: nil,
schedulingConfig: schedulingConfig)

XCTAssertTrue(areDatesEqualIgnoringSeconds(date1: expectedOptOutDate, date2: actualOptOutDate))
}

func testWhenOptOutFailedMoreThanTheThreshold_thenWeRetryAtTheSchedulingRetry() throws {
let expectedOptOutDate = Date().addingTimeInterval(schedulingConfig.retryError.hoursToSeconds)
let historyEvents: [HistoryEvent] = [
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL)),
.init(brokerId: 1, profileQueryId: 1, type: .error(error: .malformedURL))
]
let calculator = OperationPreferredDateCalculator()
let actualOptOutDate = try calculator.dateForOptOutOperation(currentPreferredRunDate: nil,
historyEvents: historyEvents,
extractedProfileID: nil,
Expand Down
Loading