Java Code Examples for org.apache.flink.queryablestate.client.state.serialization.KvStateSerializer#deserializeValue()

The following examples show how to use org.apache.flink.queryablestate.client.state.serialization.KvStateSerializer#deserializeValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KvStateRequestSerializerTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Tests value serialization utils.
 */
@Test
public void testValueSerialization() throws Exception {
	TypeSerializer<Long> valueSerializer = LongSerializer.INSTANCE;
	long expectedValue = Long.MAX_VALUE - 1292929292L;

	byte[] serializedValue = KvStateSerializer.serializeValue(expectedValue, valueSerializer);
	long actualValue = KvStateSerializer.deserializeValue(serializedValue, valueSerializer);

	assertEquals(expectedValue, actualValue);
}
 
Example 2
Source File: ImmutableValueState.java    From flink with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <V, S extends State> S createState(
	StateDescriptor<S, V> stateDescriptor,
	byte[] serializedState) throws IOException {
	final V state = KvStateSerializer.deserializeValue(
		serializedState,
		stateDescriptor.getSerializer());
	return (S) new ImmutableValueState<>(state);
}
 
Example 3
Source File: ImmutableReducingState.java    From flink with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <V, S extends State> S createState(
	StateDescriptor<S, V> stateDescriptor,
	byte[] serializedState) throws IOException {
	final V state = KvStateSerializer.deserializeValue(
		serializedState,
		stateDescriptor.getSerializer());
	return (S) new ImmutableReducingState<>(state);
}
 
Example 4
Source File: ImmutableAggregatingState.java    From flink with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <OUT, ACC, S extends State> S createState(
	StateDescriptor<S, ACC> stateDescriptor,
	byte[] serializedState) throws IOException {
	final ACC accumulator = KvStateSerializer.deserializeValue(
		serializedState,
		stateDescriptor.getSerializer());
	final OUT state = ((AggregatingStateDescriptor<?, ACC, OUT>) stateDescriptor).
		getAggregateFunction().getResult(accumulator);
	return (S) new ImmutableAggregatingState<>(state);
}
 
Example 5
Source File: KvStateRequestSerializerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Tests value deserialization with too many bytes.
 */
@Test(expected = IOException.class)
public void testDeserializeValueTooMany2() throws Exception {
	// Long + 2 bytes
	KvStateSerializer.deserializeValue(new byte[] {1, 1, 1, 1, 1, 1, 1, 1, 2, 2},
		LongSerializer.INSTANCE);
}
 
Example 6
Source File: KvStateRequestSerializerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Tests value deserialization with too many bytes.
 */
@Test(expected = IOException.class)
public void testDeserializeValueTooMany2() throws Exception {
	// Long + 2 bytes
	KvStateSerializer.deserializeValue(new byte[] {1, 1, 1, 1, 1, 1, 1, 1, 2, 2},
		LongSerializer.INSTANCE);
}
 
Example 7
Source File: ImmutableFoldingState.java    From flink with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <ACC, S extends State> S createState(
	StateDescriptor<S, ACC> stateDescriptor,
	byte[] serializedState) throws IOException {
	final ACC state = KvStateSerializer.deserializeValue(
		serializedState,
		stateDescriptor.getSerializer());
	return (S) new ImmutableFoldingState<>(state);
}
 
Example 8
Source File: ImmutableFoldingState.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <ACC, S extends State> S createState(
	StateDescriptor<S, ACC> stateDescriptor,
	byte[] serializedState) throws IOException {
	final ACC state = KvStateSerializer.deserializeValue(
		serializedState,
		stateDescriptor.getSerializer());
	return (S) new ImmutableFoldingState<>(state);
}
 
Example 9
Source File: ImmutableValueState.java    From flink with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <V, S extends State> S createState(
	StateDescriptor<S, V> stateDescriptor,
	byte[] serializedState) throws IOException {
	final V state = KvStateSerializer.deserializeValue(
		serializedState,
		stateDescriptor.getSerializer());
	return (S) new ImmutableValueState<>(state);
}
 
Example 10
Source File: ImmutableReducingState.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <V, S extends State> S createState(
	StateDescriptor<S, V> stateDescriptor,
	byte[] serializedState) throws IOException {
	final V state = KvStateSerializer.deserializeValue(
		serializedState,
		stateDescriptor.getSerializer());
	return (S) new ImmutableReducingState<>(state);
}
 
Example 11
Source File: KvStateRequestSerializerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Tests value deserialization with too many bytes.
 */
@Test(expected = IOException.class)
public void testDeserializeValueTooMany1() throws Exception {
	// Long + 1 byte
	KvStateSerializer.deserializeValue(new byte[] {1, 1, 1, 1, 1, 1, 1, 1, 2},
		LongSerializer.INSTANCE);
}
 
