Search in sources :

Example 6 with BytesRef

use of org.apache.lucene.util.BytesRef 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("dynamic_table", Collections.singletonList(new BytesRef("10.0"))).asIndexName()).getMappings().get(Constants.DEFAULT_MAPPING_TYPE);
    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("dynamic_table", Collections.singletonList(new BytesRef("10.0"))).asIndexName()).getMappings().get(Constants.DEFAULT_MAPPING_TYPE);
    metaMap = (Map) partitionMetaData.getSourceAsMap().get("_meta");
    assertThat(String.valueOf(metaMap.get("partitioned_by")), Matchers.is("[[score, double]]"));
}
Also used : PartitionName(io.crate.metadata.PartitionName) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test)

Example 7 with BytesRef

use of org.apache.lucene.util.BytesRef 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) " + "partitioned by (id) " + "with (number_of_replicas = 0)");
    ensureYellow();
    int numberOfDocs = 1000;
    final Object[][] bulkArgs = new Object[numberOfDocs][];
    for (int i = 0; i < numberOfDocs; i++) {
        bulkArgs[i] = new Object[] { i % 2, randomAsciiOfLength(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(new Runnable() {

        @Override
        public void run() {
            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("parted", Collections.singletonList(new BytesRef(String.valueOf(idToDelete)))).asIndexName();
    final Object[] deleteArgs = new Object[] { idToDelete };
    Thread deleteThread = new Thread(new Runnable() {

        @Override
        public void run() {
            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();
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) PartitionName(io.crate.metadata.PartitionName) MetaData(org.elasticsearch.cluster.metadata.MetaData) BytesRef(org.apache.lucene.util.BytesRef)

Example 8 with BytesRef

use of org.apache.lucene.util.BytesRef in project crate by crate.

the class PartitionedTableIntegrationTest method testInsertPartitionedTable.

@Test
public void testInsertPartitionedTable() throws Exception {
    execute("create table parted (id integer, name string, date timestamp)" + "partitioned by (date)");
    ensureYellow();
    String templateName = PartitionName.templateName(null, "parted");
    GetIndexTemplatesResponse templatesResponse = client().admin().indices().prepareGetTemplates(templateName).execute().actionGet();
    assertThat(templatesResponse.getIndexTemplates().get(0).template(), is(templateName + "*"));
    assertThat(templatesResponse.getIndexTemplates().get(0).name(), is(templateName));
    assertTrue(templatesResponse.getIndexTemplates().get(0).getAliases().get("parted") != null);
    execute("insert into parted (id, name, date) values (?, ?, ?)", new Object[] { 1, "Ford", 13959981214861L });
    assertThat(response.rowCount(), is(1L));
    ensureYellow();
    refresh();
    assertTrue(clusterService().state().metaData().hasAlias("parted"));
    String partitionName = new PartitionName("parted", Collections.singletonList(new BytesRef(String.valueOf(13959981214861L)))).asIndexName();
    MetaData metaData = client().admin().cluster().prepareState().execute().actionGet().getState().metaData();
    assertNotNull(metaData.indices().get(partitionName).getAliases().get("parted"));
    assertThat(client().prepareSearch(partitionName).setTypes(Constants.DEFAULT_MAPPING_TYPE).setSize(0).setQuery(new MatchAllQueryBuilder()).execute().actionGet().getHits().totalHits(), is(1L));
    execute("select id, name, date from parted");
    assertThat(response.rowCount(), is(1L));
    assertThat((Integer) response.rows()[0][0], is(1));
    assertThat((String) response.rows()[0][1], is("Ford"));
    assertThat((Long) response.rows()[0][2], is(13959981214861L));
}
Also used : PartitionName(io.crate.metadata.PartitionName) GetIndexTemplatesResponse(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse) MetaData(org.elasticsearch.cluster.metadata.MetaData) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) DocIndexMetaData(io.crate.metadata.doc.DocIndexMetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) IndexTemplateMetaData(org.elasticsearch.cluster.metadata.IndexTemplateMetaData) BytesRef(org.apache.lucene.util.BytesRef) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder) Test(org.junit.Test)

Example 9 with BytesRef

use of org.apache.lucene.util.BytesRef in project crate by crate.

the class PartitionedTableIntegrationTest method testAlterNumberOfReplicas.

@Test
public void testAlterNumberOfReplicas() throws Exception {
    execute("create table quotes (id integer, quote string, date timestamp) " + "partitioned by(date) clustered into 3 shards with (number_of_replicas='0-all')");
    ensureYellow();
    String templateName = PartitionName.templateName(null, "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("quotes"));
    List<String> partitions = ImmutableList.of(new PartitionName("quotes", Collections.singletonList(new BytesRef("1395874800000"))).asIndexName(), new PartitionName("quotes", Collections.singletonList(new BytesRef("1395961200000"))).asIndexName());
    GetSettingsResponse settingsResponse = client().admin().indices().prepareGetSettings(partitions.get(0), partitions.get(1)).execute().get();
    for (String index : partitions) {
        Settings partitionSetting = settingsResponse.getIndexToSettings().get(index);
        assertThat(partitionSetting.getAsInt(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1), is(0));
        assertThat(partitionSetting.getAsBoolean(IndexMetaData.SETTING_AUTO_EXPAND_REPLICAS, true), is(false));
    }
    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"));
    settingsResponse = client().admin().indices().prepareGetSettings(partitions.get(0), partitions.get(1)).execute().get();
    for (String index : partitions) {
        assertThat(settingsResponse.getSetting(index, IndexMetaData.SETTING_AUTO_EXPAND_REPLICAS), is("1-all"));
    }
}
Also used : PartitionName(io.crate.metadata.PartitionName) GetSettingsResponse(org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse) GetIndexTemplatesResponse(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse) Settings(org.elasticsearch.common.settings.Settings) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test)

Example 10 with BytesRef

use of org.apache.lucene.util.BytesRef in project crate by crate.

the class PartitionedTableIntegrationTest method testInsertPartitionedTableReversedPartitionedColumns.

@Test
public void testInsertPartitionedTableReversedPartitionedColumns() throws Exception {
    execute("create table parted (id integer, name string, date timestamp)" + "partitioned by (name, date)");
    ensureYellow();
    Long dateValue = System.currentTimeMillis();
    execute("insert into parted (id, date, name) values (?, ?, ?)", new Object[] { 1, dateValue, "Trillian" });
    assertThat(response.rowCount(), is(1L));
    ensureYellow();
    refresh();
    String partitionName = new PartitionName("parted", Arrays.asList(new BytesRef("Trillian"), new BytesRef(dateValue.toString()))).asIndexName();
    assertNotNull(client().admin().cluster().prepareState().execute().actionGet().getState().metaData().indices().get(partitionName).getAliases().get("parted"));
}
Also used : PartitionName(io.crate.metadata.PartitionName) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test)

Aggregations

BytesRef (org.apache.lucene.util.BytesRef)1448 Document (org.apache.lucene.document.Document)409 Directory (org.apache.lucene.store.Directory)370 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)266 ArrayList (java.util.ArrayList)186 Test (org.junit.Test)182 SortedDocValuesField (org.apache.lucene.document.SortedDocValuesField)164 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)152 Term (org.apache.lucene.index.Term)123 Analyzer (org.apache.lucene.analysis.Analyzer)121 IndexReader (org.apache.lucene.index.IndexReader)121 TermsEnum (org.apache.lucene.index.TermsEnum)116 SortedSetDocValuesField (org.apache.lucene.document.SortedSetDocValuesField)110 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)105 IOException (java.io.IOException)103 Field (org.apache.lucene.document.Field)101 StringField (org.apache.lucene.document.StringField)100 CrateUnitTest (io.crate.test.integration.CrateUnitTest)95 TextField (org.apache.lucene.document.TextField)94 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)87