Search in sources :

Example 1 with InjectMiniCluster

use of org.apache.flink.test.junit5.InjectMiniCluster in project flink by apache.

the class TaskCancelAsyncProducerConsumerITCase method testCancelAsyncProducerAndConsumer.

/**
 * Tests that a task waiting on an async producer/consumer that is stuck in a blocking buffer
 * request can be properly cancelled.
 *
 * <p>This is currently required for the Flink Kafka sources, which spawn a separate Thread
 * consuming from Kafka and producing the intermediate streams in the spawned Thread instead of
 * the main task Thread.
 */
@Test
public void testCancelAsyncProducerAndConsumer(@InjectMiniCluster MiniCluster flink) throws Exception {
    Deadline deadline = Deadline.now().plus(Duration.ofMinutes(2));
    // Job with async producer and consumer
    JobVertex producer = new JobVertex("AsyncProducer");
    producer.setParallelism(1);
    producer.setInvokableClass(AsyncProducer.class);
    JobVertex consumer = new JobVertex("AsyncConsumer");
    consumer.setParallelism(1);
    consumer.setInvokableClass(AsyncConsumer.class);
    consumer.connectNewDataSetAsInput(producer, DistributionPattern.POINTWISE, ResultPartitionType.PIPELINED);
    SlotSharingGroup slot = new SlotSharingGroup();
    producer.setSlotSharingGroup(slot);
    consumer.setSlotSharingGroup(slot);
    JobGraph jobGraph = JobGraphTestUtils.streamingJobGraph(producer, consumer);
    // Submit job and wait until running
    flink.runDetached(jobGraph);
    FutureUtils.retrySuccessfulWithDelay(() -> flink.getJobStatus(jobGraph.getJobID()), Time.milliseconds(10), deadline, status -> status == JobStatus.RUNNING, TestingUtils.defaultScheduledExecutor()).get(deadline.timeLeft().toMillis(), TimeUnit.MILLISECONDS);
    boolean producerBlocked = false;
    for (int i = 0; i < 50; i++) {
        Thread thread = ASYNC_PRODUCER_THREAD;
        if (thread != null && thread.isAlive()) {
            StackTraceElement[] stackTrace = thread.getStackTrace();
            producerBlocked = isInBlockingBufferRequest(stackTrace);
        }
        if (producerBlocked) {
            break;
        } else {
            // Retry
            Thread.sleep(500L);
        }
    }
    // Verify that async producer is in blocking request
    assertTrue("Producer thread is not blocked: " + Arrays.toString(ASYNC_PRODUCER_THREAD.getStackTrace()), producerBlocked);
    boolean consumerWaiting = false;
    for (int i = 0; i < 50; i++) {
        Thread thread = ASYNC_CONSUMER_THREAD;
        if (thread != null && thread.isAlive()) {
            consumerWaiting = thread.getState() == Thread.State.WAITING;
        }
        if (consumerWaiting) {
            break;
        } else {
            // Retry
            Thread.sleep(500L);
        }
    }
    // Verify that async consumer is in blocking request
    assertTrue("Consumer thread is not blocked.", consumerWaiting);
    flink.cancelJob(jobGraph.getJobID()).get(deadline.timeLeft().toMillis(), TimeUnit.MILLISECONDS);
    // wait until the job is canceled
    FutureUtils.retrySuccessfulWithDelay(() -> flink.getJobStatus(jobGraph.getJobID()), Time.milliseconds(10), deadline, status -> status == JobStatus.CANCELED, TestingUtils.defaultScheduledExecutor()).get(deadline.timeLeft().toMillis(), TimeUnit.MILLISECONDS);
    // Verify the expected Exceptions
    assertNotNull(ASYNC_PRODUCER_EXCEPTION);
    assertEquals(CancelTaskException.class, ASYNC_PRODUCER_EXCEPTION.getClass());
    assertNotNull(ASYNC_CONSUMER_EXCEPTION);
    assertEquals(IllegalStateException.class, ASYNC_CONSUMER_EXCEPTION.getClass());
}
Also used : RecordWriterBuilder(org.apache.flink.runtime.io.network.api.writer.RecordWriterBuilder) Deadline(org.apache.flink.api.common.time.Deadline) Arrays(java.util.Arrays) InternalMiniClusterExtension(org.apache.flink.runtime.testutils.InternalMiniClusterExtension) JobVertex(org.apache.flink.runtime.jobgraph.JobVertex) SlotSharingGroup(org.apache.flink.runtime.jobmanager.scheduler.SlotSharingGroup) JobGraph(org.apache.flink.runtime.jobgraph.JobGraph) ResultPartitionWriter(org.apache.flink.runtime.io.network.api.writer.ResultPartitionWriter) ResultPartitionType(org.apache.flink.runtime.io.network.partition.ResultPartitionType) JobStatus(org.apache.flink.api.common.JobStatus) MemorySize(org.apache.flink.configuration.MemorySize) MiniClusterResourceConfiguration(org.apache.flink.runtime.testutils.MiniClusterResourceConfiguration) NettyShuffleEnvironmentOptions(org.apache.flink.configuration.NettyShuffleEnvironmentOptions) TestLoggerExtension(org.apache.flink.util.TestLoggerExtension) TaskManagerOptions(org.apache.flink.configuration.TaskManagerOptions) FutureUtils(org.apache.flink.util.concurrent.FutureUtils) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Duration(java.time.Duration) JobGraphTestUtils(org.apache.flink.runtime.jobgraph.JobGraphTestUtils) MiniCluster(org.apache.flink.runtime.minicluster.MiniCluster) DistributionPattern(org.apache.flink.runtime.jobgraph.DistributionPattern) InputGate(org.apache.flink.runtime.io.network.partition.consumer.InputGate) CancelTaskException(org.apache.flink.runtime.execution.CancelTaskException) LongValue(org.apache.flink.types.LongValue) Assert.assertNotNull(org.junit.Assert.assertNotNull) Configuration(org.apache.flink.configuration.Configuration) AbstractInvokable(org.apache.flink.runtime.jobgraph.tasks.AbstractInvokable) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) TestingUtils(org.apache.flink.testutils.TestingUtils) RecordWriter(org.apache.flink.runtime.io.network.api.writer.RecordWriter) InjectMiniCluster(org.apache.flink.test.junit5.InjectMiniCluster) Time(org.apache.flink.api.common.time.Time) Environment(org.apache.flink.runtime.execution.Environment) LocalBufferPoolDestroyTest.isInBlockingBufferRequest(org.apache.flink.runtime.io.network.buffer.LocalBufferPoolDestroyTest.isInBlockingBufferRequest) Assert.assertEquals(org.junit.Assert.assertEquals) JobGraph(org.apache.flink.runtime.jobgraph.JobGraph) JobVertex(org.apache.flink.runtime.jobgraph.JobVertex) Deadline(org.apache.flink.api.common.time.Deadline) SlotSharingGroup(org.apache.flink.runtime.jobmanager.scheduler.SlotSharingGroup) Test(org.junit.jupiter.api.Test)

