Skip to content

Commit 07a0b5b

Browse files
committed
Add host address to BindTransportException message
When bind fails, show the host address in addition to the port. This helps debugging cases with wrong "network.host" values. Closes elastic#48001
1 parent 4a8e5ad commit 07a0b5b

File tree

5 files changed

+27
-7
lines changed

5 files changed

+27
-7
lines changed

modules/transport-netty4/src/test/java/org/elasticsearch/http/netty4/Netty4HttpServerTransportTests.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,17 @@ public void testBindUnavailableAddress() {
195195
xContentRegistry(), new NullDispatcher())) {
196196
transport.start();
197197
TransportAddress remoteAddress = randomFrom(transport.boundAddress().boundAddresses());
198-
Settings settings = Settings.builder().put("http.port", remoteAddress.getPort()).build();
198+
Settings settings = Settings.builder()
199+
.put("http.port", remoteAddress.getPort())
200+
.put("network.host", remoteAddress.getAddress())
201+
.build();
199202
try (Netty4HttpServerTransport otherTransport = new Netty4HttpServerTransport(settings, networkService, bigArrays, threadPool,
200203
xContentRegistry(), new NullDispatcher())) {
201204
BindHttpException bindHttpException = expectThrows(BindHttpException.class, otherTransport::start);
202-
assertEquals("Failed to bind to [" + remoteAddress.getPort() + "]", bindHttpException.getMessage());
205+
assertEquals(
206+
"Failed to bind to [" + remoteAddress.getPort() + "] on " + remoteAddress.address().getAddress().getHostAddress(),
207+
bindHttpException.getMessage()
208+
);
203209
}
204210
}
205211
}

plugins/transport-nio/src/test/java/org/elasticsearch/http/nio/NioHttpServerTransportTests.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,17 @@ public void testBindUnavailableAddress() {
190190
threadPool, xContentRegistry(), new NullDispatcher(), new NioGroupFactory(Settings.EMPTY, logger))) {
191191
transport.start();
192192
TransportAddress remoteAddress = randomFrom(transport.boundAddress().boundAddresses());
193-
Settings settings = Settings.builder().put("http.port", remoteAddress.getPort()).build();
193+
Settings settings = Settings.builder()
194+
.put("http.port", remoteAddress.getPort())
195+
.put("network.host", remoteAddress.getAddress())
196+
.build();
194197
try (NioHttpServerTransport otherTransport = new NioHttpServerTransport(settings, networkService, bigArrays, pageRecycler,
195198
threadPool, xContentRegistry(), new NullDispatcher(), new NioGroupFactory(Settings.EMPTY, logger))) {
196199
BindHttpException bindHttpException = expectThrows(BindHttpException.class, () -> otherTransport.start());
197-
assertEquals("Failed to bind to [" + remoteAddress.getPort() + "]", bindHttpException.getMessage());
200+
assertEquals(
201+
"Failed to bind to [" + remoteAddress.getPort() + "] on " + remoteAddress.address().getAddress().getHostAddress(),
202+
bindHttpException.getMessage()
203+
);
198204
}
199205
}
200206
}

server/src/main/java/org/elasticsearch/http/AbstractHttpServerTransport.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,10 @@ private TransportAddress bindAddress(final InetAddress hostAddress) {
174174
return true;
175175
});
176176
if (!success) {
177-
throw new BindHttpException("Failed to bind to [" + port.getPortRangeString() + "]", lastException.get());
177+
throw new BindHttpException(
178+
"Failed to bind to [" + port.getPortRangeString() + "] on " + hostAddress.getHostAddress(),
179+
lastException.get()
180+
);
178181
}
179182

180183
if (logger.isDebugEnabled()) {

server/src/main/java/org/elasticsearch/transport/TcpTransport.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,10 @@ private InetSocketAddress bindToPort(final String name, final InetAddress hostAd
377377
return true;
378378
});
379379
if (!success) {
380-
throw new BindTransportException("Failed to bind to [" + port + "]", lastException.get());
380+
throw new BindTransportException(
381+
"Failed to bind to [" + port + "] on " + hostAddress.getHostAddress(),
382+
lastException.get()
383+
);
381384
}
382385
} finally {
383386
closeLock.writeLock().unlock();

test/framework/src/main/java/org/elasticsearch/transport/AbstractSimpleTransportTestCase.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -2698,13 +2698,15 @@ public void testProfilesIncludesDefault() {
26982698

26992699
public void testBindUnavailableAddress() {
27002700
int port = serviceA.boundAddress().publishAddress().getPort();
2701+
String address = serviceA.boundAddress().publishAddress().getAddress();
27012702
Settings settings = Settings.builder()
27022703
.put(Node.NODE_NAME_SETTING.getKey(), "foobar")
2704+
.put(TransportSettings.HOST.getKey(), address)
27032705
.put(TransportSettings.PORT.getKey(), port)
27042706
.build();
27052707
BindTransportException bindTransportException = expectThrows(BindTransportException.class,
27062708
() -> buildService("test", Version.CURRENT, settings));
2707-
assertEquals("Failed to bind to ["+ port + "]", bindTransportException.getMessage());
2709+
assertEquals("Failed to bind to [" + port + "] on " + address, bindTransportException.getMessage());
27082710
}
27092711

27102712
public void testChannelCloseWhileConnecting() {

0 commit comments

Comments
 (0)