diff --git a/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/DatabaseNodeServiceTests.java b/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/DatabaseNodeServiceTests.java index 031fc91403cfb..387231b346a8b 100644 --- a/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/DatabaseNodeServiceTests.java +++ b/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/DatabaseNodeServiceTests.java @@ -405,7 +405,7 @@ static ClusterState createClusterState( : GeoIpDownloader.DATABASES_INDEX; Index index = new Index(indexName, UUID.randomUUID().toString()); IndexMetadata.Builder idxMeta = IndexMetadata.builder(index.getName()) - .settings(indexSettings(IndexVersion.current(), 1, 0).put("index.uuid", index.getUUID())); + .settings(indexSettings(IndexVersion.current(), index.getUUID(), 1, 0)); if (aliasGeoipDatabase) { idxMeta.putAlias(AliasMetadata.builder(GeoIpDownloader.DATABASES_INDEX)); } diff --git a/modules/parent-join/src/test/java/org/elasticsearch/join/aggregations/ChildrenToParentAggregatorTests.java b/modules/parent-join/src/test/java/org/elasticsearch/join/aggregations/ChildrenToParentAggregatorTests.java index 707fcc822665f..583f1634f2069 100644 --- a/modules/parent-join/src/test/java/org/elasticsearch/join/aggregations/ChildrenToParentAggregatorTests.java +++ b/modules/parent-join/src/test/java/org/elasticsearch/join/aggregations/ChildrenToParentAggregatorTests.java @@ -20,6 +20,7 @@ import org.apache.lucene.store.Directory; import org.apache.lucene.tests.index.RandomIndexWriter; import org.apache.lucene.util.BytesRef; +import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader; import org.elasticsearch.core.Tuple; import org.elasticsearch.index.Index; @@ -86,7 +87,7 @@ public void testParentChild() throws IOException { DirectoryReader indexReader = ElasticsearchDirectoryReader.wrap( DirectoryReader.open(directory), - new ShardId(new Index("foo", "_na_"), 1) + new ShardId(new Index("foo", IndexMetadata.INDEX_UUID_NA_VALUE), 1) ); // verify with all documents testCase(new MatchAllDocsQuery(), indexReader, parent -> { @@ -151,7 +152,7 @@ public void testParentChildTerms() throws IOException { DirectoryReader indexReader = ElasticsearchDirectoryReader.wrap( DirectoryReader.open(directory), - new ShardId(new Index("foo", "_na_"), 1) + new ShardId(new Index("foo", IndexMetadata.INDEX_UUID_NA_VALUE), 1) ); // verify a terms-aggregation inside the parent-aggregation testCaseTerms(new MatchAllDocsQuery(), indexReader, parent -> { @@ -192,7 +193,7 @@ public void testTermsParentChildTerms() throws IOException { DirectoryReader indexReader = ElasticsearchDirectoryReader.wrap( DirectoryReader.open(directory), - new ShardId(new Index("foo", "_na_"), 1) + new ShardId(new Index("foo", IndexMetadata.INDEX_UUID_NA_VALUE), 1) ); // verify a terms-aggregation inside the parent-aggregation which itself is inside a // terms-aggregation on the child-documents diff --git a/modules/parent-join/src/test/java/org/elasticsearch/join/aggregations/ParentToChildrenAggregatorTests.java b/modules/parent-join/src/test/java/org/elasticsearch/join/aggregations/ParentToChildrenAggregatorTests.java index ca90b0e588b18..159e389b4dae5 100644 --- a/modules/parent-join/src/test/java/org/elasticsearch/join/aggregations/ParentToChildrenAggregatorTests.java +++ b/modules/parent-join/src/test/java/org/elasticsearch/join/aggregations/ParentToChildrenAggregatorTests.java @@ -24,6 +24,7 @@ import org.apache.lucene.store.Directory; import org.apache.lucene.tests.index.RandomIndexWriter; import org.apache.lucene.util.BytesRef; +import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader; import org.elasticsearch.core.Tuple; import org.elasticsearch.index.Index; @@ -89,7 +90,7 @@ public void testParentChild() throws IOException { DirectoryReader indexReader = ElasticsearchDirectoryReader.wrap( DirectoryReader.open(directory), - new ShardId(new Index("foo", "_na_"), 1) + new ShardId(new Index("foo", IndexMetadata.INDEX_UUID_NA_VALUE), 1) ); testCase(new MatchAllDocsQuery(), indexReader, child -> { int expectedTotalChildren = 0; @@ -128,7 +129,7 @@ public void testParentChildAsSubAgg() throws IOException { try ( DirectoryReader indexReader = ElasticsearchDirectoryReader.wrap( DirectoryReader.open(directory), - new ShardId(new Index("foo", "_na_"), 1) + new ShardId(new Index("foo", IndexMetadata.INDEX_UUID_NA_VALUE), 1) ) ) { AggregationBuilder request = new TermsAggregationBuilder("t").field("kwd") @@ -180,7 +181,7 @@ public void testBestDeferringCollectorWithSubAggOfChildrenAggNeedingScores() thr try ( var indexReader = ElasticsearchDirectoryReader.wrap( DirectoryReader.open(directory), - new ShardId(new Index("foo", "_na_"), 1) + new ShardId(new Index("foo", IndexMetadata.INDEX_UUID_NA_VALUE), 1) ) ) { // invalid usage, diff --git a/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/IcuTokenizerFactoryTests.java b/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/IcuTokenizerFactoryTests.java index b508542cbf0bc..9bfcf3c00aafd 100644 --- a/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/IcuTokenizerFactoryTests.java +++ b/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/IcuTokenizerFactoryTests.java @@ -103,6 +103,6 @@ private static TestAnalysis createTestAnalysis() throws IOException { .build(); Settings nodeSettings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), home).build(); - return createTestAnalysis(new Index("test", "_na_"), nodeSettings, settings, new AnalysisICUPlugin()); + return createTestAnalysis(new Index("test", IndexMetadata.INDEX_UUID_NA_VALUE), nodeSettings, settings, new AnalysisICUPlugin()); } } diff --git a/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/SimpleIcuAnalysisTests.java b/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/SimpleIcuAnalysisTests.java index 5fa18c3a2132a..4f765cd09558d 100644 --- a/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/SimpleIcuAnalysisTests.java +++ b/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/SimpleIcuAnalysisTests.java @@ -9,6 +9,7 @@ package org.elasticsearch.plugin.analysis.icu; +import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.Index; import org.elasticsearch.index.analysis.CharFilterFactory; @@ -22,7 +23,11 @@ public class SimpleIcuAnalysisTests extends ESTestCase { public void testDefaultsIcuAnalysis() throws IOException { - TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), Settings.EMPTY, new AnalysisICUPlugin()); + TestAnalysis analysis = createTestAnalysis( + new Index("test", IndexMetadata.INDEX_UUID_NA_VALUE), + Settings.EMPTY, + new AnalysisICUPlugin() + ); TokenizerFactory tokenizerFactory = analysis.tokenizer.get("icu_tokenizer"); assertThat(tokenizerFactory, instanceOf(IcuTokenizerFactory.class)); diff --git a/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/SimpleIcuCollationTokenFilterTests.java b/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/SimpleIcuCollationTokenFilterTests.java index 9265fac32cb78..2946083177d12 100644 --- a/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/SimpleIcuCollationTokenFilterTests.java +++ b/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/SimpleIcuCollationTokenFilterTests.java @@ -17,6 +17,7 @@ import org.apache.lucene.analysis.Tokenizer; import org.apache.lucene.analysis.core.KeywordTokenizer; import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; +import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.Index; import org.elasticsearch.index.analysis.TokenFilterFactory; @@ -37,7 +38,7 @@ public void testDefaultUsage() throws Exception { .put("index.analysis.filter.myCollator.type", "icu_collation") .put("index.analysis.filter.myCollator.strength", "primary") .build(); - TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), settings, new AnalysisICUPlugin()); + TestAnalysis analysis = createTestAnalysis(new Index("test", IndexMetadata.INDEX_UUID_NA_VALUE), settings, new AnalysisICUPlugin()); TokenFilterFactory filterFactory = analysis.tokenFilter.get("myCollator"); assertCollatesToSame(filterFactory, "FOO", "foo"); @@ -55,7 +56,7 @@ public void testBasicUsage() throws Exception { .put("index.analysis.filter.myCollator.language", "tr") .put("index.analysis.filter.myCollator.strength", "primary") .build(); - TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), settings, new AnalysisICUPlugin()); + TestAnalysis analysis = createTestAnalysis(new Index("test", IndexMetadata.INDEX_UUID_NA_VALUE), settings, new AnalysisICUPlugin()); TokenFilterFactory filterFactory = analysis.tokenFilter.get("myCollator"); assertCollatesToSame(filterFactory, "I WİLL USE TURKİSH CASING", "ı will use turkish casıng"); @@ -71,7 +72,7 @@ public void testNormalization() throws IOException { .put("index.analysis.filter.myCollator.strength", "primary") .put("index.analysis.filter.myCollator.decomposition", "canonical") .build(); - TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), settings, new AnalysisICUPlugin()); + TestAnalysis analysis = createTestAnalysis(new Index("test", IndexMetadata.INDEX_UUID_NA_VALUE), settings, new AnalysisICUPlugin()); TokenFilterFactory filterFactory = analysis.tokenFilter.get("myCollator"); assertCollatesToSame(filterFactory, "I W\u0049\u0307LL USE TURKİSH CASING", "ı will use turkish casıng"); @@ -87,7 +88,7 @@ public void testSecondaryStrength() throws IOException { .put("index.analysis.filter.myCollator.strength", "secondary") .put("index.analysis.filter.myCollator.decomposition", "no") .build(); - TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), settings, new AnalysisICUPlugin()); + TestAnalysis analysis = createTestAnalysis(new Index("test", IndexMetadata.INDEX_UUID_NA_VALUE), settings, new AnalysisICUPlugin()); TokenFilterFactory filterFactory = analysis.tokenFilter.get("myCollator"); assertCollatesToSame(filterFactory, "TESTING", "testing"); @@ -104,7 +105,7 @@ public void testIgnorePunctuation() throws IOException { .put("index.analysis.filter.myCollator.strength", "primary") .put("index.analysis.filter.myCollator.alternate", "shifted") .build(); - TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), settings, new AnalysisICUPlugin()); + TestAnalysis analysis = createTestAnalysis(new Index("test", IndexMetadata.INDEX_UUID_NA_VALUE), settings, new AnalysisICUPlugin()); TokenFilterFactory filterFactory = analysis.tokenFilter.get("myCollator"); assertCollatesToSame(filterFactory, "foo-bar", "foo bar"); @@ -122,7 +123,7 @@ public void testIgnoreWhitespace() throws IOException { .put("index.analysis.filter.myCollator.alternate", "shifted") .put("index.analysis.filter.myCollator.variableTop", " ") .build(); - TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), settings, new AnalysisICUPlugin()); + TestAnalysis analysis = createTestAnalysis(new Index("test", IndexMetadata.INDEX_UUID_NA_VALUE), settings, new AnalysisICUPlugin()); TokenFilterFactory filterFactory = analysis.tokenFilter.get("myCollator"); assertCollatesToSame(filterFactory, "foo bar", "foobar"); @@ -140,7 +141,7 @@ public void testNumerics() throws IOException { .put("index.analysis.filter.myCollator.language", "en") .put("index.analysis.filter.myCollator.numeric", "true") .build(); - TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), settings, new AnalysisICUPlugin()); + TestAnalysis analysis = createTestAnalysis(new Index("test", IndexMetadata.INDEX_UUID_NA_VALUE), settings, new AnalysisICUPlugin()); TokenFilterFactory filterFactory = analysis.tokenFilter.get("myCollator"); assertCollation(filterFactory, "foobar-9", "foobar-10", -1); @@ -157,7 +158,7 @@ public void testIgnoreAccentsButNotCase() throws IOException { .put("index.analysis.filter.myCollator.strength", "primary") .put("index.analysis.filter.myCollator.caseLevel", "true") .build(); - TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), settings, new AnalysisICUPlugin()); + TestAnalysis analysis = createTestAnalysis(new Index("test", IndexMetadata.INDEX_UUID_NA_VALUE), settings, new AnalysisICUPlugin()); TokenFilterFactory filterFactory = analysis.tokenFilter.get("myCollator"); assertCollatesToSame(filterFactory, "résumé", "resume"); @@ -177,7 +178,7 @@ public void testUpperCaseFirst() throws IOException { .put("index.analysis.filter.myCollator.strength", "tertiary") .put("index.analysis.filter.myCollator.caseFirst", "upper") .build(); - TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), settings, new AnalysisICUPlugin()); + TestAnalysis analysis = createTestAnalysis(new Index("test", IndexMetadata.INDEX_UUID_NA_VALUE), settings, new AnalysisICUPlugin()); TokenFilterFactory filterFactory = analysis.tokenFilter.get("myCollator"); assertCollation(filterFactory, "Resume", "resume", -1); @@ -202,7 +203,7 @@ public void testCustomRules() throws Exception { .put("index.analysis.filter.myCollator.rules", tailoredRules) .put("index.analysis.filter.myCollator.strength", "primary") .build(); - TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), settings, new AnalysisICUPlugin()); + TestAnalysis analysis = createTestAnalysis(new Index("test", IndexMetadata.INDEX_UUID_NA_VALUE), settings, new AnalysisICUPlugin()); TokenFilterFactory filterFactory = analysis.tokenFilter.get("myCollator"); assertCollatesToSame(filterFactory, "Töne", "Toene"); @@ -218,7 +219,7 @@ public void testBasicCustomRules() throws Exception { .put("index.analysis.filter.myCollator.type", "icu_collation") .put("index.analysis.filter.myCollator.rules", "&a < g") .build(); - TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), settings, new AnalysisICUPlugin()); + TestAnalysis analysis = createTestAnalysis(new Index("test", IndexMetadata.INDEX_UUID_NA_VALUE), settings, new AnalysisICUPlugin()); TokenFilterFactory filterFactory = analysis.tokenFilter.get("myCollator"); assertCollation(filterFactory, "green", "bird", -1); diff --git a/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/SimpleIcuNormalizerCharFilterTests.java b/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/SimpleIcuNormalizerCharFilterTests.java index 2b2c155e6f6d7..794860b16162e 100644 --- a/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/SimpleIcuNormalizerCharFilterTests.java +++ b/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/SimpleIcuNormalizerCharFilterTests.java @@ -12,6 +12,7 @@ import com.ibm.icu.text.Normalizer2; import org.apache.lucene.analysis.CharFilter; +import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.Index; import org.elasticsearch.index.analysis.CharFilterFactory; @@ -25,7 +26,7 @@ public class SimpleIcuNormalizerCharFilterTests extends ESTestCase { public void testDefaultSetting() throws Exception { Settings settings = Settings.builder().put("index.analysis.char_filter.myNormalizerChar.type", "icu_normalizer").build(); - TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), settings, new AnalysisICUPlugin()); + TestAnalysis analysis = createTestAnalysis(new Index("test", IndexMetadata.INDEX_UUID_NA_VALUE), settings, new AnalysisICUPlugin()); CharFilterFactory charFilterFactory = analysis.charFilter.get("myNormalizerChar"); String input = "ʰ㌰゙5℃№㈱㌘,バッファーの正規化のテスト.㋐㋑㋒㋓㋔カキクケコザジズゼゾg̈각/각நிเกषिchkʷक्षि"; @@ -49,7 +50,7 @@ public void testNameAndModeSetting() throws Exception { .put("index.analysis.char_filter.myNormalizerChar.name", "nfkc") .put("index.analysis.char_filter.myNormalizerChar.mode", "decompose") .build(); - TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), settings, new AnalysisICUPlugin()); + TestAnalysis analysis = createTestAnalysis(new Index("test", IndexMetadata.INDEX_UUID_NA_VALUE), settings, new AnalysisICUPlugin()); CharFilterFactory charFilterFactory = analysis.charFilter.get("myNormalizerChar"); String input = "ʰ㌰゙5℃№㈱㌘,バッファーの正規化のテスト.㋐㋑㋒㋓㋔カキクケコザジズゼゾg̈각/각நிเกषिchkʷक्षि"; diff --git a/plugins/analysis-kuromoji/src/test/java/org/elasticsearch/plugin/analysis/kuromoji/KuromojiAnalysisTests.java b/plugins/analysis-kuromoji/src/test/java/org/elasticsearch/plugin/analysis/kuromoji/KuromojiAnalysisTests.java index f26213d86c5a9..238cef6f13383 100644 --- a/plugins/analysis-kuromoji/src/test/java/org/elasticsearch/plugin/analysis/kuromoji/KuromojiAnalysisTests.java +++ b/plugins/analysis-kuromoji/src/test/java/org/elasticsearch/plugin/analysis/kuromoji/KuromojiAnalysisTests.java @@ -290,7 +290,12 @@ private static TestAnalysis createTestAnalysis() throws IOException { .put(IndexMetadata.SETTING_VERSION_CREATED, IndexVersion.current()) .build(); Settings nodeSettings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), home).build(); - return createTestAnalysis(new Index("test", "_na_"), nodeSettings, settings, new AnalysisKuromojiPlugin()); + return createTestAnalysis( + new Index("test", IndexMetadata.INDEX_UUID_NA_VALUE), + nodeSettings, + settings, + new AnalysisKuromojiPlugin() + ); } public static void assertSimpleTSOutput(TokenStream stream, String[] expected) throws IOException { diff --git a/plugins/analysis-phonetic/src/test/java/org/elasticsearch/plugin/analysis/phonetic/SimplePhoneticAnalysisTests.java b/plugins/analysis-phonetic/src/test/java/org/elasticsearch/plugin/analysis/phonetic/SimplePhoneticAnalysisTests.java index ad06f1036120b..89fbd52e1cafa 100644 --- a/plugins/analysis-phonetic/src/test/java/org/elasticsearch/plugin/analysis/phonetic/SimplePhoneticAnalysisTests.java +++ b/plugins/analysis-phonetic/src/test/java/org/elasticsearch/plugin/analysis/phonetic/SimplePhoneticAnalysisTests.java @@ -38,7 +38,7 @@ public void setup() throws IOException { .loadFromStream(yaml, getClass().getResourceAsStream(yaml), false) .put(IndexMetadata.SETTING_VERSION_CREATED, IndexVersion.current()) .build(); - this.analysis = createTestAnalysis(new Index("test", "_na_"), settings, new AnalysisPhoneticPlugin()); + this.analysis = createTestAnalysis(new Index("test", IndexMetadata.INDEX_UUID_NA_VALUE), settings, new AnalysisPhoneticPlugin()); } public void testPhoneticTokenFilterFactory() throws IOException { diff --git a/plugins/analysis-smartcn/src/test/java/org/elasticsearch/plugin/analysis/smartcn/SimpleSmartChineseAnalysisTests.java b/plugins/analysis-smartcn/src/test/java/org/elasticsearch/plugin/analysis/smartcn/SimpleSmartChineseAnalysisTests.java index 09c27ff127885..1de0c3c7bfe5e 100644 --- a/plugins/analysis-smartcn/src/test/java/org/elasticsearch/plugin/analysis/smartcn/SimpleSmartChineseAnalysisTests.java +++ b/plugins/analysis-smartcn/src/test/java/org/elasticsearch/plugin/analysis/smartcn/SimpleSmartChineseAnalysisTests.java @@ -9,6 +9,7 @@ package org.elasticsearch.plugin.analysis.smartcn; +import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.Index; import org.elasticsearch.index.analysis.TokenizerFactory; @@ -21,7 +22,11 @@ public class SimpleSmartChineseAnalysisTests extends ESTestCase { public void testDefaultsIcuAnalysis() throws IOException { - final TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), Settings.EMPTY, new AnalysisSmartChinesePlugin()); + final TestAnalysis analysis = createTestAnalysis( + new Index("test", IndexMetadata.INDEX_UUID_NA_VALUE), + Settings.EMPTY, + new AnalysisSmartChinesePlugin() + ); TokenizerFactory tokenizerFactory = analysis.tokenizer.get("smartcn_tokenizer"); MatcherAssert.assertThat(tokenizerFactory, instanceOf(SmartChineseTokenizerTokenizerFactory.class)); } diff --git a/plugins/analysis-stempel/src/test/java/org/elasticsearch/index/analysis/AnalysisPolishFactoryTests.java b/plugins/analysis-stempel/src/test/java/org/elasticsearch/index/analysis/AnalysisPolishFactoryTests.java index aa7f773b86206..6951fb9ac03c4 100644 --- a/plugins/analysis-stempel/src/test/java/org/elasticsearch/index/analysis/AnalysisPolishFactoryTests.java +++ b/plugins/analysis-stempel/src/test/java/org/elasticsearch/index/analysis/AnalysisPolishFactoryTests.java @@ -42,9 +42,10 @@ protected Map> getTokenFilters() { public void testThreadSafety() throws IOException { // TODO: is this the right boilerplate? I forked this out of TransportAnalyzeAction.java: - Settings settings = indexSettings(IndexVersion.current(), 1, 0).put(IndexMetadata.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()) - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .build(); + Settings settings = indexSettings(IndexVersion.current(), UUIDs.randomBase64UUID(), 1, 0).put( + Environment.PATH_HOME_SETTING.getKey(), + createTempDir().toString() + ).build(); Environment environment = TestEnvironment.newEnvironment(settings); IndexMetadata metadata = IndexMetadata.builder(IndexMetadata.INDEX_UUID_NA_VALUE).settings(settings).build(); IndexSettings indexSettings = new IndexSettings(metadata, Settings.EMPTY); diff --git a/plugins/analysis-stempel/src/test/java/org/elasticsearch/index/analysis/PolishAnalysisTests.java b/plugins/analysis-stempel/src/test/java/org/elasticsearch/index/analysis/PolishAnalysisTests.java index 458b4bacab3fa..53d3db9c546a8 100644 --- a/plugins/analysis-stempel/src/test/java/org/elasticsearch/index/analysis/PolishAnalysisTests.java +++ b/plugins/analysis-stempel/src/test/java/org/elasticsearch/index/analysis/PolishAnalysisTests.java @@ -11,6 +11,7 @@ import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.pl.PolishAnalyzer; +import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.Index; import org.elasticsearch.index.analysis.pl.PolishStemTokenFilterFactory; @@ -24,7 +25,11 @@ public class PolishAnalysisTests extends ESTestCase { public void testDefaultsPolishAnalysis() throws IOException { - final TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), Settings.EMPTY, new AnalysisStempelPlugin()); + final TestAnalysis analysis = createTestAnalysis( + new Index("test", IndexMetadata.INDEX_UUID_NA_VALUE), + Settings.EMPTY, + new AnalysisStempelPlugin() + ); TokenFilterFactory tokenizerFactory = analysis.tokenFilter.get("polish_stem"); MatcherAssert.assertThat(tokenizerFactory, instanceOf(PolishStemTokenFilterFactory.class)); diff --git a/plugins/analysis-stempel/src/test/java/org/elasticsearch/index/analysis/SimplePolishTokenFilterTests.java b/plugins/analysis-stempel/src/test/java/org/elasticsearch/index/analysis/SimplePolishTokenFilterTests.java index 071a36bdb52b5..447cd00958095 100644 --- a/plugins/analysis-stempel/src/test/java/org/elasticsearch/index/analysis/SimplePolishTokenFilterTests.java +++ b/plugins/analysis-stempel/src/test/java/org/elasticsearch/index/analysis/SimplePolishTokenFilterTests.java @@ -14,6 +14,7 @@ import org.apache.lucene.analysis.Tokenizer; import org.apache.lucene.analysis.core.KeywordTokenizer; import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; +import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.Index; import org.elasticsearch.plugin.analysis.stempel.AnalysisStempelPlugin; @@ -35,7 +36,7 @@ public void testBasicUsage() throws Exception { } private void testToken(String source, String expected) throws IOException { - Index index = new Index("test", "_na_"); + Index index = new Index("test", IndexMetadata.INDEX_UUID_NA_VALUE); Settings settings = Settings.builder().put("index.analysis.filter.myStemmer.type", "polish_stem").build(); TestAnalysis analysis = createTestAnalysis(index, settings, new AnalysisStempelPlugin()); @@ -53,7 +54,11 @@ private void testToken(String source, String expected) throws IOException { } private void testAnalyzer(String source, String... expected_terms) throws IOException { - TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), Settings.EMPTY, new AnalysisStempelPlugin()); + TestAnalysis analysis = createTestAnalysis( + new Index("test", IndexMetadata.INDEX_UUID_NA_VALUE), + Settings.EMPTY, + new AnalysisStempelPlugin() + ); Analyzer analyzer = analysis.indexAnalyzers.get("polish").analyzer(); diff --git a/plugins/analysis-ukrainian/src/test/java/org/elasticsearch/plugin/analysis/ukrainian/SimpleUkrainianAnalyzerTests.java b/plugins/analysis-ukrainian/src/test/java/org/elasticsearch/plugin/analysis/ukrainian/SimpleUkrainianAnalyzerTests.java index 18ba55615f63a..b6da399fd99d4 100644 --- a/plugins/analysis-ukrainian/src/test/java/org/elasticsearch/plugin/analysis/ukrainian/SimpleUkrainianAnalyzerTests.java +++ b/plugins/analysis-ukrainian/src/test/java/org/elasticsearch/plugin/analysis/ukrainian/SimpleUkrainianAnalyzerTests.java @@ -12,6 +12,7 @@ import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; +import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.Index; import org.elasticsearch.test.ESTestCase; @@ -30,7 +31,11 @@ public void testBasicUsage() throws Exception { } private static void testAnalyzer(String source, String... expected_terms) throws IOException { - TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), Settings.EMPTY, new AnalysisUkrainianPlugin()); + TestAnalysis analysis = createTestAnalysis( + new Index("test", IndexMetadata.INDEX_UUID_NA_VALUE), + Settings.EMPTY, + new AnalysisUkrainianPlugin() + ); Analyzer analyzer = analysis.indexAnalyzers.get("ukrainian").analyzer(); TokenStream ts = analyzer.tokenStream("test", source); CharTermAttribute term1 = ts.addAttribute(CharTermAttribute.class); diff --git a/plugins/analysis-ukrainian/src/test/java/org/elasticsearch/plugin/analysis/ukrainian/UkrainianAnalysisTests.java b/plugins/analysis-ukrainian/src/test/java/org/elasticsearch/plugin/analysis/ukrainian/UkrainianAnalysisTests.java index baca4d2419cb5..a9380c5914551 100644 --- a/plugins/analysis-ukrainian/src/test/java/org/elasticsearch/plugin/analysis/ukrainian/UkrainianAnalysisTests.java +++ b/plugins/analysis-ukrainian/src/test/java/org/elasticsearch/plugin/analysis/ukrainian/UkrainianAnalysisTests.java @@ -11,6 +11,7 @@ import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.uk.UkrainianMorfologikAnalyzer; +import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.Index; import org.elasticsearch.test.ESTestCase; @@ -23,7 +24,11 @@ public class UkrainianAnalysisTests extends ESTestCase { public void testDefaultsUkranianAnalysis() throws IOException { - final TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), Settings.EMPTY, new AnalysisUkrainianPlugin()); + final TestAnalysis analysis = createTestAnalysis( + new Index("test", IndexMetadata.INDEX_UUID_NA_VALUE), + Settings.EMPTY, + new AnalysisUkrainianPlugin() + ); Analyzer analyzer = analysis.indexAnalyzers.get("ukrainian").analyzer(); MatcherAssert.assertThat(analyzer, instanceOf(UkrainianMorfologikAnalyzer.class)); diff --git a/server/src/internalClusterTest/java/org/elasticsearch/cluster/ClusterStateDiffIT.java b/server/src/internalClusterTest/java/org/elasticsearch/cluster/ClusterStateDiffIT.java index 67cead7b17371..dbf08fc8ab784 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/cluster/ClusterStateDiffIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/cluster/ClusterStateDiffIT.java @@ -300,7 +300,12 @@ private ClusterState.Builder randomRoutingTable(ClusterState clusterState) { } int additionalIndexCount = randomIntBetween(1, 20); for (int i = 0; i < additionalIndexCount; i++) { - builder.add(randomIndexRoutingTable("index-" + randomInt(), clusterState.nodes().getNodes().keySet().toArray(new String[0]))); + builder.add( + randomIndexRoutingTable( + new Index("index-" + randomInt(), randomUUID()), + clusterState.nodes().getNodes().keySet().toArray(new String[0]) + ) + ); } return ClusterState.builder(clusterState).routingTable(builder.build()); } @@ -308,12 +313,12 @@ private ClusterState.Builder randomRoutingTable(ClusterState clusterState) { /** * Randomly updates index routing table in the cluster state */ - private IndexRoutingTable randomIndexRoutingTable(String index, String[] nodeIds) { - IndexRoutingTable.Builder builder = IndexRoutingTable.builder(new Index(index, "_na_")); + private IndexRoutingTable randomIndexRoutingTable(Index index, String[] nodeIds) { + IndexRoutingTable.Builder builder = IndexRoutingTable.builder(index); int shardCount = randomInt(10); for (int i = 0; i < shardCount; i++) { - IndexShardRoutingTable.Builder indexShard = new IndexShardRoutingTable.Builder(new ShardId(index, "_na_", i)); + IndexShardRoutingTable.Builder indexShard = new IndexShardRoutingTable.Builder(new ShardId(index, i)); int replicaCount = randomIntBetween(1, 10); Set availableNodeIds = Sets.newHashSet(nodeIds); for (int j = 0; j < replicaCount; j++) { diff --git a/server/src/main/java/org/elasticsearch/cluster/routing/RoutingNode.java b/server/src/main/java/org/elasticsearch/cluster/routing/RoutingNode.java index 2b5806724c75f..a27bbf82aa2f9 100644 --- a/server/src/main/java/org/elasticsearch/cluster/routing/RoutingNode.java +++ b/server/src/main/java/org/elasticsearch/cluster/routing/RoutingNode.java @@ -343,6 +343,7 @@ boolean invariant() { assert relocatingShards.size() == shardRoutingsRelocating.size() && relocatingShards.containsAll(shardRoutingsRelocating); assert startedShards.size() == shardRoutingsStarted.size() && startedShards.containsAll(shardRoutingsStarted); assert shardRoutingsByIndex.equals(shardsByIndex); + assert shardsByIndex.size() == shardsByIndex.keySet().stream().map(Index::getUUID).distinct().count(); return true; } diff --git a/server/src/main/java/org/elasticsearch/indices/store/TransportNodesListShardStoreMetadata.java b/server/src/main/java/org/elasticsearch/indices/store/TransportNodesListShardStoreMetadata.java index 8d3008bc28758..7090681167e6f 100644 --- a/server/src/main/java/org/elasticsearch/indices/store/TransportNodesListShardStoreMetadata.java +++ b/server/src/main/java/org/elasticsearch/indices/store/TransportNodesListShardStoreMetadata.java @@ -201,7 +201,7 @@ public record StoreFilesMetadata(Store.MetadataSnapshot metadataSnapshot, List, Writeable { - private static final ShardId FAKE_SHARD_ID = new ShardId("_na_", "_na_", 0); + private static final ShardId FAKE_SHARD_ID = new ShardId("_na_", IndexMetadata.INDEX_UUID_NA_VALUE, 0); public static final StoreFilesMetadata EMPTY = new StoreFilesMetadata(Store.MetadataSnapshot.EMPTY, emptyList()); public static StoreFilesMetadata readFrom(StreamInput in) throws IOException { diff --git a/server/src/test/java/org/elasticsearch/ElasticsearchExceptionTests.java b/server/src/test/java/org/elasticsearch/ElasticsearchExceptionTests.java index 2741ac37ce519..4645d34ddba2f 100644 --- a/server/src/test/java/org/elasticsearch/ElasticsearchExceptionTests.java +++ b/server/src/test/java/org/elasticsearch/ElasticsearchExceptionTests.java @@ -19,6 +19,7 @@ import org.elasticsearch.client.internal.transport.NoNodeAvailableException; import org.elasticsearch.cluster.block.ClusterBlockException; import org.elasticsearch.cluster.coordination.NoMasterBlockService; +import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.node.DiscoveryNodeUtils; import org.elasticsearch.common.ParsingException; @@ -114,11 +115,11 @@ public void testGuessRootCause() { assertEquals("no such index [foo]", rootCauses[0].getMessage()); ShardSearchFailure failure = new ShardSearchFailure( new ParsingException(1, 2, "foobar", null), - new SearchShardTarget("node_1", new ShardId("foo", "_na_", 1), null) + new SearchShardTarget("node_1", new ShardId("foo", IndexMetadata.INDEX_UUID_NA_VALUE, 1), null) ); ShardSearchFailure failure1 = new ShardSearchFailure( new ParsingException(1, 2, "foobar", null), - new SearchShardTarget("node_1", new ShardId("foo", "_na_", 2), null) + new SearchShardTarget("node_1", new ShardId("foo", IndexMetadata.INDEX_UUID_NA_VALUE, 2), null) ); SearchPhaseExecutionException ex = new SearchPhaseExecutionException( "search", @@ -141,15 +142,15 @@ public void testGuessRootCause() { { ShardSearchFailure failure = new ShardSearchFailure( new ParsingException(1, 2, "foobar", null), - new SearchShardTarget("node_1", new ShardId("foo", "_na_", 1), null) + new SearchShardTarget("node_1", new ShardId("foo", IndexMetadata.INDEX_UUID_NA_VALUE, 1), null) ); ShardSearchFailure failure1 = new ShardSearchFailure( - new QueryShardException(new Index("foo1", "_na_"), "foobar", null), - new SearchShardTarget("node_1", new ShardId("foo1", "_na_", 1), null) + new QueryShardException(new Index("foo1", IndexMetadata.INDEX_UUID_NA_VALUE), "foobar", null), + new SearchShardTarget("node_1", new ShardId("foo1", IndexMetadata.INDEX_UUID_NA_VALUE, 1), null) ); ShardSearchFailure failure2 = new ShardSearchFailure( - new QueryShardException(new Index("foo1", "_na_"), "foobar", null), - new SearchShardTarget("node_1", new ShardId("foo1", "_na_", 2), null) + new QueryShardException(new Index("foo1", IndexMetadata.INDEX_UUID_NA_VALUE), "foobar", null), + new SearchShardTarget("node_1", new ShardId("foo1", IndexMetadata.INDEX_UUID_NA_VALUE, 2), null) ); SearchPhaseExecutionException ex = new SearchPhaseExecutionException( "search", @@ -310,11 +311,11 @@ public void testDeduplicate() throws IOException { { ShardSearchFailure failure = new ShardSearchFailure( new ParsingException(1, 2, "foobar", null), - new SearchShardTarget("node_1", new ShardId("foo", "_na_", 1), null) + new SearchShardTarget("node_1", new ShardId("foo", IndexMetadata.INDEX_UUID_NA_VALUE, 1), null) ); ShardSearchFailure failure1 = new ShardSearchFailure( new ParsingException(1, 2, "foobar", null), - new SearchShardTarget("node_1", new ShardId("foo", "_na_", 2), null) + new SearchShardTarget("node_1", new ShardId("foo", IndexMetadata.INDEX_UUID_NA_VALUE, 2), null) ); SearchPhaseExecutionException ex = new SearchPhaseExecutionException( "search", @@ -351,15 +352,15 @@ public void testDeduplicate() throws IOException { { ShardSearchFailure failure = new ShardSearchFailure( new ParsingException(1, 2, "foobar", null), - new SearchShardTarget("node_1", new ShardId("foo", "_na_", 1), null) + new SearchShardTarget("node_1", new ShardId("foo", IndexMetadata.INDEX_UUID_NA_VALUE, 1), null) ); ShardSearchFailure failure1 = new ShardSearchFailure( - new QueryShardException(new Index("foo1", "_na_"), "foobar", null), - new SearchShardTarget("node_1", new ShardId("foo1", "_na_", 1), null) + new QueryShardException(new Index("foo1", IndexMetadata.INDEX_UUID_NA_VALUE), "foobar", null), + new SearchShardTarget("node_1", new ShardId("foo1", IndexMetadata.INDEX_UUID_NA_VALUE, 1), null) ); ShardSearchFailure failure2 = new ShardSearchFailure( - new QueryShardException(new Index("foo1", "_na_"), "foobar", null), - new SearchShardTarget("node_1", new ShardId("foo1", "_na_", 2), null) + new QueryShardException(new Index("foo1", IndexMetadata.INDEX_UUID_NA_VALUE), "foobar", null), + new SearchShardTarget("node_1", new ShardId("foo1", IndexMetadata.INDEX_UUID_NA_VALUE, 2), null) ); SearchPhaseExecutionException ex = new SearchPhaseExecutionException( "search", @@ -407,11 +408,11 @@ public void testDeduplicate() throws IOException { { ShardSearchFailure failure = new ShardSearchFailure( new ParsingException(1, 2, "foobar", null), - new SearchShardTarget("node_1", new ShardId("foo", "_na_", 1), null) + new SearchShardTarget("node_1", new ShardId("foo", IndexMetadata.INDEX_UUID_NA_VALUE, 1), null) ); ShardSearchFailure failure1 = new ShardSearchFailure( new ParsingException(1, 2, "foobar", null), - new SearchShardTarget("node_1", new ShardId("foo", "_na_", 2), null) + new SearchShardTarget("node_1", new ShardId("foo", IndexMetadata.INDEX_UUID_NA_VALUE, 2), null) ); NullPointerException nullPointerException = new NullPointerException(); SearchPhaseExecutionException ex = new SearchPhaseExecutionException( diff --git a/server/src/test/java/org/elasticsearch/ExceptionSerializationTests.java b/server/src/test/java/org/elasticsearch/ExceptionSerializationTests.java index 64c0b9a780cfe..e358a02b791bb 100644 --- a/server/src/test/java/org/elasticsearch/ExceptionSerializationTests.java +++ b/server/src/test/java/org/elasticsearch/ExceptionSerializationTests.java @@ -28,6 +28,7 @@ import org.elasticsearch.cluster.coordination.NoMasterBlockService; import org.elasticsearch.cluster.coordination.NodeHealthCheckFailureException; import org.elasticsearch.cluster.desirednodes.VersionConflictException; +import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.node.DiscoveryNodeUtils; import org.elasticsearch.cluster.routing.IllegalShardRoutingStateException; @@ -251,7 +252,7 @@ private T serialize(T exception, TransportVersion version) } public void testIllegalShardRoutingStateException() throws IOException { - final ShardRouting routing = newShardRouting("test", 0, "xyz", false, ShardRoutingState.STARTED); + final ShardRouting routing = newShardRouting("test", randomUUID(), 0, "xyz", false, ShardRoutingState.STARTED); final String routingAsString = routing.toString(); IllegalShardRoutingStateException serialize = serialize( new IllegalShardRoutingStateException(routing, "foo", new NullPointerException()) @@ -283,7 +284,7 @@ public void testParsingException() throws IOException { } public void testQueryShardException() throws IOException { - QueryShardException ex = serialize(new QueryShardException(new Index("foo", "_na_"), "fobar", null)); + QueryShardException ex = serialize(new QueryShardException(new Index("foo", IndexMetadata.INDEX_UUID_NA_VALUE), "fobar", null)); assertEquals(ex.getIndex().getName(), "foo"); assertEquals(ex.getMessage(), "fobar"); @@ -293,7 +294,7 @@ public void testQueryShardException() throws IOException { } public void testSearchException() throws IOException { - SearchShardTarget target = new SearchShardTarget("foo", new ShardId("bar", "_na_", 1), null); + SearchShardTarget target = new SearchShardTarget("foo", new ShardId("bar", IndexMetadata.INDEX_UUID_NA_VALUE, 1), null); SearchException ex = serialize(new SearchException(null, "hello world", new NullPointerException())); assertNull(ex.shard()); assertEquals(ex.getMessage(), "hello world"); @@ -322,7 +323,7 @@ public void testSnapshotException() throws IOException { } public void testRecoverFilesRecoveryException() throws IOException { - ShardId id = new ShardId("foo", "_na_", 1); + ShardId id = new ShardId("foo", IndexMetadata.INDEX_UUID_NA_VALUE, 1); ByteSizeValue bytes = ByteSizeValue.ofBytes(randomIntBetween(0, 10000)); RecoverFilesRecoveryException ex = serialize(new RecoverFilesRecoveryException(id, 10, bytes, null)); assertEquals(ex.getShardId(), id); @@ -397,7 +398,7 @@ public void testAliasesMissingException() throws IOException { } public void testIllegalIndexShardStateException() throws IOException { - ShardId id = new ShardId("foo", "_na_", 1); + ShardId id = new ShardId("foo", IndexMetadata.INDEX_UUID_NA_VALUE, 1); IndexShardState state = randomFrom(IndexShardState.values()); IllegalIndexShardStateException ex = serialize(new IllegalIndexShardStateException(id, state, "come back later buddy")); assertEquals(id, ex.getShardId()); @@ -463,7 +464,7 @@ public void testIndexTemplateMissingException() throws IOException { } public void testRecoveryEngineException() throws IOException { - ShardId id = new ShardId("foo", "_na_", 1); + ShardId id = new ShardId("foo", IndexMetadata.INDEX_UUID_NA_VALUE, 1); RecoveryEngineException ex = serialize(new RecoveryEngineException(id, 10, "total failure", new NullPointerException())); assertEquals(id, ex.getShardId()); assertEquals("Phase[10] total failure", ex.getMessage()); @@ -536,7 +537,11 @@ public void testUnknownException() throws IOException { } public void testWriteThrowable() throws IOException { - final QueryShardException queryShardException = new QueryShardException(new Index("foo", "_na_"), "foobar", null); + final QueryShardException queryShardException = new QueryShardException( + new Index("foo", IndexMetadata.INDEX_UUID_NA_VALUE), + "foobar", + null + ); final UnknownException unknownException = new UnknownException("this exception is unknown", queryShardException); final Exception[] causes = new Exception[] { @@ -907,7 +912,7 @@ public void testElasticsearchRemoteException() throws IOException { } public void testShardLockObtainFailedException() throws IOException { - ShardId shardId = new ShardId("foo", "_na_", 1); + ShardId shardId = new ShardId("foo", IndexMetadata.INDEX_UUID_NA_VALUE, 1); ShardLockObtainFailedException orig = new ShardLockObtainFailedException(shardId, "boom"); TransportVersion version = TransportVersionUtils.randomCompatibleVersion(random()); ShardLockObtainFailedException ex = serialize(orig, version); diff --git a/server/src/test/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplainActionTests.java b/server/src/test/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplainActionTests.java index 7108a8da0d691..d2e6c118dde8e 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplainActionTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplainActionTests.java @@ -29,6 +29,7 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.xcontent.ChunkedToXContent; import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.index.Index; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.gateway.TestGatewayAllocator; import org.elasticsearch.xcontent.ToXContent; @@ -55,7 +56,7 @@ public class ClusterAllocationExplainActionTests extends ESTestCase { public void testInitializingOrRelocatingShardExplanation() throws Exception { ShardRoutingState shardRoutingState = randomFrom(ShardRoutingState.INITIALIZING, ShardRoutingState.RELOCATING); - ClusterState clusterState = ClusterStateCreationUtils.state("idx", randomBoolean(), shardRoutingState); + ClusterState clusterState = ClusterStateCreationUtils.state(new Index("idx", randomUUID()), randomBoolean(), shardRoutingState); assertThat(clusterState.metadata().projects(), aMapWithSize(1)); final ProjectId projectId = clusterState.metadata().projects().keySet().iterator().next(); @@ -140,20 +141,25 @@ public ShardAllocationDecision decideShardAllocation(ShardRouting shard, Routing public void testFindAnyUnassignedShardToExplain() { // find unassigned primary - ClusterState clusterState = ClusterStateCreationUtils.state("idx", randomBoolean(), ShardRoutingState.UNASSIGNED); + Index index = new Index("idx", randomUUID()); + ClusterState clusterState = ClusterStateCreationUtils.state(index, randomBoolean(), ShardRoutingState.UNASSIGNED); ClusterAllocationExplainRequest request = new ClusterAllocationExplainRequest(TEST_REQUEST_TIMEOUT); Set projectIds = clusterState.metadata().projects().keySet(); ShardRouting shard = findShardToExplain(request, routingAllocation(clusterState), projectIds); - assertEquals(clusterState.getRoutingTable().index("idx").shard(0).primaryShard(), shard); + assertEquals(clusterState.getRoutingTable().index(index).shard(0).primaryShard(), shard); // find unassigned replica - clusterState = ClusterStateCreationUtils.state("idx", randomBoolean(), ShardRoutingState.STARTED, ShardRoutingState.UNASSIGNED); + clusterState = ClusterStateCreationUtils.state(index, randomBoolean(), ShardRoutingState.STARTED, ShardRoutingState.UNASSIGNED); request = new ClusterAllocationExplainRequest(TEST_REQUEST_TIMEOUT); shard = findShardToExplain(request, routingAllocation(clusterState), projectIds); - assertEquals(clusterState.getRoutingTable().index("idx").shard(0).replicaShards().get(0), shard); + assertEquals(clusterState.getRoutingTable().index(index).shard(0).replicaShards().get(0), shard); // prefer unassigned primary to replica - clusterState = ClusterStateCreationUtils.stateWithAssignedPrimariesAndReplicas(new String[] { "idx1", "idx2" }, 1, 1); + clusterState = ClusterStateCreationUtils.stateWithAssignedPrimariesAndReplicas( + new Index[] { new Index("idx1", randomUUID()), new Index("idx2", randomUUID()) }, + 1, + 1 + ); final String redIndex = randomBoolean() ? "idx1" : "idx2"; final RoutingTable.Builder routingTableBuilder = RoutingTable.builder(clusterState.routingTable()); for (final IndexRoutingTable indexRoutingTable : clusterState.routingTable()) { @@ -183,7 +189,7 @@ public void testFindAnyUnassignedShardToExplain() { // no unassigned shard to explain final ClusterState allStartedClusterState = ClusterStateCreationUtils.state( - "idx", + index, randomBoolean(), ShardRoutingState.STARTED, ShardRoutingState.STARTED @@ -204,28 +210,36 @@ public void testFindAnyUnassignedShardToExplain() { } public void testFindPrimaryShardToExplain() { - ClusterState clusterState = ClusterStateCreationUtils.state("idx", randomBoolean(), randomFrom(ShardRoutingState.values())); + Index index = new Index("idx", randomUUID()); + ClusterState clusterState = ClusterStateCreationUtils.state(index, randomBoolean(), randomFrom(ShardRoutingState.values())); Set projectIds = clusterState.metadata().projects().keySet(); - ClusterAllocationExplainRequest request = new ClusterAllocationExplainRequest(TEST_REQUEST_TIMEOUT, "idx", 0, true, null); + ClusterAllocationExplainRequest request = new ClusterAllocationExplainRequest(TEST_REQUEST_TIMEOUT, index.getName(), 0, true, null); ShardRouting shard = findShardToExplain(request, routingAllocation(clusterState), projectIds); - assertEquals(clusterState.getRoutingTable().index("idx").shard(0).primaryShard(), shard); + assertEquals(clusterState.getRoutingTable().index(index).shard(0).primaryShard(), shard); } public void testFindAnyReplicaToExplain() { + Index index = new Index("idx", randomUUID()); // prefer unassigned replicas to started replicas ClusterState clusterState = ClusterStateCreationUtils.state( - "idx", + index, randomBoolean(), ShardRoutingState.STARTED, ShardRoutingState.STARTED, ShardRoutingState.UNASSIGNED ); Set projectIds = clusterState.metadata().projects().keySet(); - ClusterAllocationExplainRequest request = new ClusterAllocationExplainRequest(TEST_REQUEST_TIMEOUT, "idx", 0, false, null); + ClusterAllocationExplainRequest request = new ClusterAllocationExplainRequest( + TEST_REQUEST_TIMEOUT, + index.getName(), + 0, + false, + null + ); ShardRouting shard = findShardToExplain(request, routingAllocation(clusterState), projectIds); assertEquals( clusterState.getRoutingTable() - .index("idx") + .index(index) .shard(0) .replicaShards() .stream() @@ -237,7 +251,7 @@ public void testFindAnyReplicaToExplain() { // prefer started replicas to initializing/relocating replicas clusterState = ClusterStateCreationUtils.state( - "idx", + index, randomBoolean(), ShardRoutingState.STARTED, randomFrom(ShardRoutingState.RELOCATING, ShardRoutingState.INITIALIZING), @@ -246,7 +260,7 @@ public void testFindAnyReplicaToExplain() { request = new ClusterAllocationExplainRequest(TEST_REQUEST_TIMEOUT, "idx", 0, false, null); shard = findShardToExplain(request, routingAllocation(clusterState), projectIds); assertEquals( - clusterState.getRoutingTable().index("idx").shard(0).replicaShards().stream().filter(ShardRouting::started).findFirst().get(), + clusterState.getRoutingTable().index(index).shard(0).replicaShards().stream().filter(ShardRouting::started).findFirst().get(), shard ); } @@ -258,15 +272,16 @@ public void testFindShardAssignedToNode() { if (primary == false) { replicaStates = new ShardRoutingState[] { ShardRoutingState.STARTED }; } - ClusterState clusterState = ClusterStateCreationUtils.state("idx", randomBoolean(), ShardRoutingState.STARTED, replicaStates); + Index index = new Index("idx", randomUUID()); + ClusterState clusterState = ClusterStateCreationUtils.state(index, randomBoolean(), ShardRoutingState.STARTED, replicaStates); assertThat(clusterState.metadata().projects(), aMapWithSize(1)); final ProjectId projectId = clusterState.metadata().projects().keySet().iterator().next(); ShardRouting shardToExplain = primary - ? clusterState.routingTable(projectId).index("idx").shard(0).primaryShard() - : clusterState.routingTable(projectId).index("idx").shard(0).replicaShards().get(0); + ? clusterState.routingTable(projectId).index(index).shard(0).primaryShard() + : clusterState.routingTable(projectId).index(index).shard(0).replicaShards().get(0); ClusterAllocationExplainRequest request = new ClusterAllocationExplainRequest( TEST_REQUEST_TIMEOUT, - "idx", + index.getName(), 0, primary, shardToExplain.currentNodeId() @@ -285,7 +300,7 @@ public void testFindShardAssignedToNode() { } final ClusterAllocationExplainRequest failingRequest = new ClusterAllocationExplainRequest( TEST_REQUEST_TIMEOUT, - "idx", + index.getName(), 0, primary, explainNode diff --git a/server/src/test/java/org/elasticsearch/action/admin/cluster/allocation/TransportGetDesiredBalanceActionTests.java b/server/src/test/java/org/elasticsearch/action/admin/cluster/allocation/TransportGetDesiredBalanceActionTests.java index e8c055199ec5b..756311fe579bd 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/cluster/allocation/TransportGetDesiredBalanceActionTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/cluster/allocation/TransportGetDesiredBalanceActionTests.java @@ -141,7 +141,7 @@ public void testGetDesiredBalance() throws Exception { RoutingTable.Builder routingTableBuilder = RoutingTable.builder(); for (int i = 0; i < randomInt(8); i++) { String indexName = randomAlphaOfLength(8); - Settings.Builder settings = indexSettings(IndexVersion.current(), 1, 0); + Settings.Builder settings = indexSettings(IndexVersion.current(), randomUUID(), 1, 0); if (randomBoolean()) { settings.put(DataTier.TIER_PREFERENCE_SETTING.getKey(), randomFrom("data_hot", "data_warm", "data_cold")); } diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/close/TransportVerifyShardBeforeCloseActionTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/close/TransportVerifyShardBeforeCloseActionTests.java index 0029a307d107e..03b25c267083f 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/close/TransportVerifyShardBeforeCloseActionTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/close/TransportVerifyShardBeforeCloseActionTests.java @@ -34,6 +34,7 @@ import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.index.IndexVersion; import org.elasticsearch.index.engine.Engine; @@ -258,8 +259,8 @@ public void testVerifyShardBeforeIndexClosingFailed() { } public void testUnavailableShardsMarkedAsStale() throws Exception { - final String index = "test"; - final ShardId shardId = new ShardId(index, "_na_", 0); + final Index index = new Index("test", randomUUID()); + final ShardId shardId = new ShardId(index, 0); final int nbReplicas = randomIntBetween(1, 10); final ShardRoutingState[] replicaStates = new ShardRoutingState[nbReplicas]; diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/get/GetIndexActionTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/get/GetIndexActionTests.java index 765f7a85359da..e626b9a58a06a 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/get/GetIndexActionTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/get/GetIndexActionTests.java @@ -49,7 +49,7 @@ public class GetIndexActionTests extends ESSingleNodeTestCase { private IndicesService indicesService; private ThreadPool threadPool; private SettingsFilter settingsFilter; - private final String indexName = "test_index"; + private final Index index = new Index("test_index", randomUUID()); private TestTransportGetIndexAction getIndexAction; @@ -83,7 +83,7 @@ public void tearDown() throws Exception { } public void testIncludeDefaults() { - GetIndexRequest defaultsRequest = new GetIndexRequest(TEST_REQUEST_TIMEOUT).indices(indexName).includeDefaults(true); + GetIndexRequest defaultsRequest = new GetIndexRequest(TEST_REQUEST_TIMEOUT).indices(index.getName()).includeDefaults(true); ActionTestUtils.execute( getIndexAction, null, @@ -91,14 +91,14 @@ public void testIncludeDefaults() { ActionTestUtils.assertNoFailureListener( defaultsResponse -> assertNotNull( "index.refresh_interval should be set as we are including defaults", - defaultsResponse.getSetting(indexName, "index.refresh_interval") + defaultsResponse.getSetting(index.getName(), "index.refresh_interval") ) ) ); } public void testDoNotIncludeDefaults() { - GetIndexRequest noDefaultsRequest = new GetIndexRequest(TEST_REQUEST_TIMEOUT).indices(indexName); + GetIndexRequest noDefaultsRequest = new GetIndexRequest(TEST_REQUEST_TIMEOUT).indices(index.getName()); ActionTestUtils.execute( getIndexAction, null, @@ -106,7 +106,7 @@ public void testDoNotIncludeDefaults() { ActionTestUtils.assertNoFailureListener( noDefaultsResponse -> assertNull( "index.refresh_interval should be null as it was never set", - noDefaultsResponse.getSetting(indexName, "index.refresh_interval") + noDefaultsResponse.getSetting(index.getName(), "index.refresh_interval") ) ) ); @@ -135,7 +135,7 @@ protected void localClusterStateOperation( ProjectState state, ActionListener listener ) throws Exception { - ProjectState stateWithIndex = ClusterStateCreationUtils.state(indexName, 1, 1).projectState(ProjectId.DEFAULT); + ProjectState stateWithIndex = ClusterStateCreationUtils.state(index, 1, 1).projectState(ProjectId.DEFAULT); super.localClusterStateOperation(task, request, stateWithIndex, listener); } } diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/segments/IndicesSegmentResponseTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/segments/IndicesSegmentResponseTests.java index e33f97e7cb580..0b6fc8a77292f 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/segments/IndicesSegmentResponseTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/segments/IndicesSegmentResponseTests.java @@ -30,7 +30,7 @@ public class IndicesSegmentResponseTests extends ESTestCase { public void testToXContentSerialiationWithSortedFields() throws Exception { - ShardRouting shardRouting = TestShardRouting.newShardRouting("foo", 0, "node_id", true, ShardRoutingState.STARTED); + ShardRouting shardRouting = TestShardRouting.newShardRouting("foo", randomUUID(), 0, "node_id", true, ShardRoutingState.STARTED); Segment segment = new Segment("my"); SortField sortField = new SortField("foo", SortField.Type.STRING); @@ -54,7 +54,7 @@ public void testChunking() { final int indices = randomIntBetween(1, 10); final List routings = new ArrayList<>(indices); for (int i = 0; i < indices; i++) { - routings.add(TestShardRouting.newShardRouting("index-" + i, 0, "node_id", true, ShardRoutingState.STARTED)); + routings.add(TestShardRouting.newShardRouting("index-" + i, randomUUID(), 0, "node_id", true, ShardRoutingState.STARTED)); } Segment segment = new Segment("my"); SortField sortField = new SortField("foo", SortField.Type.STRING); diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/settings/get/GetSettingsActionTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/settings/get/GetSettingsActionTests.java index b3a3fef6a09c9..590161dabcf64 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/settings/get/GetSettingsActionTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/settings/get/GetSettingsActionTests.java @@ -15,6 +15,7 @@ import org.elasticsearch.action.support.ActionTestUtils; import org.elasticsearch.action.support.replication.ClusterStateCreationUtils; import org.elasticsearch.cluster.ProjectState; +import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; import org.elasticsearch.cluster.metadata.ProjectMetadata; import org.elasticsearch.cluster.project.TestProjectResolvers; @@ -47,7 +48,7 @@ public class GetSettingsActionTests extends ESTestCase { private ClusterService clusterService; private ThreadPool threadPool; private SettingsFilter settingsFilter; - private final String indexName = "test_index"; + private final Index index = new Index("test_index", randomUUID()); private TestTransportGetSettingsAction getSettingsAction; @@ -72,7 +73,7 @@ protected void localClusterStateOperation( ProjectState state, ActionListener listener ) { - ProjectState stateWithIndex = ClusterStateCreationUtils.state(indexName, 1, 1).projectState(state.projectId()); + ProjectState stateWithIndex = ClusterStateCreationUtils.state(index, 1, 1).projectState(state.projectId()); super.localClusterStateOperation(task, request, stateWithIndex, listener); } } @@ -107,7 +108,7 @@ public void tearDown() throws Exception { } public void testIncludeDefaults() { - GetSettingsRequest noDefaultsRequest = new GetSettingsRequest(TEST_REQUEST_TIMEOUT).indices(indexName); + GetSettingsRequest noDefaultsRequest = new GetSettingsRequest(TEST_REQUEST_TIMEOUT).indices(index.getName()); ActionTestUtils.execute( getSettingsAction, null, @@ -115,12 +116,12 @@ public void testIncludeDefaults() { ActionTestUtils.assertNoFailureListener( noDefaultsResponse -> assertNull( "index.refresh_interval should be null as it was never set", - noDefaultsResponse.getSetting(indexName, "index.refresh_interval") + noDefaultsResponse.getSetting(index.getName(), "index.refresh_interval") ) ) ); - GetSettingsRequest defaultsRequest = new GetSettingsRequest(TEST_REQUEST_TIMEOUT).indices(indexName).includeDefaults(true); + GetSettingsRequest defaultsRequest = new GetSettingsRequest(TEST_REQUEST_TIMEOUT).indices(index.getName()).includeDefaults(true); ActionTestUtils.execute( getSettingsAction, @@ -129,7 +130,7 @@ public void testIncludeDefaults() { ActionTestUtils.assertNoFailureListener( defaultsResponse -> assertNotNull( "index.refresh_interval should be set as we are including defaults", - defaultsResponse.getSetting(indexName, "index.refresh_interval") + defaultsResponse.getSetting(index.getName(), "index.refresh_interval") ) ) ); @@ -137,21 +138,21 @@ public void testIncludeDefaults() { } public void testIncludeDefaultsWithFiltering() { - GetSettingsRequest defaultsRequest = new GetSettingsRequest(TEST_REQUEST_TIMEOUT).indices(indexName) + GetSettingsRequest defaultsRequest = new GetSettingsRequest(TEST_REQUEST_TIMEOUT).indices(index.getName()) .includeDefaults(true) .names("index.refresh_interval"); ActionTestUtils.execute(getSettingsAction, null, defaultsRequest, ActionTestUtils.assertNoFailureListener(defaultsResponse -> { assertNotNull( "index.refresh_interval should be set as we are including defaults", - defaultsResponse.getSetting(indexName, "index.refresh_interval") + defaultsResponse.getSetting(index.getName(), "index.refresh_interval") ); assertNull( "index.number_of_shards should be null as this query is filtered", - defaultsResponse.getSetting(indexName, "index.number_of_shards") + defaultsResponse.getSetting(index.getName(), "index.number_of_shards") ); assertNull( "index.warmer.enabled should be null as this query is filtered", - defaultsResponse.getSetting(indexName, "index.warmer.enabled") + defaultsResponse.getSetting(index.getName(), "index.warmer.enabled") ); })); } @@ -170,7 +171,12 @@ public String[] concreteIndexNames(ProjectMetadata project, IndicesRequest reque public Index[] concreteIndices(ProjectMetadata project, IndicesRequest request) { Index[] out = new Index[request.indices().length]; for (int x = 0; x < out.length; x++) { - out[x] = new Index(request.indices()[x], "_na_"); + String indexName = request.indices()[x]; + if (project.hasIndex(indexName)) { + out[x] = project.index(indexName).getIndex(); + } else { + out[x] = new Index(indexName, IndexMetadata.INDEX_UUID_NA_VALUE); + } } return out; } diff --git a/server/src/test/java/org/elasticsearch/action/fieldcaps/RequestDispatcherTests.java b/server/src/test/java/org/elasticsearch/action/fieldcaps/RequestDispatcherTests.java index ddbdd849e97bf..fdf11bfd83e20 100644 --- a/server/src/test/java/org/elasticsearch/action/fieldcaps/RequestDispatcherTests.java +++ b/server/src/test/java/org/elasticsearch/action/fieldcaps/RequestDispatcherTests.java @@ -122,7 +122,8 @@ public void testHappyCluster() throws Exception { ProjectMetadata.Builder metadata = ProjectMetadata.builder(projectId); for (String index : allIndices) { metadata.put( - IndexMetadata.builder(index).settings(indexSettings(IndexVersions.MINIMUM_COMPATIBLE, between(1, 10), between(0, 2))) + IndexMetadata.builder(index) + .settings(indexSettings(IndexVersions.MINIMUM_COMPATIBLE, randomUUID(), between(1, 10), between(0, 2))) ); } clusterState = newClusterState(Metadata.builder().put(metadata).build(), discoNodes.build()); @@ -194,7 +195,8 @@ public void testRetryThenOk() throws Exception { ProjectMetadata.Builder metadata = ProjectMetadata.builder(projectId); for (String index : allIndices) { metadata.put( - IndexMetadata.builder(index).settings(indexSettings(IndexVersions.MINIMUM_COMPATIBLE, between(1, 10), between(1, 3))) + IndexMetadata.builder(index) + .settings(indexSettings(IndexVersions.MINIMUM_COMPATIBLE, randomUUID(), between(1, 10), between(1, 3))) ); } clusterState = newClusterState(Metadata.builder().put(metadata).build(), discoNodes.build()); @@ -317,7 +319,8 @@ public void testRetryButFails() throws Exception { ProjectMetadata.Builder metadata = ProjectMetadata.builder(projectId); for (String index : allIndices) { metadata.put( - IndexMetadata.builder(index).settings(indexSettings(IndexVersions.MINIMUM_COMPATIBLE, between(1, 10), between(0, 3))) + IndexMetadata.builder(index) + .settings(indexSettings(IndexVersions.MINIMUM_COMPATIBLE, randomUUID(), between(1, 10), between(0, 3))) ); } clusterState = newClusterState(Metadata.builder().put(metadata).build(), discoNodes.build()); @@ -442,7 +445,8 @@ public void testSuccessWithAnyMatch() throws Exception { ProjectMetadata.Builder metadata = ProjectMetadata.builder(projectId); for (String index : allIndices) { metadata.put( - IndexMetadata.builder(index).settings(indexSettings(IndexVersions.MINIMUM_COMPATIBLE, between(2, 10), between(0, 2))) + IndexMetadata.builder(index) + .settings(indexSettings(IndexVersions.MINIMUM_COMPATIBLE, randomUUID(), between(2, 10), between(0, 2))) ); } clusterState = newClusterState(Metadata.builder().put(metadata).build(), discoNodes.build()); @@ -541,7 +545,8 @@ public void testStopAfterAllShardsUnmatched() throws Exception { ProjectMetadata.Builder metadata = ProjectMetadata.builder(projectId); for (String index : allIndices) { metadata.put( - IndexMetadata.builder(index).settings(indexSettings(IndexVersions.MINIMUM_COMPATIBLE, between(1, 10), between(0, 2))) + IndexMetadata.builder(index) + .settings(indexSettings(IndexVersions.MINIMUM_COMPATIBLE, randomUUID(), between(1, 10), between(0, 2))) ); } clusterState = newClusterState(Metadata.builder().put(metadata).build(), discoNodes.build()); @@ -635,7 +640,8 @@ public void testFailWithSameException() throws Exception { ProjectMetadata.Builder metadata = ProjectMetadata.builder(projectId); for (String index : allIndices) { metadata.put( - IndexMetadata.builder(index).settings(indexSettings(IndexVersions.MINIMUM_COMPATIBLE, between(1, 10), between(0, 3))) + IndexMetadata.builder(index) + .settings(indexSettings(IndexVersions.MINIMUM_COMPATIBLE, randomUUID(), between(1, 10), between(0, 3))) ); } clusterState = newClusterState(Metadata.builder().put(metadata).build(), discoNodes.build()); diff --git a/server/src/test/java/org/elasticsearch/action/resync/TransportResyncReplicationActionTests.java b/server/src/test/java/org/elasticsearch/action/resync/TransportResyncReplicationActionTests.java index a50d598743497..04606e1ea0f21 100644 --- a/server/src/test/java/org/elasticsearch/action/resync/TransportResyncReplicationActionTests.java +++ b/server/src/test/java/org/elasticsearch/action/resync/TransportResyncReplicationActionTests.java @@ -90,8 +90,8 @@ public static void afterClass() { public void testResyncDoesNotBlockOnPrimaryAction() throws Exception { ProjectId projectId = randomProjectIdOrDefault(); try (ClusterService clusterService = createClusterService(threadPool)) { - final String indexName = randomAlphaOfLength(5); - setState(clusterService, state(projectId, indexName, true, ShardRoutingState.STARTED)); + final Index index = new Index(randomAlphaOfLength(5), randomUUID()); + setState(clusterService, state(projectId, index, true, ShardRoutingState.STARTED)); setState( clusterService, @@ -99,7 +99,7 @@ public void testResyncDoesNotBlockOnPrimaryAction() throws Exception { .blocks( ClusterBlocks.builder() .addGlobalBlock(NoMasterBlockService.NO_MASTER_BLOCK_ALL) - .addIndexBlock(indexName, IndexMetadata.INDEX_WRITE_BLOCK) + .addIndexBlock(index.getName(), IndexMetadata.INDEX_WRITE_BLOCK) ) ); @@ -130,8 +130,7 @@ public void testResyncDoesNotBlockOnPrimaryAction() throws Exception { transportService.acceptIncomingRequests(); final ShardStateAction shardStateAction = new ShardStateAction(clusterService, transportService, null, null, threadPool); - final IndexMetadata indexMetadata = clusterService.state().metadata().getProject(projectId).index(indexName); - final Index index = indexMetadata.getIndex(); + final IndexMetadata indexMetadata = clusterService.state().metadata().getProject(projectId).index(index); final ShardId shardId = new ShardId(index, 0); final IndexShardRoutingTable shardRoutingTable = clusterService.state().routingTable(projectId).shardRoutingTable(shardId); final ShardRouting primaryShardRouting = clusterService.state() diff --git a/server/src/test/java/org/elasticsearch/action/search/TransportSearchActionTests.java b/server/src/test/java/org/elasticsearch/action/search/TransportSearchActionTests.java index 9f286efe28083..990ecc2880d88 100644 --- a/server/src/test/java/org/elasticsearch/action/search/TransportSearchActionTests.java +++ b/server/src/test/java/org/elasticsearch/action/search/TransportSearchActionTests.java @@ -287,7 +287,8 @@ public void testProcessRemoteShards() { ClusterSearchShardsGroup[] groups2 = new ClusterSearchShardsGroup[] { new ClusterSearchShardsGroup( new ShardId("xyz", "xyz_id", 0), - new ShardRouting[] { TestShardRouting.newShardRouting("xyz", 0, "node3", true, ShardRoutingState.STARTED) } + new ShardRouting[] { + TestShardRouting.newShardRouting("xyz", randomUUID(), 0, "node3", true, ShardRoutingState.STARTED) } ) }; Map aliasFilters2 = Map.of("xyz", AliasFilter.of(null, "some_alias_for_xyz")); searchShardsResponseMap.put( @@ -1655,7 +1656,7 @@ public void testShouldPreFilterSearchShardsWithReadOnly() { public void testLocalShardIteratorFromPointInTime() { final int numberOfShards = randomIntBetween(1, 5); final int numberOfReplicas = randomIntBetween(0, 2); - final String[] indices = { "test-1", "test-2" }; + final Index[] indices = { new Index("test-1", randomUUID()), new Index("test-2", randomUUID()) }; final ProjectId project = randomProjectIdOrDefault(); final ClusterState clusterState = ClusterStateCreationUtils.stateWithAssignedPrimariesAndReplicas( project, diff --git a/server/src/test/java/org/elasticsearch/action/support/broadcast/node/TransportBroadcastByNodeActionTests.java b/server/src/test/java/org/elasticsearch/action/support/broadcast/node/TransportBroadcastByNodeActionTests.java index 8e86044a33673..6ded90f13f43a 100644 --- a/server/src/test/java/org/elasticsearch/action/support/broadcast/node/TransportBroadcastByNodeActionTests.java +++ b/server/src/test/java/org/elasticsearch/action/support/broadcast/node/TransportBroadcastByNodeActionTests.java @@ -100,10 +100,10 @@ public class TransportBroadcastByNodeActionTests extends ESTestCase { - private static final String TEST_INDEX = "test-index"; private static final String TEST_CLUSTER = "test-cluster"; private static ThreadPool THREAD_POOL; + private final Index testIndex = new Index("test-index", randomUUID()); private ClusterService clusterService; private CapturingTransport transport; @@ -216,7 +216,7 @@ protected void shardOperation(Request request, ShardRouting shardRouting, Task t @Override protected ShardsIterator shards(ClusterState clusterState, Request request, String[] concreteIndices) { - return clusterState.routingTable().allShards(new String[] { TEST_INDEX }); + return clusterState.routingTable().allShards(new String[] { testIndex.getName() }); } @Override @@ -288,7 +288,7 @@ public void tearDown() throws Exception { void setClusterState(ClusterService clusterService) { int numberOfNodes = randomIntBetween(3, 5); DiscoveryNodes.Builder discoBuilder = DiscoveryNodes.builder(); - IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(new Index(TEST_INDEX, "_na_")); + IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(testIndex); int shardIndex = -1; int totalIndexShards = 0; @@ -298,9 +298,9 @@ void setClusterState(ClusterService clusterService) { int numberOfShards = randomIntBetween(1, 10); totalIndexShards += numberOfShards; for (int j = 0; j < numberOfShards; j++) { - final ShardId shardId = new ShardId(TEST_INDEX, "_na_", ++shardIndex); + final ShardId shardId = new ShardId(testIndex, ++shardIndex); ShardRouting shard = TestShardRouting.newShardRouting( - TEST_INDEX, + testIndex, shardId.getId(), node.getId(), true, @@ -315,10 +315,8 @@ void setClusterState(ClusterService clusterService) { discoBuilder.masterNodeId(newNode(numberOfNodes - 1).getId()); ClusterState.Builder stateBuilder = ClusterState.builder(new ClusterName(TEST_CLUSTER)); stateBuilder.nodes(discoBuilder); - final IndexMetadata.Builder indexMetadata = IndexMetadata.builder(TEST_INDEX) - .settings(Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, IndexVersion.current())) - .numberOfReplicas(0) - .numberOfShards(totalIndexShards); + final IndexMetadata.Builder indexMetadata = IndexMetadata.builder(testIndex.getName()) + .settings(indexSettings(IndexVersion.current(), testIndex.getUUID(), totalIndexShards, 0)); stateBuilder.metadata(Metadata.builder().put(indexMetadata)); stateBuilder.routingTable(RoutingTable.builder().add(indexRoutingTable.build()).build()); @@ -338,7 +336,7 @@ public static void destroyThreadPool() { } public void testGlobalBlock() { - Request request = new Request(TEST_INDEX); + Request request = new Request(testIndex.getName()); ClusterBlocks.Builder block = ClusterBlocks.builder() .addGlobalBlock(new ClusterBlock(1, "test-block", false, true, false, RestStatus.SERVICE_UNAVAILABLE, ClusterBlockLevel.ALL)); @@ -352,23 +350,23 @@ public void testGlobalBlock() { } public void testRequestBlock() { - Request request = new Request(TEST_INDEX); + Request request = new Request(testIndex.getName()); ClusterBlocks.Builder block = ClusterBlocks.builder() .addIndexBlock( - TEST_INDEX, + testIndex.getName(), new ClusterBlock(1, "test-block", false, true, false, RestStatus.SERVICE_UNAVAILABLE, ClusterBlockLevel.ALL) ); setState(clusterService, ClusterState.builder(clusterService.state()).blocks(block)); assertEquals( - "index [" + TEST_INDEX + "] blocked by: [SERVICE_UNAVAILABLE/1/test-block];", + "index [" + testIndex.getName() + "] blocked by: [SERVICE_UNAVAILABLE/1/test-block];", safeAwaitFailure(ClusterBlockException.class, Response.class, listener -> action.doExecute(null, request, listener)) .getMessage() ); } public void testOneRequestIsSentToEachNodeHoldingAShard() { - Request request = new Request(TEST_INDEX); + Request request = new Request(testIndex.getName()); PlainActionFuture listener = new PlainActionFuture<>(); action.doExecute(null, request, listener); @@ -376,7 +374,7 @@ public void testOneRequestIsSentToEachNodeHoldingAShard() { awaitForkedTasks(); Map> capturedRequests = transport.getCapturedRequestsByTargetNodeAndClear(); - ShardsIterator shardIt = clusterService.state().routingTable().allShards(new String[] { TEST_INDEX }); + ShardsIterator shardIt = clusterService.state().routingTable().allShards(new String[] { testIndex.getName() }); Set set = new HashSet<>(); for (ShardRouting shard : shardIt) { set.add(shard.currentNodeId()); @@ -395,7 +393,7 @@ public void testOneRequestIsSentToEachNodeHoldingAShard() { allOf( containsString('[' + action.transportNodeBroadcastAction + ']'), containsString('[' + entry.getKey() + ']'), - containsString("[testrequest[" + TEST_INDEX + "]]") + containsString("[testrequest[" + testIndex.getName() + "]]") ) ); } @@ -405,7 +403,7 @@ public void testOneRequestIsSentToEachNodeHoldingAShard() { } public void testNoShardOperationsExecutedIfTaskCancelled() throws Exception { - ShardsIterator shardIt = clusterService.state().routingTable().allShards(new String[] { TEST_INDEX }); + ShardsIterator shardIt = clusterService.state().routingTable().allShards(new String[] { testIndex.getName() }); Set shards = new HashSet<>(); String nodeId = shardIt.iterator().next().currentNodeId(); for (ShardRouting shard : shardIt) { @@ -432,7 +430,7 @@ public void testNoShardOperationsExecutedIfTaskCancelled() throws Exception { // that node will not be in the local cluster state on any node that has detected the master as failing // in this case, such a shard should be treated as unassigned public void testRequestsAreNotSentToFailedMaster() { - Request request = new Request(TEST_INDEX); + Request request = new Request(testIndex.getName()); PlainActionFuture listener = new PlainActionFuture<>(); DiscoveryNode masterNode = clusterService.state().nodes().getMasterNode(); @@ -446,7 +444,7 @@ public void testRequestsAreNotSentToFailedMaster() { Map> capturedRequests = transport.getCapturedRequestsByTargetNodeAndClear(); // the master should not be in the list of nodes that requests were sent to - ShardsIterator shardIt = clusterService.state().routingTable().allShards(new String[] { TEST_INDEX }); + ShardsIterator shardIt = clusterService.state().routingTable().allShards(new String[] { testIndex.getName() }); Set set = new HashSet<>(); for (ShardRouting shard : shardIt) { if (shard.currentNodeId().equals(masterNode.getId()) == false) { @@ -466,7 +464,7 @@ public void testRequestsAreNotSentToFailedMaster() { } public void testOperationExecution() throws Exception { - ShardsIterator shardIt = clusterService.state().routingTable().allShards(new String[] { TEST_INDEX }); + ShardsIterator shardIt = clusterService.state().routingTable().allShards(new String[] { testIndex.getName() }); Set shards = new HashSet<>(); String nodeId = shardIt.iterator().next().currentNodeId(); for (ShardRouting shard : shardIt) { @@ -515,7 +513,7 @@ public void testOperationExecution() throws Exception { } public void testResultAggregation() throws ExecutionException, InterruptedException { - Request request = new Request(TEST_INDEX); + Request request = new Request(testIndex.getName()); PlainActionFuture listener = new PlainActionFuture<>(); // simulate removing the master @@ -534,7 +532,7 @@ public void testResultAggregation() throws ExecutionException, InterruptedExcept awaitForkedTasks(); Map> capturedRequests = transport.getCapturedRequestsByTargetNodeAndClear(); - ShardsIterator shardIt = clusterService.state().getRoutingTable().allShards(new String[] { TEST_INDEX }); + ShardsIterator shardIt = clusterService.state().getRoutingTable().allShards(new String[] { testIndex.getName() }); Map> map = new HashMap<>(); for (ShardRouting shard : shardIt) { if (map.containsKey(shard.currentNodeId()) == false) { @@ -588,7 +586,7 @@ public void testResultAggregation() throws ExecutionException, InterruptedExcept public void testResponsesReleasedOnCancellation() { final CancellableTask cancellableTask = new CancellableTask(randomLong(), "transport", "action", "", null, emptyMap()); final PlainActionFuture listener = new PlainActionFuture<>(); - action.execute(cancellableTask, new Request(TEST_INDEX), listener); + action.execute(cancellableTask, new Request(testIndex.getName()), listener); awaitForkedTasks(); final List capturedRequests = new ArrayList<>( @@ -674,7 +672,7 @@ protected void shardOperation(Request request, ShardRouting shardRouting, Task t action.new BroadcastByNodeTransportRequestHandler().messageReceived( action.new NodeRequest( new Request(), IntStream.range(0, 3) - .mapToObj(shardId -> TestShardRouting.newShardRouting(TEST_INDEX, shardId, "node-id", true, ShardRoutingState.STARTED)) + .mapToObj(shardId -> TestShardRouting.newShardRouting(testIndex, shardId, "node-id", true, ShardRoutingState.STARTED)) .toList(), "node-id" ), new TestTransportChannel(nodeResponseFuture), @@ -708,7 +706,7 @@ protected void shardOperation(Request request, ShardRouting shardRouting, Task t action.new BroadcastByNodeTransportRequestHandler().messageReceived( action.new NodeRequest( new Request(), IntStream.range(0, 3) - .mapToObj(shardId -> TestShardRouting.newShardRouting(TEST_INDEX, shardId, "node-id", true, ShardRoutingState.STARTED)) + .mapToObj(shardId -> TestShardRouting.newShardRouting(testIndex, shardId, "node-id", true, ShardRoutingState.STARTED)) .toList(), "node-id" ), new TestTransportChannel(nodeResponseFuture), diff --git a/server/src/test/java/org/elasticsearch/action/support/broadcast/unpromotable/TransportBroadcastUnpromotableActionTests.java b/server/src/test/java/org/elasticsearch/action/support/broadcast/unpromotable/TransportBroadcastUnpromotableActionTests.java index 29f1be8e49e5a..7f8bda3014bb7 100644 --- a/server/src/test/java/org/elasticsearch/action/support/broadcast/unpromotable/TransportBroadcastUnpromotableActionTests.java +++ b/server/src/test/java/org/elasticsearch/action/support/broadcast/unpromotable/TransportBroadcastUnpromotableActionTests.java @@ -25,6 +25,7 @@ import org.elasticsearch.common.util.concurrent.EsExecutors; import org.elasticsearch.core.IOUtils; import org.elasticsearch.core.Tuple; +import org.elasticsearch.index.Index; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.tasks.Task; import org.elasticsearch.test.ESTestCase; @@ -239,7 +240,7 @@ private int countRequestsForIndex(ClusterState state, String index) { } public void testNotStartedPrimary() { - final String index = "test"; + final Index index = new Index("test", randomUUID()); final int numPromotableReplicas = randomInt(2); final int numSearchReplicas = randomInt(2); final ClusterState state = state( @@ -250,27 +251,27 @@ public void testNotStartedPrimary() { ); setState(clusterService, state); logger.debug("--> using initial state:\n{}", clusterService.state()); - assertThat(countRequestsForIndex(state, index), is(equalTo(0))); + assertThat(countRequestsForIndex(state, index.getName()), is(equalTo(0))); } public void testMixOfStartedPromotableAndSearchReplicas() { - final String index = "test"; + final Index index = new Index("test", randomUUID()); final int numShards = 1 + randomInt(3); final int numPromotableReplicas = randomInt(2); final int numSearchReplicas = randomInt(2); ClusterState state = stateWithAssignedPrimariesAndReplicas( - new String[] { index }, + new Index[] { index }, numShards, getReplicaRoles(numPromotableReplicas, numSearchReplicas) ); setState(clusterService, state); logger.debug("--> using initial state:\n{}", clusterService.state()); - assertThat(countRequestsForIndex(state, index), is(equalTo(numShards * numSearchReplicas))); + assertThat(countRequestsForIndex(state, index.getName()), is(equalTo(numShards * numSearchReplicas))); } public void testSearchReplicasWithRandomStates() { - final String index = "test"; + final Index index = new Index("test", randomUUID()); final int numPromotableReplicas = randomInt(2); final int numSearchReplicas = randomInt(6); @@ -292,20 +293,20 @@ public void testSearchReplicasWithRandomStates() { setState(clusterService, state); logger.debug("--> using initial state:\n{}", clusterService.state()); - assertThat(countRequestsForIndex(state, index), is(equalTo(numReachableUnpromotables))); + assertThat(countRequestsForIndex(state, index.getName()), is(equalTo(numReachableUnpromotables))); } public void testInvalidNodes() throws Exception { - final String index = "test"; + final Index index = new Index("test", randomUUID()); ClusterState state = stateWithAssignedPrimariesAndReplicas( - new String[] { index }, + new Index[] { index }, randomIntBetween(1, 3), getReplicaRoles(randomInt(2), randomIntBetween(1, 2)) ); setState(clusterService, state); logger.debug("--> using initial state:\n{}", clusterService.state()); - ShardId shardId = state.routingTable().activePrimaryShardsGrouped(new String[] { index }, true).get(0).shardId(); + ShardId shardId = state.routingTable().activePrimaryShardsGrouped(new String[] { index.getName() }, true).get(0).shardId(); IndexShardRoutingTable routingTable = state.routingTable().shardRoutingTable(shardId); IndexShardRoutingTable.Builder wrongRoutingTableBuilder = new IndexShardRoutingTable.Builder(shardId); for (int i = 0; i < routingTable.size(); i++) { diff --git a/server/src/test/java/org/elasticsearch/action/support/replication/BroadcastReplicationTests.java b/server/src/test/java/org/elasticsearch/action/support/replication/BroadcastReplicationTests.java index c1567d72d059a..d123e80f5aaee 100644 --- a/server/src/test/java/org/elasticsearch/action/support/replication/BroadcastReplicationTests.java +++ b/server/src/test/java/org/elasticsearch/action/support/replication/BroadcastReplicationTests.java @@ -39,6 +39,7 @@ import org.elasticsearch.common.util.concurrent.EsExecutors; import org.elasticsearch.core.IOUtils; import org.elasticsearch.core.Tuple; +import org.elasticsearch.index.Index; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.indices.TestIndexNameExpressionResolver; import org.elasticsearch.indices.breaker.CircuitBreakerService; @@ -140,7 +141,7 @@ public static void afterClass() { } public void testNotStartedPrimary() throws InterruptedException, ExecutionException { - final String index = "test"; + final Index index = new Index("test", randomUUID()); setState( clusterService, setUpState( @@ -155,7 +156,7 @@ public void testNotStartedPrimary() throws InterruptedException, ExecutionExcept ); logger.debug("--> using initial state:\n{}", clusterService.state()); PlainActionFuture response = new PlainActionFuture<>(); - ActionTestUtils.execute(broadcastReplicationAction, null, new DummyBroadcastRequest(index), response); + ActionTestUtils.execute(broadcastReplicationAction, null, new DummyBroadcastRequest(index.getName()), response); for (Tuple> shardRequests : broadcastReplicationAction.capturedShardRequests) { if (randomBoolean()) { shardRequests.v2().onFailure(new NoShardAvailableActionException(shardRequests.v1())); @@ -170,11 +171,11 @@ public void testNotStartedPrimary() throws InterruptedException, ExecutionExcept } public void testStartedPrimary() throws InterruptedException, ExecutionException { - final String index = "test"; + final Index index = new Index("test", randomUUID()); setState(clusterService, setUpState(state(index, randomBoolean(), ShardRoutingState.STARTED), index)); logger.debug("--> using initial state:\n{}", clusterService.state()); PlainActionFuture response = new PlainActionFuture<>(); - ActionTestUtils.execute(broadcastReplicationAction, null, new DummyBroadcastRequest(index), response); + ActionTestUtils.execute(broadcastReplicationAction, null, new DummyBroadcastRequest(index.getName()), response); for (Tuple> shardRequests : broadcastReplicationAction.capturedShardRequests) { ReplicationResponse replicationResponse = new ReplicationResponse(); replicationResponse.setShardInfo(ReplicationResponse.ShardInfo.allSuccessful(1)); @@ -185,12 +186,12 @@ public void testStartedPrimary() throws InterruptedException, ExecutionException } public void testResultCombine() throws InterruptedException, ExecutionException, IOException { - final String index = "test"; + final Index index = new Index("test", randomUUID()); int numShards = 1 + randomInt(3); setState(clusterService, setUpState(stateWithAssignedPrimariesAndOneReplica(index, numShards), index)); logger.debug("--> using initial state:\n{}", clusterService.state()); PlainActionFuture response = new PlainActionFuture<>(); - ActionTestUtils.execute(broadcastReplicationAction, null, new DummyBroadcastRequest().indices(index), response); + ActionTestUtils.execute(broadcastReplicationAction, null, new DummyBroadcastRequest().indices(index.getName()), response); int succeeded = 0; int failed = 0; for (Tuple> shardRequests : broadcastReplicationAction.capturedShardRequests) { @@ -234,8 +235,8 @@ public void testNoShards() throws InterruptedException, ExecutionException, IOEx } public void testShardsList() throws InterruptedException, ExecutionException { - final String index = "test"; - final ShardId shardId = new ShardId(index, "_na_", 0); + final Index index = new Index("test", randomUUID()); + final ShardId shardId = new ShardId(index, 0); ClusterState clusterState = setUpState( state( index, @@ -345,7 +346,7 @@ private void assertBroadcastResponse(int total, int successful, int failed, Base /** * Copy index and routing table from default project to the target project. */ - private ClusterState setUpState(ClusterState state, String index) { + private ClusterState setUpState(ClusterState state, Index index) { return ClusterState.builder(state) .putProjectMetadata( ProjectMetadata.builder(projectId).put(state.metadata().getProject(Metadata.DEFAULT_PROJECT_ID).index(index), false).build() diff --git a/server/src/test/java/org/elasticsearch/action/support/replication/ReplicationOperationTests.java b/server/src/test/java/org/elasticsearch/action/support/replication/ReplicationOperationTests.java index a7abceddac58c..185afbd7b0863 100644 --- a/server/src/test/java/org/elasticsearch/action/support/replication/ReplicationOperationTests.java +++ b/server/src/test/java/org/elasticsearch/action/support/replication/ReplicationOperationTests.java @@ -33,6 +33,7 @@ import org.elasticsearch.common.util.set.Sets; import org.elasticsearch.core.TimeValue; import org.elasticsearch.core.Tuple; +import org.elasticsearch.index.Index; import org.elasticsearch.index.shard.IndexShardNotStartedException; import org.elasticsearch.index.shard.IndexShardState; import org.elasticsearch.index.shard.ReplicationGroup; @@ -87,8 +88,8 @@ public void tearDown() throws Exception { } public void testReplication() throws Exception { - final String index = "test"; - final ShardId shardId = new ShardId(index, "_na_", 0); + final Index index = new Index("test", randomUUID()); + final ShardId shardId = new ShardId(index, 0); ClusterState initialState = stateWithActivePrimary(index, true, randomInt(5)); IndexMetadata indexMetadata = initialState.getMetadata().getProject().index(index); @@ -161,8 +162,8 @@ public void testReplication() throws Exception { } public void testRetryTransientReplicationFailure() throws Exception { - final String index = "test"; - final ShardId shardId = new ShardId(index, "_na_", 0); + final Index index = new Index("test", randomUUID()); + final ShardId shardId = new ShardId(index, 0); ClusterState initialState = stateWithActivePrimary(index, true, randomInt(5)); IndexMetadata indexMetadata = initialState.getMetadata().getProject().index(index); @@ -273,8 +274,8 @@ private void addTrackingInfo( } public void testNoLongerPrimary() throws Exception { - final String index = "test"; - final ShardId shardId = new ShardId(index, "_na_", 0); + final Index index = new Index("test", randomUUID()); + final ShardId shardId = new ShardId(index, 0); ClusterState initialState = stateWithActivePrimary(index, true, 1 + randomInt(2), randomInt(2)); IndexMetadata indexMetadata = initialState.getMetadata().getProject().index(index); @@ -381,8 +382,8 @@ public void failShard(String message, Exception exception) { } public void testAddedReplicaAfterPrimaryOperation() throws Exception { - final String index = "test"; - final ShardId shardId = new ShardId(index, "_na_", 0); + final Index index = new Index("test", randomUUID()); + final ShardId shardId = new ShardId(index, 0); final ClusterState initialState = stateWithActivePrimary(index, true, 0); Set inSyncAllocationIds = initialState.metadata().getProject().index(index).inSyncAllocationIds(0); IndexShardRoutingTable shardRoutingTable = initialState.getRoutingTable().shardRoutingTable(shardId); @@ -435,8 +436,8 @@ public void perform(Request request, ActionListener listener) { } public void testWaitForActiveShards() throws Exception { - final String index = "test"; - final ShardId shardId = new ShardId(index, "_na_", 0); + final Index index = new Index("test", randomUUID()); + final ShardId shardId = new ShardId(index, 0); final int assignedReplicas = randomInt(2); final int unassignedReplicas = randomInt(2); final int totalShards = 1 + assignedReplicas + unassignedReplicas; @@ -529,8 +530,8 @@ public void testWaitForActiveShards() throws Exception { } public void testPrimaryFailureHandlingReplicaResponse() throws Exception { - final String index = "test"; - final ShardId shardId = new ShardId(index, "_na_", 0); + final Index index = new Index("test", randomUUID()); + final ShardId shardId = new ShardId(index, 0); final Request request = new Request(shardId); diff --git a/server/src/test/java/org/elasticsearch/action/support/replication/TransportReplicationActionTests.java b/server/src/test/java/org/elasticsearch/action/support/replication/TransportReplicationActionTests.java index abacd386ea905..ba7b04a9bdfa9 100644 --- a/server/src/test/java/org/elasticsearch/action/support/replication/TransportReplicationActionTests.java +++ b/server/src/test/java/org/elasticsearch/action/support/replication/TransportReplicationActionTests.java @@ -254,9 +254,10 @@ public ClusterBlockLevel indexBlockLevel() { } }; - setState(clusterService, ClusterStateCreationUtils.stateWithActivePrimary("index", true, 0)); + Index index = new Index("index", randomUUID()); + setState(clusterService, ClusterStateCreationUtils.stateWithActivePrimary(index, true, 0)); - ShardId shardId = new ShardId(clusterService.state().metadata().getProject().index("index").getIndex(), 0); + ShardId shardId = new ShardId(index, 0); { setStateWithBlock(clusterService, nonRetryableBlock, globalBlock); @@ -361,8 +362,8 @@ public ClusterBlockLevel indexBlockLevel() { } }; - final String index = "index"; - final ShardId shardId = new ShardId(index, "_na_", 0); + final Index index = new Index("index", randomUUID()); + final ShardId shardId = new ShardId(index, 0); setState(clusterService, stateWithActivePrimary(index, true, randomInt(5))); final ClusterBlocks.Builder block = ClusterBlocks.builder(); @@ -380,7 +381,7 @@ public ClusterBlockLevel indexBlockLevel() { ); } else { block.addIndexBlock( - index, + index.getName(), new ClusterBlock( randomIntBetween(1, 16), "test index block", @@ -420,8 +421,8 @@ public void assertIndexShardUninitialized() { } public void testNotStartedPrimary() { - final String index = "test"; - final ShardId shardId = new ShardId(index, "_na_", 0); + final Index index = new Index("test", randomUUID()); + final ShardId shardId = new ShardId(index, 0); // no replicas in oder to skip the replication part setState(clusterService, state(index, true, randomBoolean() ? ShardRoutingState.INITIALIZING : ShardRoutingState.UNASSIGNED)); ReplicationTask task = maybeTask(); @@ -458,8 +459,8 @@ public void testNotStartedPrimary() { } public void testShardNotInPrimaryMode() { - final String index = "test"; - final ShardId shardId = new ShardId(index, "_na_", 0); + final Index index = new Index("test", randomUUID()); + final ShardId shardId = new ShardId(index, 0); final ClusterState state = state(index, true, ShardRoutingState.RELOCATING); setState(clusterService, state); final ReplicationTask task = maybeTask(); @@ -508,8 +509,8 @@ protected void shardOperationOnPrimary( * stale index routing table on relocation target. */ public void testNoRerouteOnStaleClusterState() { - final String index = "test"; - final ShardId shardId = new ShardId(index, "_na_", 0); + final Index index = new Index("test", randomUUID()); + final ShardId shardId = new ShardId(index, 0); ClusterState state = state(index, true, ShardRoutingState.RELOCATING); String relocationTargetNode = state.getRoutingTable().shardRoutingTable(shardId).primaryShard().relocatingNodeId(); state = ClusterState.builder(state).nodes(DiscoveryNodes.builder(state.nodes()).localNodeId(relocationTargetNode)).build(); @@ -554,7 +555,7 @@ public void testNoRerouteOnStaleClusterState() { } public void testUnknownIndexOrShardOnReroute() { - final String index = "test"; + final Index index = new Index("test", randomUUID()); // no replicas in oder to skip the replication part setState(clusterService, state(index, true, randomBoolean() ? ShardRoutingState.INITIALIZING : ShardRoutingState.UNASSIGNED)); logger.debug("--> using initial state:\n{}", clusterService.state()); @@ -582,7 +583,7 @@ public void testUnknownIndexOrShardOnReroute() { assertPhase(task, "failed"); assertTrue(request.isRetrySet.get()); - request = new Request(new ShardId(index, "_na_", 10)).timeout(TimeValue.timeValueMillis(1)); + request = new Request(new ShardId(index, 10)).timeout(TimeValue.timeValueMillis(1)); listener = new PlainActionFuture<>(); reroutePhase = action.new ReroutePhase(null, request, listener); reroutePhase.run(); @@ -591,23 +592,23 @@ public void testUnknownIndexOrShardOnReroute() { } public void testClosedIndexOnReroute() { - final String index = "test"; - // no replicas in oder to skip the replication part + String indexName = "test"; + // no replicas in order to skip the replication part ClusterStateChanges clusterStateChanges = new ClusterStateChanges(xContentRegistry(), threadPool); setState( clusterService, clusterStateChanges.closeIndices( clusterStateChanges.createIndex( clusterService.state(), - new CreateIndexRequest(index).waitForActiveShards(ActiveShardCount.NONE) + new CreateIndexRequest(indexName).waitForActiveShards(ActiveShardCount.NONE) ), - new CloseIndexRequest(index) + new CloseIndexRequest(indexName) ) ); - assertThat(clusterService.state().metadata().getProject().indices().get(index).getState(), equalTo(IndexMetadata.State.CLOSE)); + IndexMetadata indexMetadata = clusterService.state().metadata().getProject().indices().get(indexName); + assertThat(indexMetadata.getState(), equalTo(IndexMetadata.State.CLOSE)); logger.debug("--> using initial state:\n{}", clusterService.state()); - Request request = new Request(new ShardId(clusterService.state().metadata().getProject().indices().get(index).getIndex(), 0)) - .timeout(TimeValue.timeValueMillis(1)); + Request request = new Request(new ShardId(indexMetadata.getIndex(), 0)).timeout(TimeValue.timeValueMillis(1)); PlainActionFuture listener = new PlainActionFuture<>(); ReplicationTask task = maybeTask(); @@ -628,8 +629,8 @@ public void testClosedIndexOnReroute() { } public void testStalePrimaryShardOnReroute() { - final String index = "test"; - final ShardId shardId = new ShardId(index, "_na_", 0); + final Index index = new Index("test", randomUUID()); + final ShardId shardId = new ShardId(index, 0); // no replicas in order to skip the replication part setState(clusterService, stateWithActivePrimary(index, true, randomInt(3))); logger.debug("--> using initial state:\n{}", clusterService.state()); @@ -683,8 +684,8 @@ private Exception randomRetryPrimaryException(ShardId shardId) { } public void testRoutePhaseExecutesRequest() { - final String index = "test"; - final ShardId shardId = new ShardId(index, "_na_", 0); + final Index index = new Index("test", randomUUID()); + final ShardId shardId = new ShardId(index, 0); ReplicationTask task = maybeTask(); setState(clusterService, stateWithActivePrimary(index, randomBoolean(), 3)); @@ -715,8 +716,8 @@ public void testRoutePhaseExecutesRequest() { } public void testPrimaryPhaseExecutesOrDelegatesRequestToRelocationTarget() throws Exception { - final String index = "test"; - final ShardId shardId = new ShardId(index, "_na_", 0); + final Index index = new Index("test", randomUUID()); + final ShardId shardId = new ShardId(index, 0); ClusterState state = stateWithActivePrimary(index, true, randomInt(5)); setState(clusterService, state); Request request = new Request(shardId).timeout(TimeValue.timeValueMillis(1)); @@ -780,8 +781,8 @@ protected void shardOperationOnPrimary( } public void testPrimaryPhaseExecutesDelegatedRequestOnRelocationTarget() { - final String index = "test"; - final ShardId shardId = new ShardId(index, "_na_", 0); + final Index index = new Index("test", randomUUID()); + final ShardId shardId = new ShardId(index, 0); ClusterState state = state(index, true, ShardRoutingState.RELOCATING); final ShardRouting primaryShard = state.getRoutingTable().shardRoutingTable(shardId).primaryShard(); final long primaryTerm = state.metadata().getProject().index(index).primaryTerm(shardId.id()); @@ -839,8 +840,8 @@ public void testPrimaryReference() { } public void testReplicaProxy() throws InterruptedException, ExecutionException { - final String index = "test"; - final ShardId shardId = new ShardId(index, "_na_", 0); + final Index index = new Index("test", randomUUID()); + final ShardId shardId = new ShardId(index, 0); ClusterState state = stateWithActivePrimary(index, true, 1 + randomInt(3), randomInt(2)); logger.info("using state: {}", state); setState(clusterService, state); @@ -912,8 +913,8 @@ public void testReplicaProxy() throws InterruptedException, ExecutionException { } public void testSeqNoIsSetOnPrimary() { - final String index = "test"; - final ShardId shardId = new ShardId(index, "_na_", 0); + final Index index = new Index("test", randomUUID()); + final ShardId shardId = new ShardId(index, 0); // we use one replica to check the primary term was set on the operation and sent to the replica setState( clusterService, @@ -987,8 +988,8 @@ private boolean shouldForceAcquirePermit(TransportReplicationAction.PrimaryActio } public void testCounterOnPrimary() throws Exception { - final String index = "test"; - final ShardId shardId = new ShardId(index, "_na_", 0); + final Index index = new Index("test", randomUUID()); + final ShardId shardId = new ShardId(index, 0); // no replica, we only want to test on primary final ClusterState state = state(index, true, ShardRoutingState.STARTED); setState(clusterService, state); @@ -1043,8 +1044,9 @@ protected void shardOperationOnPrimary( } public void testReplicasCounter() { - final ShardId shardId = new ShardId("test", "_na_", 0); - final ClusterState state = state(shardId.getIndexName(), true, ShardRoutingState.STARTED, ShardRoutingState.STARTED); + final Index index = new Index("test", randomUUID()); + final ShardId shardId = new ShardId(index, 0); + final ClusterState state = state(index, true, ShardRoutingState.STARTED, ShardRoutingState.STARTED); setState(clusterService, state); final ShardRouting replicaRouting = state.getRoutingTable().shardRoutingTable(shardId).replicaShards().get(0); boolean throwException = randomBoolean(); @@ -1096,14 +1098,14 @@ protected void shardOperationOnReplica(Request shardRequest, IndexShard replica, * when the request is using the default value for waitForActiveShards. */ public void testDefaultWaitForActiveShardsUsesIndexSetting() { - final String indexName = "test"; - final ShardId shardId = new ShardId(indexName, "_na_", 0); + final Index index = new Index("test", randomUUID()); + final ShardId shardId = new ShardId(index, 0); // test wait_for_active_shards index setting used when the default is set on the request int numReplicas = randomIntBetween(0, 5); int idxSettingWaitForActiveShards = randomIntBetween(0, numReplicas + 1); - ClusterState state = stateWithActivePrimary(indexName, randomBoolean(), numReplicas); - IndexMetadata indexMetadata = state.metadata().getProject().index(indexName); + ClusterState state = stateWithActivePrimary(index, randomBoolean(), numReplicas); + IndexMetadata indexMetadata = state.metadata().getProject().index(index); Settings indexSettings = Settings.builder() .put(indexMetadata.getSettings()) .put(SETTING_WAIT_FOR_ACTIVE_SHARDS.getKey(), Integer.toString(idxSettingWaitForActiveShards)) @@ -1113,20 +1115,20 @@ public void testDefaultWaitForActiveShardsUsesIndexSetting() { state = ClusterState.builder(state).metadata(metadataBuilder).build(); setState(clusterService, state); Request request = new Request(shardId).waitForActiveShards(ActiveShardCount.DEFAULT); // set to default so index settings are used - action.resolveRequest(state.metadata().getProject().index(indexName), request); + action.resolveRequest(state.metadata().getProject().index(index), request); assertEquals(ActiveShardCount.from(idxSettingWaitForActiveShards), request.waitForActiveShards()); // test wait_for_active_shards when default not set on the request (request value should be honored over index setting) int requestWaitForActiveShards = randomIntBetween(0, numReplicas + 1); request = new Request(shardId).waitForActiveShards(ActiveShardCount.from(requestWaitForActiveShards)); - action.resolveRequest(state.metadata().getProject().index(indexName), request); + action.resolveRequest(state.metadata().getProject().index(index), request); assertEquals(ActiveShardCount.from(requestWaitForActiveShards), request.waitForActiveShards()); } /** test that a primary request is rejected if it arrives at a shard with a wrong allocation id or term */ public void testPrimaryActionRejectsWrongAidOrWrongTerm() throws Exception { - final String index = "test"; - final ShardId shardId = new ShardId(index, "_na_", 0); + final Index index = new Index("test", randomUUID()); + final ShardId shardId = new ShardId(index, 0); setState(clusterService, state(index, true, ShardRoutingState.STARTED)); final ShardRouting primary = clusterService.state().routingTable().shardRoutingTable(shardId).primaryShard(); final long primaryTerm = clusterService.state().metadata().getProject().index(shardId.getIndexName()).primaryTerm(shardId.id()); @@ -1174,8 +1176,8 @@ public void testPrimaryActionRejectsWrongAidOrWrongTerm() throws Exception { /** test that a replica request is rejected if it arrives at a shard with a wrong allocation id */ public void testReplicaActionRejectsWrongAid() throws Exception { - final String index = "test"; - final ShardId shardId = new ShardId(index, "_na_", 0); + final Index index = new Index("test", randomUUID()); + final ShardId shardId = new ShardId(index, 0); ClusterState state = state(index, false, ShardRoutingState.STARTED, ShardRoutingState.STARTED); final ShardRouting replica = state.routingTable().shardRoutingTable(shardId).replicaShards().get(0); // simulate execution of the node holding the replica @@ -1212,8 +1214,9 @@ public void testReplicaActionRejectsWrongAid() throws Exception { * causes a retry */ public void testRetryOnReplica() throws Exception { - final ShardId shardId = new ShardId("test", "_na_", 0); - ClusterState state = state(shardId.getIndexName(), true, ShardRoutingState.STARTED, ShardRoutingState.STARTED); + final Index index = new Index("test", randomUUID()); + final ShardId shardId = new ShardId(index, 0); + ClusterState state = state(index, true, ShardRoutingState.STARTED, ShardRoutingState.STARTED); final ShardRouting replica = state.getRoutingTable().shardRoutingTable(shardId).replicaShards().get(0); final long primaryTerm = state.metadata().getProject().index(shardId.getIndexName()).primaryTerm(shardId.id()); // simulate execution of the node holding the replica @@ -1287,8 +1290,9 @@ protected void shardOperationOnReplica(Request shardRequest, IndexShard replica, } public void testRetryOnReplicaWithRealTransport() throws Exception { - final ShardId shardId = new ShardId("test", "_na_", 0); - final ClusterState initialState = state(shardId.getIndexName(), true, ShardRoutingState.STARTED, ShardRoutingState.STARTED); + final Index index = new Index("test", randomUUID()); + final ShardId shardId = new ShardId(index, 0); + final ClusterState initialState = state(index, true, ShardRoutingState.STARTED, ShardRoutingState.STARTED); final ShardRouting replica = initialState.getRoutingTable().shardRoutingTable(shardId).replicaShards().get(0); final long primaryTerm = initialState.metadata().getProject().index(shardId.getIndexName()).primaryTerm(shardId.id()); // simulate execution of the node holding the replica diff --git a/server/src/test/java/org/elasticsearch/action/support/replication/TransportWriteActionTests.java b/server/src/test/java/org/elasticsearch/action/support/replication/TransportWriteActionTests.java index f0b50087d5f1c..d4fcbf5d6a193 100644 --- a/server/src/test/java/org/elasticsearch/action/support/replication/TransportWriteActionTests.java +++ b/server/src/test/java/org/elasticsearch/action/support/replication/TransportWriteActionTests.java @@ -302,8 +302,8 @@ public void testReplicaProxy() throws InterruptedException, ExecutionException { shardStateAction, threadPool ); - final String index = "test"; - final ShardId shardId = new ShardId(index, "_na_", 0); + final Index index = new Index("test", randomUUID()); + final ShardId shardId = new ShardId(index, 0); ClusterState state = ClusterStateCreationUtils.stateWithActivePrimary(projectId, index, true, 1 + randomInt(3), randomInt(2)); logger.info("using state: {}", state); ClusterServiceUtils.setState(clusterService, state); diff --git a/server/src/test/java/org/elasticsearch/action/support/single/instance/TransportInstanceSingleOperationActionTests.java b/server/src/test/java/org/elasticsearch/action/support/single/instance/TransportInstanceSingleOperationActionTests.java index c62bee7593ca6..058d57c130f30 100644 --- a/server/src/test/java/org/elasticsearch/action/support/single/instance/TransportInstanceSingleOperationActionTests.java +++ b/server/src/test/java/org/elasticsearch/action/support/single/instance/TransportInstanceSingleOperationActionTests.java @@ -38,6 +38,7 @@ import org.elasticsearch.common.util.concurrent.EsExecutors; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.index.Index; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.indices.EmptySystemIndices; import org.elasticsearch.rest.RestStatus; @@ -228,7 +229,10 @@ public void testBasicRequestWorks() throws InterruptedException, ExecutionExcept Request request = new Request().index("test"); request.shardId = new ShardId("test", "_na_", 0); PlainActionFuture listener = new PlainActionFuture<>(); - setState(clusterService, ClusterStateCreationUtils.state(projectId, "test", randomBoolean(), ShardRoutingState.STARTED)); + setState( + clusterService, + ClusterStateCreationUtils.state(projectId, new Index("test", randomUUID()), randomBoolean(), ShardRoutingState.STARTED) + ); action.new AsyncSingleAction(request, listener).start(); assertThat(transport.capturedRequests().length, equalTo(1)); transport.handleResponse(transport.capturedRequests()[0].requestId(), new Response()); @@ -236,10 +240,11 @@ public void testBasicRequestWorks() throws InterruptedException, ExecutionExcept } public void testFailureWithoutRetry() throws Exception { - Request request = new Request().index("test"); - request.shardId = new ShardId("test", "_na_", 0); + Index index = new Index("test", randomUUID()); + Request request = new Request().index(index.getName()); + request.shardId = new ShardId(index, 0); PlainActionFuture listener = new PlainActionFuture<>(); - setState(clusterService, ClusterStateCreationUtils.state(projectId, "test", randomBoolean(), ShardRoutingState.STARTED)); + setState(clusterService, ClusterStateCreationUtils.state(projectId, index, randomBoolean(), ShardRoutingState.STARTED)); action.new AsyncSingleAction(request, listener).start(); assertThat(transport.capturedRequests().length, equalTo(1)); @@ -265,15 +270,16 @@ public void testFailureWithoutRetry() throws Exception { } public void testSuccessAfterRetryWithClusterStateUpdate() throws Exception { - Request request = new Request().index("test"); - request.shardId = new ShardId("test", "_na_", 0); + Index index = new Index("test", randomUUID()); + Request request = new Request().index(index.getName()); + request.shardId = new ShardId(index, 0); PlainActionFuture listener = new PlainActionFuture<>(); boolean local = randomBoolean(); - setState(clusterService, ClusterStateCreationUtils.state(projectId, "test", local, ShardRoutingState.INITIALIZING)); + setState(clusterService, ClusterStateCreationUtils.state(projectId, index, local, ShardRoutingState.INITIALIZING)); action.new AsyncSingleAction(request, listener).start(); // this should fail because primary not initialized assertThat(transport.capturedRequests().length, equalTo(0)); - setState(clusterService, ClusterStateCreationUtils.state(projectId, "test", local, ShardRoutingState.STARTED)); + setState(clusterService, ClusterStateCreationUtils.state(projectId, index, local, ShardRoutingState.STARTED)); // this time it should work assertThat(transport.capturedRequests().length, equalTo(1)); transport.handleResponse(transport.capturedRequests()[0].requestId(), new Response()); @@ -281,11 +287,12 @@ public void testSuccessAfterRetryWithClusterStateUpdate() throws Exception { } public void testSuccessAfterRetryWithExceptionFromTransport() throws Exception { - Request request = new Request().index("test"); - request.shardId = new ShardId("test", "_na_", 0); + Index index = new Index("test", randomUUID()); + Request request = new Request().index(index.getName()); + request.shardId = new ShardId(index, 0); PlainActionFuture listener = new PlainActionFuture<>(); boolean local = randomBoolean(); - setState(clusterService, ClusterStateCreationUtils.state(projectId, "test", local, ShardRoutingState.STARTED)); + setState(clusterService, ClusterStateCreationUtils.state(projectId, index, local, ShardRoutingState.STARTED)); action.new AsyncSingleAction(request, listener).start(); assertThat(transport.capturedRequests().length, equalTo(1)); long requestId = transport.capturedRequests()[0].requestId(); @@ -293,17 +300,19 @@ public void testSuccessAfterRetryWithExceptionFromTransport() throws Exception { DiscoveryNode node = clusterService.state().getNodes().getLocalNode(); transport.handleLocalError(requestId, new ConnectTransportException(node, "test exception")); // trigger cluster state observer - setState(clusterService, ClusterStateCreationUtils.state(projectId, "test", local, ShardRoutingState.STARTED)); + setState(clusterService, ClusterStateCreationUtils.state(projectId, index, local, ShardRoutingState.STARTED)); assertThat(transport.capturedRequests().length, equalTo(1)); transport.handleResponse(transport.capturedRequests()[0].requestId(), new Response()); listener.get(); } public void testRetryOfAnAlreadyTimedOutRequest() throws Exception { - Request request = new Request().index("test").timeout(new TimeValue(0, TimeUnit.MILLISECONDS)); - request.shardId = new ShardId("test", "_na_", 0); + Index index = new Index("test", randomUUID()); + Request request = new Request().index(index.getName()).timeout(new TimeValue(0, TimeUnit.MILLISECONDS)); + ; + request.shardId = new ShardId(index, 0); PlainActionFuture listener = new PlainActionFuture<>(); - setState(clusterService, ClusterStateCreationUtils.state(projectId, "test", randomBoolean(), ShardRoutingState.STARTED)); + setState(clusterService, ClusterStateCreationUtils.state(projectId, index, randomBoolean(), ShardRoutingState.STARTED)); action.new AsyncSingleAction(request, listener).start(); assertThat(transport.capturedRequests().length, equalTo(1)); long requestId = transport.capturedRequests()[0].requestId(); @@ -343,10 +352,11 @@ protected void resolveRequest(ProjectState state, Request request) { throw new IllegalStateException("request cannot be resolved"); } }; - Request request = new Request().index("test"); - request.shardId = new ShardId("test", "_na_", 0); + Index index = new Index("test", randomUUID()); + Request request = new Request().index(index.getName()); + request.shardId = new ShardId(index, 0); PlainActionFuture listener = new PlainActionFuture<>(); - setState(clusterService, ClusterStateCreationUtils.state(projectId, "test", randomBoolean(), ShardRoutingState.STARTED)); + setState(clusterService, ClusterStateCreationUtils.state(projectId, index, randomBoolean(), ShardRoutingState.STARTED)); action.new AsyncSingleAction(request, listener).start(); assertThat(transport.capturedRequests().length, equalTo(0)); try { diff --git a/server/src/test/java/org/elasticsearch/cluster/action/shard/ShardStartedClusterStateTaskExecutorTests.java b/server/src/test/java/org/elasticsearch/cluster/action/shard/ShardStartedClusterStateTaskExecutorTests.java index bbbf8404f1be7..f1bc6bf60afcf 100644 --- a/server/src/test/java/org/elasticsearch/cluster/action/shard/ShardStartedClusterStateTaskExecutorTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/action/shard/ShardStartedClusterStateTaskExecutorTests.java @@ -26,6 +26,7 @@ import org.elasticsearch.cluster.service.ClusterStateTaskExecutorUtils; import org.elasticsearch.common.Priority; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.index.Index; import org.elasticsearch.index.shard.IndexLongFieldRange; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.index.shard.ShardLongFieldRange; @@ -88,17 +89,16 @@ public void testNonExistentIndexMarkedAsSuccessful() throws Exception { } public void testNonExistentShardsAreMarkedAsSuccessful() throws Exception { - final String indexName = "test"; - final ClusterState clusterState = stateWithActivePrimary(indexName, true, randomInt(2), randomInt(2)); + final Index index = new Index("test", randomUUID()); + final ClusterState clusterState = stateWithActivePrimary(index, true, randomInt(2), randomInt(2)); - final IndexMetadata indexMetadata = clusterState.metadata().getProject().index(indexName); final List tasks = Stream.concat( // Existent shard id but different allocation id IntStream.range(0, randomIntBetween(1, 5)) .mapToObj( i -> new StartedShardUpdateTask( new StartedShardEntry( - new ShardId(indexMetadata.getIndex(), 0), + new ShardId(index, 0), String.valueOf(i), 0L, "allocation id", @@ -113,7 +113,7 @@ public void testNonExistentShardsAreMarkedAsSuccessful() throws Exception { .mapToObj( i -> new StartedShardUpdateTask( new StartedShardEntry( - new ShardId(indexMetadata.getIndex(), i), + new ShardId(index, i), String.valueOf(i), 0L, "shard id", @@ -130,13 +130,13 @@ public void testNonExistentShardsAreMarkedAsSuccessful() throws Exception { } public void testNonInitializingShardAreMarkedAsSuccessful() throws Exception { - final String indexName = "test"; - final ClusterState clusterState = stateWithAssignedPrimariesAndReplicas(new String[] { indexName }, randomIntBetween(2, 10), 1); + final Index index = new Index("test", randomUUID()); + final ClusterState clusterState = stateWithAssignedPrimariesAndReplicas(new Index[] { index }, randomIntBetween(2, 10), 1); - final IndexMetadata indexMetadata = clusterState.metadata().getProject().index(indexName); + final IndexMetadata indexMetadata = clusterState.metadata().getProject().index(index); final List tasks = IntStream.range(0, randomIntBetween(1, indexMetadata.getNumberOfShards())) .mapToObj(i -> { - final ShardId shardId = new ShardId(indexMetadata.getIndex(), i); + final ShardId shardId = new ShardId(index, i); final IndexShardRoutingTable shardRoutingTable = clusterState.routingTable().shardRoutingTable(shardId); final String allocationId; if (randomBoolean()) { @@ -163,10 +163,10 @@ public void testNonInitializingShardAreMarkedAsSuccessful() throws Exception { } public void testStartPrimary() throws Exception { - final String indexName = "test"; - final ClusterState clusterState = state(indexName, randomBoolean(), ShardRoutingState.INITIALIZING); + final Index index = new Index("test", randomUUID()); + final ClusterState clusterState = state(index, randomBoolean(), ShardRoutingState.INITIALIZING); - final IndexMetadata indexMetadata = clusterState.metadata().getProject().index(indexName); + final IndexMetadata indexMetadata = clusterState.metadata().getProject().index(index); final ShardId shardId = new ShardId(indexMetadata.getIndex(), 0); final long primaryTerm = indexMetadata.primaryTerm(shardId.id()); final ShardRouting primaryShard = clusterState.routingTable().shardRoutingTable(shardId).primaryShard(); @@ -196,10 +196,10 @@ public void testStartPrimary() throws Exception { } public void testStartReplica() throws Exception { - final String indexName = "test"; - final ClusterState clusterState = state(indexName, randomBoolean(), ShardRoutingState.STARTED, ShardRoutingState.INITIALIZING); + final Index index = new Index("test", randomUUID()); + final ClusterState clusterState = state(index, randomBoolean(), ShardRoutingState.STARTED, ShardRoutingState.INITIALIZING); - final IndexMetadata indexMetadata = clusterState.metadata().getProject().index(indexName); + final IndexMetadata indexMetadata = clusterState.metadata().getProject().index(index); final ShardId shardId = new ShardId(indexMetadata.getIndex(), 0); final long primaryTerm = indexMetadata.primaryTerm(shardId.id()); final ShardRouting primaryShard = clusterState.routingTable().shardRoutingTable(shardId).primaryShard(); @@ -230,14 +230,14 @@ public void testStartReplica() throws Exception { } public void testDuplicateStartsAreOkay() throws Exception { - final String indexName = "test"; - final ClusterState clusterState = state(indexName, randomBoolean(), ShardRoutingState.INITIALIZING); + final Index index = new Index("test", randomUUID()); + final ClusterState clusterState = state(index, randomBoolean(), ShardRoutingState.INITIALIZING); final ProjectMetadata project = clusterState.metadata().getProject(); - final IndexMetadata indexMetadata = project.index(indexName); + final IndexMetadata indexMetadata = project.index(index); assertThat(indexMetadata, notNullValue()); - final ShardId shardId = new ShardId(indexMetadata.getIndex(), 0); + final ShardId shardId = new ShardId(index, 0); final ShardRouting shardRouting = clusterState.routingTable(project.id()).shardRoutingTable(shardId).primaryShard(); final String allocationId = shardRouting.allocationId().getId(); final long primaryTerm = indexMetadata.primaryTerm(shardId.id()); @@ -272,24 +272,22 @@ public void testDuplicateStartsAreOkay() throws Exception { } public void testPrimaryTermsMismatchOnPrimary() throws Exception { - final String indexName = "test"; + final Index index = new Index("test", randomUUID()); final int shard = 0; final int primaryTerm = 2 + randomInt(200); - ClusterState clusterState = state(indexName, randomBoolean(), ShardRoutingState.INITIALIZING); + ClusterState clusterState = state(index, randomBoolean(), ShardRoutingState.INITIALIZING); clusterState = ClusterState.builder(clusterState) .metadata( Metadata.builder(clusterState.metadata()) .put( - IndexMetadata.builder(clusterState.metadata().getProject().index(indexName)) - .primaryTerm(shard, primaryTerm) - .build(), + IndexMetadata.builder(clusterState.metadata().getProject().index(index)).primaryTerm(shard, primaryTerm).build(), true ) .build() ) .build(); - final ShardId shardId = new ShardId(clusterState.metadata().getProject().index(indexName).getIndex(), shard); + final ShardId shardId = new ShardId(index, shard); final String primaryAllocationId = clusterState.routingTable().shardRoutingTable(shardId).primaryShard().allocationId().getId(); { final StartedShardUpdateTask task = new StartedShardUpdateTask( @@ -339,24 +337,22 @@ public void testPrimaryTermsMismatchOnPrimary() throws Exception { } public void testPrimaryTermsMismatchOnReplica() throws Exception { - final String indexName = "test"; + final Index index = new Index("test", randomUUID()); final int shard = 0; final int primaryTerm = 2 + randomInt(200); - ClusterState clusterState = state(indexName, randomBoolean(), ShardRoutingState.STARTED, ShardRoutingState.INITIALIZING); + ClusterState clusterState = state(index, randomBoolean(), ShardRoutingState.STARTED, ShardRoutingState.INITIALIZING); clusterState = ClusterState.builder(clusterState) .metadata( Metadata.builder(clusterState.metadata()) .put( - IndexMetadata.builder(clusterState.metadata().getProject().index(indexName)) - .primaryTerm(shard, primaryTerm) - .build(), + IndexMetadata.builder(clusterState.metadata().getProject().index(index)).primaryTerm(shard, primaryTerm).build(), true ) .build() ) .build(); - final ShardId shardId = new ShardId(clusterState.metadata().getProject().index(indexName).getIndex(), shard); + final ShardId shardId = new ShardId(index, shard); { final long replicaPrimaryTerm = randomBoolean() ? primaryTerm : primaryTerm - 1; final String replicaAllocationId = clusterState.routingTable() @@ -392,11 +388,11 @@ public void testPrimaryTermsMismatchOnReplica() throws Exception { } public void testExpandsTimestampRangeForPrimary() throws Exception { - final String indexName = "test"; - final ClusterState clusterState = state(indexName, randomBoolean(), ShardRoutingState.INITIALIZING); + final Index index = new Index("test", randomUUID()); + final ClusterState clusterState = state(index, randomBoolean(), ShardRoutingState.INITIALIZING); - final IndexMetadata indexMetadata = clusterState.metadata().getProject().index(indexName); - final ShardId shardId = new ShardId(indexMetadata.getIndex(), 0); + final IndexMetadata indexMetadata = clusterState.metadata().getProject().index(index); + final ShardId shardId = new ShardId(index, 0); final long primaryTerm = indexMetadata.primaryTerm(shardId.id()); final ShardRouting primaryShard = clusterState.routingTable().shardRoutingTable(shardId).primaryShard(); final String primaryAllocationId = primaryShard.allocationId().getId(); @@ -427,7 +423,7 @@ public void testExpandsTimestampRangeForPrimary() throws Exception { is(ShardRoutingState.STARTED) ); - final var timestampRange = resultingState.metadata().getProject().index(indexName).getTimestampRange(); + final var timestampRange = resultingState.metadata().getProject().index(index).getTimestampRange(); if (shardTimestampRange == ShardLongFieldRange.UNKNOWN) { assertThat(timestampRange, sameInstance(IndexLongFieldRange.UNKNOWN)); } else if (shardTimestampRange == ShardLongFieldRange.EMPTY) { @@ -438,7 +434,7 @@ public void testExpandsTimestampRangeForPrimary() throws Exception { assertThat(timestampRange.getMax(), equalTo(shardTimestampRange.getMax())); } - final var eventIngestedRange = resultingState.metadata().getProject().index(indexName).getEventIngestedRange(); + final var eventIngestedRange = resultingState.metadata().getProject().index(index).getEventIngestedRange(); if (clusterState.getMinTransportVersion().before(TransportVersions.V_8_15_0)) { assertThat(eventIngestedRange, sameInstance(IndexLongFieldRange.UNKNOWN)); } else { @@ -455,11 +451,11 @@ public void testExpandsTimestampRangeForPrimary() throws Exception { } public void testExpandsTimestampRangeForReplica() throws Exception { - final String indexName = "test"; - final ClusterState clusterState = state(indexName, randomBoolean(), ShardRoutingState.STARTED, ShardRoutingState.INITIALIZING); + final Index index = new Index("test", randomUUID()); + final ClusterState clusterState = state(index, randomBoolean(), ShardRoutingState.STARTED, ShardRoutingState.INITIALIZING); - final IndexMetadata indexMetadata = clusterState.metadata().getProject().index(indexName); - final ShardId shardId = new ShardId(indexMetadata.getIndex(), 0); + final IndexMetadata indexMetadata = clusterState.metadata().getProject().index(index); + final ShardId shardId = new ShardId(index, 0); final long primaryTerm = indexMetadata.primaryTerm(shardId.id()); assertThat(indexMetadata.getTimestampRange(), sameInstance(IndexLongFieldRange.UNKNOWN)); @@ -489,7 +485,7 @@ public void testExpandsTimestampRangeForReplica() throws Exception { is(ShardRoutingState.STARTED) ); - final IndexMetadata latestIndexMetadata = resultingState.metadata().getProject().index(indexName); + final IndexMetadata latestIndexMetadata = resultingState.metadata().getProject().index(index); assertThat(latestIndexMetadata.getTimestampRange(), sameInstance(IndexLongFieldRange.UNKNOWN)); assertThat(latestIndexMetadata.getEventIngestedRange(), sameInstance(IndexLongFieldRange.UNKNOWN)); } diff --git a/server/src/test/java/org/elasticsearch/cluster/action/shard/ShardStateActionTests.java b/server/src/test/java/org/elasticsearch/cluster/action/shard/ShardStateActionTests.java index b189d7796b51a..fcbb819a627e8 100644 --- a/server/src/test/java/org/elasticsearch/cluster/action/shard/ShardStateActionTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/action/shard/ShardStateActionTests.java @@ -33,6 +33,7 @@ import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.index.Index; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.index.shard.ShardLongFieldRange; import org.elasticsearch.index.shard.ShardLongFieldRangeWireTests; @@ -160,12 +161,12 @@ public static void stopThreadPool() { } public void testSuccess() throws InterruptedException { - final String index = "test"; + final Index index = new Index("test", randomUUID()); setState(clusterService, ClusterStateCreationUtils.stateWithActivePrimary(index, true, randomInt(5))); final TestListener listener = new TestListener(); - ShardRouting shardRouting = getRandomShardRouting(index); + ShardRouting shardRouting = getRandomShardRouting(index.getName()); shardStateAction.localShardFailed(shardRouting, "test", getSimulatedFailure(), listener); CapturingTransport.CapturedRequest[] capturedRequests = transport.getCapturedRequestsAndClear(); @@ -186,7 +187,7 @@ public void testSuccess() throws InterruptedException { } public void testNoMaster() throws InterruptedException { - final String index = "test"; + final Index index = new Index("test", randomUUID()); setState(clusterService, ClusterStateCreationUtils.stateWithActivePrimary(index, true, randomInt(5))); @@ -199,7 +200,7 @@ public void testNoMaster() throws InterruptedException { setUpMasterRetryVerification(1, retries, latch, requestId -> {}); - ShardRouting failedShard = getRandomShardRouting(index); + ShardRouting failedShard = getRandomShardRouting(index.getName()); shardStateAction.localShardFailed(failedShard, "test", getSimulatedFailure(), new ActionListener() { @Override public void onResponse(Void aVoid) { @@ -222,7 +223,7 @@ public void onFailure(Exception e) { } public void testMasterChannelException() throws InterruptedException { - final String index = "test"; + final Index index = new Index("test", randomUUID()); setState(clusterService, ClusterStateCreationUtils.stateWithActivePrimary(index, true, randomInt(5))); @@ -249,7 +250,7 @@ public void testMasterChannelException() throws InterruptedException { final int numberOfRetries = randomIntBetween(1, 256); setUpMasterRetryVerification(numberOfRetries, retries, latch, retryLoop); - ShardRouting failedShard = getRandomShardRouting(index); + ShardRouting failedShard = getRandomShardRouting(index.getName()); shardStateAction.localShardFailed(failedShard, "test", getSimulatedFailure(), new ActionListener() { @Override public void onResponse(Void aVoid) { @@ -279,12 +280,12 @@ public void onFailure(Exception e) { } public void testUnhandledFailure() { - final String index = "test"; + final Index index = new Index("test", randomUUID()); setState(clusterService, ClusterStateCreationUtils.stateWithActivePrimary(index, true, randomInt(5))); final TestListener listener = new TestListener(); - ShardRouting failedShard = getRandomShardRouting(index); + ShardRouting failedShard = getRandomShardRouting(index.getName()); shardStateAction.localShardFailed(failedShard, "test", getSimulatedFailure(), listener); final CapturingTransport.CapturedRequest[] capturedRequests = transport.getCapturedRequestsAndClear(); @@ -294,13 +295,13 @@ public void testUnhandledFailure() { } public void testShardNotFound() throws InterruptedException { - final String index = "test"; + final Index index = new Index("test", randomUUID()); setState(clusterService, ClusterStateCreationUtils.stateWithActivePrimary(index, true, randomInt(5))); final TestListener listener = new TestListener(); - ShardRouting failedShard = getRandomShardRouting(index); - RoutingTable routingTable = RoutingTable.builder(clusterService.state().getRoutingTable()).remove(index).build(); + ShardRouting failedShard = getRandomShardRouting(index.getName()); + RoutingTable routingTable = RoutingTable.builder(clusterService.state().getRoutingTable()).remove(index.getName()).build(); setState(clusterService, ClusterState.builder(clusterService.state()).routingTable(routingTable)); shardStateAction.localShardFailed(failedShard, "test", getSimulatedFailure(), listener); @@ -312,11 +313,11 @@ public void testShardNotFound() throws InterruptedException { } public void testNoLongerPrimaryShardException() throws InterruptedException { - final String index = "test"; + final Index index = new Index("test", randomUUID()); setState(clusterService, ClusterStateCreationUtils.stateWithActivePrimary(index, true, randomInt(5))); - ShardRouting failedShard = getRandomShardRouting(index); + ShardRouting failedShard = getRandomShardRouting(index.getName()); final TestListener listener = new TestListener(); long primaryTerm = clusterService.state().metadata().getProject().index(index).primaryTerm(failedShard.id()); @@ -347,9 +348,9 @@ public void testNoLongerPrimaryShardException() throws InterruptedException { } public void testCacheRemoteShardFailed() throws Exception { - final String index = "test"; + final Index index = new Index("test", randomUUID()); setState(clusterService, ClusterStateCreationUtils.stateWithActivePrimary(index, true, randomInt(5))); - ShardRouting failedShard = getRandomShardRouting(index); + ShardRouting failedShard = getRandomShardRouting(index.getName()); boolean markAsStale = randomBoolean(); int numListeners = between(1, 100); CountDownLatch latch = new CountDownLatch(numListeners); @@ -383,9 +384,9 @@ public void onFailure(Exception e) { } public void testDeduplicateRemoteShardStarted() throws InterruptedException { - final String index = "test"; + final Index index = new Index("test", randomUUID()); setState(clusterService, ClusterStateCreationUtils.stateWithActivePrimary(index, true, randomInt(5))); - ShardRouting startedShard = getRandomShardRouting(index); + ShardRouting startedShard = getRandomShardRouting(index.getName()); int numListeners = between(1, 100); CountDownLatch latch = new CountDownLatch(numListeners); long primaryTerm = randomLongBetween(1, Long.MAX_VALUE); @@ -424,12 +425,12 @@ public void onFailure(Exception e) { } public void testRemoteShardFailedConcurrently() throws Exception { - final String index = "test"; + final Index index = new Index("test", randomUUID()); final AtomicBoolean shutdown = new AtomicBoolean(false); setState(clusterService, ClusterStateCreationUtils.stateWithActivePrimary(index, true, randomInt(5))); ShardRouting[] failedShards = new ShardRouting[between(1, 5)]; for (int i = 0; i < failedShards.length; i++) { - failedShards[i] = getRandomShardRouting(index); + failedShards[i] = getRandomShardRouting(index.getName()); } final int clientThreads = between(1, 6); int iterationsPerThread = scaledRandomIntBetween(50, 500); @@ -480,10 +481,10 @@ public void onFailure(Exception e) { } public void testShardStarted() throws InterruptedException { - final String index = "test"; + final Index index = new Index("test", randomUUID()); setState(clusterService, ClusterStateCreationUtils.stateWithActivePrimary(index, true, randomInt(5))); - final ShardRouting shardRouting = getRandomShardRouting(index); + final ShardRouting shardRouting = getRandomShardRouting(index.getName()); final long primaryTerm = clusterService.state().metadata().getProject().index(shardRouting.index()).primaryTerm(shardRouting.id()); final TestListener listener = new TestListener(); shardStateAction.shardStarted( diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/DelayedAllocationServiceTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/DelayedAllocationServiceTests.java index 979349be2412a..a61ccd015a77c 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/DelayedAllocationServiceTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/DelayedAllocationServiceTests.java @@ -235,24 +235,20 @@ public void testDelayedUnassignedScheduleRerouteAfterDelayedReroute() throws Exc .put( IndexMetadata.builder("short_delay") .settings( - settings(IndexVersion.current()).put( + indexSettings(IndexVersion.current(), randomUUID(), 1, 1).put( UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), shortDelaySetting ) ) - .numberOfShards(1) - .numberOfReplicas(1) ) .put( IndexMetadata.builder("long_delay") .settings( - settings(IndexVersion.current()).put( + indexSettings(IndexVersion.current(), randomUUID(), 1, 1).put( UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), longDelaySetting ) ) - .numberOfShards(1) - .numberOfReplicas(1) ) .build(); ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/ExpectedShardSizeEstimatorTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/ExpectedShardSizeEstimatorTests.java index fcc372a53f517..3c3d4f64ee07c 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/ExpectedShardSizeEstimatorTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/ExpectedShardSizeEstimatorTests.java @@ -20,6 +20,7 @@ import org.elasticsearch.cluster.routing.allocation.RoutingAllocation; import org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders; import org.elasticsearch.common.collect.ImmutableOpenMap; +import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexVersion; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.repositories.IndexId; @@ -48,12 +49,12 @@ public class ExpectedShardSizeEstimatorTests extends ESAllocationTestCase { public void testShouldFallbackToDefaultExpectedShardSize() { - var state = ClusterState.builder(ClusterName.DEFAULT).metadata(metadata(index("my-index"))).build(); + var index = new Index("my-index", randomUUID()); + var state = ClusterState.builder(ClusterName.DEFAULT).metadata(metadata(index(index))).build(); state = buildRoutingTable(state); - var shard = shardRoutingBuilder(new ShardId("my-index", "_na_", 0), randomIdentifier(), true, ShardRoutingState.INITIALIZING) - .withRecoverySource( - randomFrom(RecoverySource.EmptyStoreRecoverySource.INSTANCE, RecoverySource.ExistingStoreRecoverySource.INSTANCE) - ).build(); + var shard = shardRoutingBuilder(new ShardId(index, 0), randomIdentifier(), true, ShardRoutingState.INITIALIZING).withRecoverySource( + randomFrom(RecoverySource.EmptyStoreRecoverySource.INSTANCE, RecoverySource.ExistingStoreRecoverySource.INSTANCE) + ).build(); var allocation = createRoutingAllocation(state, ClusterInfo.EMPTY, SnapshotShardSizeInfo.EMPTY); @@ -67,11 +68,12 @@ public void testShouldFallbackToDefaultExpectedShardSize() { public void testShouldReadExpectedSizeFromClusterInfo() { var shardSize = randomLongBetween(100, 1000); - var state = ClusterState.builder(ClusterName.DEFAULT).metadata(metadata(index("my-index"))).build(); + var index = new Index("my-index", randomUUID()); + var state = ClusterState.builder(ClusterName.DEFAULT).metadata(metadata(index(index))).build(); state = buildRoutingTable(state); - var shard = shardRoutingBuilder(new ShardId("my-index", "_na_", 0), randomIdentifier(), true, ShardRoutingState.INITIALIZING) - .withRecoverySource(RecoverySource.PeerRecoverySource.INSTANCE) - .build(); + var shard = shardRoutingBuilder(new ShardId(index, 0), randomIdentifier(), true, ShardRoutingState.INITIALIZING).withRecoverySource( + RecoverySource.PeerRecoverySource.INSTANCE + ).build(); var clusterInfo = createClusterInfo(shard, shardSize); var allocation = createRoutingAllocation(state, clusterInfo, SnapshotShardSizeInfo.EMPTY); @@ -83,10 +85,12 @@ public void testShouldReadExpectedSizeFromClusterInfo() { public void testShouldReadExpectedSizeFromPrimaryWhenAddingNewReplica() { var shardSize = randomLongBetween(100, 1000); - var state = ClusterState.builder(ClusterName.DEFAULT).metadata(metadata(index("my-index"))).build(); + var index = new Index("my-index", randomUUID()); + var state = ClusterState.builder(ClusterName.DEFAULT).metadata(metadata(index(index))).build(); state = buildRoutingTable(state); - var primary = newShardRouting("my-index", 0, randomIdentifier(), true, ShardRoutingState.STARTED); - var replica = newShardRouting("my-index", 0, randomIdentifier(), false, ShardRoutingState.INITIALIZING); + var shardId = new ShardId(index, 0); + var primary = newShardRouting(shardId, randomIdentifier(), true, ShardRoutingState.STARTED); + var replica = newShardRouting(shardId, randomIdentifier(), false, ShardRoutingState.INITIALIZING); var clusterInfo = createClusterInfo(primary, shardSize); var allocation = createRoutingAllocation(state, clusterInfo, SnapshotShardSizeInfo.EMPTY); @@ -99,31 +103,33 @@ public void testShouldReadExpectedSizeWhenInitializingFromSnapshot() { var snapshotShardSize = randomLongBetween(100, 1000); - var index = switch (randomIntBetween(0, 2)) { + var index = new Index("my-index", randomUUID()); + var indexMetadata = switch (randomIntBetween(0, 2)) { // regular snapshot - case 0 -> index("my-index"); + case 0 -> index(index).settings(indexSettings(IndexVersion.current(), index.getUUID(), 1, 0)); // searchable snapshot - case 1 -> index("my-index").settings( - indexSettings(IndexVersion.current(), 1, 0) // + case 1 -> index(index).settings( + indexSettings(IndexVersion.current(), index.getUUID(), 1, 0) // .put(INDEX_STORE_TYPE_SETTING.getKey(), SEARCHABLE_SNAPSHOT_STORE_TYPE) // ); // partial searchable snapshot - case 2 -> index("my-index").settings( - indexSettings(IndexVersion.current(), 1, 0) // + case 2 -> index(index).settings( + indexSettings(IndexVersion.current(), index.getUUID(), 1, 0) // .put(INDEX_STORE_TYPE_SETTING.getKey(), SEARCHABLE_SNAPSHOT_STORE_TYPE) // .put(SNAPSHOT_PARTIAL_SETTING.getKey(), true) // ); default -> throw new AssertionError("unexpected index type"); }; - var state = ClusterState.builder(ClusterName.DEFAULT).metadata(metadata(index)).build(); + var state = ClusterState.builder(ClusterName.DEFAULT).metadata(metadata(indexMetadata)).build(); state = buildRoutingTable(state); var snapshot = new Snapshot("repository", new SnapshotId("snapshot-1", "na")); - var indexId = new IndexId("my-index", "_na_"); + // The uuid in the IndexId is different from index uuid. + var indexId = new IndexId(index.getName(), randomUUID()); - var shard = shardRoutingBuilder(new ShardId("my-index", "_na_", 0), randomIdentifier(), true, ShardRoutingState.INITIALIZING) - .withRecoverySource(new RecoverySource.SnapshotRecoverySource(randomUUID(), snapshot, IndexVersion.current(), indexId)) - .build(); + var shard = shardRoutingBuilder(new ShardId(index, 0), randomIdentifier(), true, ShardRoutingState.INITIALIZING).withRecoverySource( + new RecoverySource.SnapshotRecoverySource(randomUUID(), snapshot, IndexVersion.current(), indexId) + ).build(); var snapshotShardSizeInfo = new SnapshotShardSizeInfo( Map.of(new InternalSnapshotsInfoService.SnapshotShard(snapshot, indexId, shard.shardId()), snapshotShardSize) @@ -131,7 +137,7 @@ public void testShouldReadExpectedSizeWhenInitializingFromSnapshot() { var allocation = createRoutingAllocation(state, ClusterInfo.EMPTY, snapshotShardSizeInfo); assertThat(getExpectedShardSize(shard, defaultValue, allocation), equalTo(snapshotShardSize)); - if (state.metadata().getProject().index("my-index").isPartialSearchableSnapshot() == false) { + if (state.metadata().getProject().index(index).isPartialSearchableSnapshot() == false) { assertTrue("Should reserve space for snapshot restore", shouldReserveSpaceForInitializingShard(shard, allocation)); } else { assertFalse( @@ -143,21 +149,24 @@ public void testShouldReadExpectedSizeWhenInitializingFromSnapshot() { public void testShouldReadSizeFromClonedShard() { + Index sourceIndex = new Index("source", randomUUID()); + Index targetIndex = new Index("target", randomUUID()); var sourceShardSize = randomLongBetween(100, 1000); - var source = newShardRouting(new ShardId("source", "_na_", 0), randomIdentifier(), true, ShardRoutingState.STARTED); - var target = shardRoutingBuilder(new ShardId("target", "_na_", 0), randomIdentifier(), true, ShardRoutingState.INITIALIZING) - .withRecoverySource(RecoverySource.LocalShardsRecoverySource.INSTANCE) - .build(); + var source = newShardRouting(sourceIndex, 0, randomIdentifier(), true, ShardRoutingState.STARTED); + var target = shardRoutingBuilder(targetIndex, 0, randomIdentifier(), true, ShardRoutingState.INITIALIZING).withRecoverySource( + RecoverySource.LocalShardsRecoverySource.INSTANCE + ).build(); var state = ClusterState.builder(ClusterName.DEFAULT) .metadata( metadata( - IndexMetadata.builder("source").settings(indexSettings(IndexVersion.current(), 1, 0)), - IndexMetadata.builder("target") + IndexMetadata.builder(sourceIndex.getName()) + .settings(indexSettings(IndexVersion.current(), sourceIndex.getUUID(), 1, 0)), + IndexMetadata.builder(targetIndex.getName()) .settings( - indexSettings(IndexVersion.current(), 1, 0) // - .put(INDEX_RESIZE_SOURCE_NAME_KEY, "source") // - .put(INDEX_RESIZE_SOURCE_UUID_KEY, "_na_") + indexSettings(IndexVersion.current(), targetIndex.getUUID(), 1, 0) // + .put(INDEX_RESIZE_SOURCE_NAME_KEY, sourceIndex.getName()) // + .put(INDEX_RESIZE_SOURCE_UUID_KEY, sourceIndex.getUUID()) ) ) ) @@ -186,8 +195,8 @@ private static RoutingAllocation createRoutingAllocation( return new RoutingAllocation(new AllocationDeciders(List.of()), state, clusterInfo, snapshotShardSizeInfo, 0); } - private static IndexMetadata.Builder index(String name) { - return IndexMetadata.builder(name).settings(indexSettings(IndexVersion.current(), 1, 0)); + private static IndexMetadata.Builder index(Index index) { + return IndexMetadata.builder(index.getName()).settings(indexSettings(IndexVersion.current(), index.getUUID(), 1, 0)); } private static Metadata metadata(IndexMetadata.Builder... indices) { diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/OperationRoutingTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/OperationRoutingTests.java index 2f90a72ce12cb..0849243bb98ff 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/OperationRoutingTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/OperationRoutingTests.java @@ -41,19 +41,18 @@ import static org.hamcrest.object.HasToString.hasToString; public class OperationRoutingTests extends ESTestCase { - public void testPreferNodes() throws InterruptedException, IOException { + public void testPreferNodes() throws IOException { TestThreadPool threadPool = null; ClusterService clusterService = null; try { threadPool = new TestThreadPool("testPreferNodes"); clusterService = ClusterServiceUtils.createClusterService(threadPool); final ProjectId projectId = randomProjectIdOrDefault(); - final String indexName = "test"; + final Index index = new Index("test", randomUUID()); ClusterServiceUtils.setState( clusterService, - ClusterStateCreationUtils.stateWithActivePrimary(projectId, indexName, true, randomInt(8)) + ClusterStateCreationUtils.stateWithActivePrimary(projectId, index, true, randomInt(8)) ); - final Index index = clusterService.state().metadata().getProject(projectId).index(indexName).getIndex(); final List shards = clusterService.state().getRoutingNodes().assignedShards(new ShardId(index, 0)); final int count = randomIntBetween(1, shards.size()); int position = 0; @@ -71,7 +70,7 @@ public void testPreferNodes() throws InterruptedException, IOException { final ShardIterator it = new OperationRouting( Settings.EMPTY, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS) - ).getShards(clusterService.state().projectState(projectId), indexName, 0, "_prefer_nodes:" + String.join(",", nodes)); + ).getShards(clusterService.state().projectState(projectId), index.getName(), 0, "_prefer_nodes:" + String.join(",", nodes)); final List all = new ArrayList<>(); ShardRouting shard; while ((shard = it.nextOrNull()) != null) { @@ -96,12 +95,11 @@ public void testPreferCombine() throws InterruptedException, IOException { threadPool = new TestThreadPool("testPreferCombine"); clusterService = ClusterServiceUtils.createClusterService(threadPool); final ProjectId projectId = randomProjectIdOrDefault(); - final String indexName = "test"; + final Index index = new Index("test", randomUUID()); ClusterServiceUtils.setState( clusterService, - ClusterStateCreationUtils.stateWithActivePrimary(projectId, indexName, true, randomInt(8)) + ClusterStateCreationUtils.stateWithActivePrimary(projectId, index, true, randomInt(8)) ); - final Index index = clusterService.state().metadata().getProject(projectId).index(indexName).getIndex(); final List shards = clusterService.state().getRoutingNodes().assignedShards(new ShardId(index, 0)); final ClusterState state = clusterService.state(); @@ -109,7 +107,7 @@ public void testPreferCombine() throws InterruptedException, IOException { final ShardIterator it = new OperationRouting( Settings.EMPTY, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS) - ).getShards(state.projectState(projectId), indexName, 0, prefer); + ).getShards(state.projectState(projectId), index.getName(), 0, prefer); final List all = new ArrayList<>(); ShardRouting shard; while (it != null && (shard = it.nextOrNull()) != null) { @@ -158,11 +156,13 @@ public void testFairSessionIdPreferences() throws InterruptedException, IOExcept final int numShards = randomIntBetween(2, 10); final int numReplicas = randomIntBetween(1, 3); final ProjectId projectId = randomProjectIdOrDefault(); + final Index[] indices = new Index[numIndices]; final String[] indexNames = new String[numIndices]; for (int i = 0; i < numIndices; i++) { - indexNames[i] = "test" + i; + indices[i] = new Index("test" + i, randomUUID()); + indexNames[i] = indices[i].getName(); } - ClusterState state = ClusterStateCreationUtils.stateWithAssignedPrimariesAndReplicas(projectId, indexNames, numShards, numReplicas); + ClusterState state = ClusterStateCreationUtils.stateWithAssignedPrimariesAndReplicas(projectId, indices, numShards, numReplicas); final int numRepeatedSearches = 4; List sessionsfirstSearch = null; OperationRouting opRouting = new OperationRouting( @@ -218,12 +218,11 @@ public void testThatOnlyNodesSupportNodeIds() throws InterruptedException, IOExc threadPool = new TestThreadPool("testThatOnlyNodesSupportNodeIds"); clusterService = ClusterServiceUtils.createClusterService(threadPool); final ProjectId projectId = randomProjectIdOrDefault(); - final String indexName = "test"; + final Index index = new Index("test", randomUUID()); ClusterServiceUtils.setState( clusterService, - ClusterStateCreationUtils.stateWithActivePrimary(projectId, indexName, true, randomInt(8)) + ClusterStateCreationUtils.stateWithActivePrimary(projectId, index, true, randomInt(8)) ); - final Index index = clusterService.state().metadata().getProject(projectId).index(indexName).getIndex(); final List shards = clusterService.state().getRoutingNodes().assignedShards(new ShardId(index, 0)); final int count = randomIntBetween(1, shards.size()); int position = 0; @@ -242,7 +241,7 @@ public void testThatOnlyNodesSupportNodeIds() throws InterruptedException, IOExc final ShardIterator it = new OperationRouting( Settings.EMPTY, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS) - ).getShards(clusterService.state().projectState(projectId), indexName, 0, "_only_nodes:" + String.join(",", nodes)); + ).getShards(clusterService.state().projectState(projectId), index.getName(), 0, "_only_nodes:" + String.join(",", nodes)); final List only = new ArrayList<>(); ShardRouting shard; while ((shard = it.nextOrNull()) != null) { @@ -256,7 +255,7 @@ public void testThatOnlyNodesSupportNodeIds() throws InterruptedException, IOExc () -> new OperationRouting( Settings.EMPTY, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS) - ).getShards(cs.state().projectState(projectId), indexName, 0, "_only_nodes:" + String.join(",", nodes)) + ).getShards(cs.state().projectState(projectId), index.getName(), 0, "_only_nodes:" + String.join(",", nodes)) ); if (nodes.size() == 1) { assertThat( @@ -285,11 +284,13 @@ public void testARSRanking() throws Exception { final int numShards = 1; final int numReplicas = 2; final ProjectId projectId = randomProjectIdOrDefault(); + final Index[] indices = new Index[numIndices]; final String[] indexNames = new String[numIndices]; for (int i = 0; i < numIndices; i++) { - indexNames[i] = "test" + i; + indices[i] = new Index("test" + i, randomUUID()); + indexNames[i] = indices[i].getName(); } - ClusterState state = ClusterStateCreationUtils.stateWithAssignedPrimariesAndReplicas(projectId, indexNames, numShards, numReplicas); + ClusterState state = ClusterStateCreationUtils.stateWithAssignedPrimariesAndReplicas(projectId, indices, numShards, numReplicas); ProjectState project = state.projectState(projectId); OperationRouting opRouting = new OperationRouting( Settings.EMPTY, @@ -365,12 +366,14 @@ public void testARSStatsAdjustment() throws Exception { int numShards = 1; int numReplicas = 1; ProjectId projectId = randomProjectIdOrDefault(); - String[] indexNames = new String[numIndices]; + final Index[] indices = new Index[numIndices]; + final String[] indexNames = new String[numIndices]; for (int i = 0; i < numIndices; i++) { - indexNames[i] = "test" + i; + indices[i] = new Index("test" + i, randomUUID()); + indexNames[i] = indices[i].getName(); } - ClusterState state = ClusterStateCreationUtils.stateWithAssignedPrimariesAndReplicas(projectId, indexNames, numShards, numReplicas); + ClusterState state = ClusterStateCreationUtils.stateWithAssignedPrimariesAndReplicas(projectId, indices, numShards, numReplicas); ProjectState project = state.projectState(projectId); OperationRouting opRouting = new OperationRouting( Settings.EMPTY, @@ -419,12 +422,14 @@ public void testARSOutstandingRequestTracking() throws Exception { int numShards = 2; int numReplicas = 1; ProjectId projectId = randomProjectIdOrDefault(); - String[] indexNames = new String[numIndices]; + final Index[] indices = new Index[numIndices]; + final String[] indexNames = new String[numIndices]; for (int i = 0; i < numIndices; i++) { - indexNames[i] = "test" + i; + indices[i] = new Index("test" + i, randomUUID()); + indexNames[i] = indices[i].getName(); } - ClusterState state = ClusterStateCreationUtils.stateWithAssignedPrimariesAndReplicas(projectId, indexNames, numShards, numReplicas); + ClusterState state = ClusterStateCreationUtils.stateWithAssignedPrimariesAndReplicas(projectId, indices, numShards, numReplicas); ProjectState project = state.projectState(projectId); OperationRouting opRouting = new OperationRouting( Settings.EMPTY, diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/PrimaryTermsTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/PrimaryTermsTests.java index 01966e40263a7..d541bef22a966 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/PrimaryTermsTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/PrimaryTermsTests.java @@ -42,9 +42,6 @@ public class PrimaryTermsTests extends ESAllocationTestCase { private static final String TEST_INDEX_2 = "test2"; private int numberOfShards; private int numberOfReplicas; - private static final Settings DEFAULT_SETTINGS = Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, IndexVersion.current()) - .build(); private AllocationService allocationService; private ClusterState clusterState; @@ -171,9 +168,9 @@ private void addNodes() { private IndexMetadata.Builder createIndexMetadata(String indexName) { primaryTermsPerIndex.put(indexName, new long[numberOfShards]); - final IndexMetadata.Builder builder = new IndexMetadata.Builder(indexName).settings(DEFAULT_SETTINGS) - .numberOfReplicas(this.numberOfReplicas) - .numberOfShards(this.numberOfShards); + final IndexMetadata.Builder builder = new IndexMetadata.Builder(indexName).settings( + indexSettings(IndexVersion.current(), randomUUID(), this.numberOfShards, this.numberOfReplicas) + ); for (int i = 0; i < numberOfShards; i++) { builder.primaryTerm(i, randomInt(200)); primaryTermsPerIndex.get(indexName)[i] = builder.primaryTerm(i); diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/RoutingNodeTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/RoutingNodeTests.java index 9ded7670e95d7..3e5da23e444d4 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/RoutingNodeTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/RoutingNodeTests.java @@ -16,6 +16,9 @@ import org.elasticsearch.index.Index; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.test.ESTestCase; +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeMatcher; import java.net.InetAddress; import java.util.Arrays; @@ -24,15 +27,25 @@ import static java.util.Collections.emptyMap; import static java.util.Collections.emptySet; +import static org.elasticsearch.cluster.routing.RoutingNodeTests.ParsedShardIdMatcher.equalsToParsedShardId; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.nullValue; public class RoutingNodeTests extends ESTestCase { - private ShardRouting unassignedShard0 = TestShardRouting.newShardRouting("test", 0, "node-1", false, ShardRoutingState.STARTED); - private ShardRouting initializingShard0 = TestShardRouting.newShardRouting("test", 1, "node-1", false, ShardRoutingState.INITIALIZING); - private ShardRouting relocatingShard0 = TestShardRouting.newShardRouting( - "test", + private final Index index = new Index("test", randomUUID()); + private final ShardRouting unassignedShard0 = TestShardRouting.newShardRouting(index, 0, "node-1", false, ShardRoutingState.STARTED); + private final ShardRouting initializingShard0 = TestShardRouting.newShardRouting( + index, + 1, + "node-1", + false, + ShardRoutingState.INITIALIZING + ); + private final ShardRouting relocatingShard0 = TestShardRouting.newShardRouting( + index, 2, "node-1", "node-2", @@ -51,49 +64,38 @@ public void setUp() throws Exception { } public void testAdd() { - ShardRouting initializingShard1 = TestShardRouting.newShardRouting("test", 3, "node-1", false, ShardRoutingState.INITIALIZING); - ShardRouting relocatingShard0 = TestShardRouting.newShardRouting( - "test", - 4, - "node-1", - "node-2", - false, - ShardRoutingState.RELOCATING - ); + ShardId shard3Id = new ShardId(index, 3); + ShardId shard4Id = new ShardId(index, 4); + ShardRouting initializingShard1 = TestShardRouting.newShardRouting(shard3Id, "node-1", false, ShardRoutingState.INITIALIZING); + ShardRouting relocatingShard0 = TestShardRouting.newShardRouting(shard4Id, "node-1", "node-2", false, ShardRoutingState.RELOCATING); routingNode.add(initializingShard1); routingNode.add(relocatingShard0); - assertThat(routingNode.getByShardId(new ShardId("test", IndexMetadata.INDEX_UUID_NA_VALUE, 3)), equalTo(initializingShard1)); - assertThat(routingNode.getByShardId(new ShardId("test", IndexMetadata.INDEX_UUID_NA_VALUE, 4)), equalTo(relocatingShard0)); + assertThat(routingNode.getByShardId(shard3Id), equalTo(initializingShard1)); + assertThat(routingNode.getByShardId(shard4Id), equalTo(relocatingShard0)); } public void testUpdate() { - ShardRouting startedShard0 = TestShardRouting.newShardRouting("test", 0, "node-1", false, ShardRoutingState.STARTED); - ShardRouting startedShard1 = TestShardRouting.newShardRouting("test", 1, "node-1", "node-2", false, ShardRoutingState.RELOCATING); - ShardRouting startedShard2 = TestShardRouting.newShardRouting("test", 2, "node-1", false, ShardRoutingState.INITIALIZING); + ShardId shard0Id = new ShardId(index, 0); + ShardId shard1Id = new ShardId(index, 1); + ShardId shard2Id = new ShardId(index, 2); + ShardRouting startedShard0 = TestShardRouting.newShardRouting(shard0Id, "node-1", false, ShardRoutingState.STARTED); + ShardRouting startedShard1 = TestShardRouting.newShardRouting(shard1Id, "node-1", "node-2", false, ShardRoutingState.RELOCATING); + ShardRouting startedShard2 = TestShardRouting.newShardRouting(shard2Id, "node-1", false, ShardRoutingState.INITIALIZING); routingNode.update(unassignedShard0, startedShard0); routingNode.update(initializingShard0, startedShard1); routingNode.update(relocatingShard0, startedShard2); - assertThat( - routingNode.getByShardId(new ShardId("test", IndexMetadata.INDEX_UUID_NA_VALUE, 0)).state(), - equalTo(ShardRoutingState.STARTED) - ); - assertThat( - routingNode.getByShardId(new ShardId("test", IndexMetadata.INDEX_UUID_NA_VALUE, 1)).state(), - equalTo(ShardRoutingState.RELOCATING) - ); - assertThat( - routingNode.getByShardId(new ShardId("test", IndexMetadata.INDEX_UUID_NA_VALUE, 2)).state(), - equalTo(ShardRoutingState.INITIALIZING) - ); + assertThat(routingNode.getByShardId(shard0Id).state(), equalTo(ShardRoutingState.STARTED)); + assertThat(routingNode.getByShardId(shard1Id).state(), equalTo(ShardRoutingState.RELOCATING)); + assertThat(routingNode.getByShardId(shard2Id).state(), equalTo(ShardRoutingState.INITIALIZING)); } public void testRemove() { routingNode.remove(unassignedShard0); routingNode.remove(initializingShard0); routingNode.remove(relocatingShard0); - assertThat(routingNode.getByShardId(new ShardId("test", IndexMetadata.INDEX_UUID_NA_VALUE, 0)), is(nullValue())); - assertThat(routingNode.getByShardId(new ShardId("test", IndexMetadata.INDEX_UUID_NA_VALUE, 1)), is(nullValue())); - assertThat(routingNode.getByShardId(new ShardId("test", IndexMetadata.INDEX_UUID_NA_VALUE, 2)), is(nullValue())); + assertThat(routingNode.getByShardId(new ShardId(index, 0)), is(nullValue())); + assertThat(routingNode.getByShardId(new ShardId(index, 1)), is(nullValue())); + assertThat(routingNode.getByShardId(new ShardId(index, 2)), is(nullValue())); } public void testNumberOfShardsWithState() { @@ -109,10 +111,13 @@ public void testShardsWithState() { } public void testShardsWithStateInIndex() { - assertThat(routingNode.shardsWithState("test", ShardRoutingState.INITIALIZING, ShardRoutingState.STARTED).count(), equalTo(2L)); - assertThat(routingNode.shardsWithState("test", ShardRoutingState.STARTED).count(), equalTo(1L)); - assertThat(routingNode.shardsWithState("test", ShardRoutingState.RELOCATING).count(), equalTo(1L)); - assertThat(routingNode.shardsWithState("test", ShardRoutingState.INITIALIZING).count(), equalTo(1L)); + assertThat( + routingNode.shardsWithState(index.getName(), ShardRoutingState.INITIALIZING, ShardRoutingState.STARTED).count(), + equalTo(2L) + ); + assertThat(routingNode.shardsWithState(index.getName(), ShardRoutingState.STARTED).count(), equalTo(1L)); + assertThat(routingNode.shardsWithState(index.getName(), ShardRoutingState.RELOCATING).count(), equalTo(1L)); + assertThat(routingNode.shardsWithState(index.getName(), ShardRoutingState.INITIALIZING).count(), equalTo(1L)); } public void testNumberOfOwningShards() { @@ -120,9 +125,11 @@ public void testNumberOfOwningShards() { } public void testNumberOfOwningShardsForIndex() { - final ShardRouting test1Shard0 = TestShardRouting.newShardRouting("test1", 0, "node-1", false, ShardRoutingState.STARTED); + Index index1 = new Index("test1", randomUUID()); + Index index2 = new Index("test2", randomUUID()); + final ShardRouting test1Shard0 = TestShardRouting.newShardRouting(index1, 0, "node-1", false, ShardRoutingState.STARTED); final ShardRouting test2Shard0 = TestShardRouting.newShardRouting( - "test2", + index2, 0, "node-1", "node-2", @@ -131,22 +138,32 @@ public void testNumberOfOwningShardsForIndex() { ); routingNode.add(test1Shard0); routingNode.add(test2Shard0); - assertThat(routingNode.numberOfOwningShardsForIndex(new Index("test", IndexMetadata.INDEX_UUID_NA_VALUE)), equalTo(2)); - assertThat(routingNode.numberOfOwningShardsForIndex(new Index("test1", IndexMetadata.INDEX_UUID_NA_VALUE)), equalTo(1)); - assertThat(routingNode.numberOfOwningShardsForIndex(new Index("test2", IndexMetadata.INDEX_UUID_NA_VALUE)), equalTo(0)); + assertThat(routingNode.numberOfOwningShardsForIndex(index), equalTo(2)); + assertThat(routingNode.numberOfOwningShardsForIndex(index1), equalTo(1)); + assertThat(routingNode.numberOfOwningShardsForIndex(index2), equalTo(0)); assertThat(routingNode.numberOfOwningShardsForIndex(new Index("test3", IndexMetadata.INDEX_UUID_NA_VALUE)), equalTo(0)); } public void testReturnStartedShards() { - assertThat(startedShardsSet(routingNode), equalTo(Set.of(ShardId.fromString("[test][0]")))); + Matcher shard0Matcher = equalsToParsedShardId("[test][0]"); + assertThat(startedShardsSet(routingNode), contains(shard0Matcher)); - ShardRouting startedShard = TestShardRouting.newShardRouting("test1", 1, "node-1", false, ShardRoutingState.STARTED); + ShardRouting anotherStartedShard = TestShardRouting.newShardRouting( + "test1", + randomUUID(), + 1, + "node-1", + false, + ShardRoutingState.STARTED + ); - routingNode.add(startedShard); - assertThat(startedShardsSet(routingNode), equalTo(Set.of(ShardId.fromString("[test][0]"), ShardId.fromString("[test1][1]")))); + routingNode.add(anotherStartedShard); + Matcher shard1Matcher = equalsToParsedShardId("[test1][1]"); + assertThat(startedShardsSet(routingNode), containsInAnyOrder(shard0Matcher, shard1Matcher)); ShardRouting relocatingShard = TestShardRouting.newShardRouting( "test2", + IndexMetadata.INDEX_UUID_NA_VALUE, 2, "node-1", "node-2", @@ -154,14 +171,40 @@ public void testReturnStartedShards() { ShardRoutingState.RELOCATING ); routingNode.add(relocatingShard); - assertThat(startedShardsSet(routingNode), equalTo(Set.of(ShardId.fromString("[test][0]"), ShardId.fromString("[test1][1]")))); + assertThat(startedShardsSet(routingNode), containsInAnyOrder(shard0Matcher, shard1Matcher)); - routingNode.remove(startedShard); - assertThat(startedShardsSet(routingNode), equalTo(Set.of(ShardId.fromString("[test][0]")))); + routingNode.remove(anotherStartedShard); + assertThat(startedShardsSet(routingNode), containsInAnyOrder(shard0Matcher)); } private static Set startedShardsSet(RoutingNode routingNode) { return Arrays.stream(routingNode.started()).map(ShardRouting::shardId).collect(Collectors.toSet()); } + /** + * When parsing from string, we lose uuid information, this is why we do not check shard id equality. + */ + static class ParsedShardIdMatcher extends TypeSafeMatcher { + + private final ShardId expectedShardId; + + private ParsedShardIdMatcher(ShardId expectedShardId) { + this.expectedShardId = expectedShardId; + } + + @Override + protected boolean matchesSafely(ShardId shardId) { + return expectedShardId.getIndexName().equals(shardId.getIndexName()) && expectedShardId.getId() == shardId.getId(); + } + + @Override + public void describeTo(Description description) { + description.appendText("equals to " + expectedShardId.toString()); + } + + static Matcher equalsToParsedShardId(String shardId) { + return new ParsedShardIdMatcher(ShardId.fromString(shardId)); + } + } + } diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/RoutingTableGenerator.java b/server/src/test/java/org/elasticsearch/cluster/routing/RoutingTableGenerator.java index 39c4ec00f3180..e971bfe5f0a52 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/RoutingTableGenerator.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/RoutingTableGenerator.java @@ -13,6 +13,7 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.cluster.health.ClusterHealthStatus; import org.elasticsearch.cluster.metadata.IndexMetadata; +import org.elasticsearch.index.Index; import org.elasticsearch.index.shard.ShardId; import static org.elasticsearch.cluster.health.ClusterShardHealth.getInactivePrimaryHealth; @@ -20,7 +21,7 @@ public class RoutingTableGenerator { private static int node_id = 1; - private ShardRouting genShardRouting(String index, int shardId, boolean primary) { + private ShardRouting genShardRouting(Index index, int shardId, boolean primary) { ShardRoutingState state; @@ -34,19 +35,11 @@ private ShardRouting genShardRouting(String index, int shardId, boolean primary) } return switch (state) { - case STARTED -> TestShardRouting.newShardRouting( - index, - shardId, - "node_" + (node_id++), - null, - primary, - ShardRoutingState.STARTED - ); + case STARTED -> TestShardRouting.newShardRouting(index, shardId, "node_" + (node_id++), primary, ShardRoutingState.STARTED); case INITIALIZING -> TestShardRouting.newShardRouting( index, shardId, "node_" + (node_id++), - null, primary, ShardRoutingState.INITIALIZING ); @@ -64,8 +57,8 @@ private ShardRouting genShardRouting(String index, int shardId, boolean primary) } public IndexShardRoutingTable.Builder genShardRoutingTable(IndexMetadata indexMetadata, int shardId, ShardCounter counter) { - final String index = indexMetadata.getIndex().getName(); - IndexShardRoutingTable.Builder builder = new IndexShardRoutingTable.Builder(new ShardId(index, "_na_", shardId)); + final Index index = indexMetadata.getIndex(); + IndexShardRoutingTable.Builder builder = new IndexShardRoutingTable.Builder(new ShardId(index, shardId)); final ShardRouting primary = genShardRouting(index, shardId, true); counter.update(primary); builder.addShard(primary); diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/RoutingTableTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/RoutingTableTests.java index 1393be120f9ac..5320825f0111e 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/RoutingTableTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/RoutingTableTests.java @@ -49,16 +49,13 @@ public class RoutingTableTests extends ESAllocationTestCase { - private static final String TEST_INDEX_1 = "test1"; - private static final String TEST_INDEX_2 = "test2"; + private final Index testIndex1 = new Index("test1", randomUUID()); + private final Index testIndex2 = new Index("test2", randomUUID()); private RoutingTable emptyRoutingTable; private int numberOfShards; private int numberOfReplicas; private int shardsPerIndex; private int totalNumberOfShards; - private static final Settings DEFAULT_SETTINGS = Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, IndexVersion.current()) - .build(); private final AllocationService ALLOCATION_SERVICE = createAllocationService( Settings.builder() .put("cluster.routing.allocation.node_concurrent_recoveries", Integer.MAX_VALUE) // don't limit recoveries @@ -77,19 +74,17 @@ public void setUp() throws Exception { this.totalNumberOfShards = this.shardsPerIndex * 2; logger.info("Setup test with {} shards and {} replicas.", this.numberOfShards, this.numberOfReplicas); this.emptyRoutingTable = new RoutingTable.Builder().build(); - Metadata metadata = Metadata.builder().put(createIndexMetadata(TEST_INDEX_1)).put(createIndexMetadata(TEST_INDEX_2)).build(); + Metadata metadata = Metadata.builder().put(createIndexMetadata(testIndex1)).put(createIndexMetadata(testIndex2)).build(); RoutingTable testRoutingTable = new RoutingTable.Builder().add( - new IndexRoutingTable.Builder( - TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY, - metadata.getProject().index(TEST_INDEX_1).getIndex() - ).initializeAsNew(metadata.getProject().index(TEST_INDEX_1)).build() + new IndexRoutingTable.Builder(TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY, testIndex1).initializeAsNew( + metadata.getProject().index(testIndex1) + ).build() ) .add( - new IndexRoutingTable.Builder( - TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY, - metadata.getProject().index(TEST_INDEX_2).getIndex() - ).initializeAsNew(metadata.getProject().index(TEST_INDEX_2)).build() + new IndexRoutingTable.Builder(TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY, testIndex2).initializeAsNew( + metadata.getProject().index(testIndex2) + ).build() ) .build(); this.clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(metadata).routingTable(testRoutingTable).build(); @@ -115,17 +110,17 @@ private void startInitializingShards(String index) { clusterState = startInitializingShardsAndReroute(ALLOCATION_SERVICE, clusterState, index); } - private IndexMetadata.Builder createIndexMetadata(String indexName) { - return new IndexMetadata.Builder(indexName).settings(DEFAULT_SETTINGS) - .numberOfReplicas(this.numberOfReplicas) - .numberOfShards(this.numberOfShards); + private IndexMetadata.Builder createIndexMetadata(Index index) { + return new IndexMetadata.Builder(index.getName()).settings( + indexSettings(IndexVersion.current(), index.getUUID(), this.numberOfShards, this.numberOfReplicas) + ); } public void testAllShards() { assertThat(this.emptyRoutingTable.allShards().count(), is(0L)); assertThat(this.clusterState.routingTable().allShards().count(), is((long) this.totalNumberOfShards)); - assertThat(this.clusterState.routingTable().allShards(TEST_INDEX_1).size(), is(this.shardsPerIndex)); + assertThat(this.clusterState.routingTable().allShards(testIndex1.getName()).size(), is(this.shardsPerIndex)); try { assertThat(this.clusterState.routingTable().allShards("not_existing").size(), is(0)); fail("Exception expected when calling allShards() with non existing index name"); @@ -135,13 +130,14 @@ public void testAllShards() { } public void testHasIndex() { - assertThat(clusterState.routingTable().hasIndex(TEST_INDEX_1), is(true)); + assertThat(clusterState.routingTable().hasIndex(testIndex1), is(true)); assertThat(clusterState.routingTable().hasIndex("foobar"), is(false)); } public void testIndex() { - assertThat(clusterState.routingTable().index(TEST_INDEX_1).getIndex().getName(), is(TEST_INDEX_1)); - assertThat(clusterState.routingTable().index(new Index(TEST_INDEX_1, UUIDs.randomBase64UUID())), is(nullValue())); + assertThat(clusterState.routingTable().index(testIndex1).getIndex(), is(testIndex1)); + Index otherUuid = new Index(testIndex1.getName(), randomValueOtherThan(testIndex1.getUUID(), UUIDs::randomBase64UUID)); + assertThat(clusterState.routingTable().index(otherUuid), is(nullValue())); assertThat(clusterState.routingTable().index("foobar"), is(nullValue())); } @@ -161,7 +157,7 @@ public void testShardsWithState() { ); assertThat(shardsWithState(clusterState.getRoutingNodes(), ShardRoutingState.INITIALIZING).size(), is(2 * this.numberOfShards)); - startInitializingShards(TEST_INDEX_1); + startInitializingShards(testIndex1.getName()); assertThat(shardsWithState(clusterState.getRoutingNodes(), ShardRoutingState.STARTED).size(), is(this.numberOfShards)); int initializingExpected = this.numberOfShards + this.numberOfShards * this.numberOfReplicas; assertThat(shardsWithState(clusterState.getRoutingNodes(), ShardRoutingState.INITIALIZING).size(), is(initializingExpected)); @@ -170,7 +166,7 @@ public void testShardsWithState() { is(this.totalNumberOfShards - initializingExpected - this.numberOfShards) ); - startInitializingShards(TEST_INDEX_2); + startInitializingShards(testIndex2.getName()); assertThat(shardsWithState(clusterState.getRoutingNodes(), ShardRoutingState.STARTED).size(), is(2 * this.numberOfShards)); initializingExpected = 2 * this.numberOfShards * this.numberOfReplicas; assertThat(shardsWithState(clusterState.getRoutingNodes(), ShardRoutingState.INITIALIZING).size(), is(initializingExpected)); @@ -180,8 +176,8 @@ public void testShardsWithState() { ); // now start all replicas too - startInitializingShards(TEST_INDEX_1); - startInitializingShards(TEST_INDEX_2); + startInitializingShards(testIndex1.getName()); + startInitializingShards(testIndex2.getName()); assertThat(shardsWithState(clusterState.getRoutingNodes(), ShardRoutingState.STARTED).size(), is(this.totalNumberOfShards)); } @@ -189,49 +185,55 @@ public void testActivePrimaryShardsGrouped() { assertThat(this.emptyRoutingTable.activePrimaryShardsGrouped(new String[0], true).size(), is(0)); assertThat(this.emptyRoutingTable.activePrimaryShardsGrouped(new String[0], false).size(), is(0)); - assertThat(clusterState.routingTable().activePrimaryShardsGrouped(new String[] { TEST_INDEX_1 }, false).size(), is(0)); + assertThat(clusterState.routingTable().activePrimaryShardsGrouped(new String[] { testIndex1.getName() }, false).size(), is(0)); assertThat( - clusterState.routingTable().activePrimaryShardsGrouped(new String[] { TEST_INDEX_1 }, true).size(), + clusterState.routingTable().activePrimaryShardsGrouped(new String[] { testIndex1.getName() }, true).size(), is(this.numberOfShards) ); initPrimaries(); - assertThat(clusterState.routingTable().activePrimaryShardsGrouped(new String[] { TEST_INDEX_1 }, false).size(), is(0)); + assertThat(clusterState.routingTable().activePrimaryShardsGrouped(new String[] { testIndex1.getName() }, false).size(), is(0)); assertThat( - clusterState.routingTable().activePrimaryShardsGrouped(new String[] { TEST_INDEX_1 }, true).size(), + clusterState.routingTable().activePrimaryShardsGrouped(new String[] { testIndex1.getName() }, true).size(), is(this.numberOfShards) ); - startInitializingShards(TEST_INDEX_1); + startInitializingShards(testIndex1.getName()); assertThat( - clusterState.routingTable().activePrimaryShardsGrouped(new String[] { TEST_INDEX_1 }, false).size(), + clusterState.routingTable().activePrimaryShardsGrouped(new String[] { testIndex1.getName() }, false).size(), is(this.numberOfShards) ); assertThat( - clusterState.routingTable().activePrimaryShardsGrouped(new String[] { TEST_INDEX_1, TEST_INDEX_2 }, false).size(), + clusterState.routingTable() + .activePrimaryShardsGrouped(new String[] { testIndex1.getName(), testIndex2.getName() }, false) + .size(), is(this.numberOfShards) ); assertThat( - clusterState.routingTable().activePrimaryShardsGrouped(new String[] { TEST_INDEX_1 }, true).size(), + clusterState.routingTable().activePrimaryShardsGrouped(new String[] { testIndex1.getName() }, true).size(), is(this.numberOfShards) ); - startInitializingShards(TEST_INDEX_2); + startInitializingShards(testIndex2.getName()); assertThat( - clusterState.routingTable().activePrimaryShardsGrouped(new String[] { TEST_INDEX_2 }, false).size(), + clusterState.routingTable().activePrimaryShardsGrouped(new String[] { testIndex2.getName() }, false).size(), is(this.numberOfShards) ); assertThat( - clusterState.routingTable().activePrimaryShardsGrouped(new String[] { TEST_INDEX_1, TEST_INDEX_2 }, false).size(), + clusterState.routingTable() + .activePrimaryShardsGrouped(new String[] { testIndex1.getName(), testIndex2.getName() }, false) + .size(), is(2 * this.numberOfShards) ); assertThat( - clusterState.routingTable().activePrimaryShardsGrouped(new String[] { TEST_INDEX_1, TEST_INDEX_2 }, true).size(), + clusterState.routingTable() + .activePrimaryShardsGrouped(new String[] { testIndex1.getName(), testIndex2.getName() }, true) + .size(), is(2 * this.numberOfShards) ); try { - clusterState.routingTable().activePrimaryShardsGrouped(new String[] { TEST_INDEX_1, "not_exists" }, true); + clusterState.routingTable().activePrimaryShardsGrouped(new String[] { testIndex1.getName(), "not_exists" }, true); fail("Calling with non-existing index name should raise IndexMissingException"); } catch (IndexNotFoundException e) { // expected @@ -242,73 +244,82 @@ public void testAllActiveShardsGrouped() { assertThat(this.emptyRoutingTable.allActiveShardsGrouped(new String[0], true).size(), is(0)); assertThat(this.emptyRoutingTable.allActiveShardsGrouped(new String[0], false).size(), is(0)); - assertThat(clusterState.routingTable().allActiveShardsGrouped(new String[] { TEST_INDEX_1 }, false).size(), is(0)); - assertThat(clusterState.routingTable().allActiveShardsGrouped(new String[] { TEST_INDEX_1 }, true).size(), is(this.shardsPerIndex)); + assertThat(clusterState.routingTable().allActiveShardsGrouped(new String[] { testIndex1.getName() }, false).size(), is(0)); + assertThat( + clusterState.routingTable().allActiveShardsGrouped(new String[] { testIndex1.getName() }, true).size(), + is(this.shardsPerIndex) + ); initPrimaries(); - assertThat(clusterState.routingTable().allActiveShardsGrouped(new String[] { TEST_INDEX_1 }, false).size(), is(0)); - assertThat(clusterState.routingTable().allActiveShardsGrouped(new String[] { TEST_INDEX_1 }, true).size(), is(this.shardsPerIndex)); + assertThat(clusterState.routingTable().allActiveShardsGrouped(new String[] { testIndex1.getName() }, false).size(), is(0)); + assertThat( + clusterState.routingTable().allActiveShardsGrouped(new String[] { testIndex1.getName() }, true).size(), + is(this.shardsPerIndex) + ); - startInitializingShards(TEST_INDEX_1); + startInitializingShards(testIndex1.getName()); assertThat( - clusterState.routingTable().allActiveShardsGrouped(new String[] { TEST_INDEX_1 }, false).size(), + clusterState.routingTable().allActiveShardsGrouped(new String[] { testIndex1.getName() }, false).size(), is(this.numberOfShards) ); assertThat( - clusterState.routingTable().allActiveShardsGrouped(new String[] { TEST_INDEX_1, TEST_INDEX_2 }, false).size(), + clusterState.routingTable().allActiveShardsGrouped(new String[] { testIndex1.getName(), testIndex2.getName() }, false).size(), is(this.numberOfShards) ); - assertThat(clusterState.routingTable().allActiveShardsGrouped(new String[] { TEST_INDEX_1 }, true).size(), is(this.shardsPerIndex)); + assertThat( + clusterState.routingTable().allActiveShardsGrouped(new String[] { testIndex1.getName() }, true).size(), + is(this.shardsPerIndex) + ); - startInitializingShards(TEST_INDEX_2); + startInitializingShards(testIndex2.getName()); assertThat( - clusterState.routingTable().allActiveShardsGrouped(new String[] { TEST_INDEX_2 }, false).size(), + clusterState.routingTable().allActiveShardsGrouped(new String[] { testIndex2.getName() }, false).size(), is(this.numberOfShards) ); assertThat( - clusterState.routingTable().allActiveShardsGrouped(new String[] { TEST_INDEX_1, TEST_INDEX_2 }, false).size(), + clusterState.routingTable().allActiveShardsGrouped(new String[] { testIndex1.getName(), testIndex2.getName() }, false).size(), is(2 * this.numberOfShards) ); assertThat( - clusterState.routingTable().allActiveShardsGrouped(new String[] { TEST_INDEX_1, TEST_INDEX_2 }, true).size(), + clusterState.routingTable().allActiveShardsGrouped(new String[] { testIndex1.getName(), testIndex2.getName() }, true).size(), is(this.totalNumberOfShards) ); try { - clusterState.routingTable().allActiveShardsGrouped(new String[] { TEST_INDEX_1, "not_exists" }, true); + clusterState.routingTable().allActiveShardsGrouped(new String[] { testIndex1.getName(), "not_exists" }, true); } catch (IndexNotFoundException e) { fail("Calling with non-existing index should be ignored at the moment"); } } public void testAllAssignedShardsGrouped() { - assertThat(clusterState.routingTable().allAssignedShardsGrouped(new String[] { TEST_INDEX_1 }, false).size(), is(0)); + assertThat(clusterState.routingTable().allAssignedShardsGrouped(new String[] { testIndex1.getName() }, false).size(), is(0)); assertThat( - clusterState.routingTable().allAssignedShardsGrouped(new String[] { TEST_INDEX_1 }, true).size(), + clusterState.routingTable().allAssignedShardsGrouped(new String[] { testIndex1.getName() }, true).size(), is(this.shardsPerIndex) ); initPrimaries(); assertThat( - clusterState.routingTable().allAssignedShardsGrouped(new String[] { TEST_INDEX_1 }, false).size(), + clusterState.routingTable().allAssignedShardsGrouped(new String[] { testIndex1.getName() }, false).size(), is(this.numberOfShards) ); assertThat( - clusterState.routingTable().allAssignedShardsGrouped(new String[] { TEST_INDEX_1 }, true).size(), + clusterState.routingTable().allAssignedShardsGrouped(new String[] { testIndex1.getName() }, true).size(), is(this.shardsPerIndex) ); assertThat( - clusterState.routingTable().allAssignedShardsGrouped(new String[] { TEST_INDEX_1, TEST_INDEX_2 }, false).size(), + clusterState.routingTable().allAssignedShardsGrouped(new String[] { testIndex1.getName(), testIndex2.getName() }, false).size(), is(2 * this.numberOfShards) ); assertThat( - clusterState.routingTable().allAssignedShardsGrouped(new String[] { TEST_INDEX_1, TEST_INDEX_2 }, true).size(), + clusterState.routingTable().allAssignedShardsGrouped(new String[] { testIndex1.getName(), testIndex2.getName() }, true).size(), is(this.totalNumberOfShards) ); try { - clusterState.routingTable().allAssignedShardsGrouped(new String[] { TEST_INDEX_1, "not_exists" }, false); + clusterState.routingTable().allAssignedShardsGrouped(new String[] { testIndex1.getName(), "not_exists" }, false); } catch (IndexNotFoundException e) { fail("Calling with non-existing index should be ignored at the moment"); } @@ -317,22 +328,31 @@ public void testAllAssignedShardsGrouped() { public void testAllShardsForMultipleIndices() { assertThat(this.emptyRoutingTable.allShards(new String[0]).getShardRoutings().size(), is(0)); - assertThat(clusterState.routingTable().allShards(new String[] { TEST_INDEX_1 }).getShardRoutings().size(), is(this.shardsPerIndex)); + assertThat( + clusterState.routingTable().allShards(new String[] { testIndex1.getName() }).getShardRoutings().size(), + is(this.shardsPerIndex) + ); initPrimaries(); - assertThat(clusterState.routingTable().allShards(new String[] { TEST_INDEX_1 }).getShardRoutings().size(), is(this.shardsPerIndex)); + assertThat( + clusterState.routingTable().allShards(new String[] { testIndex1.getName() }).getShardRoutings().size(), + is(this.shardsPerIndex) + ); - startInitializingShards(TEST_INDEX_1); - assertThat(clusterState.routingTable().allShards(new String[] { TEST_INDEX_1 }).getShardRoutings().size(), is(this.shardsPerIndex)); + startInitializingShards(testIndex1.getName()); + assertThat( + clusterState.routingTable().allShards(new String[] { testIndex1.getName() }).getShardRoutings().size(), + is(this.shardsPerIndex) + ); - startInitializingShards(TEST_INDEX_2); + startInitializingShards(testIndex2.getName()); assertThat( - clusterState.routingTable().allShards(new String[] { TEST_INDEX_1, TEST_INDEX_2 }).getShardRoutings().size(), + clusterState.routingTable().allShards(new String[] { testIndex1.getName(), testIndex2.getName() }).getShardRoutings().size(), is(this.totalNumberOfShards) ); try { - clusterState.routingTable().allShards(new String[] { TEST_INDEX_1, "not_exists" }); + clusterState.routingTable().allShards(new String[] { testIndex1.getName(), "not_exists" }); } catch (IndexNotFoundException e) { fail("Calling with non-existing index should be ignored at the moment"); } @@ -426,24 +446,24 @@ public void testDistinctNodes() { public void testAddAsRecovery() { { - final IndexMetadata indexMetadata = createIndexMetadata(TEST_INDEX_1).state(IndexMetadata.State.OPEN).build(); + final IndexMetadata indexMetadata = createIndexMetadata(testIndex1).state(IndexMetadata.State.OPEN).build(); final RoutingTable routingTable = new RoutingTable.Builder(TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY).addAsRecovery( indexMetadata ).build(); - assertThat(routingTable.hasIndex(TEST_INDEX_1), is(true)); - assertThat(routingTable.allShards(TEST_INDEX_1).size(), is(this.shardsPerIndex)); - assertThat(routingTable.index(TEST_INDEX_1).shardsWithState(UNASSIGNED).size(), is(this.shardsPerIndex)); + assertThat(routingTable.hasIndex(testIndex1), is(true)); + assertThat(routingTable.allShards(testIndex1.getName()).size(), is(this.shardsPerIndex)); + assertThat(routingTable.index(testIndex1).shardsWithState(UNASSIGNED).size(), is(this.shardsPerIndex)); } { - final IndexMetadata indexMetadata = createIndexMetadata(TEST_INDEX_1).state(IndexMetadata.State.CLOSE).build(); + final IndexMetadata indexMetadata = createIndexMetadata(testIndex1).state(IndexMetadata.State.CLOSE).build(); final RoutingTable routingTable = new RoutingTable.Builder(TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY).addAsRecovery( indexMetadata ).build(); - assertThat(routingTable.hasIndex(TEST_INDEX_1), is(false)); - expectThrows(IndexNotFoundException.class, () -> routingTable.allShards(TEST_INDEX_1)); + assertThat(routingTable.hasIndex(testIndex1), is(false)); + expectThrows(IndexNotFoundException.class, () -> routingTable.allShards(testIndex1.getName())); } { - final IndexMetadata indexMetadata = createIndexMetadata(TEST_INDEX_1).build(); + final IndexMetadata indexMetadata = createIndexMetadata(testIndex1).build(); final IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(indexMetadata) .state(IndexMetadata.State.CLOSE) .settings( @@ -456,9 +476,9 @@ public void testAddAsRecovery() { final RoutingTable routingTable = new RoutingTable.Builder(TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY).addAsRecovery( indexMetadataBuilder.build() ).build(); - assertThat(routingTable.hasIndex(TEST_INDEX_1), is(true)); - assertThat(routingTable.allShards(TEST_INDEX_1).size(), is(this.shardsPerIndex)); - assertThat(routingTable.index(TEST_INDEX_1).shardsWithState(UNASSIGNED).size(), is(this.shardsPerIndex)); + assertThat(routingTable.hasIndex(testIndex1), is(true)); + assertThat(routingTable.allShards(testIndex1.getName()).size(), is(this.shardsPerIndex)); + assertThat(routingTable.index(testIndex1).shardsWithState(UNASSIGNED).size(), is(this.shardsPerIndex)); } } diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/ShardMovementWriteLoadSimulatorTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/ShardMovementWriteLoadSimulatorTests.java index 59a37747ae2fd..be09b63c860d1 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/ShardMovementWriteLoadSimulatorTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/ShardMovementWriteLoadSimulatorTests.java @@ -16,6 +16,7 @@ import org.elasticsearch.cluster.metadata.ProjectId; import org.elasticsearch.cluster.routing.allocation.RoutingAllocation; import org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders; +import org.elasticsearch.index.Index; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.snapshots.SnapshotShardSizeInfo; import org.elasticsearch.test.ESTestCase; @@ -39,7 +40,10 @@ public class ShardMovementWriteLoadSimulatorTests extends ESTestCase { private static final RoutingChangesObserver NOOP = new RoutingChangesObserver() { }; - private static final String[] INDICES = { "indexOne", "indexTwo", "indexThree" }; + private final Index[] indices = { + new Index("indexOne", randomUUID()), + new Index("indexTwo", randomUUID()), + new Index("indexThree", randomUUID()) }; /** * We should not adjust the values if there's no movement @@ -131,7 +135,7 @@ public void testMovementBetweenNodesWithNoThreadPoolAndWriteLoadStats() { final var allocation = createRoutingAllocationWithRandomisedWriteLoads( originalNode0ThreadPoolStats, originalNode1ThreadPoolStats, - new HashSet<>(randomSubsetOf(Arrays.asList(INDICES))) + new HashSet<>(randomSubsetOf(Arrays.asList(indices))) ); final var shardMovementWriteLoadSimulator = new ShardMovementWriteLoadSimulator(allocation); @@ -164,7 +168,7 @@ private NodeUsageStatsForThreadPools.ThreadPoolUsageStats randomThreadPoolUsageS private RoutingAllocation createRoutingAllocationWithRandomisedWriteLoads( NodeUsageStatsForThreadPools.ThreadPoolUsageStats node0ThreadPoolStats, NodeUsageStatsForThreadPools.ThreadPoolUsageStats node1ThreadPoolStats, - Set indicesWithNoWriteLoad + Set indicesWithNoWriteLoad ) { final Map nodeUsageStats = new HashMap<>(); if (node0ThreadPoolStats != null) { @@ -181,7 +185,7 @@ private RoutingAllocation createRoutingAllocationWithRandomisedWriteLoads( clusterState.metadata() .getProject(ProjectId.DEFAULT) .stream() - .filter(index -> indicesWithNoWriteLoad.contains(index.getIndex().getName()) == false) + .filter(index -> indicesWithNoWriteLoad.contains(index.getIndex()) == false) .flatMap(index -> IntStream.range(0, 3).mapToObj(shardNum -> new ShardId(index.getIndex(), shardNum))) .collect( Collectors.toUnmodifiableMap( @@ -202,6 +206,6 @@ private RoutingAllocation createRoutingAllocationWithRandomisedWriteLoads( } private ClusterState createClusterState() { - return ClusterStateCreationUtils.stateWithAssignedPrimariesAndReplicas(INDICES, 3, 0); + return ClusterStateCreationUtils.stateWithAssignedPrimariesAndReplicas(indices, 3, 0); } } diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/ShardRoutingTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/ShardRoutingTests.java index 23fdb47619ef8..dcde45b78816e 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/ShardRoutingTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/ShardRoutingTests.java @@ -157,7 +157,7 @@ private static ShardRouting mutateRole(ShardRouting instance) { ); } - private ShardRouting randomShardRouting(String index, int shard) { + private ShardRouting randomShardRouting(Index index, int shard) { ShardRoutingState state = randomFrom(ShardRoutingState.values()); return TestShardRouting.newShardRouting( index, @@ -170,10 +170,16 @@ private ShardRouting randomShardRouting(String index, int shard) { } public void testIsSameAllocation() { - ShardRouting unassignedShard0 = TestShardRouting.newShardRouting("test", 0, null, false, ShardRoutingState.UNASSIGNED); - ShardRouting unassignedShard1 = TestShardRouting.newShardRouting("test", 1, null, false, ShardRoutingState.UNASSIGNED); - ShardRouting initializingShard0 = TestShardRouting.newShardRouting("test", 0, "1", randomBoolean(), ShardRoutingState.INITIALIZING); - ShardRouting initializingShard1 = TestShardRouting.newShardRouting("test", 1, "1", randomBoolean(), ShardRoutingState.INITIALIZING); + Index index = new Index("test", UUIDs.randomBase64UUID()); + ShardRouting unassignedShard0 = TestShardRouting.newShardRouting(new ShardId(index, 0), null, false, ShardRoutingState.UNASSIGNED); + ShardRouting unassignedShard1 = TestShardRouting.newShardRouting(new ShardId(index, 1), null, false, ShardRoutingState.UNASSIGNED); + ShardRouting initializingShard0 = TestShardRouting.newShardRouting(index, 0, "1", randomBoolean(), ShardRoutingState.INITIALIZING); + ShardRouting initializingShard1 = TestShardRouting.newShardRouting( + new ShardId(index, 1), + "1", + randomBoolean(), + ShardRoutingState.INITIALIZING + ); ShardRouting startedShard0 = initializingShard0.moveToStarted(ShardRouting.UNAVAILABLE_EXPECTED_SHARD_SIZE); ShardRouting startedShard1 = initializingShard1.moveToStarted(ShardRouting.UNAVAILABLE_EXPECTED_SHARD_SIZE); @@ -194,16 +200,17 @@ public void testIsSameAllocation() { } public void testIsSourceTargetRelocation() { - ShardRouting unassignedShard0 = TestShardRouting.newShardRouting("test", 0, null, false, ShardRoutingState.UNASSIGNED); + Index index = new Index("test", UUIDs.randomBase64UUID()); + ShardRouting unassignedShard0 = TestShardRouting.newShardRouting(new ShardId(index, 0), null, false, ShardRoutingState.UNASSIGNED); ShardRouting initializingShard0 = TestShardRouting.newShardRouting( - "test", + index, 0, "node1", randomBoolean(), ShardRoutingState.INITIALIZING ); ShardRouting initializingShard1 = TestShardRouting.newShardRouting( - "test", + index, 1, "node1", randomBoolean(), @@ -252,7 +259,7 @@ public void testIsSourceTargetRelocation() { } public void testEqualsIgnoringVersion() { - ShardRouting routing = randomShardRouting("test", 0); + ShardRouting routing = randomShardRouting(new Index("test", randomUUID()), 0); ShardRouting otherRouting = routing; @@ -361,8 +368,7 @@ public void testEqualsIgnoringVersion() { case 5: // change primary flag otherRouting = shardRoutingBuilder( - otherRouting.getIndexName(), - otherRouting.id(), + otherRouting.shardId(), otherRouting.currentNodeId(), otherRouting.primary() == false, otherRouting.state() @@ -372,8 +378,7 @@ public void testEqualsIgnoringVersion() { // change state ShardRoutingState newState = randomValueOtherThan(otherRouting.state(), () -> randomFrom(ShardRoutingState.values())); otherRouting = shardRoutingBuilder( - otherRouting.getIndexName(), - otherRouting.id(), + otherRouting.shardId(), newState == ShardRoutingState.UNASSIGNED ? null : Objects.requireNonNullElse(otherRouting.currentNodeId(), "1"), otherRouting.primary(), newState @@ -393,8 +398,7 @@ public void testEqualsIgnoringVersion() { if (randomBoolean() && otherRouting.state() == ShardRoutingState.UNASSIGNED) { // change unassigned info otherRouting = shardRoutingBuilder( - otherRouting.getIndexName(), - otherRouting.id(), + otherRouting.shardId(), otherRouting.currentNodeId(), otherRouting.primary(), otherRouting.state() @@ -420,7 +424,7 @@ public void testEqualsIgnoringVersion() { public void testExpectedSize() throws IOException { final int iters = randomIntBetween(10, 100); for (int i = 0; i < iters; i++) { - ShardRouting routing = randomShardRouting("test", 0); + ShardRouting routing = randomShardRouting(new Index("test", randomUUID()), 0); long byteSize = randomIntBetween(0, Integer.MAX_VALUE); if (routing.unassigned()) { routing = ShardRoutingHelper.initialize(routing, "foo", byteSize); diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/UnassignedInfoTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/UnassignedInfoTests.java index 1f1130e420c9d..b520dc9da56b4 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/UnassignedInfoTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/UnassignedInfoTests.java @@ -534,7 +534,7 @@ public void testReplicaAdded() { * The unassigned meta is kept when a shard goes to INITIALIZING, but cleared when it moves to STARTED. */ public void testStateTransitionMetaHandling() { - ShardRouting shard = shardRoutingBuilder("test", 1, null, true, ShardRoutingState.UNASSIGNED).withUnassignedInfo( + ShardRouting shard = shardRoutingBuilder("test", randomUUID(), 1, null, true, ShardRoutingState.UNASSIGNED).withUnassignedInfo( new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, null) ).build(); assertThat(shard.unassignedInfo(), notNullValue()); @@ -552,7 +552,7 @@ public void testStateTransitionMetaHandling() { public void testNodeLeave() { AllocationService allocation = createAllocationService(); Metadata metadata = Metadata.builder() - .put(IndexMetadata.builder("test").settings(settings(IndexVersion.current())).numberOfShards(1).numberOfReplicas(1)) + .put(IndexMetadata.builder("test").settings(indexSettings(IndexVersion.current(), randomUUID(), 1, 1))) .build(); ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) .metadata(metadata) @@ -817,8 +817,8 @@ private void checkRemainingDelayCalculation( public void testNumberOfDelayedUnassigned() throws Exception { MockAllocationService allocation = createAllocationService(Settings.EMPTY, new DelayedShardsMockGatewayAllocator()); Metadata metadata = Metadata.builder() - .put(IndexMetadata.builder("test1").settings(settings(IndexVersion.current())).numberOfShards(1).numberOfReplicas(1)) - .put(IndexMetadata.builder("test2").settings(settings(IndexVersion.current())).numberOfShards(1).numberOfReplicas(1)) + .put(IndexMetadata.builder("test1").settings(indexSettings(IndexVersion.current(), randomUUID(), 1, 1))) + .put(IndexMetadata.builder("test2").settings(indexSettings(IndexVersion.current(), randomUUID(), 1, 1))) .build(); ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) .metadata(metadata) @@ -856,18 +856,20 @@ public void testFindNextDelayedAllocation() { .put( IndexMetadata.builder("test1") .settings( - settings(IndexVersion.current()).put(UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), delayTest1) + indexSettings(IndexVersion.current(), randomUUID(), 1, 1).put( + UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), + delayTest1 + ) ) - .numberOfShards(1) - .numberOfReplicas(1) ) .put( IndexMetadata.builder("test2") .settings( - settings(IndexVersion.current()).put(UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), delayTest2) + indexSettings(IndexVersion.current(), randomUUID(), 1, 1).put( + UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), + delayTest2 + ) ) - .numberOfShards(1) - .numberOfReplicas(1) ) .build(); ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/AddIncrementallyTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/AddIncrementallyTests.java index dae624f213f3e..57d40019e2f4b 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/AddIncrementallyTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/AddIncrementallyTests.java @@ -262,9 +262,7 @@ private ClusterState initCluster( for (int i = 0; i < numberOfIndices; i++) { IndexMetadata.Builder index = IndexMetadata.builder("test" + i) - .settings(settings(IndexVersion.current())) - .numberOfShards(numberOfShards) - .numberOfReplicas(numberOfReplicas); + .settings(indexSettings(IndexVersion.current(), randomUUID(), numberOfShards, numberOfReplicas)); metadataBuilder = metadataBuilder.put(index); } @@ -312,9 +310,7 @@ private ClusterState addIndex( ); IndexMetadata.Builder index = IndexMetadata.builder("test" + indexOrdinal) - .settings(settings(IndexVersion.current())) - .numberOfShards(numberOfShards) - .numberOfReplicas(numberOfReplicas); + .settings(indexSettings(IndexVersion.current(), randomUUID(), numberOfShards, numberOfReplicas)); IndexMetadata imd = index.build(); metadataBuilder = metadataBuilder.put(imd, true); routingTableBuilder.addAsNew(imd); diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/AllocationPriorityTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/AllocationPriorityTests.java index 3baa671e6d853..56cef19ca9068 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/AllocationPriorityTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/AllocationPriorityTests.java @@ -57,15 +57,11 @@ public void testPrioritizedIndicesAllocatedFirst() { Metadata metadata = Metadata.builder() .put( IndexMetadata.builder("first") - .settings(settings(IndexVersion.current()).put(IndexMetadata.SETTING_PRIORITY, priorityFirst)) - .numberOfShards(2) - .numberOfReplicas(1) + .settings(indexSettings(IndexVersion.current(), randomUUID(), 2, 1).put(IndexMetadata.SETTING_PRIORITY, priorityFirst)) ) .put( IndexMetadata.builder("second") - .settings(settings(IndexVersion.current()).put(IndexMetadata.SETTING_PRIORITY, prioritySecond)) - .numberOfShards(2) - .numberOfReplicas(1) + .settings(indexSettings(IndexVersion.current(), randomUUID(), 2, 1).put(IndexMetadata.SETTING_PRIORITY, prioritySecond)) ) .build(); RoutingTable initialRoutingTable = RoutingTable.builder(TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY) diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/AwarenessAllocationTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/AwarenessAllocationTests.java index 76a0110f75fa0..6e55c4382fde7 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/AwarenessAllocationTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/AwarenessAllocationTests.java @@ -312,8 +312,8 @@ public void testMoveShardOnceNewNodeWithAttributeAdded4() { logger.info("Building initial routing table for 'moveShardOnceNewNodeWithAttributeAdded4'"); Metadata metadata = Metadata.builder() - .put(IndexMetadata.builder("test1").settings(settings(IndexVersion.current())).numberOfShards(5).numberOfReplicas(1)) - .put(IndexMetadata.builder("test2").settings(settings(IndexVersion.current())).numberOfShards(5).numberOfReplicas(1)) + .put(IndexMetadata.builder("test1").settings(indexSettings(IndexVersion.current(), randomUUID(), 5, 1))) + .put(IndexMetadata.builder("test2").settings(indexSettings(IndexVersion.current(), randomUUID(), 5, 1))) .build(); RoutingTable initialRoutingTable = RoutingTable.builder(TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY) @@ -742,8 +742,8 @@ public void testFullAwareness3() { logger.info("Building initial routing table for 'fullAwareness3'"); Metadata metadata = Metadata.builder() - .put(IndexMetadata.builder("test1").settings(settings(IndexVersion.current())).numberOfShards(5).numberOfReplicas(1)) - .put(IndexMetadata.builder("test2").settings(settings(IndexVersion.current())).numberOfShards(5).numberOfReplicas(1)) + .put(IndexMetadata.builder("test1").settings(indexSettings(IndexVersion.current(), randomUUID(), 5, 1))) + .put(IndexMetadata.builder("test2").settings(indexSettings(IndexVersion.current(), randomUUID(), 5, 1))) .build(); RoutingTable initialRoutingTable = RoutingTable.builder(TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY) @@ -830,7 +830,7 @@ public void testUnbalancedZones() { logger.info("Building initial routing table for 'testUnbalancedZones'"); Metadata metadata = Metadata.builder() - .put(IndexMetadata.builder("test").settings(settings(IndexVersion.current())).numberOfShards(5).numberOfReplicas(1)) + .put(IndexMetadata.builder("test").settings(indexSettings(IndexVersion.current(), randomUUID(), 5, 1))) .build(); RoutingTable initialRoutingTable = RoutingTable.builder(TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY) diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/BalanceConfigurationTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/BalanceConfigurationTests.java index 6ad85864a2417..2b4fab33c772d 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/BalanceConfigurationTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/BalanceConfigurationTests.java @@ -150,9 +150,7 @@ private ClusterState initCluster(AllocationService strategy) { for (int i = 0; i < numberOfIndices; i++) { IndexMetadata.Builder index = IndexMetadata.builder("test" + i) - .settings(settings(IndexVersion.current())) - .numberOfShards(numberOfShards) - .numberOfReplicas(numberOfReplicas); + .settings(indexSettings(IndexVersion.current(), randomUUID(), numberOfShards, numberOfReplicas)); metadataBuilder = metadataBuilder.put(index); } diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/BalancedSingleShardTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/BalancedSingleShardTests.java index ea732a93f4e1e..88d4f7b1f1a98 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/BalancedSingleShardTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/BalancedSingleShardTests.java @@ -27,6 +27,7 @@ import org.elasticsearch.cluster.routing.allocation.decider.Decision.Type; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.Tuple; +import org.elasticsearch.index.Index; import org.elasticsearch.snapshots.SnapshotShardSizeInfo; import java.util.Arrays; @@ -50,7 +51,7 @@ public class BalancedSingleShardTests extends ESAllocationTestCase { public void testRebalanceNonStartedShardNotAllowed() { BalancedShardsAllocator allocator = new BalancedShardsAllocator(Settings.EMPTY); ClusterState clusterState = ClusterStateCreationUtils.state( - "idx", + new Index("idx", randomUUID()), randomBoolean(), randomFrom(ShardRoutingState.INITIALIZING, ShardRoutingState.UNASSIGNED, ShardRoutingState.RELOCATING) ); @@ -67,7 +68,11 @@ public void testRebalanceNonStartedShardNotAllowed() { public void testRebalanceNotAllowedDuringPendingAsyncFetch() { BalancedShardsAllocator allocator = new BalancedShardsAllocator(Settings.EMPTY); - ClusterState clusterState = ClusterStateCreationUtils.state("idx", randomBoolean(), ShardRoutingState.STARTED); + ClusterState clusterState = ClusterStateCreationUtils.state( + new Index("idx", randomUUID()), + randomBoolean(), + ShardRoutingState.STARTED + ); assertThat(clusterState.metadata().projects(), aMapWithSize(1)); final ProjectId projectId = clusterState.metadata().projects().keySet().iterator().next(); @@ -94,7 +99,11 @@ public Decision canRebalance(ShardRouting shardRouting, RoutingAllocation alloca } }; BalancedShardsAllocator allocator = new BalancedShardsAllocator(Settings.EMPTY); - ClusterState clusterState = ClusterStateCreationUtils.state("idx", randomBoolean(), ShardRoutingState.STARTED); + ClusterState clusterState = ClusterStateCreationUtils.state( + new Index("idx", randomUUID()), + randomBoolean(), + ShardRoutingState.STARTED + ); assertThat(clusterState.metadata().projects(), aMapWithSize(1)); final ProjectId projectId = clusterState.metadata().projects().keySet().iterator().next(); @@ -187,7 +196,7 @@ public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, Routing } public void testSingleShardBalanceProducesSameResultsAsBalanceStep() { - final String[] indices = { "idx1", "idx2" }; + final Index[] indices = { new Index("idx1", randomUUID()), new Index("idx2", randomUUID()) }; // Create a cluster state with 2 indices, each with 1 started primary shard, and only // one node initially so that all primary shards get allocated to the same node. We are only // using 2 indices (i.e. 2 total primary shards) because if we have any more than 2 started shards @@ -260,7 +269,9 @@ public Decision canRebalance(ShardRouting shardRouting, RoutingAllocation alloca public void testNodeDecisionsRanking() { // only one shard, so moving it will not create a better balance anywhere, so all node decisions should // return the same ranking as the current node - ClusterState clusterState = ClusterStateCreationUtils.state(randomIntBetween(1, 10), new String[] { "idx" }, 1); + Index index = new Index("idx", randomUUID()); + Index[] indices = { index }; + ClusterState clusterState = ClusterStateCreationUtils.state(randomIntBetween(1, 10), indices, 1); ShardRouting shardToRebalance = clusterState.routingTable().index("idx").shardsWithState(ShardRoutingState.STARTED).get(0); MoveDecision decision = executeRebalanceFor(shardToRebalance, clusterState, emptySet()); int currentRanking = decision.getCurrentNodeRanking(); @@ -271,8 +282,8 @@ public void testNodeDecisionsRanking() { // start off with one node and several shards assigned to that node, then add a few nodes to the cluster, // each of these new nodes should have a better ranking than the current, given a low enough threshold - clusterState = ClusterStateCreationUtils.state(1, new String[] { "idx" }, randomIntBetween(2, 10)); - shardToRebalance = clusterState.routingTable().index("idx").shardsWithState(ShardRoutingState.STARTED).get(0); + clusterState = ClusterStateCreationUtils.state(1, indices, randomIntBetween(2, 10)); + shardToRebalance = clusterState.routingTable().index(index).shardsWithState(ShardRoutingState.STARTED).get(0); clusterState = addNodesToClusterState(clusterState, randomIntBetween(1, 10)); decision = executeRebalanceFor(shardToRebalance, clusterState, emptySet()); for (NodeAllocationResult result : decision.getNodeDecisions()) { @@ -284,7 +295,7 @@ public void testNodeDecisionsRanking() { // to rebalance. the new node should have the best ranking (because it has no shards), followed by the node currently // holding the shard as well as the other node with only 2 shards (they should have the same ranking), followed by the // node with 3 shards which will have the lowest ranking. - clusterState = ClusterStateCreationUtils.state(3, new String[] { "idx" }, 7); + clusterState = ClusterStateCreationUtils.state(3, indices, 7); shardToRebalance = null; Set nodesWithTwoShards = new HashSet<>(); String nodeWithThreeShards = null; @@ -371,7 +382,7 @@ public Decision canRebalance(ShardRouting shardRouting, RoutingAllocation alloca List allocationDeciders = Arrays.asList(rebalanceDecider, allocationDecider); final int numShards = randomIntBetween(8, 13); BalancedShardsAllocator allocator = new BalancedShardsAllocator(balancerSettings); - ClusterState clusterState = ClusterStateCreationUtils.state("idx", 2, numShards); + ClusterState clusterState = ClusterStateCreationUtils.state(new Index("idx", randomUUID()), 2, numShards); // add a new node so shards can be rebalanced there DiscoveryNodes.Builder nodesBuilder = DiscoveryNodes.builder(clusterState.nodes()); nodesBuilder.add(newNode(randomAlphaOfLength(7))); diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/CatAllocationTestCase.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/CatAllocationTestCase.java index f6ffa6d2200b5..847d0215fa899 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/CatAllocationTestCase.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/CatAllocationTestCase.java @@ -67,18 +67,18 @@ public void testRun() throws IOException { while ((line = reader.readLine()) != null) { final Matcher matcher; if ((matcher = pattern.matcher(line)).matches()) { - final String index = matcher.group(1); - Idx idx = indices.get(index); + final String indexName = matcher.group(1); + Idx idx = indices.get(indexName); if (idx == null) { - idx = new Idx(index); - indices.put(index, idx); + idx = new Idx(indexName, randomUUID()); + indices.put(indexName, idx); } final int shard = Integer.parseInt(matcher.group(2)); final boolean primary = matcher.group(3).equals("p"); ShardRoutingState state = ShardRoutingState.valueOf(matcher.group(4)); String ip = matcher.group(5); nodes.add(ip); - ShardRouting routing = TestShardRouting.newShardRouting(index, shard, ip, null, primary, state); + ShardRouting routing = TestShardRouting.newShardRouting(indexName, idx.uuid, shard, ip, null, primary, state); idx.add(routing); logger.debug("Add routing {}", routing); } else { @@ -93,9 +93,7 @@ public void testRun() throws IOException { RoutingTable.Builder routingTableBuilder = RoutingTable.builder(); for (Idx idx : indices.values()) { IndexMetadata.Builder idxMetaBuilder = IndexMetadata.builder(idx.name) - .settings(settings(IndexVersion.current())) - .numberOfShards(idx.numShards()) - .numberOfReplicas(idx.numReplicas()); + .settings(indexSettings(IndexVersion.current(), idx.uuid, idx.numShards(), idx.numReplicas())); for (ShardRouting shardRouting : idx.routing) { if (shardRouting.active()) { Set allocationIds = idxMetaBuilder.getInSyncAllocationIds(shardRouting.id()); @@ -167,10 +165,12 @@ private ClusterState rebalance(ClusterState clusterState) { public class Idx { final String name; + final String uuid; final List routing = new ArrayList<>(); - public Idx(String name) { + public Idx(String name, String uuid) { this.name = name; + this.uuid = uuid; } public void add(ShardRouting r) { diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/ClusterRebalanceRoutingTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/ClusterRebalanceRoutingTests.java index b26f17b891db2..a179042a04f9a 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/ClusterRebalanceRoutingTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/ClusterRebalanceRoutingTests.java @@ -49,10 +49,12 @@ public void testAlways() { ); Metadata metadata = Metadata.builder() - .put(IndexMetadata.builder("test1").settings(indexSettings(IndexVersion.current(), 1, 1))) + .put(IndexMetadata.builder("test1").settings(indexSettings(IndexVersion.current(), randomUUID(), 1, 1))) .put( IndexMetadata.builder("test2") - .settings(indexSettings(IndexVersion.current(), 1, 1).put("index.routing.allocation.include._id", "node1,node2")) + .settings( + indexSettings(IndexVersion.current(), randomUUID(), 1, 1).put("index.routing.allocation.include._id", "node1,node2") + ) ) .build(); @@ -131,17 +133,15 @@ public void testClusterPrimariesActive1() { ); Metadata metadata = Metadata.builder() - .put(IndexMetadata.builder("test1").settings(settings(IndexVersion.current())).numberOfShards(1).numberOfReplicas(1)) + .put(IndexMetadata.builder("test1").settings(indexSettings(IndexVersion.current(), randomUUID(), 1, 1))) .put( IndexMetadata.builder("test2") .settings( - settings(IndexVersion.current()).put( + indexSettings(IndexVersion.current(), randomUUID(), 1, 1).put( IndexMetadata.INDEX_ROUTING_INCLUDE_GROUP_SETTING.getConcreteSettingForNamespace("_id").getKey(), "node1,node2" ) ) - .numberOfShards(1) - .numberOfReplicas(1) ) .build(); @@ -234,8 +234,8 @@ public void testClusterPrimariesActive2() { ); Metadata metadata = Metadata.builder() - .put(IndexMetadata.builder("test1").settings(settings(IndexVersion.current())).numberOfShards(1).numberOfReplicas(1)) - .put(IndexMetadata.builder("test2").settings(settings(IndexVersion.current())).numberOfShards(1).numberOfReplicas(1)) + .put(IndexMetadata.builder("test1").settings(indexSettings(IndexVersion.current(), randomUUID(), 1, 1))) + .put(IndexMetadata.builder("test2").settings(indexSettings(IndexVersion.current(), randomUUID(), 1, 1))) .build(); @@ -312,8 +312,8 @@ public void testClusterAllActive1() { ); Metadata metadata = Metadata.builder() - .put(IndexMetadata.builder("test1").settings(settings(IndexVersion.current())).numberOfShards(1).numberOfReplicas(1)) - .put(IndexMetadata.builder("test2").settings(settings(IndexVersion.current())).numberOfShards(1).numberOfReplicas(1)) + .put(IndexMetadata.builder("test1").settings(indexSettings(IndexVersion.current(), randomUUID(), 1, 1))) + .put(IndexMetadata.builder("test2").settings(indexSettings(IndexVersion.current(), randomUUID(), 1, 1))) .build(); RoutingTable initialRoutingTable = RoutingTable.builder(TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY) @@ -421,8 +421,8 @@ public void testClusterAllActive2() { ); Metadata metadata = Metadata.builder() - .put(IndexMetadata.builder("test1").settings(settings(IndexVersion.current())).numberOfShards(1).numberOfReplicas(1)) - .put(IndexMetadata.builder("test2").settings(settings(IndexVersion.current())).numberOfShards(1).numberOfReplicas(1)) + .put(IndexMetadata.builder("test1").settings(indexSettings(IndexVersion.current(), randomUUID(), 1, 1))) + .put(IndexMetadata.builder("test2").settings(indexSettings(IndexVersion.current(), randomUUID(), 1, 1))) .build(); RoutingTable initialRoutingTable = RoutingTable.builder(TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY) @@ -499,8 +499,8 @@ public void testClusterAllActive3() { ); Metadata metadata = Metadata.builder() - .put(IndexMetadata.builder("test1").settings(settings(IndexVersion.current())).numberOfShards(1).numberOfReplicas(1)) - .put(IndexMetadata.builder("test2").settings(settings(IndexVersion.current())).numberOfShards(1).numberOfReplicas(1)) + .put(IndexMetadata.builder("test1").settings(indexSettings(IndexVersion.current(), randomUUID(), 1, 1))) + .put(IndexMetadata.builder("test2").settings(indexSettings(IndexVersion.current(), randomUUID(), 1, 1))) .build(); RoutingTable initialRoutingTable = RoutingTable.builder(TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY) @@ -608,8 +608,8 @@ public void allocateUnassigned( ); Metadata metadata = Metadata.builder() - .put(IndexMetadata.builder("test").settings(settings(IndexVersion.current())).numberOfShards(2).numberOfReplicas(0)) - .put(IndexMetadata.builder("test1").settings(settings(IndexVersion.current())).numberOfShards(2).numberOfReplicas(0)) + .put(IndexMetadata.builder("test").settings(indexSettings(IndexVersion.current(), randomUUID(), 2, 0))) + .put(IndexMetadata.builder("test1").settings(indexSettings(IndexVersion.current(), randomUUID(), 2, 0))) .build(); RoutingTable initialRoutingTable = RoutingTable.builder(TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY) diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/DiskThresholdMonitorTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/DiskThresholdMonitorTests.java index c896d8a8f20fd..26700c673c655 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/DiskThresholdMonitorTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/DiskThresholdMonitorTests.java @@ -76,27 +76,27 @@ private void doTestMarkFloodStageIndicesReadOnly(boolean testMaxHeadroom) { ProjectMetadata projectMetadata = ProjectMetadata.builder(projectId) .put( IndexMetadata.builder("test") - .settings(settings(IndexVersion.current()).put("index.routing.allocation.require._id", "node2")) - .numberOfShards(1) - .numberOfReplicas(0) + .settings( + indexSettings(IndexVersion.current(), randomUUID(), 1, 0).put("index.routing.allocation.require._id", "node2") + ) ) .put( IndexMetadata.builder("test_1") - .settings(settings(IndexVersion.current()).put("index.routing.allocation.require._id", "node1")) - .numberOfShards(1) - .numberOfReplicas(0) + .settings( + indexSettings(IndexVersion.current(), randomUUID(), 1, 0).put("index.routing.allocation.require._id", "node1") + ) ) .put( IndexMetadata.builder("test_2") - .settings(settings(IndexVersion.current()).put("index.routing.allocation.require._id", "node1")) - .numberOfShards(1) - .numberOfReplicas(0) + .settings( + indexSettings(IndexVersion.current(), randomUUID(), 1, 0).put("index.routing.allocation.require._id", "node1") + ) ) .put( IndexMetadata.builder("frozen") - .settings(settings(IndexVersion.current()).put("index.routing.allocation.require._id", "frozen")) - .numberOfShards(1) - .numberOfReplicas(0) + .settings( + indexSettings(IndexVersion.current(), randomUUID(), 1, 0).put("index.routing.allocation.require._id", "frozen") + ) ) .build(); RoutingTable routingTable = RoutingTable.builder(TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY) @@ -463,8 +463,8 @@ private void doTestAutoReleaseIndices(boolean testMaxHeadroom) { ); final ProjectId projectId = randomProjectIdOrDefault(); ProjectMetadata project = ProjectMetadata.builder(projectId) - .put(IndexMetadata.builder("test_1").settings(settings(IndexVersion.current())).numberOfShards(2).numberOfReplicas(1)) - .put(IndexMetadata.builder("test_2").settings(settings(IndexVersion.current())).numberOfShards(2).numberOfReplicas(1)) + .put(IndexMetadata.builder("test_1").settings(indexSettings(IndexVersion.current(), randomUUID(), 2, 1))) + .put(IndexMetadata.builder("test_2").settings(indexSettings(IndexVersion.current(), randomUUID(), 2, 1))) .build(); RoutingTable routingTable = RoutingTable.builder(TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY) .addAsNew(project.index("test_1")) @@ -814,8 +814,8 @@ private void doTestNoAutoReleaseOfIndicesOnReplacementNodes(boolean testMaxHeadr ); final var projectId = randomProjectIdOrDefault(); final ProjectMetadata projectMetadata = ProjectMetadata.builder(projectId) - .put(IndexMetadata.builder("test_1").settings(settings(IndexVersion.current())).numberOfShards(2).numberOfReplicas(1)) - .put(IndexMetadata.builder("test_2").settings(settings(IndexVersion.current())).numberOfShards(2).numberOfReplicas(1)) + .put(IndexMetadata.builder("test_1").settings(indexSettings(IndexVersion.current(), randomUUID(), 2, 1))) + .put(IndexMetadata.builder("test_2").settings(indexSettings(IndexVersion.current(), randomUUID(), 2, 1))) .build(); final RoutingTable routingTable = RoutingTable.builder(TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY) .addAsNew(projectMetadata.index("test_1")) diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/FailedNodeRoutingTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/FailedNodeRoutingTests.java index 32119f1c3dfac..140244bc94ccb 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/FailedNodeRoutingTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/FailedNodeRoutingTests.java @@ -66,8 +66,8 @@ public void testSimpleFailedNodeTest() { ); Metadata metadata = Metadata.builder() - .put(IndexMetadata.builder("test1").settings(settings(IndexVersion.current())).numberOfShards(1).numberOfReplicas(1)) - .put(IndexMetadata.builder("test2").settings(settings(IndexVersion.current())).numberOfShards(1).numberOfReplicas(1)) + .put(IndexMetadata.builder("test1").settings(indexSettings(IndexVersion.current(), randomUUID(), 1, 1))) + .put(IndexMetadata.builder("test2").settings(indexSettings(IndexVersion.current(), randomUUID(), 1, 1))) .build(); RoutingTable initialRoutingTable = RoutingTable.builder(TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY) diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/FilterRoutingTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/FilterRoutingTests.java index 76bc74e24bc16..7585795cf8536 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/FilterRoutingTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/FilterRoutingTests.java @@ -365,8 +365,8 @@ public void testConcurrentRecoveriesAfterShardsCannotRemainOnNode() { logger.info("Building initial routing table"); Metadata metadata = Metadata.builder() - .put(IndexMetadata.builder("test1").settings(settings(IndexVersion.current())).numberOfShards(2).numberOfReplicas(0)) - .put(IndexMetadata.builder("test2").settings(settings(IndexVersion.current())).numberOfShards(2).numberOfReplicas(0)) + .put(IndexMetadata.builder("test1").settings(indexSettings(IndexVersion.current(), randomUUID(), 2, 0))) + .put(IndexMetadata.builder("test2").settings(indexSettings(IndexVersion.current(), randomUUID(), 2, 0))) .build(); RoutingTable initialRoutingTable = RoutingTable.builder(TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY) diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/IndexBalanceTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/IndexBalanceTests.java index 22a3f51a2d32c..002fe37d7757e 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/IndexBalanceTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/IndexBalanceTests.java @@ -55,8 +55,8 @@ public void testBalanceAllNodesStarted() { logger.info("Building initial routing table"); Metadata metadata = Metadata.builder() - .put(IndexMetadata.builder("test").settings(settings(IndexVersion.current())).numberOfShards(3).numberOfReplicas(1)) - .put(IndexMetadata.builder("test1").settings(settings(IndexVersion.current())).numberOfShards(3).numberOfReplicas(1)) + .put(IndexMetadata.builder("test").settings(indexSettings(IndexVersion.current(), randomUUID(), 3, 1))) + .put(IndexMetadata.builder("test1").settings(indexSettings(IndexVersion.current(), randomUUID(), 3, 1))) .build(); RoutingTable initialRoutingTable = RoutingTable.builder(TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY) @@ -172,8 +172,8 @@ public void testBalanceIncrementallyStartNodes() { logger.info("Building initial routing table"); Metadata metadata = Metadata.builder() - .put(IndexMetadata.builder("test").settings(settings(IndexVersion.current())).numberOfShards(3).numberOfReplicas(1)) - .put(IndexMetadata.builder("test1").settings(settings(IndexVersion.current())).numberOfShards(3).numberOfReplicas(1)) + .put(IndexMetadata.builder("test").settings(indexSettings(IndexVersion.current(), randomUUID(), 3, 1))) + .put(IndexMetadata.builder("test1").settings(indexSettings(IndexVersion.current(), randomUUID(), 3, 1))) .build(); RoutingTable initialRoutingTable = RoutingTable.builder(TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY) @@ -293,7 +293,7 @@ public void testBalanceAllNodesStartedAddIndex() { logger.info("Building initial routing table"); Metadata metadata = Metadata.builder() - .put(IndexMetadata.builder("test").settings(settings(IndexVersion.current())).numberOfShards(3).numberOfReplicas(1)) + .put(IndexMetadata.builder("test").settings(indexSettings(IndexVersion.current(), randomUUID(), 3, 1))) .build(); RoutingTable initialRoutingTable = RoutingTable.builder(TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY) @@ -490,7 +490,7 @@ private static void addIndex( ) { final var inSyncIds = randomList(numberOfShards, numberOfShards, () -> UUIDs.randomBase64UUID(random())); final var indexMetadataBuilder = IndexMetadata.builder(indexName) - .settings(indexSettings(IndexVersion.current(), numberOfShards, 0)); + .settings(indexSettings(IndexVersion.current(), randomUUID(), numberOfShards, 0)); for (int shardId = 0; shardId < numberOfShards; shardId++) { indexMetadataBuilder.putInSyncAllocationIds(shardId, Set.of(inSyncIds.get(shardId))); } diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/IndexVersionAllocationDeciderTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/IndexVersionAllocationDeciderTests.java index 6f7c3762aa86f..83c14971b3631 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/IndexVersionAllocationDeciderTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/IndexVersionAllocationDeciderTests.java @@ -189,9 +189,7 @@ public void testRandom() { for (int i = 0; i < numIndices; i++) { builder.put( IndexMetadata.builder("test_" + i) - .settings(settings(IndexVersion.current())) - .numberOfShards(between(1, 5)) - .numberOfReplicas(between(0, 2)) + .settings(indexSettings(IndexVersion.current(), randomUUID(), between(1, 5), between(0, 2))) ); } Metadata metadata = builder.build(); @@ -313,8 +311,10 @@ public void testRollingRestart() { } public void testRebalanceDoesNotAllocatePrimaryAndReplicasOnDifferentVersionNodes() { - ShardId shard1 = new ShardId("test1", "_na_", 0); - ShardId shard2 = new ShardId("test2", "_na_", 0); + Index index1 = new Index("test1", randomUUID()); + ShardId shard1 = new ShardId(index1, 0); + Index index2 = new Index("test2", randomUUID()); + ShardId shard2 = new ShardId(index2, 0); final DiscoveryNode newNode = DiscoveryNodeUtils.builder("newNode").roles(MASTER_DATA_ROLES).build(); final DiscoveryNode oldNode1 = DiscoveryNodeUtils.builder("oldNode1") .roles(MASTER_DATA_ROLES) @@ -330,17 +330,13 @@ public void testRebalanceDoesNotAllocatePrimaryAndReplicasOnDifferentVersionNode AllocationId allocationId2R = AllocationId.newInitializing(); Metadata metadata = Metadata.builder() .put( - IndexMetadata.builder(shard1.getIndexName()) - .settings(settings(IndexVersion.current()).put(Settings.EMPTY)) - .numberOfShards(1) - .numberOfReplicas(1) + IndexMetadata.builder(index1.getName()) + .settings(indexSettings(IndexVersion.current(), index1.getUUID(), 1, 1)) .putInSyncAllocationIds(0, Sets.newHashSet(allocationId1P.getId(), allocationId1R.getId())) ) .put( - IndexMetadata.builder(shard2.getIndexName()) - .settings(settings(IndexVersion.current()).put(Settings.EMPTY)) - .numberOfShards(1) - .numberOfReplicas(1) + IndexMetadata.builder(index2.getName()) + .settings(indexSettings(IndexVersion.current(), index2.getUUID(), 1, 1)) .putInSyncAllocationIds(0, Sets.newHashSet(allocationId2P.getId(), allocationId2R.getId())) ) .build(); @@ -349,18 +345,13 @@ public void testRebalanceDoesNotAllocatePrimaryAndReplicasOnDifferentVersionNode IndexRoutingTable.builder(shard1.getIndex()) .addIndexShard( new IndexShardRoutingTable.Builder(shard1).addShard( - shardRoutingBuilder(shard1.getIndexName(), shard1.getId(), newNode.getId(), true, ShardRoutingState.STARTED) - .withAllocationId(allocationId1P) + shardRoutingBuilder(shard1, newNode.getId(), true, ShardRoutingState.STARTED).withAllocationId(allocationId1P) .build() ) .addShard( - shardRoutingBuilder( - shard1.getIndexName(), - shard1.getId(), - oldNode1.getId(), - false, - ShardRoutingState.STARTED - ).withAllocationId(allocationId1R).build() + shardRoutingBuilder(shard1, oldNode1.getId(), false, ShardRoutingState.STARTED).withAllocationId( + allocationId1R + ).build() ) ) ) @@ -368,18 +359,13 @@ public void testRebalanceDoesNotAllocatePrimaryAndReplicasOnDifferentVersionNode IndexRoutingTable.builder(shard2.getIndex()) .addIndexShard( new IndexShardRoutingTable.Builder(shard2).addShard( - shardRoutingBuilder(shard2.getIndexName(), shard2.getId(), newNode.getId(), true, ShardRoutingState.STARTED) - .withAllocationId(allocationId2P) + shardRoutingBuilder(shard2, newNode.getId(), true, ShardRoutingState.STARTED).withAllocationId(allocationId2P) .build() ) .addShard( - shardRoutingBuilder( - shard2.getIndexName(), - shard2.getId(), - oldNode1.getId(), - false, - ShardRoutingState.STARTED - ).withAllocationId(allocationId2R).build() + shardRoutingBuilder(shard2, oldNode1.getId(), false, ShardRoutingState.STARTED).withAllocationId( + allocationId2R + ).build() ) ) ) @@ -399,8 +385,8 @@ public void testRebalanceDoesNotAllocatePrimaryAndReplicasOnDifferentVersionNode ); state = strategy.reroute(state, new AllocationCommands(), true, false, false, ActionListener.noop()).clusterState(); // the two indices must stay as is, the replicas cannot move to oldNode2 because versions don't match - assertThat(state.routingTable().index(shard2.getIndex()).shardsWithState(ShardRoutingState.RELOCATING).size(), equalTo(0)); - assertThat(state.routingTable().index(shard1.getIndex()).shardsWithState(ShardRoutingState.RELOCATING).size(), equalTo(0)); + assertThat(state.routingTable().index(index2).shardsWithState(ShardRoutingState.RELOCATING).size(), equalTo(0)); + assertThat(state.routingTable().index(index1).shardsWithState(ShardRoutingState.RELOCATING).size(), equalTo(0)); } public void testRestoreDoesNotAllocateSnapshotOnOlderNodes() { diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/NodeVersionAllocationDeciderTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/NodeVersionAllocationDeciderTests.java index 3334390cd1fbd..8259838a1fae7 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/NodeVersionAllocationDeciderTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/NodeVersionAllocationDeciderTests.java @@ -191,9 +191,7 @@ public void testRandom() { for (int i = 0; i < numIndices; i++) { builder.put( IndexMetadata.builder("test_" + i) - .settings(settings(IndexVersion.current())) - .numberOfShards(between(1, 5)) - .numberOfReplicas(between(0, 2)) + .settings(indexSettings(IndexVersion.current(), randomUUID(), between(1, 5), between(0, 2))) ); } Metadata metadata = builder.build(); @@ -315,8 +313,10 @@ public void testRollingRestart() { } public void testRebalanceDoesNotAllocatePrimaryAndReplicasOnDifferentVersionNodes() { - ShardId shard1 = new ShardId("test1", "_na_", 0); - ShardId shard2 = new ShardId("test2", "_na_", 0); + Index index1 = new Index("test1", randomUUID()); + Index index2 = new Index("test2", randomUUID()); + ShardId shard1 = new ShardId(index1, 0); + ShardId shard2 = new ShardId(index2, 0); final DiscoveryNode newNode = DiscoveryNodeUtils.builder("newNode").roles(MASTER_DATA_ROLES).build(); final DiscoveryNode oldNode1 = DiscoveryNodeUtils.builder("oldNode1") .roles(MASTER_DATA_ROLES) @@ -333,16 +333,12 @@ public void testRebalanceDoesNotAllocatePrimaryAndReplicasOnDifferentVersionNode Metadata metadata = Metadata.builder() .put( IndexMetadata.builder(shard1.getIndexName()) - .settings(settings(IndexVersion.current()).put(Settings.EMPTY)) - .numberOfShards(1) - .numberOfReplicas(1) + .settings(indexSettings(IndexVersion.current(), index1.getUUID(), 1, 1)) .putInSyncAllocationIds(0, Sets.newHashSet(allocationId1P.getId(), allocationId1R.getId())) ) .put( IndexMetadata.builder(shard2.getIndexName()) - .settings(settings(IndexVersion.current()).put(Settings.EMPTY)) - .numberOfShards(1) - .numberOfReplicas(1) + .settings(indexSettings(IndexVersion.current(), index2.getUUID(), 1, 1)) .putInSyncAllocationIds(0, Sets.newHashSet(allocationId2P.getId(), allocationId2R.getId())) ) .build(); @@ -351,18 +347,13 @@ public void testRebalanceDoesNotAllocatePrimaryAndReplicasOnDifferentVersionNode IndexRoutingTable.builder(shard1.getIndex()) .addIndexShard( new IndexShardRoutingTable.Builder(shard1).addShard( - shardRoutingBuilder(shard1.getIndexName(), shard1.getId(), newNode.getId(), true, ShardRoutingState.STARTED) - .withAllocationId(allocationId1P) + shardRoutingBuilder(shard1, newNode.getId(), true, ShardRoutingState.STARTED).withAllocationId(allocationId1P) .build() ) .addShard( - shardRoutingBuilder( - shard1.getIndexName(), - shard1.getId(), - oldNode1.getId(), - false, - ShardRoutingState.STARTED - ).withAllocationId(allocationId1R).build() + shardRoutingBuilder(shard1, oldNode1.getId(), false, ShardRoutingState.STARTED).withAllocationId( + allocationId1R + ).build() ) ) ) @@ -370,18 +361,13 @@ public void testRebalanceDoesNotAllocatePrimaryAndReplicasOnDifferentVersionNode IndexRoutingTable.builder(shard2.getIndex()) .addIndexShard( new IndexShardRoutingTable.Builder(shard2).addShard( - shardRoutingBuilder(shard2.getIndexName(), shard2.getId(), newNode.getId(), true, ShardRoutingState.STARTED) - .withAllocationId(allocationId2P) + shardRoutingBuilder(shard2, newNode.getId(), true, ShardRoutingState.STARTED).withAllocationId(allocationId2P) .build() ) .addShard( - shardRoutingBuilder( - shard2.getIndexName(), - shard2.getId(), - oldNode1.getId(), - false, - ShardRoutingState.STARTED - ).withAllocationId(allocationId2R).build() + shardRoutingBuilder(shard2, oldNode1.getId(), false, ShardRoutingState.STARTED).withAllocationId( + allocationId2R + ).build() ) ) ) diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/PreferLocalPrimariesToRelocatingPrimariesTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/PreferLocalPrimariesToRelocatingPrimariesTests.java index d4b95fc93c11a..b4e49d30f9381 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/PreferLocalPrimariesToRelocatingPrimariesTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/PreferLocalPrimariesToRelocatingPrimariesTests.java @@ -52,12 +52,8 @@ public void testPreferLocalPrimaryAllocationOverFiltered() { logger.info("create 2 indices with [{}] no replicas, and wait till all are allocated", numberOfShards); Metadata metadata = Metadata.builder() - .put( - IndexMetadata.builder("test1").settings(settings(IndexVersion.current())).numberOfShards(numberOfShards).numberOfReplicas(0) - ) - .put( - IndexMetadata.builder("test2").settings(settings(IndexVersion.current())).numberOfShards(numberOfShards).numberOfReplicas(0) - ) + .put(IndexMetadata.builder("test1").settings(indexSettings(IndexVersion.current(), randomUUID(), numberOfShards, 0))) + .put(IndexMetadata.builder("test2").settings(indexSettings(IndexVersion.current(), randomUUID(), numberOfShards, 0))) .build(); RoutingTable initialRoutingTable = RoutingTable.builder(TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY) @@ -80,18 +76,16 @@ public void testPreferLocalPrimaryAllocationOverFiltered() { logger.info("remove one of the nodes and apply filter to move everything from another node"); + IndexMetadata index1Metadata = clusterState.metadata().getProject().index("test1"); + IndexMetadata index2Metadata = clusterState.metadata().getProject().index("test2"); metadata = Metadata.builder() .put( - IndexMetadata.builder(clusterState.metadata().getProject().index("test1")) - .settings( - indexSettings(IndexVersion.current(), numberOfShards, 0).put("index.routing.allocation.exclude._name", "node2") - ) + IndexMetadata.builder(index1Metadata) + .settings(Settings.builder().put(index1Metadata.getSettings()).put("index.routing.allocation.exclude._name", "node2")) ) .put( - IndexMetadata.builder(clusterState.metadata().getProject().index("test2")) - .settings( - indexSettings(IndexVersion.current(), numberOfShards, 0).put("index.routing.allocation.exclude._name", "node2") - ) + IndexMetadata.builder(index2Metadata) + .settings(Settings.builder().put(index2Metadata.getSettings()).put("index.routing.allocation.exclude._name", "node2")) ) .build(); clusterState = ClusterState.builder(clusterState) diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/PreferPrimaryAllocationTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/PreferPrimaryAllocationTests.java index c7dffd11c39a9..9168fb23dff6c 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/PreferPrimaryAllocationTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/PreferPrimaryAllocationTests.java @@ -41,8 +41,8 @@ public void testPreferPrimaryAllocationOverReplicas() { logger.info("create several indices with no replicas, and wait till all are allocated"); Metadata metadata = Metadata.builder() - .put(IndexMetadata.builder("test1").settings(settings(IndexVersion.current())).numberOfShards(10).numberOfReplicas(0)) - .put(IndexMetadata.builder("test2").settings(settings(IndexVersion.current())).numberOfShards(10).numberOfReplicas(0)) + .put(IndexMetadata.builder("test1").settings(indexSettings(IndexVersion.current(), randomUUID(), 10, 0))) + .put(IndexMetadata.builder("test2").settings(indexSettings(IndexVersion.current(), randomUUID(), 10, 0))) .build(); RoutingTable initialRoutingTable = RoutingTable.builder(TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY) diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/RandomAllocationDeciderTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/RandomAllocationDeciderTests.java index 169cae3b815c8..b9f875747e1b8 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/RandomAllocationDeciderTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/RandomAllocationDeciderTests.java @@ -79,10 +79,7 @@ public void testRandomDecisions() { int numShards = scaledRandomIntBetween(1, 20); totalNumShards += numShards * (replicas + 1); metaBuilder.put( - IndexMetadata.builder("INDEX_" + i) - .settings(settings(IndexVersion.current())) - .numberOfShards(numShards) - .numberOfReplicas(replicas) + IndexMetadata.builder("INDEX_" + i).settings(indexSettings(IndexVersion.current(), randomUUID(), numShards, replicas)) ); } diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/ResizeAllocationDeciderTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/ResizeAllocationDeciderTests.java index cdf47ee1dc6cc..50f90ef12d12a 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/ResizeAllocationDeciderTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/ResizeAllocationDeciderTests.java @@ -123,7 +123,14 @@ public void testNonResizeRouting() { ClusterState clusterState = createInitialClusterState(true); ResizeAllocationDecider resizeAllocationDecider = new ResizeAllocationDecider(); RoutingAllocation routingAllocation = new RoutingAllocation(null, clusterState, null, null, 0); - ShardRouting shardRouting = TestShardRouting.newShardRouting("non-resize", 0, null, true, ShardRoutingState.UNASSIGNED); + ShardRouting shardRouting = TestShardRouting.newShardRouting( + "non-resize", + randomUUID(), + 0, + null, + true, + ShardRoutingState.UNASSIGNED + ); assertEquals(Decision.ALWAYS, resizeAllocationDecider.canAllocate(shardRouting, routingAllocation)); assertEquals( Decision.ALWAYS, diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/RoutingNodesTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/RoutingNodesTests.java index a053b5c6d515f..96d48b657db78 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/RoutingNodesTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/RoutingNodesTests.java @@ -70,8 +70,8 @@ public void testBalanceAllNodesStarted() { logger.info("Building initial routing table"); Metadata metadata = Metadata.builder() - .put(IndexMetadata.builder("test").settings(settings(IndexVersion.current())).numberOfShards(3).numberOfReplicas(1)) - .put(IndexMetadata.builder("test1").settings(settings(IndexVersion.current())).numberOfShards(3).numberOfReplicas(1)) + .put(IndexMetadata.builder("test").settings(indexSettings(IndexVersion.current(), randomUUID(), 3, 1))) + .put(IndexMetadata.builder("test1").settings(indexSettings(IndexVersion.current(), randomUUID(), 3, 1))) .build(); RoutingTable initialRoutingTable = RoutingTable.builder(TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY) @@ -136,8 +136,8 @@ public void testBalanceIncrementallyStartNodes() { logger.info("Building initial routing table"); Metadata metadata = Metadata.builder() - .put(IndexMetadata.builder("test").settings(settings(IndexVersion.current())).numberOfShards(3).numberOfReplicas(1)) - .put(IndexMetadata.builder("test1").settings(settings(IndexVersion.current())).numberOfShards(3).numberOfReplicas(1)) + .put(IndexMetadata.builder("test").settings(indexSettings(IndexVersion.current(), randomUUID(), 3, 1))) + .put(IndexMetadata.builder("test1").settings(indexSettings(IndexVersion.current(), randomUUID(), 3, 1))) .build(); RoutingTable initialRoutingTable = RoutingTable.builder(TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY) @@ -196,7 +196,7 @@ public void testBalanceAllNodesStartedAddIndex() { logger.info("Building initial routing table"); Metadata metadata = Metadata.builder() - .put(IndexMetadata.builder("test").settings(settings(IndexVersion.current())).numberOfShards(3).numberOfReplicas(1)) + .put(IndexMetadata.builder("test").settings(indexSettings(IndexVersion.current(), randomUUID(), 3, 1))) .build(); RoutingTable initialRoutingTable = RoutingTable.builder(TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY) diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/SameShardRoutingTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/SameShardRoutingTests.java index 713a72a8117ed..5be938d2cc532 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/SameShardRoutingTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/SameShardRoutingTests.java @@ -264,8 +264,8 @@ public void testSameHostCheckDisabledByAutoExpandReplicas() { public void testForceAllocatePrimaryOnSameNodeNotAllowed() { SameShardAllocationDecider decider = new SameShardAllocationDecider(createBuiltInClusterSettings()); - ClusterState clusterState = ClusterStateCreationUtils.state("idx", randomIntBetween(2, 4), 1); - Index index = clusterState.getMetadata().getProject().index("idx").getIndex(); + Index index = new Index("idx", randomUUID()); + ClusterState clusterState = ClusterStateCreationUtils.state(index, randomIntBetween(2, 4), 1); ShardRouting primaryShard = clusterState.routingTable().index(index).shard(0).primaryShard(); RoutingNode routingNode = clusterState.getRoutingNodes().node(primaryShard.currentNodeId()); RoutingAllocation routingAllocation = new RoutingAllocation( diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/ShardVersioningTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/ShardVersioningTests.java index 129f0681f0d52..6271440851fe7 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/ShardVersioningTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/ShardVersioningTests.java @@ -39,8 +39,8 @@ public void testSimple() { ); Metadata metadata = Metadata.builder() - .put(IndexMetadata.builder("test1").settings(settings(IndexVersion.current())).numberOfShards(1).numberOfReplicas(1)) - .put(IndexMetadata.builder("test2").settings(settings(IndexVersion.current())).numberOfShards(1).numberOfReplicas(1)) + .put(IndexMetadata.builder("test1").settings(indexSettings(IndexVersion.current(), randomUUID(), 1, 1))) + .put(IndexMetadata.builder("test2").settings(indexSettings(IndexVersion.current(), 1, 1))) .build(); RoutingTable routingTable = RoutingTable.builder(TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY) diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/ShardsLimitAllocationTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/ShardsLimitAllocationTests.java index aed63dd5cc163..ae7637a01611c 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/ShardsLimitAllocationTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/ShardsLimitAllocationTests.java @@ -22,6 +22,7 @@ import org.elasticsearch.cluster.routing.ShardRoutingState; import org.elasticsearch.cluster.routing.allocation.decider.ShardsLimitAllocationDecider; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexVersion; import static org.elasticsearch.cluster.routing.RoutingNodesHelper.numberOfShardsWithState; @@ -147,12 +148,13 @@ public void testIndexLevelShardsLimitRemain() { logger.info("Building initial routing table"); + Index testIndex = new Index("test", randomUUID()); Metadata metadata = Metadata.builder() - .put(IndexMetadata.builder("test").settings(indexSettings(IndexVersion.current(), 5, 0))) + .put(IndexMetadata.builder(testIndex.getName()).settings(indexSettings(IndexVersion.current(), testIndex.getUUID(), 5, 0))) .build(); RoutingTable initialRoutingTable = RoutingTable.builder(TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY) - .addAsNew(metadata.getProject().index("test")) + .addAsNew(metadata.getProject().index(testIndex)) .build(); ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(metadata).routingTable(initialRoutingTable).build(); @@ -166,13 +168,14 @@ public void testIndexLevelShardsLimitRemain() { assertThat(numberOfShardsWithState(clusterState.getRoutingNodes(), STARTED), equalTo(5)); logger.info("add another index with 5 shards"); + Index testIndex1 = new Index("test1", randomUUID()); metadata = Metadata.builder(clusterState.metadata()) - .put(IndexMetadata.builder("test1").settings(indexSettings(IndexVersion.current(), 5, 0))) + .put(IndexMetadata.builder(testIndex1.getName()).settings(indexSettings(IndexVersion.current(), testIndex1.getUUID(), 5, 0))) .build(); RoutingTable updatedRoutingTable = RoutingTable.builder( TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY, clusterState.routingTable() - ).addAsNew(metadata.getProject().index("test1")).build(); + ).addAsNew(metadata.getProject().index(testIndex1)).build(); clusterState = ClusterState.builder(clusterState).metadata(metadata).routingTable(updatedRoutingTable).build(); @@ -185,21 +188,21 @@ public void testIndexLevelShardsLimitRemain() { assertThat(numberOfShardsWithState(clusterState.getRoutingNodes(), STARTED), equalTo(10)); for (ShardRouting shardRouting : clusterState.getRoutingNodes().node("node1")) { - assertThat(shardRouting.getIndexName(), equalTo("test")); + assertThat(shardRouting.getIndexName(), equalTo(testIndex.getName())); } for (ShardRouting shardRouting : clusterState.getRoutingNodes().node("node2")) { - assertThat(shardRouting.getIndexName(), equalTo("test1")); + assertThat(shardRouting.getIndexName(), equalTo(testIndex1.getName())); } logger.info("update {} for test, see that things move", ShardsLimitAllocationDecider.INDEX_TOTAL_SHARDS_PER_NODE_SETTING.getKey()); + IndexMetadata indexMetadata = clusterState.metadata().getProject().index(testIndex); metadata = Metadata.builder(clusterState.metadata()) .put( - IndexMetadata.builder(clusterState.metadata().getProject().index("test")) + IndexMetadata.builder(indexMetadata) .settings( - indexSettings(IndexVersion.current(), 5, 0).put( - ShardsLimitAllocationDecider.INDEX_TOTAL_SHARDS_PER_NODE_SETTING.getKey(), - 3 - ) + Settings.builder() + .put(indexMetadata.getSettings()) + .put(ShardsLimitAllocationDecider.INDEX_TOTAL_SHARDS_PER_NODE_SETTING.getKey(), 3) ) ) .build(); @@ -216,7 +219,7 @@ public void testIndexLevelShardsLimitRemain() { // the first move will destroy the balance and the balancer will move 2 shards from node2 to node one right after // moving the nodes to node2 since we consider INITIALIZING nodes during rebalance clusterState = startInitializingShardsAndReroute(strategy, clusterState); - // now we are done compared to EvenShardCountAllocator since the Balancer is not soely based on the average + // now we are done compared to EvenShardCountAllocator since the Balancer is not solely based on the average assertThat(clusterState.getRoutingNodes().node("node1").numberOfShardsWithState(STARTED), equalTo(5)); assertThat(clusterState.getRoutingNodes().node("node2").numberOfShardsWithState(STARTED), equalTo(5)); } diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/SingleShardNoReplicasRoutingTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/SingleShardNoReplicasRoutingTests.java index 2027253754354..038f7b23b9685 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/SingleShardNoReplicasRoutingTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/SingleShardNoReplicasRoutingTests.java @@ -212,9 +212,7 @@ public void testMultiIndexEvenDistribution() { Metadata.Builder metadataBuilder = Metadata.builder(); for (int i = 0; i < numberOfIndices; i++) { - metadataBuilder.put( - IndexMetadata.builder("test" + i).settings(settings(IndexVersion.current())).numberOfShards(1).numberOfReplicas(0) - ); + metadataBuilder.put(IndexMetadata.builder("test" + i).settings(indexSettings(IndexVersion.current(), randomUUID(), 1, 0))); } Metadata metadata = metadataBuilder.build(); @@ -324,9 +322,7 @@ public void testMultiIndexUnevenNodes() { Metadata.Builder metadataBuilder = Metadata.builder(); for (int i = 0; i < numberOfIndices; i++) { - metadataBuilder.put( - IndexMetadata.builder("test" + i).settings(settings(IndexVersion.current())).numberOfShards(1).numberOfReplicas(0) - ); + metadataBuilder.put(IndexMetadata.builder("test" + i).settings(indexSettings(IndexVersion.current(), randomUUID(), 1, 0))); } Metadata metadata = metadataBuilder.build(); diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/allocator/BalancedShardsAllocatorTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/allocator/BalancedShardsAllocatorTests.java index 3667de9c65e4e..9d6bb566f7c05 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/allocator/BalancedShardsAllocatorTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/allocator/BalancedShardsAllocatorTests.java @@ -45,6 +45,7 @@ import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.core.Strings; import org.elasticsearch.core.Tuple; +import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexVersion; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.snapshots.SnapshotShardSizeInfo; @@ -87,7 +88,7 @@ public class BalancedShardsAllocatorTests extends ESAllocationTestCase { public void testDecideShardAllocation() { BalancedShardsAllocator allocator = new BalancedShardsAllocator(Settings.EMPTY); - ClusterState clusterState = ClusterStateCreationUtils.state("idx", false, ShardRoutingState.STARTED); + ClusterState clusterState = ClusterStateCreationUtils.state(new Index("idx", randomUUID()), false, ShardRoutingState.STARTED); assertEquals(clusterState.nodes().getSize(), 3); // add new index @@ -146,14 +147,14 @@ public void testDecideShardAllocationWhenThereAreMultipleProjects() { // Create some projects with some assigned indices for (int i = 1; i <= numberOfProjects; i++) { var projectId = ProjectId.fromId(Strings.format("proj_%02d", i)); - String[] indices = { + Index[] indices = { // 2 indices that are unique to this project - "index_proj_" + i + "a", - "index_proj_" + i + "b", + new Index("index_proj_" + i + "a", randomUUID()), + new Index("index_proj_" + i + "b", randomUUID()), // 1 index that is shared in all projects - "common_index", + new Index("common_index", randomUUID()), // 1 index that is shared with some projects - "common_index_" + (i % 3) }; + new Index("common_index_" + (i % 3), randomUUID()) }; final Tuple project = ClusterStateCreationUtils .projectWithAssignedPrimariesAndReplicas(projectId, indices, 1, 0, nodes); metadataBuilder.put(project.v1()); @@ -179,7 +180,7 @@ public void testDecideShardAllocationWhenThereAreMultipleProjects() { var origProject = randomFrom(clusterState.metadata().projects().values()); final var indexName = "new_index"; ProjectMetadata updatedProject = ProjectMetadata.builder(origProject) - .put(IndexMetadata.builder(indexName).settings(indexSettings(IndexVersion.current(), 1, 0))) + .put(IndexMetadata.builder(indexName).settings(indexSettings(IndexVersion.current(), randomUUID(), 1, 0))) .build(); GlobalRoutingTable routingTable = GlobalRoutingTable.builder(clusterState.globalRoutingTable()) .put( @@ -705,11 +706,11 @@ private static ClusterInfo createClusterInfo(Map indexSizes) { } private static IndexMetadata.Builder anIndex(String name) { - return anIndex(name, indexSettings(IndexVersion.current(), 1, 0)); + return anIndex(name, indexSettings(IndexVersion.current(), randomUUID(), 1, 0)); } private static IndexMetadata.Builder anIndex(String name, Settings.Builder settings) { - return IndexMetadata.builder(name).settings(settings); + return IndexMetadata.builder(name).settings(settings.put(IndexMetadata.SETTING_INDEX_UUID, randomUUID())); } private static ClusterState createStateWithIndices(IndexMetadata.Builder... indexMetadataBuilders) { @@ -769,7 +770,8 @@ private void addIndex( ) { var numberOfShards = assignments.entrySet().stream().mapToInt(Map.Entry::getValue).sum(); var inSyncIds = randomList(numberOfShards, numberOfShards, () -> UUIDs.randomBase64UUID(random())); - var indexMetadataBuilder = IndexMetadata.builder(name).settings(indexSettings(IndexVersion.current(), numberOfShards, 0)); + var indexMetadataBuilder = IndexMetadata.builder(name) + .settings(indexSettings(IndexVersion.current(), randomUUID(), numberOfShards, 0)); for (int shardId = 0; shardId < numberOfShards; shardId++) { indexMetadataBuilder.putInSyncAllocationIds(shardId, Set.of(inSyncIds.get(shardId))); diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/allocator/ClusterAllocationSimulationTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/allocator/ClusterAllocationSimulationTests.java index 6ef622948f5c5..1c9aa067a5252 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/allocator/ClusterAllocationSimulationTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/allocator/ClusterAllocationSimulationTests.java @@ -122,7 +122,7 @@ public void testBalanceQuality() throws IOException { metadataBuilder.put( IndexMetadata.builder(indexName) .settings( - indexSettings(IndexVersion.current(), shardCount, replicaCount).put( + indexSettings(IndexVersion.current(), randomUUID(), shardCount, replicaCount).put( IndexMetadata.INDEX_ROUTING_REQUIRE_GROUP_PREFIX + ".fake_tier", tier ) diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/allocator/ClusterBalanceStatsTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/allocator/ClusterBalanceStatsTests.java index 49976c172e88f..254d8be824ba4 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/allocator/ClusterBalanceStatsTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/allocator/ClusterBalanceStatsTests.java @@ -406,11 +406,7 @@ private static ClusterState createClusterState(List nodes, List UUIDs.randomBase64UUID(random())); var shardSize = randomLongBetween(10_000_000L, 10_000_000_000L); - var indexMetadataBuilder = IndexMetadata.builder(indexName).settings(indexSettings(IndexVersion.current(), shards, replicas)); + var indexMetadataBuilder = IndexMetadata.builder(indexName) + .settings(indexSettings(IndexVersion.current(), randomUUID(), shards, replicas)); if (randomBoolean()) { indexMetadataBuilder.shardSizeInBytesForecast(smallShardSizeDeviation(shardSize)); } @@ -757,7 +759,9 @@ public void testComputeConsideringShardSizes() { metadataBuilder.put( IndexMetadata.builder(indexName) - .settings(indexSettings(IndexVersion.current(), 1, 1).put("index.routing.allocation.exclude._id", "node-2")) + .settings( + indexSettings(IndexVersion.current(), randomUUID(), 1, 1).put("index.routing.allocation.exclude._id", "node-2") + ) ); var indexId = metadataBuilder.get(indexName).getIndex(); @@ -790,7 +794,9 @@ public void testComputeConsideringShardSizes() { metadataBuilder.put( IndexMetadata.builder(indexName) - .settings(indexSettings(IndexVersion.current(), 1, 0).put("index.routing.allocation.exclude._id", "node-2")) + .settings( + indexSettings(IndexVersion.current(), randomUUID(), 1, 0).put("index.routing.allocation.exclude._id", "node-2") + ) ); var indexId = metadataBuilder.get(indexName).getIndex(); @@ -877,21 +883,27 @@ public void testAccountForSizeOfMisplacedShardsDuringNewComputation() { var routingTableBuilder = RoutingTable.builder(TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY); // index-1 is allocated according to the desired balance - var indexMetadata1 = IndexMetadata.builder("index-1").settings(indexSettings(IndexVersion.current(), 2, 0)).build(); + var index1 = new Index("index-1", randomUUID()); + var index1ShardId_0 = new ShardId(index1, 0); + var index1ShardId_1 = new ShardId(index1, 1); + var indexMetadata1 = IndexMetadata.builder(index1.getName()) + .settings(indexSettings(IndexVersion.current(), index1.getUUID(), 2, 0)) + .build(); routingTableBuilder.add( IndexRoutingTable.builder(indexMetadata1.getIndex()) - .addShard(newShardRouting(shardIdFrom(indexMetadata1, 0), "node-1", true, STARTED)) - .addShard(newShardRouting(shardIdFrom(indexMetadata1, 1), "node-2", true, STARTED)) + .addShard(newShardRouting(index1ShardId_0, "node-1", true, STARTED)) + .addShard(newShardRouting(index1ShardId_1, "node-2", true, STARTED)) ); - clusterInfoBuilder.withShard(shardIdFrom(indexMetadata1, 0), true, ByteSizeValue.ofGb(8).getBytes()) - .withShard(shardIdFrom(indexMetadata1, 1), true, ByteSizeValue.ofGb(8).getBytes()); + clusterInfoBuilder.withShard(index1ShardId_0, true, ByteSizeValue.ofGb(8).getBytes()) + .withShard(index1ShardId_1, true, ByteSizeValue.ofGb(8).getBytes()); // index-2 is restored earlier but is not started on the desired node yet - var indexMetadata2 = IndexMetadata.builder("index-2").settings(indexSettings(IndexVersion.current(), 1, 0)).build(); - snapshotShardSizes.put( - new SnapshotShard(snapshot, indexIdFrom(indexMetadata2), shardIdFrom(indexMetadata2, 0)), - ByteSizeValue.ofGb(1).getBytes() - ); + var index2 = new Index("index-2", randomUUID()); + var index2ShardId = new ShardId(index2, 0); + var indexMetadata2 = IndexMetadata.builder(index2.getName()) + .settings(indexSettings(IndexVersion.current(), index2.getUUID(), 1, 0)) + .build(); + snapshotShardSizes.put(new SnapshotShard(snapshot, indexIdFrom(indexMetadata2), index2ShardId), ByteSizeValue.ofGb(1).getBytes()); var index2SnapshotRecoverySource = new RecoverySource.SnapshotRecoverySource( "restore", snapshot, @@ -903,9 +915,8 @@ public void testAccountForSizeOfMisplacedShardsDuringNewComputation() { case 0 -> routingTableBuilder.addAsNewRestore(indexMetadata2, index2SnapshotRecoverySource, Set.of()); // index is initializing on desired node case 1 -> { - ShardId index2ShardId = shardIdFrom(indexMetadata2, 0); routingTableBuilder.add( - IndexRoutingTable.builder(indexMetadata2.getIndex()) + IndexRoutingTable.builder(index2) .addShard( shardRoutingBuilder(index2ShardId, "node-1", true, INITIALIZING).withRecoverySource( index2SnapshotRecoverySource @@ -921,9 +932,8 @@ public void testAccountForSizeOfMisplacedShardsDuringNewComputation() { } // index is initializing on undesired node case 2 -> { - ShardId index2ShardId = shardIdFrom(indexMetadata2, 0); routingTableBuilder.add( - IndexRoutingTable.builder(indexMetadata2.getIndex()) + IndexRoutingTable.builder(index2) .addShard( shardRoutingBuilder(index2ShardId, "node-2", true, INITIALIZING).withRecoverySource( index2SnapshotRecoverySource @@ -940,28 +950,32 @@ public void testAccountForSizeOfMisplacedShardsDuringNewComputation() { // index is started on undesired node case 3 -> { routingTableBuilder.add( - IndexRoutingTable.builder(indexMetadata2.getIndex()) - .addShard(newShardRouting(shardIdFrom(indexMetadata2, 0), "node-2", true, STARTED)) + IndexRoutingTable.builder(index2).addShard(newShardRouting(index2ShardId, "node-2", true, STARTED)) ); clusterInfoBuilder.withNodeUsedSpace("node-2", ByteSizeValue.ofGb(1).getBytes()) - .withShard(shardIdFrom(indexMetadata2, 0), true, ByteSizeValue.ofGb(1).getBytes()); + .withShard(index2ShardId, true, ByteSizeValue.ofGb(1).getBytes()); } default -> throw new AssertionError("unexpected randomization"); } // index-3 is restored as new from snapshot - var indexMetadata3 = IndexMetadata.builder("index-3").settings(indexSettings(IndexVersion.current(), 2, 0)).build(); + var index3 = new Index("index-3", randomUUID()); + var index3ShardId_0 = new ShardId(index3, 0); + var index3ShardId_1 = new ShardId(index3, 1); + var indexMetadata3 = IndexMetadata.builder(index3.getName()) + .settings(indexSettings(IndexVersion.current(), index3.getUUID(), 2, 0)) + .build(); routingTableBuilder.addAsNewRestore( indexMetadata3, new RecoverySource.SnapshotRecoverySource("restore", snapshot, IndexVersion.current(), indexIdFrom(indexMetadata3)), Set.of() ); snapshotShardSizes.put( - new SnapshotShard(snapshot, indexIdFrom(indexMetadata3), shardIdFrom(indexMetadata3, 0)), + new SnapshotShard(snapshot, indexIdFrom(indexMetadata3), index3ShardId_0), ByteSizeValue.ofMb(512).getBytes() ); snapshotShardSizes.put( - new SnapshotShard(snapshot, indexIdFrom(indexMetadata3), shardIdFrom(indexMetadata3, 1)), + new SnapshotShard(snapshot, indexIdFrom(indexMetadata3), index3ShardId_1), ByteSizeValue.ofMb(512).getBytes() ); @@ -978,11 +992,11 @@ public void testAccountForSizeOfMisplacedShardsDuringNewComputation() { snapshot, RestoreInProgress.State.STARTED, randomBoolean(), - List.of(indexMetadata2.getIndex().getName(), indexMetadata3.getIndex().getName()), + List.of(index2.getName(), index3.getName()), Map.ofEntries( - Map.entry(shardIdFrom(indexMetadata2, 0), new RestoreInProgress.ShardRestoreStatus(randomUUID())), - Map.entry(shardIdFrom(indexMetadata3, 0), new RestoreInProgress.ShardRestoreStatus(randomUUID())), - Map.entry(shardIdFrom(indexMetadata3, 1), new RestoreInProgress.ShardRestoreStatus(randomUUID())) + Map.entry(index2ShardId, new RestoreInProgress.ShardRestoreStatus(randomUUID())), + Map.entry(index3ShardId_0, new RestoreInProgress.ShardRestoreStatus(randomUUID())), + Map.entry(index3ShardId_1, new RestoreInProgress.ShardRestoreStatus(randomUUID())) ) ) ).build() @@ -1001,9 +1015,9 @@ public void testAccountForSizeOfMisplacedShardsDuringNewComputation() { var initialDesiredBalance = new DesiredBalance( 1, Map.ofEntries( - Map.entry(shardIdFrom(indexMetadata1, 0), new ShardAssignment(Set.of("node-1"), 1, 0, 0)), - Map.entry(shardIdFrom(indexMetadata1, 1), new ShardAssignment(Set.of("node-2"), 1, 0, 0)), - Map.entry(shardIdFrom(indexMetadata2, 0), new ShardAssignment(Set.of("node-1"), 1, 0, 0)) + Map.entry(index1ShardId_0, new ShardAssignment(Set.of("node-1"), 1, 0, 0)), + Map.entry(index1ShardId_1, new ShardAssignment(Set.of("node-2"), 1, 0, 0)), + Map.entry(index2ShardId, new ShardAssignment(Set.of("node-1"), 1, 0, 0)) ) ); var nextDesiredBalance = createDesiredBalanceComputer(new BalancedShardsAllocator()).compute( @@ -1019,20 +1033,20 @@ public void testAccountForSizeOfMisplacedShardsDuringNewComputation() { anyOf( equalTo( Map.ofEntries( - Map.entry(shardIdFrom(indexMetadata1, 0), new ShardAssignment(Set.of("node-1"), 1, 0, 0)), - Map.entry(shardIdFrom(indexMetadata1, 1), new ShardAssignment(Set.of("node-2"), 1, 0, 0)), - Map.entry(shardIdFrom(indexMetadata2, 0), new ShardAssignment(Set.of("node-1"), 1, 0, 0)), - Map.entry(shardIdFrom(indexMetadata3, 0), new ShardAssignment(Set.of("node-2"), 1, 0, 0)), - Map.entry(shardIdFrom(indexMetadata3, 1), new ShardAssignment(Set.of("node-2"), 1, 0, 0)) + Map.entry(index1ShardId_0, new ShardAssignment(Set.of("node-1"), 1, 0, 0)), + Map.entry(index1ShardId_1, new ShardAssignment(Set.of("node-2"), 1, 0, 0)), + Map.entry(index2ShardId, new ShardAssignment(Set.of("node-1"), 1, 0, 0)), + Map.entry(index3ShardId_0, new ShardAssignment(Set.of("node-2"), 1, 0, 0)), + Map.entry(index3ShardId_1, new ShardAssignment(Set.of("node-2"), 1, 0, 0)) ) ), equalTo( Map.ofEntries( - Map.entry(shardIdFrom(indexMetadata1, 0), new ShardAssignment(Set.of("node-1"), 1, 0, 0)), - Map.entry(shardIdFrom(indexMetadata1, 1), new ShardAssignment(Set.of("node-2"), 1, 0, 0)), - Map.entry(shardIdFrom(indexMetadata2, 0), new ShardAssignment(Set.of("node-2"), 1, 0, 0)), - Map.entry(shardIdFrom(indexMetadata3, 0), new ShardAssignment(Set.of("node-1"), 1, 0, 0)), - Map.entry(shardIdFrom(indexMetadata3, 1), new ShardAssignment(Set.of("node-1"), 1, 0, 0)) + Map.entry(index1ShardId_0, new ShardAssignment(Set.of("node-1"), 1, 0, 0)), + Map.entry(index1ShardId_1, new ShardAssignment(Set.of("node-2"), 1, 0, 0)), + Map.entry(index2ShardId, new ShardAssignment(Set.of("node-2"), 1, 0, 0)), + Map.entry(index3ShardId_0, new ShardAssignment(Set.of("node-1"), 1, 0, 0)), + Map.entry(index3ShardId_1, new ShardAssignment(Set.of("node-1"), 1, 0, 0)) ) ) ) @@ -1052,31 +1066,38 @@ public void testAccountForSizeOfAllInitializingShardsDuringAllocation() { var routingTableBuilder = RoutingTable.builder(TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY); // index-1 is allocated according to the desired balance - var indexMetadata1 = IndexMetadata.builder("index-1").settings(indexSettings(IndexVersion.current(), 2, 0)).build(); + var index1 = new Index("index-1", randomUUID()); + var index1ShardId_0 = new ShardId(index1, 0); + var index1ShardId_1 = new ShardId(index1, 1); + var indexMetadata1 = IndexMetadata.builder(index1.getName()) + .settings(indexSettings(IndexVersion.current(), index1.getUUID(), 2, 0)) + .build(); routingTableBuilder.add( - IndexRoutingTable.builder(indexMetadata1.getIndex()) - .addShard(newShardRouting(shardIdFrom(indexMetadata1, 0), "node-1", true, STARTED)) - .addShard(newShardRouting(shardIdFrom(indexMetadata1, 1), "node-2", true, STARTED)) + IndexRoutingTable.builder(index1) + .addShard(newShardRouting(index1ShardId_0, "node-1", true, STARTED)) + .addShard(newShardRouting(index1ShardId_1, "node-2", true, STARTED)) ); - clusterInfoBuilder.withShard(shardIdFrom(indexMetadata1, 0), true, ByteSizeValue.ofGb(8).getBytes()) - .withShard(shardIdFrom(indexMetadata1, 1), true, ByteSizeValue.ofGb(8).getBytes()); + clusterInfoBuilder.withShard(index1ShardId_0, true, ByteSizeValue.ofGb(8).getBytes()) + .withShard(index1ShardId_1, true, ByteSizeValue.ofGb(8).getBytes()); // index-2 & index-3 are restored as new from snapshot - var indexMetadata2 = IndexMetadata.builder("index-2") - .settings(indexSettings(IndexVersion.current(), 1, 0).put(IndexMetadata.INDEX_PRIORITY_SETTING.getKey(), 2)) + var index2 = new Index("index-2", randomUUID()); + var index2ShardId = new ShardId(index2, 0); + var indexMetadata2 = IndexMetadata.builder(index2.getName()) + .settings(indexSettings(IndexVersion.current(), index2.getUUID(), 1, 0).put(IndexMetadata.INDEX_PRIORITY_SETTING.getKey(), 2)) .build(); routingTableBuilder.addAsNewRestore( indexMetadata2, new RecoverySource.SnapshotRecoverySource("restore", snapshot, IndexVersion.current(), indexIdFrom(indexMetadata2)), Set.of() ); - snapshotShardSizes.put( - new SnapshotShard(snapshot, indexIdFrom(indexMetadata2), shardIdFrom(indexMetadata2, 0)), - ByteSizeValue.ofGb(1).getBytes() - ); + snapshotShardSizes.put(new SnapshotShard(snapshot, indexIdFrom(indexMetadata2), index2ShardId), ByteSizeValue.ofGb(1).getBytes()); - var indexMetadata3 = IndexMetadata.builder("index-3") - .settings(indexSettings(IndexVersion.current(), 2, 0).put(IndexMetadata.INDEX_PRIORITY_SETTING.getKey(), 1)) + var index3 = new Index("index-3", randomUUID()); + var index3ShardId_0 = new ShardId(index3, 0); + var index3ShardId_1 = new ShardId(index3, 1); + var indexMetadata3 = IndexMetadata.builder(index3.getName()) + .settings(indexSettings(IndexVersion.current(), index3.getUUID(), 2, 0).put(IndexMetadata.INDEX_PRIORITY_SETTING.getKey(), 1)) .build(); routingTableBuilder.addAsNewRestore( indexMetadata3, @@ -1084,11 +1105,11 @@ public void testAccountForSizeOfAllInitializingShardsDuringAllocation() { Set.of() ); snapshotShardSizes.put( - new SnapshotShard(snapshot, indexIdFrom(indexMetadata3), shardIdFrom(indexMetadata3, 0)), + new SnapshotShard(snapshot, indexIdFrom(indexMetadata3), index3ShardId_0), ByteSizeValue.ofMb(512).getBytes() ); snapshotShardSizes.put( - new SnapshotShard(snapshot, indexIdFrom(indexMetadata3), shardIdFrom(indexMetadata3, 1)), + new SnapshotShard(snapshot, indexIdFrom(indexMetadata3), index3ShardId_1), ByteSizeValue.ofMb(512).getBytes() ); @@ -1105,11 +1126,11 @@ public void testAccountForSizeOfAllInitializingShardsDuringAllocation() { snapshot, RestoreInProgress.State.STARTED, randomBoolean(), - List.of(indexMetadata2.getIndex().getName(), indexMetadata3.getIndex().getName()), + List.of(index2.getName(), index3.getName()), Map.ofEntries( - Map.entry(shardIdFrom(indexMetadata2, 0), new RestoreInProgress.ShardRestoreStatus(randomUUID())), - Map.entry(shardIdFrom(indexMetadata3, 0), new RestoreInProgress.ShardRestoreStatus(randomUUID())), - Map.entry(shardIdFrom(indexMetadata3, 1), new RestoreInProgress.ShardRestoreStatus(randomUUID())) + Map.entry(index2ShardId, new RestoreInProgress.ShardRestoreStatus(randomUUID())), + Map.entry(index3ShardId_0, new RestoreInProgress.ShardRestoreStatus(randomUUID())), + Map.entry(index3ShardId_1, new RestoreInProgress.ShardRestoreStatus(randomUUID())) ) ) ).build() @@ -1128,8 +1149,8 @@ public void testAccountForSizeOfAllInitializingShardsDuringAllocation() { var initialDesiredBalance = new DesiredBalance( 1, Map.ofEntries( - Map.entry(shardIdFrom(indexMetadata1, 0), new ShardAssignment(Set.of("node-1"), 1, 0, 0)), - Map.entry(shardIdFrom(indexMetadata1, 1), new ShardAssignment(Set.of("node-2"), 1, 0, 0)) + Map.entry(index1ShardId_0, new ShardAssignment(Set.of("node-1"), 1, 0, 0)), + Map.entry(index1ShardId_1, new ShardAssignment(Set.of("node-2"), 1, 0, 0)) ) ); var nextDesiredBalance = createDesiredBalanceComputer(new BalancedShardsAllocator()).compute( @@ -1145,20 +1166,20 @@ public void testAccountForSizeOfAllInitializingShardsDuringAllocation() { anyOf( equalTo( Map.ofEntries( - Map.entry(shardIdFrom(indexMetadata1, 0), new ShardAssignment(Set.of("node-1"), 1, 0, 0)), - Map.entry(shardIdFrom(indexMetadata1, 1), new ShardAssignment(Set.of("node-2"), 1, 0, 0)), - Map.entry(shardIdFrom(indexMetadata2, 0), new ShardAssignment(Set.of("node-1"), 1, 0, 0)), - Map.entry(shardIdFrom(indexMetadata3, 0), new ShardAssignment(Set.of("node-2"), 1, 0, 0)), - Map.entry(shardIdFrom(indexMetadata3, 1), new ShardAssignment(Set.of("node-2"), 1, 0, 0)) + Map.entry(index1ShardId_0, new ShardAssignment(Set.of("node-1"), 1, 0, 0)), + Map.entry(index1ShardId_1, new ShardAssignment(Set.of("node-2"), 1, 0, 0)), + Map.entry(index2ShardId, new ShardAssignment(Set.of("node-1"), 1, 0, 0)), + Map.entry(index3ShardId_0, new ShardAssignment(Set.of("node-2"), 1, 0, 0)), + Map.entry(index3ShardId_1, new ShardAssignment(Set.of("node-2"), 1, 0, 0)) ) ), equalTo( Map.ofEntries( - Map.entry(shardIdFrom(indexMetadata1, 0), new ShardAssignment(Set.of("node-1"), 1, 0, 0)), - Map.entry(shardIdFrom(indexMetadata1, 1), new ShardAssignment(Set.of("node-2"), 1, 0, 0)), - Map.entry(shardIdFrom(indexMetadata2, 0), new ShardAssignment(Set.of("node-2"), 1, 0, 0)), - Map.entry(shardIdFrom(indexMetadata3, 0), new ShardAssignment(Set.of("node-1"), 1, 0, 0)), - Map.entry(shardIdFrom(indexMetadata3, 1), new ShardAssignment(Set.of("node-1"), 1, 0, 0)) + Map.entry(index1ShardId_0, new ShardAssignment(Set.of("node-1"), 1, 0, 0)), + Map.entry(index1ShardId_1, new ShardAssignment(Set.of("node-2"), 1, 0, 0)), + Map.entry(index2ShardId, new ShardAssignment(Set.of("node-2"), 1, 0, 0)), + Map.entry(index3ShardId_0, new ShardAssignment(Set.of("node-1"), 1, 0, 0)), + Map.entry(index3ShardId_1, new ShardAssignment(Set.of("node-1"), 1, 0, 0)) ) ) ) diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/allocator/DesiredBalanceReconcilerTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/allocator/DesiredBalanceReconcilerTests.java index 1f8d59a958bfe..9b4abcff327d6 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/allocator/DesiredBalanceReconcilerTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/allocator/DesiredBalanceReconcilerTests.java @@ -1328,7 +1328,9 @@ public void testShouldLogOnTooManyUndesiredAllocations() { final var metadataBuilder = Metadata.builder(); final var routingTableBuilder = RoutingTable.builder(); for (int i = 0; i < shardCount; i++) { - final var indexMetadata = IndexMetadata.builder("index-" + i).settings(indexSettings(IndexVersion.current(), 1, 0)).build(); + final var indexMetadata = IndexMetadata.builder("index-" + i) + .settings(indexSettings(IndexVersion.current(), randomUUID(), 1, 0)) + .build(); final var index = indexMetadata.getIndex(); final var shardId = new ShardId(index, 0); metadataBuilder.put(indexMetadata, false); diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/allocator/DesiredBalanceShardsAllocatorTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/allocator/DesiredBalanceShardsAllocatorTests.java index 21d547c1593b8..cc75661c1176a 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/allocator/DesiredBalanceShardsAllocatorTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/allocator/DesiredBalanceShardsAllocatorTests.java @@ -994,7 +994,7 @@ protected void submitReconcileTask(DesiredBalance desiredBalance) { } private static IndexMetadata createIndex(String name) { - return IndexMetadata.builder(name).settings(indexSettings(IndexVersion.current(), 1, 0)).build(); + return IndexMetadata.builder(name).settings(indexSettings(IndexVersion.current(), randomUUID(), 1, 0)).build(); } private static AllocationService createAllocationService( diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/allocator/OrderedShardsIteratorTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/allocator/OrderedShardsIteratorTests.java index c4345d50c394e..e002a7d25555d 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/allocator/OrderedShardsIteratorTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/allocator/OrderedShardsIteratorTests.java @@ -119,10 +119,10 @@ public void testShouldOrderShardByPriority() { IndexMetadata lookup = IndexMetadata.builder("lookup").settings(indexSettings(IndexVersion.current(), 1, 0)).build(); IndexMetadata ds1 = IndexMetadata.builder(".ds-data-stream-2024.04.18-000001") - .settings(indexSettings(IndexVersion.current(), 1, 0)) + .settings(indexSettings(IndexVersion.current(), randomUUID(), 1, 0)) .build(); IndexMetadata ds2 = IndexMetadata.builder(".ds-data-stream-2024.04.18-000002") - .settings(indexSettings(IndexVersion.current(), 1, 0)) + .settings(indexSettings(IndexVersion.current(), randomUUID(), 1, 0)) .build(); var metadata = Metadata.builder() @@ -231,9 +231,9 @@ public void testShouldOrderShardByPriorityAcrossMultipleProjects() { private Metadata createMetadata(RoutingTable routingTable) { final ProjectMetadata.Builder project = ProjectMetadata.builder(Metadata.DEFAULT_PROJECT_ID); for (var idxRoutingTable : routingTable) { - final String indexName = idxRoutingTable.getIndex().getName(); - final IndexMetadata indexMetadata = IndexMetadata.builder(indexName) - .settings(indexSettings(IndexVersion.current(), 1, 1)) + final Index index = idxRoutingTable.getIndex(); + final IndexMetadata indexMetadata = IndexMetadata.builder(index.getName()) + .settings(indexSettings(IndexVersion.current(), index.getUUID(), 1, 1)) .build(); project.put(indexMetadata, false); } @@ -261,7 +261,7 @@ private static RoutingAllocation createRoutingAllocation(DiscoveryNodes nodes, M } private static IndexRoutingTable index(String indexName, String nodeId) { - return index(new Index(indexName, "_na_"), nodeId); + return index(new Index(indexName, randomUUID()), nodeId); } private static IndexRoutingTable index(Index index, String nodeId) { diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/DiskThresholdDeciderTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/DiskThresholdDeciderTests.java index c4ca84e6e977f..a151787415766 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/DiskThresholdDeciderTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/DiskThresholdDeciderTests.java @@ -866,16 +866,13 @@ private void doTestCanRemainWithShardRelocatingAway(boolean testMaxHeadroom) { DiscoveryNode discoveryNode1 = newNode("node1"); DiscoveryNode discoveryNode2 = newNode("node2"); - - var testMetadata = IndexMetadata.builder("test") - .settings(settings(IndexVersion.current())) - .numberOfShards(2) - .numberOfReplicas(0) + Index testIndex = new Index("test", randomUUID()); + var testMetadata = IndexMetadata.builder(testIndex.getName()) + .settings(indexSettings(IndexVersion.current(), testIndex.getUUID(), 2, 0)) .build(); - var fooMetadata = IndexMetadata.builder("foo") - .settings(settings(IndexVersion.current())) - .numberOfShards(1) - .numberOfReplicas(0) + Index fooIndex = new Index("foo", randomUUID()); + var fooMetadata = IndexMetadata.builder(fooIndex.getName()) + .settings(indexSettings(IndexVersion.current(), fooIndex.getUUID(), 1, 0)) .build(); ClusterState baseClusterState = ClusterState.builder(ClusterName.DEFAULT) .metadata(Metadata.builder().put(testMetadata, false).put(fooMetadata, false).build()) @@ -885,9 +882,12 @@ private void doTestCanRemainWithShardRelocatingAway(boolean testMaxHeadroom) { .nodes(DiscoveryNodes.builder().add(discoveryNode1).add(discoveryNode2).build()) .build(); + ShardId shard0Id = new ShardId(testIndex, 0); + ShardId shard1Id = new ShardId(testIndex, 1); + // Two shards consuming each 80% of disk space while 70% is allowed, so shard 0 isn't allowed here - ShardRouting firstRouting = TestShardRouting.newShardRouting("test", 0, "node1", null, true, ShardRoutingState.STARTED); - ShardRouting secondRouting = TestShardRouting.newShardRouting("test", 1, "node1", null, true, ShardRoutingState.STARTED); + ShardRouting firstRouting = TestShardRouting.newShardRouting(shard0Id, "node1", null, true, ShardRoutingState.STARTED); + ShardRouting secondRouting = TestShardRouting.newShardRouting(shard1Id, "node1", null, true, ShardRoutingState.STARTED); RoutingNode firstRoutingNode = RoutingNodesHelper.routingNode("node1", discoveryNode1, firstRouting, secondRouting); RoutingTable.Builder builder = RoutingTable.builder() .add( @@ -906,7 +906,7 @@ private void doTestCanRemainWithShardRelocatingAway(boolean testMaxHeadroom) { ); routingAllocation.debugDecision(true); Decision decision = diskThresholdDecider.canRemain( - routingAllocation.metadata().getProject().getIndexSafe(firstRouting.index()), + routingAllocation.metadata().getProject().getIndexSafe(testIndex), firstRouting, firstRoutingNode, routingAllocation @@ -926,9 +926,9 @@ private void doTestCanRemainWithShardRelocatingAway(boolean testMaxHeadroom) { ); // Two shards consuming each 80% of disk space while 70% is allowed, but one is relocating, so shard 0 can stay - firstRouting = TestShardRouting.newShardRouting("test", 0, "node1", null, true, ShardRoutingState.STARTED); - secondRouting = TestShardRouting.newShardRouting("test", 1, "node1", "node2", true, ShardRoutingState.RELOCATING); - ShardRouting fooRouting = TestShardRouting.newShardRouting("foo", 0, null, true, ShardRoutingState.UNASSIGNED); + firstRouting = TestShardRouting.newShardRouting(shard0Id, "node1", null, true, ShardRoutingState.STARTED); + secondRouting = TestShardRouting.newShardRouting(shard1Id, "node1", "node2", true, ShardRoutingState.RELOCATING); + ShardRouting fooRouting = TestShardRouting.newShardRouting(fooIndex, 0, null, true, ShardRoutingState.UNASSIGNED); fooRouting = fooRouting.updateUnassigned( new UnassignedInfo( fooRouting.unassignedInfo().reason(), diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/DiskThresholdDeciderUnitTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/DiskThresholdDeciderUnitTests.java index debb4343931d7..30ce882e79c25 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/DiskThresholdDeciderUnitTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/DiskThresholdDeciderUnitTests.java @@ -524,9 +524,16 @@ public void testShardSizeAndRelocatingSize() { public void testTakesIntoAccountExpectedSizeForInitializingSearchableSnapshots() { var searchableSnapshotIndex = IndexMetadata.builder("searchable_snapshot") - .settings(indexSettings(IndexVersion.current(), 3, 0).put(INDEX_STORE_TYPE_SETTING.getKey(), SEARCHABLE_SNAPSHOT_STORE_TYPE)) + .settings( + indexSettings(IndexVersion.current(), randomUUID(), 3, 0).put( + INDEX_STORE_TYPE_SETTING.getKey(), + SEARCHABLE_SNAPSHOT_STORE_TYPE + ) + ) + .build(); + var regularIndex = IndexMetadata.builder("regular_index") + .settings(indexSettings(IndexVersion.current(), randomUUID(), 1, 0)) .build(); - var regularIndex = IndexMetadata.builder("regular_index").settings(indexSettings(IndexVersion.current(), 1, 0)).build(); String nodeId = "node1"; var knownShardSizes = new HashMap(); diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/EnableAllocationShortCircuitTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/EnableAllocationShortCircuitTests.java index ba0c431493f57..56ca1888731df 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/EnableAllocationShortCircuitTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/EnableAllocationShortCircuitTests.java @@ -61,9 +61,7 @@ private static ClusterState createClusterStateWithAllShardsAssigned() { final RoutingTable.Builder routingTableBuilder = RoutingTable.builder(TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY); for (int i = randomIntBetween(1, 10); i >= 0; i--) { final IndexMetadata indexMetadata = IndexMetadata.builder("test" + i) - .settings(settings(IndexVersion.current())) - .numberOfShards(1) - .numberOfReplicas(randomIntBetween(0, numberOfNodes - 1)) + .settings(indexSettings(IndexVersion.current(), randomUUID(), 1, randomIntBetween(0, numberOfNodes - 1))) .build(); metadataBuilder.put(indexMetadata, true); routingTableBuilder.addAsNew(indexMetadata); diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/EnableAllocationTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/EnableAllocationTests.java index edba0f74c6551..7b62772aabf69 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/EnableAllocationTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/EnableAllocationTests.java @@ -170,13 +170,11 @@ public void testEnableClusterBalance() { .put( IndexMetadata.builder("always_disabled") .settings( - settings(IndexVersion.current()).put( + indexSettings(IndexVersion.current(), randomUUID(), 1, 1).put( EnableAllocationDecider.INDEX_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), Rebalance.NONE ) ) - .numberOfShards(1) - .numberOfReplicas(1) ) .build(); diff --git a/server/src/test/java/org/elasticsearch/gateway/PriorityComparatorTests.java b/server/src/test/java/org/elasticsearch/gateway/PriorityComparatorTests.java index a1e6463807388..0212a15a8fb73 100644 --- a/server/src/test/java/org/elasticsearch/gateway/PriorityComparatorTests.java +++ b/server/src/test/java/org/elasticsearch/gateway/PriorityComparatorTests.java @@ -34,10 +34,10 @@ public class PriorityComparatorTests extends ESTestCase { public void testPreferNewIndices() { RoutingNodes.UnassignedShards shards = new RoutingNodes.UnassignedShards(mock(RoutingNodes.class)); List shardRoutings = Arrays.asList( - shardRoutingBuilder("oldest", 0, null, randomBoolean(), ShardRoutingState.UNASSIGNED).withUnassignedInfo( + shardRoutingBuilder("oldest", randomUUID(), 0, null, randomBoolean(), ShardRoutingState.UNASSIGNED).withUnassignedInfo( randomUnassignedInfo("foobar") ).build(), - shardRoutingBuilder("newest", 0, null, randomBoolean(), ShardRoutingState.UNASSIGNED).withUnassignedInfo( + shardRoutingBuilder("newest", randomUUID(), 0, null, randomBoolean(), ShardRoutingState.UNASSIGNED).withUnassignedInfo( randomUnassignedInfo("foobar") ).build() ); @@ -71,10 +71,10 @@ protected IndexMetadata getMetadata(Index index) { public void testPreferPriorityIndices() { RoutingNodes.UnassignedShards shards = new RoutingNodes.UnassignedShards(mock(RoutingNodes.class)); List shardRoutings = Arrays.asList( - shardRoutingBuilder("oldest", 0, null, randomBoolean(), ShardRoutingState.UNASSIGNED).withUnassignedInfo( + shardRoutingBuilder("oldest", randomUUID(), 0, null, randomBoolean(), ShardRoutingState.UNASSIGNED).withUnassignedInfo( randomUnassignedInfo("foobar") ).build(), - shardRoutingBuilder("newest", 0, null, randomBoolean(), ShardRoutingState.UNASSIGNED).withUnassignedInfo( + shardRoutingBuilder("newest", randomUUID(), 0, null, randomBoolean(), ShardRoutingState.UNASSIGNED).withUnassignedInfo( randomUnassignedInfo("foobar") ).build() ); @@ -108,10 +108,10 @@ protected IndexMetadata getMetadata(Index index) { public void testPreferSystemIndices() { RoutingNodes.UnassignedShards shards = new RoutingNodes.UnassignedShards(mock(RoutingNodes.class)); List shardRoutings = Arrays.asList( - shardRoutingBuilder("oldest", 0, null, randomBoolean(), ShardRoutingState.UNASSIGNED).withUnassignedInfo( + shardRoutingBuilder("oldest", randomUUID(), 0, null, randomBoolean(), ShardRoutingState.UNASSIGNED).withUnassignedInfo( randomUnassignedInfo("foobar") ).build(), - shardRoutingBuilder("newest", 0, null, randomBoolean(), ShardRoutingState.UNASSIGNED).withUnassignedInfo( + shardRoutingBuilder("newest", randomUUID(), 0, null, randomBoolean(), ShardRoutingState.UNASSIGNED).withUnassignedInfo( randomUnassignedInfo("foobar") ).build() ); @@ -175,13 +175,9 @@ public void testPriorityComparatorSort() { for (int i = 0; i < numShards; i++) { IndexMetadata indexMeta = randomFrom(indices); shards.add( - shardRoutingBuilder( - indexMeta.getIndex().getName(), - randomIntBetween(1, 5), - null, - randomBoolean(), - ShardRoutingState.UNASSIGNED - ).withUnassignedInfo(randomUnassignedInfo("foobar")).build() + shardRoutingBuilder(indexMeta.getIndex(), randomIntBetween(1, 5), null, randomBoolean(), ShardRoutingState.UNASSIGNED) + .withUnassignedInfo(randomUnassignedInfo("foobar")) + .build() ); } shards.sort(new PriorityComparator() { diff --git a/server/src/test/java/org/elasticsearch/health/node/DiskHealthIndicatorServiceTests.java b/server/src/test/java/org/elasticsearch/health/node/DiskHealthIndicatorServiceTests.java index 597cfaad380d1..dd8bce07f3cd4 100644 --- a/server/src/test/java/org/elasticsearch/health/node/DiskHealthIndicatorServiceTests.java +++ b/server/src/test/java/org/elasticsearch/health/node/DiskHealthIndicatorServiceTests.java @@ -1106,8 +1106,10 @@ static ClusterState createClusterState( int numberOfShards = indexNameToNodeIdsMap.get(index).size() == 0 ? 1 : indexNameToNodeIdsMap.get(index).size(); IndexMetadata indexMetadata = IndexMetadata.builder(index) .settings( - indexSettings(IndexVersion.current(), numberOfShards, 0).put(SETTING_CREATION_DATE, System.currentTimeMillis()) - .put(IndexMetadata.INDEX_BLOCKS_READ_ONLY_ALLOW_DELETE_SETTING.getKey(), blockedIndices.contains(index)) + indexSettings(IndexVersion.current(), randomUUID(), numberOfShards, 0).put( + SETTING_CREATION_DATE, + System.currentTimeMillis() + ).put(IndexMetadata.INDEX_BLOCKS_READ_ONLY_ALLOW_DELETE_SETTING.getKey(), blockedIndices.contains(index)) ) .build(); if (blockedIndices.contains(index)) { diff --git a/server/src/test/java/org/elasticsearch/health/node/tracker/DiskHealthTrackerTests.java b/server/src/test/java/org/elasticsearch/health/node/tracker/DiskHealthTrackerTests.java index 80cd006bfd104..55f0927a02d97 100644 --- a/server/src/test/java/org/elasticsearch/health/node/tracker/DiskHealthTrackerTests.java +++ b/server/src/test/java/org/elasticsearch/health/node/tracker/DiskHealthTrackerTests.java @@ -26,6 +26,7 @@ import org.elasticsearch.health.HealthStatus; import org.elasticsearch.health.metadata.HealthMetadata; import org.elasticsearch.health.node.DiskHealthInfo; +import org.elasticsearch.index.Index; import org.elasticsearch.monitor.fs.FsInfo; import org.elasticsearch.node.NodeService; import org.elasticsearch.test.ESTestCase; @@ -210,8 +211,8 @@ public void testYellowStatusForNonDataNode() { } public void testHasRelocatingShards() { - String indexName = "my-index"; - final ClusterState state = state(indexName, true, ShardRoutingState.RELOCATING); + final Index index = new Index("my-index", randomUUID()); + final ClusterState state = state(index, true, ShardRoutingState.RELOCATING); // local node coincides with the node hosting the (relocating) primary shard DiscoveryNode localNode = state.nodes().getLocalNode(); assertTrue(DiskHealthTracker.hasRelocatingShards(state, localNode)); diff --git a/server/src/test/java/org/elasticsearch/index/IndexModuleTests.java b/server/src/test/java/org/elasticsearch/index/IndexModuleTests.java index 088c748bde5f6..b00d7ac08351a 100644 --- a/server/src/test/java/org/elasticsearch/index/IndexModuleTests.java +++ b/server/src/test/java/org/elasticsearch/index/IndexModuleTests.java @@ -648,7 +648,11 @@ public void testMmapNotAllowed() { .put("index.store.type", storeType) .build(); final Settings nodeSettings = Settings.builder().put(IndexModule.NODE_STORE_ALLOW_MMAP.getKey(), false).build(); - final IndexSettings indexSettings = IndexSettingsModule.newIndexSettings(new Index("foo", "_na_"), settings, nodeSettings); + final IndexSettings indexSettings = IndexSettingsModule.newIndexSettings( + new Index("foo", IndexMetadata.INDEX_UUID_NA_VALUE), + settings, + nodeSettings + ); final IndexModule module = createIndexModule(indexSettings, emptyAnalysisRegistry, indexNameExpressionResolver); final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> newIndexService(module)); assertThat(e, hasToString(containsString("store type [" + storeType + "] is not allowed"))); diff --git a/server/src/test/java/org/elasticsearch/index/IndexSettingsTests.java b/server/src/test/java/org/elasticsearch/index/IndexSettingsTests.java index 691ca7682f30c..bebf2343c8037 100644 --- a/server/src/test/java/org/elasticsearch/index/IndexSettingsTests.java +++ b/server/src/test/java/org/elasticsearch/index/IndexSettingsTests.java @@ -156,7 +156,7 @@ public void testSettingsConsistency() { IndexMetadata metadata = newIndexMeta("index", Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, version).build()); IndexSettings settings = new IndexSettings(metadata, Settings.EMPTY); assertEquals(version, settings.getIndexVersionCreated()); - assertEquals("_na_", settings.getUUID()); + assertEquals(IndexMetadata.INDEX_UUID_NA_VALUE, settings.getUUID()); try { settings.updateIndexMetadata( newIndexMeta( diff --git a/server/src/test/java/org/elasticsearch/index/MergePolicyConfigTests.java b/server/src/test/java/org/elasticsearch/index/MergePolicyConfigTests.java index 8ee7cc2b51118..df1b63d9a1f50 100644 --- a/server/src/test/java/org/elasticsearch/index/MergePolicyConfigTests.java +++ b/server/src/test/java/org/elasticsearch/index/MergePolicyConfigTests.java @@ -20,6 +20,7 @@ import org.apache.lucene.index.SegmentReader; import org.apache.lucene.index.TieredMergePolicy; import org.apache.lucene.store.Directory; +import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; @@ -34,7 +35,7 @@ import static org.hamcrest.Matchers.equalTo; public class MergePolicyConfigTests extends ESTestCase { - protected final ShardId shardId = new ShardId("index", "_na_", 1); + protected final ShardId shardId = new ShardId("index", IndexMetadata.INDEX_UUID_NA_VALUE, 1); public void testCompoundFileSettings() throws IOException { assertCompoundThreshold(Settings.EMPTY, 1.0, ByteSizeValue.ofGb(1)); diff --git a/server/src/test/java/org/elasticsearch/index/cache/bitset/BitSetFilterCacheTests.java b/server/src/test/java/org/elasticsearch/index/cache/bitset/BitSetFilterCacheTests.java index 997cb123dbf8e..6f26479df7930 100644 --- a/server/src/test/java/org/elasticsearch/index/cache/bitset/BitSetFilterCacheTests.java +++ b/server/src/test/java/org/elasticsearch/index/cache/bitset/BitSetFilterCacheTests.java @@ -91,7 +91,7 @@ public void testInvalidateEntries() throws Exception { writer.commit(); DirectoryReader reader = DirectoryReader.open(writer); - reader = ElasticsearchDirectoryReader.wrap(reader, new ShardId("test", "_na_", 0)); + reader = ElasticsearchDirectoryReader.wrap(reader, new ShardId("test", IndexMetadata.INDEX_UUID_NA_VALUE, 0)); BitsetFilterCache cache = new BitsetFilterCache(INDEX_SETTINGS, BitsetFilterCache.Listener.NOOP); BitSetProducer filter = cache.getBitSetProducer(new TermQuery(new Term("field", "value"))); diff --git a/server/src/test/java/org/elasticsearch/indices/cluster/IndicesClusterStateServiceRandomUpdatesTests.java b/server/src/test/java/org/elasticsearch/indices/cluster/IndicesClusterStateServiceRandomUpdatesTests.java index 464d9fe092d1c..d531b2782bb05 100644 --- a/server/src/test/java/org/elasticsearch/indices/cluster/IndicesClusterStateServiceRandomUpdatesTests.java +++ b/server/src/test/java/org/elasticsearch/indices/cluster/IndicesClusterStateServiceRandomUpdatesTests.java @@ -159,20 +159,25 @@ public void testRandomClusterStateUpdates() { */ public void testJoiningNewClusterOnlyRemovesInMemoryIndexStructures() { // a cluster state derived from the initial state that includes a created index - String name = "index_" + randomAlphaOfLength(8).toLowerCase(Locale.ROOT); + Index index = new Index("index_" + randomAlphaOfLength(8).toLowerCase(Locale.ROOT), randomUUID()); ShardRoutingState[] replicaStates = new ShardRoutingState[randomIntBetween(0, 3)]; Arrays.fill(replicaStates, ShardRoutingState.UNASSIGNED); - ClusterState stateWithIndex = ClusterStateCreationUtils.state(name, randomBoolean(), ShardRoutingState.INITIALIZING, replicaStates); + ClusterState stateWithIndex = ClusterStateCreationUtils.state( + index, + randomBoolean(), + ShardRoutingState.INITIALIZING, + replicaStates + ); // the initial state which is derived from the newly created cluster state but doesn't contain the index ClusterState initialState = ClusterState.builder(stateWithIndex) - .metadata(Metadata.builder(stateWithIndex.metadata()).remove(name)) + .metadata(Metadata.builder(stateWithIndex.metadata()).remove(index.getName())) .routingTable(RoutingTable.builder().build()) .build(); // pick a data node to simulate the adding an index cluster state change event on, that has shards assigned to it DiscoveryNode node = stateWithIndex.nodes() - .get(randomFrom(stateWithIndex.routingTable().index(name).shardsWithState(INITIALIZING)).currentNodeId()); + .get(randomFrom(stateWithIndex.routingTable().index(index.getName()).shardsWithState(INITIALIZING)).currentNodeId()); // simulate the cluster state change on the node ClusterState localState = adaptClusterStateToLocalNode(stateWithIndex, node); @@ -201,9 +206,8 @@ public void testJoiningNewClusterOnlyRemovesInMemoryIndexStructures() { // but the persistent data is still there RecordingIndicesService indicesService = (RecordingIndicesService) indicesCSSvc.indicesService; for (IndexMetadata indexMetadata : stateWithIndex.metadata().getProject()) { - Index index = indexMetadata.getIndex(); - assertNull(indicesService.indexService(index)); - assertFalse(indicesService.isDeleted(index)); + assertNull(indicesService.indexService(indexMetadata.getIndex())); + assertFalse(indicesService.isDeleted(indexMetadata.getIndex())); } } @@ -217,7 +221,7 @@ public void testJoiningNewClusterOnlyRemovesInMemoryIndexStructures() { */ public void testInitializingPrimaryRemovesInitializingReplicaWithSameAID() { disableRandomFailures(); - String index = "index_" + randomAlphaOfLength(8).toLowerCase(Locale.ROOT); + Index index = new Index("index_" + randomAlphaOfLength(8).toLowerCase(Locale.ROOT), randomUUID()); ClusterState state = ClusterStateCreationUtils.state( index, randomBoolean(), @@ -228,7 +232,7 @@ public void testInitializingPrimaryRemovesInitializingReplicaWithSameAID() { // the initial state which is derived from the newly created cluster state but doesn't contain the index ClusterState previousState = ClusterState.builder(state) - .metadata(Metadata.builder(state.metadata()).remove(index)) + .metadata(Metadata.builder(state.metadata()).remove(index.getName())) .routingTable(RoutingTable.builder().build()) .build(); @@ -270,7 +274,7 @@ public void testInitializingPrimaryRemovesInitializingReplicaWithSameAID() { public void testRecoveryFailures() { disableRandomFailures(); - String index = "index_" + randomAlphaOfLength(8).toLowerCase(Locale.ROOT); + Index index = new Index("index_" + randomAlphaOfLength(8).toLowerCase(Locale.ROOT), randomUUID()); ClusterState state = ClusterStateCreationUtils.state( index, randomBoolean(), @@ -280,7 +284,7 @@ public void testRecoveryFailures() { // the initial state which is derived from the newly created cluster state but doesn't contain the index ClusterState previousState = ClusterState.builder(state) - .metadata(Metadata.builder(state.metadata()).remove(index)) + .metadata(Metadata.builder(state.metadata()).remove(index.getName())) .routingTable(RoutingTable.builder().build()) .build(); diff --git a/server/src/test/java/org/elasticsearch/rest/RestResponseTests.java b/server/src/test/java/org/elasticsearch/rest/RestResponseTests.java index c22698ec4f926..1a4556b5600d6 100644 --- a/server/src/test/java/org/elasticsearch/rest/RestResponseTests.java +++ b/server/src/test/java/org/elasticsearch/rest/RestResponseTests.java @@ -22,6 +22,7 @@ import org.elasticsearch.ResourceNotFoundException; import org.elasticsearch.action.search.SearchPhaseExecutionException; import org.elasticsearch.action.search.ShardSearchFailure; +import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.logging.Loggers; @@ -228,11 +229,11 @@ public void testConvert() throws IOException { RestChannel channel = new DetailedExceptionRestChannel(request); ShardSearchFailure failure = new ShardSearchFailure( new ParsingException(1, 2, "foobar", null), - new SearchShardTarget("node_1", new ShardId("foo", "_na_", 1), null) + new SearchShardTarget("node_1", new ShardId("foo", IndexMetadata.INDEX_UUID_NA_VALUE, 1), null) ); ShardSearchFailure failure1 = new ShardSearchFailure( new ParsingException(1, 2, "foobar", null), - new SearchShardTarget("node_1", new ShardId("foo", "_na_", 2), null) + new SearchShardTarget("node_1", new ShardId("foo", IndexMetadata.INDEX_UUID_NA_VALUE, 2), null) ); SearchPhaseExecutionException ex = new SearchPhaseExecutionException( "search", diff --git a/server/src/test/java/org/elasticsearch/rest/action/cat/RestCatRecoveryActionTests.java b/server/src/test/java/org/elasticsearch/rest/action/cat/RestCatRecoveryActionTests.java index 3988b4a68a428..bf81579aa17af 100644 --- a/server/src/test/java/org/elasticsearch/rest/action/cat/RestCatRecoveryActionTests.java +++ b/server/src/test/java/org/elasticsearch/rest/action/cat/RestCatRecoveryActionTests.java @@ -11,6 +11,7 @@ import org.elasticsearch.action.admin.indices.recovery.RecoveryResponse; import org.elasticsearch.action.support.DefaultShardOperationFailedException; +import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.routing.RecoverySource; import org.elasticsearch.cluster.routing.RecoverySource.SnapshotRecoverySource; @@ -51,7 +52,7 @@ public void testRestRecoveryAction() { for (int i = 0; i < successfulShards; i++) { final RecoveryState state = mock(RecoveryState.class); - when(state.getShardId()).thenReturn(new ShardId(new Index("index", "_na_"), i)); + when(state.getShardId()).thenReturn(new ShardId(new Index("index", IndexMetadata.INDEX_UUID_NA_VALUE), i)); final RecoveryState.Timer timer = mock(RecoveryState.Timer.class); final long startTime = randomLongBetween(0, new Date().getTime()); when(timer.startTime()).thenReturn(startTime); diff --git a/server/src/test/java/org/elasticsearch/rest/action/cat/RestShardsActionTests.java b/server/src/test/java/org/elasticsearch/rest/action/cat/RestShardsActionTests.java index 47e9b5fd04324..2bb78b1a5a777 100644 --- a/server/src/test/java/org/elasticsearch/rest/action/cat/RestShardsActionTests.java +++ b/server/src/test/java/org/elasticsearch/rest/action/cat/RestShardsActionTests.java @@ -23,6 +23,7 @@ import org.elasticsearch.cluster.routing.ShardRoutingState; import org.elasticsearch.cluster.routing.TestShardRouting; import org.elasticsearch.common.Table; +import org.elasticsearch.index.Index; import org.elasticsearch.index.shard.IndexingStats; import org.elasticsearch.index.shard.ShardPath; import org.elasticsearch.test.ESTestCase; @@ -127,7 +128,7 @@ private void mockShardStats(boolean includeCommonStats) { this.localNode = DiscoveryNodeUtils.create("local"); this.shardRoutings = new ArrayList<>(numShards); Map shardStatsMap = new HashMap<>(); - String index = "index"; + Index index = new Index("index", randomUUID()); for (int i = 0; i < numShards; i++) { ShardRoutingState shardRoutingState = ShardRoutingState.fromValue((byte) randomIntBetween(2, 3)); ShardRouting shardRouting = TestShardRouting.newShardRouting(index, i, localNode.getId(), randomBoolean(), shardRoutingState); diff --git a/server/src/test/java/org/elasticsearch/search/SearchServiceSingleNodeTests.java b/server/src/test/java/org/elasticsearch/search/SearchServiceSingleNodeTests.java index 45922280b74d3..2af5863d22884 100644 --- a/server/src/test/java/org/elasticsearch/search/SearchServiceSingleNodeTests.java +++ b/server/src/test/java/org/elasticsearch/search/SearchServiceSingleNodeTests.java @@ -1242,6 +1242,7 @@ public void testBeforeShardLockDuringShardCreate() { service.beforeIndexShardCreated( TestShardRouting.newShardRouting( "test", + randomUUID(), 0, randomAlphaOfLength(5), randomAlphaOfLength(5), diff --git a/test/framework/src/main/java/org/elasticsearch/action/support/replication/ClusterStateCreationUtils.java b/test/framework/src/main/java/org/elasticsearch/action/support/replication/ClusterStateCreationUtils.java index 36d71387df59f..1e491dee50b26 100644 --- a/test/framework/src/main/java/org/elasticsearch/action/support/replication/ClusterStateCreationUtils.java +++ b/test/framework/src/main/java/org/elasticsearch/action/support/replication/ClusterStateCreationUtils.java @@ -28,11 +28,11 @@ import org.elasticsearch.cluster.routing.ShardRoutingState; import org.elasticsearch.cluster.routing.TestShardRouting; import org.elasticsearch.cluster.routing.UnassignedInfo; -import org.elasticsearch.common.UUIDs; import org.elasticsearch.common.collect.Iterators; import org.elasticsearch.core.Tuple; import org.elasticsearch.health.node.selection.HealthNode; import org.elasticsearch.health.node.selection.HealthNodeTaskParams; +import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexVersion; import org.elasticsearch.index.shard.IndexLongFieldRange; import org.elasticsearch.index.shard.ShardId; @@ -62,15 +62,15 @@ */ public class ClusterStateCreationUtils { /** - * Creates cluster state with and index that has one shard and #(replicaStates) replicas + * Creates a cluster state with and index that has one shard and #(replicaStates) replicas * - * @param index name of the index + * @param index the index * @param activePrimaryLocal if active primary should coincide with the local node in the cluster state * @param primaryState state of primary - * @param replicaStates states of the replicas. length of this array determines also the number of replicas + * @param replicaStates states of the replicas. The length of this array determines also the number of replicas */ public static ClusterState state( - String index, + Index index, boolean activePrimaryLocal, ShardRoutingState primaryState, ShardRoutingState... replicaStates @@ -79,17 +79,17 @@ public static ClusterState state( } /** - * Creates cluster state with an index that has one shard and #(replicaStates) replicas + * Creates a cluster state with an index that has one shard and #(replicaStates) replicas * * @param projectId project to create index in - * @param index name of the index + * @param index the index * @param activePrimaryLocal if active primary should coincide with the local node in the cluster state * @param primaryState state of primary * @param replicaStates states of the replicas. length of this array determines also the number of replicas */ public static ClusterState state( ProjectId projectId, - String index, + Index index, boolean activePrimaryLocal, ShardRoutingState primaryState, ShardRoutingState... replicaStates @@ -104,15 +104,15 @@ public static ClusterState state( } /** - * Creates cluster state with and index that has one shard and #(replicaStates) replicas with given roles + * Creates a cluster state with and index that has one shard and #(replicaStates) replicas with given roles * - * @param index name of the index + * @param index the index * @param activePrimaryLocal if active primary should coincide with the local node in the cluster state * @param primaryState state of primary * @param replicaStates states and roles of the replicas. length of this collection determines also the number of replicas */ public static ClusterState state( - String index, + Index index, boolean activePrimaryLocal, ShardRoutingState primaryState, List> replicaStates @@ -121,17 +121,17 @@ public static ClusterState state( } /** - * Creates cluster state with an index that has one shard and #(replicaStates) replicas with given roles + * Creates a cluster state with an index that has one shard and #(replicaStates) replicas with given roles * * @param projectId project to create index in - * @param index name of the index + * @param index the index * @param activePrimaryLocal if active primary should coincide with the local node in the cluster state * @param primaryState state of primary * @param replicaStates states and roles of the replicas. length of this collection determines also the number of replicas */ public static ClusterState state( ProjectId projectId, - String index, + Index index, boolean activePrimaryLocal, ShardRoutingState primaryState, List> replicaStates @@ -140,16 +140,16 @@ public static ClusterState state( } /** - * Creates cluster state with an index that has one shard and #(replicaStates) replicas with given roles + * Creates a cluster state with an index that has one shard and #(replicaStates) replicas with given roles * - * @param index name of the index + * @param index the index * @param activePrimaryLocal if active primary should coincide with the local node in the cluster state * @param primaryState state of primary * @param primaryRole role of primary * @param replicaStates states and roles of the replicas. length of this collection determines also the number of replicas */ public static ClusterState state( - String index, + Index index, boolean activePrimaryLocal, ShardRoutingState primaryState, ShardRouting.Role primaryRole, @@ -162,7 +162,7 @@ public static ClusterState state( * Creates cluster state with an index that has one shard and #(replicaStates) replicas with given roles * * @param projectId project to create index in - * @param index name of the index + * @param index the index * @param activePrimaryLocal if active primary should coincide with the local node in the cluster state * @param primaryState state of primary * @param primaryRole role of primary @@ -170,7 +170,7 @@ public static ClusterState state( */ public static ClusterState state( ProjectId projectId, - String index, + Index index, boolean activePrimaryLocal, ShardRoutingState primaryState, ShardRouting.Role primaryRole, @@ -197,7 +197,7 @@ public static ClusterState state( } } numberOfNodes = Math.max(2, numberOfNodes); // we need a non-local master to test shard failures - final ShardId shardId = new ShardId(index, "_na_", 0); + final ShardId shardId = new ShardId(index, 0); DiscoveryNodes.Builder discoBuilder = DiscoveryNodes.builder(); Set unassignedNodes = new HashSet<>(); for (int i = 0; i < numberOfNodes + 1; i++) { @@ -212,8 +212,13 @@ public static ClusterState state( ? IndexLongFieldRange.UNKNOWN : IndexLongFieldRange.NO_SHARDS; - IndexMetadata indexMetadata = IndexMetadata.builder(index) - .settings(indexSettings(IndexVersion.current(), 1, numberOfReplicas).put(SETTING_CREATION_DATE, System.currentTimeMillis())) + IndexMetadata indexMetadata = IndexMetadata.builder(index.getName()) + .settings( + indexSettings(IndexVersion.current(), index.getUUID(), 1, numberOfReplicas).put( + SETTING_CREATION_DATE, + System.currentTimeMillis() + ) + ) .primaryTerm(0, primaryTerm) .timestampRange(timeFieldRange) .eventIngestedRange(timeFieldRange) @@ -243,7 +248,7 @@ public static ClusterState state( unassignedInfo = new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, null); } indexShardRoutingBuilder.addShard( - shardRoutingBuilder(index, 0, primaryNode, true, primaryState).withRelocatingNodeId(relocatingNode) + shardRoutingBuilder(shardId, primaryNode, true, primaryState).withRelocatingNodeId(relocatingNode) .withUnassignedInfo(unassignedInfo) .withRole(primaryRole) .build() @@ -263,7 +268,7 @@ public static ClusterState state( unassignedInfo = new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, null); } indexShardRoutingBuilder.addShard( - shardRoutingBuilder(index, shardId.id(), replicaNode, false, replicaState.v1()).withRelocatingNodeId(relocatingNode) + shardRoutingBuilder(shardId, replicaNode, false, replicaState.v1()).withRelocatingNodeId(relocatingNode) .withUnassignedInfo(unassignedInfo) .withRole(replicaState.v2()) .build() @@ -300,18 +305,18 @@ public static ClusterState state( } /** - * Creates cluster state with an index that has #(numberOfPrimaries) primary shards in the started state and no replicas. + * Creates a cluster state with an index that has #(numberOfPrimaries) primary shards in the started state and no replicas. * The cluster state contains #(numberOfNodes) nodes and assigns primaries to those nodes. */ - public static ClusterState state(String index, int numberOfNodes, int numberOfPrimaries) { + public static ClusterState state(Index index, int numberOfNodes, int numberOfPrimaries) { return state(Metadata.DEFAULT_PROJECT_ID, index, numberOfNodes, numberOfPrimaries); } /** - * Creates cluster state with an index that has #(numberOfPrimaries) primary shards in the started state and no replicas. + * Creates a cluster state with an index that has #(numberOfPrimaries) primary shards in the started state and no replicas. * The cluster state contains #(numberOfNodes) nodes and assigns primaries to those nodes. */ - public static ClusterState state(ProjectId projectId, String index, int numberOfNodes, int numberOfPrimaries) { + public static ClusterState state(ProjectId projectId, Index index, int numberOfNodes, int numberOfPrimaries) { DiscoveryNodes.Builder discoBuilder = DiscoveryNodes.builder(); Set nodes = new HashSet<>(); for (int i = 0; i < numberOfNodes; i++) { @@ -321,13 +326,18 @@ public static ClusterState state(ProjectId projectId, String index, int numberOf } discoBuilder.localNodeId(newNode(0).getId()); discoBuilder.masterNodeId(randomFrom(nodes)); - IndexMetadata indexMetadata = IndexMetadata.builder(index) - .settings(indexSettings(IndexVersion.current(), numberOfPrimaries, 0).put(SETTING_CREATION_DATE, System.currentTimeMillis())) + IndexMetadata indexMetadata = IndexMetadata.builder(index.getName()) + .settings( + indexSettings(IndexVersion.current(), index.getUUID(), numberOfPrimaries, 0).put( + SETTING_CREATION_DATE, + System.currentTimeMillis() + ) + ) .build(); - IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(indexMetadata.getIndex()); + IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index); for (int i = 0; i < numberOfPrimaries; i++) { - ShardId shardId = new ShardId(indexMetadata.getIndex(), i); + ShardId shardId = new ShardId(index, i); IndexShardRoutingTable.Builder indexShardRoutingBuilder = IndexShardRoutingTable.builder(shardId); indexShardRoutingBuilder.addShard( TestShardRouting.newShardRouting(shardId, randomFrom(nodes), true, ShardRoutingState.STARTED) @@ -343,20 +353,20 @@ public static ClusterState state(ProjectId projectId, String index, int numberOf } /** - * Creates cluster state with the given indices, each index containing #(numberOfPrimaries) + * Creates a cluster state with the given indices, each index containing #(numberOfPrimaries) * started primary shards and no replicas. The cluster state contains #(numberOfNodes) nodes * and assigns primaries to those nodes. */ - public static ClusterState state(int numberOfNodes, String[] indices, int numberOfPrimaries) { + public static ClusterState state(int numberOfNodes, Index[] indices, int numberOfPrimaries) { return state(Metadata.DEFAULT_PROJECT_ID, numberOfNodes, indices, numberOfPrimaries); } /** - * Creates cluster state with the given indices, each index containing #(numberOfPrimaries) + * Creates a cluster state with the given indices, each index containing #(numberOfPrimaries) * started primary shards and no replicas. The cluster state contains #(numberOfNodes) nodes * and assigns primaries to those nodes. */ - public static ClusterState state(ProjectId projectId, int numberOfNodes, String[] indices, int numberOfPrimaries) { + public static ClusterState state(ProjectId projectId, int numberOfNodes, Index[] indices, int numberOfPrimaries) { DiscoveryNodes.Builder discoBuilder = DiscoveryNodes.builder(); Set nodes = new HashSet<>(); for (int i = 0; i < numberOfNodes; i++) { @@ -370,17 +380,20 @@ public static ClusterState state(ProjectId projectId, int numberOfNodes, String[ RoutingTable.Builder routingTable = RoutingTable.builder(); List nodesList = new ArrayList<>(nodes); int currentNodeToAssign = 0; - for (String index : indices) { - IndexMetadata indexMetadata = IndexMetadata.builder(index) + for (Index index : indices) { + IndexMetadata indexMetadata = IndexMetadata.builder(index.getName()) .settings( - indexSettings(IndexVersion.current(), numberOfPrimaries, 0).put(SETTING_CREATION_DATE, System.currentTimeMillis()) + indexSettings(IndexVersion.current(), index.getUUID(), numberOfPrimaries, 0).put( + SETTING_CREATION_DATE, + System.currentTimeMillis() + ) ) .eventIngestedRange(IndexLongFieldRange.UNKNOWN) .build(); - IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(indexMetadata.getIndex()); + IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index); for (int i = 0; i < numberOfPrimaries; i++) { - ShardId shardId = new ShardId(indexMetadata.getIndex(), i); + ShardId shardId = new ShardId(index, i); IndexShardRoutingTable.Builder indexShardRoutingBuilder = IndexShardRoutingTable.builder(shardId); indexShardRoutingBuilder.addShard( TestShardRouting.newShardRouting(shardId, nodesList.get(currentNodeToAssign++), true, ShardRoutingState.STARTED) @@ -402,16 +415,16 @@ public static ClusterState state(ProjectId projectId, int numberOfNodes, String[ } /** - * Creates cluster state with several shards and one replica and all shards STARTED. + * Creates a cluster state with several shards and one replica and all shards STARTED. */ - public static ClusterState stateWithAssignedPrimariesAndOneReplica(String index, int numberOfShards) { + public static ClusterState stateWithAssignedPrimariesAndOneReplica(Index index, int numberOfShards) { return stateWithAssignedPrimariesAndOneReplica(Metadata.DEFAULT_PROJECT_ID, index, numberOfShards); } /** - * Creates cluster state with several shards and one replica and all shards STARTED. + * Creates a cluster state with several shards and one replica and all shards STARTED. */ - public static ClusterState stateWithAssignedPrimariesAndOneReplica(ProjectId projectId, String index, int numberOfShards) { + public static ClusterState stateWithAssignedPrimariesAndOneReplica(ProjectId projectId, Index index, int numberOfShards) { int numberOfNodes = 2; // we need a non-local master to test shard failures DiscoveryNodes.Builder discoBuilder = DiscoveryNodes.builder(); @@ -421,21 +434,26 @@ public static ClusterState stateWithAssignedPrimariesAndOneReplica(ProjectId pro } discoBuilder.localNodeId(newNode(0).getId()); discoBuilder.masterNodeId(newNode(1).getId()); // we need a non-local master to test shard failures - IndexMetadata indexMetadata = IndexMetadata.builder(index) - .settings(indexSettings(IndexVersion.current(), numberOfShards, 1).put(SETTING_CREATION_DATE, System.currentTimeMillis())) + IndexMetadata indexMetadata = IndexMetadata.builder(index.getName()) + .settings( + indexSettings(IndexVersion.current(), index.getUUID(), numberOfShards, 1).put( + SETTING_CREATION_DATE, + System.currentTimeMillis() + ) + ) .build(); ClusterState.Builder state = ClusterState.builder(new ClusterName("test")); state.nodes(discoBuilder); state.metadata(Metadata.builder().put(ProjectMetadata.builder(projectId).put(indexMetadata, false)).generateClusterUuidIfNeeded()); IndexRoutingTable.Builder indexRoutingTableBuilder = IndexRoutingTable.builder(indexMetadata.getIndex()); for (int i = 0; i < numberOfShards; i++) { - final ShardId shardId = new ShardId(index, "_na_", i); + final ShardId shardId = new ShardId(index, i); IndexShardRoutingTable.Builder indexShardRoutingBuilder = IndexShardRoutingTable.builder(shardId); indexShardRoutingBuilder.addShard( - TestShardRouting.newShardRouting(index, i, newNode(0).getId(), null, true, ShardRoutingState.STARTED) + TestShardRouting.newShardRouting(shardId, newNode(0).getId(), null, true, ShardRoutingState.STARTED) ); indexShardRoutingBuilder.addShard( - TestShardRouting.newShardRouting(index, i, newNode(1).getId(), null, false, ShardRoutingState.STARTED) + TestShardRouting.newShardRouting(shardId, newNode(1).getId(), null, false, ShardRoutingState.STARTED) ); indexRoutingTableBuilder.addIndexShard(indexShardRoutingBuilder); } @@ -446,18 +464,18 @@ public static ClusterState stateWithAssignedPrimariesAndOneReplica(ProjectId pro } /** - * Creates cluster state with several indexes, shards and replicas and all shards STARTED. + * Creates a cluster state with several indexes, shards and replicas and all shards STARTED. */ - public static ClusterState stateWithAssignedPrimariesAndReplicas(String[] indices, int numberOfShards, int numberOfReplicas) { + public static ClusterState stateWithAssignedPrimariesAndReplicas(Index[] indices, int numberOfShards, int numberOfReplicas) { return stateWithAssignedPrimariesAndReplicas(Metadata.DEFAULT_PROJECT_ID, indices, numberOfShards, numberOfReplicas); } /** - * Creates cluster state with several indexes, shards and replicas and all shards STARTED. + * Creates a cluster state with several indexes, shards and replicas and all shards STARTED. */ public static ClusterState stateWithAssignedPrimariesAndReplicas( ProjectId projectId, - String[] indices, + Index[] indices, int numberOfShards, int numberOfReplicas ) { @@ -470,10 +488,10 @@ public static ClusterState stateWithAssignedPrimariesAndReplicas( } /** - * Creates cluster state with several indexes, shards and replicas (with given roles) and all shards STARTED. + * Creates a cluster state with several indexes, shards and replicas (with given roles) and all shards STARTED. */ public static ClusterState stateWithAssignedPrimariesAndReplicas( - String[] indices, + Index[] indices, int numberOfShards, List replicaRoles ) { @@ -481,11 +499,11 @@ public static ClusterState stateWithAssignedPrimariesAndReplicas( } /** - * Creates cluster state with several indexes, shards and replicas (with given roles) and all shards STARTED. + * Creates a cluster state with several indexes, shards and replicas (with given roles) and all shards STARTED. */ public static ClusterState stateWithAssignedPrimariesAndReplicas( ProjectId projectId, - String[] indices, + Index[] indices, int numberOfShards, List replicaRoles ) { @@ -506,10 +524,10 @@ public static ClusterState stateWithAssignedPrimariesAndReplicas( Metadata.Builder metadataBuilder = Metadata.builder(); final ProjectMetadata.Builder projectBuilder = ProjectMetadata.builder(projectId); - for (String index : indices) { - IndexMetadata indexMetadata = IndexMetadata.builder(index) + for (Index index : indices) { + IndexMetadata indexMetadata = IndexMetadata.builder(index.getName()) .settings( - indexSettings(IndexVersion.current(), numberOfShards, replicaRoles.size()).put( + indexSettings(IndexVersion.current(), index.getUUID(), numberOfShards, replicaRoles.size()).put( SETTING_CREATION_DATE, System.currentTimeMillis() ) @@ -520,14 +538,14 @@ public static ClusterState stateWithAssignedPrimariesAndReplicas( projectBuilder.put(indexMetadata, false); IndexRoutingTable.Builder indexRoutingTableBuilder = IndexRoutingTable.builder(indexMetadata.getIndex()); for (int i = 0; i < numberOfShards; i++) { - final ShardId shardId = new ShardId(index, "_na_", i); + final ShardId shardId = new ShardId(index, i); IndexShardRoutingTable.Builder indexShardRoutingBuilder = IndexShardRoutingTable.builder(shardId); indexShardRoutingBuilder.addShard( - TestShardRouting.newShardRouting(index, i, newNode(0).getId(), null, true, ShardRoutingState.STARTED) + TestShardRouting.newShardRouting(shardId, newNode(0).getId(), null, true, ShardRoutingState.STARTED) ); for (int replica = 0; replica < replicaRoles.size(); replica++) { indexShardRoutingBuilder.addShard( - shardRoutingBuilder(index, i, newNode(replica + 1).getId(), false, ShardRoutingState.STARTED).withRole( + shardRoutingBuilder(shardId, newNode(replica + 1).getId(), false, ShardRoutingState.STARTED).withRole( replicaRoles.get(replica) ).build() ); @@ -546,7 +564,7 @@ public static ClusterState stateWithAssignedPrimariesAndReplicas( public static Tuple projectWithAssignedPrimariesAndReplicas( ProjectId projectId, - String[] indices, + Index[] indices, int numberOfShards, int numberOfReplicas, DiscoveryNodes nodes @@ -563,7 +581,7 @@ public static Tuple projectWithAs private static Tuple projectWithAssignedPrimariesAndReplicas( ProjectId projectId, - String[] indices, + Index[] indices, int numberOfShards, List replicaRoles, DiscoveryNodes nodes @@ -571,14 +589,13 @@ private static Tuple projectWithA final ProjectMetadata.Builder projectBuilder = ProjectMetadata.builder(projectId); final RoutingTable.Builder routingTableBuilder = RoutingTable.builder(); final Iterator nodeIterator = Iterators.cycling(nodes); - for (String index : indices) { - final String uuid = UUIDs.base64UUID(); - IndexMetadata indexMetadata = IndexMetadata.builder(index) + for (Index index : indices) { + IndexMetadata indexMetadata = IndexMetadata.builder(index.getName()) .settings( - indexSettings(IndexVersion.current(), numberOfShards, replicaRoles.size()).put( + indexSettings(IndexVersion.current(), index.getUUID(), numberOfShards, replicaRoles.size()).put( SETTING_CREATION_DATE, System.currentTimeMillis() - ).put(IndexMetadata.SETTING_INDEX_UUID, uuid) + ) ) .timestampRange(IndexLongFieldRange.UNKNOWN) .eventIngestedRange(IndexLongFieldRange.UNKNOWN) @@ -586,7 +603,7 @@ private static Tuple projectWithA projectBuilder.put(indexMetadata, false); IndexRoutingTable.Builder indexRoutingTableBuilder = IndexRoutingTable.builder(indexMetadata.getIndex()); for (int i = 0; i < numberOfShards; i++) { - final ShardId shardId = new ShardId(index, uuid, i); + final ShardId shardId = new ShardId(index, i); IndexShardRoutingTable.Builder indexShardRoutingBuilder = IndexShardRoutingTable.builder(shardId); indexShardRoutingBuilder.addShard( TestShardRouting.newShardRouting(shardId, nodeIterator.next().getId(), null, true, ShardRoutingState.STARTED) @@ -609,11 +626,11 @@ private static Tuple projectWithA * Creates cluster state with and index that has one shard and as many replicas as numberOfReplicas. * Primary will be STARTED in cluster state but replicas will be one of UNASSIGNED, INITIALIZING, STARTED or RELOCATING. * - * @param index name of the index + * @param index the index the shards belong to * @param activePrimaryLocal if active primary should coincide with the local node in the cluster state * @param numberOfReplicas number of replicas */ - public static ClusterState stateWithActivePrimary(String index, boolean activePrimaryLocal, int numberOfReplicas) { + public static ClusterState stateWithActivePrimary(Index index, boolean activePrimaryLocal, int numberOfReplicas) { return stateWithActivePrimary(Metadata.DEFAULT_PROJECT_ID, index, activePrimaryLocal, numberOfReplicas); } @@ -622,11 +639,11 @@ public static ClusterState stateWithActivePrimary(String index, boolean activePr * Primary will be STARTED in cluster state but replicas will be one of UNASSIGNED, INITIALIZING, STARTED or RELOCATING. * * @param projectId project to create index in - * @param index name of the index + * @param index the index the shards belong to * @param activePrimaryLocal if active primary should coincide with the local node in the cluster state * @param numberOfReplicas number of replicas */ - public static ClusterState stateWithActivePrimary(ProjectId projectId, String index, boolean activePrimaryLocal, int numberOfReplicas) { + public static ClusterState stateWithActivePrimary(ProjectId projectId, Index index, boolean activePrimaryLocal, int numberOfReplicas) { int assignedReplicas = randomIntBetween(0, numberOfReplicas); return stateWithActivePrimary(projectId, index, activePrimaryLocal, assignedReplicas, numberOfReplicas - assignedReplicas); } @@ -636,13 +653,13 @@ public static ClusterState stateWithActivePrimary(ProjectId projectId, String in * Primary will be STARTED in cluster state. Some (unassignedReplicas) will be UNASSIGNED and * some (assignedReplicas) will be one of INITIALIZING, STARTED or RELOCATING. * - * @param index name of the index + * @param index the index the shards belong to * @param activePrimaryLocal if active primary should coincide with the local node in the cluster state * @param assignedReplicas number of replicas that should have INITIALIZING, STARTED or RELOCATING state * @param unassignedReplicas number of replicas that should be unassigned */ public static ClusterState stateWithActivePrimary( - String index, + Index index, boolean activePrimaryLocal, int assignedReplicas, int unassignedReplicas @@ -656,14 +673,14 @@ public static ClusterState stateWithActivePrimary( * some (assignedReplicas) will be one of INITIALIZING, STARTED or RELOCATING. * * @param projectId project to create index in - * @param index name of the index + * @param index the index the shards belong to * @param activePrimaryLocal if active primary should coincide with the local node in the cluster state * @param assignedReplicas number of replicas that should have INITIALIZING, STARTED or RELOCATING state * @param unassignedReplicas number of replicas that should be unassigned */ public static ClusterState stateWithActivePrimary( ProjectId projectId, - String index, + Index index, boolean activePrimaryLocal, int assignedReplicas, int unassignedReplicas diff --git a/test/framework/src/main/java/org/elasticsearch/cluster/metadata/DataStreamTestHelper.java b/test/framework/src/main/java/org/elasticsearch/cluster/metadata/DataStreamTestHelper.java index e31d3912fa722..1a6b483c7f915 100644 --- a/test/framework/src/main/java/org/elasticsearch/cluster/metadata/DataStreamTestHelper.java +++ b/test/framework/src/main/java/org/elasticsearch/cluster/metadata/DataStreamTestHelper.java @@ -78,6 +78,7 @@ import static org.elasticsearch.cluster.metadata.DataStream.getDefaultFailureStoreName; import static org.elasticsearch.cluster.metadata.IndexMetadata.SETTING_INDEX_UUID; import static org.elasticsearch.test.ESTestCase.generateRandomStringArray; +import static org.elasticsearch.test.ESTestCase.indexSettings; import static org.elasticsearch.test.ESTestCase.randomAlphaOfLength; import static org.elasticsearch.test.ESTestCase.randomAlphanumericOfLength; import static org.elasticsearch.test.ESTestCase.randomBoolean; @@ -88,6 +89,7 @@ import static org.elasticsearch.test.ESTestCase.randomMillisUpToYear9999; import static org.elasticsearch.test.ESTestCase.randomPositiveTimeValue; import static org.elasticsearch.test.ESTestCase.randomProjectIdOrDefault; +import static org.elasticsearch.test.ESTestCase.randomUUID; import static org.junit.Assert.fail; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; @@ -608,12 +610,10 @@ public static void getClusterStateWithDataStream( } public static IndexMetadata createIndexMetadata(String name, boolean hidden, Settings settings, int replicas) { - Settings.Builder b = Settings.builder() - .put(settings) - .put(IndexMetadata.SETTING_VERSION_CREATED, IndexVersion.current()) + Settings.Builder indexSettingsBuilder = indexSettings(IndexVersion.current(), randomUUID(), 1, replicas).put(settings) .put("index.hidden", hidden); - return IndexMetadata.builder(name).settings(b).numberOfShards(1).numberOfReplicas(replicas).build(); + return IndexMetadata.builder(name).settings(indexSettingsBuilder).build(); } public static String backingIndexPattern(String dataStreamName, long generation) { diff --git a/test/framework/src/main/java/org/elasticsearch/cluster/routing/TestShardRouting.java b/test/framework/src/main/java/org/elasticsearch/cluster/routing/TestShardRouting.java index 84e1dd532e2b7..a5822dab6cd95 100644 --- a/test/framework/src/main/java/org/elasticsearch/cluster/routing/TestShardRouting.java +++ b/test/framework/src/main/java/org/elasticsearch/cluster/routing/TestShardRouting.java @@ -11,7 +11,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.elasticsearch.cluster.metadata.IndexMetadata; +import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexVersion; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.repositories.IndexId; @@ -42,8 +42,19 @@ public class TestShardRouting { private static final Logger logger = LogManager.getLogger(TestShardRouting.class); - public static Builder shardRoutingBuilder(String index, int shardId, String currentNodeId, boolean primary, ShardRoutingState state) { - return shardRoutingBuilder(new ShardId(index, IndexMetadata.INDEX_UUID_NA_VALUE, shardId), currentNodeId, primary, state); + public static Builder shardRoutingBuilder( + String index, + String uuid, + int shardId, + String currentNodeId, + boolean primary, + ShardRoutingState state + ) { + return shardRoutingBuilder(new ShardId(index, uuid, shardId), currentNodeId, primary, state); + } + + public static Builder shardRoutingBuilder(Index index, int shardId, String currentNodeId, boolean primary, ShardRoutingState state) { + return shardRoutingBuilder(new ShardId(index, shardId), currentNodeId, primary, state); } public static Builder shardRoutingBuilder(ShardId shardId, String currentNodeId, boolean primary, ShardRoutingState state) { @@ -123,8 +134,19 @@ public ShardRouting build() { } } - public static ShardRouting newShardRouting(String index, int shardId, String currentNodeId, boolean primary, ShardRoutingState state) { - return newShardRouting(new ShardId(index, IndexMetadata.INDEX_UUID_NA_VALUE, shardId), currentNodeId, primary, state); + public static ShardRouting newShardRouting( + String index, + String uuid, + int shardId, + String currentNodeId, + boolean primary, + ShardRoutingState state + ) { + return newShardRouting(new ShardId(index, uuid, shardId), currentNodeId, primary, state); + } + + public static ShardRouting newShardRouting(Index index, int shardId, String currentNodeId, boolean primary, ShardRoutingState state) { + return newShardRouting(new ShardId(index, shardId), currentNodeId, primary, state); } public static ShardRouting newShardRouting(ShardId shardId, String currentNodeId, boolean primary, ShardRoutingState state) { @@ -190,19 +212,25 @@ public static ShardRouting newShardRouting( public static ShardRouting newShardRouting( String index, + String uuid, int shardId, String currentNodeId, String relocatingNodeId, boolean primary, ShardRoutingState state ) { - return newShardRouting( - new ShardId(index, IndexMetadata.INDEX_UUID_NA_VALUE, shardId), - currentNodeId, - relocatingNodeId, - primary, - state - ); + return newShardRouting(new ShardId(index, uuid, shardId), currentNodeId, relocatingNodeId, primary, state); + } + + public static ShardRouting newShardRouting( + Index index, + int shardId, + String currentNodeId, + String relocatingNodeId, + boolean primary, + ShardRoutingState state + ) { + return newShardRouting(new ShardId(index, shardId), currentNodeId, relocatingNodeId, primary, state); } public static ShardRouting newShardRouting( diff --git a/test/framework/src/main/java/org/elasticsearch/index/engine/EngineTestCase.java b/test/framework/src/main/java/org/elasticsearch/index/engine/EngineTestCase.java index 36f50f3ce958e..91a5e8faaa765 100644 --- a/test/framework/src/main/java/org/elasticsearch/index/engine/EngineTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/index/engine/EngineTestCase.java @@ -153,7 +153,7 @@ @SuppressWarnings("HiddenField") public abstract class EngineTestCase extends ESTestCase { - protected final ShardId shardId = new ShardId(new Index("index", "_na_"), 0); + protected final ShardId shardId = new ShardId(new Index("index", IndexMetadata.INDEX_UUID_NA_VALUE), 0); protected final AllocationId allocationId = AllocationId.newInitializing(); protected static final IndexSettings INDEX_SETTINGS = IndexSettingsModule.newIndexSettings("index", Settings.EMPTY); diff --git a/test/framework/src/main/java/org/elasticsearch/index/mapper/MapperServiceTestCase.java b/test/framework/src/main/java/org/elasticsearch/index/mapper/MapperServiceTestCase.java index 71452917465a7..ea91d228750ae 100644 --- a/test/framework/src/main/java/org/elasticsearch/index/mapper/MapperServiceTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/index/mapper/MapperServiceTestCase.java @@ -899,6 +899,6 @@ protected void validateRoundTripReader(String syntheticSource, DirectoryReader r } protected static DirectoryReader wrapInMockESDirectoryReader(DirectoryReader directoryReader) throws IOException { - return ElasticsearchDirectoryReader.wrap(directoryReader, new ShardId(new Index("index", "_na_"), 0)); + return ElasticsearchDirectoryReader.wrap(directoryReader, new ShardId(new Index("index", IndexMetadata.INDEX_UUID_NA_VALUE), 0)); } } diff --git a/test/framework/src/main/java/org/elasticsearch/index/shard/IndexShardTestCase.java b/test/framework/src/main/java/org/elasticsearch/index/shard/IndexShardTestCase.java index aed1d10342768..6151ace5f2131 100644 --- a/test/framework/src/main/java/org/elasticsearch/index/shard/IndexShardTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/index/shard/IndexShardTestCase.java @@ -262,7 +262,7 @@ protected IndexShard newShard( EngineFactory engineFactory, final IndexingOperationListener... listeners ) throws IOException { - return newShard(primary, new ShardId("index", "_na_", 0), settings, engineFactory, listeners); + return newShard(primary, new ShardId("index", IndexMetadata.INDEX_UUID_NA_VALUE, 0), settings, engineFactory, listeners); } /** diff --git a/test/framework/src/main/java/org/elasticsearch/search/aggregations/AggregatorTestCase.java b/test/framework/src/main/java/org/elasticsearch/search/aggregations/AggregatorTestCase.java index 5e451e2e79f10..9dd701410c12c 100644 --- a/test/framework/src/main/java/org/elasticsearch/search/aggregations/AggregatorTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/search/aggregations/AggregatorTestCase.java @@ -1099,7 +1099,7 @@ public String toString() { } protected static DirectoryReader wrapInMockESDirectoryReader(DirectoryReader directoryReader) throws IOException { - return ElasticsearchDirectoryReader.wrap(directoryReader, new ShardId(new Index("_index", "_na_"), 0)); + return ElasticsearchDirectoryReader.wrap(directoryReader, new ShardId(new Index("_index", IndexMetadata.INDEX_UUID_NA_VALUE), 0)); } /** diff --git a/test/framework/src/main/java/org/elasticsearch/test/AbstractBroadcastResponseTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/AbstractBroadcastResponseTestCase.java index cf6f7c9865161..86605f960592a 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/AbstractBroadcastResponseTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/AbstractBroadcastResponseTestCase.java @@ -13,6 +13,7 @@ import org.elasticsearch.action.support.DefaultShardOperationFailedException; import org.elasticsearch.action.support.broadcast.BaseBroadcastResponse; import org.elasticsearch.action.support.broadcast.BroadcastResponse; +import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.index.Index; @@ -71,8 +72,8 @@ protected T createTestInstance() { for (int i = 0; i < failedShards; i++) { ElasticsearchException exception = new ElasticsearchException("exception message " + i); String index = randomAlphaOfLengthBetween(3, 10); - exception.setIndex(new Index(index, "_na_")); - exception.setShard(new ShardId(index, "_na_", i)); + exception.setIndex(new Index(index, IndexMetadata.INDEX_UUID_NA_VALUE)); + exception.setShard(new ShardId(index, IndexMetadata.INDEX_UUID_NA_VALUE, i)); if (randomBoolean()) { failures.add(new DefaultShardOperationFailedException(exception)); } else { @@ -118,7 +119,7 @@ protected boolean assertToXContentEquivalence() { public void testFailuresDeduplication() throws IOException { List failures = new ArrayList<>(); - Index index = new Index("test", "_na_"); + Index index = new Index("test", IndexMetadata.INDEX_UUID_NA_VALUE); ElasticsearchException exception1 = new ElasticsearchException("foo", new IllegalArgumentException("bar")); exception1.setIndex(index); exception1.setShard(new ShardId(index, 0)); diff --git a/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java index 43243e431db48..202e96ceecda2 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java @@ -1688,6 +1688,11 @@ public static Settings.Builder indexSettings(IndexVersion indexVersionCreated, S .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, replicas); } + /** Return consistent index settings for the provided index version, uuid, shard- and replica-count, */ + public static Settings.Builder indexSettings(IndexVersion indexVersionCreated, String uuid) { + return settings(indexVersionCreated).put(IndexMetadata.SETTING_INDEX_UUID, uuid); + } + /** Return consistent index settings for the provided shard- and replica-count. */ public static Settings.Builder indexSettings(int shards, int replicas) { return Settings.builder() diff --git a/x-pack/plugin/autoscaling/src/test/java/org/elasticsearch/xpack/autoscaling/storage/ReactiveStorageDeciderDecisionTests.java b/x-pack/plugin/autoscaling/src/test/java/org/elasticsearch/xpack/autoscaling/storage/ReactiveStorageDeciderDecisionTests.java index 3f56065f41e09..5ab48f2a65f34 100644 --- a/x-pack/plugin/autoscaling/src/test/java/org/elasticsearch/xpack/autoscaling/storage/ReactiveStorageDeciderDecisionTests.java +++ b/x-pack/plugin/autoscaling/src/test/java/org/elasticsearch/xpack/autoscaling/storage/ReactiveStorageDeciderDecisionTests.java @@ -820,9 +820,10 @@ private static ClusterState addRandomIndices(int minShards, int maxShardCopies, RoutingTable.Builder routingTableBuilder = RoutingTable.builder(TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY); while (shards > 0) { IndexMetadata indexMetadata = IndexMetadata.builder("test" + "-" + shards) - .settings(settings(IndexVersion.current()).put(randomFrom(tierSettingNames), "data_hot")) - .numberOfShards(randomIntBetween(1, 5)) - .numberOfReplicas(randomIntBetween(0, maxShardCopies - 1)) + .settings( + indexSettings(IndexVersion.current(), randomUUID(), randomIntBetween(1, 5), randomIntBetween(0, maxShardCopies - 1)) + .put(randomFrom(tierSettingNames), "data_hot") + ) .build(); builder.put(indexMetadata, false); diff --git a/x-pack/plugin/autoscaling/src/test/java/org/elasticsearch/xpack/autoscaling/storage/ReactiveStorageDeciderServiceTests.java b/x-pack/plugin/autoscaling/src/test/java/org/elasticsearch/xpack/autoscaling/storage/ReactiveStorageDeciderServiceTests.java index 18115a35039b2..6d0f9f3e7154b 100644 --- a/x-pack/plugin/autoscaling/src/test/java/org/elasticsearch/xpack/autoscaling/storage/ReactiveStorageDeciderServiceTests.java +++ b/x-pack/plugin/autoscaling/src/test/java/org/elasticsearch/xpack/autoscaling/storage/ReactiveStorageDeciderServiceTests.java @@ -592,7 +592,7 @@ public void testCanRemainOnlyHighestTierPreference() { ClusterState clusterState = stateBuilder.build(); ShardRouting shardRouting = TestShardRouting.newShardRouting( - indexMetadata.getIndex().getName(), + indexMetadata.getIndex(), randomInt(10), clusterState.nodes().iterator().next().getId(), randomBoolean(), @@ -668,7 +668,7 @@ public void testNeedsThisTier() { ClusterState clusterState = stateBuilder.build(); ShardRouting shardRouting = TestShardRouting.newShardRouting( - indexMetadata.getIndex().getName(), + indexMetadata.getIndex(), randomInt(10), clusterState.nodes().iterator().next().getId(), randomBoolean(), @@ -701,7 +701,7 @@ public void testNeedsThisTierLegacy() { boolean primary = randomBoolean(); ShardRouting shardRouting = TestShardRouting.newShardRouting( - indexMetadata.getIndex().getName(), + indexMetadata.getIndex(), randomInt(10), clusterState.nodes().iterator().next().getId(), primary, diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinatorTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinatorTests.java index 0730b2ae4dc46..07ded2d1afa35 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinatorTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinatorTests.java @@ -750,10 +750,13 @@ public void testGetLeaderIndicesToFollow() { routingTableBuilder.add(indexRoutingTable); } - imdBuilder.put(IndexMetadata.builder("logs-0").settings(settings(IndexVersion.current())).numberOfShards(1).numberOfReplicas(0)); - ShardRouting shardRouting = TestShardRouting.newShardRouting("logs-0", 0, "1", true, ShardRoutingState.INITIALIZING) + Index indexLogs = new Index("logs-0", randomUUID()); + imdBuilder.put( + IndexMetadata.builder(indexLogs.getName()).settings(indexSettings(IndexVersion.current(), indexLogs.getUUID(), 1, 0)) + ); + ShardRouting shardRouting = TestShardRouting.newShardRouting(indexLogs, 0, "1", true, ShardRoutingState.INITIALIZING) .moveToStarted(ShardRouting.UNAVAILABLE_EXPECTED_SHARD_SIZE); - IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(imdBuilder.get("logs-0").getIndex()).addShard(shardRouting).build(); + IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(indexLogs).addShard(shardRouting).build(); routingTableBuilder.add(indexRoutingTable); ClusterState remoteState = ClusterState.builder(new ClusterName("remote")) @@ -813,9 +816,10 @@ public void testGetLeaderIndicesToFollow_shardsNotStarted() { // 1 shard started and another not started: ClusterState remoteState = createRemoteClusterState("index1", true); Metadata.Builder mBuilder = Metadata.builder(remoteState.metadata()); - mBuilder.put(IndexMetadata.builder("index2").settings(settings(IndexVersion.current())).numberOfShards(1).numberOfReplicas(0)); - ShardRouting shardRouting = TestShardRouting.newShardRouting("index2", 0, "1", true, ShardRoutingState.INITIALIZING); - IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(mBuilder.get("index2").getIndex()).addShard(shardRouting).build(); + Index index2 = new Index("index2", randomUUID()); + mBuilder.put(IndexMetadata.builder(index2.getName()).settings(indexSettings(IndexVersion.current(), index2.getUUID(), 1, 0))); + ShardRouting shardRouting = TestShardRouting.newShardRouting(index2, 0, "1", true, ShardRoutingState.INITIALIZING); + IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(index2).addShard(shardRouting).build(); remoteState = ClusterState.builder(remoteState.getClusterName()) .metadata(mBuilder) .routingTable(RoutingTable.builder(remoteState.routingTable()).add(indexRoutingTable).build()) @@ -827,9 +831,7 @@ public void testGetLeaderIndicesToFollow_shardsNotStarted() { // Start second shard: shardRouting = shardRouting.moveToStarted(ShardRouting.UNAVAILABLE_EXPECTED_SHARD_SIZE); - indexRoutingTable = IndexRoutingTable.builder(remoteState.metadata().getProject().indices().get("index2").getIndex()) - .addShard(shardRouting) - .build(); + indexRoutingTable = IndexRoutingTable.builder(index2).addShard(shardRouting).build(); remoteState = ClusterState.builder(remoteState.getClusterName()) .metadata(remoteState.metadata()) .routingTable(RoutingTable.builder(remoteState.routingTable()).add(indexRoutingTable).build()) @@ -844,21 +846,20 @@ public void testGetLeaderIndicesToFollow_shardsNotStarted() { public void testGetLeaderIndicesToFollowWithClosedIndices() { final AutoFollowPattern autoFollowPattern = createAutoFollowPattern("remote", "*"); + Index index = new Index("test-index", randomUUID()); // index is opened - ClusterState remoteState = ClusterStateCreationUtils.stateWithActivePrimary("test-index", true, randomIntBetween(1, 3), 0); + ClusterState remoteState = ClusterStateCreationUtils.stateWithActivePrimary(index, true, randomIntBetween(1, 3), 0); List result = AutoFollower.getLeaderIndicesToFollow(autoFollowPattern, remoteState, Collections.emptyList()); assertThat(result.size(), equalTo(1)); - assertThat(result, hasItem(remoteState.metadata().getProject().index("test-index").getIndex())); + assertThat(result, hasItem(index)); // index is closed remoteState = ClusterState.builder(remoteState) .metadata( Metadata.builder(remoteState.metadata()) .put( - IndexMetadata.builder(remoteState.metadata().getProject().index("test-index")) - .state(IndexMetadata.State.CLOSE) - .build(), + IndexMetadata.builder(remoteState.metadata().getProject().index(index)).state(IndexMetadata.State.CLOSE).build(), true ) .build() @@ -1379,7 +1380,9 @@ public void testStats() { Arrays.asList( new AutoFollowCoordinator.AutoFollowResult( "_alias1", - Collections.singletonList(Tuple.tuple(new Index("index1", "_na_"), new RuntimeException("error-1"))) + Collections.singletonList( + Tuple.tuple(new Index("index1", IndexMetadata.INDEX_UUID_NA_VALUE), new RuntimeException("error-1")) + ) ), new AutoFollowCoordinator.AutoFollowResult( "_alias2", diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ShardChangesActionTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ShardChangesActionTests.java index 5f6db10a1aaf7..d7f21d9eef6ad 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ShardChangesActionTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ShardChangesActionTests.java @@ -149,7 +149,14 @@ public void testGetOperations() throws Exception { public void testGetOperationsWhenShardNotStarted() throws Exception { IndexShard indexShard = Mockito.mock(IndexShard.class); - ShardRouting shardRouting = TestShardRouting.newShardRouting("index", 0, "_node_id", true, ShardRoutingState.INITIALIZING); + ShardRouting shardRouting = TestShardRouting.newShardRouting( + "index", + randomUUID(), + 0, + "_node_id", + true, + ShardRoutingState.INITIALIZING + ); Mockito.when(indexShard.routingEntry()).thenReturn(shardRouting); expectThrows( IndexShardNotStartedException.class, diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/datatiers/DataTierUsageFixtures.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/datatiers/DataTierUsageFixtures.java index b7b6338ce9704..6915eeb855989 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/datatiers/DataTierUsageFixtures.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/datatiers/DataTierUsageFixtures.java @@ -101,7 +101,7 @@ private static ShardStats shardStat(long byteCount, long docCount, ShardRouting } static IndexMetadata indexMetadata(String indexName, int numberOfShards, int numberOfReplicas, String... dataTierPrefs) { - Settings.Builder settingsBuilder = indexSettings(IndexVersion.current(), numberOfShards, numberOfReplicas).put( + Settings.Builder settingsBuilder = indexSettings(IndexVersion.current(), randomUUID(), numberOfShards, numberOfReplicas).put( SETTING_CREATION_DATE, System.currentTimeMillis() ); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForActiveShardsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForActiveShardsTests.java index 60454d8294e9e..054ec5df04b70 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForActiveShardsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForActiveShardsTests.java @@ -111,12 +111,8 @@ public void testResultEvaluatedOnWriteIndexAliasWhenExists() { ShardRoutingRoleStrategy.NO_SHARD_CREATION, rolledIndex.getIndex() ); - routingTable.addShard( - TestShardRouting.newShardRouting(rolledIndex.getIndex().getName(), 0, "node", null, true, ShardRoutingState.STARTED) - ); - routingTable.addShard( - TestShardRouting.newShardRouting(rolledIndex.getIndex().getName(), 0, "node2", null, false, ShardRoutingState.STARTED) - ); + routingTable.addShard(TestShardRouting.newShardRouting(rolledIndex.getIndex(), 0, "node", true, ShardRoutingState.STARTED)); + routingTable.addShard(TestShardRouting.newShardRouting(rolledIndex.getIndex(), 0, "node2", false, ShardRoutingState.STARTED)); var project = ProjectMetadata.builder(randomProjectIdOrDefault()).put(originalIndex, false).put(rolledIndex, false).build(); ProjectState state = ClusterState.builder(ClusterName.DEFAULT) .putProjectMetadata(project) @@ -151,12 +147,8 @@ public void testResultEvaluatedOnOnlyIndexTheAliasPointsToIfWriteIndexIsNull() { ShardRoutingRoleStrategy.NO_SHARD_CREATION, rolledIndex.getIndex() ); - routingTable.addShard( - TestShardRouting.newShardRouting(rolledIndex.getIndex().getName(), 0, "node", null, true, ShardRoutingState.STARTED) - ); - routingTable.addShard( - TestShardRouting.newShardRouting(rolledIndex.getIndex().getName(), 0, "node2", null, false, ShardRoutingState.STARTED) - ); + routingTable.addShard(TestShardRouting.newShardRouting(rolledIndex.getIndex(), 0, "node", true, ShardRoutingState.STARTED)); + routingTable.addShard(TestShardRouting.newShardRouting(rolledIndex.getIndex(), 0, "node2", false, ShardRoutingState.STARTED)); var project = ProjectMetadata.builder(randomProjectIdOrDefault()).put(originalIndex, false).put(rolledIndex, false).build(); ProjectState state = ClusterState.builder(ClusterName.DEFAULT) .putProjectMetadata(project) @@ -204,24 +196,13 @@ public void testResultEvaluatedOnDataStream() throws IOException { ShardRoutingRoleStrategy.NO_SHARD_CREATION, failureRolledIndexMeta.getIndex() ); - routingTable.addShard( - TestShardRouting.newShardRouting(rolledIndexMeta.getIndex().getName(), 0, "node", null, true, ShardRoutingState.STARTED) - ); - routingTable.addShard( - TestShardRouting.newShardRouting(rolledIndexMeta.getIndex().getName(), 0, "node2", null, false, ShardRoutingState.STARTED) - ); + routingTable.addShard(TestShardRouting.newShardRouting(rolledIndexMeta.getIndex(), 0, "node", true, ShardRoutingState.STARTED)); + routingTable.addShard(TestShardRouting.newShardRouting(rolledIndexMeta.getIndex(), 0, "node2", false, ShardRoutingState.STARTED)); failureRoutingTable.addShard( - TestShardRouting.newShardRouting(failureRolledIndexMeta.getIndex().getName(), 0, "node", null, true, ShardRoutingState.STARTED) + TestShardRouting.newShardRouting(failureRolledIndexMeta.getIndex(), 0, "node", true, ShardRoutingState.STARTED) ); failureRoutingTable.addShard( - TestShardRouting.newShardRouting( - failureRolledIndexMeta.getIndex().getName(), - 0, - "node2", - null, - false, - ShardRoutingState.STARTED - ) + TestShardRouting.newShardRouting(failureRolledIndexMeta.getIndex(), 0, "node2", false, ShardRoutingState.STARTED) ); final var project = ProjectMetadata.builder(randomProjectIdOrDefault()) @@ -280,12 +261,8 @@ public void testResultReportsMeaningfulMessage() throws IOException { ShardRoutingRoleStrategy.NO_SHARD_CREATION, rolledIndex.getIndex() ); - routingTable.addShard( - TestShardRouting.newShardRouting(rolledIndex.getIndex().getName(), 0, "node", null, true, ShardRoutingState.STARTED) - ); - routingTable.addShard( - TestShardRouting.newShardRouting(rolledIndex.getIndex().getName(), 0, "node2", null, false, ShardRoutingState.STARTED) - ); + routingTable.addShard(TestShardRouting.newShardRouting(rolledIndex.getIndex(), 0, "node", true, ShardRoutingState.STARTED)); + routingTable.addShard(TestShardRouting.newShardRouting(rolledIndex.getIndex(), 0, "node2", false, ShardRoutingState.STARTED)); var project = ProjectMetadata.builder(randomProjectIdOrDefault()).put(originalIndex, false).put(rolledIndex, false).build(); ProjectState state = ClusterState.builder(ClusterName.DEFAULT) .putProjectMetadata(project) diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForIndexColorStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForIndexColorStepTests.java index dfa53f032cf30..f848b76ab8ee5 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForIndexColorStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForIndexColorStepTests.java @@ -230,13 +230,7 @@ public void testStepReturnsFalseIfTargetIndexIsMissing() { .build(); String indexPrefix = randomAlphaOfLengthBetween(5, 10) + "-"; - ShardRouting shardRouting = TestShardRouting.newShardRouting( - originalIndex.getIndex().getName(), - 0, - "1", - true, - ShardRoutingState.STARTED - ); + ShardRouting shardRouting = TestShardRouting.newShardRouting(originalIndex.getIndex(), 0, "1", true, ShardRoutingState.STARTED); IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(originalIndex.getIndex()).addShard(shardRouting).build(); final var project = ProjectMetadata.builder(randomProjectIdOrDefault()).put(originalIndex, false).build(); @@ -272,7 +266,7 @@ public void testStepWaitsForTargetIndexHealthWhenPrefixConfigured() { .numberOfReplicas(2) .build(); ShardRouting originalShardRouting = TestShardRouting.newShardRouting( - originalIndex.getIndex().getName(), + originalIndex.getIndex(), 0, "1", true, diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/searchablesnapshots/SearchableSnapshotShardStatsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/searchablesnapshots/SearchableSnapshotShardStatsTests.java index 10611e7502ee7..a832d89df2477 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/searchablesnapshots/SearchableSnapshotShardStatsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/searchablesnapshots/SearchableSnapshotShardStatsTests.java @@ -34,6 +34,7 @@ protected SearchableSnapshotShardStats createTestInstance() { IndexId indexId = new IndexId(randomAlphaOfLength(5), randomAlphaOfLength(5)); ShardRouting shardRouting = TestShardRouting.newShardRouting( randomAlphaOfLength(5), + randomUUID(), randomInt(10), randomAlphaOfLength(5), randomBoolean(), diff --git a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/enrich/LookupFromIndexOperatorTests.java b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/enrich/LookupFromIndexOperatorTests.java index 75b1a4db120e1..0422960805a9a 100644 --- a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/enrich/LookupFromIndexOperatorTests.java +++ b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/enrich/LookupFromIndexOperatorTests.java @@ -42,6 +42,7 @@ import org.elasticsearch.core.IOUtils; import org.elasticsearch.core.Releasable; import org.elasticsearch.core.Releasables; +import org.elasticsearch.index.Index; import org.elasticsearch.index.mapper.KeywordFieldMapper; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.mapper.MapperServiceTestCase; @@ -186,7 +187,7 @@ private LookupFromIndexService lookupService(DriverContext mainContext) { IndexNameExpressionResolver indexNameExpressionResolver = TestIndexNameExpressionResolver.newInstance(); releasables.add(clusterService::stop); final var projectId = randomProjectIdOrDefault(); - ClusterServiceUtils.setState(clusterService, ClusterStateCreationUtils.state(projectId, "idx", 1, 1)); + ClusterServiceUtils.setState(clusterService, ClusterStateCreationUtils.state(projectId, new Index("idx", randomUUID()), 1, 1)); if (beCranky) { logger.info("building a cranky lookup"); } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedNodeSelectorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedNodeSelectorTests.java index 50c260eff3d89..4052fae13907a 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedNodeSelectorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedNodeSelectorTests.java @@ -718,10 +718,10 @@ private void givenClusterStateWithDatastream( private static RoutingTable generateRoutingTable(IndexMetadata indexMetadata, List> states) { IndexRoutingTable.Builder rtBuilder = IndexRoutingTable.builder(indexMetadata.getIndex()); - final String index = indexMetadata.getIndex().getName(); + final Index index = indexMetadata.getIndex(); int counter = 0; for (Tuple state : states) { - ShardId shardId = new ShardId(index, "_na_", counter); + ShardId shardId = new ShardId(index, counter); IndexShardRoutingTable.Builder shardRTBuilder = new IndexShardRoutingTable.Builder(shardId); ShardRouting shardRouting; diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndicesStatsMonitoringDocTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndicesStatsMonitoringDocTests.java index d01f2e0168a72..181e3a0721ee6 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndicesStatsMonitoringDocTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndicesStatsMonitoringDocTests.java @@ -221,6 +221,13 @@ private ShardPath mockShardPath() { } private ShardRouting mockShardRouting(final boolean primary) { - return TestShardRouting.newShardRouting(randomAlphaOfLength(5), randomInt(), randomAlphaOfLength(5), primary, INITIALIZING); + return TestShardRouting.newShardRouting( + randomAlphaOfLength(5), + randomUUID(), + randomInt(), + randomAlphaOfLength(5), + primary, + INITIALIZING + ); } } diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/shards/ShardsMonitoringDocTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/shards/ShardsMonitoringDocTests.java index b001ad42ad3c4..d3e30ea0dd222 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/shards/ShardsMonitoringDocTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/shards/ShardsMonitoringDocTests.java @@ -41,6 +41,7 @@ public void setUp() throws Exception { node = assignedToNode ? MonitoringTestUtils.randomMonitoringNode(random()) : null; shardRouting = newShardRouting( randomAlphaOfLength(5), + randomUUID(), randomIntBetween(0, 5), assignedToNode ? node.getUUID() : null, randomBoolean(), @@ -90,28 +91,28 @@ public void testConstructorStateUuidMustNotBeNull() { } public void testIdWithPrimaryShardAssigned() { - shardRouting = newShardRouting("_index_0", 123, "_node_0", randomAlphaOfLength(5), true, INITIALIZING); + shardRouting = newShardRouting("_index_0", randomUUID(), 123, "_node_0", randomAlphaOfLength(5), true, INITIALIZING); assertEquals("_state_uuid_0:_node_0:_index_0:s123:p", ShardMonitoringDoc.id("_state_uuid_0", shardRouting, 0)); } public void testIdWithReplicaShardAssigned() { - shardRouting = newShardRouting("_index_1", 456, "_node_1", randomAlphaOfLength(5), false, INITIALIZING); + shardRouting = newShardRouting("_index_1", randomUUID(), 456, "_node_1", randomAlphaOfLength(5), false, INITIALIZING); assertEquals("_state_uuid_1:_node_1:_index_1:s456:r1", ShardMonitoringDoc.id("_state_uuid_1", shardRouting, 1)); } public void testIdWithPrimaryShardUnassigned() { - shardRouting = newShardRouting("_index_2", 789, null, true, UNASSIGNED); + shardRouting = newShardRouting("_index_2", randomUUID(), 789, null, true, UNASSIGNED); assertEquals("_state_uuid_2:_na:_index_2:s789:p", ShardMonitoringDoc.id("_state_uuid_2", shardRouting, 0)); } public void testIdWithReplicaShardUnassigned() { - shardRouting = newShardRouting("_index_3", 159, null, false, UNASSIGNED); + shardRouting = newShardRouting("_index_3", randomUUID(), 159, null, false, UNASSIGNED); assertEquals("_state_uuid_3:_na:_index_3:s159:r1", ShardMonitoringDoc.id("_state_uuid_3", shardRouting, 1)); } @Override public void testToXContent() throws IOException { - shardRouting = newShardRouting("_index", 1, "_index_uuid", "_node_uuid", true, INITIALIZING); + shardRouting = newShardRouting("_index", randomUUID(), 1, "_index_uuid", "_node_uuid", true, INITIALIZING); final MonitoringDoc.Node node = new MonitoringDoc.Node("_uuid", "_host", "_addr", "_ip", "_name", 1504169190855L); final ShardMonitoringDoc doc = new ShardMonitoringDoc( "_cluster", diff --git a/x-pack/plugin/searchable-snapshots/src/test/java/org/elasticsearch/xpack/searchablesnapshots/action/SearchableSnapshotsStatsResponseTests.java b/x-pack/plugin/searchable-snapshots/src/test/java/org/elasticsearch/xpack/searchablesnapshots/action/SearchableSnapshotsStatsResponseTests.java index f3de4052cd127..402d9d4302458 100644 --- a/x-pack/plugin/searchable-snapshots/src/test/java/org/elasticsearch/xpack/searchablesnapshots/action/SearchableSnapshotsStatsResponseTests.java +++ b/x-pack/plugin/searchable-snapshots/src/test/java/org/elasticsearch/xpack/searchablesnapshots/action/SearchableSnapshotsStatsResponseTests.java @@ -15,6 +15,7 @@ import org.elasticsearch.cluster.routing.ShardRouting; import org.elasticsearch.cluster.routing.ShardRoutingState; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.index.Index; import org.elasticsearch.repositories.IndexId; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.snapshots.SnapshotId; @@ -110,7 +111,7 @@ private SearchableSnapshotsStatsResponse createTestInstance() { final int successfulShards = totalShards > 0 ? randomIntBetween(0, totalShards) : 0; final int failedShards = totalShards - successfulShards; - final String indexName = randomAlphaOfLength(10); + final Index index = new Index(randomAlphaOfLength(10), randomUUID()); final int replicas = randomIntBetween(0, 2); final SnapshotId snapshotId = new SnapshotId(randomAlphaOfLength(5), randomAlphaOfLength(5)); final IndexId indexId = new IndexId(randomAlphaOfLength(5), randomAlphaOfLength(5)); @@ -120,12 +121,12 @@ private SearchableSnapshotsStatsResponse createTestInstance() { for (int i = 0; i < totalShards; i++) { if (i < successfulShards) { - shardStats.add(createSearchableSnapshotShardStats(indexName, i, true, snapshotId, indexId)); + shardStats.add(createSearchableSnapshotShardStats(index, i, true, snapshotId, indexId)); for (int j = 0; j < replicas; j++) { - shardStats.add(createSearchableSnapshotShardStats(indexName, i, false, snapshotId, indexId)); + shardStats.add(createSearchableSnapshotShardStats(index, i, false, snapshotId, indexId)); } } else { - shardFailures.add(new DefaultShardOperationFailedException(indexName, i, new Exception())); + shardFailures.add(new DefaultShardOperationFailedException(index.getName(), i, new Exception())); } } @@ -133,7 +134,7 @@ private SearchableSnapshotsStatsResponse createTestInstance() { } private static SearchableSnapshotShardStats createSearchableSnapshotShardStats( - String index, + Index index, int shardId, boolean primary, SnapshotId snapshotId, diff --git a/x-pack/plugin/searchable-snapshots/src/test/java/org/elasticsearch/xpack/searchablesnapshots/recovery/SearchableSnapshotsRecoveryStateTests.java b/x-pack/plugin/searchable-snapshots/src/test/java/org/elasticsearch/xpack/searchablesnapshots/recovery/SearchableSnapshotsRecoveryStateTests.java index 9aece3ad7e8c4..fbba9067b5dc2 100644 --- a/x-pack/plugin/searchable-snapshots/src/test/java/org/elasticsearch/xpack/searchablesnapshots/recovery/SearchableSnapshotsRecoveryStateTests.java +++ b/x-pack/plugin/searchable-snapshots/src/test/java/org/elasticsearch/xpack/searchablesnapshots/recovery/SearchableSnapshotsRecoveryStateTests.java @@ -145,6 +145,7 @@ public void testResetAfterRemoteTranslogIsSetResetsFlag() { private SearchableSnapshotRecoveryState createRecoveryState() { ShardRouting shardRouting = TestShardRouting.newShardRouting( randomAlphaOfLength(10), + randomUUID(), 0, randomAlphaOfLength(10), true, diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/AuthorizedIndicesTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/AuthorizedIndicesTests.java index e7ca427966bc4..181a71566b8a6 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/AuthorizedIndicesTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/AuthorizedIndicesTests.java @@ -275,7 +275,7 @@ public void testDataStreamsAreNotIncludedInAuthorizedIndices() { true ) .put(new IndexMetadata.Builder(backingIndex).settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true) - .put(DataStreamTestHelper.newInstance("adatastream1", List.of(new Index(backingIndex, "_na_")))) + .put(DataStreamTestHelper.newInstance("adatastream1", List.of(new Index(backingIndex, IndexMetadata.INDEX_UUID_NA_VALUE)))) .build(); final PlainActionFuture future = new PlainActionFuture<>(); final Set descriptors = Sets.newHashSet(aStarRole, bRole); diff --git a/x-pack/plugin/transform/src/internalClusterTest/java/org/elasticsearch/xpack/transform/checkpoint/TransformGetCheckpointTests.java b/x-pack/plugin/transform/src/internalClusterTest/java/org/elasticsearch/xpack/transform/checkpoint/TransformGetCheckpointTests.java index a3c4776543350..4604a3cf0fc37 100644 --- a/x-pack/plugin/transform/src/internalClusterTest/java/org/elasticsearch/xpack/transform/checkpoint/TransformGetCheckpointTests.java +++ b/x-pack/plugin/transform/src/internalClusterTest/java/org/elasticsearch/xpack/transform/checkpoint/TransformGetCheckpointTests.java @@ -55,6 +55,7 @@ import java.time.Clock; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Map; @@ -77,7 +78,7 @@ public class TransformGetCheckpointTests extends ESSingleNodeTestCase { private Client client; private Task transformTask; private final String indexNamePattern = "test_index-"; - private String[] testIndices; + private Index[] testIndices; private int numberOfNodes; private int numberOfIndices; private int numberOfShards; @@ -126,11 +127,11 @@ protected void onSendRequest(long requestId, String action, TransportRequest req transportService.start(); transportService.acceptIncomingRequests(); - List testIndicesList = new ArrayList<>(); + List testIndicesList = new ArrayList<>(); for (int i = 0; i < numberOfIndices; ++i) { - testIndicesList.add(indexNamePattern + i); + testIndicesList.add(new Index(indexNamePattern + i, randomUUID())); } - testIndices = testIndicesList.toArray(new String[0]); + testIndices = testIndicesList.toArray(new Index[0]); clusterStateWithIndex = ClusterState.builder(ClusterStateCreationUtils.state(numberOfNodes, testIndices, numberOfShards)) .putCompatibilityVersions("node01", TransportVersions.V_8_5_0, Map.of()) @@ -196,7 +197,7 @@ public void testSingleIndexRequest() throws InterruptedException { public void testMultiIndexRequest() throws InterruptedException { GetCheckpointAction.Request request = new GetCheckpointAction.Request( - testIndices, + Arrays.stream(testIndices).map(Index::getName).toArray(String[]::new), IndicesOptions.LENIENT_EXPAND_OPEN, null, null, diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/execution/TriggeredWatchStoreTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/execution/TriggeredWatchStoreTests.java index d734d5816aa7d..909f00ca0e05a 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/execution/TriggeredWatchStoreTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/execution/TriggeredWatchStoreTests.java @@ -347,7 +347,7 @@ public void testLoadingFailsWithTwoAliases() { IndexRoutingTable.Builder indexRoutingTableBuilder = IndexRoutingTable.builder(index); indexRoutingTableBuilder.addIndexShard( new IndexShardRoutingTable.Builder(new ShardId(index, 0)).addShard( - TestShardRouting.newShardRouting("triggered-watches-alias", 0, "_node_id", null, true, ShardRoutingState.STARTED) + TestShardRouting.newShardRouting(index, 0, "_node_id", null, true, ShardRoutingState.STARTED) ) ); indexRoutingTableBuilder.addReplica(ShardRouting.Role.DEFAULT); @@ -355,7 +355,7 @@ public void testLoadingFailsWithTwoAliases() { IndexRoutingTable.Builder otherIndexRoutingTableBuilder = IndexRoutingTable.builder(otherIndex); otherIndexRoutingTableBuilder.addIndexShard( new IndexShardRoutingTable.Builder(new ShardId(otherIndex, 0)).addShard( - TestShardRouting.newShardRouting("whatever", 0, "_node_id", null, true, ShardRoutingState.STARTED) + TestShardRouting.newShardRouting(otherIndex, 0, "_node_id", null, true, ShardRoutingState.STARTED) ) ); diff --git a/x-pack/plugin/wildcard/src/test/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldMapperTests.java b/x-pack/plugin/wildcard/src/test/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldMapperTests.java index 15a6efdff47c4..3d9f8910be94e 100644 --- a/x-pack/plugin/wildcard/src/test/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldMapperTests.java +++ b/x-pack/plugin/wildcard/src/test/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldMapperTests.java @@ -1080,7 +1080,7 @@ protected MappedFieldType provideMappedFieldType(String name) { } protected final SearchExecutionContext createMockContext() { - Index index = new Index(randomAlphaOfLengthBetween(1, 10), "_na_"); + Index index = new Index(randomAlphaOfLengthBetween(1, 10), IndexMetadata.INDEX_UUID_NA_VALUE); IndexSettings idxSettings = IndexSettingsModule.newIndexSettings( index, Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, IndexVersion.current()).build()