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

[ECO-5013] feat: Implement room lifecycle monitoring #94

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
132 changes: 132 additions & 0 deletions chat-android/src/main/java/com/ably/chat/Emitter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package com.ably.chat

import java.util.TreeSet
import java.util.concurrent.LinkedBlockingQueue
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
* Kotlin Emitter interface for supplied value
* Spec: RTE1
*/
internal interface Emitter<V> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t think we need this, as we’re essentially reinventing Flow here. Additionally, we’re missing some crucial points in the ScopedEmitter implementation. In its current form, listeners will be invoked non-sequentially, which can be misleading.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was hoping we could have a discussion about this eventually. I don't think we're trying to reinvent the wheel here, though the implementation might resemble shared or hot flows.

Unlike cold flows, hot flows continue emitting values even if no one is actively collecting them. This can lead to situations where the flow keeps emitting values even when the component is in the background, which could potentially cause memory leaks.

Additionally, the flow API is well-suited for use in coroutine-based functions, as both emit and collect are suspending functions. On the other hand, tryEmit can be used in non-suspending functions (like the channel.on method here), although it's generally not recommended.

Since tryEmit() is not a suspending function, it requires a buffer to store the emitted values until they are processed by subscribers. To make this work, we need to set the buffer size to unlimited, but this can affect performance and slow down other subscribers.

Another challenge is canceling flows, as there’s no guarantee that queued events will be delivered when cancellation occurs.

I'll create a separate thread to dive deeper into this and explore how the Emitter implementation can be leveraged for the existing public API to handle messages and presence events asynchronously on the Dispatchers.Default scope, as opposed to relying on the current blocking subscribers.

fun emit(value: V)
fun on(block: suspend CoroutineScope.(V) -> Unit): Subscription
fun once(block: suspend CoroutineScope.(V) -> Unit): Subscription
fun offAll()
}

/**
* ScopedEmitter is a thread-safe, non-blocking emitter implementation for Kotlin.
* It ensures that all subscribers receive events asynchronously in the same order under given scope.
*
* @param V The type of value to be emitted.
* @param subscriberScope The CoroutineScope in which the subscribers will run. Defaults to Dispatchers.Default.
* @param logger An optional logger for logging errors during event processing.
*/
internal class ScopedEmitter<V> (
private val subscriberScope: CoroutineScope = CoroutineScope(Dispatchers.Default),
private val logger: Logger? = null,
) : Emitter<V> {

// Sorted list of unique subscribers based on supplied block
private val subscribers = TreeSet<AsyncSubscriber<V>>()

// Emitter scope to make sure all subscribers receive events in same order.
// Will be automatically garbage collected once all jobs are performed.
private val sequentialScope = CoroutineScope(Dispatchers.Default.limitedParallelism(1))

val finishedProcessing: Boolean
get() = subscribers.all { it.values.isEmpty() && !it.isSubscriberRunning }

@get:Synchronized
val subscribersCount: Int
get() = subscribers.size

@Synchronized
override fun emit(value: V) {
for (subscriber in subscribers.toList()) {
subscriber.inform(value)
if (subscriber.once) {
off(subscriber)
}
}
}

private fun register(subscriber: AsyncSubscriber<V>): Subscription {
subscribers.add(subscriber)
return Subscription {
off(subscriber)
}
}

@Synchronized
override fun on(block: suspend CoroutineScope.(V) -> Unit): Subscription {
val subscriber = AsyncSubscriber(sequentialScope, subscriberScope, block, false, logger)
return register(subscriber)
}

@Synchronized
override fun once(block: suspend CoroutineScope.(V) -> Unit): Subscription {
val subscriber = AsyncSubscriber(sequentialScope, subscriberScope, block, true, logger)
return register(subscriber)
}

@Synchronized
override fun offAll() {
subscribers.clear()
}

@Synchronized
private fun off(subscriber: AsyncSubscriber<V>) {
subscribers.remove(subscriber)
}
}

