Search in sources :

Example 81 with OneShotLatch

use of org.apache.flink.core.testutils.OneShotLatch in project flink by apache.

the class ContinuousFileProcessingMigrationTest method writeReaderSnapshot.

/**
 * Manually run this to write binary snapshot data. Remove @Ignore to run.
 */
@Ignore
@Test
public void writeReaderSnapshot() throws Exception {
    File testFolder = tempFolder.newFolder();
    TimestampedFileInputSplit split1 = new TimestampedFileInputSplit(0, 3, new Path("test/test1"), 0, 100, null);
    TimestampedFileInputSplit split2 = new TimestampedFileInputSplit(10, 2, new Path("test/test2"), 101, 200, null);
    TimestampedFileInputSplit split3 = new TimestampedFileInputSplit(10, 1, new Path("test/test2"), 0, 100, null);
    TimestampedFileInputSplit split4 = new TimestampedFileInputSplit(11, 0, new Path("test/test3"), 0, 100, null);
    // this always blocks to ensure that the reader doesn't to any actual processing so that
    // we keep the state for the four splits
    final OneShotLatch blockingLatch = new OneShotLatch();
    BlockingFileInputFormat format = new BlockingFileInputFormat(blockingLatch, new Path(testFolder.getAbsolutePath()));
    OneInputStreamOperatorTestHarness<TimestampedFileInputSplit, FileInputSplit> testHarness = createHarness(format);
    testHarness.setTimeCharacteristic(TimeCharacteristic.EventTime);
    testHarness.open();
    // create some state in the reader
    testHarness.processElement(new StreamRecord<>(split1));
    testHarness.processElement(new StreamRecord<>(split2));
    testHarness.processElement(new StreamRecord<>(split3));
    testHarness.processElement(new StreamRecord<>(split4));
    // take a snapshot of the operator's state. This will be used
    // to initialize another reader and compare the results of the
    // two operators.
    final OperatorSubtaskState snapshot;
    synchronized (testHarness.getCheckpointLock()) {
        snapshot = testHarness.snapshot(0L, 0L);
    }
    OperatorSnapshotUtil.writeStateHandle(snapshot, "src/test/resources/reader-migration-test-flink" + flinkGenerateSavepointVersion + "-snapshot");
}
Also used : Path(org.apache.flink.core.fs.Path) FileInputSplit(org.apache.flink.core.fs.FileInputSplit) TimestampedFileInputSplit(org.apache.flink.streaming.api.functions.source.TimestampedFileInputSplit) TimestampedFileInputSplit(org.apache.flink.streaming.api.functions.source.TimestampedFileInputSplit) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) File(java.io.File) OperatorSubtaskState(org.apache.flink.runtime.checkpoint.OperatorSubtaskState) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 82 with OneShotLatch

use of org.apache.flink.core.testutils.OneShotLatch in project flink by apache.

the class JobManagerSharedServicesTest method assertExecutorPoolSize.

private void assertExecutorPoolSize(Executor executor, int expectedPoolSize) throws InterruptedException {
    final CountDownLatch expectedPoolSizeLatch = new CountDownLatch(expectedPoolSize);
    final int expectedPoolSizePlusOne = expectedPoolSize + 1;
    final CountDownLatch expectedPoolSizePlusOneLatch = new CountDownLatch(expectedPoolSizePlusOne);
    final OneShotLatch releaseLatch = new OneShotLatch();
    ThrowingRunnable<Exception> countsDown = () -> {
        expectedPoolSizePlusOneLatch.countDown();
        expectedPoolSizeLatch.countDown();
        // block the runnable to keep the thread occupied
        releaseLatch.await();
    };
    for (int i = 0; i < expectedPoolSizePlusOne; i++) {
        executor.execute(ThrowingRunnable.unchecked(countsDown));
    }
    // the expected pool size latch should complete since we expect to have enough threads
    expectedPoolSizeLatch.await();
    assertEquals(1, expectedPoolSizePlusOneLatch.getCount());
    // unblock the runnables
    releaseLatch.trigger();
}
Also used : OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 83 with OneShotLatch

use of org.apache.flink.core.testutils.OneShotLatch in project flink by apache.

the class JobMasterQueryableStateTest method registerSlotsRequiredForJobExecution.

