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

Remove rabbitmq connection count and fix health check #2426

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@
@ConnectorAttribute(name = "queue.x-queue-mode", direction = INCOMING, description = "If automatically declare queue, we can choose different modes of queue [lazy, default]", type = "string")
@ConnectorAttribute(name = "max-outgoing-internal-queue-size", direction = OUTGOING, description = "The maximum size of the outgoing internal queue", type = "int")
@ConnectorAttribute(name = "max-incoming-internal-queue-size", direction = INCOMING, description = "The maximum size of the incoming internal queue", type = "int", defaultValue = "500000")
@ConnectorAttribute(name = "connection-count", direction = INCOMING, description = "The number of RabbitMQ connections to create for consuming from this queue. This might be necessary to consume from a sharded queue with a single client.", type = "int", defaultValue = "1")
@ConnectorAttribute(name = "queue.x-max-priority", direction = INCOMING, description = "Define priority level queue consumer", type = "int")
@ConnectorAttribute(name = "queue.x-delivery-limit", direction = INCOMING, description = "If queue.x-queue-type is quorum, when a message has been returned more times than the limit the message will be dropped or dead-lettered", type = "long")
@ConnectorAttribute(name = "queue.arguments", direction = INCOMING, description = "The identifier of the key-value Map exposed as bean used to provide arguments for queue creation", type = "string", defaultValue = "rabbitmq-queue-arguments")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,6 @@ public interface RabbitMQLogging extends BasicLogger {
@Message(id = 17038, value = "No valid content_type set, failing back to byte[]. If that's wanted, set the content type to application/octet-stream with \"content-type-override\"")
void typeConversionFallback();

@LogMessage(level = Level.DEBUG)
@Message(id = 17039, value = "Connection '%d' with RabbitMQ broker established for channel `%s`")
void connectionEstablished(int connectionIndex, String channel);

@LogMessage(level = Logger.Level.DEBUG)
@Message(id = 17040, value = "Established dead letter binding of queue `%s` to exchange '%s' using routing key '%s'")
void deadLetterBindingEstablished(String queueName, String exchangeName, String routingKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@

import static io.smallrye.reactive.messaging.rabbitmq.i18n.RabbitMQExceptions.ex;
import static io.smallrye.reactive.messaging.rabbitmq.i18n.RabbitMQLogging.log;
import static io.smallrye.reactive.messaging.rabbitmq.internals.RabbitMQClientHelper.declareExchangeIfNeeded;
import static io.smallrye.reactive.messaging.rabbitmq.internals.RabbitMQClientHelper.serverQueueName;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Flow;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

import jakarta.enterprise.inject.Instance;

Expand Down Expand Up @@ -46,7 +42,7 @@ public class IncomingRabbitMQChannel {

private final RabbitMQOpenTelemetryInstrumenter instrumenter;
private final AtomicReference<Flow.Subscription> subscription = new AtomicReference<>();
private final List<RabbitMQClient> clients = new CopyOnWriteArrayList<>();
private volatile RabbitMQClient client;
private final RabbitMQConnectorIncomingConfiguration config;
private final Multi<? extends Message<?>> stream;
private final RabbitMQConnector connector;
Expand All @@ -64,19 +60,10 @@ public IncomingRabbitMQChannel(RabbitMQConnector connector,
final RabbitMQFailureHandler onNack = createFailureHandler(connector.failureHandlerFactories(), ic);
final RabbitMQAckHandler onAck = createAckHandler(ic);

final Integer connectionCount = ic.getConnectionCount();
Multi<? extends Message<?>> multi = Multi.createFrom().range(0, connectionCount)
.onItem()
.transformToUniAndMerge(
connectionIdx -> createConsumer(connector, ic, connectionIdx))
.collect().asList()
.onItem()
.invoke(list -> clients.addAll(list.stream().map(t -> t.getItem1().client()).collect(Collectors.toList())))
Multi<? extends Message<?>> multi = createConsumer(connector, ic)
.invoke(tuple -> client = tuple.getItem1().client())
// Translate all consumers into a merged stream of messages
.onItem()
.transformToMulti(tuples -> Multi.createBy().merging()
.streams(tuples.stream().map(t -> getStreamOfMessages(t.getItem2(), t.getItem1(), ic, onNack, onAck))
.collect(Collectors.toList())));
.onItem().transformToMulti(tuple -> getStreamOfMessages(tuple.getItem2(), tuple.getItem1(), ic, onNack, onAck));

if (ic.getBroadcast()) {
multi = multi.broadcast().toAllSubscribers();
Expand All @@ -99,20 +86,16 @@ public HealthReport.HealthReportBuilder isAlive(HealthReport.HealthReportBuilder

private HealthReport.HealthReportBuilder computeHealthReport(HealthReport.HealthReportBuilder builder) {
if (config.getHealthLazySubscription()) {
if (subscription.get() != null) {
if (subscription.get() == null) {
return builder.add(new HealthReport.ChannelInfo(config.getChannel(), true));
}
}

// Verify that all clients are connected and channel opened
boolean alive = true;

// Verify that all consumers are connected.
for (RabbitMQClient client : clients) {
alive = alive && client.isConnected()
&& client.isOpenChannel();
if (client == null) {
return builder.add(new HealthReport.ChannelInfo(config.getChannel(), false));
}

boolean alive = client.isConnected() && client.isOpenChannel();
return builder.add(new HealthReport.ChannelInfo(config.getChannel(), alive));
}

Expand All @@ -125,7 +108,7 @@ public HealthReport.HealthReportBuilder isReady(HealthReport.HealthReportBuilder
}

private Uni<Tuple2<ClientHolder, RabbitMQConsumer>> createConsumer(RabbitMQConnector connector,
RabbitMQConnectorIncomingConfiguration ic, Integer connectionIdx) {
RabbitMQConnectorIncomingConfiguration ic) {
// Create a client
final RabbitMQClient client = RabbitMQClientHelper.createClient(connector, ic);
client.getDelegate().addConnectionEstablishedCallback(promise -> {
Expand All @@ -151,7 +134,7 @@ private Uni<Tuple2<ClientHolder, RabbitMQConsumer>> createConsumer(RabbitMQConne
}
final ClientHolder holder = new ClientHolder(client, ic, connector.vertx(), root);
return holder.getOrEstablishConnection()
.invoke(() -> log.connectionEstablished(connectionIdx, ic.getChannel()))
.invoke(() -> log.connectionEstablished(ic.getChannel()))
.flatMap(connection -> createConsumer(ic, connection).map(consumer -> Tuple2.of(holder, consumer)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,8 @@ private MapBasedConfig getBaseConfig() {
return new MapBasedConfig()
.with("mp.messaging.incoming.in.queue.name", "in")
.with("mp.messaging.incoming.in.connector", RabbitMQConnector.CONNECTOR_NAME)
.with("mp.messaging.incoming.in.host", host)
.with("mp.messaging.incoming.in.port", port)
.with("rabbitmq-username", username)
.with("rabbitmq-password", password)
.with("mp.messaging.outgoing.out.queue.name", "out")
.with("mp.messaging.outgoing.out.connector", RabbitMQConnector.CONNECTOR_NAME)
.with("mp.messaging.outgoing.out.host", host)
.with("mp.messaging.outgoing.out.port", port);
.with("mp.messaging.outgoing.out.connector", RabbitMQConnector.CONNECTOR_NAME);
}

@Test
Expand Down
Loading