use of org.apache.flink.runtime.query.KvStateID in project flink by apache.
the class KvStateServerHandlerTest method testIncomingBufferIsRecycled.
/**
* Tests that incoming buffer instances are recycled.
*/
@Test
public void testIncomingBufferIsRecycled() throws Exception {
KvStateRegistry registry = new KvStateRegistry();
AtomicKvStateRequestStats stats = new AtomicKvStateRequestStats();
KvStateServerHandler handler = new KvStateServerHandler(registry, TEST_THREAD_POOL, stats);
EmbeddedChannel channel = new EmbeddedChannel(getFrameDecoder(), handler);
ByteBuf request = KvStateRequestSerializer.serializeKvStateRequest(channel.alloc(), 282872, new KvStateID(), new byte[0]);
assertEquals(1, request.refCnt());
// Write regular request
channel.writeInbound(request);
assertEquals("Buffer not recycled", 0, request.refCnt());
// Write unexpected msg
ByteBuf unexpected = channel.alloc().buffer(8);
unexpected.writeInt(4);
unexpected.writeInt(4);
assertEquals(1, unexpected.refCnt());
channel.writeInbound(unexpected);
assertEquals("Buffer not recycled", 0, unexpected.refCnt());
}
use of org.apache.flink.runtime.query.KvStateID in project flink by apache.
the class KvStateRequestSerializerTest method testKvStateRequestSerialization.
/**
* Tests KvState request serialization.
*/
@Test
public void testKvStateRequestSerialization() throws Exception {
long requestId = Integer.MAX_VALUE + 1337L;
KvStateID kvStateId = new KvStateID();
byte[] serializedKeyAndNamespace = randomByteArray(1024);
ByteBuf buf = KvStateRequestSerializer.serializeKvStateRequest(alloc, requestId, kvStateId, serializedKeyAndNamespace);
int frameLength = buf.readInt();
assertEquals(KvStateRequestType.REQUEST, KvStateRequestSerializer.deserializeHeader(buf));
KvStateRequest request = KvStateRequestSerializer.deserializeKvStateRequest(buf);
assertEquals(buf.readerIndex(), frameLength + 4);
assertEquals(requestId, request.getRequestId());
assertEquals(kvStateId, request.getKvStateId());
assertArrayEquals(serializedKeyAndNamespace, request.getSerializedKeyAndNamespace());
}
use of org.apache.flink.runtime.query.KvStateID in project flink by apache.
the class KvStateClientTest method testFailureClosesChannel.
/**
* Tests that a server failure closes the connection and removes it from
* the established connections.
*/
@Test
public void testFailureClosesChannel() throws Exception {
Deadline deadline = TEST_TIMEOUT.fromNow();
AtomicKvStateRequestStats stats = new AtomicKvStateRequestStats();
KvStateClient client = null;
Channel serverChannel = null;
try {
client = new KvStateClient(1, stats);
final LinkedBlockingQueue<ByteBuf> received = new LinkedBlockingQueue<>();
final AtomicReference<Channel> channel = new AtomicReference<>();
serverChannel = createServerChannel(new ChannelInboundHandlerAdapter() {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
channel.set(ctx.channel());
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
received.add((ByteBuf) msg);
}
});
KvStateServerAddress serverAddress = getKvStateServerAddress(serverChannel);
// Requests
List<Future<byte[]>> futures = new ArrayList<>();
futures.add(client.getKvState(serverAddress, new KvStateID(), new byte[0]));
futures.add(client.getKvState(serverAddress, new KvStateID(), new byte[0]));
ByteBuf buf = received.poll(deadline.timeLeft().toMillis(), TimeUnit.MILLISECONDS);
assertNotNull("Receive timed out", buf);
buf.release();
buf = received.poll(deadline.timeLeft().toMillis(), TimeUnit.MILLISECONDS);
assertNotNull("Receive timed out", buf);
buf.release();
assertEquals(1, stats.getNumConnections());
Channel ch = channel.get();
assertNotNull("Channel not active", ch);
// Respond with failure
ch.writeAndFlush(KvStateRequestSerializer.serializeServerFailure(serverChannel.alloc(), new RuntimeException("Expected test server failure")));
try {
Await.result(futures.remove(0), deadline.timeLeft());
fail("Did not throw expected server failure");
} catch (RuntimeException ignored) {
// Expected
}
try {
Await.result(futures.remove(0), deadline.timeLeft());
fail("Did not throw expected server failure");
} catch (RuntimeException ignored) {
// Expected
}
assertEquals(0, stats.getNumConnections());
// Counts can take some time to propagate
while (deadline.hasTimeLeft() && (stats.getNumSuccessful() != 0 || stats.getNumFailed() != 2)) {
Thread.sleep(100);
}
assertEquals(2, stats.getNumRequests());
assertEquals(0, stats.getNumSuccessful());
assertEquals(2, stats.getNumFailed());
} finally {
if (client != null) {
client.shutDown();
}
if (serverChannel != null) {
serverChannel.close();
}
assertEquals("Channel leak", 0, stats.getNumConnections());
}
}
Aggregations