private class AsyncSubscriber<V>(
private val emitterSequentialScope: CoroutineScope,
private val subscriberScope: CoroutineScope,
private val subscriberBlock: (suspend CoroutineScope.(V) -> Unit),
val once: Boolean,
private val logger: Logger? = null,
) : Comparable<V> {
val values = LinkedBlockingQueue<V>() // Accessed by both Emitter#emit and emitterSequentialScope
var isSubscriberRunning = false // Only accessed as a part of emitterSequentialScope

fun inform(value: V) {
values.add(value)
emitterSequentialScope.launch {
if (!isSubscriberRunning) {
isSubscriberRunning = true
while (values.isNotEmpty()) {
val valueTobeEmitted = values.poll()
safelyPublish(valueTobeEmitted as V) // Process sequentially, similar to core ably eventEmitter
}
isSubscriberRunning = false
}
}
}

private suspend fun safelyPublish(value: V) {
runCatching {
subscriberScope.launch {
try {
subscriberBlock(value)
} catch (t: Throwable) {
// Catching exception to avoid error propagation to parent
logger?.warn("Error processing value $value", t)
}
}.join()
}
}

override fun compareTo(other: V): Int {
// Avoid registering duplicate anonymous subscriber block with same instance id
// Common scenario when Android activity is refreshed or some app components refresh
if (other is AsyncSubscriber<*>) {
return this.subscriberBlock.hashCode().compareTo(other.subscriberBlock.hashCode())
}
return this.hashCode().compareTo(other.hashCode())
}
}
130 changes: 124 additions & 6 deletions chat-android/src/main/java/com/ably/chat/RoomLifecycleManager.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.ably.chat

import io.ably.lib.realtime.ChannelEvent
import io.ably.lib.realtime.ChannelState
import io.ably.lib.realtime.ChannelStateListener
import io.ably.lib.types.AblyException
import io.ably.lib.types.ErrorInfo
import kotlin.coroutines.resume
Expand Down Expand Up @@ -63,6 +65,11 @@ internal abstract class ContributesToRoomLifecycleImpl(logger: Logger) : Contrib
}
}

internal data class ContributorStateChange(
val contributor: ContributesToRoomLifecycle,
val stateChange: ChannelStateListener.ChannelStateChange,
)

/**
* The order of precedence for lifecycle operations, passed to PriorityQueueExecutor which allows
* us to ensure that internal operations take precedence over user-driven operations.
Expand Down Expand Up @@ -131,13 +138,21 @@ internal class RoomLifecycleManager(
*/
private val atomicCoroutineScope = AtomicCoroutineScope(roomScope)

/**
* contributorStateChangeEmitter is responsible for emitting and subscribing to ContributorStateChange events.
* Events are emitted by room contributors/features using the underlying channel.
* All emitted events are processed sequentially under the specified roomScope.
*/
private val contributorStateChangeMonitor = ScopedEmitter<ContributorStateChange>(roomScope)

/**
* This flag indicates whether some sort of controlled operation is in progress (e.g. attaching, detaching, releasing).
*
* It is used to prevent the room status from being changed by individual channel state changes and ignore
* underlying channel events until we reach a consistent state.
*/
private var operationInProgress = false
private val operationInProgress: Boolean
get() = !atomicCoroutineScope.finishedProcessing

