Search in sources :

Example 1 with JobProxy

use of com.hazelcast.jet.impl.JobProxy in project hazelcast by hazelcast.

the class KinesisIntegrationTest method restart_staticStream.

private void restart_staticStream(boolean graceful) {
    HELPER.createStream(3);
    JobConfig jobConfig = new JobConfig().setProcessingGuarantee(ProcessingGuarantee.AT_LEAST_ONCE).setSnapshotIntervalMillis(SECONDS.toMillis(1));
    Job job = hz().getJet().newJob(getPipeline(kinesisSource().build()), jobConfig);
    Map<String, List<String>> expectedMessages = sendMessages();
    // wait for some data to start coming out of the pipeline
    assertTrueEventually(() -> assertFalse(results.isEmpty()));
    ((JobProxy) job).restart(graceful);
    assertMessages(expectedMessages, true, !graceful);
}
Also used : JobProxy(com.hazelcast.jet.impl.JobProxy) List(java.util.List) Job(com.hazelcast.jet.Job) JobConfig(com.hazelcast.jet.config.JobConfig)

Example 2 with JobProxy

use of com.hazelcast.jet.impl.JobProxy in project hazelcast by hazelcast.

the class KinesisIntegrationTest method restart_dynamicStream.

private void restart_dynamicStream(boolean graceful) {
    HELPER.createStream(3);
    JobConfig jobConfig = new JobConfig().setProcessingGuarantee(ProcessingGuarantee.AT_LEAST_ONCE).setSnapshotIntervalMillis(SECONDS.toMillis(1));
    Job job = hz().getJet().newJob(getPipeline(kinesisSource().build()), jobConfig);
    Map<String, List<String>> expectedMessages = sendMessages();
    // wait for some data to start coming out of the pipeline
    assertTrueEventually(() -> assertFalse(results.isEmpty()));
    List<Shard> openShards = listOpenShards();
    Shard shard1 = openShards.get(0);
    Shard shard2 = openShards.get(1);
    Shard shard3 = openShards.get(2);
    splitShard(shard1);
    HELPER.waitForStreamToActivate();
    assertOpenShards(4, shard1);
    mergeShards(shard2, shard3);
    HELPER.waitForStreamToActivate();
    assertOpenShards(3, shard2, shard3);
    ((JobProxy) job).restart(graceful);
    assertMessages(expectedMessages, false, !graceful);
}
Also used : JobProxy(com.hazelcast.jet.impl.JobProxy) List(java.util.List) Job(com.hazelcast.jet.Job) Shard(com.amazonaws.services.kinesis.model.Shard) JobConfig(com.hazelcast.jet.config.JobConfig)

Example 3 with JobProxy

use of com.hazelcast.jet.impl.JobProxy in project hazelcast by hazelcast.

the class JmsSourceIntegrationTestBase method stressTest.

