-
Notifications
You must be signed in to change notification settings - Fork 1
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
[TH2-1988] separate connections for publishing and subscribing #318
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
804d1ff
separate connections for publishing and subscribing
cdfbbd0
integration test for separate connections for publishing and subscribing
492c387
HealthMetrics fix
5710005
version update, dependencies update, README.md
b041ca6
'connection manager receives messages when publishing is blocked' tes…
2acea6e
`connection manager receives messages when publishing is blocked` log…
6b5c84a
rollback debug changes
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
207 changes: 130 additions & 77 deletions
207
...va/com/exactpro/th2/common/schema/message/impl/rabbitmq/connection/ConnectionManager.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,7 @@ import org.junit.jupiter.api.Assertions.assertEquals | |
import org.junit.jupiter.api.Assertions.assertFalse | ||
import org.junit.jupiter.api.Assertions.assertNotEquals | ||
import org.junit.jupiter.api.Assertions.assertTrue | ||
import org.junit.jupiter.api.assertDoesNotThrow | ||
import org.junit.jupiter.api.BeforeAll | ||
import org.junit.jupiter.api.Test | ||
import org.slf4j.LoggerFactory | ||
|
@@ -56,6 +57,7 @@ import java.time.Duration | |
import java.util.concurrent.ArrayBlockingQueue | ||
import java.util.concurrent.CompletableFuture | ||
import java.util.concurrent.CountDownLatch | ||
import java.util.concurrent.Executors | ||
import java.util.concurrent.TimeUnit | ||
import java.util.concurrent.atomic.AtomicInteger | ||
import kotlin.concurrent.thread | ||
|
@@ -1190,6 +1192,102 @@ class TestConnectionManager { | |
) | ||
) | ||
|
||
@Test | ||
fun `connection manager receives messages when publishing is blocked`() { | ||
val routingKey = "routingKey1" | ||
val queueName = "queue1" | ||
val exchange = "test-exchange1" | ||
rabbit.let { rabbit -> | ||
declareQueue(rabbit, queueName) | ||
declareFanoutExchangeWithBinding(rabbit, exchange, queueName) | ||
|
||
LOGGER.info { "Started with port ${rabbit.amqpPort}" } | ||
LOGGER.info { "Started with port ${rabbit.amqpPort}" } | ||
val messagesCount = 10 | ||
val blockAfter = 3 | ||
val countDown = CountDownLatch(messagesCount) | ||
val messageSizeBytes = 7 | ||
createConnectionManager( | ||
rabbit, ConnectionManagerConfiguration( | ||
subscriberName = "test", | ||
prefetchCount = DEFAULT_PREFETCH_COUNT, | ||
confirmationTimeout = DEFAULT_CONFIRMATION_TIMEOUT, | ||
enablePublisherConfirmation = true, | ||
maxInflightPublicationsBytes = messagesCount * messageSizeBytes, | ||
heartbeatIntervalSeconds = 1, | ||
minConnectionRecoveryTimeout = 2000, | ||
maxConnectionRecoveryTimeout = 2000, | ||
// to avoid unexpected delays before recovery | ||
retryTimeDeviationPercent = 0 | ||
) | ||
).use { manager -> | ||
repeat(messagesCount) { index -> | ||
if (index == blockAfter) { | ||
assertFalse(manager.isPublishingBlocked) | ||
|
||
// blocks all publishers ( https://www.rabbitmq.com/docs/memory ) | ||
rabbit.executeInContainerWithLogging("rabbitmqctl", "set_vm_memory_high_watermark", "0") | ||
} | ||
|
||
manager.basicPublish(exchange, routingKey, null, "Hello $index".toByteArray(Charsets.UTF_8)) | ||
LOGGER.info("Published $index") | ||
|
||
if (index == blockAfter) { | ||
// wait for blocking of publishing connection | ||
Awaitility.await("publishing blocked") | ||
.pollInterval(10L, TimeUnit.MILLISECONDS) | ||
.atMost(100L, TimeUnit.MILLISECONDS) | ||
.until { manager.isPublishingBlocked } | ||
} | ||
} | ||
|
||
val receivedMessages = linkedSetOf<String>() | ||
LOGGER.info { "creating consumer" } | ||
|
||
val subscribeFuture = Executors.newSingleThreadExecutor().submit { | ||
manager.basicConsume(queueName, { _, delivery, ack -> | ||
val message = delivery.body.toString(Charsets.UTF_8) | ||
LOGGER.info { "Received $message from ${delivery.envelope.routingKey}" } | ||
if (receivedMessages.add(message)) { | ||
// decrement only unique messages | ||
countDown.countDown() | ||
} else { | ||
LOGGER.warn { "Duplicated $message for ${delivery.envelope.routingKey}" } | ||
} | ||
ack.confirm() | ||
}) { | ||
LOGGER.info { "Canceled $it" } | ||
} | ||
} | ||
|
||
assertDoesNotThrow("Failed to subscribe to queue") { | ||
// if subscription connection is blocked generates TimeoutException | ||
subscribeFuture.get(1, TimeUnit.SECONDS) | ||
subscribeFuture.cancel(true) | ||
} | ||
|
||
Awaitility.await("receive messages sent before blocking") | ||
.pollInterval(10L, TimeUnit.MILLISECONDS) | ||
.atMost(100L, TimeUnit.MILLISECONDS) | ||
.until { blockAfter.toLong() == messagesCount - countDown.count } | ||
|
||
Thread.sleep(100) // ensure no more messages received | ||
assertEquals(blockAfter.toLong(), messagesCount - countDown.count) | ||
assertTrue(manager.isPublishingBlocked) | ||
|
||
// unblocks publishers | ||
rabbit.executeInContainerWithLogging("rabbitmqctl", "set_vm_memory_high_watermark", "0.4") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we unlock the publisher in this test? |
||
assertFalse(manager.isPublishingBlocked) | ||
|
||
// delay receiving all messages | ||
Awaitility.await("all messages received") | ||
.pollInterval(10L, TimeUnit.MILLISECONDS) | ||
.atMost(100L, TimeUnit.MILLISECONDS) | ||
.until { countDown.count == 0L } | ||
} | ||
} | ||
} | ||
|
||
@AfterEach | ||
fun cleanupRabbitMq() { | ||
// cleanup is done to prevent queue name collision during test | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
messagesCount is greater than blockAfter, it means that publisher not blocking in this test because check logic is after this circle
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We send 3 messages, then block publishers, then send 7 more. Subscribe, receive 3, unblock, receive 7. This allows to ensure that publishers were actually blocked. We can simplify the scenario: just send a few messages, block publishers, try to read messages.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
then send 7 more
-> If publisher blocked we could able to publish additional messages