org.elasticsearch.cluster.metadata.IndexTemplateMetaData Java Examples

The following examples show how to use org.elasticsearch.cluster.metadata.IndexTemplateMetaData. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: IndexTemplateUpgraderTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void test__all_is_removed_from_template_mapping() throws Throwable {
    String templateName = PartitionName.templateName("doc", "events");
    var template = IndexTemplateMetaData.builder(templateName)
        .patterns(List.of("*"))
        .putMapping(
            Constants.DEFAULT_MAPPING_TYPE,
            "{" +
            "   \"default\": {" +
            "       \"_all\": {\"enabled\": false}," +
            "       \"properties\": {" +
            "           \"name\": {" +
            "               \"type\": \"keyword\"" +
            "           }" +
            "       }" +
            "   }" +
            "}")
        .build();

    IndexTemplateUpgrader upgrader = new IndexTemplateUpgrader();
    Map<String, IndexTemplateMetaData> result = upgrader.apply(Map.of(templateName, template));
    IndexTemplateMetaData updatedTemplate = result.get(templateName);

    CompressedXContent compressedXContent = updatedTemplate.mappings().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(compressedXContent.string(), is("{\"default\":{\"properties\":{\"name\":{\"type\":\"keyword\"}}}}"));
}
 
Example #2
Source File: TransportGetIndexTemplatesAction.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
protected void masterOperation(GetIndexTemplatesRequest request, ClusterState state, ActionListener<GetIndexTemplatesResponse> listener) {
    List<IndexTemplateMetaData> results;

    // If we did not ask for a specific name, then we return all templates
    if (request.names().length == 0) {
        results = Arrays.asList(state.metaData().templates().values().toArray(IndexTemplateMetaData.class));
    } else {
        results = new ArrayList<>();
    }

    for (String name : request.names()) {
        if (Regex.isSimpleMatchPattern(name)) {
            for (ObjectObjectCursor<String, IndexTemplateMetaData> entry : state.metaData().templates()) {
                if (Regex.simpleMatch(name, entry.key)) {
                    results.add(entry.value);
                }
            }
        } else if (state.metaData().templates().containsKey(name)) {
            results.add(state.metaData().templates().get(name));
        }
    }

    listener.onResponse(new GetIndexTemplatesResponse(results));
}
 
Example #3
Source File: TestCluster.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Removes all templates, except the templates defined in the exclude
 */
public void wipeAllTemplates(Set<String> exclude) {
    if (size() > 0) {
        GetIndexTemplatesResponse response = client().admin().indices().prepareGetTemplates().get();
        for (IndexTemplateMetaData indexTemplate : response.getIndexTemplates()) {
            if (exclude.contains(indexTemplate.getName())) {
                continue;
            }
            try {
                client().admin().indices().prepareDeleteTemplate(indexTemplate.getName()).execute().actionGet();
            } catch (IndexTemplateMissingException e) {
                // ignore
            }
        }
    }
}
 
Example #4
Source File: MetaDataUpgrader.java    From crate with Apache License 2.0 6 votes vote down vote up
public MetaDataUpgrader(Collection<UnaryOperator<Map<String, MetaData.Custom>>> customMetaDataUpgraders,
                        Collection<UnaryOperator<Map<String, IndexTemplateMetaData>>> indexTemplateMetaDataUpgraders) {
    this.customMetaDataUpgraders = customs -> {
        Map<String, MetaData.Custom> upgradedCustoms = new HashMap<>(customs);
        for (UnaryOperator<Map<String, MetaData.Custom>> customMetaDataUpgrader : customMetaDataUpgraders) {
            upgradedCustoms = customMetaDataUpgrader.apply(upgradedCustoms);
        }
        return upgradedCustoms;
    };

    this.indexTemplateMetaDataUpgraders = templates -> {
        Map<String, IndexTemplateMetaData> upgradedTemplates = new HashMap<>(templates);
        for (UnaryOperator<Map<String, IndexTemplateMetaData>> upgrader : indexTemplateMetaDataUpgraders) {
            upgradedTemplates = upgrader.apply(upgradedTemplates);
        }
        return upgradedTemplates;
    };
}
 
