Search in sources :

Example 71 with CompletionException

use of java.util.concurrent.CompletionException in project hbase by apache.

the class TestThreadLocalPoolMap method testLocality.

@Test
public void testLocality() throws ExecutionException, InterruptedException {
    String key = "key";
    AtomicInteger id = new AtomicInteger();
    Runnable runnable = () -> {
        try {
            String myId = Integer.toString(id.getAndIncrement());
            for (int i = 0; i < 3; i++) {
                String result = poolMap.getOrCreate(key, () -> myId);
                assertEquals(myId, result);
                Thread.yield();
            }
        } catch (IOException e) {
            throw new CompletionException(e);
        }
    };
    CompletableFuture<Void> future1 = CompletableFuture.runAsync(runnable);
    CompletableFuture<Void> future2 = CompletableFuture.runAsync(runnable);
    /* test for successful completion */
    future1.get();
    future2.get();
    assertEquals(2, poolMap.values().size());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompletionException(java.util.concurrent.CompletionException) IOException(java.io.IOException) Test(org.junit.Test)

Example 72 with CompletionException

use of java.util.concurrent.CompletionException in project hbase by apache.

the class TestRoundRobinPoolMap method testMultiThreadedRoundRobin.

@Test
public void testMultiThreadedRoundRobin() throws ExecutionException, InterruptedException {
    String key = "key";
    AtomicInteger id = new AtomicInteger();
    List<String> results = Collections.synchronizedList(new ArrayList<>());
    Runnable runnable = () -> {
        try {
            for (int i = 0; i < POOL_SIZE; i++) {
                String value = Integer.toString(id.getAndIncrement());
                String result = poolMap.getOrCreate(key, () -> value);
                results.add(result);
                Thread.yield();
            }
        } catch (IOException e) {
            throw new CompletionException(e);
        }
    };
    CompletableFuture<Void> future1 = CompletableFuture.runAsync(runnable);
    CompletableFuture<Void> future2 = CompletableFuture.runAsync(runnable);
    /* test for successful completion */
    future1.get();
    future2.get();
    assertEquals(POOL_SIZE, poolMap.values().size());
    /* check every elements occur twice */
    Collections.sort(results);
    Iterator<String> iterator = results.iterator();
    for (int i = 0; i < POOL_SIZE; i++) {
        String next1 = iterator.next();
        String next2 = iterator.next();
        assertEquals(next1, next2);
    }
    assertFalse(iterator.hasNext());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompletionException(java.util.concurrent.CompletionException) IOException(java.io.IOException) Test(org.junit.Test)

Example 73 with CompletionException

use of java.util.concurrent.CompletionException in project hazelcast by hazelcast.

the class OrderedProcessingMergingStagesTest method when_hashJoin2_applied_primary_stream_order_is_preserved.

@Test
public void when_hashJoin2_applied_primary_stream_order_is_preserved() {
    int validatedItemCountPerGenerator = ITEM_COUNT;
    int eventsPerSecondPerGenerator = 5 * ITEM_COUNT;
    int generatorCount = 2;
    // Generate monotonic increasing items that are distinct for each generator.
    GeneratorFunction<Map.Entry<Long, Long>> generator1 = (ts, seq) -> Util.entry(0L, generatorCount * seq);
    GeneratorFunction<Map.Entry<Long, Long>> generator2 = (ts, seq) -> Util.entry(1L, generatorCount * seq + 1);
    StreamStage<Map.Entry<Long, Long>> srcStage = p.readFrom(itemsParallel(eventsPerSecondPerGenerator, Arrays.asList(generator1, generator2))).withIngestionTimestamps();
    BatchStage<Map.Entry<Long, Long>> batchStage = p.readFrom(TestSources.items(Util.entry(0L, 0L), Util.entry(1L, 0L)));
    BatchStage<Map.Entry<Long, Long>> batchStage2 = p.readFrom(TestSources.items(Util.entry(0L, 0L), Util.entry(1L, 0L)));
    StreamStage<Map.Entry<Long, Long>> joined = srcStage.hashJoin2(batchStage, JoinClause.onKeys(Map.Entry::getKey, Map.Entry::getKey), batchStage2, JoinClause.onKeys(Map.Entry::getKey, Map.Entry::getKey), (primary, stg1, stg2) -> primary).setLocalParallelism(HIGH_LOCAL_PARALLELISM);
    joined.groupingKey(Map.Entry::getKey).mapStateful(() -> create(generatorCount), this::orderValidator).writeTo(AssertionSinks.assertCollectedEventually(60, list -> {
        assertTrue("when", validatedItemCountPerGenerator <= list.size());
        assertFalse("There is some reordered items in the list", list.contains(false));
    }));
    Job job = hz.getJet().newJob(p);
    try {
        job.join();
        fail("Job should have completed with an AssertionCompletedException, but completed normally");
    } catch (CompletionException e) {
        String errorMsg = e.getCause().getMessage();
        assertTrue("Job was expected to complete with AssertionCompletedException, but completed with: " + e.getCause(), errorMsg.contains(AssertionCompletedException.class.getName()));
    }
}
Also used : AssertionSinks(com.hazelcast.jet.pipeline.test.AssertionSinks) Arrays(java.util.Arrays) BeforeClass(org.junit.BeforeClass) QuickTest(com.hazelcast.test.annotation.QuickTest) ParallelStreamP(com.hazelcast.jet.pipeline.test.ParallelStreamP) HazelcastSerialParametersRunnerFactory(com.hazelcast.test.HazelcastSerialParametersRunnerFactory) JetAssert.assertFalse(com.hazelcast.jet.core.test.JetAssert.assertFalse) JetAssert.assertTrue(com.hazelcast.jet.core.test.JetAssert.assertTrue) Map(java.util.Map) Nonnull(javax.annotation.Nonnull) Job(com.hazelcast.jet.Job) Before(org.junit.Before) UseParametersRunnerFactory(org.junit.runners.Parameterized.UseParametersRunnerFactory) HazelcastInstance(com.hazelcast.core.HazelcastInstance) AfterClass(org.junit.AfterClass) LongStream(java.util.stream.LongStream) JetTestSupport(com.hazelcast.jet.core.JetTestSupport) ProcessorMetaSupplier(com.hazelcast.jet.core.ProcessorMetaSupplier) Test(org.junit.Test) CompletionException(java.util.concurrent.CompletionException) Category(org.junit.experimental.categories.Category) Serializable(java.io.Serializable) Objects(java.util.Objects) JetAssert.fail(com.hazelcast.jet.core.test.JetAssert.fail) TestSources(com.hazelcast.jet.pipeline.test.TestSources) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) GeneratorFunction(com.hazelcast.jet.pipeline.test.GeneratorFunction) Hazelcast(com.hazelcast.core.Hazelcast) AssertionCompletedException(com.hazelcast.jet.pipeline.test.AssertionCompletedException) LongAccumulator(com.hazelcast.jet.accumulator.LongAccumulator) Assert(org.junit.Assert) Util(com.hazelcast.jet.Util) AssertionCompletedException(com.hazelcast.jet.pipeline.test.AssertionCompletedException) CompletionException(java.util.concurrent.CompletionException) Job(com.hazelcast.jet.Job) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 74 with CompletionException

use of java.util.concurrent.CompletionException in project hazelcast by hazelcast.

the class OrderedProcessingMergingStagesTest method when_innerJoin_applied_primary_stream_order_is_preserved.

@Test
public void when_innerJoin_applied_primary_stream_order_is_preserved() {
    int validatedItemCountPerGenerator = ITEM_COUNT;
    int eventsPerSecondPerGenerator = 5 * ITEM_COUNT;
    int generatorCount = 2;
    // Generate monotonic increasing items that are distinct for each generator.
    GeneratorFunction<Map.Entry<Long, Long>> generator1 = (ts, seq) -> Util.entry(0L, generatorCount * seq);
    GeneratorFunction<Map.Entry<Long, Long>> generator2 = (ts, seq) -> Util.entry(1L, generatorCount * seq + 1);
    StreamStage<Map.Entry<Long, Long>> srcStage = p.readFrom(itemsParallel(eventsPerSecondPerGenerator, Arrays.asList(generator1, generator2))).withIngestionTimestamps().setLocalParallelism(HIGH_LOCAL_PARALLELISM);
    BatchStage<Map.Entry<Long, Long>> batchStage = p.readFrom(TestSources.items(Util.entry(0L, 0L), Util.entry(1L, 0L)));
    StreamStage<Map.Entry<Long, Long>> joined = srcStage.innerHashJoin(batchStage, JoinClause.onKeys(Map.Entry::getKey, Map.Entry::getKey), (primary, stg) -> primary).setLocalParallelism(LOW_LOCAL_PARALLELISM);
    joined.groupingKey(Map.Entry::getKey).mapStateful(() -> create(generatorCount), this::orderValidator).writeTo(AssertionSinks.assertCollectedEventually(60, list -> {
        assertTrue("when", validatedItemCountPerGenerator <= list.size());
        assertFalse("There is some reordered items in the list", list.contains(false));
    }));
    Job job = hz.getJet().newJob(p);
    try {
        job.join();
        fail("Job should have completed with an AssertionCompletedException, but completed normally");
    } catch (CompletionException e) {
        String errorMsg = e.getCause().getMessage();
        assertTrue("Job was expected to complete with AssertionCompletedException, but completed with: " + e.getCause(), errorMsg.contains(AssertionCompletedException.class.getName()));
    }
}
Also used : AssertionSinks(com.hazelcast.jet.pipeline.test.AssertionSinks) Arrays(java.util.Arrays) BeforeClass(org.junit.BeforeClass) QuickTest(com.hazelcast.test.annotation.QuickTest) ParallelStreamP(com.hazelcast.jet.pipeline.test.ParallelStreamP) HazelcastSerialParametersRunnerFactory(com.hazelcast.test.HazelcastSerialParametersRunnerFactory) JetAssert.assertFalse(com.hazelcast.jet.core.test.JetAssert.assertFalse) JetAssert.assertTrue(com.hazelcast.jet.core.test.JetAssert.assertTrue) Map(java.util.Map) Nonnull(javax.annotation.Nonnull) Job(com.hazelcast.jet.Job) Before(org.junit.Before) UseParametersRunnerFactory(org.junit.runners.Parameterized.UseParametersRunnerFactory) HazelcastInstance(com.hazelcast.core.HazelcastInstance) AfterClass(org.junit.AfterClass) LongStream(java.util.stream.LongStream) JetTestSupport(com.hazelcast.jet.core.JetTestSupport) ProcessorMetaSupplier(com.hazelcast.jet.core.ProcessorMetaSupplier) Test(org.junit.Test) CompletionException(java.util.concurrent.CompletionException) Category(org.junit.experimental.categories.Category) Serializable(java.io.Serializable) Objects(java.util.Objects) JetAssert.fail(com.hazelcast.jet.core.test.JetAssert.fail) TestSources(com.hazelcast.jet.pipeline.test.TestSources) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) GeneratorFunction(com.hazelcast.jet.pipeline.test.GeneratorFunction) Hazelcast(com.hazelcast.core.Hazelcast) AssertionCompletedException(com.hazelcast.jet.pipeline.test.AssertionCompletedException) LongAccumulator(com.hazelcast.jet.accumulator.LongAccumulator) Assert(org.junit.Assert) Util(com.hazelcast.jet.Util) AssertionCompletedException(com.hazelcast.jet.pipeline.test.AssertionCompletedException) CompletionException(java.util.concurrent.CompletionException) Job(com.hazelcast.jet.Job) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 75 with CompletionException

