This is an automated email from the ASF dual-hosted git repository. lhotari pushed a commit to branch branch-4.1 in repository https://gitbox.apache.org/repos/asf/pulsar.git
commit a447eb31d68a33d881226bd7569498600b655e56 Author: Lari Hotari <[email protected]> AuthorDate: Tue Nov 4 20:33:27 2025 +0200 [improve][broker] Add tests for using absolute FQDN for advertisedAddress and remove extra dot from brokerId (#24787) (cherry picked from commit a1ed5c4a26029a6c6d6cb455ee653f133d651ce7) --- .../org/apache/pulsar/broker/PulsarService.java | 14 ++++-- .../pulsar/broker/PulsarServiceBrokerIdTest.java | 57 ++++++++++++++++++++++ .../ClientTlsAbsoluteAdvertisedAddressTest.java | 33 +++++++++++++ 3 files changed, 101 insertions(+), 3 deletions(-) diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/PulsarService.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/PulsarService.java index 15fe1dcd610..e61fbfac566 100644 --- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/PulsarService.java +++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/PulsarService.java @@ -966,9 +966,7 @@ public class PulsarService implements AutoCloseable, ShutdownService { // the broker id is used in the load manager to identify the broker // it should not be used for making connections to the broker - this.brokerId = - String.format("%s:%s", advertisedAddress, config.getWebServicePort() - .or(config::getWebServicePortTls).orElseThrow()); + this.brokerId = createBrokerId(); if (this.compactionServiceFactory == null) { this.compactionServiceFactory = loadCompactionServiceFactory(); @@ -1098,6 +1096,16 @@ public class PulsarService implements AutoCloseable, ShutdownService { } } + private String createBrokerId() { + // Remove any trailing dot from the absolute FQDN in the broker id to prevent it from impacting + // broker entries in the metadata store when making the advertised address absolute + String brokerIdHostPart = advertisedAddress.replaceFirst("\\.$", ""); + // Although broker id contains a hostname and port, it is not meant to be used for connecting to the broker, + // It is simply a unique identifier for the broker. + return String.format("%s:%s", brokerIdHostPart, config.getWebServicePort() + .or(config::getWebServicePortTls).orElseThrow()); + } + public void runWhenReadyForIncomingRequests(Runnable runnable) { // Here we don't call the thenRun() methods because CompletableFuture maintains a stack for pending callbacks, // not a queue. Once the future is complete, the pending callbacks will be executed in reverse order of diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/PulsarServiceBrokerIdTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/PulsarServiceBrokerIdTest.java new file mode 100644 index 00000000000..d3304671eac --- /dev/null +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/PulsarServiceBrokerIdTest.java @@ -0,0 +1,57 @@ +/* + * 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; + +import java.net.InetAddress; +import lombok.extern.slf4j.Slf4j; +import org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest; +import org.testng.Assert; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +@Slf4j +public class PulsarServiceBrokerIdTest extends MockedPulsarServiceBaseTest { + private String hostAddress; + + @BeforeMethod + @Override + protected void setup() throws Exception { + super.internalSetup(); + } + + @AfterMethod(alwaysRun = true) + @Override + protected void cleanup() throws Exception { + super.internalCleanup(); + } + + @Override + protected void doInitConf() throws Exception { + super.doInitConf(); + hostAddress = InetAddress.getLocalHost().getCanonicalHostName(); + // make it an absolute FQDN by adding a trailing dot + conf.setAdvertisedAddress(hostAddress + "."); + } + + @Test + public void testBrokerIdDoesntContainTrailingDot() throws Exception { + Assert.assertEquals(pulsar.getBrokerId(), hostAddress + ":" + conf.getWebServicePort().get()); + } +} diff --git a/tests/integration/src/test/java/org/apache/pulsar/tests/integration/tls/ClientTlsAbsoluteAdvertisedAddressTest.java b/tests/integration/src/test/java/org/apache/pulsar/tests/integration/tls/ClientTlsAbsoluteAdvertisedAddressTest.java new file mode 100644 index 00000000000..9f87a1c9ce7 --- /dev/null +++ b/tests/integration/src/test/java/org/apache/pulsar/tests/integration/tls/ClientTlsAbsoluteAdvertisedAddressTest.java @@ -0,0 +1,33 @@ +/* + * 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.tests.integration.tls; + +/** + * Test that the client can connect to a broker using TLS with an absolute advertised address. + */ +public class ClientTlsAbsoluteAdvertisedAddressTest extends ClientTlsTest { + @Override + protected void beforeStartCluster() throws Exception { + super.beforeStartCluster(); + getPulsarCluster().getBrokers().forEach(brokerContainer -> { + // make the advertised address absolute by adding a dot at the end + brokerContainer.withEnv("advertisedAddress", brokerContainer.getHostName() + "."); + }); + } +} \ No newline at end of file
