Search in sources :

Example 66 with KeyGroupRange

use of org.apache.flink.runtime.state.KeyGroupRange in project flink by apache.

the class KvStateServerHandlerTest method testQueryExecutorShutDown.

/**
	 * Tests the failure response on a rejected execution, because the query
	 * executor has been closed.
	 */
@Test
public void testQueryExecutorShutDown() throws Exception {
    KvStateRegistry registry = new KvStateRegistry();
    AtomicKvStateRequestStats stats = new AtomicKvStateRequestStats();
    ExecutorService closedExecutor = Executors.newSingleThreadExecutor();
    closedExecutor.shutdown();
    assertTrue(closedExecutor.isShutdown());
    KvStateServerHandler handler = new KvStateServerHandler(registry, closedExecutor, stats);
    EmbeddedChannel channel = new EmbeddedChannel(getFrameDecoder(), handler);
    int numKeyGroups = 1;
    AbstractStateBackend abstractBackend = new MemoryStateBackend();
    DummyEnvironment dummyEnv = new DummyEnvironment("test", 1, 0);
    dummyEnv.setKvStateRegistry(registry);
    KeyedStateBackend<Integer> backend = abstractBackend.createKeyedStateBackend(dummyEnv, new JobID(), "test_op", IntSerializer.INSTANCE, numKeyGroups, new KeyGroupRange(0, 0), registry.createTaskRegistry(dummyEnv.getJobID(), dummyEnv.getJobVertexId()));
    final TestRegistryListener registryListener = new TestRegistryListener();
    registry.registerListener(registryListener);
    // Register state
    ValueStateDescriptor<Integer> desc = new ValueStateDescriptor<>("any", IntSerializer.INSTANCE);
    desc.setQueryable("vanilla");
    backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, desc);
    assertTrue(registryListener.registrationName.equals("vanilla"));
    ByteBuf request = KvStateRequestSerializer.serializeKvStateRequest(channel.alloc(), 282872, registryListener.kvStateId, new byte[0]);
    // Write the request and wait for the response
    channel.writeInbound(request);
    ByteBuf buf = (ByteBuf) readInboundBlocking(channel);
    // skip frame length
    buf.skipBytes(4);
    // Verify the response
    assertEquals(KvStateRequestType.REQUEST_FAILURE, KvStateRequestSerializer.deserializeHeader(buf));
    KvStateRequestFailure response = KvStateRequestSerializer.deserializeKvStateRequestFailure(buf);
    assertTrue(response.getCause().getMessage().contains("RejectedExecutionException"));
    assertEquals(1, stats.getNumRequests());
    assertEquals(1, stats.getNumFailed());
}
Also used : KvStateRegistry(org.apache.flink.runtime.query.KvStateRegistry) KvStateRequestFailure(org.apache.flink.runtime.query.netty.message.KvStateRequestFailure) MemoryStateBackend(org.apache.flink.runtime.state.memory.MemoryStateBackend) KeyGroupRange(org.apache.flink.runtime.state.KeyGroupRange) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) DummyEnvironment(org.apache.flink.runtime.operators.testutils.DummyEnvironment) ByteBuf(io.netty.buffer.ByteBuf) ValueStateDescriptor(org.apache.flink.api.common.state.ValueStateDescriptor) ExecutorService(java.util.concurrent.ExecutorService) AbstractStateBackend(org.apache.flink.runtime.state.AbstractStateBackend) JobID(org.apache.flink.api.common.JobID) Test(org.junit.Test)

Example 67 with KeyGroupRange

use of org.apache.flink.runtime.state.KeyGroupRange in project flink by apache.

the class KvStateServerHandlerTest method testSimpleQuery.

/**
	 * Tests a simple successful query via an EmbeddedChannel.
	 */
