Search in sources :

Example 11 with LongAccumulator

use of com.hazelcast.jet.accumulator.LongAccumulator in project hazelcast by hazelcast.

the class PostgresCdcWhiteBlackListIntegrationTest method pipeline.

private Pipeline pipeline(StreamSource<ChangeRecord> source) {
    Pipeline pipeline = Pipeline.create();
    pipeline.readFrom(source).withNativeTimestamps(0).filter(t -> t.schema().startsWith(SCHEMA_PREFIX)).setLocalParallelism(1).<ChangeRecord>customTransform("filter_timestamps", filterTimestampsProcessorSupplier()).setLocalParallelism(1).groupingKey(record -> (Integer) record.key().toMap().get("id")).mapStateful(LongAccumulator::new, (accumulator, rowId, record) -> {
        long count = accumulator.get();
        accumulator.add(1);
        Operation operation = record.operation();
        RecordPart value = record.value();
        TableRow row = value.toObject(TableRow.class);
        return entry(rowId + "/" + count, operation + ":" + row);
    }).setLocalParallelism(1).peek().writeTo(Sinks.map(SINK_MAP_NAME));
    return pipeline;
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Arrays(java.util.Arrays) Connection(java.sql.Connection) Pipeline(com.hazelcast.jet.pipeline.Pipeline) Sinks(com.hazelcast.jet.pipeline.Sinks) RecordPart(com.hazelcast.jet.cdc.RecordPart) Test(org.junit.Test) StreamSource(com.hazelcast.jet.pipeline.StreamSource) Category(org.junit.experimental.categories.Category) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) List(java.util.List) Operation(com.hazelcast.jet.cdc.Operation) LongAccumulator(com.hazelcast.jet.accumulator.LongAccumulator) Util.entry(com.hazelcast.jet.Util.entry) Statement(java.sql.Statement) ChangeRecord(com.hazelcast.jet.cdc.ChangeRecord) JobStatus(com.hazelcast.jet.core.JobStatus) Job(com.hazelcast.jet.Job) Before(org.junit.Before) RecordPart(com.hazelcast.jet.cdc.RecordPart) Operation(com.hazelcast.jet.cdc.Operation) ChangeRecord(com.hazelcast.jet.cdc.ChangeRecord) Pipeline(com.hazelcast.jet.pipeline.Pipeline)

Example 12 with LongAccumulator

use of com.hazelcast.jet.accumulator.LongAccumulator in project hazelcast by hazelcast.

the class MetricsTest method customUnit.

@Test
public void customUnit() {
    pipeline.readFrom(TestSources.items(0L, 1L, 2L, 3L, 4L)).mapStateful(LongAccumulator::new, (acc, i) -> {
        acc.add(i);
        Metric metric = Metrics.metric("sum", Unit.COUNT);
        metric.set(acc.get());
        return acc.get();
    }).writeTo(Sinks.noop());
    Job job = runPipeline(pipeline.toDag());
    JobMetricsChecker checker = new JobMetricsChecker(job);
    checker.assertSummedMetricValue("sum", 10);
}
Also used : ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) AggregateOperations.counting(com.hazelcast.jet.aggregate.AggregateOperations.counting) Arrays(java.util.Arrays) QuickTest(com.hazelcast.test.annotation.QuickTest) RunWith(org.junit.runner.RunWith) Processor(com.hazelcast.jet.core.Processor) Processors(com.hazelcast.jet.core.processor.Processors) CompletableFuture(java.util.concurrent.CompletableFuture) ServiceFactories.nonSharedService(com.hazelcast.jet.pipeline.ServiceFactories.nonSharedService) HashSet(java.util.HashSet) TestProcessors(com.hazelcast.jet.core.TestProcessors) Arrays.asList(java.util.Arrays.asList) DAG(com.hazelcast.jet.core.DAG) Edge(com.hazelcast.jet.core.Edge) 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) FunctionEx(com.hazelcast.function.FunctionEx) Pipeline(com.hazelcast.jet.pipeline.Pipeline) JetTestSupport(com.hazelcast.jet.core.JetTestSupport) EXACTLY_ONCE(com.hazelcast.jet.config.ProcessingGuarantee.EXACTLY_ONCE) JobConfig(com.hazelcast.jet.config.JobConfig) Sinks(com.hazelcast.jet.pipeline.Sinks) Assert.assertTrue(org.junit.Assert.assertTrue) ResettableSingletonTraverser(com.hazelcast.jet.core.ResettableSingletonTraverser) Test(org.junit.Test) Category(org.junit.experimental.categories.Category) SupplierEx(com.hazelcast.function.SupplierEx) NoOutputSourceP(com.hazelcast.jet.core.TestProcessors.NoOutputSourceP) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) Vertex(com.hazelcast.jet.core.Vertex) TestSources(com.hazelcast.jet.pipeline.test.TestSources) List(java.util.List) LongAccumulator(com.hazelcast.jet.accumulator.LongAccumulator) Assert.assertFalse(org.junit.Assert.assertFalse) TransformP(com.hazelcast.jet.impl.processor.TransformP) FunctionEx.identity(com.hazelcast.function.FunctionEx.identity) HazelcastParallelClassRunner(com.hazelcast.test.HazelcastParallelClassRunner) RUNNING(com.hazelcast.jet.core.JobStatus.RUNNING) Assert.assertEquals(org.junit.Assert.assertEquals) Edge.between(com.hazelcast.jet.core.Edge.between) SinkProcessors.writeListP(com.hazelcast.jet.core.processor.SinkProcessors.writeListP) Job(com.hazelcast.jet.Job) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 13 with LongAccumulator

