You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@artemis.apache.org by ro...@apache.org on 2026/07/31 11:19:31 UTC

(artemis) 02/02: ARTEMIS-6172 Fix HTTP tunneling server async packets starve request replies

This is an automated email from the ASF dual-hosted git repository.

gemmellr pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/artemis.git

commit 29ba2a4c08786e876b27c966ff0fc62019d6bd53
Author: Domenico Francesco Bruscino <br...@apache.org>
AuthorDate: Fri Jul 31 09:05:27 2026 +0200

    ARTEMIS-6172 Fix HTTP tunneling server async packets starve request replies
    
    With httpEnabled=true, Core packets are tunneled over HTTP. HttpAcceptorHandler
    pairs each client POST with one queued HTTP response slot; every server
    outbound ByteBuf must take a slot before it can be flushed.
    Server-initiated / async packets (e.g. ClusterTopologyChangeMessage) also
    consume those slots. When they drain the queue, replies to the client’s own
    requests block on responses.take() until another HTTP request arrives.
    The client is often waiting for that reply, so the connection stalls.
---
 .../artemis/core/protocol/ProtocolHandler.java     |   3 +-
 .../remoting/impl/netty/HttpAcceptorHandler.java   |   9 ++
 .../impl/netty/HttpAcceptorHandlerTest.java        | 142 +++++++++++++++++++++
 docs/user-manual/versions.adoc                     |   2 +
 .../integration/http/CoreClientOverHttpTest.java   |  85 +++++++++++-
 5 files changed, 238 insertions(+), 3 deletions(-)

diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/protocol/ProtocolHandler.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/protocol/ProtocolHandler.java
index b0ed7a167a..205991a457 100644
--- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/protocol/ProtocolHandler.java
+++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/protocol/ProtocolHandler.java
@@ -33,6 +33,7 @@ import io.netty.channel.ChannelPipeline;
 import io.netty.handler.codec.ByteToMessageDecoder;
 import io.netty.handler.codec.http.DefaultFullHttpResponse;
 import io.netty.handler.codec.http.FullHttpRequest;
+import io.netty.handler.codec.http.HttpHeaderNames;
 import io.netty.handler.codec.http.HttpHeaders;
 import io.netty.handler.codec.http.HttpObjectAggregator;
 import io.netty.handler.codec.http.HttpRequestDecoder;
@@ -154,7 +155,7 @@ public class ProtocolHandler {
       public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
          if (msg instanceof FullHttpRequest httpRequest) {
             HttpHeaders headers = httpRequest.headers();
-            String upgrade = headers.get("upgrade");
+            String upgrade = headers.get(HttpHeaderNames.UPGRADE);
 
             if (upgrade != null && upgrade.equalsIgnoreCase("websocket")) {
                int stompMaxFramePayloadLength = ConfigurationHelper.getIntProperty(TransportConstants.STOMP_MAX_FRAME_PAYLOAD_LENGTH, -1, nettyAcceptor.getConfiguration());
diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/remoting/impl/netty/HttpAcceptorHandler.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/remoting/impl/netty/HttpAcceptorHandler.java
index cfc4708616..7796196906 100644
--- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/remoting/impl/netty/HttpAcceptorHandler.java
+++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/remoting/impl/netty/HttpAcceptorHandler.java
@@ -86,6 +86,15 @@ public class HttpAcceptorHandler extends ChannelDuplexHandler {
          responses.put(new ResponseHolder(System.currentTimeMillis() + responseTime, new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK)));
          ReferenceCountUtil.release(msg);
          return;
+      } else if (method.equals(HttpMethod.GET) && !request.headers().contains(HttpHeaderNames.UPGRADE)) {
+         // HTTP tunnel poll for a pending response; do not consume protocol Upgrade GETs (e.g. WebSocket).
+         if (responses.isEmpty()) {
+            responses.put(new ResponseHolder(System.currentTimeMillis() + responseTime,
+               new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK)));
+         }
+         // Consumed here (not forwarded), so release; unlike POST we keep no content, so no retain.
+         ReferenceCountUtil.release(msg);
+         return;
       }
       super.channelRead(ctx, msg);
    }
