Search in sources :

Example 11 with EXACTLY_ONCE

use of com.hazelcast.jet.config.ProcessingGuarantee.EXACTLY_ONCE in project hazelcast by hazelcast.

the class WriteKafkaP method init.

@Override
public void init(@Nonnull Outbox outbox, @Nonnull Context context) {
    this.context = context;
    ProcessingGuarantee guarantee = context.processingGuarantee() == EXACTLY_ONCE && !exactlyOnce ? AT_LEAST_ONCE : context.processingGuarantee();
    snapshotUtility = new TransactionPoolSnapshotUtility<>(outbox, context, false, guarantee, TXN_POOL_SIZE, (processorIndex, txnIndex) -> new KafkaTransactionId(context.jobId(), context.jobConfig().getName(), context.vertexName(), processorIndex, txnIndex), txnId -> {
        if (txnId != null) {
            properties.put("transactional.id", txnId.getKafkaId());
        }
        return new KafkaTransaction<>(txnId, properties, context.logger());
    }, txnId -> {
        try {
            recoverTransaction(txnId, true);
        } catch (ProducerFencedException e) {
            context.logger().warning("Failed to finish the commit of a transaction ID saved in the " + "snapshot, data loss can occur. Transaction id: " + txnId.getKafkaId(), e);
        }
    }, txnId -> recoverTransaction(txnId, false));
}
Also used : ProcessingGuarantee(com.hazelcast.jet.config.ProcessingGuarantee) LoggingUtil(com.hazelcast.jet.impl.util.LoggingUtil) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) Outbox(com.hazelcast.jet.core.Outbox) Processor(com.hazelcast.jet.core.Processor) HashMap(java.util.HashMap) KafkaProcessors(com.hazelcast.jet.kafka.KafkaProcessors) ExceptionUtil.sneakyThrow(com.hazelcast.jet.impl.util.ExceptionUtil.sneakyThrow) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Watermark(com.hazelcast.jet.core.Watermark) KafkaProducer(org.apache.kafka.clients.producer.KafkaProducer) ILogger(com.hazelcast.logging.ILogger) Map(java.util.Map) Inbox(com.hazelcast.jet.core.Inbox) Nonnull(javax.annotation.Nonnull) FunctionEx(com.hazelcast.function.FunctionEx) TransactionalResource(com.hazelcast.jet.impl.processor.TwoPhaseSnapshotCommitUtility.TransactionalResource) TimeoutException(org.apache.kafka.common.errors.TimeoutException) Properties(java.util.Properties) TransactionPoolSnapshotUtility(com.hazelcast.jet.impl.processor.TransactionPoolSnapshotUtility) EXACTLY_ONCE(com.hazelcast.jet.config.ProcessingGuarantee.EXACTLY_ONCE) SupplierEx(com.hazelcast.function.SupplierEx) Serializable(java.io.Serializable) Objects(java.util.Objects) Util.idToString(com.hazelcast.jet.Util.idToString) InvalidTxnStateException(org.apache.kafka.common.errors.InvalidTxnStateException) ProducerFencedException(org.apache.kafka.common.errors.ProducerFencedException) ProcessingGuarantee(com.hazelcast.jet.config.ProcessingGuarantee) Callback(org.apache.kafka.clients.producer.Callback) AT_LEAST_ONCE(com.hazelcast.jet.config.ProcessingGuarantee.AT_LEAST_ONCE) TwoPhaseSnapshotCommitUtility(com.hazelcast.jet.impl.processor.TwoPhaseSnapshotCommitUtility) ProducerFencedException(org.apache.kafka.common.errors.ProducerFencedException)

Example 12 with EXACTLY_ONCE

use of com.hazelcast.jet.config.ProcessingGuarantee.EXACTLY_ONCE in project hazelcast by hazelcast.

the class SourceBuilder_TopologyChangeTest method testTopologyChange.

