use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project herddb by diennea.
the class NetworkChannelTest method testCloseServer.
@Test
public void testCloseServer() throws Exception {
try (NettyChannelAcceptor server = new NettyChannelAcceptor("localhost", NetworkUtils.assignFirstFreePort(), true)) {
server.setEnableJVMNetwork(false);
server.setEnableRealNetwork(true);
server.setAcceptor((Channel channel) -> {
channel.setMessagesReceiver(new ChannelEventListener() {
});
return (ServerSideConnection) () -> new Random().nextLong();
});
server.start();
ExecutorService executor = Executors.newCachedThreadPool();
AtomicBoolean closeNotificationReceived = new AtomicBoolean();
try (Channel client = NettyConnector.connect(server.getHost(), server.getPort(), true, 0, 0, new ChannelEventListener() {
@Override
public void channelClosed(Channel channel) {
System.out.println("client channelClosed");
closeNotificationReceived.set(true);
}
}, executor, new NioEventLoopGroup(10, executor))) {
// closing the server should eventually close the client
server.close();
TestUtils.waitForCondition(() -> closeNotificationReceived.get(), NOOP, 100);
assertTrue(closeNotificationReceived.get());
} finally {
executor.shutdown();
}
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project ballerina by ballerina-lang.
the class WebSocketClient method handhshake.
/**
* @return true if the handshake is done properly.
* @throws URISyntaxException throws if there is an error in the URI syntax.
* @throws InterruptedException throws if the connecting the server is interrupted.
*/
public boolean handhshake() throws InterruptedException, URISyntaxException, SSLException {
boolean isDone = false;
URI uri = new URI(url);
String scheme = uri.getScheme() == null ? "ws" : uri.getScheme();
final String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost();
final int port;
if (uri.getPort() == -1) {
if ("ws".equalsIgnoreCase(scheme)) {
port = 80;
} else if ("wss".equalsIgnoreCase(scheme)) {
port = 443;
} else {
port = -1;
}
} else {
port = uri.getPort();
}
if (!"ws".equalsIgnoreCase(scheme) && !"wss".equalsIgnoreCase(scheme)) {
logger.error("Only WS(S) is supported.");
return false;
}
final boolean ssl = "wss".equalsIgnoreCase(scheme);
final SslContext sslCtx;
if (ssl) {
sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
} else {
sslCtx = null;
}
group = new NioEventLoopGroup();
DefaultHttpHeaders httpHeaders = new DefaultHttpHeaders();
headers.entrySet().forEach(header -> {
httpHeaders.add(header.getKey(), header.getValue());
});
try {
// Connect with V13 (RFC 6455 aka HyBi-17). You can change it to V08 or V00.
// If you change it to V00, ping is not supported and remember to change
// HttpResponseDecoder to WebSocketHttpResponseDecoder in the pipeline.
handler = new WebSocketClientHandler(WebSocketClientHandshakerFactory.newHandshaker(uri, WebSocketVersion.V13, null, true, httpHeaders));
Bootstrap b = new Bootstrap();
b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) {
ChannelPipeline p = ch.pipeline();
if (sslCtx != null) {
p.addLast(sslCtx.newHandler(ch.alloc(), host, port));
}
p.addLast(new HttpClientCodec(), new HttpObjectAggregator(8192), WebSocketClientCompressionHandler.INSTANCE, handler);
}
});
channel = b.connect(uri.getHost(), port).sync().channel();
isDone = handler.handshakeFuture().sync().isSuccess();
} catch (Exception e) {
logger.error("Handshake unsuccessful : " + e.getMessage(), e);
return false;
}
logger.info("WebSocket Handshake successful : " + isDone);
return isDone;
}
use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project bookkeeper by apache.
the class BookieClientTest method testGetBookieInfo.
@Test
public void testGetBookieInfo() throws IOException, InterruptedException {
BookieSocketAddress addr = bs.getLocalAddress();
BookieClient bc = new BookieClient(new ClientConfiguration(), new NioEventLoopGroup(), executor, scheduler, NullStatsLogger.INSTANCE);
long flags = BookkeeperProtocol.GetBookieInfoRequest.Flags.FREE_DISK_SPACE_VALUE | BookkeeperProtocol.GetBookieInfoRequest.Flags.TOTAL_DISK_CAPACITY_VALUE;
class CallbackObj {
int rc;
long requested;
long freeDiskSpace, totalDiskCapacity;
CountDownLatch latch = new CountDownLatch(1);
CallbackObj(long requested) {
this.requested = requested;
this.rc = 0;
this.freeDiskSpace = 0L;
this.totalDiskCapacity = 0L;
}
}
CallbackObj obj = new CallbackObj(flags);
bc.getBookieInfo(addr, flags, new GetBookieInfoCallback() {
@Override
public void getBookieInfoComplete(int rc, BookieInfo bInfo, Object ctx) {
CallbackObj obj = (CallbackObj) ctx;
obj.rc = rc;
if (rc == Code.OK) {
if ((obj.requested & BookkeeperProtocol.GetBookieInfoRequest.Flags.FREE_DISK_SPACE_VALUE) != 0) {
obj.freeDiskSpace = bInfo.getFreeDiskSpace();
}
if ((obj.requested & BookkeeperProtocol.GetBookieInfoRequest.Flags.TOTAL_DISK_CAPACITY_VALUE) != 0) {
obj.totalDiskCapacity = bInfo.getTotalDiskSpace();
}
}
obj.latch.countDown();
}
}, obj);
obj.latch.await();
System.out.println("Return code: " + obj.rc + "FreeDiskSpace: " + obj.freeDiskSpace + " TotalCapacity: " + obj.totalDiskCapacity);
assertTrue("GetBookieInfo failed with error " + obj.rc, obj.rc == Code.OK);
assertTrue("GetBookieInfo failed with error " + obj.rc, obj.freeDiskSpace <= obj.totalDiskCapacity);
assertTrue("GetBookieInfo failed with error " + obj.rc, obj.totalDiskCapacity > 0);
}
use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project bookkeeper by apache.
the class TestPerChannelBookieClient method testConnectCloseRace.
/**
* Test that a race does not exist between connection completion
* and client closure. If a race does exist, this test will simply
* hang at releaseExternalResources() as it is uninterruptible.
* This specific race was found in
* {@link https://issues.apache.org/jira/browse/BOOKKEEPER-485}.
*/
@Test
public void testConnectCloseRace() throws Exception {
EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
OrderedExecutor executor = getOrderedSafeExecutor();
BookieSocketAddress addr = getBookie(0);
for (int i = 0; i < 1000; i++) {
PerChannelBookieClient client = new PerChannelBookieClient(executor, eventLoopGroup, addr, authProvider, extRegistry);
client.connectIfNeededAndDoOp(new GenericCallback<PerChannelBookieClient>() {
@Override
public void operationComplete(int rc, PerChannelBookieClient client) {
// do nothing, we don't care about doing anything with the connection,
// we just want to trigger it connecting.
}
});
client.close();
}
eventLoopGroup.shutdownGracefully();
executor.shutdown();
}
use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project bookkeeper by apache.
the class TestPerChannelBookieClient method testDisconnectRace.
/**
* Test that all resources are freed if connections and disconnections
* are interleaved randomly.
*
* {@link https://issues.apache.org/jira/browse/BOOKKEEPER-620}
*/
@Test
public void testDisconnectRace() throws Exception {
final GenericCallback<PerChannelBookieClient> nullop = new GenericCallback<PerChannelBookieClient>() {
@Override
public void operationComplete(int rc, PerChannelBookieClient client) {
// do nothing, we don't care about doing anything with the connection,
// we just want to trigger it connecting.
}
};
final int iterations = 100000;
EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
OrderedExecutor executor = getOrderedSafeExecutor();
BookieSocketAddress addr = getBookie(0);
final PerChannelBookieClient client = new PerChannelBookieClient(executor, eventLoopGroup, addr, authProvider, extRegistry);
final AtomicBoolean shouldFail = new AtomicBoolean(false);
final AtomicBoolean running = new AtomicBoolean(true);
final CountDownLatch disconnectRunning = new CountDownLatch(1);
Thread connectThread = new Thread() {
public void run() {
try {
if (!disconnectRunning.await(10, TimeUnit.SECONDS)) {
LOG.error("Disconnect thread never started");
shouldFail.set(true);
}
} catch (InterruptedException ie) {
LOG.error("Connect thread interrupted", ie);
Thread.currentThread().interrupt();
running.set(false);
}
for (int i = 0; i < iterations && running.get(); i++) {
client.connectIfNeededAndDoOp(nullop);
}
running.set(false);
}
};
Thread disconnectThread = new Thread() {
public void run() {
disconnectRunning.countDown();
while (running.get()) {
client.disconnect();
}
}
};
Thread checkThread = new Thread() {
public void run() {
ConnectionState state;
Channel channel;
while (running.get()) {
synchronized (client) {
state = client.state;
channel = client.channel;
if ((state == ConnectionState.CONNECTED && (channel == null || !channel.isActive())) || (state != ConnectionState.CONNECTED && channel != null && channel.isActive())) {
LOG.error("State({}) and channel({}) inconsistent " + channel, state, channel == null ? null : channel.isActive());
shouldFail.set(true);
running.set(false);
}
}
}
}
};
connectThread.start();
disconnectThread.start();
checkThread.start();
connectThread.join();
disconnectThread.join();
checkThread.join();
assertFalse("Failure in threads, check logs", shouldFail.get());
client.close();
eventLoopGroup.shutdownGracefully();
executor.shutdown();
}
Aggregations