Search in sources :

Example 1 with SqlService

use of com.hazelcast.sql.SqlService in project hazelcast by hazelcast.

the class SqlJobManagementTest method when_clientDisconnects_then_jobContinues.

@Test
public void when_clientDisconnects_then_jobContinues() {
    HazelcastInstance client = factory().newHazelcastClient();
    SqlService sqlService = client.getSql();
    createMapping("dest", Long.class, Long.class);
    sqlService.execute("CREATE JOB testJob AS SINK INTO dest SELECT v, v FROM TABLE(GENERATE_STREAM(100))");
    Job job = instance().getJet().getJob("testJob");
    assertNotNull(job);
    assertJobStatusEventually(job, RUNNING);
    // When
    client.shutdown();
    sleepSeconds(1);
    // Then
    assertEquals(RUNNING, job.getStatus());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) SqlService(com.hazelcast.sql.SqlService) Job(com.hazelcast.jet.Job) Test(org.junit.Test)

Example 2 with SqlService

use of com.hazelcast.sql.SqlService in project hazelcast by hazelcast.

the class SqlTestSupport method assertRowsEventuallyInAnyOrder.

/**
 * Execute a query and wait for the results to contain all the {@code
 * expectedRows}. Suitable for streaming queries that don't terminate, but
 * return a deterministic set of rows. Rows can arrive in any order.
 * <p>
 * After all expected rows are received, the method further waits a little
 * more if any extra rows are received, and fails, if they are.
 *
 * @param sql          The query
 * @param arguments    The query arguments
 * @param expectedRows Expected rows
 */
public static void assertRowsEventuallyInAnyOrder(String sql, List<Object> arguments, Collection<Row> expectedRows) {
    SqlService sqlService = instance().getSql();
    CompletableFuture<Void> future = new CompletableFuture<>();
    Deque<Row> rows = new ArrayDeque<>();
    Thread thread = new Thread(() -> {
        SqlStatement statement = new SqlStatement(sql);
        arguments.forEach(statement::addParameter);
        try (SqlResult result = sqlService.execute(statement)) {
            ResultIterator<SqlRow> iterator = (ResultIterator<SqlRow>) result.iterator();
            for (int i = 0; i < expectedRows.size() && iterator.hasNext() || iterator.hasNext(50, TimeUnit.MILLISECONDS) == YES; i++) {
                rows.add(new Row(iterator.next()));
            }
            future.complete(null);
        } catch (Throwable e) {
            e.printStackTrace();
            future.completeExceptionally(e);
        }
    });
    thread.start();
    try {
        try {
            future.get(10, TimeUnit.SECONDS);
        } catch (TimeoutException e) {
            thread.interrupt();
            thread.join();
        }
    } catch (Exception e) {
        throw sneakyThrow(e);
    }
    List<Row> actualRows = new ArrayList<>(rows);
    assertThat(actualRows).containsExactlyInAnyOrderElementsOf(expectedRows);
}
Also used : SqlRow(com.hazelcast.sql.SqlRow) JetSqlRow(com.hazelcast.sql.impl.row.JetSqlRow) SqlResult(com.hazelcast.sql.SqlResult) ResultIterator(com.hazelcast.sql.impl.ResultIterator) ArrayList(java.util.ArrayList) ArrayDeque(java.util.ArrayDeque) TimeoutException(java.util.concurrent.TimeoutException) HazelcastException(com.hazelcast.core.HazelcastException) SqlStatement(com.hazelcast.sql.SqlStatement) CompletableFuture(java.util.concurrent.CompletableFuture) SqlService(com.hazelcast.sql.SqlService) SqlRow(com.hazelcast.sql.SqlRow) JetSqlRow(com.hazelcast.sql.impl.row.JetSqlRow) TimeoutException(java.util.concurrent.TimeoutException)

Example 3 with SqlService

use of com.hazelcast.sql.SqlService in project hazelcast by hazelcast.

the class SqlClientTest method when_jobFails_then_clientFindsOut.