Example #5
Source File: DDLClusterStateHelpers.java    From crate with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
static Map<String, Object> mergeTemplateMapping(IndexTemplateMetaData templateMetaData,
                                                        Map<String, Object> newMapping) {
    Map<String, Object> mergedMapping = new HashMap<>();
    for (ObjectObjectCursor<String, CompressedXContent> cursor : templateMetaData.mappings()) {
        Map<String, Object> mapping = parseMapping(cursor.value.toString());
        Object o = mapping.get(Constants.DEFAULT_MAPPING_TYPE);
        assert o instanceof Map :
            "o must not be null and must be instance of Map";

        //noinspection unchecked
        XContentHelper.update(mergedMapping, (Map) o, false);
    }
    XContentHelper.update(mergedMapping, newMapping, false);
    return mergedMapping;
}
 
Example #6
Source File: AbstractOpenCloseTableClusterStateTaskExecutor.java    From crate with Apache License 2.0 6 votes vote down vote up
static IndexTemplateMetaData updateOpenCloseOnPartitionTemplate(IndexTemplateMetaData indexTemplateMetaData,
                                                                boolean openTable) {
    Map<String, Object> metaMap = Collections.singletonMap("_meta", Collections.singletonMap("closed", true));
    if (openTable) {
        //Remove the mapping from the template.
        return DDLClusterStateHelpers.updateTemplate(
            indexTemplateMetaData,
            Collections.emptyMap(),
            metaMap,
            Settings.EMPTY,
            (n, s) -> { },
            s -> true);
    } else {
        //Otherwise, add the mapping to the template.
        return DDLClusterStateHelpers.updateTemplate(
            indexTemplateMetaData,
            metaMap,
            Collections.emptyMap(),
            Settings.EMPTY,
            (n, s) -> { },
            s -> true);
    }
}
 
Example #7
Source File: IndexTemplateUpgraderTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvalidSettingIsRemovedForTemplateInCustomSchema() {
    Settings settings = Settings.builder().put("index.recovery.initial_shards", "quorum").build();
    String templateName = PartitionName.templateName("foobar", "t1");
    IndexTemplateMetaData template = IndexTemplateMetaData.builder(templateName)
        .settings(settings)
        .patterns(Collections.singletonList("*"))
        .build();

    IndexTemplateUpgrader indexTemplateUpgrader = new IndexTemplateUpgrader();
    Map<String, IndexTemplateMetaData> result = indexTemplateUpgrader.apply(Collections.singletonMap(templateName, template));

    assertThat(
        "Outdated setting `index.recovery.initial_shards` must be removed",
        result.get(templateName).settings().hasValue("index.recovery.initial_shards"),
        is(false)
    );
}
 
Example #8
Source File: TransportCreatePartitionsAction.java    From crate with Apache License 2.0 6 votes vote down vote up
private List<IndexTemplateMetaData> findTemplates(CreatePartitionsRequest request, ClusterState state) {
    List<IndexTemplateMetaData> templates = new ArrayList<>();
    String firstIndex = request.indices().iterator().next();


    // note: only use the first index name to see if template matches.
    // this means
    for (ObjectCursor<IndexTemplateMetaData> cursor : state.metaData().templates().values()) {
        IndexTemplateMetaData template = cursor.value;
        for (String pattern : template.getPatterns()) {
            if (Regex.simpleMatch(pattern, firstIndex)) {
                templates.add(template);
                break;
            }
        }
    }
    CollectionUtil.timSort(templates, (o1, o2) -> o2.order() - o1.order());
    return templates;
}
 
Example #9
Source File: TransportCreatePartitionsAction.java    From crate with Apache License 2.0 6 votes vote down vote up
private Settings createIndexSettings(ClusterState currentState, List<IndexTemplateMetaData> templates) {
    Settings.Builder indexSettingsBuilder = Settings.builder();
    // apply templates, here, in reverse order, since first ones are better matching
    for (int i = templates.size() - 1; i >= 0; i--) {
        indexSettingsBuilder.put(templates.get(i).settings());
    }
    if (indexSettingsBuilder.get(IndexMetaData.SETTING_VERSION_CREATED) == null) {
        DiscoveryNodes nodes = currentState.nodes();
        final Version createdVersion = Version.min(Version.CURRENT, nodes.getSmallestNonClientNodeVersion());
        indexSettingsBuilder.put(IndexMetaData.SETTING_VERSION_CREATED, createdVersion);
    }
    if (indexSettingsBuilder.get(IndexMetaData.SETTING_CREATION_DATE) == null) {
        indexSettingsBuilder.put(IndexMetaData.SETTING_CREATION_DATE, new DateTime(DateTimeZone.UTC).getMillis());
    }
    indexSettingsBuilder.put(IndexMetaData.SETTING_INDEX_UUID, UUIDs.randomBase64UUID());

    return indexSettingsBuilder.build();
}
 