use of java.util.concurrent.CompletionException in project hazelcast by hazelcast.

the class OrderedStreamProcessingTest method when_source_is_parallel_2.

@Test
public void when_source_is_parallel_2() {
    int validatedItemCountPerGenerator = ITEM_COUNT;
    int eventsPerSecondPerGenerator = 5 * ITEM_COUNT;
    int generatorCount = 4;
    // Generate monotonic increasing items that are distinct for each generator.
    GeneratorFunction<Long> generator1 = (ts, seq) -> generatorCount * seq;
    GeneratorFunction<Long> generator2 = (ts, seq) -> generatorCount * seq + 1;
    GeneratorFunction<Long> generator3 = (ts, seq) -> generatorCount * seq + 2;
    GeneratorFunction<Long> generator4 = (ts, seq) -> generatorCount * seq + 3;
    List<Long> sequence1 = LongStream.range(0, validatedItemCountPerGenerator).map(i -> generatorCount * i).boxed().collect(toList());
    List<Long> sequence2 = LongStream.range(0, validatedItemCountPerGenerator).map(i -> generatorCount * i + 1).boxed().collect(toList());
    List<Long> sequence3 = LongStream.range(0, validatedItemCountPerGenerator).map(i -> generatorCount * i + 2).boxed().collect(toList());
    List<Long> sequence4 = LongStream.range(0, validatedItemCountPerGenerator).map(i -> generatorCount * i + 3).boxed().collect(toList());
    StreamStage<Long> srcStage = p.readFrom(itemsParallel(eventsPerSecondPerGenerator, Arrays.asList(generator1, generator2, generator3, generator4))).withIngestionTimestamps();
    StreamStage<Long> applied = srcStage.apply(transform);
    applied.filter(i -> i % generatorCount == 0).writeTo(AssertionSinks.assertCollectedEventually(60, list -> Assert.assertArrayEquals(list.toArray(), sequence1.toArray())));
    applied.filter(i -> i % generatorCount == 1).writeTo(AssertionSinks.assertCollectedEventually(60, list -> Assert.assertArrayEquals(list.toArray(), sequence2.toArray())));
    applied.filter(i -> i % generatorCount == 2).writeTo(AssertionSinks.assertCollectedEventually(60, list -> Assert.assertArrayEquals(list.toArray(), sequence3.toArray())));
    applied.filter(i -> i % generatorCount == 3).writeTo(AssertionSinks.assertCollectedEventually(60, list -> Assert.assertArrayEquals(list.toArray(), sequence4.toArray())));
    Job job = jet.newJob(p);
    try {
        job.join();
        fail("Job should have completed with an AssertionCompletedException, but completed normally");
    } catch (CompletionException e) {
        String errorMsg = e.getCause().getMessage();
        assertTrue("Job was expected to complete with AssertionCompletedException, but completed with: " + e.getCause(), errorMsg.contains(AssertionCompletedException.class.getName()));
    }
}
Also used : AssertionSinks(com.hazelcast.jet.pipeline.test.AssertionSinks) Arrays(java.util.Arrays) JetInstance(com.hazelcast.jet.JetInstance) BeforeClass(org.junit.BeforeClass) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) Processors(com.hazelcast.jet.core.processor.Processors) ParallelStreamP(com.hazelcast.jet.pipeline.test.ParallelStreamP) HazelcastSerialParametersRunnerFactory(com.hazelcast.test.HazelcastSerialParametersRunnerFactory) JetAssert.assertFalse(com.hazelcast.jet.core.test.JetAssert.assertFalse) JetAssert.assertTrue(com.hazelcast.jet.core.test.JetAssert.assertTrue) Nonnull(javax.annotation.Nonnull) Job(com.hazelcast.jet.Job) Before(org.junit.Before) UseParametersRunnerFactory(org.junit.runners.Parameterized.UseParametersRunnerFactory) PredicateEx(com.hazelcast.function.PredicateEx) FunctionEx(com.hazelcast.function.FunctionEx) AfterClass(org.junit.AfterClass) LongStream(java.util.stream.LongStream) HazelcastParametrizedRunner(com.hazelcast.test.HazelcastParametrizedRunner) JetTestSupport(com.hazelcast.jet.core.JetTestSupport) Parameter(org.junit.runners.Parameterized.Parameter) ProcessorMetaSupplier(com.hazelcast.jet.core.ProcessorMetaSupplier) Collection(java.util.Collection) Test(org.junit.Test) CompletionException(java.util.concurrent.CompletionException) Traversers(com.hazelcast.jet.Traversers) Serializable(java.io.Serializable) Objects(java.util.Objects) JetAssert.fail(com.hazelcast.jet.core.test.JetAssert.fail) TestSources(com.hazelcast.jet.pipeline.test.TestSources) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) GeneratorFunction(com.hazelcast.jet.pipeline.test.GeneratorFunction) Hazelcast(com.hazelcast.core.Hazelcast) AssertionCompletedException(com.hazelcast.jet.pipeline.test.AssertionCompletedException) LongAccumulator(com.hazelcast.jet.accumulator.LongAccumulator) Functions.wholeItem(com.hazelcast.function.Functions.wholeItem) Assert(org.junit.Assert) AssertionCompletedException(com.hazelcast.jet.pipeline.test.AssertionCompletedException) CompletionException(java.util.concurrent.CompletionException) Job(com.hazelcast.jet.Job) Test(org.junit.Test)

Aggregations

CompletionException (java.util.concurrent.CompletionException)199 Test (org.junit.Test)80 CompletableFuture (java.util.concurrent.CompletableFuture)62 List (java.util.List)52 ArrayList (java.util.ArrayList)51 IOException (java.io.IOException)45 Map (java.util.Map)39 Collection (java.util.Collection)31 ExecutionException (java.util.concurrent.ExecutionException)31 HashMap (java.util.HashMap)30 Collections (java.util.Collections)24 TimeUnit (java.util.concurrent.TimeUnit)22 Collectors (java.util.stream.Collectors)22 FlinkException (org.apache.flink.util.FlinkException)22 Before (org.junit.Before)21 Duration (java.time.Duration)19 Arrays (java.util.Arrays)19 BeforeClass (org.junit.BeforeClass)19 ExecutorService (java.util.concurrent.ExecutorService)18 Nonnull (javax.annotation.Nonnull)17