Search in sources :

Example 46 with MockEnvironment

use of org.apache.flink.runtime.operators.testutils.MockEnvironment in project flink by apache.

the class SourceFunctionUtil method runRichSourceFunction.

private static <T extends Serializable> List<T> runRichSourceFunction(SourceFunction<T> sourceFunction) throws Exception {
    try (MockEnvironment environment = new MockEnvironmentBuilder().setTaskName("MockTask").setManagedMemorySize(3 * 1024 * 1024).setInputSplitProvider(new MockInputSplitProvider()).setBufferSize(1024).build()) {
        AbstractStreamOperator<?> operator = mock(AbstractStreamOperator.class);
        when(operator.getExecutionConfig()).thenReturn(new ExecutionConfig());
        RuntimeContext runtimeContext = new StreamingRuntimeContext(operator, environment, new HashMap<>());
        ((RichFunction) sourceFunction).setRuntimeContext(runtimeContext);
        ((RichFunction) sourceFunction).open(new Configuration());
        return runNonRichSourceFunction(sourceFunction);
    }
}
Also used : MockEnvironmentBuilder(org.apache.flink.runtime.operators.testutils.MockEnvironmentBuilder) StreamingRuntimeContext(org.apache.flink.streaming.api.operators.StreamingRuntimeContext) Configuration(org.apache.flink.configuration.Configuration) RichFunction(org.apache.flink.api.common.functions.RichFunction) MockEnvironment(org.apache.flink.runtime.operators.testutils.MockEnvironment) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) RuntimeContext(org.apache.flink.api.common.functions.RuntimeContext) StreamingRuntimeContext(org.apache.flink.streaming.api.operators.StreamingRuntimeContext) MockInputSplitProvider(org.apache.flink.runtime.operators.testutils.MockInputSplitProvider)

Example 47 with MockEnvironment

use of org.apache.flink.runtime.operators.testutils.MockEnvironment in project flink by apache.

the class StateBackendTestBase method buildMockEnv.

private MockEnvironment buildMockEnv() throws Exception {
    MockEnvironment mockEnvironment = MockEnvironment.builder().setTaskStateManager(getTestTaskStateManager()).build();
    mockEnvironment.setCheckpointStorageAccess(getCheckpointStorageAccess());
    return mockEnvironment;
}
Also used : MockEnvironment(org.apache.flink.runtime.operators.testutils.MockEnvironment)

Example 48 with MockEnvironment

use of org.apache.flink.runtime.operators.testutils.MockEnvironment in project flink by apache.

the class RocksDBAsyncSnapshotTest method testCleanupOfSnapshotsInFailureCase.

/**
 * Test that the snapshot files are cleaned up in case of a failure during the snapshot
 * procedure.
 */
@Test
public void testCleanupOfSnapshotsInFailureCase() throws Exception {
    long checkpointId = 1L;
    long timestamp = 42L;
    MockEnvironment env = MockEnvironment.builder().build();
    final IOException testException = new IOException("Test exception");
    CheckpointStateOutputStream outputStream = spy(new FailingStream(testException));
    RocksDBStateBackend backend = new RocksDBStateBackend((StateBackend) new MemoryStateBackend());
    backend.setDbStoragePath(temporaryFolder.newFolder().toURI().toString());
    AbstractKeyedStateBackend<Void> keyedStateBackend = backend.createKeyedStateBackend(env, new JobID(), "test operator", VoidSerializer.INSTANCE, 1, new KeyGroupRange(0, 0), null, TtlTimeProvider.DEFAULT, new UnregisteredMetricsGroup(), Collections.emptyList(), new CloseableRegistry());
    try {
        // register a state so that the state backend has to checkpoint something
        keyedStateBackend.getPartitionedState("namespace", StringSerializer.INSTANCE, new ValueStateDescriptor<>("foobar", String.class));
        RunnableFuture<SnapshotResult<KeyedStateHandle>> snapshotFuture = keyedStateBackend.snapshot(checkpointId, timestamp, new TestCheckpointStreamFactory(() -> outputStream), CheckpointOptions.forCheckpointWithDefaultLocation());
        try {
            FutureUtils.runIfNotDoneAndGet(snapshotFuture);
            fail("Expected an exception to be thrown here.");
        } catch (ExecutionException e) {
            Assert.assertEquals(testException, e.getCause());
        }
        verify(outputStream).close();
    } finally {
        IOUtils.closeQuietly(keyedStateBackend);
        keyedStateBackend.dispose();
        IOUtils.closeQuietly(env);
    }
}
Also used : UnregisteredMetricsGroup(org.apache.flink.metrics.groups.UnregisteredMetricsGroup) SnapshotResult(org.apache.flink.runtime.state.SnapshotResult) MemoryStateBackend(org.apache.flink.runtime.state.memory.MemoryStateBackend) KeyGroupRange(org.apache.flink.runtime.state.KeyGroupRange) IOException(java.io.IOException) CloseableRegistry(org.apache.flink.core.fs.CloseableRegistry) TestCheckpointStreamFactory(org.apache.flink.runtime.state.testutils.TestCheckpointStreamFactory) MockEnvironment(org.apache.flink.runtime.operators.testutils.MockEnvironment) StreamMockEnvironment(org.apache.flink.streaming.runtime.tasks.StreamMockEnvironment) CheckpointStateOutputStream(org.apache.flink.runtime.state.CheckpointStateOutputStream) ExecutionException(java.util.concurrent.ExecutionException) JobID(org.apache.flink.api.common.JobID) Test(org.junit.Test)

Example 49 with MockEnvironment

use of org.apache.flink.runtime.operators.testutils.MockEnvironment in project flink by apache.

the class RocksDBStateBackendConfigTest method testConfigureTimerServiceLoadingFromApplication.

