use of org.elasticsearch.common.component.Lifecycle in project crate by crate.
the class TcpTransport method bindToPort.
private InetSocketAddress bindToPort(final String name, final InetAddress hostAddress, String port) {
PortsRange portsRange = new PortsRange(port);
final AtomicReference<Exception> lastException = new AtomicReference<>();
final AtomicReference<InetSocketAddress> boundSocket = new AtomicReference<>();
closeLock.writeLock().lock();
try {
// No need for locking here since Lifecycle objects can't move from STARTED to INITIALIZED
if (lifecycle.initialized() == false && lifecycle.started() == false) {
throw new IllegalStateException("transport has been stopped");
}
boolean success = portsRange.iterate(portNumber -> {
try {
TcpServerChannel channel = bind(name, new InetSocketAddress(hostAddress, portNumber));
serverChannels.computeIfAbsent(name, k -> new ArrayList<>()).add(channel);
boundSocket.set(channel.getLocalAddress());
} catch (Exception e) {
lastException.set(e);
return false;
}
return true;
});
if (!success) {
throw new BindTransportException("Failed to bind to " + NetworkAddress.format(hostAddress, portsRange), lastException.get());
}
} finally {
closeLock.writeLock().unlock();
}
if (logger.isDebugEnabled()) {
logger.debug("Bound profile [{}] to address {{}}", name, NetworkAddress.format(boundSocket.get()));
}
return boundSocket.get();
}
use of org.elasticsearch.common.component.Lifecycle in project crate by crate.
the class TcpTransportTests method testExceptionHandling.
private void testExceptionHandling(boolean startTransport, Exception exception, boolean expectClosed, MockLogAppender.LoggingExpectation... expectations) throws IllegalAccessException {
final TestThreadPool testThreadPool = new TestThreadPool("test");
MockLogAppender appender = new MockLogAppender();
try {
appender.start();
Loggers.addAppender(LogManager.getLogger(TcpTransport.class), appender);
for (MockLogAppender.LoggingExpectation expectation : expectations) {
appender.addExpectation(expectation);
}
final Lifecycle lifecycle = new Lifecycle();
if (startTransport) {
lifecycle.moveToStarted();
}
final FakeTcpChannel channel = new FakeTcpChannel();
final PlainActionFuture<Void> listener = new PlainActionFuture<>();
channel.addCloseListener(listener);
var logger = Loggers.getLogger(TcpTransport.class);
TcpTransport.handleException(logger, channel, exception, lifecycle, new OutboundHandler(randomAlphaOfLength(10), Version.CURRENT, new StatsTracker(), testThreadPool, BigArrays.NON_RECYCLING_INSTANCE));
if (expectClosed) {
assertTrue(listener.isDone());
assertThat(listener.actionGet(), nullValue());
} else {
assertFalse(listener.isDone());
}
appender.assertAllExpectationsMatched();
} finally {
Loggers.removeAppender(LogManager.getLogger(TcpTransport.class), appender);
appender.stop();
ThreadPool.terminate(testThreadPool, 30, TimeUnit.SECONDS);
}
}
Aggregations