Search in sources :

Example 96 with SQLResponse

use of io.crate.testing.SQLResponse in project crate by crate.

the class PartitionedTableConcurrentIntegrationTest method testSelectWhileShardsAreRelocating.

/**
     * Test depends on 2 data nodes
     */
@Test
public void testSelectWhileShardsAreRelocating() throws Throwable {
    execute("create table t (name string, p string) " + "clustered into 2 shards " + "partitioned by (p) with (number_of_replicas = 0)");
    ensureYellow();
    execute("insert into t (name, p) values (?, ?)", new Object[][] { new Object[] { "Marvin", "a" }, new Object[] { "Trillian", "a" } });
    execute("refresh table t");
    execute("set global stats.enabled=true");
    final AtomicReference<Throwable> lastThrowable = new AtomicReference<>();
    final CountDownLatch selects = new CountDownLatch(100);
    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            while (selects.getCount() > 0) {
                try {
                    execute("select * from t");
                } catch (Throwable t) {
                    // The failed job should have three started operations
                    SQLResponse res = execute("select id from sys.jobs_log where error is not null order by started desc limit 1");
                    if (res.rowCount() > 0) {
                        String id = (String) res.rows()[0][0];
                        res = execute("select count(*) from sys.operations_log where name=? or name = ?and job_id = ?", new Object[] { "collect", "fetchContext", id });
                        if ((long) res.rows()[0][0] < 3) {
                            // set the error if there where less than three attempts
                            lastThrowable.set(t);
                        }
                    }
                } finally {
                    selects.countDown();
                }
            }
        }
    });
    t.start();
    PartitionName partitionName = new PartitionName("t", Collections.singletonList(new BytesRef("a")));
    final String indexName = partitionName.asIndexName();
    ClusterService clusterService = internalCluster().getInstance(ClusterService.class);
    DiscoveryNodes nodes = clusterService.state().nodes();
    List<String> nodeIds = new ArrayList<>(2);
    for (DiscoveryNode node : nodes) {
        if (node.dataNode()) {
            nodeIds.add(node.getId());
        }
    }
    final Map<String, String> nodeSwap = new HashMap<>(2);
    nodeSwap.put(nodeIds.get(0), nodeIds.get(1));
    nodeSwap.put(nodeIds.get(1), nodeIds.get(0));
    final CountDownLatch relocations = new CountDownLatch(20);
    Thread relocatingThread = new Thread(new Runnable() {

        @Override
        public void run() {
            while (relocations.getCount() > 0) {
                ClusterStateResponse clusterStateResponse = admin().cluster().prepareState().setIndices(indexName).execute().actionGet();
                List<ShardRouting> shardRoutings = clusterStateResponse.getState().routingTable().allShards(indexName);
                ClusterRerouteRequestBuilder clusterRerouteRequestBuilder = admin().cluster().prepareReroute();
                int numMoves = 0;
                for (ShardRouting shardRouting : shardRoutings) {
                    if (shardRouting.currentNodeId() == null) {
                        continue;
                    }
                    if (shardRouting.state() != ShardRoutingState.STARTED) {
                        continue;
                    }
                    String toNode = nodeSwap.get(shardRouting.currentNodeId());
                    clusterRerouteRequestBuilder.add(new MoveAllocationCommand(shardRouting.shardId(), shardRouting.currentNodeId(), toNode));
                    numMoves++;
                }
                if (numMoves > 0) {
                    clusterRerouteRequestBuilder.execute().actionGet();
                    client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForRelocatingShards(0).setTimeout(ACCEPTABLE_RELOCATION_TIME).execute().actionGet();
                    relocations.countDown();
                }
            }
        }
    });
    relocatingThread.start();
    relocations.await(SQLTransportExecutor.REQUEST_TIMEOUT.getSeconds() + 1, TimeUnit.SECONDS);
    selects.await(SQLTransportExecutor.REQUEST_TIMEOUT.getSeconds() + 1, TimeUnit.SECONDS);
    Throwable throwable = lastThrowable.get();
    if (throwable != null) {
        throw throwable;
    }
    t.join();
    relocatingThread.join();
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ClusterRerouteRequestBuilder(org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteRequestBuilder) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) MoveAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand) AtomicReference(java.util.concurrent.atomic.AtomicReference) SQLResponse(io.crate.testing.SQLResponse) CountDownLatch(java.util.concurrent.CountDownLatch) PartitionName(io.crate.metadata.PartitionName) ClusterService(org.elasticsearch.cluster.ClusterService) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) BytesRef(org.apache.lucene.util.BytesRef) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) Test(org.junit.Test)

Example 97 with SQLResponse

use of io.crate.testing.SQLResponse in project crate by crate.

the class CopyIntegrationTest method testCopyColumnsToDirectory.

@Test
public void testCopyColumnsToDirectory() throws Exception {
    this.setup.groupBySetup();
    String uriTemplate = Paths.get(folder.getRoot().toURI()).toUri().toString();
    SQLResponse response = execute("copy characters (name, details['job']) to DIRECTORY ?", new Object[] { uriTemplate });
    assertThat(response.cols().length, is(0));
    assertThat(response.rowCount(), is(7L));
    List<String> lines = new ArrayList<>(7);
    DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(folder.getRoot().toURI()), "*.json");
    for (Path entry : stream) {
        lines.addAll(Files.readAllLines(entry, StandardCharsets.UTF_8));
    }
    Path path = Paths.get(folder.getRoot().toURI().resolve("characters_0_.json"));
    assertTrue(path.toFile().exists());
    assertThat(lines.size(), is(7));
    boolean foundJob = false;
    boolean foundName = false;
    for (String line : lines) {
        foundName = foundName || line.contains("Arthur Dent");
        foundJob = foundJob || line.contains("Sandwitch Maker");
        assertThat(line.split(",").length, is(2));
        assertThat(line.trim(), startsWith("["));
        assertThat(line.trim(), endsWith("]"));
    }
    assertTrue(foundJob);
    assertTrue(foundName);
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) SQLResponse(io.crate.testing.SQLResponse) Test(org.junit.Test)

