Search in sources :

Example 81 with Pipeline

use of com.hazelcast.jet.pipeline.Pipeline in project hazelcast by hazelcast.

the class MySqlCdcIntegrationTest method orders.

@Test
@Category(NightlyTest.class)
public void orders() {
    // given
    List<String> expectedRecords = Arrays.asList("10001/0:INSERT:Order {orderNumber=10001, orderDate=" + new Date(1452902400000L) + ", quantity=1, productId=102}", "10002/0:INSERT:Order {orderNumber=10002, orderDate=" + new Date(1452988800000L) + ", quantity=2, productId=105}", "10003/0:INSERT:Order {orderNumber=10003, orderDate=" + new Date(1455840000000L) + ", quantity=2, productId=106}", "10004/0:INSERT:Order {orderNumber=10004, orderDate=" + new Date(1456012800000L) + ", quantity=1, productId=107}");
    Pipeline pipeline = Pipeline.create();
    pipeline.readFrom(source("orders")).withoutTimestamps().groupingKey(MySqlCdcIntegrationTest::getOrderNumber).mapStateful(LongAccumulator::new, (accumulator, orderId, record) -> {
        long count = accumulator.get();
        accumulator.add(1);
        Operation operation = record.operation();
        RecordPart value = record.value();
        Order order = value.toObject(Order.class);
        return entry(orderId + "/" + count, operation + ":" + order);
    }).setLocalParallelism(1).writeTo(Sinks.map("results"));
    // when
    HazelcastInstance hz = createHazelcastInstances(2)[0];
    Job job = hz.getJet().newJob(pipeline);
    // then
    try {
        assertEqualsEventually(() -> mapResultsToSortedList(hz.getMap("results")), expectedRecords);
    } finally {
        job.cancel();
        assertJobStatusEventually(job, JobStatus.FAILED);
    }
}
Also used : LongAccumulator(com.hazelcast.jet.accumulator.LongAccumulator) HazelcastInstance(com.hazelcast.core.HazelcastInstance) RecordPart(com.hazelcast.jet.cdc.RecordPart) Operation(com.hazelcast.jet.cdc.Operation) Job(com.hazelcast.jet.Job) Date(java.util.Date) Pipeline(com.hazelcast.jet.pipeline.Pipeline) Category(org.junit.experimental.categories.Category) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 82 with Pipeline

use of com.hazelcast.jet.pipeline.Pipeline in project hazelcast by hazelcast.

the class MySqlCdcIntegrationTest method customers.

