Search in sources :

Example 1 with UseRandomizedSchema

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

the class TransportSQLActionTest method test_bit_string_can_be_inserted_and_queried.

@Test
@UseJdbc(0)
@UseRandomizedSchema(random = false)
public void test_bit_string_can_be_inserted_and_queried() throws Exception {
    execute("create table tbl (xs bit(4))");
    execute("insert into tbl (xs) values (B'0000'), (B'0001'), (B'0011'), (B'0111'), (B'1111'), (B'1001')");
    assertThat(response.rowCount(), is(6L));
    execute("refresh table tbl");
    execute("SELECT _doc['xs'], xs, _raw, xs::bit(3) FROM tbl WHERE xs = B'1001'");
    assertThat(TestingHelpers.printedTable(response.rows()), is("B'1001'| B'1001'| {\"xs\":\"CQ==\"}| B'100'\n"));
    // use LIMIT 1 to hit a different execution path that should load `xs` differently
    execute("SELECT _doc['xs'], xs, _raw, xs::bit(3) FROM tbl WHERE xs = B'1001' LIMIT 1");
    assertThat(TestingHelpers.printedTable(response.rows()), is("B'1001'| B'1001'| {\"xs\":\"CQ==\"}| B'100'\n"));
    var properties = new Properties();
    properties.put("user", "crate");
    properties.put("password", "");
    ArrayList<String> results = new ArrayList<>();
    try (var conn = DriverManager.getConnection(sqlExecutor.jdbcUrl(), properties)) {
        Statement stmt = conn.createStatement();
        ResultSet result = stmt.executeQuery("select xs from tbl order by xs");
        while (result.next()) {
            String string = result.getString(1);
            results.add(string);
        }
    }
    assertThat(results, Matchers.contains("B'0000'", "B'0001'", "B'0011'", "B'0111'", "B'1001'", "B'1111'"));
}
Also used : Statement(java.sql.Statement) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) Matchers.containsString(org.hamcrest.Matchers.containsString) Properties(java.util.Properties) UseJdbc(io.crate.testing.UseJdbc) Test(org.junit.Test) UseRandomizedSchema(io.crate.testing.UseRandomizedSchema)

Example 2 with UseRandomizedSchema

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

the class TransportSQLActionTest method testUnknownTableJobGetsRemoved.

@Test
@UseRandomizedSchema(random = false)
public void testUnknownTableJobGetsRemoved() throws Exception {
    String uniqueId = UUID.randomUUID().toString();
    String stmtStr = "select '" + uniqueId + "' from foobar";
    String stmtStrWhere = "select ''" + uniqueId + "'' from foobar";
    assertThrowsMatches(() -> execute(stmtStr), isSQLError(containsString("Relation 'foobar' unknown"), UNDEFINED_TABLE, NOT_FOUND, 4041));
    execute("select stmt from sys.jobs where stmt='" + stmtStrWhere + "'");
    assertEquals(response.rowCount(), 0L);
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test) UseRandomizedSchema(io.crate.testing.UseRandomizedSchema)

Example 3 with UseRandomizedSchema

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

the class DiskDisruptionIT method testGlobalCheckpointIsSafe.

/**
 * This test checks that all operations below the global checkpoint are properly persisted.
 * It simulates a full power outage by preventing translog checkpoint files to be written and restart the cluster. This means that
 * all un-fsynced data will be lost.
 */
@UseRandomizedSchema(random = false)
@Test
public void testGlobalCheckpointIsSafe() throws Exception {
    startCluster(rarely() ? 5 : 3);
    var numberOfShards = 1 + randomInt(2);
    var numberOfReplicas = randomInt(2);
    execute("create table test (id int primary key, data string) " + "clustered into " + numberOfShards + " shards " + "with (" + " number_of_replicas = ?, " + " \"global_checkpoint_sync.interval\" = '1s' " + ")", new Object[] { numberOfReplicas });
    ensureGreen();
    AtomicBoolean stopGlobalCheckpointFetcher = new AtomicBoolean();
    Map<Integer, Long> shardToGcp = new ConcurrentHashMap<>();
    for (int i = 0; i < numberOfShards; i++) {
        shardToGcp.put(i, SequenceNumbers.NO_OPS_PERFORMED);
    }
    final Thread globalCheckpointSampler = new Thread(() -> {
        while (stopGlobalCheckpointFetcher.get() == false) {
            try {
                var response = execute("select id, seq_no_stats['global_checkpoint'] " + "from sys.shards where table_name='test'");
                for (var row : response.rows()) {
                    final int shardId = (int) row[0];
                    final long globalCheckpoint = (long) row[1];
                    shardToGcp.compute(shardId, (i, v) -> Math.max(v, globalCheckpoint));
                }
            } catch (Exception e) {
                // ignore
                logger.debug("failed to fetch shard stats", e);
            }
        }
    });
    globalCheckpointSampler.start();
    int numDocsToIndex = RandomizedTest.randomIntBetween(2, 50);
    try (BackgroundIndexer indexer = new BackgroundIndexer("test", "data", sqlExecutor.jdbcUrl(), -1, RandomizedTest.scaledRandomIntBetween(2, 5), false, random())) {
        indexer.setRequestTimeout(TimeValue.ZERO);
        indexer.setIgnoreIndexingFailures(true);
        indexer.setFailureAssertion(e -> {
        });
        indexer.start(-1);
        waitForDocs(randomIntBetween(1, numDocsToIndex), indexer, sqlExecutor);
        logger.info("injecting failures");
        injectTranslogFailures();
        logger.info("stopping indexing");
    }
    logger.info("full cluster restart");
    internalCluster().fullRestart(new InternalTestCluster.RestartCallback() {

        @Override
        public void onAllNodesStopped() {
            logger.info("stopping failures");
            stopTranslogFailures();
        }
    });
    stopGlobalCheckpointFetcher.set(true);
    logger.info("waiting for global checkpoint sampler");
    globalCheckpointSampler.join();
    logger.info("waiting for green");
    ensureGreen();
    execute("select distinct id, seq_no_stats['max_seq_no'] from sys.shards where table_name='test' and " + "routing_state in ('STARTED', 'RELOCATING')");
    assertThat(response.rowCount(), is((long) numberOfShards));
    for (var row : response.rows()) {
        final int shardId = (int) row[0];
        final long maxSeqNo = (long) row[1];
        assertThat(maxSeqNo, greaterThanOrEqualTo(shardToGcp.get(shardId)));
    }
}
Also used : BackgroundIndexer(org.elasticsearch.test.BackgroundIndexer) InternalTestCluster(org.elasticsearch.test.InternalTestCluster) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test) RandomizedTest(com.carrotsearch.randomizedtesting.RandomizedTest) UseRandomizedSchema(io.crate.testing.UseRandomizedSchema)

