forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support HAProxy proxy protocol for broker and proxy (apache#8686)
### Motivation Currently, if enable the proxy in the pulsar cluster and client connect to the cluster through the proxy, when we get topic stats, the consumer address and producer address are not the real client address. This PR fix this problem by leverage HAProxy proxy protocol since this is a more general approach. more details about the proxy protocol see https://www.haproxy.com/blog/haproxy/proxy-protocol/ https://docs.aws.amazon.com/elasticloadbalancing/latest/classic/enable-proxy-protocol.html http://www.haproxy.org/download/1.8/doc/proxy-protocol.txt ### Modifications Allow enable proxy protocol on proxy or broker. ### Verifying this change Tests added
- Loading branch information
1 parent
39aaaf3
commit 625627c
Showing
22 changed files
with
423 additions
and
5 deletions.
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
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
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
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
105 changes: 105 additions & 0 deletions
105
pulsar-broker/src/test/java/org/apache/pulsar/broker/service/EnableProxyProtocolTest.java
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 |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.pulsar.broker.service; | ||
|
||
import io.netty.buffer.Unpooled; | ||
import lombok.Cleanup; | ||
import org.apache.pulsar.client.admin.PulsarAdminException; | ||
import org.apache.pulsar.client.api.PulsarClientException; | ||
import org.apache.pulsar.client.impl.ClientCnx; | ||
import org.apache.pulsar.client.impl.PulsarClientImpl; | ||
import org.apache.pulsar.common.policies.data.SubscriptionStats; | ||
import org.apache.pulsar.common.policies.data.TopicStats; | ||
import org.awaitility.Awaitility; | ||
import org.testng.Assert; | ||
import org.testng.annotations.AfterClass; | ||
import org.testng.annotations.BeforeClass; | ||
import org.testng.annotations.Test; | ||
|
||
import java.net.InetSocketAddress; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class EnableProxyProtocolTest extends BrokerTestBase { | ||
|
||
@BeforeClass | ||
@Override | ||
protected void setup() throws Exception { | ||
conf.setHaProxyProtocolEnabled(true); | ||
super.baseSetup(); | ||
} | ||
|
||
@AfterClass(alwaysRun = true) | ||
@Override | ||
protected void cleanup() throws Exception { | ||
super.internalCleanup(); | ||
} | ||
|
||
@Test | ||
public void testSimpleProduceAndConsume() throws PulsarClientException { | ||
final String namespace = "prop/ns-abc"; | ||
final String topicName = "persistent://" + namespace + "/testSimpleProduceAndConsume"; | ||
final String subName = "my-subscriber-name"; | ||
final int messages = 100; | ||
|
||
@Cleanup | ||
org.apache.pulsar.client.api.Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName) | ||
.subscribe(); | ||
|
||
@Cleanup | ||
org.apache.pulsar.client.api.Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).create(); | ||
for (int i = 0; i < messages; i++) { | ||
producer.send(("Message-" + i).getBytes()); | ||
} | ||
|
||
int received = 0; | ||
for (int i = 0; i < messages; i++) { | ||
consumer.acknowledge(consumer.receive()); | ||
received++; | ||
} | ||
|
||
Assert.assertEquals(received, messages); | ||
} | ||
|
||
@Test | ||
public void testProxyProtocol() throws PulsarClientException, ExecutionException, InterruptedException, PulsarAdminException { | ||
final String namespace = "prop/ns-abc"; | ||
final String topicName = "persistent://" + namespace + "/testProxyProtocol"; | ||
final String subName = "my-subscriber-name"; | ||
PulsarClientImpl client = (PulsarClientImpl) pulsarClient; | ||
CompletableFuture<ClientCnx> cnx = client.getCnxPool().getConnection(InetSocketAddress.createUnresolved("localhost", pulsar.getBrokerListenPort().get())); | ||
// Simulate the proxy protcol message | ||
cnx.get().ctx().channel().writeAndFlush(Unpooled.copiedBuffer("PROXY TCP4 198.51.100.22 203.0.113.7 35646 80\r\n".getBytes())); | ||
pulsarClient.newConsumer().topic(topicName).subscriptionName(subName) | ||
.subscribe(); | ||
org.apache.pulsar.broker.service.Consumer c = pulsar.getBrokerService().getTopicReference(topicName).get().getSubscription(subName).getConsumers().get(0); | ||
Awaitility.await().atMost(3, TimeUnit.SECONDS).untilAsserted(() -> Assert.assertTrue(c.cnx().hasHAProxyMessage())); | ||
TopicStats topicStats = admin.topics().getStats(topicName); | ||
Assert.assertEquals(topicStats.subscriptions.size(), 1); | ||
SubscriptionStats subscriptionStats = topicStats.subscriptions.get(subName); | ||
Assert.assertEquals(subscriptionStats.consumers.size(), 1); | ||
Assert.assertEquals(subscriptionStats.consumers.get(0).getAddress(), "198.51.100.22:35646"); | ||
|
||
pulsarClient.newProducer().topic(topicName).create(); | ||
topicStats = admin.topics().getStats(topicName); | ||
Assert.assertEquals(topicStats.publishers.size(), 1); | ||
Assert.assertEquals(topicStats.publishers.get(0).getAddress(), "198.51.100.22:35646"); | ||
} | ||
} |
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
49 changes: 49 additions & 0 deletions
49
...-common/src/main/java/org/apache/pulsar/common/protocol/OptionalProxyProtocolDecoder.java
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.pulsar.common.protocol; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.ChannelInboundHandlerAdapter; | ||
import io.netty.handler.codec.ProtocolDetectionResult; | ||
import io.netty.handler.codec.ProtocolDetectionState; | ||
import io.netty.handler.codec.haproxy.HAProxyMessageDecoder; | ||
import io.netty.handler.codec.haproxy.HAProxyProtocolVersion; | ||
|
||
/** | ||
* Decoder that added whether a new connection is prefixed with the ProxyProtocol. | ||
* More about the ProxyProtocol see: http://www.haproxy.org/download/1.8/doc/proxy-protocol.txt. | ||
*/ | ||
public class OptionalProxyProtocolDecoder extends ChannelInboundHandlerAdapter { | ||
|
||
public static final String NAME = "optional-proxy-protocol-decoder"; | ||
|
||
@Override | ||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { | ||
if (msg instanceof ByteBuf) { | ||
ProtocolDetectionResult<HAProxyProtocolVersion> result = | ||
HAProxyMessageDecoder.detectProtocol((ByteBuf) msg); | ||
if (result.state() == ProtocolDetectionState.DETECTED) { | ||
ctx.pipeline().addAfter(NAME, null, new HAProxyMessageDecoder()); | ||
ctx.pipeline().remove(this); | ||
} | ||
} | ||
super.channelRead(ctx, msg); | ||
} | ||
} |
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
Oops, something went wrong.