Example 98 with SQLResponse

use of io.crate.testing.SQLResponse in project crate by crate.

the class CopyIntegrationTest method testCopyToWithWhereNoMatch.

@Test
public void testCopyToWithWhereNoMatch() throws Exception {
    this.setup.groupBySetup();
    String uriTemplate = Paths.get(folder.getRoot().toURI()).toUri().toString();
    SQLResponse response = execute("copy characters where gender = 'foo' to DIRECTORY ?", new Object[] { uriTemplate });
    assertThat(response.rowCount(), is(0L));
}
Also used : SQLResponse(io.crate.testing.SQLResponse) Test(org.junit.Test)

Example 99 with SQLResponse

use of io.crate.testing.SQLResponse in project crate by crate.

the class EmptyStringRoutingIntegrationTest method testCopyFromEmptyStringRouting.

@Test
// copy has no rowcount
@UseJdbc(0)
public void testCopyFromEmptyStringRouting() throws Exception {
    execute("create table t (i int primary key, c string primary key, a int) clustered by (c)");
    ensureYellow();
    execute("insert into t (i, c) values (1, ''), (2, '')");
    refresh();
    String uri = Paths.get(folder.getRoot().toURI()).toUri().toString();
    SQLResponse response = execute("copy t to directory ?", new Object[] { uri });
    assertThat(response.rowCount(), is(2L));
    execute("delete from t");
    refresh();
    execute("copy t from ? with (shared=true)", new Object[] { uri + "t_*" });
    refresh();
    response = execute("select c, count(*) from t group by c");
    assertThat(response.rowCount(), is(1L));
    assertThat((long) response.rows()[0][1], is(2L));
}
Also used : SQLResponse(io.crate.testing.SQLResponse) UseJdbc(io.crate.testing.UseJdbc) Test(org.junit.Test)

Example 100 with SQLResponse

use of io.crate.testing.SQLResponse in project crate by crate.

the class PartitionedTableIntegrationTest method testDeleteFromPartitionedTableDeleteByQuery.

@Test
public void testDeleteFromPartitionedTableDeleteByQuery() throws Exception {
    execute("create table quotes (id integer, quote string, timestamp timestamp) " + "partitioned by(timestamp) with (number_of_replicas=0)");
    ensureYellow();
    execute("insert into quotes (id, quote, timestamp) values(?, ?, ?)", new Object[] { 1, "Don't panic", 1395874800000L });
    execute("insert into quotes (id, quote, timestamp) values(?, ?, ?)", new Object[] { 2, "Time is an illusion. Lunchtime doubly so", 1395961200000L });
    execute("insert into quotes (id, quote, timestamp) values(?, ?, ?)", new Object[] { 3, "I'd far rather be happy than right any day", 1396303200000L });
    execute("insert into quotes (id, quote, timestamp) values(?, ?, ?)", new Object[] { 4, "Now panic", 1395874800000L });
    ensureYellow();
    refresh();
    SQLResponse response = execute("select partition_ident from information_schema.table_partitions " + "where table_name='quotes' and schema_name='doc'" + "order by partition_ident");
    assertThat(response.rowCount(), is(3L));
    assertThat((String) response.rows()[0][0], is(new PartitionName("parted", ImmutableList.of(new BytesRef("1395874800000"))).ident()));
    assertThat((String) response.rows()[1][0], is(new PartitionName("parted", ImmutableList.of(new BytesRef("1395961200000"))).ident()));
    assertThat((String) response.rows()[2][0], is(new PartitionName("parted", ImmutableList.of(new BytesRef("1396303200000"))).ident()));
    execute("delete from quotes where quote = 'Don''t panic'");
    refresh();
    execute("select * from quotes where quote = 'Don''t panic'");
    assertThat(this.response.rowCount(), is(0L));
    // Test that no partitions were deleted
    SQLResponse newResponse = execute("select partition_ident from information_schema.table_partitions " + "where table_name='quotes' and schema_name='doc'" + "order by partition_ident");
    assertThat(newResponse.rows(), is(response.rows()));
}
Also used : PartitionName(io.crate.metadata.PartitionName) SQLResponse(io.crate.testing.SQLResponse) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test)

Aggregations

SQLResponse (io.crate.testing.SQLResponse)109 Test (org.junit.Test)78 Map (java.util.Map)13 UseJdbc (io.crate.testing.UseJdbc)10 HashMap (java.util.HashMap)10 ArrayList (java.util.ArrayList)8 PartitionName (io.crate.metadata.PartitionName)4 TestingHelpers.resolveCanonicalString (io.crate.testing.TestingHelpers.resolveCanonicalString)4 Path (java.nio.file.Path)4 BytesRef (org.apache.lucene.util.BytesRef)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 TreeMap (java.util.TreeMap)2 List (java.util.List)1 UUID (java.util.UUID)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 ClusterRerouteRequestBuilder (org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteRequestBuilder)1 ClusterStateResponse (org.elasticsearch.action.admin.cluster.state.ClusterStateResponse)1 SearchRequest (org.elasticsearch.action.search.SearchRequest)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1