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

feat: limit re-trying commit to 2 retry attemps #2068

Merged
merged 2 commits into from
Sep 18, 2023
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 @@ -111,10 +111,15 @@ private enum class CommitStrategy {
}

private fun CoreFailure.getStrategy(
retryCount: Int,
vitorhugods marked this conversation as resolved.
Show resolved Hide resolved
retryOnClientMismatch: Boolean = true,
retryOnStaleMessage: Boolean = true
): CommitStrategy {
return if (this is NetworkFailure.ServerMiscommunication && this.kaliumException is KaliumException.InvalidRequestError) {
return if (
retryCount > 0 &&
this is NetworkFailure.ServerMiscommunication &&
kaliumException is KaliumException.InvalidRequestError
) {
if (this.kaliumException.isMlsClientMismatch() && retryOnClientMismatch) {
CommitStrategy.DISCARD_AND_RETRY
} else if (
Expand Down Expand Up @@ -458,18 +463,27 @@ internal class MLSConversationDataSource(
) =
operation()
.flatMapLeft {
handleCommitFailure(it, groupID, retryOnClientMismatch, retryOnStaleMessage, operation)
handleCommitFailure(
failure = it,
groupID = groupID,
retryCount = 2,
retryOnClientMismatch = retryOnClientMismatch,
retryOnStaleMessage = retryOnStaleMessage,
retryOperation = operation
)
}

private suspend fun handleCommitFailure(
failure: CoreFailure,
groupID: GroupID,
retryCount: Int,
retryOnClientMismatch: Boolean,
retryOnStaleMessage: Boolean,
retryOperation: suspend () -> Either<CoreFailure, Unit>
): Either<CoreFailure, Unit> {
return when (
failure.getStrategy(
retryCount = retryCount,
retryOnClientMismatch = retryOnClientMismatch,
retryOnStaleMessage = retryOnStaleMessage
)
Expand All @@ -478,7 +492,14 @@ internal class MLSConversationDataSource(
CommitStrategy.DISCARD_AND_RETRY -> discardCommitAndRetry(groupID, retryOperation)
CommitStrategy.ABORT -> return discardCommit(groupID).flatMap { Either.Left(failure) }
}.flatMapLeft {
handleCommitFailure(it, groupID, retryOnClientMismatch, retryOnStaleMessage, retryOperation)
handleCommitFailure(
failure = it,
groupID = groupID,
retryCount = retryCount - 1,
retryOnClientMismatch = retryOnClientMismatch,
retryOnStaleMessage = retryOnStaleMessage,
retryOperation = retryOperation
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,27 @@ class MLSConversationRepositoryTest {
.wasInvoked(once)
}

@Test
fun givenRetryLimitIsReached_whenCallingAddMemberToMLSGroup_thenClearCommitAndFail() = runTest {
val (arrangement, mlsConversationRepository) = Arrangement()
.withClaimKeyPackagesSuccessful()
.withGetMLSClientSuccessful()
.withAddMLSMemberSuccessful()
.withSendCommitBundleFailing(Arrangement.MLS_STALE_MESSAGE_ERROR, times = Int.MAX_VALUE)
.withCommitPendingProposalsSuccessful()
.withClearProposalTimerSuccessful()
.withWaitUntilLiveSuccessful()
.arrange()

val result = mlsConversationRepository.addMemberToMLSGroup(Arrangement.GROUP_ID, listOf(TestConversation.USER_ID1))
result.shouldFail()

verify(arrangement.mlsClient)
.function(arrangement.mlsClient::clearPendingCommit)
.with(eq(Arrangement.RAW_GROUP_ID))
.wasInvoked(once)
}

@Test
fun givenSuccessfulResponses_whenCallingRequestToJoinGroup_ThenGroupStateIsUpdated() = runTest {
val (arrangement, mlsConversationRepository) = Arrangement()
Expand Down Expand Up @@ -552,6 +573,24 @@ class MLSConversationRepositoryTest {
.wasInvoked(once)
}

@Test
fun givenRetryLimitIsReached_whenCallingCommitPendingProposals_thenClearCommitAndFail() = runTest {
val (arrangement, mlsConversationRepository) = Arrangement()
.withGetMLSClientSuccessful()
.withCommitPendingProposalsSuccessful()
.withSendCommitBundleFailing(Arrangement.MLS_STALE_MESSAGE_ERROR, times = Int.MAX_VALUE)
.withWaitUntilLiveSuccessful()
.arrange()

val result = mlsConversationRepository.commitPendingProposals(Arrangement.GROUP_ID)
result.shouldFail()

verify(arrangement.mlsClient)
.function(arrangement.mlsClient::clearPendingCommit)
.with(eq(Arrangement.RAW_GROUP_ID))
.wasInvoked(once)
}

@Test
fun givenSuccessfulResponses_whenCallingRemoveMemberFromGroup_thenCommitBundleIsSentAndAccepted() = runTest {
val (arrangement, mlsConversationRepository) = Arrangement()
Expand Down Expand Up @@ -637,6 +676,27 @@ class MLSConversationRepositoryTest {
.wasInvoked(once)
}

@Test
fun givenRetryLimitIsReached_whenCallingRemoveMemberFromGroup_thenClearCommitAndFail() = runTest {
val (arrangement, mlsConversationRepository) = Arrangement()
.withCommitPendingProposalsSuccessful()
.withGetMLSClientSuccessful()
.withFetchClientsOfUsersSuccessful()
.withRemoveMemberSuccessful()
.withSendCommitBundleFailing(Arrangement.MLS_STALE_MESSAGE_ERROR, times = Int.MAX_VALUE)
.withWaitUntilLiveSuccessful()
.arrange()

val users = listOf(TestUser.USER_ID)
val result = mlsConversationRepository.removeMembersFromMLSGroup(Arrangement.GROUP_ID, users)
result.shouldFail()

verify(arrangement.mlsClient)
.function(arrangement.mlsClient::clearPendingCommit)
.with(eq(Arrangement.RAW_GROUP_ID))
.wasInvoked(once)
}

@Test
fun givenClientMismatchError_whenCallingRemoveMemberFromGroup_thenClearCommitAndRetry() = runTest {
val (arrangement, mlsConversationRepository) = Arrangement()
Expand Down Expand Up @@ -874,6 +934,25 @@ class MLSConversationRepositoryTest {
.wasInvoked(once)
}

@Test
fun givenRetryLimitIsReached_whenCallingUpdateKeyMaterial_clearCommitAndFail() = runTest {
val (arrangement, mlsConversationRepository) = Arrangement()
.withGetMLSClientSuccessful()
.withUpdateKeyingMaterialSuccessful()
.withCommitPendingProposalsSuccessful()
.withSendCommitBundleFailing(Arrangement.MLS_STALE_MESSAGE_ERROR, times = Int.MAX_VALUE)
.withWaitUntilLiveSuccessful()
.arrange()

val result = mlsConversationRepository.updateKeyingMaterial(Arrangement.GROUP_ID)
result.shouldFail()

verify(arrangement.mlsClient)
.function(arrangement.mlsClient::clearPendingCommit)
.with(eq(Arrangement.RAW_GROUP_ID))
.wasInvoked(once)
}

@Test
fun givenConversationWithOutdatedEpoch_whenCallingIsGroupOutOfSync_returnsTrue() = runTest {
val returnEpoch = 10UL
Expand Down
Loading