private void stressTest(boolean graceful, ProcessingGuarantee maxGuarantee, boolean useTopic) throws Exception {
    lastListInStressTest = null;
    final int MESSAGE_COUNT = 4_000;
    Pipeline p = Pipeline.create();
    String destName = "queue-" + counter++;
    JmsSourceBuilder sourceBuilder;
    if (useTopic) {
        sourceBuilder = Sources.jmsTopicBuilder(getConnectionFactory()).sharedConsumer(true).consumerFn(s -> s.createSharedDurableConsumer(s.createTopic(destName), "foo-consumer"));
        // create the durable subscriber now so that it doesn't lose the initial messages
        try (Connection conn = getConnectionFactory().get().createConnection()) {
            conn.setClientID("foo-client-id");
            try (Session sess = conn.createSession(false, DUPS_OK_ACKNOWLEDGE)) {
                sess.createDurableSubscriber(sess.createTopic(destName), "foo-consumer");
            }
        }
    } else {
        sourceBuilder = Sources.jmsQueueBuilder(getConnectionFactory()).destinationName(destName);
    }
    p.readFrom(sourceBuilder.maxGuarantee(maxGuarantee).build(msg -> Long.parseLong(((TextMessage) msg).getText()))).withoutTimestamps().peek().mapStateful(CopyOnWriteArrayList<Long>::new, (list, item) -> {
        lastListInStressTest = list;
        list.add(item);
        return null;
    }).writeTo(Sinks.logger());
    Job job = instance().getJet().newJob(p, new JobConfig().setProcessingGuarantee(ProcessingGuarantee.EXACTLY_ONCE).setSnapshotIntervalMillis(50));
    assertJobStatusEventually(job, RUNNING);
    // start a producer that will produce MESSAGE_COUNT messages on the background to the queue, 1000 msgs/s
    @SuppressWarnings("rawtypes") Future producerFuture = spawn(() -> {
        try (Connection connection = getConnectionFactory().get().createConnection();
            Session session = connection.createSession(false, AUTO_ACKNOWLEDGE);
            MessageProducer producer = session.createProducer(useTopic ? session.createTopic(destName) : session.createQueue(destName))) {
            long startTime = System.nanoTime();
            for (int i = 0; i < MESSAGE_COUNT; i++) {
                producer.send(session.createTextMessage(String.valueOf(i)));
                Thread.sleep(Math.max(0, i - NANOSECONDS.toMillis(System.nanoTime() - startTime)));
            }
        } catch (Exception e) {
            throw sneakyThrow(e);
        }
    });
    int iteration = 0;
    JobRepository jr = new JobRepository(instance());
    waitForFirstSnapshot(jr, job.getId(), 20, true);
    while (!producerFuture.isDone()) {
        Thread.sleep(ThreadLocalRandom.current().nextInt(200));
        // We also do it before the first restart to workaround https://issues.apache.org/jira/browse/ARTEMIS-2546
        if (iteration++ % 3 == 0) {
            waitForNextSnapshot(jr, job.getId(), 20, true);
        }
        ((JobProxy) job).restart(graceful);
        assertJobStatusEventually(job, RUNNING);
    }
    // call for the side-effect of throwing if the producer failed
    producerFuture.get();
    assertTrueEventually(() -> {
        Map<Long, Long> counts = lastListInStressTest.stream().collect(Collectors.groupingBy(Function.identity(), TreeMap::new, Collectors.counting()));
        for (long i = 0; i < MESSAGE_COUNT; i++) {
            counts.putIfAbsent(i, 0L);
        }
        String countsStr = "counts: " + counts;
        if (maxGuarantee == NONE) {
            // we don't assert anything and only wait little more and check that the job didn't fail
            sleepSeconds(1);
        } else {
            // in EXACTLY_ONCE the list must have each item exactly once
            // in AT_LEAST_ONCE the list must have each item at least once
            assertTrue(countsStr, counts.values().stream().allMatch(cnt -> maxGuarantee == EXACTLY_ONCE ? cnt == 1 : cnt > 0));
        }
        logger.info(countsStr);
    }, 30);
    assertEquals(job.getStatus(), RUNNING);
}
Also used : AggregateOperations.counting(com.hazelcast.jet.aggregate.AggregateOperations.counting) Mockito.doThrow(org.mockito.Mockito.doThrow) Session(javax.jms.Session) Future(java.util.concurrent.Future) WindowDefinition.tumbling(com.hazelcast.jet.pipeline.WindowDefinition.tumbling) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) MessageProducer(javax.jms.MessageProducer) JobStatus(com.hazelcast.jet.core.JobStatus) SimpleTestInClusterSupport(com.hazelcast.jet.SimpleTestInClusterSupport) FunctionEx(com.hazelcast.function.FunctionEx) Pipeline(com.hazelcast.jet.pipeline.Pipeline) JobConfig(com.hazelcast.jet.config.JobConfig) PredicateEx.alwaysFalse(com.hazelcast.function.PredicateEx.alwaysFalse) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Instant(java.time.Instant) DUPS_OK_ACKNOWLEDGE(javax.jms.Session.DUPS_OK_ACKNOWLEDGE) JMSException(javax.jms.JMSException) Collectors(java.util.stream.Collectors) SupplierEx(com.hazelcast.function.SupplierEx) ZoneId(java.time.ZoneId) Sources(com.hazelcast.jet.pipeline.Sources) List(java.util.List) MessageConsumer(javax.jms.MessageConsumer) Assert.assertFalse(org.junit.Assert.assertFalse) WindowResult(com.hazelcast.jet.datamodel.WindowResult) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeClass(org.junit.BeforeClass) NANOSECONDS(java.util.concurrent.TimeUnit.NANOSECONDS) AUTO_ACKNOWLEDGE(javax.jms.Session.AUTO_ACKNOWLEDGE) MINUTES(java.util.concurrent.TimeUnit.MINUTES) JobProxy(com.hazelcast.jet.impl.JobProxy) StreamSource(com.hazelcast.jet.pipeline.StreamSource) ExceptionUtil.sneakyThrow(com.hazelcast.jet.impl.util.ExceptionUtil.sneakyThrow) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) Function(java.util.function.Function) ArrayList(java.util.ArrayList) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) MapWatermarksToString.mapWatermarksToString(com.hazelcast.jet.core.TestProcessors.MapWatermarksToString.mapWatermarksToString) JmsSourceBuilder(com.hazelcast.jet.pipeline.JmsSourceBuilder) Message(javax.jms.Message) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) Job(com.hazelcast.jet.Job) Before(org.junit.Before) IList(com.hazelcast.collection.IList) JobRepository(com.hazelcast.jet.impl.JobRepository) Connection(javax.jms.Connection) TextMessage(javax.jms.TextMessage) EXACTLY_ONCE(com.hazelcast.jet.config.ProcessingGuarantee.EXACTLY_ONCE) Sinks(com.hazelcast.jet.pipeline.Sinks) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) NONE(com.hazelcast.jet.config.ProcessingGuarantee.NONE) TreeMap(java.util.TreeMap) JmsTestUtil.consumeMessages(com.hazelcast.jet.impl.connector.JmsTestUtil.consumeMessages) RUNNING(com.hazelcast.jet.core.JobStatus.RUNNING) ProcessingGuarantee(com.hazelcast.jet.config.ProcessingGuarantee) AT_LEAST_ONCE(com.hazelcast.jet.config.ProcessingGuarantee.AT_LEAST_ONCE) ConnectionFactory(javax.jms.ConnectionFactory) Assert.assertEquals(org.junit.Assert.assertEquals) Connection(javax.jms.Connection) MapWatermarksToString.mapWatermarksToString(com.hazelcast.jet.core.TestProcessors.MapWatermarksToString.mapWatermarksToString) JobRepository(com.hazelcast.jet.impl.JobRepository) JmsSourceBuilder(com.hazelcast.jet.pipeline.JmsSourceBuilder) JobConfig(com.hazelcast.jet.config.JobConfig) JMSException(javax.jms.JMSException) Pipeline(com.hazelcast.jet.pipeline.Pipeline) JobProxy(com.hazelcast.jet.impl.JobProxy) Future(java.util.concurrent.Future) MessageProducer(javax.jms.MessageProducer) Job(com.hazelcast.jet.Job) Session(javax.jms.Session)