@Test
public void testSimpleQuery() 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);
    // 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 = abstractBackend.createKeyedStateBackend(dummyEnv, new JobID(), "test_op", IntSerializer.INSTANCE, numKeyGroups, new KeyGroupRange(0, 0), registry.createTaskRegistry(dummyEnv.getJobID(), dummyEnv.getJobVertexId()));
    final TestRegistryListener registryListener = new TestRegistryListener();
    registry.registerListener(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 = KvStateRequestSerializer.serializeKeyAndNamespace(key, IntSerializer.INSTANCE, VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE);
    long requestId = Integer.MAX_VALUE + 182828L;
    assertTrue(registryListener.registrationName.equals("vanilla"));
    ByteBuf request = KvStateRequestSerializer.serializeKvStateRequest(channel.alloc(), requestId, registryListener.kvStateId, serializedKeyAndNamespace);
    // Write the request and wait for the response
    channel.writeInbound(request);
    ByteBuf buf = (ByteBuf) readInboundBlocking(channel);
    // skip frame length
    buf.skipBytes(4);
    // Verify the response
    assertEquals(KvStateRequestType.REQUEST_RESULT, KvStateRequestSerializer.deserializeHeader(buf));
    KvStateRequestResult response = KvStateRequestSerializer.deserializeKvStateRequestResult(buf);
    assertEquals(requestId, response.getRequestId());
    int actualValue = KvStateRequestSerializer.deserializeValue(response.getSerializedResult(), 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() != 1 && System.nanoTime() <= deadline) {
        Thread.sleep(10);
    }
    assertEquals(stats.toString(), 1, stats.getNumSuccessful());
}
Also used : KvStateRegistry(org.apache.flink.runtime.query.KvStateRegistry) KvStateRequestResult(org.apache.flink.runtime.query.netty.message.KvStateRequestResult) MemoryStateBackend(org.apache.flink.runtime.state.memory.MemoryStateBackend) KeyGroupRange(org.apache.flink.runtime.state.KeyGroupRange) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) DummyEnvironment(org.apache.flink.runtime.operators.testutils.DummyEnvironment) ByteBuf(io.netty.buffer.ByteBuf) ValueStateDescriptor(org.apache.flink.api.common.state.ValueStateDescriptor) AbstractStateBackend(org.apache.flink.runtime.state.AbstractStateBackend) JobID(org.apache.flink.api.common.JobID) Test(org.junit.Test)

Example 68 with KeyGroupRange

use of org.apache.flink.runtime.state.KeyGroupRange in project flink by apache.

the class QueryableStateClientTest method testForceLookupOnOutdatedLocation.

/**
	 * All failures should lead to a retry with a forced location lookup.
	 *
	 * UnknownKvStateID, UnknownKvStateKeyGroupLocation, UnknownKvStateLocation,
	 * ConnectException are checked explicitly as these indicate out-of-sync
	 * KvStateLocation.
	 */