Example 12
Source File: KvStateRequestSerializerTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Tests value deserialization with too many bytes.
 */
@Test(expected = IOException.class)
public void testDeserializeValueTooMany1() throws Exception {
	// Long + 1 byte
	KvStateSerializer.deserializeValue(new byte[] {1, 1, 1, 1, 1, 1, 1, 1, 2},
		LongSerializer.INSTANCE);
}
 
Example 13
Source File: ImmutableFoldingState.java    From flink with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <ACC, S extends State> S createState(
	StateDescriptor<S, ACC> stateDescriptor,
	byte[] serializedState) throws IOException {
	final ACC state = KvStateSerializer.deserializeValue(
		serializedState,
		stateDescriptor.getSerializer());
	return (S) new ImmutableFoldingState<>(state);
}
 
Example 14
Source File: ImmutableReducingState.java    From flink with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <V, S extends State> S createState(
	StateDescriptor<S, V> stateDescriptor,
	byte[] serializedState) throws IOException {
	final V state = KvStateSerializer.deserializeValue(
		serializedState,
		stateDescriptor.getSerializer());
	return (S) new ImmutableReducingState<>(state);
}
 
Example 15
Source File: KvStateServerHandlerTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Tests a simple successful query via an EmbeddedChannel.
 */
@Test
public void testSimpleQuery() throws Exception {
	KvStateRegistry registry = new KvStateRegistry();
	AtomicKvStateRequestStats stats = new AtomicKvStateRequestStats();

	MessageSerializer<KvStateInternalRequest, KvStateResponse> serializer =
			new MessageSerializer<>(new KvStateInternalRequest.KvStateInternalRequestDeserializer(), new KvStateResponse.KvStateResponseDeserializer());

	KvStateServerHandler handler = new KvStateServerHandler(testServer, registry, serializer, stats);
	EmbeddedChannel channel = new EmbeddedChannel(getFrameDecoder(), handler);

	// Register state
	ValueStateDescriptor<Integer> desc = new ValueStateDescriptor<>("any", IntSerializer.INSTANCE);
	desc.setQueryable("vanilla");

	int numKeyGroups = 1;
	AbstractStateBackend abstractBackend = new MemoryStateBackend();
	DummyEnvironment dummyEnv = new DummyEnvironment("test", 1, 0);
	dummyEnv.setKvStateRegistry(registry);
	AbstractKeyedStateBackend<Integer> backend = createKeyedStateBackend(registry, numKeyGroups, abstractBackend, dummyEnv);

	final TestRegistryListener registryListener = new TestRegistryListener();
	registry.registerListener(dummyEnv.getJobID(), registryListener);

	// Update the KvState and request it
	int expectedValue = 712828289;

	int key = 99812822;
	backend.setCurrentKey(key);
	ValueState<Integer> state = backend.getPartitionedState(
			VoidNamespace.INSTANCE,
			VoidNamespaceSerializer.INSTANCE,
			desc);

	state.update(expectedValue);

	byte[] serializedKeyAndNamespace = KvStateSerializer.serializeKeyAndNamespace(
			key,
			IntSerializer.INSTANCE,
			VoidNamespace.INSTANCE,
			VoidNamespaceSerializer.INSTANCE);

	long requestId = Integer.MAX_VALUE + 182828L;

	assertTrue(registryListener.registrationName.equals("vanilla"));

	KvStateInternalRequest request = new KvStateInternalRequest(
			registryListener.kvStateId, serializedKeyAndNamespace);

	ByteBuf serRequest = MessageSerializer.serializeRequest(channel.alloc(), requestId, request);

	// Write the request and wait for the response
	channel.writeInbound(serRequest);

	ByteBuf buf = (ByteBuf) readInboundBlocking(channel);
	buf.skipBytes(4); // skip frame length

	// Verify the response
	assertEquals(MessageType.REQUEST_RESULT, MessageSerializer.deserializeHeader(buf));
	long deserRequestId = MessageSerializer.getRequestId(buf);
	KvStateResponse response = serializer.deserializeResponse(buf);
	buf.release();

	assertEquals(requestId, deserRequestId);

	int actualValue = KvStateSerializer.deserializeValue(response.getContent(), IntSerializer.INSTANCE);
	assertEquals(expectedValue, actualValue);

	assertEquals(stats.toString(), 1, stats.getNumRequests());

	// Wait for async successful request report
	long deadline = System.nanoTime() + TimeUnit.NANOSECONDS.convert(30, TimeUnit.SECONDS);
	while (stats.getNumSuccessful() != 1L && System.nanoTime() <= deadline) {
		Thread.sleep(10L);
	}

	assertEquals(stats.toString(), 1L, stats.getNumSuccessful());
}
 
