Search in sources :

Example 66 with JobClient

use of org.apache.flink.core.execution.JobClient in project flink by apache.

the class SinkTestSuiteBase method testBasicSink.

// ----------------------------- Basic test cases ---------------------------------
/**
 * Test DataStream connector sink.
 *
 * <p>The following tests will create a sink in the external system, generate a collection of
 * test data and write them to this sink by the Flink Job.
 *
 * <p>In order to pass these tests, the number of records produced by Flink need to be equals to
 * the generated test data. And the records in the sink will be compared to the test data by the
 * different semantics. There's no requirement for records order.
 */
@TestTemplate
@DisplayName("Test data stream sink")
public void testBasicSink(TestEnvironment testEnv, DataStreamSinkExternalContext<T> externalContext, CheckpointingMode semantic) throws Exception {
    TestingSinkSettings sinkSettings = getTestingSinkSettings(semantic);
    final List<T> testRecords = generateTestData(sinkSettings, externalContext);
    // Build and execute Flink job
    StreamExecutionEnvironment execEnv = testEnv.createExecutionEnvironment(TestEnvironmentSettings.builder().setConnectorJarPaths(externalContext.getConnectorJarPaths()).build());
    execEnv.enableCheckpointing(50);
    DataStream<T> dataStream = execEnv.fromCollection(testRecords).name("sourceInSinkTest").setParallelism(1).returns(externalContext.getProducedType());
    tryCreateSink(dataStream, externalContext, sinkSettings).setParallelism(1).name("sinkInSinkTest");
    final JobClient jobClient = execEnv.executeAsync("DataStream Sink Test");
    waitForJobStatus(jobClient, Collections.singletonList(JobStatus.FINISHED), Deadline.fromNow(DEFAULT_JOB_STATUS_CHANGE_TIMEOUT));
    // Check test result
    checkResultWithSemantic(externalContext.createSinkDataReader(sinkSettings), testRecords, semantic);
}
Also used : DEFAULT_COLLECT_DATA_TIMEOUT(org.apache.flink.connector.testframe.utils.ConnectorTestConstants.DEFAULT_COLLECT_DATA_TIMEOUT) DEFAULT_JOB_STATUS_CHANGE_TIMEOUT(org.apache.flink.connector.testframe.utils.ConnectorTestConstants.DEFAULT_JOB_STATUS_CHANGE_TIMEOUT) TestingSinkSettings(org.apache.flink.connector.testframe.external.sink.TestingSinkSettings) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) JobClient(org.apache.flink.core.execution.JobClient) TestTemplate(org.junit.jupiter.api.TestTemplate) DisplayName(org.junit.jupiter.api.DisplayName)

Example 67 with JobClient

use of org.apache.flink.core.execution.JobClient in project flink by apache.

the class SinkTestSuiteBase method testMetrics.

/**
 * Test connector sink metrics.
 *
 * <p>This test will create a sink in the external system, generate test data and write them to
 * the sink via a Flink job. Then read and compare the metrics.
 *
 * <p>Now test: numRecordsOut
 */