use of com.hazelcast.jet.accumulator.LongAccumulator in project hazelcast by hazelcast.

the class Processors_slidingWindowingIntegrationTest method smokeTest.

@Test
public void smokeTest() throws Exception {
    HazelcastInstance instance = createHazelcastInstance();
    SlidingWindowPolicy wDef = slidingWinPolicy(2000, 1000);
    DAG dag = new DAG();
    // to prevent serialization of whole class
    boolean isBatchLocal = isBatch;
    FunctionEx<? super MyEvent, ?> keyFn = MyEvent::getKey;
    ToLongFunctionEx<? super MyEvent> timestampFn = MyEvent::getTimestamp;
    List<MyEvent> inputData = singletonList(new MyEvent(10, "a", 1L));
    Vertex source = dag.newVertex("source", () -> new EmitListP(inputData, isBatchLocal)).localParallelism(1);
    Vertex insertPP = dag.newVertex("insertWmP", insertWatermarksP(eventTimePolicy(timestampFn, limitingLag(0), wDef.frameSize(), wDef.frameOffset(), 0))).localParallelism(1);
    Vertex sink = dag.newVertex("sink", SinkProcessors.writeListP("sink"));
    dag.edge(between(source, insertPP).isolated());
    AggregateOperation<LongAccumulator, Long> counting = counting();
    if (singleStageProcessor) {
        Vertex slidingWin = dag.newVertex("slidingWin", Processors.aggregateToSlidingWindowP(singletonList(keyFn), singletonList(timestampFn), TimestampKind.EVENT, wDef, 0L, counting, KeyedWindowResult::new));
        dag.edge(between(insertPP, slidingWin).partitioned(MyEvent::getKey).distributed()).edge(between(slidingWin, sink));
    } else {
        Vertex accumulateByFrame = dag.newVertex("accumulateByFrame", Processors.accumulateByFrameP(singletonList(keyFn), singletonList(timestampFn), TimestampKind.EVENT, wDef, counting.withIdentityFinish()));
        Vertex slidingWin = dag.newVertex("slidingWin", combineToSlidingWindowP(wDef, counting, KeyedWindowResult::new));
        dag.edge(between(insertPP, accumulateByFrame).partitioned(keyFn)).edge(between(accumulateByFrame, slidingWin).partitioned(entryKey()).distributed()).edge(between(slidingWin, sink));
    }
    Job job = instance.getJet().newJob(dag);
    if (isBatch) {
        job.join();
    }
    IList<MyEvent> sinkList = instance.getList("sink");
    List<KeyedWindowResult<String, Long>> expectedOutput = asList(new KeyedWindowResult<>(-1000, 1000, "a", 1L), new KeyedWindowResult<>(0, 2000, "a", 1L));
    assertTrueEventually(() -> assertEquals(streamToString(expectedOutput.stream()), streamToString(new ArrayList<>(sinkList).stream())), 5);
    // wait a little more and make sure, that there are no more frames
    Thread.sleep(1000);
    assertEquals(expectedOutput.size(), sinkList.size());
}
Also used : ArrayList(java.util.ArrayList) KeyedWindowResult(com.hazelcast.jet.datamodel.KeyedWindowResult) LongAccumulator(com.hazelcast.jet.accumulator.LongAccumulator) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Job(com.hazelcast.jet.Job) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 14 with LongAccumulator

use of com.hazelcast.jet.accumulator.LongAccumulator in project hazelcast by hazelcast.

the class SessionWindowP_failoverTest method init.