private void testTopologyChange(Supplier<HazelcastInstance> secondMemberSupplier, Consumer<HazelcastInstance> changeTopologyFn, boolean assertMonotonicity) {
    stateRestored = false;
    StreamSource<Integer> source = SourceBuilder.timestampedStream("src", ctx -> new NumberGeneratorContext()).<Integer>fillBufferFn((src, buffer) -> {
        long expectedCount = NANOSECONDS.toMillis(System.nanoTime() - src.startTime);
        expectedCount = Math.min(expectedCount, src.current + 100);
        while (src.current < expectedCount) {
            buffer.add(src.current, src.current);
            src.current++;
        }
    }).createSnapshotFn(src -> {
        System.out.println("Will save " + src.current + " to snapshot");
        return src;
    }).restoreSnapshotFn((src, states) -> {
        stateRestored = true;
        assert states.size() == 1;
        src.restore(states.get(0));
        System.out.println("Restored " + src.current + " from snapshot");
    }).build();
    Config config = smallInstanceConfig();
    // restart sooner after member add
    config.getJetConfig().setScaleUpDelayMillis(1000);
    HazelcastInstance hz = createHazelcastInstance(config);
    HazelcastInstance possibleSecondNode = secondMemberSupplier.get();
    long windowSize = 100;
    IList<WindowResult<Long>> result = hz.getList("result-" + UuidUtil.newUnsecureUuidString());
    Pipeline p = Pipeline.create();
    p.readFrom(source).withNativeTimestamps(0).window(tumbling(windowSize)).aggregate(AggregateOperations.counting()).peek().writeTo(Sinks.list(result));
    Job job = hz.getJet().newJob(p, new JobConfig().setProcessingGuarantee(EXACTLY_ONCE).setSnapshotIntervalMillis(500));
    assertTrueEventually(() -> assertFalse("result list is still empty", result.isEmpty()));
    assertJobStatusEventually(job, JobStatus.RUNNING);
    JobRepository jr = new JobRepository(hz);
    waitForFirstSnapshot(jr, job.getId(), 10, false);
    assertFalse(stateRestored);
    changeTopologyFn.accept(possibleSecondNode);
    assertTrueEventually(() -> assertTrue("restoreSnapshotFn was not called", stateRestored));
    // wait until more results are added
    int oldSize = result.size();
    assertTrueEventually(() -> assertTrue("no more results added to the list", result.size() > oldSize));
    cancelAndJoin(job);
    // results should contain sequence of results, each with count=windowSize, monotonic, if job was
    // allowed to terminate gracefully
    Iterator<WindowResult<Long>> iterator = result.iterator();
    for (int i = 0; i < result.size(); i++) {
        WindowResult<Long> next = iterator.next();
        assertEquals(windowSize, (long) next.result());
        if (assertMonotonicity) {
            assertEquals(i * windowSize, next.start());
        }
    }
}
Also used : ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) NANOSECONDS(java.util.concurrent.TimeUnit.NANOSECONDS) RunWith(org.junit.runner.RunWith) Supplier(java.util.function.Supplier) HazelcastSerialClassRunner(com.hazelcast.test.HazelcastSerialClassRunner) WindowDefinition.tumbling(com.hazelcast.jet.pipeline.WindowDefinition.tumbling) UuidUtil(com.hazelcast.internal.util.UuidUtil) JobStatus(com.hazelcast.jet.core.JobStatus) Job(com.hazelcast.jet.Job) IList(com.hazelcast.collection.IList) JobRepository(com.hazelcast.jet.impl.JobRepository) Config(com.hazelcast.config.Config) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Iterator(java.util.Iterator) JetTestSupport(com.hazelcast.jet.core.JetTestSupport) EXACTLY_ONCE(com.hazelcast.jet.config.ProcessingGuarantee.EXACTLY_ONCE) JobConfig(com.hazelcast.jet.config.JobConfig) AggregateOperations(com.hazelcast.jet.aggregate.AggregateOperations) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Category(org.junit.experimental.categories.Category) Serializable(java.io.Serializable) Consumer(java.util.function.Consumer) Assert.assertFalse(org.junit.Assert.assertFalse) Assert.assertEquals(org.junit.Assert.assertEquals) WindowResult(com.hazelcast.jet.datamodel.WindowResult) Config(com.hazelcast.config.Config) JobConfig(com.hazelcast.jet.config.JobConfig) JobRepository(com.hazelcast.jet.impl.JobRepository) JobConfig(com.hazelcast.jet.config.JobConfig) HazelcastInstance(com.hazelcast.core.HazelcastInstance) WindowResult(com.hazelcast.jet.datamodel.WindowResult) Job(com.hazelcast.jet.Job)