@TestTemplate
@DisplayName("Test sink metrics")
public void testMetrics(TestEnvironment testEnv, DataStreamSinkExternalContext<T> externalContext, CheckpointingMode semantic) throws Exception {
    TestingSinkSettings sinkSettings = getTestingSinkSettings(semantic);
    int parallelism = 1;
    final List<T> testRecords = generateTestData(sinkSettings, externalContext);
    // make sure use different names when executes multi times
    String sinkName = "metricTestSink" + testRecords.hashCode();
    final StreamExecutionEnvironment env = testEnv.createExecutionEnvironment(TestEnvironmentSettings.builder().setConnectorJarPaths(externalContext.getConnectorJarPaths()).build());
    env.enableCheckpointing(50);
    DataStreamSource<T> source = env.fromSource(new FromElementsSource<>(Boundedness.CONTINUOUS_UNBOUNDED, testRecords, testRecords.size()), WatermarkStrategy.noWatermarks(), "metricTestSource").setParallelism(1);
    DataStream<T> dataStream = source.returns(externalContext.getProducedType());
    tryCreateSink(dataStream, externalContext, sinkSettings).name(sinkName).setParallelism(parallelism);
    final JobClient jobClient = env.executeAsync("Metrics Test");
    final MetricQuerier queryRestClient = new MetricQuerier(new Configuration());
    final ExecutorService executorService = Executors.newCachedThreadPool();
    try {
        waitForAllTaskRunning(() -> getJobDetails(new RestClient(new Configuration(), executorService), testEnv.getRestEndpoint(), jobClient.getJobID()), Deadline.fromNow(DEFAULT_JOB_STATUS_CHANGE_TIMEOUT));
        waitUntilCondition(() -> {
            // test metrics
            try {
                return compareSinkMetrics(queryRestClient, testEnv, externalContext, jobClient.getJobID(), sinkName, testRecords.size());
            } catch (Exception e) {
                // skip failed assert try
                return false;
            }
        }, Deadline.fromNow(DEFAULT_COLLECT_DATA_TIMEOUT));
    } finally {
        // Clean up
        executorService.shutdown();
        killJob(jobClient);
    }
}
Also used : Configuration(org.apache.flink.configuration.Configuration) RestClient(org.apache.flink.runtime.rest.RestClient) TestingSinkSettings(org.apache.flink.connector.testframe.external.sink.TestingSinkSettings) MetricQuerier(org.apache.flink.connector.testframe.utils.MetricQuerier) JobClient(org.apache.flink.core.execution.JobClient) TestAbortedException(org.opentest4j.TestAbortedException) DEFAULT_COLLECT_DATA_TIMEOUT(org.apache.flink.connector.testframe.utils.ConnectorTestConstants.DEFAULT_COLLECT_DATA_TIMEOUT) DEFAULT_JOB_STATUS_CHANGE_TIMEOUT(org.apache.flink.connector.testframe.utils.ConnectorTestConstants.DEFAULT_JOB_STATUS_CHANGE_TIMEOUT) FromElementsSource(org.apache.flink.connector.testframe.source.FromElementsSource) ExecutorService(java.util.concurrent.ExecutorService) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) TestTemplate(org.junit.jupiter.api.TestTemplate) DisplayName(org.junit.jupiter.api.DisplayName)

Example 68 with JobClient

use of org.apache.flink.core.execution.JobClient in project flink by apache.

the class SinkTestSuiteBase method restartFromSavepoint.

