@Override public CompletableFuture<Void> shutdown() { return kvStateClient.shutdown(); } }
@Override public ByteBuf readChunk(ByteBufAllocator byteBufAllocator) throws Exception { return readChunk(); }
@Override public void start() throws Throwable { super.start(); }
private static Client<KvStateInternalRequest, KvStateResponse> createInternalClient(int threads) { final MessageSerializer<KvStateInternalRequest, KvStateResponse> messageSerializer = new MessageSerializer<>( new KvStateInternalRequest.KvStateInternalRequestDeserializer(), new KvStateResponse.KvStateResponseDeserializer()); return new Client<>( "Queryable State Proxy Client", threads, messageSerializer, new DisabledKvStateRequestStats()); }
@Override public AbstractServerHandler<TestMessage, TestMessage> initializeHandler() { return new AbstractServerHandler<TestMessage, TestMessage>( this, new MessageSerializer<>(new TestMessage.TestMessageDeserializer(), new TestMessage.TestMessageDeserializer()), requestStats) { @Override public CompletableFuture<TestMessage> handleRequest(long requestId, TestMessage request) { TestMessage response = new TestMessage(getServerName() + '-' + request.getMessage()); return CompletableFuture.completedFuture(response); } @Override public CompletableFuture<Void> shutdown() { return CompletableFuture.completedFuture(null); } }; }
@Override public void close() throws Exception { shutdownServer().get(); if (requestStats instanceof AtomicKvStateRequestStats) { AtomicKvStateRequestStats stats = (AtomicKvStateRequestStats) requestStats; Assert.assertEquals(0L, stats.getNumConnections()); } Assert.assertTrue(getQueryExecutor().isTerminated()); Assert.assertTrue(isEventGroupShutdown()); } }
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { try { callback.onFailure(cause); } catch (Throwable t) { LOG.error("Failed to notify callback about failure", t); } }
@Override public CompletableFuture<TestMessage> handleRequest(long requestId, TestMessage request) { TestMessage response = new TestMessage(getServerName() + '-' + request.getMessage()); return CompletableFuture.completedFuture(response); }
protected String getServerName() { return server.getServerName(); }
@Override public void close() throws Exception { shutdown().join(); Assert.assertTrue(isEventGroupShutdown()); } }
/** * Create the handler. * * @param serializer the serializer used to (de-)serialize messages * @param stats statistics collector */ public AbstractServerHandler( final AbstractServerBase<REQ, RESP> server, final MessageSerializer<REQ, RESP> serializer, final KvStateRequestStats stats) { this.server = Preconditions.checkNotNull(server); this.serializer = Preconditions.checkNotNull(serializer); this.queryExecutor = server.getQueryExecutor(); this.stats = Preconditions.checkNotNull(stats); }
@Override public InetSocketAddress getServerAddress() { return super.getServerAddress(); }
@Override public TestMessage deserializeMessage(ByteBuf buf) { int length = buf.readInt(); String message = ""; if (length > 0) { byte[] name = new byte[length]; buf.readBytes(name); message = new String(name, ConfigConstants.DEFAULT_CHARSET); } return new TestMessage(message); } }
/** * Shuts down the client and returns a {@link CompletableFuture} that * will be completed when the shutdown process is completed. * * <p>If an exception is thrown for any reason, then the returned future * will be completed exceptionally with that exception. * * @return A {@link CompletableFuture} for further handling of the * shutdown result. */ public CompletableFuture<?> shutdownAndHandle() { return client.shutdown(); }
@Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { // Only the client is expected to close the channel. Otherwise it // indicates a failure. Note that this will be invoked in both cases // though. If the callback closed the channel, the callback must be // ignored. try { callback.onFailure(new ClosedChannelException()); } catch (Throwable t) { LOG.error("Failed to notify callback about failure", t); } } }
/** * Creates a thread pool for the query execution. * @return Thread pool for query execution */ private ExecutorService createQueryExecutor() { ThreadFactory threadFactory = new ThreadFactoryBuilder() .setDaemon(true) .setNameFormat("Flink " + getServerName() + " Thread %d") .build(); return Executors.newFixedThreadPool(numQueryThreads, threadFactory); }
@Override public ByteBuf readChunk(ChannelHandlerContext ctx) throws Exception { return readChunk(); }
@Override public void start() throws Throwable { super.start(); }
@Override public InetSocketAddress getServerAddress() { return super.getServerAddress(); }
/** * Shuts down the client and waits until shutdown is completed. * * <p>If an exception is thrown, a warning is logged containing * the exception message. */ public void shutdownAndWait() { try { client.shutdown().get(); LOG.info("The Queryable State Client was shutdown successfully."); } catch (Exception e) { LOG.warn("The Queryable State Client shutdown failed: ", e); } }