Example 4 with JobProxy

use of com.hazelcast.jet.impl.JobProxy in project hazelcast by hazelcast.

the class SinkStressTestUtil method test_withRestarts.

public static void test_withRestarts(@Nonnull HazelcastInstance instance, @Nonnull ILogger logger, @Nonnull Sink<Integer> sink, boolean graceful, boolean exactlyOnce, @Nonnull SupplierEx<List<Integer>> actualItemsSupplier) {
    int numItems = 1000;
    Pipeline p = Pipeline.create();
    p.readFrom(SourceBuilder.stream("src", procCtx -> new int[] { procCtx.globalProcessorIndex() == 0 ? 0 : Integer.MAX_VALUE }).<Integer>fillBufferFn((ctx, buf) -> {
        if (ctx[0] < numItems) {
            buf.add(ctx[0]++);
            sleepMillis(5);
        }
    }).distributed(1).createSnapshotFn(ctx -> ctx[0] < Integer.MAX_VALUE ? ctx[0] : null).restoreSnapshotFn((ctx, state) -> ctx[0] = ctx[0] != Integer.MAX_VALUE ? state.get(0) : Integer.MAX_VALUE).build()).withoutTimestamps().peek().writeTo(sink);
    JobConfig config = new JobConfig().setProcessingGuarantee(exactlyOnce ? EXACTLY_ONCE : AT_LEAST_ONCE).setSnapshotIntervalMillis(50);
    JobProxy job = (JobProxy) instance.getJet().newJob(p, config);
    long endTime = System.nanoTime() + SECONDS.toNanos(TEST_TIMEOUT_SECONDS);
    int lastCount = 0;
    String expectedRows = IntStream.range(0, numItems).mapToObj(i -> i + (exactlyOnce ? "=1" : "")).collect(joining("\n"));
    // We'll restart once, then restart again after a short sleep (possibly during initialization),
    // and then assert some output so that the test isn't constantly restarting without any progress
    Long lastExecutionId = null;
    for (; ; ) {
        lastExecutionId = assertJobRunningEventually(instance, job, lastExecutionId);
        job.restart(graceful);
        lastExecutionId = assertJobRunningEventually(instance, job, lastExecutionId);
        sleepMillis(ThreadLocalRandom.current().nextInt(400));
        job.restart(graceful);
        try {
            List<Integer> actualItems;
            Set<Integer> distinctActualItems;
            do {
                actualItems = actualItemsSupplier.get();
                distinctActualItems = new HashSet<>(actualItems);
            } while (distinctActualItems.size() < Math.min(numItems, 100 + lastCount) && System.nanoTime() < endTime);
            lastCount = distinctActualItems.size();
            logger.info("number of committed items in the sink so far: " + lastCount);
            if (exactlyOnce) {
                String actualItemsStr = actualItems.stream().collect(groupingBy(identity(), TreeMap::new, counting())).entrySet().stream().map(Object::toString).collect(joining("\n"));
                assertEquals(expectedRows, actualItemsStr);
            } else {
                assertEquals(expectedRows, distinctActualItems.stream().map(Objects::toString).collect(joining("\n")));
            }
            // if content matches, break the loop. Otherwise restart and try again
            break;
        } catch (AssertionError e) {
            if (System.nanoTime() >= endTime) {
                throw e;
            }
        }
    }
}
Also used : IntStream(java.util.stream.IntStream) Collectors.counting(java.util.stream.Collectors.counting) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) JobProxy(com.hazelcast.jet.impl.JobProxy) HashSet(java.util.HashSet) ILogger(com.hazelcast.logging.ILogger) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Nonnull(javax.annotation.Nonnull) HazelcastInstance(com.hazelcast.core.HazelcastInstance) HazelcastTestSupport.sleepMillis(com.hazelcast.test.HazelcastTestSupport.sleepMillis) Pipeline(com.hazelcast.jet.pipeline.Pipeline) EXACTLY_ONCE(com.hazelcast.jet.config.ProcessingGuarantee.EXACTLY_ONCE) JobConfig(com.hazelcast.jet.config.JobConfig) Set(java.util.Set) SupplierEx(com.hazelcast.function.SupplierEx) Collectors.joining(java.util.stream.Collectors.joining) Objects(java.util.Objects) List(java.util.List) TreeMap(java.util.TreeMap) JetTestSupport.assertJobRunningEventually(com.hazelcast.jet.core.JetTestSupport.assertJobRunningEventually) Function.identity(java.util.function.Function.identity) SourceBuilder(com.hazelcast.jet.pipeline.SourceBuilder) AT_LEAST_ONCE(com.hazelcast.jet.config.ProcessingGuarantee.AT_LEAST_ONCE) Sink(com.hazelcast.jet.pipeline.Sink) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Assert.assertEquals(org.junit.Assert.assertEquals) JobConfig(com.hazelcast.jet.config.JobConfig) Pipeline(com.hazelcast.jet.pipeline.Pipeline) JobProxy(com.hazelcast.jet.impl.JobProxy) Objects(java.util.Objects)

