use of io.crate.metadata.RelationName in project crate by crate.
the class PartitionedTableIntegrationTest method testAlterPartitionedTableKeepsMetadata.
@Test
public void testAlterPartitionedTableKeepsMetadata() throws Exception {
execute("create table dynamic_table (" + " id integer, " + " score double" + ") partitioned by (score) with (number_of_replicas=0, column_policy='dynamic')");
ensureGreen();
execute("insert into dynamic_table (id, score) values (1, 10)");
execute("refresh table dynamic_table");
ensureGreen();
MappingMetadata partitionMetadata = clusterService().state().metadata().indices().get(new PartitionName(new RelationName(sqlExecutor.getCurrentSchema(), "dynamic_table"), Collections.singletonList("10.0")).asIndexName()).mapping();
Map<String, Object> metaMap = (Map) partitionMetadata.getSourceAsMap().get("_meta");
assertThat(String.valueOf(metaMap.get("partitioned_by")), Matchers.is("[[score, double]]"));
execute("alter table dynamic_table set (column_policy= 'dynamic')");
waitNoPendingTasksOnAll();
partitionMetadata = clusterService().state().metadata().indices().get(new PartitionName(new RelationName(sqlExecutor.getCurrentSchema(), "dynamic_table"), Collections.singletonList("10.0")).asIndexName()).mapping();
metaMap = (Map) partitionMetadata.getSourceAsMap().get("_meta");
assertThat(String.valueOf(metaMap.get("partitioned_by")), Matchers.is("[[score, double]]"));
}
use of io.crate.metadata.RelationName in project crate by crate.
the class PartitionedTableIntegrationTest method testAlterNumberOfReplicas.
@Test
public void testAlterNumberOfReplicas() {
String defaultSchema = sqlExecutor.getCurrentSchema();
execute("create table quotes (" + " id integer," + " quote string," + " date timestamp with time zone" + ") partitioned by(date) " + "clustered into 3 shards with (number_of_replicas='0-all')");
ensureYellow();
String templateName = PartitionName.templateName(defaultSchema, "quotes");
GetIndexTemplatesResponse templatesResponse = client().admin().indices().prepareGetTemplates(templateName).execute().actionGet();
Settings templateSettings = templatesResponse.getIndexTemplates().get(0).getSettings();
assertThat(templateSettings.get(IndexMetadata.SETTING_AUTO_EXPAND_REPLICAS), is("0-all"));
execute("alter table quotes set (number_of_replicas=0)");
ensureYellow();
templatesResponse = client().admin().indices().prepareGetTemplates(templateName).execute().actionGet();
templateSettings = templatesResponse.getIndexTemplates().get(0).getSettings();
assertThat(templateSettings.getAsInt(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1), is(0));
assertThat(templateSettings.getAsBoolean(IndexMetadata.SETTING_AUTO_EXPAND_REPLICAS, true), is(false));
execute("insert into quotes (id, quote, date) values (?, ?, ?), (?, ?, ?)", new Object[] { 1, "Don't panic", 1395874800000L, 2, "Now panic", 1395961200000L });
assertThat(response.rowCount(), is(2L));
ensureYellow();
refresh();
assertTrue(clusterService().state().metadata().hasAlias(getFqn("quotes")));
List<String> partitions = List.of(new PartitionName(new RelationName(defaultSchema, "quotes"), Collections.singletonList("1395874800000")).asIndexName(), new PartitionName(new RelationName(defaultSchema, "quotes"), Collections.singletonList("1395961200000")).asIndexName());
execute("select number_of_replicas from information_schema.table_partitions");
assertThat(printedTable(response.rows()), is("0\n" + "0\n"));
execute("select number_of_replicas, number_of_shards from information_schema.tables where table_name = 'quotes'");
assertEquals("0", response.rows()[0][0]);
assertEquals(3, response.rows()[0][1]);
execute("alter table quotes set (number_of_replicas='1-all')");
ensureYellow();
execute("select number_of_replicas from information_schema.tables where table_name = 'quotes'");
assertEquals("1-all", response.rows()[0][0]);
templatesResponse = client().admin().indices().prepareGetTemplates(templateName).execute().actionGet();
templateSettings = templatesResponse.getIndexTemplates().get(0).getSettings();
assertThat(templateSettings.get(IndexMetadata.SETTING_AUTO_EXPAND_REPLICAS), is("1-all"));
execute("select number_of_replicas from information_schema.table_partitions");
assertThat(printedTable(response.rows()), is("1-all\n" + "1-all\n"));
}
use of io.crate.metadata.RelationName in project crate by crate.
the class InformationSchemaTest method testOpenClosePartitionInformation.
@Test
public void testOpenClosePartitionInformation() {
execute("create table t (i int) partitioned by (i)");
ensureYellow();
execute("insert into t values (1)");
String partitionIdent = new PartitionName(new RelationName(sqlExecutor.getCurrentSchema(), "t"), Collections.singletonList("1")).ident();
execute("insert into t values (2), (3)");
ensureYellow();
execute("select closed from information_schema.table_partitions where table_name = 't'");
assertEquals(3, response.rowCount());
assertEquals(false, response.rows()[0][0]);
assertEquals(false, response.rows()[1][0]);
assertEquals(false, response.rows()[2][0]);
execute("alter table t partition (i = 1) close");
execute("select partition_ident, values from information_schema.table_partitions" + " where table_name = 't' and closed = true");
assertEquals(1, response.rowCount());
HashMap values = (HashMap) response.rows()[0][1];
assertEquals(1, values.get("i"));
assertTrue(partitionIdent.endsWith((String) response.rows()[0][0]));
execute("alter table t partition (i = 1) open");
execute("select closed from information_schema.table_partitions");
assertEquals(3, response.rowCount());
assertEquals(false, response.rows()[0][0]);
assertEquals(false, response.rows()[1][0]);
assertEquals(false, response.rows()[2][0]);
}
use of io.crate.metadata.RelationName in project crate by crate.
the class PartitionedTableConcurrentIntegrationTest method deletePartitionWhileInsertingData.
private void deletePartitionWhileInsertingData(final boolean useBulk) throws Exception {
execute("create table parted (id int, name string) " + "clustered into 1 shards " + "partitioned by (id) " + "with (number_of_replicas = 0)");
int numberOfDocs = 100;
final Object[][] bulkArgs = new Object[numberOfDocs][];
for (int i = 0; i < numberOfDocs; i++) {
bulkArgs[i] = new Object[] { i % 2, randomAsciiLettersOfLength(10) };
}
// partition to delete
final int idToDelete = 1;
final AtomicReference<Exception> exceptionRef = new AtomicReference<>();
final CountDownLatch insertLatch = new CountDownLatch(1);
final String insertStmt = "insert into parted (id, name) values (?, ?)";
Thread insertThread = new Thread(() -> {
try {
if (useBulk) {
execute(insertStmt, bulkArgs);
} else {
for (Object[] args : bulkArgs) {
execute(insertStmt, args);
}
}
} catch (Exception t) {
exceptionRef.set(t);
} finally {
insertLatch.countDown();
}
});
final CountDownLatch deleteLatch = new CountDownLatch(1);
final String partitionName = new PartitionName(new RelationName(sqlExecutor.getCurrentSchema(), "parted"), Collections.singletonList(String.valueOf(idToDelete))).asIndexName();
final Object[] deleteArgs = new Object[] { idToDelete };
Thread deleteThread = new Thread(() -> {
boolean deleted = false;
while (!deleted) {
try {
Metadata metadata = client().admin().cluster().prepareState().execute().actionGet().getState().metadata();
if (metadata.indices().get(partitionName) != null) {
execute("delete from parted where id = ?", deleteArgs);
deleted = true;
}
} catch (Throwable t) {
// ignore (mostly partition index does not exists yet)
}
}
deleteLatch.countDown();
});
insertThread.start();
deleteThread.start();
deleteLatch.await(SQLTransportExecutor.REQUEST_TIMEOUT.getSeconds() + 1, TimeUnit.SECONDS);
insertLatch.await(SQLTransportExecutor.REQUEST_TIMEOUT.getSeconds() + 1, TimeUnit.SECONDS);
Exception exception = exceptionRef.get();
if (exception != null) {
throw exception;
}
insertThread.join();
deleteThread.join();
}
use of io.crate.metadata.RelationName in project crate by crate.
the class SelectFromViewAnalyzerTest method testSelectFromViewIsResolvedToViewQueryDefinition.
@Test
public void testSelectFromViewIsResolvedToViewQueryDefinition() {
QueriedSelectRelation query = e.analyze("select * from doc.v1");
assertThat(query.outputs(), contains(isField("name"), isField("count(*)")));
assertThat(query.groupBy(), Matchers.empty());
assertThat(query.from(), contains(instanceOf(AnalyzedView.class)));
QueriedSelectRelation queriedDocTable = (QueriedSelectRelation) ((AnalyzedView) query.from().get(0)).relation();
assertThat(queriedDocTable.groupBy(), contains(isReference("name")));
assertThat(queriedDocTable.from(), contains(isDocTable(new RelationName("doc", "t1"))));
}
Aggregations