diff --git a/artemis-server/src/test/java/org/apache/activemq/artemis/core/remoting/impl/netty/HttpAcceptorHandlerTest.java b/artemis-server/src/test/java/org/apache/activemq/artemis/core/remoting/impl/netty/HttpAcceptorHandlerTest.java
index a23d0f2731..d6bc310612 100644
--- a/artemis-server/src/test/java/org/apache/activemq/artemis/core/remoting/impl/netty/HttpAcceptorHandlerTest.java
+++ b/artemis-server/src/test/java/org/apache/activemq/artemis/core/remoting/impl/netty/HttpAcceptorHandlerTest.java
@@ -16,13 +16,28 @@
  */
 package org.apache.activemq.artemis.core.remoting.impl.netty;
 
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 import io.netty.channel.embedded.EmbeddedChannel;
+import io.netty.handler.codec.http.DefaultFullHttpRequest;
+import io.netty.handler.codec.http.FullHttpRequest;
+import io.netty.handler.codec.http.FullHttpResponse;
+import io.netty.handler.codec.http.HttpHeaderNames;
+import io.netty.handler.codec.http.HttpMethod;
+import io.netty.handler.codec.http.HttpVersion;
+import org.apache.activemq.artemis.utils.Wait;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
 import org.mockito.Mockito;
 import org.mockito.junit.jupiter.MockitoExtension;
 
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.mockito.Mockito.spy;
 
 @ExtendWith(MockitoExtension.class)
@@ -60,4 +75,131 @@ public class HttpAcceptorHandlerTest {
 
       Mockito.verify(spy).unregisterKeepAliveHandler(httpHandler);
    }