Example 5 with JobProxy

use of com.hazelcast.jet.impl.JobProxy in project hazelcast by hazelcast.

the class PostgresCdcIntegrationTest method stressTest_exactlyOnce.

public void stressTest_exactlyOnce(boolean graceful) throws Exception {
    int updates = 1000;
    int restarts = 10;
    int snapshotIntervalMs = 100;
    Pipeline pipeline = customersPipeline(Long.MAX_VALUE);
    JobConfig config = new JobConfig().setProcessingGuarantee(ProcessingGuarantee.EXACTLY_ONCE).setSnapshotIntervalMillis(snapshotIntervalMs);
    HazelcastInstance hz = createHazelcastInstances(2)[0];
    Job job = hz.getJet().newJob(pipeline, config);
    JetTestSupport.assertJobStatusEventually(job, JobStatus.RUNNING);
    assertEqualsEventually(() -> hz.getMap("results").size(), 4);
    // make sure the job stores a Postgres WAL offset so that it won't trigger database snapshots after any restart
    // multiple snapshots are a problem for this test, because it is updating the same row, so subsequent snapshots
    // will return different images
    JobRepository jr = new JobRepository(hz);
    waitForNextSnapshot(jr, job.getId(), 20, false);
    String lsnFlushedBeforeRestart = getConfirmedFlushLsn();
    Future<?> dbChangesFuture = spawn(() -> uncheckRun(() -> {
        for (int i = 1; i <= updates; i++) {
            executeBatch("UPDATE customers SET first_name='Anne" + i + "' WHERE id=1004");
        }
    }));
    for (int i = 0; i < restarts; i++) {
        ((JobProxy) job).restart(graceful);
        assertJobStatusEventually(job, RUNNING);
        Thread.sleep(ThreadLocalRandom.current().nextInt(snapshotIntervalMs * 2));
    }
    JetTestSupport.assertJobStatusEventually(job, JobStatus.RUNNING);
    try {
        List<String> expectedPatterns = new ArrayList<>(Arrays.asList("1001/00000:SYNC:Customer \\{id=1001, firstName=Sally, lastName=Thomas, " + "email=sally.thomas@acme.com\\}", "1002/00000:SYNC:Customer \\{id=1002, firstName=George, lastName=Bailey, " + "email=gbailey@foobar.com\\}", "1003/00000:SYNC:Customer \\{id=1003, firstName=Edward, lastName=Walker, " + "email=ed@walker.com\\}", "1004/00000:SYNC:Customer \\{id=1004, firstName=Anne, lastName=Kretchmar, " + "email=annek@noanswer.org\\}"));
        for (int i = 1; i <= updates; i++) {
            expectedPatterns.add("1004/" + format("%05d", i) + ":UPDATE:Customer \\{id=1004, firstName=Anne" + i + ", lastName=Kretchmar, email=annek@noanswer.org\\}");
        }
        assertTrueEventually(() -> assertMatch(expectedPatterns, mapResultsToSortedList(hz.getMap("results"))));
        assertTrueEventually(() -> assertNotEquals(lsnFlushedBeforeRestart, getConfirmedFlushLsn()));
    } finally {
        job.cancel();
        assertJobStatusEventually(job, JobStatus.FAILED);
        dbChangesFuture.get();
    }
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) JobProxy(com.hazelcast.jet.impl.JobProxy) ArrayList(java.util.ArrayList) Job(com.hazelcast.jet.Job) JobRepository(com.hazelcast.jet.impl.JobRepository) JobConfig(com.hazelcast.jet.config.JobConfig) Pipeline(com.hazelcast.jet.pipeline.Pipeline)