@Test
public void testForceLookupOnOutdatedLocation() throws Exception {
    KvStateLocationLookupService lookupService = mock(KvStateLocationLookupService.class);
    KvStateClient networkClient = mock(KvStateClient.class);
    QueryableStateClient client = new QueryableStateClient(lookupService, networkClient, testActorSystem.dispatcher());
    try {
        JobID jobId = new JobID();
        int numKeyGroups = 4;
        //
        // UnknownKvStateLocation
        //
        String query1 = "lucky";
        Future<KvStateLocation> unknownKvStateLocation = Futures.failed(new UnknownKvStateLocation(query1));
        when(lookupService.getKvStateLookupInfo(eq(jobId), eq(query1))).thenReturn(unknownKvStateLocation);
        Future<byte[]> result = client.getKvState(jobId, query1, 0, new byte[0]);
        try {
            Await.result(result, timeout);
            fail("Did not throw expected UnknownKvStateLocation exception");
        } catch (UnknownKvStateLocation ignored) {
        // Expected
        }
        verify(lookupService, times(2)).getKvStateLookupInfo(eq(jobId), eq(query1));
        //
        // UnknownKvStateKeyGroupLocation
        //
        String query2 = "unlucky";
        Future<KvStateLocation> unknownKeyGroupLocation = Futures.successful(new KvStateLocation(jobId, new JobVertexID(), numKeyGroups, query2));
        when(lookupService.getKvStateLookupInfo(eq(jobId), eq(query2))).thenReturn(unknownKeyGroupLocation);
        result = client.getKvState(jobId, query2, 0, new byte[0]);
        try {
            Await.result(result, timeout);
            fail("Did not throw expected UnknownKvStateKeyGroupLocation exception");
        } catch (UnknownKvStateKeyGroupLocation ignored) {
        // Expected
        }
        verify(lookupService, times(2)).getKvStateLookupInfo(eq(jobId), eq(query2));
        //
        // UnknownKvStateID
        //
        String query3 = "water";
        KvStateID kvStateId = new KvStateID();
        Future<byte[]> unknownKvStateId = Futures.failed(new UnknownKvStateID(kvStateId));
        KvStateServerAddress serverAddress = new KvStateServerAddress(InetAddress.getLocalHost(), 12323);
        KvStateLocation location = new KvStateLocation(jobId, new JobVertexID(), numKeyGroups, query3);
        for (int i = 0; i < numKeyGroups; i++) {
            location.registerKvState(new KeyGroupRange(i, i), kvStateId, serverAddress);
        }
        when(lookupService.getKvStateLookupInfo(eq(jobId), eq(query3))).thenReturn(Futures.successful(location));
        when(networkClient.getKvState(eq(serverAddress), eq(kvStateId), any(byte[].class))).thenReturn(unknownKvStateId);
        result = client.getKvState(jobId, query3, 0, new byte[0]);
        try {
            Await.result(result, timeout);
            fail("Did not throw expected UnknownKvStateID exception");
        } catch (UnknownKvStateID ignored) {
        // Expected
        }
        verify(lookupService, times(2)).getKvStateLookupInfo(eq(jobId), eq(query3));
        //
        // ConnectException
        //
        String query4 = "space";
        Future<byte[]> connectException = Futures.failed(new ConnectException());
        kvStateId = new KvStateID();
        serverAddress = new KvStateServerAddress(InetAddress.getLocalHost(), 11123);
        location = new KvStateLocation(jobId, new JobVertexID(), numKeyGroups, query4);
        for (int i = 0; i < numKeyGroups; i++) {
            location.registerKvState(new KeyGroupRange(i, i), kvStateId, serverAddress);
        }
        when(lookupService.getKvStateLookupInfo(eq(jobId), eq(query4))).thenReturn(Futures.successful(location));
        when(networkClient.getKvState(eq(serverAddress), eq(kvStateId), any(byte[].class))).thenReturn(connectException);
        result = client.getKvState(jobId, query4, 0, new byte[0]);
        try {
            Await.result(result, timeout);
            fail("Did not throw expected ConnectException exception");
        } catch (ConnectException ignored) {
        // Expected
        }
        verify(lookupService, times(2)).getKvStateLookupInfo(eq(jobId), eq(query4));
        //
        // Other Exceptions don't lead to a retry no retry
        //
        String query5 = "universe";
        Future<KvStateLocation> exception = Futures.failed(new RuntimeException("Test exception"));
        when(lookupService.getKvStateLookupInfo(eq(jobId), eq(query5))).thenReturn(exception);
        client.getKvState(jobId, query5, 0, new byte[0]);
        verify(lookupService, times(1)).getKvStateLookupInfo(eq(jobId), eq(query5));
    } finally {
        client.shutDown();
    }
}
Also used : UnknownKvStateID(org.apache.flink.runtime.query.netty.UnknownKvStateID) KvStateClient(org.apache.flink.runtime.query.netty.KvStateClient) JobVertexID(org.apache.flink.runtime.jobgraph.JobVertexID) KeyGroupRange(org.apache.flink.runtime.state.KeyGroupRange) Matchers.anyString(org.mockito.Matchers.anyString) UnknownKvStateID(org.apache.flink.runtime.query.netty.UnknownKvStateID) JobID(org.apache.flink.api.common.JobID) ConnectException(java.net.ConnectException) Test(org.junit.Test)

Example 69 with KeyGroupRange

use of org.apache.flink.runtime.state.KeyGroupRange in project flink by apache.

the class QueryableStateClientTest method testLookupMultipleJobIds.

/**
	 * Tests that the QueryableState client correctly caches location lookups
	 * keyed by both job and name. This test is mainly due to a previous bug due
	 * to which cache entries were by name only. This is a problem, because the
	 * same client can be used to query multiple jobs.
	 */
@Test
public void testLookupMultipleJobIds() throws Exception {
    String name = "unique-per-job";
    // Exact contents don't matter here
    KvStateLocation location = new KvStateLocation(new JobID(), new JobVertexID(), 1, name);
    location.registerKvState(new KeyGroupRange(0, 0), new KvStateID(), new KvStateServerAddress(InetAddress.getLocalHost(), 892));
    JobID jobId1 = new JobID();
    JobID jobId2 = new JobID();
    KvStateLocationLookupService lookupService = mock(KvStateLocationLookupService.class);
    when(lookupService.getKvStateLookupInfo(any(JobID.class), anyString())).thenReturn(Futures.successful(location));
    KvStateClient networkClient = mock(KvStateClient.class);
    when(networkClient.getKvState(any(KvStateServerAddress.class), any(KvStateID.class), any(byte[].class))).thenReturn(Futures.successful(new byte[0]));
    QueryableStateClient client = new QueryableStateClient(lookupService, networkClient, testActorSystem.dispatcher());
    // Query ies with same name, but different job IDs should lead to a
    // single lookup per query and job ID.
    client.getKvState(jobId1, name, 0, new byte[0]);
    client.getKvState(jobId2, name, 0, new byte[0]);
    verify(lookupService, times(1)).getKvStateLookupInfo(eq(jobId1), eq(name));
    verify(lookupService, times(1)).getKvStateLookupInfo(eq(jobId2), eq(name));
}
Also used : KvStateClient(org.apache.flink.runtime.query.netty.KvStateClient) JobVertexID(org.apache.flink.runtime.jobgraph.JobVertexID) KeyGroupRange(org.apache.flink.runtime.state.KeyGroupRange) UnknownKvStateID(org.apache.flink.runtime.query.netty.UnknownKvStateID) Matchers.anyString(org.mockito.Matchers.anyString) JobID(org.apache.flink.api.common.JobID) Test(org.junit.Test)