@Test
public void when_jobFails_then_clientFindsOut() {
    HazelcastInstance client = factory().newHazelcastClient();
    SqlService sqlService = client.getSql();
    sqlService.execute("CREATE MAPPING t TYPE " + TestFailingSqlConnector.TYPE_NAME);
    assertThatThrownBy(() -> {
        SqlResult result = sqlService.execute("SELECT * FROM t");
        for (SqlRow r : result) {
            System.out.println(r);
        }
    }).hasMessageContaining("mock failure");
}
Also used : SqlRow(com.hazelcast.sql.SqlRow) HazelcastInstance(com.hazelcast.core.HazelcastInstance) SqlResult(com.hazelcast.sql.SqlResult) SqlService(com.hazelcast.sql.SqlService) Test(org.junit.Test)

Example 4 with SqlService

use of com.hazelcast.sql.SqlService in project hazelcast by hazelcast.

the class SqlClientTest method when_resultClosed_then_jobCancelled_withNoResults.

@Test
public void when_resultClosed_then_jobCancelled_withNoResults() {
    /*
        There was an issue that RootResultConsumerSink didn't check for failures, unless
        it had some items in the inbox.
         */
    HazelcastInstance client = factory().newHazelcastClient();
    SqlService sqlService = client.getSql();
    logger.info("before select");
    SqlResult result = sqlService.execute("SELECT * FROM TABLE(GENERATE_STREAM(0))");
    logger.info("after execute returned");
    Job job = awaitSingleRunningJob(client);
    logger.info("Job is running.");
    result.close();
    logger.info("after res.close() returned");
    assertJobStatusEventually(job, FAILED);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) SqlResult(com.hazelcast.sql.SqlResult) SqlService(com.hazelcast.sql.SqlService) Job(com.hazelcast.jet.Job) Test(org.junit.Test)

Example 5 with SqlService

use of com.hazelcast.sql.SqlService in project hazelcast by hazelcast.

the class LightJobBench method sqlBench.

@Test
public void sqlBench() {
    int warmUpIterations = 100;
    int realIterations = 200;
    SqlService sqlService = inst.getSql();
    logger.info("will submit " + warmUpIterations + " jobs");
    SqlTestSupport.createMapping(inst, "m", int.class, int.class);
    inst.getMap("m").put(1, 1);
    int numRows = 0;
    for (int i = 0; i < warmUpIterations; i++) {
        for (SqlRow ignored : sqlService.execute("select * from m")) {
            numRows++;
        }
    }
    logger.info("warmup jobs done, starting benchmark");
    long start = System.nanoTime();
    for (int i = 0; i < realIterations; i++) {
        for (SqlRow ignored : sqlService.execute("select * from m")) {
            numRows++;
        }
    }
    long elapsedMicros = NANOSECONDS.toMicros(System.nanoTime() - start);
    System.out.println(numRows);
    System.out.println(realIterations + " queries run in " + (elapsedMicros / realIterations) + " us/job");
}
Also used : SqlRow(com.hazelcast.sql.SqlRow) SqlService(com.hazelcast.sql.SqlService) Test(org.junit.Test)

Aggregations

SqlService (com.hazelcast.sql.SqlService)12 SqlResult (com.hazelcast.sql.SqlResult)8 SqlRow (com.hazelcast.sql.SqlRow)8 Test (org.junit.Test)7 HazelcastInstance (com.hazelcast.core.HazelcastInstance)6 Job (com.hazelcast.jet.Job)4 JetSqlRow (com.hazelcast.sql.impl.row.JetSqlRow)4 ArrayList (java.util.ArrayList)4 SqlStatement (com.hazelcast.sql.SqlStatement)3 HazelcastException (com.hazelcast.core.HazelcastException)2 ArrayDeque (java.util.ArrayDeque)2 BitSet (java.util.BitSet)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 TimeoutException (java.util.concurrent.TimeoutException)2 Util.idToString (com.hazelcast.jet.Util.idToString)1 FAILED (com.hazelcast.jet.core.JobStatus.FAILED)1 JetServiceBackend (com.hazelcast.jet.impl.JetServiceBackend)1 ExecutionContext (com.hazelcast.jet.impl.execution.ExecutionContext)1 TestBatchSqlConnector (com.hazelcast.jet.sql.impl.connector.test.TestBatchSqlConnector)1 TestFailingSqlConnector (com.hazelcast.jet.sql.impl.connector.test.TestFailingSqlConnector)1