|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.cluster.metadata; |
| 9 | + |
| 10 | +import org.elasticsearch.common.UUIDs; |
| 11 | +import org.elasticsearch.common.settings.IndexScopedSettings; |
| 12 | +import org.elasticsearch.common.settings.Settings; |
| 13 | +import org.elasticsearch.index.IndexModule; |
| 14 | +import org.elasticsearch.index.mapper.MapperRegistry; |
| 15 | +import org.elasticsearch.plugins.MapperPlugin; |
| 16 | +import org.elasticsearch.test.ESTestCase; |
| 17 | +import org.elasticsearch.xpack.searchablesnapshots.SearchableSnapshotsConstants; |
| 18 | + |
| 19 | +import java.util.Collections; |
| 20 | + |
| 21 | +import static org.elasticsearch.test.VersionUtils.randomIndexCompatibleVersion; |
| 22 | +import static org.hamcrest.Matchers.equalTo; |
| 23 | + |
| 24 | +public class IndexMetadataConversionTests extends ESTestCase { |
| 25 | + |
| 26 | + public void testConvertSearchableSnapshotSettings() { |
| 27 | + IndexMetadataVerifier service = getIndexMetadataVerifier(); |
| 28 | + IndexMetadata src = newIndexMeta("foo", Settings.EMPTY); |
| 29 | + IndexMetadata indexMetadata = service.convertSharedCacheTierPreference(src); |
| 30 | + assertSame(indexMetadata, src); |
| 31 | + |
| 32 | + // A full_copy searchable snapshot (settings should be untouched) |
| 33 | + src = newIndexMeta("foo", Settings.builder() |
| 34 | + .put(IndexModule.INDEX_STORE_TYPE_SETTING.getKey(), "snapshot") |
| 35 | + .put(SearchableSnapshotsConstants.SNAPSHOT_PARTIAL_SETTING.getKey(), false) |
| 36 | + .put("index.routing.allocation.include._tier", "data_hot") |
| 37 | + .put("index.routing.allocation.exclude._tier", "data_warm") |
| 38 | + .put("index.routing.allocation.require._tier", "data_hot") |
| 39 | + .put("index.routing.allocation.include._tier_preference", "data_cold") |
| 40 | + .build()); |
| 41 | + indexMetadata = service.convertSharedCacheTierPreference(src); |
| 42 | + assertSame(indexMetadata, src); |
| 43 | + |
| 44 | + // A shared_cache searchable snapshot with valid settings (metadata should be untouched) |
| 45 | + src = newIndexMeta("foo", Settings.builder() |
| 46 | + .put(IndexModule.INDEX_STORE_TYPE_SETTING.getKey(), "snapshot") |
| 47 | + .put(SearchableSnapshotsConstants.SNAPSHOT_PARTIAL_SETTING.getKey(), false) |
| 48 | + .put("index.routing.allocation.include._tier_preference", "data_frozen") |
| 49 | + .build()); |
| 50 | + indexMetadata = service.convertSharedCacheTierPreference(src); |
| 51 | + assertSame(indexMetadata, src); |
| 52 | + |
| 53 | + // A shared_cache searchable snapshot (should have its settings converted) |
| 54 | + src = newIndexMeta("foo", Settings.builder() |
| 55 | + .put(IndexModule.INDEX_STORE_TYPE_SETTING.getKey(), "snapshot") |
| 56 | + .put(SearchableSnapshotsConstants.SNAPSHOT_PARTIAL_SETTING.getKey(), true) |
| 57 | + .put("index.routing.allocation.include._tier", "data_hot") |
| 58 | + .put("index.routing.allocation.exclude._tier", "data_warm") |
| 59 | + .put("index.routing.allocation.require._tier", "data_hot") |
| 60 | + .put("index.routing.allocation.include._tier_preference", "data_frozen,data_cold") |
| 61 | + .build()); |
| 62 | + indexMetadata = service.convertSharedCacheTierPreference(src); |
| 63 | + assertNotSame(indexMetadata, src); |
| 64 | + Settings newSettings = indexMetadata.getSettings(); |
| 65 | + assertNull(newSettings.get("index.routing.allocation.include._tier")); |
| 66 | + assertNull(newSettings.get("index.routing.allocation.exclude._tier")); |
| 67 | + assertNull(newSettings.get("index.routing.allocation.require._tier")); |
| 68 | + assertThat(newSettings.get("index.routing.allocation.include._tier_preference"), equalTo("data_frozen")); |
| 69 | + } |
| 70 | + |
| 71 | + private IndexMetadataVerifier getIndexMetadataVerifier() { |
| 72 | + return new IndexMetadataVerifier( |
| 73 | + Settings.EMPTY, |
| 74 | + xContentRegistry(), |
| 75 | + new MapperRegistry(Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap(), |
| 76 | + MapperPlugin.NOOP_FIELD_FILTER), IndexScopedSettings.DEFAULT_SCOPED_SETTINGS, |
| 77 | + null |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + public static IndexMetadata newIndexMeta(String name, Settings indexSettings) { |
| 82 | + final Settings settings = Settings.builder() |
| 83 | + .put(IndexMetadata.SETTING_VERSION_CREATED, randomIndexCompatibleVersion(random())) |
| 84 | + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, between(0, 5)) |
| 85 | + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, between(1, 5)) |
| 86 | + .put(IndexMetadata.SETTING_CREATION_DATE, randomNonNegativeLong()) |
| 87 | + .put(IndexMetadata.SETTING_INDEX_UUID, UUIDs.randomBase64UUID(random())) |
| 88 | + .put(indexSettings) |
| 89 | + .build(); |
| 90 | + final IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(name).settings(settings); |
| 91 | + if (randomBoolean()) { |
| 92 | + indexMetadataBuilder.state(IndexMetadata.State.CLOSE); |
| 93 | + } |
| 94 | + return indexMetadataBuilder.build(); |
| 95 | + } |
| 96 | +} |
0 commit comments