/**
 * Validates that user custom configuration from code should override the flink-conf.yaml.
 */
@Test
public void testConfigureTimerServiceLoadingFromApplication() throws Exception {
    final MockEnvironment env = new MockEnvironmentBuilder().build();
    // priorityQueueStateType of the job backend
    final EmbeddedRocksDBStateBackend backend = new EmbeddedRocksDBStateBackend();
    backend.setPriorityQueueStateType(EmbeddedRocksDBStateBackend.PriorityQueueStateType.HEAP);
    // priorityQueueStateType in the cluster config
    final Configuration configFromConfFile = new Configuration();
    configFromConfFile.setString(RocksDBOptions.TIMER_SERVICE_FACTORY.key(), RocksDBStateBackend.PriorityQueueStateType.ROCKSDB.toString());
    // configure final backend from job and cluster config
    final EmbeddedRocksDBStateBackend configuredRocksDBStateBackend = backend.configure(configFromConfFile, Thread.currentThread().getContextClassLoader());
    final RocksDBKeyedStateBackend<Integer> keyedBackend = createKeyedStateBackend(configuredRocksDBStateBackend, env, IntSerializer.INSTANCE);
    // priorityQueueStateType of the job backend should be preserved
    assertThat(keyedBackend.getPriorityQueueFactory(), instanceOf(HeapPriorityQueueSetFactory.class));
    keyedBackend.close();
    keyedBackend.dispose();
    env.close();
}
Also used : MockEnvironmentBuilder(org.apache.flink.runtime.operators.testutils.MockEnvironmentBuilder) Configuration(org.apache.flink.configuration.Configuration) HeapPriorityQueueSetFactory(org.apache.flink.runtime.state.heap.HeapPriorityQueueSetFactory) MockEnvironment(org.apache.flink.runtime.operators.testutils.MockEnvironment) Test(org.junit.Test)

Example 50 with MockEnvironment

use of org.apache.flink.runtime.operators.testutils.MockEnvironment in project flink by apache.

the class RocksDBStateBackendConfigTest method testConfigureTimerService.

@Test
public void testConfigureTimerService() throws Exception {
    final MockEnvironment env = getMockEnvironment(tempFolder.newFolder());
    // Fix the option key string
    Assert.assertEquals("state.backend.rocksdb.timer-service.factory", RocksDBOptions.TIMER_SERVICE_FACTORY.key());
    // Fix the option value string and ensure all are covered
    Assert.assertEquals(2, EmbeddedRocksDBStateBackend.PriorityQueueStateType.values().length);
    Assert.assertEquals("ROCKSDB", EmbeddedRocksDBStateBackend.PriorityQueueStateType.ROCKSDB.toString());
    Assert.assertEquals("HEAP", EmbeddedRocksDBStateBackend.PriorityQueueStateType.HEAP.toString());
    // Fix the default
    Assert.assertEquals(EmbeddedRocksDBStateBackend.PriorityQueueStateType.ROCKSDB, RocksDBOptions.TIMER_SERVICE_FACTORY.defaultValue());
    EmbeddedRocksDBStateBackend rocksDbBackend = new EmbeddedRocksDBStateBackend();
    RocksDBKeyedStateBackend<Integer> keyedBackend = createKeyedStateBackend(rocksDbBackend, env, IntSerializer.INSTANCE);
    Assert.assertEquals(RocksDBPriorityQueueSetFactory.class, keyedBackend.getPriorityQueueFactory().getClass());
    keyedBackend.dispose();
    Configuration conf = new Configuration();
    conf.set(RocksDBOptions.TIMER_SERVICE_FACTORY, EmbeddedRocksDBStateBackend.PriorityQueueStateType.HEAP);
    rocksDbBackend = rocksDbBackend.configure(conf, Thread.currentThread().getContextClassLoader());
    keyedBackend = createKeyedStateBackend(rocksDbBackend, env, IntSerializer.INSTANCE);
    Assert.assertEquals(HeapPriorityQueueSetFactory.class, keyedBackend.getPriorityQueueFactory().getClass());
    keyedBackend.dispose();
    env.close();
}
Also used : Configuration(org.apache.flink.configuration.Configuration) MockEnvironment(org.apache.flink.runtime.operators.testutils.MockEnvironment) Test(org.junit.Test)

Aggregations

MockEnvironment (org.apache.flink.runtime.operators.testutils.MockEnvironment)53 Test (org.junit.Test)40 Configuration (org.apache.flink.configuration.Configuration)20 MockEnvironmentBuilder (org.apache.flink.runtime.operators.testutils.MockEnvironmentBuilder)17 StreamRecord (org.apache.flink.streaming.runtime.streamrecord.StreamRecord)16 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)14 CheckpointMetaData (org.apache.flink.runtime.checkpoint.CheckpointMetaData)12 DataInputStatus (org.apache.flink.streaming.runtime.io.DataInputStatus)11 CoreMatchers.equalTo (org.hamcrest.CoreMatchers.equalTo)11 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)10 Arrays (java.util.Arrays)10 StringSerializer (org.apache.flink.api.common.typeutils.base.StringSerializer)10 MockStreamTaskBuilder (org.apache.flink.streaming.util.MockStreamTaskBuilder)10 CheckpointMetricsBuilder (org.apache.flink.runtime.checkpoint.CheckpointMetricsBuilder)9 StreamConfig (org.apache.flink.streaming.api.graph.StreamConfig)9 List (java.util.List)8 TypeSerializer (org.apache.flink.api.common.typeutils.TypeSerializer)8 CloseableRegistry (org.apache.flink.core.fs.CloseableRegistry)8 CheckpointException (org.apache.flink.runtime.checkpoint.CheckpointException)8