+
+   @Test
+   public void testGetWithoutUpgradeIsConsumedAndReleased() {
+      EmbeddedChannel channel = new EmbeddedChannel();
+      channel.pipeline().addLast(HTTP_HANDLER, new HttpAcceptorHandler(spy, 1000, channel));
+
+      FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
+      channel.writeInbound(request);
+
+      assertNull(channel.readInbound());
+      assertEquals(0, request.refCnt());
+
+      channel.finishAndReleaseAll();
+   }
+
+   @Test
+   public void testGetWithUpgradeIsForwarded() {
+      EmbeddedChannel channel = new EmbeddedChannel();
+      channel.pipeline().addLast(HTTP_HANDLER, new HttpAcceptorHandler(spy, 1000, channel));
+
+      FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
+      request.headers().set(HttpHeaderNames.UPGRADE, "websocket");
+      channel.writeInbound(request);
+
+      assertSame(request, channel.readInbound());
+      assertEquals(1, request.refCnt());
+      request.release();
+
+      channel.finishAndReleaseAll();
+   }
+
+   /**
+    * Regression for ARTEMIS-6172: an async server write can consume the only HTTP response
+    * slot from a prior POST. A subsequent client GET must enqueue a new slot so the
+    * request reply is not starved.
+    */
+   @Test
+   public void testGetUnblocksReplyAfterAsyncPacketConsumesResponseSlot() throws Exception {
+      EmbeddedChannel channel = new EmbeddedChannel();
+      channel.pipeline().addLast(HTTP_HANDLER, new HttpAcceptorHandler(spy, 1000, channel));
+
+      FullHttpRequest post = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/",
+         Unpooled.wrappedBuffer(new byte[]{1}));
+      assertTrue(channel.writeInbound(post));
+      ByteBuf inbound = channel.readInbound();
+      assertNotNull(inbound);
+      inbound.release();
+
+      // Server-initiated / async packet consumes the only response slot.
+      channel.writeOutbound(Unpooled.wrappedBuffer(new byte[]{0xA}));
+      FullHttpResponse asyncResponse = awaitOutboundHttpResponse(channel);
+      assertEquals(1, asyncResponse.content().readableBytes());
+      assertEquals(0xA, asyncResponse.content().getByte(asyncResponse.content().readerIndex()));
+      asyncResponse.release();
+
+      // Request reply has no remaining slot and must not flush yet.
+      channel.writeOutbound(Unpooled.wrappedBuffer(new byte[]{0xB}));
+      assertFalse(Wait.waitFor(() -> {
+         channel.runPendingTasks();
+         return channel.outboundMessages().peek() != null;
+      }, 200, 20));
+
+      // Idle GET polls for pending server data when the response queue is empty.
+      FullHttpRequest get = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
+      channel.writeInbound(get);
+      assertNull(channel.readInbound());
+      assertEquals(0, get.refCnt());
+
+      FullHttpResponse replyResponse = awaitOutboundHttpResponse(channel);
+      assertEquals(1, replyResponse.content().readableBytes());
+      assertEquals(0xB, replyResponse.content().getByte(replyResponse.content().readerIndex()));
+      replyResponse.release();
+
+      channel.finishAndReleaseAll();
+   }
+
+   @Test
+   public void testGetDoesNotEnqueueExtraSlotWhenResponsesAlreadyQueued() throws Exception {
+      EmbeddedChannel channel = new EmbeddedChannel();
+      channel.pipeline().addLast(HTTP_HANDLER, new HttpAcceptorHandler(spy, 1000, channel));
+
+      FullHttpRequest post = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/",
+         Unpooled.wrappedBuffer(new byte[]{1}));
+      assertTrue(channel.writeInbound(post));
+      ByteBuf inbound = channel.readInbound();
+      assertNotNull(inbound);
+      inbound.release();
+
+      // Response slot already exists from the POST; GET must not add another.
+      FullHttpRequest get = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
+      channel.writeInbound(get);
+      assertNull(channel.readInbound());
+      assertEquals(0, get.refCnt());
+
+      channel.writeOutbound(Unpooled.wrappedBuffer(new byte[]{0xA}));
+      FullHttpResponse first = awaitOutboundHttpResponse(channel);
+      assertEquals(0xA, first.content().getByte(first.content().readerIndex()));
+      first.release();
+
+      // Only one slot was available; a second write stays blocked without another request.
+      channel.writeOutbound(Unpooled.wrappedBuffer(new byte[]{0xB}));
+      assertFalse(Wait.waitFor(() -> {
+         channel.runPendingTasks();
+         return channel.outboundMessages().peek() != null;
+      }, 200, 20));
+
+      FullHttpRequest getForReply = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
+      channel.writeInbound(getForReply);
+      assertEquals(0, getForReply.refCnt());
+
+      FullHttpResponse second = awaitOutboundHttpResponse(channel);
+      assertEquals(0xB, second.content().getByte(second.content().readerIndex()));
+      second.release();
+
+      channel.finishAndReleaseAll();
+   }
+
+   private static FullHttpResponse awaitOutboundHttpResponse(EmbeddedChannel channel) throws Exception {
+      Wait.assertTrue(() -> {
+         channel.runPendingTasks();
+         return channel.outboundMessages().peek() != null;
+      }, 5000, 10);
+      Object outbound = channel.readOutbound();
+      assertNotNull(outbound);
+      assertTrue(outbound instanceof FullHttpResponse, "unexpected outbound: " + outbound);
+      return (FullHttpResponse) outbound;
+   }
 }
diff --git a/docs/user-manual/versions.adoc b/docs/user-manual/versions.adoc
index 87e3207309..3b6653a777 100644
--- a/docs/user-manual/versions.adoc
+++ b/docs/user-manual/versions.adoc
@@ -26,6 +26,8 @@ https://issues.apache.org/jira/secure/ReleaseNote.jspa?projectId=12315920&versio
 === Upgrading from 2.55.0
 
 * Due to https://issues.apache.org/jira/browse/ARTEMIS-6128[ARTEMIS-6128] the `Alder32` and `fileAlder32` properties have been deprecated in favor of `Adler32` and `fileAdler32` respectively.