@Test
@Category(QuickTest.class)
public void customers() throws Exception {
    // given
    List<String> expectedRecords = Arrays.asList("1001/0:INSERT:Customer {id=1001, firstName=Sally, lastName=Thomas, email=sally.thomas@acme.com}", "1002/0:INSERT:Customer {id=1002, firstName=George, lastName=Bailey, email=gbailey@foobar.com}", "1003/0:INSERT:Customer {id=1003, firstName=Edward, lastName=Walker, email=ed@walker.com}", "1004/0:INSERT:Customer {id=1004, firstName=Anne, lastName=Kretchmar, email=annek@noanswer.org}", "1004/1:UPDATE:Customer {id=1004, firstName=Anne Marie, lastName=Kretchmar, email=annek@noanswer.org}", "1005/0:INSERT:Customer {id=1005, firstName=Jason, lastName=Bourne, email=jason@bourne.org}", "1005/1:DELETE:Customer {id=1005, firstName=Jason, lastName=Bourne, email=jason@bourne.org}");
    Pipeline pipeline = Pipeline.create();
    pipeline.readFrom(source("customers")).withNativeTimestamps(0).<ChangeRecord>customTransform("filter_timestamps", filterTimestampsProcessorSupplier()).groupingKey(record -> (Integer) record.key().toMap().get("id")).mapStateful(LongAccumulator::new, (accumulator, customerId, record) -> {
        long count = accumulator.get();
        accumulator.add(1);
        Operation operation = record.operation();
        RecordPart value = record.value();
        Customer customer = value.toObject(Customer.class);
        return entry(customerId + "/" + count, operation + ":" + customer);
    }).setLocalParallelism(1).writeTo(Sinks.map("results"));
    // when
    HazelcastInstance hz = createHazelcastInstances(2)[0];
    Job job = hz.getJet().newJob(pipeline);
    // then
    assertEqualsEventually(() -> hz.getMap("results").size(), 4);
    // when
    try (Connection connection = getConnection(mysql, "inventory")) {
        Statement statement = connection.createStatement();
        statement.addBatch("UPDATE customers SET first_name='Anne Marie' WHERE id=1004");
        statement.addBatch("INSERT INTO customers VALUES (1005, 'Jason', 'Bourne', 'jason@bourne.org')");
        statement.addBatch("DELETE FROM customers WHERE id=1005");
        statement.executeBatch();
    }
    // then
    try {
        assertEqualsEventually(() -> mapResultsToSortedList(hz.getMap("results")), expectedRecords);
    } finally {
        job.cancel();
        assertJobStatusEventually(job, JobStatus.FAILED);
    }
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) Arrays(java.util.Arrays) Connection(java.sql.Connection) QuickTest(com.hazelcast.test.annotation.QuickTest) Date(java.util.Date) StreamSource(com.hazelcast.jet.pipeline.StreamSource) CdcSinks(com.hazelcast.jet.cdc.CdcSinks) ParsingException(com.hazelcast.jet.cdc.ParsingException) Operation(com.hazelcast.jet.cdc.Operation) Util.entry(com.hazelcast.jet.Util.entry) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) ChangeRecord(com.hazelcast.jet.cdc.ChangeRecord) JobStatus(com.hazelcast.jet.core.JobStatus) Nonnull(javax.annotation.Nonnull) Job(com.hazelcast.jet.Job) HazelcastInstance(com.hazelcast.core.HazelcastInstance) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Pipeline(com.hazelcast.jet.pipeline.Pipeline) JetTestSupport(com.hazelcast.jet.core.JetTestSupport) JobConfig(com.hazelcast.jet.config.JobConfig) Sinks(com.hazelcast.jet.pipeline.Sinks) RecordPart(com.hazelcast.jet.cdc.RecordPart) Test(org.junit.Test) Category(org.junit.experimental.categories.Category) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) LongAccumulator(com.hazelcast.jet.accumulator.LongAccumulator) Statement(java.sql.Statement) ProcessingGuarantee(com.hazelcast.jet.config.ProcessingGuarantee) Statement(java.sql.Statement) Connection(java.sql.Connection) Operation(com.hazelcast.jet.cdc.Operation) Pipeline(com.hazelcast.jet.pipeline.Pipeline) LongAccumulator(com.hazelcast.jet.accumulator.LongAccumulator) HazelcastInstance(com.hazelcast.core.HazelcastInstance) RecordPart(com.hazelcast.jet.cdc.RecordPart) Job(com.hazelcast.jet.Job) ChangeRecord(com.hazelcast.jet.cdc.ChangeRecord) Category(org.junit.experimental.categories.Category) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 83 with Pipeline

use of com.hazelcast.jet.pipeline.Pipeline in project hazelcast by hazelcast.

the class MySqlCdcNetworkIntegrationTest method when_databaseShutdownDuringSnapshotting.