Example 13 with EXACTLY_ONCE

use of com.hazelcast.jet.config.ProcessingGuarantee.EXACTLY_ONCE in project hazelcast by hazelcast.

the class GracefulShutdownTest method when_shutDown.

private void when_shutDown(boolean shutdownCoordinator, boolean snapshotted) {
    DAG dag = new DAG();
    final int numItems = 50_000;
    Vertex source = dag.newVertex("source", throttle(() -> new EmitIntegersP(numItems), 10_000)).localParallelism(1);
    Vertex sink = dag.newVertex("sink", SinkProcessors.writeListP("sink"));
    dag.edge(between(source, sink));
    Job job = client.getJet().newJob(dag, new JobConfig().setProcessingGuarantee(snapshotted ? EXACTLY_ONCE : NONE).setSnapshotIntervalMillis(HOURS.toMillis(1)));
    assertJobStatusEventually(job, JobStatus.RUNNING);
    logger.info("sleeping 1 sec");
    sleepSeconds(1);
    int shutDownInstance = shutdownCoordinator ? 0 : 1;
    int liveInstance = shutdownCoordinator ? 1 : 0;
    // When
    logger.info("Shutting down instance...");
    instances[shutDownInstance].shutdown();
    logger.info("Joining job...");
    job.join();
    logger.info("Joined");
    // Then
    // If the shutdown was graceful, output items must not be duplicated
    Map<Integer, Integer> expected;
    Map<Integer, Integer> actual = new ArrayList<>(instances[liveInstance].<Integer>getList("sink")).stream().collect(Collectors.toMap(Function.identity(), item -> 1, Integer::sum));
    if (snapshotted) {
        logger.info("savedCounters=" + EmitIntegersP.savedCounters);
        assertEquals(EmitIntegersP.savedCounters.toString(), 2, EmitIntegersP.savedCounters.size());
        int minCounter = EmitIntegersP.savedCounters.values().stream().mapToInt(Integer::intValue).min().getAsInt();
        expected = IntStream.range(0, numItems).boxed().collect(Collectors.toMap(Function.identity(), item -> item < minCounter ? 2 : 1));
        assertEquals(expected, actual);
    } else {
        // Items 0, 1, ... up to the point when the member was shut down should be 3 times
        // in the output: twice from before shutdown and one from after it, because it will start
        // from the beginning when the job is not snapshotted.
        assertEquals(3, actual.get(0).intValue());
        assertEquals(3, actual.get(1).intValue());
        assertEquals(1, actual.get(numItems - 1).intValue());
    }
}
Also used : IntStream(java.util.stream.IntStream) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) RunWith(org.junit.runner.RunWith) Function(java.util.function.Function) HazelcastSerialClassRunner(com.hazelcast.test.HazelcastSerialClassRunner) MapStore(com.hazelcast.map.MapStore) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) MapConfig(com.hazelcast.config.MapConfig) Future(java.util.concurrent.Future) TestUtil.throttle(com.hazelcast.jet.core.TestUtil.throttle) Map(java.util.Map) Nonnull(javax.annotation.Nonnull) Job(com.hazelcast.jet.Job) Before(org.junit.Before) JobRepository(com.hazelcast.jet.impl.JobRepository) Config(com.hazelcast.config.Config) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Collection(java.util.Collection) EXACTLY_ONCE(com.hazelcast.jet.config.ProcessingGuarantee.EXACTLY_ONCE) JobConfig(com.hazelcast.jet.config.JobConfig) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) SlowTest(com.hazelcast.test.annotation.SlowTest) Category(org.junit.experimental.categories.Category) Collectors(java.util.stream.Collectors) SupplierEx(com.hazelcast.function.SupplierEx) NoOutputSourceP(com.hazelcast.jet.core.TestProcessors.NoOutputSourceP) NONE(com.hazelcast.jet.config.ProcessingGuarantee.NONE) DynamicConfigurationAwareConfig(com.hazelcast.internal.dynamicconfig.DynamicConfigurationAwareConfig) BroadcastKey.broadcastKey(com.hazelcast.jet.core.BroadcastKey.broadcastKey) SinkProcessors(com.hazelcast.jet.core.processor.SinkProcessors) HOURS(java.util.concurrent.TimeUnit.HOURS) RUNNING(com.hazelcast.jet.core.JobStatus.RUNNING) Assert.assertEquals(org.junit.Assert.assertEquals) JetServiceBackend(com.hazelcast.jet.impl.JetServiceBackend) Edge.between(com.hazelcast.jet.core.Edge.between) ArrayList(java.util.ArrayList) Job(com.hazelcast.jet.Job) JobConfig(com.hazelcast.jet.config.JobConfig)