Example #10
Source File: TransportGetIndexTemplatesAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
protected void masterOperation(GetIndexTemplatesRequest request, ClusterState state, ActionListener<GetIndexTemplatesResponse> listener) {
    List<IndexTemplateMetaData> results;

    // If we did not ask for a specific name, then we return all templates
    if (request.names().length == 0) {
        results = Arrays.asList(state.metaData().templates().values().toArray(IndexTemplateMetaData.class));
    } else {
        results = new ArrayList<>();
    }

    for (String name : request.names()) {
        if (Regex.isSimpleMatchPattern(name)) {
            for (ObjectObjectCursor<String, IndexTemplateMetaData> entry : state.metaData().templates()) {
                if (Regex.simpleMatch(name, entry.key)) {
                    results.add(entry.value);
                }
            }
        } else if (state.metaData().templates().containsKey(name)) {
            results.add(state.metaData().templates().get(name));
        }
    }

    listener.onResponse(new GetIndexTemplatesResponse(results));
}
 
Example #11
Source File: TransportSchemaUpdateAction.java    From crate with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
static ClusterState updateTemplate(NamedXContentRegistry xContentRegistry,
                                   ClusterState currentState,
                                   String templateName,
                                   Map<String, Object> newMapping) throws Exception {
    IndexTemplateMetaData template = currentState.metaData().templates().get(templateName);
    if (template == null) {
        throw new ResourceNotFoundException("Template \"" + templateName + "\" for partitioned table is missing");
    }

    IndexTemplateMetaData.Builder templateBuilder = new IndexTemplateMetaData.Builder(template);
    for (ObjectObjectCursor<String, CompressedXContent> cursor : template.mappings()) {
        Map<String, Object> source = parseMapping(xContentRegistry, cursor.value.toString());
        mergeIntoSource(source, newMapping);
        try (XContentBuilder xContentBuilder = JsonXContent.contentBuilder()) {
            templateBuilder.putMapping(cursor.key, Strings.toString(xContentBuilder.map(source)));
        }
    }
    MetaData.Builder builder = MetaData.builder(currentState.metaData()).put(templateBuilder);
    return ClusterState.builder(currentState).metaData(builder).build();
}
 
Example #12
Source File: PartitionedTableIntegrationTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@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"));
}
 
