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

Always recreate stream on failure. #205

Merged
merged 2 commits into from
Jun 13, 2024
Merged
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
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ android.suppressUnsupportedOptionWarnings=android.suppressUnsupportedOptionWarni
kotlin.code.style=official
kotlin.mpp.stability.nowarn=true
GROUP=dev.yorkie
VERSION_NAME=0.4.24-rc
VERSION_NAME=0.4.24-rc2
POM_DESCRIPTION=Document store for building collaborative editing applications.
POM_INCEPTION_YEAR=2022
POM_URL=https://github.com/yorkie-team/yorkie-android-sdk
Expand Down
60 changes: 35 additions & 25 deletions yorkie/src/main/kotlin/dev/yorkie/core/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import dev.yorkie.util.createSingleThreadDispatcher
import java.io.Closeable
import java.io.InterruptedIOException
import java.util.UUID
import java.util.concurrent.TimeoutException
import kotlin.collections.Map.Entry
import kotlin.coroutines.coroutineContext
import kotlin.time.Duration
Expand Down Expand Up @@ -75,6 +76,7 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import kotlinx.coroutines.withTimeoutOrNull
import okhttp3.OkHttpClient

/**
Expand Down Expand Up @@ -310,32 +312,39 @@ public class Client @VisibleForTesting internal constructor(
while (true) {
ensureActive()
latestStream.safeClose()
val stream = service.watchDocument(
attachment.document.key.documentBasedRequestHeader,
).also {
latestStream = it
}
val stream = withTimeoutOrNull(1_000) {
service.watchDocument(
attachment.document.key.documentBasedRequestHeader,
).also {
latestStream = it
}
} ?: continue
val streamJob = launch(start = CoroutineStart.UNDISPATCHED) {
val channel = stream.responseChannel()
var retry = 0
while (!stream.isReceiveClosed() && !channel.isClosedForReceive) {
val receiveResult = channel.receiveCatching()
receiveResult.onSuccess {
attachment.document.publishEvent(StreamConnectionChanged.Connected)
handleWatchDocumentsResponse(attachment.document.key, it)
retry = 0
}.onFailure {
if (receiveResult.isClosed) {
return@onFailure
withTimeoutOrNull(60_000) {
val receiveResult = channel.receiveCatching()
receiveResult.onSuccess {
attachment.document.publishEvent(StreamConnectionChanged.Connected)
handleWatchDocumentsResponse(attachment.document.key, it)
}.onFailure {
if (receiveResult.isClosed) {
stream.safeClose()
return@onFailure
}
handleWatchStreamFailure(attachment.document, stream, it)
}.onClosed {
handleWatchStreamFailure(
attachment.document,
stream,
it ?: ClosedReceiveChannelException("Channel was closed"),
)
}
retry++
handleWatchStreamFailure(attachment.document, stream, it, retry > 3)
}.onClosed {
} ?: run {
handleWatchStreamFailure(
attachment.document,
stream,
it ?: ClosedReceiveChannelException("Channel was closed"),
true,
TimeoutException("channel timed out"),
Comment on lines +315 to +347
Copy link

Choose a reason for hiding this comment

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

Tip

Codebase Verification

The ClientTest.kt file includes instances of withTimeoutOrNull, but they are used to test document synchronization and not directly related to createWatchJob. This indicates that the timeout handling in createWatchJob is not explicitly tested.

  • ClientTest.kt contains tests for document synchronization using withTimeoutOrNull.
  • No specific tests cover the timeout scenarios within createWatchJob.
Analysis chain

The refactoring of createWatchJob to include timeout handling and stream recreation upon failure is well-implemented. However, ensure proper testing of the timeout values to match expected behavior in production environments.


To ensure that the timeout values in createWatchJob are properly tested, we need to search for test cases that might be testing this method explicitly or indirectly. Let's look for test files that might be related to this functionality and check if they include any tests for timeouts.


The search results indicate that there are no specific test cases directly testing the createWatchJob method for timeout values, but there are instances of withTimeoutOrNull in ClientTest.kt. This suggests that some timeout behavior is being tested, but we need to verify if these tests are comprehensive and cover the scenarios in createWatchJob.

To proceed, let's review the contents of ClientTest.kt to determine if the timeout values and behaviors in createWatchJob are adequately tested.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the timeout values are tested.
# Test: Search for the test cases related to `createWatchJob`.
rg --type kotlin --files-with-matches 'fun createWatchJob' | xargs rg 'withTimeoutOrNull'

Length of output: 241



Script:

#!/bin/bash
# Search for test files that might be related to the Client class or createWatchJob method.
fd -e kt -e kts -e java --type f -x grep -l 'Client' {} +
fd -e kt -e kts -e java --type f -x grep -l 'createWatchJob' {} +

# Check if any of these files contain tests for timeout values.
grep -E 'withTimeoutOrNull|TimeoutException' $(fd -e kt -e kts -e java --type f -x grep -l 'Client' {} +)
grep -E 'withTimeoutOrNull|TimeoutException' $(fd -e kt -e kts -e java --type f -x grep -l 'createWatchJob' {} +)

Length of output: 21210



Script:

#!/bin/bash
# Display the relevant lines from ClientTest.kt that include withTimeoutOrNull to verify if they test createWatchJob.
rg --context 10 'withTimeoutOrNull' ./yorkie/src/androidTest/kotlin/dev/yorkie/core/ClientTest.kt

Length of output: 2242

)
}
}
Expand All @@ -362,12 +371,10 @@ public class Client @VisibleForTesting internal constructor(
document: Document,
stream: ServerOnlyStreamInterface<*, *>,
cause: Throwable?,
closeStream: Boolean,
) {
onWatchStreamCanceled(document)
if (closeStream) {
stream.safeClose()
}
stream.safeClose()

cause?.let(::sendWatchStreamException)
coroutineContext.ensureActive()
delay(options.reconnectStreamDelay.inWholeMilliseconds)
Expand Down Expand Up @@ -409,11 +416,14 @@ public class Client @VisibleForTesting internal constructor(
}

private suspend fun ServerOnlyStreamInterface<*, *>?.safeClose() {
if (this == null || isReceiveClosed()) {
if (this == null) {
return
}
withContext(NonCancellable) {
receiveClose()
runCatching {
responseChannel().cancel()
receiveClose()
}
}
}

Expand Down
Loading