Example 2 with InjectMiniCluster

use of org.apache.flink.test.junit5.InjectMiniCluster in project flink by apache.

the class AbstractHAJobRunITCase method testJobExecutionInHaMode.

@Test
public void testJobExecutionInHaMode(@InjectMiniCluster MiniCluster flinkCluster) throws Exception {
    final JobGraph jobGraph = JobGraphTestUtils.singleNoOpJobGraph();
    // providing a timeout helps making the test fail in case some issue occurred while
    // initializing the cluster
    flinkCluster.submitJob(jobGraph).get(30, TimeUnit.SECONDS);
    final Deadline deadline = Deadline.fromNow(Duration.ofSeconds(30));
    final JobStatus jobStatus = FutureUtils.retrySuccessfulWithDelay(() -> flinkCluster.getJobStatus(jobGraph.getJobID()), Time.milliseconds(10), deadline, status -> flinkCluster.isRunning() && status == JobStatus.FINISHED, TestingUtils.defaultScheduledExecutor()).get(deadline.timeLeft().toMillis(), TimeUnit.MILLISECONDS);
    assertThat(jobStatus).isEqualTo(JobStatus.FINISHED);
    runAfterJobTermination();
}
Also used : JobStatus(org.apache.flink.api.common.JobStatus) Deadline(org.apache.flink.api.common.time.Deadline) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Configuration(org.apache.flink.configuration.Configuration) JobGraph(org.apache.flink.runtime.jobgraph.JobGraph) JobStatus(org.apache.flink.api.common.JobStatus) Order(org.junit.jupiter.api.Order) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) TestingUtils(org.apache.flink.testutils.TestingUtils) TestLoggerExtension(org.apache.flink.util.TestLoggerExtension) AllCallbackWrapper(org.apache.flink.core.testutils.AllCallbackWrapper) FileSystem(org.apache.flink.core.fs.FileSystem) FutureUtils(org.apache.flink.util.concurrent.FutureUtils) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) InjectMiniCluster(org.apache.flink.test.junit5.InjectMiniCluster) Duration(java.time.Duration) JobGraphTestUtils(org.apache.flink.runtime.jobgraph.JobGraphTestUtils) MiniCluster(org.apache.flink.runtime.minicluster.MiniCluster) Time(org.apache.flink.api.common.time.Time) HighAvailabilityOptions(org.apache.flink.configuration.HighAvailabilityOptions) ZooKeeperExtension(org.apache.flink.runtime.zookeeper.ZooKeeperExtension) JobGraph(org.apache.flink.runtime.jobgraph.JobGraph) Deadline(org.apache.flink.api.common.time.Deadline) Test(org.junit.jupiter.api.Test)

Aggregations

Duration (java.time.Duration)2 TimeUnit (java.util.concurrent.TimeUnit)2 JobStatus (org.apache.flink.api.common.JobStatus)2 Deadline (org.apache.flink.api.common.time.Deadline)2 Time (org.apache.flink.api.common.time.Time)2 Configuration (org.apache.flink.configuration.Configuration)2 JobGraph (org.apache.flink.runtime.jobgraph.JobGraph)2 JobGraphTestUtils (org.apache.flink.runtime.jobgraph.JobGraphTestUtils)2 MiniCluster (org.apache.flink.runtime.minicluster.MiniCluster)2 InjectMiniCluster (org.apache.flink.test.junit5.InjectMiniCluster)2 TestingUtils (org.apache.flink.testutils.TestingUtils)2 TestLoggerExtension (org.apache.flink.util.TestLoggerExtension)2 FutureUtils (org.apache.flink.util.concurrent.FutureUtils)2 Test (org.junit.jupiter.api.Test)2 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)2 RegisterExtension (org.junit.jupiter.api.extension.RegisterExtension)2 Arrays (java.util.Arrays)1 HighAvailabilityOptions (org.apache.flink.configuration.HighAvailabilityOptions)1 MemorySize (org.apache.flink.configuration.MemorySize)1 NettyShuffleEnvironmentOptions (org.apache.flink.configuration.NettyShuffleEnvironmentOptions)1