Example 70 with KeyGroupRange

use of org.apache.flink.runtime.state.KeyGroupRange in project flink by apache.

the class KVStateRequestSerializerRocksDBTest method testMapSerialization.

/**
	 * Tests map serialization and deserialization match.
	 *
	 * @see KvStateRequestSerializerTest#testMapSerialization()
	 * KvStateRequestSerializerTest#testMapSerialization() using the heap state back-end
	 * test
	 */
@Test
public void testMapSerialization() throws Exception {
    final long key = 0L;
    // objects for RocksDB state list serialisation
    DBOptions dbOptions = PredefinedOptions.DEFAULT.createDBOptions();
    dbOptions.setCreateIfMissing(true);
    ColumnFamilyOptions columnFamilyOptions = PredefinedOptions.DEFAULT.createColumnOptions();
    final RocksDBKeyedStateBackend<Long> longHeapKeyedStateBackend = new RocksDBKeyedStateBackend<>(new JobID(), "no-op", ClassLoader.getSystemClassLoader(), temporaryFolder.getRoot(), dbOptions, columnFamilyOptions, mock(TaskKvStateRegistry.class), LongSerializer.INSTANCE, 1, new KeyGroupRange(0, 0), new ExecutionConfig());
    longHeapKeyedStateBackend.setCurrentKey(key);
    final InternalMapState<VoidNamespace, Long, String> mapState = (InternalMapState<VoidNamespace, Long, String>) longHeapKeyedStateBackend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, new MapStateDescriptor<>("test", LongSerializer.INSTANCE, StringSerializer.INSTANCE));
    KvStateRequestSerializerTest.testMapSerialization(key, mapState);
}
Also used : MapStateDescriptor(org.apache.flink.api.common.state.MapStateDescriptor) KeyGroupRange(org.apache.flink.runtime.state.KeyGroupRange) TaskKvStateRegistry(org.apache.flink.runtime.query.TaskKvStateRegistry) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) RocksDBKeyedStateBackend(org.apache.flink.contrib.streaming.state.RocksDBKeyedStateBackend) ColumnFamilyOptions(org.rocksdb.ColumnFamilyOptions) InternalMapState(org.apache.flink.runtime.state.internal.InternalMapState) DBOptions(org.rocksdb.DBOptions) VoidNamespace(org.apache.flink.runtime.state.VoidNamespace) JobID(org.apache.flink.api.common.JobID) Test(org.junit.Test) KvStateRequestSerializerTest(org.apache.flink.runtime.query.netty.message.KvStateRequestSerializerTest)

Aggregations

KeyGroupRange (org.apache.flink.runtime.state.KeyGroupRange)106 Test (org.junit.Test)67 JobID (org.apache.flink.api.common.JobID)46 JobVertexID (org.apache.flink.runtime.jobgraph.JobVertexID)38 ArrayList (java.util.ArrayList)26 CloseableRegistry (org.apache.flink.core.fs.CloseableRegistry)23 MemoryStateBackend (org.apache.flink.runtime.state.memory.MemoryStateBackend)21 DummyEnvironment (org.apache.flink.runtime.operators.testutils.DummyEnvironment)18 KvStateRegistry (org.apache.flink.runtime.query.KvStateRegistry)18 UnregisteredMetricsGroup (org.apache.flink.metrics.groups.UnregisteredMetricsGroup)17 HashMap (java.util.HashMap)15 KeyGroupsStateHandle (org.apache.flink.runtime.state.KeyGroupsStateHandle)15 TestProcessingTimeService (org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService)15 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)14 ExecutionJobVertex (org.apache.flink.runtime.executiongraph.ExecutionJobVertex)14 KeyedStateHandle (org.apache.flink.runtime.state.KeyedStateHandle)14 AcknowledgeCheckpoint (org.apache.flink.runtime.messages.checkpoint.AcknowledgeCheckpoint)13 OperatorStateHandle (org.apache.flink.runtime.state.OperatorStateHandle)13 List (java.util.List)12 StreamStateHandle (org.apache.flink.runtime.state.StreamStateHandle)12