private static void registerSlotsRequiredForJobExecution(JobMasterGateway jobMasterGateway, JobID jobId) throws ExecutionException, InterruptedException {
    final OneShotLatch oneTaskSubmittedLatch = new OneShotLatch();
    final TaskExecutorGateway taskExecutorGateway = new TestingTaskExecutorGatewayBuilder().setSubmitTaskConsumer((taskDeploymentDescriptor, jobMasterId) -> {
        oneTaskSubmittedLatch.trigger();
        return CompletableFuture.completedFuture(Acknowledge.get());
    }).createTestingTaskExecutorGateway();
    JobMasterTestUtils.registerTaskExecutorAndOfferSlots(rpcService, jobMasterGateway, jobId, PARALLELISM, taskExecutorGateway, testingTimeout);
    oneTaskSubmittedLatch.await();
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) JobVertex(org.apache.flink.runtime.jobgraph.JobVertex) SlotSharingGroup(org.apache.flink.runtime.jobmanager.scheduler.SlotSharingGroup) BeforeClass(org.junit.BeforeClass) TestingRpcService(org.apache.flink.runtime.rpc.TestingRpcService) JobGraph(org.apache.flink.runtime.jobgraph.JobGraph) JobMasterBuilder(org.apache.flink.runtime.jobmaster.utils.JobMasterBuilder) ExceptionUtils(org.apache.flink.util.ExceptionUtils) CompletableFuture(java.util.concurrent.CompletableFuture) CoreMatchers.either(org.hamcrest.CoreMatchers.either) JobStatus(org.apache.flink.api.common.JobStatus) JobType(org.apache.flink.runtime.jobgraph.JobType) TaskExecutorGateway(org.apache.flink.runtime.taskexecutor.TaskExecutorGateway) JobVertexID(org.apache.flink.runtime.jobgraph.JobVertexID) InetAddress(java.net.InetAddress) Assert.assertThat(org.junit.Assert.assertThat) After(org.junit.After) JobGraphTestUtils(org.apache.flink.runtime.jobgraph.JobGraphTestUtils) TestLogger(org.apache.flink.util.TestLogger) Assert.fail(org.junit.Assert.fail) KeyGroupRange(org.apache.flink.runtime.state.KeyGroupRange) AfterClass(org.junit.AfterClass) UnknownKvStateLocation(org.apache.flink.runtime.query.UnknownKvStateLocation) KvStateLocation(org.apache.flink.runtime.query.KvStateLocation) FlinkMatchers.containsCause(org.apache.flink.core.testutils.FlinkMatchers.containsCause) AbstractInvokable(org.apache.flink.runtime.jobgraph.tasks.AbstractInvokable) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) RpcUtils(org.apache.flink.runtime.rpc.RpcUtils) InetSocketAddress(java.net.InetSocketAddress) UnknownHostException(java.net.UnknownHostException) Acknowledge(org.apache.flink.runtime.messages.Acknowledge) ExecutionException(java.util.concurrent.ExecutionException) JobID(org.apache.flink.api.common.JobID) FlinkJobNotFoundException(org.apache.flink.runtime.messages.FlinkJobNotFoundException) TestingTaskExecutorGatewayBuilder(org.apache.flink.runtime.taskexecutor.TestingTaskExecutorGatewayBuilder) Time(org.apache.flink.api.common.time.Time) KvStateID(org.apache.flink.queryablestate.KvStateID) Assert.assertEquals(org.junit.Assert.assertEquals) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) TaskExecutorGateway(org.apache.flink.runtime.taskexecutor.TaskExecutorGateway) TestingTaskExecutorGatewayBuilder(org.apache.flink.runtime.taskexecutor.TestingTaskExecutorGatewayBuilder)

Example 84 with OneShotLatch

use of org.apache.flink.core.testutils.OneShotLatch in project flink by apache.

the class JobMasterServiceLeadershipRunnerTest method testCloseReleasesClassLoaderLease.

@Test
public void testCloseReleasesClassLoaderLease() throws Exception {
    final OneShotLatch closeClassLoaderLeaseLatch = new OneShotLatch();
    final TestingClassLoaderLease classLoaderLease = TestingClassLoaderLease.newBuilder().setCloseRunnable(closeClassLoaderLeaseLatch::trigger).build();
    try (JobManagerRunner jobManagerRunner = newJobMasterServiceLeadershipRunnerBuilder().setClassLoaderLease(classLoaderLease).build()) {
        jobManagerRunner.start();
        jobManagerRunner.close();
        closeClassLoaderLeaseLatch.await();
    }
}
Also used : TestingClassLoaderLease(org.apache.flink.runtime.execution.librarycache.TestingClassLoaderLease) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) Test(org.junit.Test)

Example 85 with OneShotLatch

use of org.apache.flink.core.testutils.OneShotLatch in project flink by apache.

the class BackgroundTaskTest method testRunAfterExecutesBackgroundTaskAfterPreviousHasCompleted.

@Test
public void testRunAfterExecutesBackgroundTaskAfterPreviousHasCompleted() {
    final OneShotLatch blockingLatch = new OneShotLatch();
    final BlockingQueue<Integer> taskCompletions = new ArrayBlockingQueue<>(2);
    final BackgroundTask<Void> backgroundTask = BackgroundTask.initialBackgroundTask(() -> {
        blockingLatch.await();
        taskCompletions.offer(1);
        return null;
    }, TEST_EXECUTOR_RESOURCE.getExecutor()).runAfter(() -> {
        taskCompletions.offer(2);
        return null;
    }, TEST_EXECUTOR_RESOURCE.getExecutor());
    blockingLatch.trigger();
    backgroundTask.getTerminationFuture().join();
    assertThat(taskCompletions, Matchers.contains(1, 2));
}
Also used : ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) Test(org.junit.Test)

Aggregations

OneShotLatch (org.apache.flink.core.testutils.OneShotLatch)138 Test (org.junit.Test)118 JobID (org.apache.flink.api.common.JobID)41 CompletableFuture (java.util.concurrent.CompletableFuture)38 ExecutionException (java.util.concurrent.ExecutionException)27 Configuration (org.apache.flink.configuration.Configuration)26 IOException (java.io.IOException)24 Before (org.junit.Before)24 FlinkException (org.apache.flink.util.FlinkException)23 TestLogger (org.apache.flink.util.TestLogger)21 File (java.io.File)20 UUID (java.util.UUID)18 TimeoutException (java.util.concurrent.TimeoutException)18 TestingResourceManagerGateway (org.apache.flink.runtime.resourcemanager.utils.TestingResourceManagerGateway)18 Time (org.apache.flink.api.common.time.Time)17 TestingJobMasterGateway (org.apache.flink.runtime.jobmaster.utils.TestingJobMasterGateway)17 Rule (org.junit.Rule)17 Collections (java.util.Collections)16 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)16 RpcUtils (org.apache.flink.runtime.rpc.RpcUtils)16