Example 14 with EXACTLY_ONCE

use of com.hazelcast.jet.config.ProcessingGuarantee.EXACTLY_ONCE in project hazelcast by hazelcast.

the class AsyncTransformUsingServiceP_IntegrationTest method before.

@Before
public void before() {
    journaledMap = instance().getMap(randomMapName("journaledMap"));
    journaledMap.putAll(IntStream.range(0, NUM_ITEMS).boxed().collect(toMap(i -> i, i -> i)));
    sinkList = instance().getList(randomMapName("sinkList"));
    jobConfig = new JobConfig().setProcessingGuarantee(EXACTLY_ONCE).setSnapshotIntervalMillis(0);
    serviceFactory = sharedService(pctx -> Executors.newFixedThreadPool(8), ExecutorService::shutdown);
}
Also used : ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) Traverser(com.hazelcast.jet.Traverser) ServiceFactories.sharedService(com.hazelcast.jet.pipeline.ServiceFactories.sharedService) QuickTest(com.hazelcast.test.annotation.QuickTest) BiFunctionEx(com.hazelcast.function.BiFunctionEx) Collectors.toMap(java.util.stream.Collectors.toMap) Arrays.asList(java.util.Arrays.asList) DAG(com.hazelcast.jet.core.DAG) SimpleTestInClusterSupport(com.hazelcast.jet.SimpleTestInClusterSupport) FunctionEx(com.hazelcast.function.FunctionEx) WatermarkPolicy(com.hazelcast.jet.core.WatermarkPolicy) HazelcastParametrizedRunner(com.hazelcast.test.HazelcastParametrizedRunner) Pipeline(com.hazelcast.jet.pipeline.Pipeline) Collection(java.util.Collection) JobConfig(com.hazelcast.jet.config.JobConfig) START_FROM_OLDEST(com.hazelcast.jet.pipeline.JournalInitialPosition.START_FROM_OLDEST) Category(org.junit.experimental.categories.Category) Executors(java.util.concurrent.Executors) Collectors.joining(java.util.stream.Collectors.joining) Sources(com.hazelcast.jet.pipeline.Sources) Stream(java.util.stream.Stream) EventJournalMapEvent(com.hazelcast.map.EventJournalMapEvent) SinkProcessors(com.hazelcast.jet.core.processor.SinkProcessors) IntStream(java.util.stream.IntStream) PredicateEx.alwaysTrue(com.hazelcast.function.PredicateEx.alwaysTrue) EdgeConfig(com.hazelcast.jet.config.EdgeConfig) BeforeClass(org.junit.BeforeClass) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) CompletableFuture(java.util.concurrent.CompletableFuture) HazelcastSerialParametersRunnerFactory(com.hazelcast.test.HazelcastSerialParametersRunnerFactory) Function(java.util.function.Function) DEFAULT_MAX_CONCURRENT_OPS(com.hazelcast.jet.pipeline.GeneralStage.DEFAULT_MAX_CONCURRENT_OPS) TestUtil.throttle(com.hazelcast.jet.core.TestUtil.throttle) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) ServiceFactory(com.hazelcast.jet.pipeline.ServiceFactory) ProcessorSupplier(com.hazelcast.jet.core.ProcessorSupplier) ExecutorService(java.util.concurrent.ExecutorService) Job(com.hazelcast.jet.Job) Before(org.junit.Before) UseParametersRunnerFactory(org.junit.runners.Parameterized.UseParametersRunnerFactory) IList(com.hazelcast.collection.IList) Config(com.hazelcast.config.Config) SourceProcessors.streamMapP(com.hazelcast.jet.core.processor.SourceProcessors.streamMapP) Parameter(org.junit.runners.Parameterized.Parameter) EXACTLY_ONCE(com.hazelcast.jet.config.ProcessingGuarantee.EXACTLY_ONCE) Sinks(com.hazelcast.jet.pipeline.Sinks) Traversers.traverseItems(com.hazelcast.jet.Traversers.traverseItems) Test(org.junit.Test) Vertex(com.hazelcast.jet.core.Vertex) FunctionEx.identity(com.hazelcast.function.FunctionEx.identity) EventTimePolicy.eventTimePolicy(com.hazelcast.jet.core.EventTimePolicy.eventTimePolicy) RUNNING(com.hazelcast.jet.core.JobStatus.RUNNING) TriFunction(com.hazelcast.jet.function.TriFunction) Assert.assertEquals(org.junit.Assert.assertEquals) IMap(com.hazelcast.map.IMap) Edge.between(com.hazelcast.jet.core.Edge.between) JobConfig(com.hazelcast.jet.config.JobConfig) Before(org.junit.Before)