Example 4 with UseRandomizedSchema

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

the class PartitionedTableIntegrationTest method testAlterTableAddColumnOnPartitionedTable.

@Test
@UseRandomizedSchema(random = false)
public void testAlterTableAddColumnOnPartitionedTable() throws Exception {
    execute("create table t (id int primary key, date timestamp with time zone primary key) " + "partitioned by (date) " + "clustered into 1 shards " + "with (number_of_replicas=0)");
    execute("insert into t (id, date) values (1, '2014-01-01')");
    execute("insert into t (id, date) values (10, '2015-01-01')");
    ensureYellow();
    refresh();
    execute("alter table t add name string");
    execute("select * from t");
    assertThat(Arrays.asList(response.cols()), Matchers.containsInAnyOrder("date", "id", "name"));
    GetIndexTemplatesResponse templatesResponse = client().admin().indices().getTemplates(new GetIndexTemplatesRequest(".partitioned.t.")).actionGet();
    IndexTemplateMetadata metadata = templatesResponse.getIndexTemplates().get(0);
    String mappingSource = metadata.mappings().get(DEFAULT_MAPPING_TYPE).toString();
    Map mapping = (Map) XContentFactory.xContent(mappingSource).createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, mappingSource).map().get(DEFAULT_MAPPING_TYPE);
    assertNotNull(((Map) mapping.get("properties")).get("name"));
    // template order must not be touched
    assertThat(metadata.order(), is(100));
}
Also used : GetIndexTemplatesResponse(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse) IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) GetIndexTemplatesRequest(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test) UseRandomizedSchema(io.crate.testing.UseRandomizedSchema)

Example 5 with UseRandomizedSchema

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

the class PartitionedTableIntegrationTest method testAlterTableAddColumnOnPartitionedTableWithoutPartitions.

@Test
@UseRandomizedSchema(random = false)
public void testAlterTableAddColumnOnPartitionedTableWithoutPartitions() throws Exception {
    execute("create table t (id int primary key, date timestamp with time zone primary key) " + "partitioned by (date) " + "clustered into 1 shards " + "with (number_of_replicas=0)");
    ensureYellow();
    execute("alter table t add column name string");
    execute("alter table t add column ft_name string index using fulltext");
    ensureYellow();
    execute("select * from t");
    assertThat(Arrays.asList(response.cols()), Matchers.containsInAnyOrder("date", "ft_name", "id", "name"));
    GetIndexTemplatesResponse templatesResponse = client().admin().indices().getTemplates(new GetIndexTemplatesRequest(".partitioned.t.")).actionGet();
    IndexTemplateMetadata metadata = templatesResponse.getIndexTemplates().get(0);
    String mappingSource = metadata.mappings().get(DEFAULT_MAPPING_TYPE).toString();
    Map mapping = (Map) XContentFactory.xContent(mappingSource).createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, mappingSource).map().get(DEFAULT_MAPPING_TYPE);
    assertNotNull(((Map) mapping.get("properties")).get("name"));
    assertNotNull(((Map) mapping.get("properties")).get("ft_name"));
}
Also used : GetIndexTemplatesResponse(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse) IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) GetIndexTemplatesRequest(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test) UseRandomizedSchema(io.crate.testing.UseRandomizedSchema)

Aggregations

UseRandomizedSchema (io.crate.testing.UseRandomizedSchema)6 Test (org.junit.Test)5 HashMap (java.util.HashMap)2 Map (java.util.Map)2 GetIndexTemplatesRequest (org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest)2 GetIndexTemplatesResponse (org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse)2 IndexTemplateMetadata (org.elasticsearch.cluster.metadata.IndexTemplateMetadata)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 RandomizedTest (com.carrotsearch.randomizedtesting.RandomizedTest)1 UseJdbc (io.crate.testing.UseJdbc)1 IOException (java.io.IOException)1 ResultSet (java.sql.ResultSet)1 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 Random (java.util.Random)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 BackgroundIndexer (org.elasticsearch.test.BackgroundIndexer)1 InternalTestCluster (org.elasticsearch.test.InternalTestCluster)1