Skip to content

Commit

Permalink
Implement RETRY room lifecycle operation
Browse files Browse the repository at this point in the history
Based on spec at 8ff947d.

The internal triggering of the RETRY operation (as specified by
CHA-RL1h3 and CHA-RL4b9) will come in #50.

Note that, since the RETRY operation does not currently cause a
transition to SUSPENDED, the `hasOperationInProgress` manager property
does not return `true` if there’s a RETRY in progress. (As mentioned in
ec1645a, we’ll address this in #50.)

Resolves #51.
  • Loading branch information
lawrence-forooghian committed Nov 14, 2024
1 parent 7cd83a8 commit 86eb3ae
Show file tree
Hide file tree
Showing 3 changed files with 540 additions and 10 deletions.
146 changes: 136 additions & 10 deletions Sources/AblyChat/RoomLifecycleManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,12 @@ internal actor DefaultRoomLifecycleManager<Contributor: RoomLifecycleContributor
internal enum Status: Equatable {
case initialized
case attachingDueToAttachOperation(attachOperationID: UUID)
case attachingDueToRetryOperation(retryOperationID: UUID)
case attachingDueToContributorStateChange(error: ARTErrorInfo?)
case attached
case detaching(detachOperationID: UUID)
case detached
case detachedDueToRetryOperation(retryOperationID: UUID)
case suspended(retryOperationID: UUID, error: ARTErrorInfo)
case failed(error: ARTErrorInfo)
case releasing(releaseOperationID: UUID)
Expand All @@ -196,13 +198,15 @@ internal actor DefaultRoomLifecycleManager<Contributor: RoomLifecycleContributor
.initialized
case .attachingDueToAttachOperation:
.attaching(error: nil)
case .attachingDueToRetryOperation:
.attaching(error: nil)
case let .attachingDueToContributorStateChange(error: error):
.attaching(error: error)
case .attached:
.attached
case .detaching:
.detaching
case .detached:
case .detached, .detachedDueToRetryOperation:
.detached
case let .suspended(_, error):
.suspended(error: error)
Expand All @@ -219,8 +223,12 @@ internal actor DefaultRoomLifecycleManager<Contributor: RoomLifecycleContributor
switch self {
case let .attachingDueToAttachOperation(attachOperationID):
attachOperationID
case let .attachingDueToRetryOperation(retryOperationID):
retryOperationID
case let .detaching(detachOperationID):
detachOperationID
case let .detachedDueToRetryOperation(retryOperationID):
retryOperationID
case let .releasing(releaseOperationID):
releaseOperationID
case let .suspended(retryOperationID, _):
Expand Down Expand Up @@ -676,7 +684,7 @@ internal actor DefaultRoomLifecycleManager<Contributor: RoomLifecycleContributor
case .released:
// CHA-RL1c
throw ARTErrorInfo(chatError: .roomIsReleased)
case .initialized, .suspended, .attachingDueToAttachOperation, .attachingDueToContributorStateChange, .detached, .detaching, .failed:
case .initialized, .suspended, .attachingDueToAttachOperation, .attachingDueToRetryOperation, .attachingDueToContributorStateChange, .detached, .detachedDueToRetryOperation, .detaching, .failed:
break
}

Expand Down Expand Up @@ -792,7 +800,7 @@ internal actor DefaultRoomLifecycleManager<Contributor: RoomLifecycleContributor

private func bodyOfDetachOperation(operationID: UUID) async throws {
switch status {
case .detached:
case .detached, .detachedDueToRetryOperation:
// CHA-RL2a
return
case .releasing:
Expand All @@ -804,22 +812,38 @@ internal actor DefaultRoomLifecycleManager<Contributor: RoomLifecycleContributor
case .failed:
// CHA-RL2d
throw ARTErrorInfo(chatError: .roomInFailedState)
case .initialized, .suspended, .attachingDueToAttachOperation, .attachingDueToContributorStateChange, .attached, .detaching:
case .initialized, .suspended, .attachingDueToAttachOperation, .attachingDueToRetryOperation, .attachingDueToContributorStateChange, .attached, .detaching:
break
}

// CHA-RL2e
clearTransientDisconnectTimeouts()
changeStatus(to: .detaching(detachOperationID: operationID))

try await performDetachmentCycle()
try await performDetachmentCycle(trigger: .detachOperation)
}

/// Describes the reason a CHA-RL2f detachment cycle is being performed.
private enum DetachmentCycleTrigger {
case detachOperation
case retryOperation(retryOperationID: UUID, triggeringContributor: Contributor)

/// Given a CHA-RL2f detachment cycle triggered by this trigger, returns the DETACHED status to which the room should transition per CHA-RL2g.
var detachedStatus: Status {
switch self {
case .detachOperation:
.detached
case let .retryOperation(retryOperationID, _):
.detachedDueToRetryOperation(retryOperationID: retryOperationID)
}
}
}

/// Performs the “CHA-RL2f detachment cycle”, to use the terminology of CHA-RL5a.
private func performDetachmentCycle() async throws {
private func performDetachmentCycle(trigger: DetachmentCycleTrigger) async throws {
// CHA-RL2f
var firstDetachError: Error?
for contributor in contributors {
for contributor in contributorsForDetachmentCycle(trigger: trigger) {
logger.log(message: "Detaching contributor \(contributor)", level: .info)
do {
try await contributor.channel.detach()
Expand Down Expand Up @@ -871,7 +895,19 @@ internal actor DefaultRoomLifecycleManager<Contributor: RoomLifecycleContributor
}

// CHA-RL2g
changeStatus(to: .detached)
changeStatus(to: trigger.detachedStatus)
}

/// Returns the contributors that should be detached in a CHA-RL2f detachment cycle.
private func contributorsForDetachmentCycle(trigger: DetachmentCycleTrigger) -> [Contributor] {
switch trigger {
case .detachOperation:
// CHA-RL2f
contributors
case let .retryOperation(_, triggeringContributor):
// CHA-RL5a
contributors.filter { $0.id != triggeringContributor.id }
}
}

// MARK: - RELEASE operation
Expand Down Expand Up @@ -899,7 +935,7 @@ internal actor DefaultRoomLifecycleManager<Contributor: RoomLifecycleContributor
case .released:
// CHA-RL3a
return
case .detached:
case .detached, .detachedDueToRetryOperation:
// CHA-RL3b
changeStatus(to: .released)
return
Expand All @@ -908,7 +944,7 @@ internal actor DefaultRoomLifecycleManager<Contributor: RoomLifecycleContributor
// See note on waitForCompletionOfOperationWithID for the current need for this force try
// swiftlint:disable:next force_try
return try! await waitForCompletionOfOperationWithID(releaseOperationID, waitingOperationID: operationID)
case .initialized, .attached, .attachingDueToAttachOperation, .attachingDueToContributorStateChange, .detaching, .suspended, .failed:
case .initialized, .attached, .attachingDueToAttachOperation, .attachingDueToRetryOperation, .attachingDueToContributorStateChange, .detaching, .suspended, .failed:
break
}

Expand Down Expand Up @@ -946,4 +982,94 @@ internal actor DefaultRoomLifecycleManager<Contributor: RoomLifecycleContributor
// CHA-RL3g
changeStatus(to: .released)
}

// MARK: - RETRY operation

/// Implements CHA-RL5’s RETRY operation.
///
/// - Parameters:
/// - forcedOperationID: Allows tests to force the operation to have a given ID. In combination with the ``testsOnly_subscribeToOperationWaitEvents`` API, this allows tests to verify that one test-initiated operation is waiting for another test-initiated operation.
/// - triggeringContributor: This is, in the language of CHA-RL5a, “the channel that became SUSPENDED”.
internal func performRetryOperation(testsOnly_forcingOperationID forcedOperationID: UUID? = nil, triggeredByContributor triggeringContributor: Contributor) async {
await performAnOperation(forcingOperationID: forcedOperationID) { operationID in
await bodyOfRetryOperation(operationID: operationID, triggeredByContributor: triggeringContributor)
}
}

private func bodyOfRetryOperation(operationID: UUID, triggeredByContributor triggeringContributor: Contributor) async {
// CHA-RL5a
do {
try await performDetachmentCycle(
trigger: .retryOperation(
retryOperationID: operationID,
triggeringContributor: triggeringContributor
)
)
} catch {
logger.log(message: "RETRY’s detachment cycle failed with error \(error). Ending RETRY.", level: .debug)
return
}

// CHA-RL5d
do {
try await waitForContributorThatTriggeredRetryToBecomeAttached(triggeringContributor)
} catch {
// CHA-RL5e
logger.log(message: "RETRY’s waiting for triggering contributor to attach failed with error \(error). Ending RETRY.", level: .debug)
return
}

// CHA-RL5f
changeStatus(to: .attachingDueToRetryOperation(retryOperationID: operationID))
do {
try await performAttachmentCycle()
} catch {
logger.log(message: "RETRY’s attachment cycle failed with error \(error). Ending RETRY.", level: .debug)
return
}
}

/// Performs CHA-RL5d’s “the room waits until the original channel that caused the retry loop naturally enters the ATTACHED state”.
///
/// Throws an error if the room enters the FAILED status, which is considered terminal by the RETRY operation.
private func waitForContributorThatTriggeredRetryToBecomeAttached(_ triggeringContributor: Contributor) async throws {
logger.log(message: "RETRY waiting for \(triggeringContributor) to enter ATTACHED", level: .debug)

let handleState = { [self] (state: ARTRealtimeChannelState, associatedError: ARTErrorInfo?) in
switch state {
// CHA-RL5d
case .attached:
logger.log(message: "RETRY completed waiting for \(triggeringContributor) to enter ATTACHED", level: .debug)
return true
// CHA-RL5e
case .failed:
guard let associatedError else {
preconditionFailure("Contributor entered FAILED but there’s no associated error")
}
logger.log(message: "RETRY failed waiting for \(triggeringContributor) to enter ATTACHED, since it entered FAILED with error \(associatedError)", level: .debug)

changeStatus(to: .failed(error: associatedError))
throw associatedError
case .attaching, .detached, .detaching, .initialized, .suspended:
return false
@unknown default:
return false
}
}

// Check whether the contributor is already in one of the states that we’re going to wait for. CHA-RL5d doesn’t make this check explicit but it seems like the right thing to do (asked in https://github.com/ably/specification/issues/221).
// TODO: this assumes that if you fetch a channel’s `state` and then its `errorReason`, they will both refer to the same channel state; this may not be true due to threading, address in https://github.com/ably-labs/ably-chat-swift/issues/49
if try await handleState(triggeringContributor.channel.state, triggeringContributor.channel.errorReason) {
return
}

// TODO: this assumes that if you check a channel’s state, and it’s x, and you then immediately add a state listener, you’ll definitely find out if the channel changes to a state other than x; this may not be true due to threading, address in https://github.com/ably-labs/ably-chat-swift/issues/49
for await stateChange in await triggeringContributor.channel.subscribeToState() {
// (I prefer this way of writing it, in this case)
// swiftlint:disable:next for_where
if try handleState(stateChange.current, stateChange.reason) {
return
}
}
}
}
Loading

0 comments on commit 86eb3ae

Please sign in to comment.