Aggregations

JobConfig (com.hazelcast.jet.config.JobConfig)6 JobProxy (com.hazelcast.jet.impl.JobProxy)6 Job (com.hazelcast.jet.Job)5 Pipeline (com.hazelcast.jet.pipeline.Pipeline)4 List (java.util.List)4 EXACTLY_ONCE (com.hazelcast.jet.config.ProcessingGuarantee.EXACTLY_ONCE)3 JobRepository (com.hazelcast.jet.impl.JobRepository)3 HazelcastInstance (com.hazelcast.core.HazelcastInstance)2 SupplierEx (com.hazelcast.function.SupplierEx)2 SimpleTestInClusterSupport (com.hazelcast.jet.SimpleTestInClusterSupport)2 AT_LEAST_ONCE (com.hazelcast.jet.config.ProcessingGuarantee.AT_LEAST_ONCE)2 RUNNING (com.hazelcast.jet.core.JobStatus.RUNNING)2 Sinks (com.hazelcast.jet.pipeline.Sinks)2 SourceBuilder (com.hazelcast.jet.pipeline.SourceBuilder)2 TreeMap (java.util.TreeMap)2 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Shard (com.amazonaws.services.kinesis.model.Shard)1 JSON (com.fasterxml.jackson.jr.ob.JSON)1 IList (com.hazelcast.collection.IList)1