-
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
Removes bindOnLocalhost=boolean. Adds bindAddress and advertisedAddress. #26
Changes from 6 commits
c7e3a7a
cc08ed3
6fa126f
3745033
393e10e
3ed746e
1bec010
bf27260
368bb8d
10ccdb6
2a0920a
98f01cd
94dde8e
2b9c25c
18d3c91
2683759
1db4d34
e51b961
1ac44bf
d4c76f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,8 +37,11 @@ webServicePortTls=8443 | |
# Enable the WebSocket API service in broker | ||
webSocketServiceEnabled=false | ||
|
||
# Control whether to bind directly on localhost rather than on normal hostname | ||
bindOnLocalhost=false | ||
# Hostname or IP address the service binds on, default is InetAddress.getLocalHost().getHostName(). | ||
bindAddress= | ||
|
||
# Hostname or IP address the service advertises to the outside world. If not set, the value of bindAddress is used. | ||
advertisedAddress= | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leaving empty for clarity. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the bind address was There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed in 3745033 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update the comment with "default to hostname" |
||
|
||
# Name of the cluster to which this broker belongs to | ||
clusterName= | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,8 +27,11 @@ brokerServicePort=6650 | |
# Port to use to server HTTP request | ||
webServicePort=8080 | ||
|
||
# Control whether to bind directly on localhost rather than on normal hostname | ||
bindOnLocalhost=true | ||
# Hostname or IP address the service binds on, default is InetAddress.getLocalHost().getHostName(). | ||
bindAddress= | ||
|
||
# Hostname or IP address the service advertises to the outside world. If not set, the value of bindAddress is used. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. default to |
||
advertisedAddress= | ||
|
||
# Name of the cluster to which this broker belongs to | ||
clusterName=standalone | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,9 +43,13 @@ public class ServiceConfiguration { | |
private int webServicePort = 8080; | ||
// Port to use to server HTTPS request | ||
private int webServicePortTls = 8443; | ||
// Control whether to bind directly on localhost rather than on normal | ||
// hostname | ||
private boolean bindOnLocalhost = false; | ||
|
||
// Hostname or IP address the service binds on. | ||
// If not set, InetAddress.getLocalHost().getHostName() will be used. | ||
private String bindAddress; | ||
// Controls which hostname is advertised to the discovery service via ZooKeeper. | ||
// If not set, bindAddress is used. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the comment is out of date now, since it defaults to |
||
private String advertisedAddress; | ||
|
||
// Enable the WebSocket API service | ||
private boolean webSocketServiceEnabled = false; | ||
|
@@ -291,12 +295,20 @@ public void setWebServicePortTls(int webServicePortTls) { | |
this.webServicePortTls = webServicePortTls; | ||
} | ||
|
||
public boolean isBindOnLocalhost() { | ||
return bindOnLocalhost; | ||
public String getBindAddress() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Preferably, the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was trying to avoid There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, scratch that, that's a silly idea. I'll just re-throw the exception and handle where used. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed in 3745033 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @merlimat Why would multiple threads try to initialize a single instance of |
||
return this.bindAddress; | ||
} | ||
|
||
public void setBindAddress(String bindAddress) { | ||
this.bindAddress = bindAddress; | ||
} | ||
|
||
public String getAdvertisedAddress() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above, we should just return what was configured, and in any case, the fallback here should be on |
||
return this.advertisedAddress; | ||
} | ||
|
||
public void setBindOnLocalhost(boolean bindOnLocalhost) { | ||
this.bindOnLocalhost = bindOnLocalhost; | ||
public void setAdvertisedAddress(String advertisedAddress) { | ||
this.advertisedAddress = advertisedAddress; | ||
} | ||
|
||
public boolean isWebSocketServiceEnabled() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
import java.io.IOException; | ||
import java.net.InetAddress; | ||
import java.net.URL; | ||
import java.net.UnknownHostException; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.Executors; | ||
|
@@ -98,6 +99,7 @@ public class PulsarService implements AutoCloseable { | |
private PulsarAdmin adminClient = null; | ||
private ZooKeeperClientFactory zkClientFactory = null; | ||
private final String host; | ||
private final String advertisedAddress; | ||
private final String webServiceAddress; | ||
private final String webServiceAddressTls; | ||
private final String brokerServiceUrl; | ||
|
@@ -119,6 +121,7 @@ public enum State { | |
public PulsarService(ServiceConfiguration config) { | ||
state = State.Init; | ||
this.host = host(config); | ||
this.advertisedAddress = advertisedHost(config); | ||
this.webServiceAddress = webAddress(config); | ||
this.webServiceAddressTls = webAddressTls(config); | ||
this.brokerServiceUrl = brokerUrl(config); | ||
|
@@ -565,41 +568,56 @@ public MessagingServiceShutdownHook getShutdownService() { | |
/** | ||
* Derive the host | ||
* | ||
* @param isBindOnLocalhost | ||
* @return | ||
* @return Hostname or IP address the service binds on. | ||
*/ | ||
public static String host(ServiceConfiguration config) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @merlimat is it okay to change There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure For rebasing, I will squash it anyway when merging. I don't expect particular conflicts with current master. |
||
try { | ||
if (!config.isBindOnLocalhost()) { | ||
if (config.getBindAddress() == null) { | ||
try { | ||
return InetAddress.getLocalHost().getHostName(); | ||
} else { | ||
return "localhost"; | ||
} catch (UnknownHostException ex) { | ||
LOG.error(ex.getMessage(), ex); | ||
throw new IllegalStateException("Failed to resolve localhost name.", ex); | ||
} | ||
} catch (Exception e) { | ||
LOG.error(e.getMessage(), e); | ||
throw new IllegalStateException("failed to find host", e); | ||
} | ||
return config.getBindAddress(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These 2 methods should have the above logic to check whether bindAddress and advertisedAddress were configured. |
||
} | ||
|
||
/** | ||
* Advertised service host. | ||
* | ||
* @return Hostname or IP address the service advertises to the outside world. | ||
*/ | ||
public static String advertisedHost(ServiceConfiguration config) { | ||
if (config.getAdvertisedAddress() == null) { | ||
try { | ||
return InetAddress.getLocalHost().getHostName(); | ||
} catch (UnknownHostException ex) { | ||
LOG.error(ex.getMessage(), ex); | ||
throw new IllegalStateException("Failed to resolve localhost name.", ex); | ||
} | ||
} | ||
return config.getAdvertisedAddress(); | ||
} | ||
|
||
public static String brokerUrl(ServiceConfiguration config) { | ||
return "pulsar://" + host(config) + ":" + config.getBrokerServicePort(); | ||
return "pulsar://" + advertisedHost(config) + ":" + config.getBrokerServicePort(); | ||
} | ||
|
||
public static String brokerUrlTls(ServiceConfiguration config) { | ||
if (config.isTlsEnabled()) { | ||
return "pulsar://" + host(config) + ":" + config.getBrokerServicePortTls(); | ||
return "pulsar://" + advertisedHost(config) + ":" + config.getBrokerServicePortTls(); | ||
} else { | ||
return ""; | ||
} | ||
} | ||
|
||
public static String webAddress(ServiceConfiguration config) { | ||
return String.format("http://%s:%d", host(config), config.getWebServicePort()); | ||
return String.format("http://%s:%d", advertisedHost(config), config.getWebServicePort()); | ||
} | ||
|
||
public static String webAddressTls(ServiceConfiguration config) { | ||
if (config.isTlsEnabled()) { | ||
return String.format("https://%s:%d", host(config), config.getWebServicePortTls()); | ||
return String.format("https://%s:%d", advertisedHost(config), config.getWebServicePortTls()); | ||
} else { | ||
return ""; | ||
} | ||
|
@@ -609,6 +627,10 @@ public String getHost() { | |
return host; | ||
} | ||
|
||
public String getAdvertisedAddress() { | ||
return advertisedAddress; | ||
} | ||
|
||
public String getWebServiceAddress() { | ||
return webServiceAddress; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* Copyright 2016 Yahoo Inc. | ||
* | ||
* Licensed 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 com.yahoo.pulsar.broker.service; | ||
|
||
import com.yahoo.pulsar.broker.PulsarService; | ||
import com.yahoo.pulsar.broker.ServiceConfiguration; | ||
import com.yahoo.pulsar.zookeeper.LocalBookkeeperEnsemble; | ||
import org.apache.zookeeper.data.Stat; | ||
import org.json.JSONObject; | ||
import org.json.JSONStringer; | ||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.testng.annotations.AfterMethod; | ||
import org.testng.annotations.BeforeMethod; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.Properties; | ||
|
||
import static com.yahoo.pulsar.broker.ServiceConfigurationLoader.create; | ||
|
||
public class AdvertisedAddressTest { | ||
|
||
LocalBookkeeperEnsemble bkEnsemble; | ||
PulsarService pulsar; | ||
|
||
private final int ZOOKEEPER_PORT = 12759; | ||
private final int BROKER_WEBSERVICE_PORT = 15782; | ||
private final int BROKER_SERVICE_PORT = 16650; | ||
|
||
private final String bindAddress = "127.0.0.1"; | ||
private final String advertisedAddress = "pulsar-usc.example.com"; | ||
|
||
@Before | ||
public void setup() throws Exception { | ||
bkEnsemble = new LocalBookkeeperEnsemble(3, ZOOKEEPER_PORT, 5001); | ||
bkEnsemble.start(); | ||
ServiceConfiguration config = create(new Properties(System.getProperties())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as we are not loading There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed. |
||
config.setZookeeperServers("127.0.0.1" + ":" + ZOOKEEPER_PORT); | ||
config.setWebServicePort(BROKER_WEBSERVICE_PORT); | ||
config.setClusterName("usc"); | ||
config.setBrokerServicePort(BROKER_SERVICE_PORT); | ||
config.setBindAddress(bindAddress); | ||
config.setAdvertisedAddress(advertisedAddress); | ||
config.setManagedLedgerMaxEntriesPerLedger(5); | ||
config.setManagedLedgerMinLedgerRolloverTimeMinutes(0); | ||
pulsar = new PulsarService(config); | ||
pulsar.start(); | ||
} | ||
|
||
@After | ||
public void shutdown() throws Exception { | ||
pulsar.close(); | ||
bkEnsemble.stop(); | ||
} | ||
|
||
@Test | ||
public void testAdvertisedAddress() throws Exception { | ||
Assert.assertEquals( pulsar.getHost(), bindAddress ); | ||
Assert.assertEquals( pulsar.getAdvertisedAddress(), advertisedAddress ); | ||
Assert.assertEquals( pulsar.getBrokerServiceUrl(), String.format("pulsar://%s:%d", advertisedAddress, BROKER_SERVICE_PORT) ); | ||
Assert.assertEquals( pulsar.getWebServiceAddress(), String.format("http://%s:%d", advertisedAddress, BROKER_WEBSERVICE_PORT) ); | ||
String brokerZkPath = String.format("/loadbalance/brokers/%s:%d", bindAddress, BROKER_WEBSERVICE_PORT); | ||
String bkBrokerData = new String(bkEnsemble.getZkClient().getData(brokerZkPath, false, new Stat()), StandardCharsets.UTF_8); | ||
JSONObject jsonBkBrokerData = new JSONObject(bkBrokerData); | ||
Assert.assertEquals( jsonBkBrokerData.get("pulsarServiceUrl"), pulsar.getBrokerServiceUrl() ); | ||
Assert.assertEquals( jsonBkBrokerData.get("webServiceUrl"), pulsar.getWebServiceAddress() ); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,8 @@ brokerServicePort=6650 | |
brokerServicePortTls=6651 | ||
webServicePort=8080 | ||
webServicePortTls=4443 | ||
bindOnLocalhost=false | ||
bindAddress= | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Left empty for clarity. |
||
advertisedAddress= | ||
clusterName="test_cluster" | ||
brokerShutdownTimeoutMs=3000 | ||
backlogQuotaCheckEnabled=true | ||
|
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.
Leaving empty for clarity.
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.
Shouldn't this be 0.0.0.0 instead of
hostname
by default?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.
I guess that's a personal preference. Some people like things wide open by default, some do not. I don't mind making it
0.0.0.0
.