private void restartFromSavepoint(TestEnvironment testEnv, DataStreamSinkExternalContext<T> externalContext, CheckpointingMode semantic, final int beforeParallelism, final int afterParallelism) throws Exception {
    // Step 1: Preparation
    TestingSinkSettings sinkSettings = getTestingSinkSettings(semantic);
    final StreamExecutionEnvironment execEnv = testEnv.createExecutionEnvironment(TestEnvironmentSettings.builder().setConnectorJarPaths(externalContext.getConnectorJarPaths()).build());
    execEnv.setRestartStrategy(RestartStrategies.noRestart());
    // Step 2: Generate test data
    final List<T> testRecords = generateTestData(sinkSettings, externalContext);
    // Step 3: Build and execute Flink job
    int numBeforeSuccess = testRecords.size() / 2;
    DataStreamSource<T> source = execEnv.fromSource(new FromElementsSource<>(Boundedness.CONTINUOUS_UNBOUNDED, testRecords, numBeforeSuccess), WatermarkStrategy.noWatermarks(), "beforeRestartSource").setParallelism(1);
    DataStream<T> dataStream = source.returns(externalContext.getProducedType());
    tryCreateSink(dataStream, externalContext, sinkSettings).name("Sink restart test").setParallelism(beforeParallelism);
    /**
     * The job should stop after consume a specified number of records. In order to know when
     * the specified number of records have been consumed, a collect sink is need to be watched.
     */
    CollectResultIterator<T> iterator = addCollectSink(source);
    final JobClient jobClient = execEnv.executeAsync("Restart Test");
    iterator.setJobClient(jobClient);
    // Step 4: Wait for the expected result and stop Flink job with a savepoint
    final ExecutorService executorService = Executors.newCachedThreadPool();
    String savepointPath;
    try {
        waitForAllTaskRunning(() -> getJobDetails(new RestClient(new Configuration(), executorService), testEnv.getRestEndpoint(), jobClient.getJobID()), Deadline.fromNow(DEFAULT_JOB_STATUS_CHANGE_TIMEOUT));
        waitExpectedSizeData(iterator, numBeforeSuccess);
        savepointPath = jobClient.stopWithSavepoint(true, testEnv.getCheckpointUri(), SavepointFormatType.CANONICAL).get(30, TimeUnit.SECONDS);
        waitForJobStatus(jobClient, Collections.singletonList(JobStatus.FINISHED), Deadline.fromNow(DEFAULT_JOB_STATUS_CHANGE_TIMEOUT));
    } catch (Exception e) {
        executorService.shutdown();
        killJob(jobClient);
        throw e;
    }
    List<T> target = testRecords.subList(0, numBeforeSuccess);
    checkResultWithSemantic(externalContext.createSinkDataReader(sinkSettings), target, semantic);
    // Step 4: restart the Flink job with the savepoint
    final StreamExecutionEnvironment restartEnv = testEnv.createExecutionEnvironment(TestEnvironmentSettings.builder().setConnectorJarPaths(externalContext.getConnectorJarPaths()).setSavepointRestorePath(savepointPath).build());
    restartEnv.enableCheckpointing(50);
    DataStreamSource<T> restartSource = restartEnv.fromSource(new FromElementsSource<>(Boundedness.CONTINUOUS_UNBOUNDED, testRecords, testRecords.size()), WatermarkStrategy.noWatermarks(), "restartSource").setParallelism(1);
    DataStream<T> sinkStream = restartSource.returns(externalContext.getProducedType());
    tryCreateSink(sinkStream, externalContext, sinkSettings).setParallelism(afterParallelism);
    addCollectSink(restartSource);
    final JobClient restartJobClient = restartEnv.executeAsync("Restart Test");
    try {
        // Check the result
        checkResultWithSemantic(externalContext.createSinkDataReader(sinkSettings), testRecords, semantic);
    } finally {
        executorService.shutdown();
        killJob(restartJobClient);
        iterator.close();
    }
}
Also used : Configuration(org.apache.flink.configuration.Configuration) RestClient(org.apache.flink.runtime.rest.RestClient) TestingSinkSettings(org.apache.flink.connector.testframe.external.sink.TestingSinkSettings) JobClient(org.apache.flink.core.execution.JobClient) TestAbortedException(org.opentest4j.TestAbortedException) DEFAULT_COLLECT_DATA_TIMEOUT(org.apache.flink.connector.testframe.utils.ConnectorTestConstants.DEFAULT_COLLECT_DATA_TIMEOUT) DEFAULT_JOB_STATUS_CHANGE_TIMEOUT(org.apache.flink.connector.testframe.utils.ConnectorTestConstants.DEFAULT_JOB_STATUS_CHANGE_TIMEOUT) FromElementsSource(org.apache.flink.connector.testframe.source.FromElementsSource) ExecutorService(java.util.concurrent.ExecutorService) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)

Example 69 with JobClient

use of org.apache.flink.core.execution.JobClient in project flink by apache.

the class LocalRecoveryITCase method submitJob.

