-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Changes from 16 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
be868bb
PIP-61:
6303ad4
PIP-61:
9dbcebf
PIP-61:
0789052
PIP-61:
25df9e9
Merge branch 'master' into feature/pip-61
9fa3fde
PIP-61:
ca0d70e
PIP-61:
38021b0
PIP-61:
25e4c13
PIP-61:
08dbd6a
PIP-61:
8dd83ef
PIP-61:
f929d9e
PIP-61:
da069af
PIP-61:
c39a06d
fix code by review
8b306d4
Merge branch 'master' into feature/pip-61
zplinuxlover f381c69
Merge remote-tracking branch 'origin/master' into feature/pip-61
159ec9d
fix code by review
5bfdc16
Merge branch 'master' into feature/pip-61
codelipenghui 57100be
remove unused import in Commands.java file.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
137 changes: 137 additions & 0 deletions
137
...er-common/src/main/java/org/apache/pulsar/broker/validator/MultipleListenerValidator.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,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; | ||
} | ||
|
||
} |
100 changes: 100 additions & 0 deletions
100
...ommon/src/test/java/org/apache/pulsar/broker/validator/MultipleListenerValidatorTest.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,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); | ||
} | ||
|
||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid use import .*
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolve the problem