diff --git a/server/src/main/java/org/opensearch/index/IndexSettings.java b/server/src/main/java/org/opensearch/index/IndexSettings.java index 8ac024e003f74..ac6ba8d688c45 100644 --- a/server/src/main/java/org/opensearch/index/IndexSettings.java +++ b/server/src/main/java/org/opensearch/index/IndexSettings.java @@ -112,7 +112,12 @@ public static IndexMergePolicy fromString(String text) { return policy; } } - return null; + throw new IllegalArgumentException( + "The setting has unsupported policy specified: " + + text + + ". Please use one of: " + + String.join(", ", Arrays.stream(IndexMergePolicy.values()).map(IndexMergePolicy::getValue).toArray(String[]::new)) + ); } } @@ -601,34 +606,17 @@ public static IndexMergePolicy fromString(String text) { Property.Dynamic ); - public static final Setting INDEX_MERGE_POLICY = Setting.simpleString("index.merge.policy", DEFAULT_POLICY, policy -> { - if (IndexMergePolicy.fromString(policy) == null) { - throw new IllegalArgumentException( - "The " - + IndexSettings.INDEX_MERGE_POLICY.getKey() - + " has unsupported policy specified: " - + policy - + ". Please use one of: " - + String.join(", ", Arrays.stream(IndexMergePolicy.values()).map(IndexMergePolicy::getValue).toArray(String[]::new)) - ); - } - }, Property.IndexScope); + public static final Setting INDEX_MERGE_POLICY = Setting.simpleString( + "index.merge.policy", + DEFAULT_POLICY, + IndexMergePolicy::fromString, + Property.IndexScope + ); public static final Setting TIME_INDEX_MERGE_POLICY = Setting.simpleString( "indices.time_index.default_index_merge_policy", DEFAULT_POLICY, - policy -> { - if (IndexMergePolicy.fromString(policy) == null) { - throw new IllegalArgumentException( - "The " - + IndexSettings.TIME_INDEX_MERGE_POLICY.getKey() - + " has unsupported policy specified: " - + policy - + ". Please use one of: " - + String.join(", ", Arrays.stream(IndexMergePolicy.values()).map(IndexMergePolicy::getValue).toArray(String[]::new)) - ); - } - }, + IndexMergePolicy::fromString, Property.NodeScope ); diff --git a/server/src/test/java/org/opensearch/index/MergePolicySettingsTests.java b/server/src/test/java/org/opensearch/index/MergePolicySettingsTests.java index 2b6bff33a18ca..a98c00879fbc1 100644 --- a/server/src/test/java/org/opensearch/index/MergePolicySettingsTests.java +++ b/server/src/test/java/org/opensearch/index/MergePolicySettingsTests.java @@ -188,31 +188,25 @@ public void testInvalidMergePolicy() throws IOException { IllegalArgumentException.class, () -> IndexSettings.INDEX_MERGE_POLICY.get(invalidSettings) ); - assertThat(exc1.getMessage(), containsString(IndexSettings.INDEX_MERGE_POLICY.getKey() + " has unsupported policy specified: ")); + assertThat(exc1.getMessage(), containsString(" has unsupported policy specified: ")); IllegalArgumentException exc2 = expectThrows( IllegalArgumentException.class, () -> indexSettings(invalidSettings).getMergePolicy(false) ); - assertThat(exc2.getMessage(), containsString(IndexSettings.INDEX_MERGE_POLICY.getKey() + " has unsupported policy specified: ")); + assertThat(exc2.getMessage(), containsString(" has unsupported policy specified: ")); final Settings invalidSettings2 = Settings.builder().put(IndexSettings.TIME_INDEX_MERGE_POLICY.getKey(), "invalid").build(); IllegalArgumentException exc3 = expectThrows( IllegalArgumentException.class, () -> IndexSettings.TIME_INDEX_MERGE_POLICY.get(invalidSettings2) ); - assertThat( - exc3.getMessage(), - containsString(IndexSettings.TIME_INDEX_MERGE_POLICY.getKey() + " has unsupported policy specified: ") - ); + assertThat(exc3.getMessage(), containsString(" has unsupported policy specified: ")); IllegalArgumentException exc4 = expectThrows( IllegalArgumentException.class, () -> new IndexSettings(newIndexMeta("test", Settings.EMPTY), invalidSettings2).getMergePolicy(true) ); - assertThat( - exc4.getMessage(), - containsString(IndexSettings.TIME_INDEX_MERGE_POLICY.getKey() + " has unsupported policy specified: ") - ); + assertThat(exc4.getMessage(), containsString(" has unsupported policy specified: ")); } public void testUpdateSettingsForLogByteSizeMergePolicy() throws IOException {