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

PIP-61: Advertise multiple addresses #6903

Merged
merged 19 commits into from
Jun 3, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ public class ServiceConfiguration implements PulsarConfiguration {
)
private String advertisedAddress;

//
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove this line if it is not needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have remove the code for this line

@FieldContext(category=CATEGORY_SERVER, doc = "")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add documentation for these two settings?

Also, can you add these settings to broker.conf file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have add the configuration doc here

private String advertisedListeners;

//
@FieldContext(category=CATEGORY_SERVER, doc = "")
private String internalListenerName;

@FieldContext(
category = CATEGORY_SERVER,
doc = "Number of threads to use for Netty IO."
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/**
* 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.validator;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;
import org.apache.pulsar.broker.ServiceConfiguration;
import org.apache.pulsar.policies.data.loadbalancer.AdvertisedListener;

import java.net.URI;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

/**
* the validator for pulsar multiple listeners.
*/
public final class MultipleListenerValidator {

/**
* validate the configure of `advertisedListeners`, `internalListenerName`, `advertisedAddress`.
* 1. `advertisedListeners` and `advertisedAddress` must not appear together.
* 2. the listener name in `advertisedListeners` must not duplicate.
* 3. user can not assign same 'host:port' to different listener.
* 4. if `internalListenerName` is absent, the first `listener` in the `advertisedListeners` will be the `internalListenerName`.
* 5. if pulsar do not specify `brokerServicePortTls`, should only contain one entry of `pulsar://` per listener name.
* @param config the pulsar broker configure.
* @return
*/
public static Map<String, AdvertisedListener> validateAndAnalysisAdvertisedListener(ServiceConfiguration config) {
if (StringUtils.isNotBlank(config.getAdvertisedListeners()) && StringUtils.isNotBlank(config.getAdvertisedAddress())) {
throw new IllegalArgumentException("`advertisedListeners` and `advertisedAddress` must not appear together");
}
if (StringUtils.isBlank(config.getAdvertisedListeners())) {
return Collections.EMPTY_MAP;
}
Optional<String> firstListenerName = Optional.empty();
Map<String, List<String>> listeners = Maps.newHashMap();
for (final String str : StringUtils.split(config.getAdvertisedListeners(), ",")) {
int index = str.indexOf(":");
if (index <= 0) {
throw new IllegalArgumentException("the configure entry `advertisedListeners` is invalid. because " +
str + " do not contain listener name");
}
String listenerName = StringUtils.trim(str.substring(0, index));
if (!firstListenerName.isPresent()) {
firstListenerName = Optional.of(listenerName);
}
String value = StringUtils.trim(str.substring(index + 1));
listeners.computeIfAbsent(listenerName, k -> Lists.newArrayListWithCapacity(2));
listeners.get(listenerName).add(value);
}
if (StringUtils.isBlank(config.getInternalListenerName())) {
config.setInternalListenerName(firstListenerName.get());
}
if (!listeners.containsKey(config.getInternalListenerName())) {
throw new IllegalArgumentException("the `advertisedListeners` configure do not contain `internalListenerName` entry");
}
final Map<String, AdvertisedListener> result = Maps.newHashMap();
final Map<String, Set<String>> reverseMappings = Maps.newHashMap();
for (final Map.Entry<String, List<String>> entry : listeners.entrySet()) {
if (entry.getValue().size() > 2) {
throw new IllegalArgumentException("there are redundant configure for listener `" + entry.getKey() + "`");
}
URI pulsarAddress = null, pulsarSslAddress = null;
for (final String strUri : entry.getValue()) {
try {
URI uri = URI.create(strUri);
if (StringUtils.equalsIgnoreCase(uri.getScheme(), "pulsar")) {
if (pulsarAddress == null) {
pulsarAddress = uri;
} else {
throw new IllegalArgumentException("there are redundant configure for listener `" + entry.getKey() + "`");
}
} else if (StringUtils.equalsIgnoreCase(uri.getScheme(), "pulsar+ssl")) {
if (pulsarSslAddress == null) {
pulsarSslAddress = uri;
} else {
throw new IllegalArgumentException("there are redundant configure for listener `" + entry.getKey() + "`");
}
}
String hostPort = String.format("%s:%d", uri.getHost(), uri.getPort());
reverseMappings.computeIfAbsent(hostPort, k -> Sets.newTreeSet());
Set<String> sets = reverseMappings.get(hostPort);
if (sets == null) {
sets = Sets.newTreeSet();
reverseMappings.put(hostPort, sets);
}
sets.add(entry.getKey());
if (sets.size() > 1) {
throw new IllegalArgumentException("must not specify `" + hostPort + "` to different listener.");
}
} catch (Throwable cause) {
throw new IllegalArgumentException("the value " + strUri + " in the `advertisedListeners` configure is invalid");
}
}
if (!config.getBrokerServicePortTls().isPresent()) {
if (pulsarSslAddress != null) {
throw new IllegalArgumentException("If pulsar do not start ssl port, there is no need to configure " +
" `pulsar+ssl` in `" + entry.getKey() + "` listener.");
}
} else {
if (pulsarSslAddress == null) {
throw new IllegalArgumentException("the `" + entry.getKey() + "` listener in the `advertisedListeners` "
+ " do not specify `pulsar+ssl` address.");
}
}
if (pulsarAddress == null) {
throw new IllegalArgumentException("the `" + entry.getKey() + "` listener in the `advertisedListeners` "
+ " do not specify `pulsar` address.");
}
result.put(entry.getKey(), AdvertisedListener.builder().brokerServiceUrl(pulsarAddress).brokerServiceUrlTls(pulsarSslAddress).build());
}
return result;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* 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.validator;

import org.apache.pulsar.broker.ServiceConfiguration;
import org.testng.annotations.Test;

import java.util.Optional;

/**
* testcase for MultipleListenerValidator.
*/
public class MultipleListenerValidatorTest {

@Test(expectedExceptions = IllegalArgumentException.class)
public void testAppearTogether() {
ServiceConfiguration config = new ServiceConfiguration();
config.setAdvertisedAddress("127.0.0.1");
config.setAdvertisedListeners("internal:pulsar://192.168.1.11:6660,internal:pulsar+ssl://192.168.1.11:6651");
config.setInternalListenerName("internal");
MultipleListenerValidator.validateAndAnalysisAdvertisedListener(config);
}

@Test(expectedExceptions = IllegalArgumentException.class)
public void testListenerDuplicate_1() {
ServiceConfiguration config = new ServiceConfiguration();
config.setAdvertisedListeners(" internal:pulsar://127.0.0.1:6660, internal:pulsar+ssl://127.0.0.1:6651,"
+ " internal:pulsar://192.168.1.11:6660, internal:pulsar+ssl://192.168.1.11:6651");
config.setInternalListenerName("internal");
MultipleListenerValidator.validateAndAnalysisAdvertisedListener(config);
}

@Test(expectedExceptions = IllegalArgumentException.class)
public void testListenerDuplicate_2() {
ServiceConfiguration config = new ServiceConfiguration();
config.setAdvertisedListeners(" internal:pulsar://127.0.0.1:6660," + " internal:pulsar://192.168.1.11:6660");
config.setInternalListenerName("internal");
MultipleListenerValidator.validateAndAnalysisAdvertisedListener(config);
}

@Test(expectedExceptions = IllegalArgumentException.class)
public void testDifferentListenerWithSameHostPort() {
ServiceConfiguration config = new ServiceConfiguration();
config.setAdvertisedListeners(" internal:pulsar://127.0.0.1:6660," + " external:pulsar://127.0.0.1:6660");
config.setInternalListenerName("internal");
MultipleListenerValidator.validateAndAnalysisAdvertisedListener(config);
}

@Test(expectedExceptions = IllegalArgumentException.class)
public void testListenerWithoutTLSPort() {
ServiceConfiguration config = new ServiceConfiguration();
config.setAdvertisedListeners(" internal:pulsar://127.0.0.1:6660, internal:pulsar+ssl://127.0.0.1:6651");
config.setInternalListenerName("internal");
MultipleListenerValidator.validateAndAnalysisAdvertisedListener(config);
}

@Test
public void testListenerWithTLSPort() {
ServiceConfiguration config = new ServiceConfiguration();
config.setBrokerServicePortTls(Optional.of(6651));
config.setAdvertisedListeners(" internal:pulsar://127.0.0.1:6660, internal:pulsar+ssl://127.0.0.1:6651");
config.setInternalListenerName("internal");
MultipleListenerValidator.validateAndAnalysisAdvertisedListener(config);
}

@Test(expectedExceptions = IllegalArgumentException.class)
public void testListenerWithoutNonTLSAddress() {
ServiceConfiguration config = new ServiceConfiguration();
config.setBrokerServicePortTls(Optional.of(6651));
config.setAdvertisedListeners(" internal:pulsar+ssl://127.0.0.1:6651");
config.setInternalListenerName("internal");
MultipleListenerValidator.validateAndAnalysisAdvertisedListener(config);
}

@Test(expectedExceptions = IllegalArgumentException.class)
public void testWithoutListenerNameInAdvertisedListeners() {
ServiceConfiguration config = new ServiceConfiguration();
config.setBrokerServicePortTls(Optional.of(6651));
config.setAdvertisedListeners(" internal:pulsar://127.0.0.1:6660, internal:pulsar+ssl://127.0.0.1:6651");
config.setInternalListenerName("external");
MultipleListenerValidator.validateAndAnalysisAdvertisedListener(config);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@
import java.lang.reflect.Method;
import java.net.InetSocketAddress;
import java.net.URI;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.*;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid use import .*

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolve the problem

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -69,6 +65,7 @@
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.pulsar.PulsarVersion;
import org.apache.pulsar.broker.admin.AdminResource;
import org.apache.pulsar.broker.authentication.AuthenticationService;
Expand All @@ -89,6 +86,7 @@
import org.apache.pulsar.broker.service.schema.SchemaRegistryService;
import org.apache.pulsar.broker.stats.MetricsGenerator;
import org.apache.pulsar.broker.stats.prometheus.PrometheusMetricsServlet;
import org.apache.pulsar.broker.validator.MultipleListenerValidator;
import org.apache.pulsar.broker.web.WebService;
import org.apache.pulsar.client.admin.PulsarAdmin;
import org.apache.pulsar.client.admin.PulsarAdminBuilder;
Expand All @@ -114,6 +112,7 @@
import org.apache.pulsar.functions.worker.WorkerConfig;
import org.apache.pulsar.functions.worker.WorkerService;
import org.apache.pulsar.functions.worker.WorkerUtils;
import org.apache.pulsar.policies.data.loadbalancer.AdvertisedListener;
import org.apache.pulsar.transaction.coordinator.TransactionMetadataStoreProvider;
import org.apache.pulsar.websocket.WebSocketConsumerServlet;
import org.apache.pulsar.websocket.WebSocketProducerServlet;
Expand Down Expand Up @@ -201,6 +200,8 @@ public enum State {

private final ReentrantLock mutex = new ReentrantLock();
private final Condition isClosedCondition = mutex.newCondition();
// key is listener name , value is pulsar address and pulsar ssl address
private Map<String, AdvertisedListener> advertisedListeners;

public PulsarService(ServiceConfiguration config) {
this(config, Optional.empty());
Expand All @@ -209,10 +210,21 @@ public PulsarService(ServiceConfiguration config) {
public PulsarService(ServiceConfiguration config, Optional<WorkerService> functionWorkerService) {
// Validate correctness of configuration
PulsarConfigurationLoader.isComplete(config);

// validate `advertisedAddress`, `advertisedListeners`, `internalListenerName`
Map<String, AdvertisedListener> result = MultipleListenerValidator.validateAndAnalysisAdvertisedListener(config);
if (result != null) {
this.advertisedListeners = Collections.unmodifiableMap(result);
} else {
this.advertisedListeners = Collections.unmodifiableMap(Collections.emptyMap());
}
state = State.Init;
// use `internalListenerName` listener as `advertisedAddress`
this.bindAddress = ServiceConfigurationUtils.getDefaultOrConfiguredAddress(config.getBindAddress());
this.advertisedAddress = advertisedAddress(config);
if (!this.advertisedListeners.isEmpty()) {
this.advertisedAddress = this.advertisedListeners.get(config.getInternalListenerName()).getBrokerServiceUrl().getHost();
} else {
this.advertisedAddress = advertisedAddress(config);
}
this.brokerVersion = PulsarVersion.getVersion();
this.config = config;
this.shutdownService = new MessagingServiceShutdownHook(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -849,14 +849,14 @@ public void start() throws PulsarServerException {
Map<String, String> protocolData = pulsar.getProtocolDataToAdvertise();

lastData = new LocalBrokerData(pulsar.getSafeWebServiceAddress(), pulsar.getWebServiceAddressTls(),
pulsar.getSafeBrokerServiceUrl(), pulsar.getBrokerServiceUrlTls());
pulsar.getSafeBrokerServiceUrl(), pulsar.getBrokerServiceUrlTls(), pulsar.getAdvertisedListeners());
lastData.setProtocols(protocolData);
// configure broker-topic mode
lastData.setPersistentTopicsEnabled(pulsar.getConfiguration().isEnablePersistentTopics());
lastData.setNonPersistentTopicsEnabled(pulsar.getConfiguration().isEnableNonPersistentTopics());

localData = new LocalBrokerData(pulsar.getSafeWebServiceAddress(), pulsar.getWebServiceAddressTls(),
pulsar.getSafeBrokerServiceUrl(), pulsar.getBrokerServiceUrlTls());
pulsar.getSafeBrokerServiceUrl(), pulsar.getBrokerServiceUrlTls(), pulsar.getAdvertisedListeners());
localData.setProtocols(protocolData);
localData.setBrokerVersionString(pulsar.getBrokerVersion());
// configure broker-topic mode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package org.apache.pulsar.broker.loadbalance.impl;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Set;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* to redirect the client to try again.
*/
public class LookupResult {
enum Type {
public enum Type {
BrokerUrl, RedirectUrl
}

Expand All @@ -49,6 +49,11 @@ public LookupResult(String httpUrl, String httpUrlTls, String brokerServiceUrl,
this.lookupData = new LookupData(brokerServiceUrl, brokerServiceUrlTls, httpUrl, httpUrlTls);
}

public LookupResult(String httpUrl, String httpUrlTls, String brokerServiceUrl, String brokerServiceUrlTls, Type type) {
this.type = type;
this.lookupData = new LookupData(brokerServiceUrl, brokerServiceUrlTls, httpUrl, httpUrlTls);
}

public boolean isBrokerUrl() {
return type == Type.BrokerUrl;
}
Expand Down
Loading