Example 16
Source File: KvStateServerHandlerTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * Tests a simple successful query via an EmbeddedChannel.
 */
@Test
public void testSimpleQuery() throws Exception {
	KvStateRegistry registry = new KvStateRegistry();
	AtomicKvStateRequestStats stats = new AtomicKvStateRequestStats();

	MessageSerializer<KvStateInternalRequest, KvStateResponse> serializer =
			new MessageSerializer<>(new KvStateInternalRequest.KvStateInternalRequestDeserializer(), new KvStateResponse.KvStateResponseDeserializer());

	KvStateServerHandler handler = new KvStateServerHandler(testServer, registry, serializer, stats);
	EmbeddedChannel channel = new EmbeddedChannel(getFrameDecoder(), handler);

	// Register state
	ValueStateDescriptor<Integer> desc = new ValueStateDescriptor<>("any", IntSerializer.INSTANCE);
	desc.setQueryable("vanilla");

	int numKeyGroups = 1;
	AbstractStateBackend abstractBackend = new MemoryStateBackend();
	DummyEnvironment dummyEnv = new DummyEnvironment("test", 1, 0);
	dummyEnv.setKvStateRegistry(registry);
	AbstractKeyedStateBackend<Integer> backend = createKeyedStateBackend(registry, numKeyGroups, abstractBackend, dummyEnv);

	final TestRegistryListener registryListener = new TestRegistryListener();
	registry.registerListener(dummyEnv.getJobID(), registryListener);

	// Update the KvState and request it
	int expectedValue = 712828289;

	int key = 99812822;
	backend.setCurrentKey(key);
	ValueState<Integer> state = backend.getPartitionedState(
			VoidNamespace.INSTANCE,
			VoidNamespaceSerializer.INSTANCE,
			desc);

	state.update(expectedValue);

	byte[] serializedKeyAndNamespace = KvStateSerializer.serializeKeyAndNamespace(
			key,
			IntSerializer.INSTANCE,
			VoidNamespace.INSTANCE,
			VoidNamespaceSerializer.INSTANCE);

	long requestId = Integer.MAX_VALUE + 182828L;

	assertTrue(registryListener.registrationName.equals("vanilla"));

	KvStateInternalRequest request = new KvStateInternalRequest(
			registryListener.kvStateId, serializedKeyAndNamespace);

	ByteBuf serRequest = MessageSerializer.serializeRequest(channel.alloc(), requestId, request);

	// Write the request and wait for the response
	channel.writeInbound(serRequest);

	ByteBuf buf = (ByteBuf) readInboundBlocking(channel);
	buf.skipBytes(4); // skip frame length

	// Verify the response
	assertEquals(MessageType.REQUEST_RESULT, MessageSerializer.deserializeHeader(buf));
	long deserRequestId = MessageSerializer.getRequestId(buf);
	KvStateResponse response = serializer.deserializeResponse(buf);

	assertEquals(requestId, deserRequestId);

	int actualValue = KvStateSerializer.deserializeValue(response.getContent(), IntSerializer.INSTANCE);
	assertEquals(expectedValue, actualValue);

	assertEquals(stats.toString(), 1, stats.getNumRequests());

	// Wait for async successful request report
	long deadline = System.nanoTime() + TimeUnit.NANOSECONDS.convert(30, TimeUnit.SECONDS);
	while (stats.getNumSuccessful() != 1L && System.nanoTime() <= deadline) {
		Thread.sleep(10L);
	}

	assertEquals(stats.toString(), 1L, stats.getNumSuccessful());
}
 
Example 17
Source File: KvStateRequestSerializerTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Tests value deserialization with too few bytes.
 */
@Test(expected = IOException.class)
public void testDeserializeValueEmpty() throws Exception {
	KvStateSerializer.deserializeValue(new byte[] {}, LongSerializer.INSTANCE);
}
 
Example 18
Source File: KvStateRequestSerializerTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Tests value deserialization with too few bytes.
 */
@Test(expected = IOException.class)
public void testDeserializeValueEmpty() throws Exception {
	KvStateSerializer.deserializeValue(new byte[] {}, LongSerializer.INSTANCE);
}
 
Example 19
Source File: KvStateRequestSerializerTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Tests value deserialization with too few bytes.
 */
@Test(expected = IOException.class)
public void testDeserializeValueTooShort() throws Exception {
	// 1 byte (incomplete Long)
	KvStateSerializer.deserializeValue(new byte[] {1}, LongSerializer.INSTANCE);
}
 
Example 20
Source File: KvStateRequestSerializerTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * Tests value deserialization with too few bytes.
 */
@Test(expected = IOException.class)
public void testDeserializeValueTooShort() throws Exception {
	// 1 byte (incomplete Long)
	KvStateSerializer.deserializeValue(new byte[] {1}, LongSerializer.INSTANCE);
}