+* Due to https://issues.apache.org/jira/browse/ARTEMIS-6172[ARTEMIS-6172] the HTTP-specific transport parameters deprecated by https://issues.apache.org/jira/browse/ARTEMIS-5819[ARTEMIS-5819] (`httpClientIdleTime`, `httpClientIdleScanPeriod`, `httpResponseTime`, `httpServerScanPeriod`) are restored.
+These are required so HTTP-tunneled Core connections can create response slots for server-initiated packets (for example cluster topology updates) without starving replies to client requests.
 
 == Version 2.55.0
 
diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/http/CoreClientOverHttpTest.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/http/CoreClientOverHttpTest.java
index f0e23cc07b..801ccdf898 100644
--- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/http/CoreClientOverHttpTest.java
+++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/http/CoreClientOverHttpTest.java
@@ -18,6 +18,7 @@ package org.apache.activemq.artemis.tests.integration.http;
 
 import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.activemq.artemis.api.core.QueueConfiguration;
 import org.apache.activemq.artemis.api.core.SimpleString;
@@ -28,22 +29,31 @@ import org.apache.activemq.artemis.api.core.client.ClientMessage;
 import org.apache.activemq.artemis.api.core.client.ClientProducer;
 import org.apache.activemq.artemis.api.core.client.ClientSession;
 import org.apache.activemq.artemis.api.core.client.ClientSessionFactory;
+import org.apache.activemq.artemis.api.core.client.ClusterTopologyListener;
 import org.apache.activemq.artemis.api.core.client.ServerLocator;
+import org.apache.activemq.artemis.api.core.client.TopologyMember;
+import org.apache.activemq.artemis.core.client.impl.Topology;
+import org.apache.activemq.artemis.core.client.impl.TopologyMemberImpl;
 import org.apache.activemq.artemis.core.config.Configuration;
 import org.apache.activemq.artemis.core.remoting.impl.netty.TransportConstants;
 import org.apache.activemq.artemis.core.server.ActiveMQServer;
 import org.apache.activemq.artemis.core.server.ActiveMQServers;
+import org.apache.activemq.artemis.core.server.cluster.ClusterConnection;
 import org.apache.activemq.artemis.jms.client.ActiveMQTextMessage;
 import org.apache.activemq.artemis.tests.util.ActiveMQTestBase;
 import org.apache.activemq.artemis.utils.RandomUtil;