Example #13
Source File: Templates.java    From crate with Apache License 2.0 6 votes vote down vote up
public static IndexTemplateMetaData.Builder copyWithNewName(IndexTemplateMetaData source, RelationName newName) {
    String targetTemplateName = PartitionName.templateName(newName.schema(), newName.name());
    String targetAlias = newName.indexNameOrAlias();
    IndexTemplateMetaData.Builder templateBuilder = IndexTemplateMetaData
        .builder(targetTemplateName)
        .patterns(Collections.singletonList(PartitionName.templatePrefix(newName.schema(), newName.name())))
        .settings(source.settings())
        .order(source.order())
        .putAlias(AliasMetaData.builder(targetAlias).build())
        .version(source.version());

    for (ObjectObjectCursor<String, CompressedXContent> mapping : source.mappings()) {
        try {
            templateBuilder.putMapping(mapping.key, mapping.value);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    return templateBuilder;
}
 
Example #14
Source File: AlterTableOperation.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private Map<String, Object> mergeTemplateMapping(IndexTemplateMetaData templateMetaData,
                                                 Map<String, Object> newMapping) {
    Map<String, Object> mergedMapping = new HashMap<>();
    for (ObjectObjectCursor<String, CompressedXContent> cursor : templateMetaData.mappings()) {
        try {
            Map<String, Object> mapping = parseMapping(cursor.value.toString());
            Object o = mapping.get(Constants.DEFAULT_MAPPING_TYPE);
            assert o != null && o instanceof Map;

            XContentHelper.update(mergedMapping, (Map) o, false);
        } catch (IOException e) {
            // pass
        }
    }
    XContentHelper.update(mergedMapping, newMapping, false);
    return mergedMapping;
}
 
Example #15
Source File: AlterTableOperation.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private ListenableFuture<Long> updateTemplate(Map<String, Object> newMappings,
                                              Settings newSettings,
                                              TableIdent tableIdent,
                                              AbstractDDLAnalyzedStatement statement) {
    String templateName = PartitionName.templateName(tableIdent.schema(), tableIdent.name());
    IndexTemplateMetaData indexTemplateMetaData =
            clusterService.state().metaData().templates().get(templateName);
    if (indexTemplateMetaData == null) {
        return Futures.immediateFailedFuture(new RuntimeException("Template for partitioned table is missing"));
    }

    // merge mappings
    Map<String, Object> mapping = mergeTemplateMapping(indexTemplateMetaData, newMappings);

    // merge settings
    Settings.Builder settingsBuilder = Settings.builder();
    settingsBuilder.put(indexTemplateMetaData.settings());
    settingsBuilder.put(newSettings);

    PutIndexTemplateRequest request = new PutIndexTemplateRequest(templateName)
            .create(false)
            .mapping(Constants.DEFAULT_MAPPING_TYPE, mapping)
            .order(indexTemplateMetaData.order())
            .settings(settingsBuilder.build())
            .template(indexTemplateMetaData.template());

    request.putHeader(LoginUserContext.USER_INFO_KEY, statement.getParameterContext().getLoginUserContext());
    for (ObjectObjectCursor<String, AliasMetaData> container : indexTemplateMetaData.aliases()) {
        Alias alias = new Alias(container.key);
        request.alias(alias);
    }

    SettableFuture<Long> result = SettableFuture.create();
    transportActionProvider.transportPutIndexTemplateAction().execute(request,
            new SettableFutureToNullActionListener<PutIndexTemplateResponse>(result));

    return result;
}
 
Example #16
Source File: AbstractOpenCloseTableClusterStateTaskExecutor.java    From crate with Apache License 2.0 5 votes vote down vote up
protected Context prepare(ClusterState currentState, OpenCloseTableOrPartitionRequest request) {
    RelationName relationName = request.tableIdent();
    String partitionIndexName = request.partitionIndexName();
    MetaData metaData = currentState.metaData();
    String indexToResolve = partitionIndexName != null ? partitionIndexName : relationName.indexNameOrAlias();
    PartitionName partitionName = partitionIndexName != null ? PartitionName.fromIndexOrTemplate(partitionIndexName) : null;
    String[] concreteIndices = indexNameExpressionResolver.concreteIndexNames(
        currentState, IndicesOptions.lenientExpandOpen(), indexToResolve);
    Set<IndexMetaData> indicesMetaData = DDLClusterStateHelpers.indexMetaDataSetFromIndexNames(metaData, concreteIndices, indexState());
    IndexTemplateMetaData indexTemplateMetaData = null;
    if (partitionIndexName == null) {
        indexTemplateMetaData = DDLClusterStateHelpers.templateMetaData(metaData, relationName);
    }
    return new Context(indicesMetaData, indexTemplateMetaData, partitionName);
}
 
Example #17
Source File: DDLClusterStateHelpers.java    From crate with Apache License 2.0 5 votes vote down vote up
static IndexTemplateMetaData updateTemplate(IndexTemplateMetaData indexTemplateMetaData,
                                            Map<String, Object> newMappings,
                                            Map<String, Object> mappingsToRemove,
                                            Settings newSettings,
                                            BiConsumer<String, Settings> settingsValidator,
                                            Predicate<String> settingsFilter) {

    // merge mappings & remove mappings
    Map<String, Object> mapping = removeFromMapping(
        mergeTemplateMapping(indexTemplateMetaData, newMappings),
        mappingsToRemove);

    // merge settings
    final Settings settings = Settings.builder()
        .put(indexTemplateMetaData.settings())
        .put(newSettings)
        .normalizePrefix(IndexMetaData.INDEX_SETTING_PREFIX)
        // Private settings must not be (over-)written as they are generated, remove them.
        .build().filter(settingsFilter);

    settingsValidator.accept(indexTemplateMetaData.getName(), settings);

    // wrap it in a type map if its not
    if (mapping.size() != 1 || mapping.containsKey(Constants.DEFAULT_MAPPING_TYPE) == false) {
        mapping = MapBuilder.<String, Object>newMapBuilder().put(Constants.DEFAULT_MAPPING_TYPE, mapping).map();
    }
    try {
        return new IndexTemplateMetaData.Builder(indexTemplateMetaData)
            .settings(settings)
            .putMapping(Constants.DEFAULT_MAPPING_TYPE, Strings.toString(XContentFactory.jsonBuilder().map(mapping)))
            .build();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #18
Source File: DefaultTemplateService.java    From crate with Apache License 2.0 5 votes vote down vote up
public static IndexTemplateMetaData createDefaultIndexTemplateMetaData() throws IOException {
    return IndexTemplateMetaData.builder(TEMPLATE_NAME)
        .order(0)
        .putMapping(Constants.DEFAULT_MAPPING_TYPE, DEFAULT_MAPPING_SOURCE)
        .patterns(Collections.singletonList("*"))
        .build();
}
 
Example #19
Source File: DefaultTemplateService.java    From crate with Apache License 2.0 5 votes vote down vote up
private static ImmutableOpenMap<String, IndexTemplateMetaData> createCopyWithDefaultTemplateAdded(
    ImmutableOpenMap<String, IndexTemplateMetaData> currentTemplates) throws IOException {

    ImmutableOpenMap.Builder<String, IndexTemplateMetaData> builder = ImmutableOpenMap.builder(currentTemplates);
    builder.put(TEMPLATE_NAME, createDefaultIndexTemplateMetaData());
    return builder.build();
}
 
Example #20
Source File: RenameTableClusterStateExecutor.java    From crate with Apache License 2.0 5 votes vote down vote up
private static void renameTemplate(MetaData.Builder newMetaData,
                                   IndexTemplateMetaData sourceTemplateMetaData,
                                   RelationName target) {
    IndexTemplateMetaData.Builder updatedTemplate = Templates.copyWithNewName(sourceTemplateMetaData, target);
    newMetaData
        .removeTemplate(sourceTemplateMetaData.getName())
        .put(updatedTemplate);
}
 
Example #21
Source File: ElasticsearchAssertions.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Assert that an index template is missing
 */
public static void assertIndexTemplateMissing(GetIndexTemplatesResponse templatesResponse, String name) {
    List<String> templateNames = new ArrayList<>();
    for (IndexTemplateMetaData indexTemplateMetaData : templatesResponse.getIndexTemplates()) {
        templateNames.add(indexTemplateMetaData.name());
    }
    assertThat(templateNames, not(hasItem(name)));
}
 
Example #22
Source File: ElasticsearchAssertions.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Assert that an index template exists
 */
public static void assertIndexTemplateExists(GetIndexTemplatesResponse templatesResponse, String name) {
    List<String> templateNames = new ArrayList<>();
    for (IndexTemplateMetaData indexTemplateMetaData : templatesResponse.getIndexTemplates()) {
        templateNames.add(indexTemplateMetaData.name());
    }
    assertThat(templateNames, hasItem(name));
}
 
Example #23
Source File: IndexTemplateUpgraderTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testDefaultTemplateIsUpgraded() throws IOException {
    IndexTemplateUpgrader upgrader = new IndexTemplateUpgrader();

    HashMap<String, IndexTemplateMetaData> templates = new HashMap<>();
    IndexTemplateMetaData oldTemplate = IndexTemplateMetaData.builder(TEMPLATE_NAME)
        .patterns(Collections.singletonList("*"))
        .build();
    templates.put(TEMPLATE_NAME, oldTemplate);

    Map<String, IndexTemplateMetaData> upgradedTemplates = upgrader.apply(templates);
    assertThat(upgradedTemplates.get(TEMPLATE_NAME), is(DefaultTemplateService.createDefaultIndexTemplateMetaData()));
}
 
Example #24
Source File: IndexTemplateUpgraderTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testArchivedSettingsAreRemovedOnPartitionedTableTemplates() {
    IndexTemplateUpgrader upgrader = new IndexTemplateUpgrader();

    Settings settings = Settings.builder()
        .put(ARCHIVED_SETTINGS_PREFIX + "some.setting", true)   // archived, must be filtered out
        .put(SETTING_NUMBER_OF_SHARDS, 4)
        .build();

    HashMap<String, IndexTemplateMetaData> templates = new HashMap<>();
    String partitionTemplateName = PartitionName.templateName("doc", "t1");
    IndexTemplateMetaData oldPartitionTemplate = IndexTemplateMetaData.builder(partitionTemplateName)
        .settings(settings)
        .patterns(Collections.singletonList("*"))
        .build();
    templates.put(partitionTemplateName, oldPartitionTemplate);

    String nonPartitionTemplateName = "non-partition-template";
    IndexTemplateMetaData oldNonPartitionTemplate = IndexTemplateMetaData.builder(nonPartitionTemplateName)
        .settings(settings)
        .patterns(Collections.singletonList("*"))
        .build();
    templates.put(nonPartitionTemplateName, oldNonPartitionTemplate);

    Map<String, IndexTemplateMetaData> upgradedTemplates = upgrader.apply(templates);
    IndexTemplateMetaData upgradedTemplate = upgradedTemplates.get(partitionTemplateName);
    assertThat(upgradedTemplate.settings().keySet(), contains(SETTING_NUMBER_OF_SHARDS));

    // ensure all other attributes remains the same
    assertThat(upgradedTemplate.mappings(), is(oldPartitionTemplate.mappings()));
    assertThat(upgradedTemplate.patterns(), is(oldPartitionTemplate.patterns()));
    assertThat(upgradedTemplate.order(), is(oldPartitionTemplate.order()));
    assertThat(upgradedTemplate.aliases(), is(oldPartitionTemplate.aliases()));

    // ensure non partitioned table templates are untouched
    assertThat(upgradedTemplates.get(nonPartitionTemplateName), is(oldNonPartitionTemplate));
}
 
Example #25
Source File: AlterTableClusterStateExecutorTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testPrivateSettingsAreRemovedOnUpdateTemplate() throws IOException {
    IndexScopedSettings indexScopedSettings = new IndexScopedSettings(Settings.EMPTY, Collections.emptySet());

    RelationName relationName = new RelationName(Schemas.DOC_SCHEMA_NAME, "t1");
    String templateName = PartitionName.templateName(relationName.schema(), relationName.name());

    Settings settings = Settings.builder()
        .put(SETTING_CREATION_DATE, false)      // private, must be filtered out
        .put(SETTING_NUMBER_OF_SHARDS, 4)
        .build();
    IndexTemplateMetaData indexTemplateMetaData = IndexTemplateMetaData.builder(templateName)
        .patterns(Collections.singletonList("*"))
        .settings(settings)
        .build();

    ClusterState initialState = ClusterState.builder(ClusterState.EMPTY_STATE)
        .metaData(MetaData.builder().put(indexTemplateMetaData))
        .build();

    ClusterState result =
        AlterTableClusterStateExecutor.updateTemplate(initialState,
                                                      relationName,
                                                      settings,
                                                      Collections.emptyMap(),
                                                      (x, y) -> { },
                                                      indexScopedSettings);

    IndexTemplateMetaData template = result.getMetaData().getTemplates().get(templateName);
    assertThat(template.settings().keySet(), contains(SETTING_NUMBER_OF_SHARDS));
}
 
Example #26
Source File: DDLClusterStateHelpersTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testMergeTemplateMapping() throws Exception {
    Map<String, Object> oldMapping = MapBuilder.<String, Object>newMapBuilder()
        .put("properties", MapBuilder.<String, String>newMapBuilder().put("foo", "foo").map())
        .put("_meta", MapBuilder.<String, String>newMapBuilder().put("meta1", "val1").map())
        .map();

    Map<String, Object> newMapping = MapBuilder.<String, Object>newMapBuilder()
        .put("properties", MapBuilder.<String, String>newMapBuilder().put("foo", "bar").map())
        .put("_meta", MapBuilder.<String, String>newMapBuilder()
            .put("meta1", "v1")
            .put("meta2", "v2")
            .map())
        .map();

    Map<String, Object> mapping = DDLClusterStateHelpers.mergeTemplateMapping(
        IndexTemplateMetaData.builder("foo")
            .patterns(List.of("*"))
            .putMapping(Constants.DEFAULT_MAPPING_TYPE,
                        Strings.toString(XContentFactory.jsonBuilder().map(
                        MapBuilder.<String, Object>newMapBuilder()
                            .put(Constants.DEFAULT_MAPPING_TYPE, oldMapping)
                            .map())))
            .build(),
        newMapping);
    assertThat(Strings.toString(XContentFactory.jsonBuilder().map(mapping)), is(
        "{\"_meta\":{\"meta2\":\"v2\",\"meta1\":\"v1\"},\"properties\":{\"foo\":\"bar\"}}"));
}
 
Example #27
Source File: PartitionedTableIntegrationTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example #28
Source File: ColumnPolicyIntegrationTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testStrictPartitionedTableInsert() throws Exception {
    execute("create table numbers (" +
            "  num int, " +
            "  odd boolean," +
            "  prime boolean" +
            ") partitioned by (odd) with (column_policy='strict', number_of_replicas=0)");
    ensureYellow();

    GetIndexTemplatesResponse response = client().admin().indices()
        .prepareGetTemplates(PartitionName.templateName(sqlExecutor.getCurrentSchema(), "numbers"))
        .execute().actionGet();
    assertThat(response.getIndexTemplates().size(), is(1));
    IndexTemplateMetaData template = response.getIndexTemplates().get(0);
    CompressedXContent mappingStr = template.mappings().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(mappingStr, is(notNullValue()));
    Tuple<XContentType, Map<String, Object>> typeAndMap =
        XContentHelper.convertToMap(mappingStr.compressedReference(), false, XContentType.JSON);
    @SuppressWarnings("unchecked")
    Map<String, Object> mapping = (Map<String, Object>) typeAndMap.v2().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(decodeMappingValue(mapping.get("dynamic")), is(ColumnPolicy.STRICT));

    execute("insert into numbers (num, odd, prime) values (?, ?, ?)",
        new Object[]{6, true, false});
    execute("refresh table numbers");

    Map<String, Object> sourceMap = getSourceMap(
        new PartitionName(new RelationName("doc", "numbers"), Arrays.asList("true")).asIndexName());
    assertThat(decodeMappingValue(sourceMap.get("dynamic")), is(ColumnPolicy.STRICT));

    expectedException.expect(SQLActionException.class);
    expectedException.expectMessage("Column perfect unknown");
    execute("insert into numbers (num, odd, prime, perfect) values (?, ?, ?, ?)",
        new Object[]{28, true, false, true});
}
 
Example #29
Source File: ColumnPolicyIntegrationTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testStrictPartitionedTableUpdate() throws Exception {
    execute("create table numbers (" +
            "  num int, " +
            "  odd boolean," +
            "  prime boolean" +
            ") partitioned by (odd) with (column_policy='strict', number_of_replicas=0)");
    ensureYellow();

    GetIndexTemplatesResponse response = client().admin().indices()
        .prepareGetTemplates(PartitionName.templateName(sqlExecutor.getCurrentSchema(), "numbers"))
        .execute().actionGet();
    assertThat(response.getIndexTemplates().size(), is(1));
    IndexTemplateMetaData template = response.getIndexTemplates().get(0);
    CompressedXContent mappingStr = template.mappings().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(mappingStr, is(notNullValue()));
    Tuple<XContentType, Map<String, Object>> typeAndMap = XContentHelper.convertToMap(mappingStr.compressedReference(), false);
    @SuppressWarnings("unchecked")
    Map<String, Object> mapping = (Map<String, Object>) typeAndMap.v2().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(decodeMappingValue(mapping.get("dynamic")), is(ColumnPolicy.STRICT));

    execute("insert into numbers (num, odd, prime) values (?, ?, ?)",
        new Object[]{6, true, false});
    execute("refresh table numbers");

    Map<String, Object> sourceMap = getSourceMap(
        new PartitionName(new RelationName("doc", "numbers"), Arrays.asList("true")).asIndexName());
    assertThat(decodeMappingValue(sourceMap.get("dynamic")), is(ColumnPolicy.STRICT));

    expectedException.expect(SQLActionException.class);
    expectedException.expectMessage("Column perfect unknown");
    execute("update numbers set num=?, perfect=? where num=6",
        new Object[]{28, true});
}
 
Example #30
Source File: IndexTemplateUpgrader.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, IndexTemplateMetaData> apply(Map<String, IndexTemplateMetaData> templates) {
    HashMap<String, IndexTemplateMetaData> upgradedTemplates = archiveUnknownOrInvalidSettings(templates);
    try {
        upgradedTemplates.put(TEMPLATE_NAME, DefaultTemplateService.createDefaultIndexTemplateMetaData());
    } catch (IOException e) {
        logger.error("Error while trying to upgrade the default template", e);
    }
    return upgradedTemplates;
}