@Test
public void when_databaseShutdownDuringSnapshotting() throws Exception {
    mysql = initMySql(null, null);
    int port = fixPortBinding(mysql, MYSQL_PORT);
    Pipeline pipeline = initPipeline(mysql.getContainerIpAddress(), port);
    // when job starts
    HazelcastInstance hz = createHazelcastInstances(2)[0];
    Job job = hz.getJet().newJob(pipeline);
    assertJobStatusEventually(job, RUNNING);
    // and snapshotting is ongoing (we have no exact way of identifying
    // the moment, but random sleep will catch it at least some of the time)
    MILLISECONDS.sleep(ThreadLocalRandom.current().nextInt(100, 500));
    // and DB is stopped
    stopContainer(mysql);
    boolean neverReconnect = reconnectBehavior.getMaxAttempts() == 0;
    if (neverReconnect) {
        // then job fails
        assertJobFailsWithConnectException(job, true);
    } else {
        // and DB is started anew
        mysql = initMySql(null, port);
        // then snapshotting finishes successfully
        try {
            assertEqualsEventually(() -> hz.getMap("results").size(), 4);
            assertEquals(RUNNING, job.getStatus());
        } finally {
            abortJob(job);
        }
    }
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Job(com.hazelcast.jet.Job) Pipeline(com.hazelcast.jet.pipeline.Pipeline) AbstractCdcIntegrationTest(com.hazelcast.jet.cdc.AbstractCdcIntegrationTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 84 with Pipeline

use of com.hazelcast.jet.pipeline.Pipeline in project hazelcast by hazelcast.

the class PostgresCdcAuthAndConnectionIntegrationTest method incorrectDatabaseName.

@Test
public void incorrectDatabaseName() {
    StreamSource<ChangeRecord> source = PostgresCdcSources.postgres("name").setDatabaseAddress(postgres.getContainerIpAddress()).setDatabasePort(postgres.getMappedPort(POSTGRESQL_PORT)).setDatabaseUser("postgres").setDatabasePassword("postgres").setDatabaseName("wrongDatabaseName").build();
    Pipeline pipeline = pipeline(source);
    HazelcastInstance hz = createHazelcastInstances(2)[0];
    // when
    Job job = hz.getJet().newJob(pipeline);
    // then
    assertThatThrownBy(job::join).hasRootCauseInstanceOf(JetException.class).hasStackTraceContaining("database \"wrongDatabaseName\" does not exist");
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) JetException(com.hazelcast.jet.JetException) Job(com.hazelcast.jet.Job) ChangeRecord(com.hazelcast.jet.cdc.ChangeRecord) Pipeline(com.hazelcast.jet.pipeline.Pipeline) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 85 with Pipeline

use of com.hazelcast.jet.pipeline.Pipeline in project hazelcast by hazelcast.

the class PostgresCdcListenBeforeExistsIntegrationTest method listenBeforeTableExists.

@Test
public void listenBeforeTableExists() throws Exception {
    // given
    createSchema(SCHEMA);
    List<String> expectedRecords = Collections.singletonList("1001/0:(SYNC|INSERT):TableRow \\{id=1001, value1=someValue1, value2=someValue2, value3=null\\}");
    StreamSource<ChangeRecord> source = sourceBuilder("source").setSchemaWhitelist(SCHEMA).setTableWhitelist(SCHEMA + ".someTable").build();
    Pipeline pipeline = pipeline(source);
    // when
    HazelcastInstance hz = createHazelcastInstances(2)[0];
    Job job = hz.getJet().newJob(pipeline);
    assertJobStatusEventually(job, RUNNING);
    assertReplicationSlotActive();
    try {
        // then
        createTableWithData(SCHEMA, "someTable");
        insertIntoTable(SCHEMA, "someTable", 1001, "someValue1", "someValue2");
        assertTrueEventually(() -> assertMatch(expectedRecords, mapResultsToSortedList(hz.getMap(SINK_MAP_NAME))));
    } finally {
        job.cancel();
        assertJobStatusEventually(job, JobStatus.FAILED);
    }
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Job(com.hazelcast.jet.Job) ChangeRecord(com.hazelcast.jet.cdc.ChangeRecord) Pipeline(com.hazelcast.jet.pipeline.Pipeline) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Aggregations

Pipeline (com.hazelcast.jet.pipeline.Pipeline)379 Test (org.junit.Test)300 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)142 QuickTest (com.hazelcast.test.annotation.QuickTest)142 Job (com.hazelcast.jet.Job)125 Sinks (com.hazelcast.jet.pipeline.Sinks)107 Category (org.junit.experimental.categories.Category)100 HazelcastInstance (com.hazelcast.core.HazelcastInstance)94 JobConfig (com.hazelcast.jet.config.JobConfig)86 Assert.assertEquals (org.junit.Assert.assertEquals)73 List (java.util.List)72 NightlyTest (com.hazelcast.test.annotation.NightlyTest)65 Before (org.junit.Before)64 Entry (java.util.Map.Entry)61 TestSources (com.hazelcast.jet.pipeline.test.TestSources)58 Assert.assertTrue (org.junit.Assert.assertTrue)50 Sources (com.hazelcast.jet.pipeline.Sources)49 IOException (java.io.IOException)48 BeforeClass (org.junit.BeforeClass)48 SimpleTestInClusterSupport (com.hazelcast.jet.SimpleTestInClusterSupport)42