Example 15 with EXACTLY_ONCE

use of com.hazelcast.jet.config.ProcessingGuarantee.EXACTLY_ONCE in project hazelcast by hazelcast.

the class VertexDef_HigherPrioritySourceTest method assertHigherPriorityVertices.

private void assertHigherPriorityVertices(Vertex... vertices) {
    JobConfig jobConfig = new JobConfig();
    Map<MemberInfo, ExecutionPlan> executionPlans = createExecutionPlans(nodeEngineImpl, membersView, dag, 0, 0, jobConfig, 0, false, null);
    ExecutionPlan plan = executionPlans.values().iterator().next();
    SnapshotContext ssContext = new SnapshotContext(mock(ILogger.class), "job", 0, EXACTLY_ONCE);
    // In the production code the plan#initialize is only called from places where we have already set up the
    // processor classloaders
    JetServiceBackend jetService = nodeEngineImpl.getService(JetServiceBackend.SERVICE_NAME);
    jetService.getJobClassLoaderService().getOrCreateClassLoader(jobConfig, 0, JobPhase.EXECUTION);
    try {
        jetService.getJobClassLoaderService().prepareProcessorClassLoaders(0);
        plan.initialize(nodeEngineImpl, 0, 0, ssContext, null, (InternalSerializationService) nodeEngineImpl.getSerializationService());
    } finally {
        jetService.getJobClassLoaderService().clearProcessorClassLoaders();
    }
    jetService.getJobClassLoaderService().tryRemoveClassloadersForJob(0, JobPhase.EXECUTION);
    Set<Integer> higherPriorityVertices = VertexDef.getHigherPriorityVertices(plan.getVertices());
    String actualHigherPriorityVertices = plan.getVertices().stream().filter(v -> higherPriorityVertices.contains(v.vertexId())).map(VertexDef::name).sorted().collect(joining("\n"));
    String expectedVertices = Arrays.stream(vertices).map(Vertex::getName).sorted().collect(joining("\n"));
    assertEquals(expectedVertices, actualHigherPriorityVertices);
}
Also used : AbstractProcessor(com.hazelcast.jet.core.AbstractProcessor) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) Arrays(java.util.Arrays) ExecutionPlanBuilder.createExecutionPlans(com.hazelcast.jet.impl.execution.init.ExecutionPlanBuilder.createExecutionPlans) SnapshotContext(com.hazelcast.jet.impl.execution.SnapshotContext) BeforeClass(org.junit.BeforeClass) MasterJobContext(com.hazelcast.jet.impl.MasterJobContext) QuickTest(com.hazelcast.test.annotation.QuickTest) ILogger(com.hazelcast.logging.ILogger) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) MemberInfo(com.hazelcast.internal.cluster.MemberInfo) Map(java.util.Map) Edge.from(com.hazelcast.jet.core.Edge.from) DAG(com.hazelcast.jet.core.DAG) Edge(com.hazelcast.jet.core.Edge) ClusterServiceImpl(com.hazelcast.internal.cluster.impl.ClusterServiceImpl) SimpleTestInClusterSupport(com.hazelcast.jet.SimpleTestInClusterSupport) NodeEngineImpl(com.hazelcast.spi.impl.NodeEngineImpl) JobPhase(com.hazelcast.jet.impl.JobClassLoaderService.JobPhase) Collections.nCopies(java.util.Collections.nCopies) ProcessorMetaSupplier(com.hazelcast.jet.core.ProcessorMetaSupplier) EXACTLY_ONCE(com.hazelcast.jet.config.ProcessingGuarantee.EXACTLY_ONCE) JobConfig(com.hazelcast.jet.config.JobConfig) Set(java.util.Set) Test(org.junit.Test) Category(org.junit.experimental.categories.Category) Field(java.lang.reflect.Field) Collectors.joining(java.util.stream.Collectors.joining) Vertex(com.hazelcast.jet.core.Vertex) List(java.util.List) Assert.assertEquals(org.junit.Assert.assertEquals) JetServiceBackend(com.hazelcast.jet.impl.JetServiceBackend) Edge.between(com.hazelcast.jet.core.Edge.between) Mockito.mock(org.mockito.Mockito.mock) Vertex(com.hazelcast.jet.core.Vertex) JobConfig(com.hazelcast.jet.config.JobConfig) SnapshotContext(com.hazelcast.jet.impl.execution.SnapshotContext) MemberInfo(com.hazelcast.internal.cluster.MemberInfo) ILogger(com.hazelcast.logging.ILogger) JetServiceBackend(com.hazelcast.jet.impl.JetServiceBackend)

Aggregations

EXACTLY_ONCE (com.hazelcast.jet.config.ProcessingGuarantee.EXACTLY_ONCE)17 JobConfig (com.hazelcast.jet.config.JobConfig)16 Assert.assertEquals (org.junit.Assert.assertEquals)16 Test (org.junit.Test)15 Job (com.hazelcast.jet.Job)14 Assert.assertTrue (org.junit.Assert.assertTrue)13 JobRepository (com.hazelcast.jet.impl.JobRepository)12 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)12 Map (java.util.Map)12 Category (org.junit.experimental.categories.Category)12 Before (org.junit.Before)11 List (java.util.List)10 Config (com.hazelcast.config.Config)9 HazelcastInstance (com.hazelcast.core.HazelcastInstance)9 FunctionEx (com.hazelcast.function.FunctionEx)9 Edge.between (com.hazelcast.jet.core.Edge.between)9 RUNNING (com.hazelcast.jet.core.JobStatus.RUNNING)9 Collectors (java.util.stream.Collectors)9 IntStream (java.util.stream.IntStream)9 IList (com.hazelcast.collection.IList)8