/**
* A map of pending discontinuity events.
Expand All @@ -160,7 +175,113 @@ internal class RoomLifecycleManager(
private val retryDurationInMs: Long = 250

init {
// TODO - [CHA-RL4] set up room monitoring here
setupContributorListeners() // CHA-RL4
}

/**
* Sets up listeners for each contributor to the room status.
* Spec : CHA-RL4
*/
@Suppress("CognitiveComplexMethod", "LongMethod", "CyclomaticComplexMethod")
private fun setupContributorListeners() {
contributorStateChangeMonitor.on { change ->
val contributor = change.contributor
val stateChangeEvent = change.stateChange.event
val stateChangeReason = change.stateChange.reason
val stateResumed = change.stateChange.resumed

logger.debug(
"setupContributorListeners(); feature: ${contributor.featureName}, event: ${stateChangeEvent.toString().uppercase()}",
)

if (stateChangeEvent == ChannelEvent.attached || stateChangeEvent == ChannelEvent.update) {
// CHA-RL4b8 - If all features attached and room is not attached, transition room to ATTACHED state
if (!operationInProgress &&
statusLifecycle.status !== RoomStatus.Attached &&
contributors.all { it.channel.state == ChannelState.attached }
) {
logger.debug("setupContributorListeners(); all features are attached, transitioning room to ATTACHED state")
statusLifecycle.setStatus(RoomStatus.Attached)
}

logger.debug("setupContributorListeners(); event: ${stateChangeEvent.toString().uppercase()}")
// CHA-RL4a1 - If we're in a resumed state, we should ignore the event
if (stateResumed) {
logger.debug("setupContributorListeners(); resume is true, so ignore")
return@on
}
// CHA-RL4a2- If this is our first attach, we should ignore the event
if (!firstAttachesCompleted.containsKey(contributor)) {
logger.debug("setupContributorListeners(); first attach so ignore the event")
return@on
}
// CHA-RL4a3, CHA-RL4b1 - If operation in progress, we should queue the event
if (operationInProgress) {
if (pendingDiscontinuityEvents.containsKey(contributor)) {
logger.debug("setupContributorListeners(); operationInProgress, found existing discontinuity event, so ignore")
return@on
}
logger.warn(
"setupContributorListeners(); operation in progress, " +
"queuing pending update event for feature: ${contributor.featureName}",
)
pendingDiscontinuityEvents[contributor] = stateChangeReason
return@on
}
// CHA-RL4a4- If operation not in progress, we should emit discontinuity event
logger.debug("setupContributorListeners(); processing discontinuity event for feature: ${contributor.featureName}")
contributor.discontinuityDetected(stateChangeReason)
return@on
}

// CHA-RL4b - If we're in the middle of an operation, we should ignore other events
if (operationInProgress) {
logger.debug("setupContributorListeners(); operationInProgress, skip events if not ATTACH and UPDATE")
return@on
}

when (stateChangeEvent) {
ChannelEvent.attaching -> { // CHA-RL4b7
logger.debug("setupContributorListeners(); feature: ${contributor.featureName}, detected channel attaching")
if (statusLifecycle.status !== RoomStatus.Attaching) {
statusLifecycle.setStatus(RoomStatus.Attaching, stateChangeReason)
logger.debug("setupContributorListeners(); changing room status to ATTACHING")
}
return@on
}
ChannelEvent.failed -> { // CHA-RL4b5
logger.warn("setupContributorListeners(); feature: ${contributor.featureName}, detected channel failure")
if (statusLifecycle.status !== RoomStatus.Failed) {
statusLifecycle.setStatus(RoomStatus.Failed, stateChangeReason)
logger.debug("setupContributorListeners(); changing room status to FAILED, winding down all channels")
atomicCoroutineScope.async(LifecycleOperationPrecedence.Internal.priority) {
runDownChannelsOnFailedAttach()
}
}
return@on
}
ChannelEvent.suspended -> { // CHA-RL4b9
logger.warn("setupContributorListeners(); feature: ${contributor.featureName}, detected channel suspension")
if (statusLifecycle.status !== RoomStatus.Suspended) {
statusLifecycle.setStatus(RoomStatus.Suspended, stateChangeReason)
logger.debug("setupContributorListeners(); changed room status to SUSPENDED, retrying attach")
atomicCoroutineScope.async(LifecycleOperationPrecedence.Internal.priority) {
doRetry(contributor)
}
}
return@on
}
else -> logger.warn(
"setupContributorListeners(); no op for event: $stateChangeEvent received for feature: ${contributor.featureName}",
)
}
}
// Set up channel state change listener for each contributor
for (contributor in contributors) {
contributor.channel.on {
contributorStateChangeMonitor.emit(ContributorStateChange(contributor, it))
}
}
}

/**
Expand Down Expand Up @@ -336,7 +457,6 @@ internal class RoomLifecycleManager(

// At this point, we force the room status to be attaching
clearAllTransientDetachTimeouts()
operationInProgress = true
statusLifecycle.setStatus(RoomStatus.Attaching) // CHA-RL1e
logger.debug("attach(); transitioned room to ATTACHING state")

Expand Down Expand Up @@ -428,7 +548,6 @@ internal class RoomLifecycleManager(
logger.debug("doAttach(); attach success for all features: ${contributors.map { it.featureName }.joinWithBrackets}")
this.statusLifecycle.setStatus(attachResult)
logger.debug("doAttach(); transitioned room to ATTACHED state")
this.operationInProgress = false

// Iterate the pending discontinuity events and trigger them
for ((contributor, error) in pendingDiscontinuityEvents) {
Expand Down Expand Up @@ -551,7 +670,6 @@ internal class RoomLifecycleManager(
}

// CHA-RL2e - We force the room status to be detaching
operationInProgress = true
clearAllTransientDetachTimeouts()
statusLifecycle.setStatus(RoomStatus.Detaching)
logger.debug("detach(); transitioned room to DETACHING state")
Expand Down Expand Up @@ -623,7 +741,6 @@ internal class RoomLifecycleManager(
// CHA-RL3l - We force the room status to be releasing.
// Any transient disconnect timeouts shall be cleared.
clearAllTransientDetachTimeouts()
operationInProgress = true
statusLifecycle.setStatus(RoomStatus.Releasing)
logger.debug("release(); transitioned room to RELEASING state")

Expand Down Expand Up @@ -686,6 +803,7 @@ internal class RoomLifecycleManager(
contributors.forEach {
it.release()
}
contributorStateChangeMonitor.offAll()
logger.debug("doRelease(); underlying channels released from core SDK")
statusLifecycle.setStatus(RoomStatus.Released) // CHA-RL3g
logger.debug("doRelease(); transitioned room to RELEASED state")
Expand Down
Loading
Loading