+import org.apache.activemq.artemis.utils.Wait;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 public class CoreClientOverHttpTest extends ActiveMQTestBase {
 
    private static final SimpleString QUEUE = SimpleString.of("CoreClientOverHttpTestQueue");
+   private static final String CONNECTOR_NAME = "http-connector";
    private Configuration conf;
    private ActiveMQServer server;
    private ServerLocator locator;
@@ -54,12 +64,21 @@ public class CoreClientOverHttpTest extends ActiveMQTestBase {
       super.setUp();
       Map<String, Object> params = new HashMap<>();
       params.put(TransportConstants.HTTP_ENABLED_PROP_NAME, true);
+      // Keep idle GET scans aggressive so HTTP response slots can be replenished quickly.
+      params.put(TransportConstants.HTTP_CLIENT_IDLE_PROP_NAME, 100L);
+      params.put(TransportConstants.HTTP_CLIENT_IDLE_SCAN_PERIOD, 50L);
 
-      conf = createDefaultInVMConfig().clearAcceptorConfigurations().addAcceptorConfiguration(new TransportConfiguration(NETTY_ACCEPTOR_FACTORY, params));
+      TransportConfiguration connectorConfig = new TransportConfiguration(NETTY_CONNECTOR_FACTORY, params, CONNECTOR_NAME);
+      conf = createDefaultInVMConfig()
+         .clearAcceptorConfigurations()
+         .addAcceptorConfiguration(new TransportConfiguration(NETTY_ACCEPTOR_FACTORY, params))
+         .addConnectorConfiguration(CONNECTOR_NAME, connectorConfig)
+         .addClusterConfiguration(basicClusterConnectionConfig(CONNECTOR_NAME));
 
       server = addServer(ActiveMQServers.newActiveMQServer(conf, false));
       server.start();
-      locator = addServerLocator(ActiveMQClient.createServerLocatorWithoutHA(new TransportConfiguration(NETTY_CONNECTOR_FACTORY, params)));
+      locator = addServerLocator(ActiveMQClient.createServerLocatorWithoutHA(connectorConfig));
+      locator.setCallTimeout(5000);
    }
 
    @Test
@@ -144,4 +163,66 @@ public class CoreClientOverHttpTest extends ActiveMQTestBase {
 
       session.close();
    }
+
+   /**
+    * ARTEMIS-6172: server-initiated ClusterTopology packets consume HTTP response slots.
+    * Idle GET polling must replenish slots so request/reply traffic is not starved.
+    */
+   @Test
+   public void testClusterTopologyDoesNotStarveHttpRequestReplies() throws Exception {
+      AtomicInteger topologyEvents = new AtomicInteger();
+      locator.addClusterTopologyListener(new ClusterTopologyListener() {
+         @Override
+         public void nodeUP(TopologyMember topologyMember, boolean last) {
+            topologyEvents.incrementAndGet();
+         }
+
+         @Override
+         public void nodeDown(long uniqueEventID, String nodeID) {
+            topologyEvents.incrementAndGet();
+         }
+      });
+
+      ClientSessionFactory sf = createSessionFactory(locator);
+      ClientSession session = sf.createSession(false, true, true);
+
+      ClusterConnection clusterConnection = server.getClusterManager().getClusterConnection("cluster1");
+      assertNotNull(clusterConnection);
+      Topology topology = clusterConnection.getTopology();
+
+      final int topologyUpdates = 50;
+      final int topologyEventsBeforeFlood = topologyEvents.get();
+      final long uniqueEventIDBase = System.currentTimeMillis();
+      for (int i = 0; i < topologyUpdates; i++) {
+         Map<String, Object> memberParams = new HashMap<>();
+         memberParams.put(TransportConstants.HOST_PROP_NAME, "127.0.0.1");
+         memberParams.put(TransportConstants.PORT_PROP_NAME, 17000 + i);
+         TransportConfiguration memberConnector = new TransportConfiguration(NETTY_CONNECTOR_FACTORY, memberParams);
+         // Use backup-only members so ClusterConnectionImpl does not attempt bridges to fake nodes,
+         // while still emitting ClusterTopology packets on the HTTP client connection.
+         String nodeId = "http-topo-" + i;
+         TopologyMemberImpl member = new TopologyMemberImpl(nodeId, null, null, null, memberConnector);
+         assertTrue(topology.updateMember(uniqueEventIDBase + i, nodeId, member));
+      }
+
+      Wait.assertTrue(() -> topologyEvents.get() >= topologyEventsBeforeFlood + topologyUpdates, 5_000, 50);
+
+      // Synchronous Core requests must still complete after async topology consumed HTTP slots.
+      SimpleString starvationQueue = SimpleString.of("HttpTopologyStarvationQueue");
+      session.createQueue(QueueConfiguration.of(starvationQueue).setDurable(false));
+
+      ClientProducer producer = session.createProducer(starvationQueue);
+      ClientMessage message = session.createMessage(false);
+      message.getBodyBuffer().writeString("topology-http");
+      producer.send(message);
+
+      ClientConsumer consumer = session.createConsumer(starvationQueue);
+      session.start();
+      ClientMessage received = consumer.receive(5_000);
+      assertNotNull(received);
+      assertEquals("topology-http", received.getBodyBuffer().readString());
+      received.acknowledge();
+
+      session.close();
+   }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@artemis.apache.org
For additional commands, e-mail: commits-help@artemis.apache.org