private JobClient submitJob(int parallelism, StandaloneSessionClusterEntrypoint clusterEntrypoint) throws Exception {
    final StreamExecutionEnvironment env = StreamExecutionEnvironment.createRemoteEnvironment("localhost", clusterEntrypoint.getRestPort(), new Configuration());
    env.setParallelism(parallelism);
    env.enableCheckpointing(100, CheckpointingMode.EXACTLY_ONCE);
    env.addSource(new LocalRecoverySource()).keyBy(x -> x).addSink(new DiscardingSink<>());
    final JobClient jobClient = env.executeAsync();
    return jobClient;
}
Also used : Deadline(org.apache.flink.api.common.time.Deadline) ClusterOptions(org.apache.flink.configuration.ClusterOptions) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CheckpointingMode(org.apache.flink.streaming.api.CheckpointingMode) CheckpointingStatistics(org.apache.flink.runtime.rest.messages.checkpoints.CheckpointingStatistics) FunctionSnapshotContext(org.apache.flink.runtime.state.FunctionSnapshotContext) EmptyRequestBody(org.apache.flink.runtime.rest.messages.EmptyRequestBody) ArrayList(java.util.ArrayList) ListState(org.apache.flink.api.common.state.ListState) TestLoggerExtension(org.apache.flink.util.TestLoggerExtension) TaskManagerOptions(org.apache.flink.configuration.TaskManagerOptions) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Duration(java.time.Duration) RichParallelSourceFunction(org.apache.flink.streaming.api.functions.source.RichParallelSourceFunction) ListStateDescriptor(org.apache.flink.api.common.state.ListStateDescriptor) RestOptions(org.apache.flink.configuration.RestOptions) Nonnull(javax.annotation.Nonnull) HeartbeatManagerOptions(org.apache.flink.configuration.HeartbeatManagerOptions) StandaloneSessionClusterEntrypoint(org.apache.flink.runtime.entrypoint.StandaloneSessionClusterEntrypoint) Iterator(java.util.Iterator) CheckpointedFunction(org.apache.flink.streaming.api.checkpoint.CheckpointedFunction) DiscardingSink(org.apache.flink.streaming.api.functions.sink.DiscardingSink) FunctionInitializationContext(org.apache.flink.runtime.state.FunctionInitializationContext) Collection(java.util.Collection) Configuration(org.apache.flink.configuration.Configuration) CheckpointingStatisticsHeaders(org.apache.flink.runtime.rest.messages.checkpoints.CheckpointingStatisticsHeaders) IOException(java.io.IOException) JobManagerOptions(org.apache.flink.configuration.JobManagerOptions) TestProcessBuilder(org.apache.flink.test.util.TestProcessBuilder) Preconditions(org.apache.flink.util.Preconditions) JobClient(org.apache.flink.core.execution.JobClient) File(java.io.File) CheckpointingOptions(org.apache.flink.configuration.CheckpointingOptions) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) Executors(org.apache.flink.util.concurrent.Executors) List(java.util.List) JobID(org.apache.flink.api.common.JobID) JobMessageParameters(org.apache.flink.runtime.rest.messages.JobMessageParameters) TaskExecutorProcessEntryPoint(org.apache.flink.test.recovery.utils.TaskExecutorProcessEntryPoint) TempDir(org.junit.jupiter.api.io.TempDir) Optional(java.util.Optional) CommonTestUtils(org.apache.flink.runtime.testutils.CommonTestUtils) StreamingRuntimeContext(org.apache.flink.streaming.api.operators.StreamingRuntimeContext) Collections(java.util.Collections) RestClient(org.apache.flink.runtime.rest.RestClient) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) Configuration(org.apache.flink.configuration.Configuration) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) JobClient(org.apache.flink.core.execution.JobClient)

Example 70 with JobClient

use of org.apache.flink.core.execution.JobClient in project flink by apache.

the class LocalRecoveryITCase method testRecoverLocallyFromProcessCrashWithWorkingDirectory.