private void init(ProcessingGuarantee guarantee) throws Exception {
    AggregateOperation1<Object, LongAccumulator, Long> aggrOp = counting();
    p = new SessionWindowP<>(5000, 0L, singletonList((ToLongFunctionEx<Entry<?, Long>>) Entry::getValue), singletonList(entryKey()), aggrOp, KeyedWindowResult::new);
    Outbox outbox = new TestOutbox(128);
    Context context = new TestProcessorContext().setProcessingGuarantee(guarantee);
    p.init(outbox, context);
}
Also used : LongAccumulator(com.hazelcast.jet.accumulator.LongAccumulator) TestProcessorContext(com.hazelcast.jet.core.test.TestProcessorContext) Context(com.hazelcast.jet.core.Processor.Context) Entry(java.util.Map.Entry) TestOutbox(com.hazelcast.jet.core.test.TestOutbox) Outbox(com.hazelcast.jet.core.Outbox) TestOutbox(com.hazelcast.jet.core.test.TestOutbox) TestProcessorContext(com.hazelcast.jet.core.test.TestProcessorContext)

Example 15 with LongAccumulator

use of com.hazelcast.jet.accumulator.LongAccumulator in project hazelcast by hazelcast.

the class OrderedBatchParallelismTest method applyTransformAndGetDag.

private DAG applyTransformAndGetDag(FunctionEx<BatchStage<Long>, BatchStage<Long>> transform) {
    PipelineImpl p = (PipelineImpl) Pipeline.create().setPreserveOrder(true);
    BatchStage<Long> source = p.readFrom(TestSources.items(1L)).setLocalParallelism(UPSTREAM_PARALLELISM);
    BatchStage<Long> applied = source.apply(transform);
    applied.mapStateful(LongAccumulator::new, (s, x) -> x).writeTo(Sinks.noop());
    return p.toDag(PIPELINE_CTX);
}
Also used : PipelineImpl(com.hazelcast.jet.impl.pipeline.PipelineImpl) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) AggregateOperations.counting(com.hazelcast.jet.aggregate.AggregateOperations.counting) Arrays(java.util.Arrays) QuickTest(com.hazelcast.test.annotation.QuickTest) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) Processors(com.hazelcast.jet.core.processor.Processors) HazelcastSerialParametersRunnerFactory(com.hazelcast.test.HazelcastSerialParametersRunnerFactory) DAG(com.hazelcast.jet.core.DAG) UseParametersRunnerFactory(org.junit.runners.Parameterized.UseParametersRunnerFactory) FunctionEx(com.hazelcast.function.FunctionEx) HazelcastParametrizedRunner(com.hazelcast.test.HazelcastParametrizedRunner) Parameter(org.junit.runners.Parameterized.Parameter) Collection(java.util.Collection) Test(org.junit.Test) Traversers(com.hazelcast.jet.Traversers) Category(org.junit.experimental.categories.Category) Vertex(com.hazelcast.jet.core.Vertex) TestSources(com.hazelcast.jet.pipeline.test.TestSources) List(java.util.List) LongAccumulator(com.hazelcast.jet.accumulator.LongAccumulator) Functions.wholeItem(com.hazelcast.function.Functions.wholeItem) Context(com.hazelcast.jet.impl.pipeline.PipelineImpl.Context) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) PipelineImpl(com.hazelcast.jet.impl.pipeline.PipelineImpl)

Aggregations

LongAccumulator (com.hazelcast.jet.accumulator.LongAccumulator)43 Test (org.junit.Test)32 Category (org.junit.experimental.categories.Category)24 RunWith (org.junit.runner.RunWith)20 QuickTest (com.hazelcast.test.annotation.QuickTest)19 Arrays (java.util.Arrays)17 List (java.util.List)17 Assert.assertEquals (org.junit.Assert.assertEquals)17 Job (com.hazelcast.jet.Job)16 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)16 HazelcastInstance (com.hazelcast.core.HazelcastInstance)14 FunctionEx (com.hazelcast.function.FunctionEx)14 Pipeline (com.hazelcast.jet.pipeline.Pipeline)14 Util.entry (com.hazelcast.jet.Util.entry)13 HazelcastParallelClassRunner (com.hazelcast.test.HazelcastParallelClassRunner)13 Entry (java.util.Map.Entry)13 SupplierEx (com.hazelcast.function.SupplierEx)11 Sinks (com.hazelcast.jet.pipeline.Sinks)11 Tag.tag0 (com.hazelcast.jet.datamodel.Tag.tag0)10 Tag.tag1 (com.hazelcast.jet.datamodel.Tag.tag1)10