@Test
public void testRecoverLocallyFromProcessCrashWithWorkingDirectory() throws Exception {
    final Configuration configuration = new Configuration();
    configuration.set(JobManagerOptions.ADDRESS, "localhost");
    configuration.set(JobManagerOptions.PORT, 0);
    configuration.set(RestOptions.BIND_PORT, "0");
    configuration.set(HeartbeatManagerOptions.HEARTBEAT_TIMEOUT, 10000L);
    configuration.set(HeartbeatManagerOptions.HEARTBEAT_INTERVAL, 1000L);
    configuration.set(HeartbeatManagerOptions.HEARTBEAT_RPC_FAILURE_THRESHOLD, 1);
    configuration.set(ClusterOptions.PROCESS_WORKING_DIR_BASE, tmpDirectory.getAbsolutePath());
    configuration.set(CheckpointingOptions.LOCAL_RECOVERY, true);
    configuration.set(TaskManagerOptions.SLOT_TIMEOUT, Duration.ofSeconds(30L));
    final int parallelism = 3;
    boolean success = false;
    Collection<TaskManagerProcess> taskManagerProcesses = Collections.emptyList();
    try (final StandaloneSessionClusterEntrypoint clusterEntrypoint = new StandaloneSessionClusterEntrypoint(configuration)) {
        clusterEntrypoint.startCluster();
        final Configuration configurationTemplate = new Configuration(configuration);
        configurationTemplate.set(JobManagerOptions.PORT, clusterEntrypoint.getRpcPort());
        taskManagerProcesses = startTaskManagerProcesses(parallelism, configurationTemplate);
        final JobClient jobClient = submitJob(parallelism, clusterEntrypoint);
        final long waitingTimeInSeconds = 45L;
        waitUntilCheckpointCompleted(configuration, clusterEntrypoint.getRestPort(), jobClient.getJobID(), Deadline.fromNow(Duration.ofSeconds(waitingTimeInSeconds)));
        restartTaskManagerProcesses(taskManagerProcesses, parallelism - 1);
        jobClient.getJobExecutionResult().get(waitingTimeInSeconds, TimeUnit.SECONDS);
        success = true;
    } finally {
        if (!success) {
            for (TaskManagerProcess taskManagerProcess : taskManagerProcesses) {
                printLogOutput(taskManagerProcess);
            }
        }
        for (TaskManagerProcess taskManagerProcess : taskManagerProcesses) {
            taskManagerProcess.terminate();
        }
    }
}
Also used : StandaloneSessionClusterEntrypoint(org.apache.flink.runtime.entrypoint.StandaloneSessionClusterEntrypoint) Configuration(org.apache.flink.configuration.Configuration) JobClient(org.apache.flink.core.execution.JobClient) StandaloneSessionClusterEntrypoint(org.apache.flink.runtime.entrypoint.StandaloneSessionClusterEntrypoint) TaskExecutorProcessEntryPoint(org.apache.flink.test.recovery.utils.TaskExecutorProcessEntryPoint) Test(org.junit.jupiter.api.Test)

Aggregations

JobClient (org.apache.flink.core.execution.JobClient)70 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)36 Test (org.junit.Test)32 JobExecutionResult (org.apache.flink.api.common.JobExecutionResult)16 Configuration (org.apache.flink.configuration.Configuration)16 JobListener (org.apache.flink.core.execution.JobListener)14 ArrayList (java.util.ArrayList)12 List (java.util.List)10 JobID (org.apache.flink.api.common.JobID)10 ExecutionException (java.util.concurrent.ExecutionException)9 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 DEFAULT_COLLECT_DATA_TIMEOUT (org.apache.flink.connector.testframe.utils.ConnectorTestConstants.DEFAULT_COLLECT_DATA_TIMEOUT)8 DEFAULT_JOB_STATUS_CHANGE_TIMEOUT (org.apache.flink.connector.testframe.utils.ConnectorTestConstants.DEFAULT_JOB_STATUS_CHANGE_TIMEOUT)8 IOException (java.io.IOException)7 DisplayName (org.junit.jupiter.api.DisplayName)7 TestTemplate (org.junit.jupiter.api.TestTemplate)7 Iterator (java.util.Iterator)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 ExecutionEnvironment (org.apache.flink.api.java.ExecutionEnvironment)6 Preconditions.checkNotNull (org.apache.flink.util.Preconditions.checkNotNull)6