Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mapping IllegalArgumentException to SettingsException extend OpenSearchException in AbstractScopedSettings class #4792

Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Backport Apache Lucene version change for 2.4.0 ([#4677](https://github.com/opensearch-project/OpenSearch/pull/4677))
- Refactor Base Action class javadocs to OpenSearch.API ([#4732](https://github.com/opensearch-project/OpenSearch/pull/4732))
- Migrate client transports to Apache HttpClient / Core 5.x ([#4459](https://github.com/opensearch-project/OpenSearch/pull/4459))
- Mapping IllegalArgumentException to InvalidArgumentException extend OpenSearchException in AbstractScopedSettings class ([#4792](https://github.com/opensearch-project/OpenSearch/pull/4792))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Describe what has changed for the caller instead?


### Deprecated

Expand Down Expand Up @@ -169,4 +170,4 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
### Security

[Unreleased]: https://github.com/opensearch-project/OpenSearch/compare/2.2.0...HEAD
[2.x]: https://github.com/opensearch-project/OpenSearch/compare/2.2.0...2.x
[2.x]: https://github.com/opensearch-project/OpenSearch/compare/2.2.0...2.x
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public void testClusterUpdateSettingNonExistent() {
assertThat(exception.status(), equalTo(RestStatus.BAD_REQUEST));
assertThat(
exception.getMessage(),
equalTo("OpenSearch exception [type=illegal_argument_exception, reason=transient setting [" + setting + "], not recognized]")
equalTo("OpenSearch exception [type=settings_exception, reason=transient setting [" + setting + "], not recognized]")
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1437,8 +1437,7 @@ public void testIndexPutSettings() throws IOException {
assertThat(
exception.getMessage(),
startsWith(
"OpenSearch exception [type=illegal_argument_exception, "
+ "reason=final index setting [index.number_of_shards], not updateable"
"OpenSearch exception [type=settings_exception, " + "reason=final index setting [index.number_of_shards], not updateable"
)
);
}
Expand Down Expand Up @@ -1475,7 +1474,7 @@ public void testIndexPutSettingNonExistent() throws IOException {
assertThat(
exception.getMessage(),
equalTo(
"OpenSearch exception [type=illegal_argument_exception, "
"OpenSearch exception [type=settings_exception, "
+ "reason=unknown setting [index.no_idea_what_you_are_talking_about] please check that any required plugins are installed, "
+ "or check the breaking changes documentation for removed settings]"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@
import org.opensearch.cloud.azure.classic.management.AzureComputeService.Discovery;
import org.opensearch.cloud.azure.classic.management.AzureComputeService.Management;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.settings.SettingsException;
import org.opensearch.test.OpenSearchIntegTestCase;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.instanceOf;

@OpenSearchIntegTestCase.ClusterScope(scope = OpenSearchIntegTestCase.Scope.TEST, numDataNodes = 0, numClientNodes = 0)
public class AzureSimpleTests extends AbstractAzureComputeServiceTestCase {
Expand Down Expand Up @@ -78,7 +80,8 @@ public void testOneNodeShouldRunUsingWrongSettings() {
.put(Management.SERVICE_NAME_SETTING.getKey(), "dummy")
.put(Discovery.HOST_TYPE_SETTING.getKey(), "do_not_exist");

IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> internalCluster().startNode(settings));
assertThat(e.getMessage(), containsString("invalid value for host type [do_not_exist]"));
SettingsException e = expectThrows(SettingsException.class, () -> internalCluster().startNode(settings));
assertThat(e.getCause(), instanceOf(IllegalArgumentException.class));
assertThat(e.getCause().getMessage(), containsString("invalid value for host type [do_not_exist]"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@

package org.opensearch.example.customsettings;

import org.hamcrest.Matchers;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.settings.SettingsException;
import org.opensearch.test.OpenSearchTestCase;

import static org.opensearch.example.customsettings.ExampleCustomSettingsConfig.VALIDATED_SETTING;
Expand All @@ -50,10 +52,11 @@ public void testValidatedSetting() {
final String actual = VALIDATED_SETTING.get(Settings.builder().put(VALIDATED_SETTING.getKey(), expected).build());
assertEquals(expected, actual);

final IllegalArgumentException exception = expectThrows(
IllegalArgumentException.class,
final SettingsException exception = expectThrows(
SettingsException.class,
() -> VALIDATED_SETTING.get(Settings.builder().put("custom.validated", "it's forbidden").build())
);
assertEquals("Setting must not contain [forbidden]", exception.getMessage());
assertThat(exception.getCause(), Matchers.instanceOf(IllegalArgumentException.class));
assertEquals("Setting must not contain [forbidden]", exception.getCause().getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

package org.opensearch.repositories.azure;

import org.opensearch.common.settings.SettingsException;
import reactor.core.scheduler.Schedulers;

import org.junit.AfterClass;
Expand All @@ -46,6 +47,7 @@
import org.opensearch.repositories.blobstore.BlobStoreTestUtil;
import org.opensearch.test.OpenSearchTestCase;

import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -161,21 +163,22 @@ public void testChunkSize() {
assertEquals(new ByteSizeValue(size, ByteSizeUnit.MB), azureRepository.chunkSize());

// zero bytes is not allowed
IllegalArgumentException e = expectThrows(
IllegalArgumentException.class,
SettingsException e = expectThrows(
SettingsException.class,
() -> azureRepository(Settings.builder().put("chunk_size", "0").build())
);
assertEquals("failed to parse value [0] for setting [chunk_size], must be >= [1b]", e.getMessage());
assertThat(e.getCause(), instanceOf(IllegalArgumentException.class));
assertEquals("failed to parse value [0] for setting [chunk_size], must be >= [1b]", e.getCause().getMessage());

// negative bytes not allowed
e = expectThrows(IllegalArgumentException.class, () -> azureRepository(Settings.builder().put("chunk_size", "-1").build()));
assertEquals("failed to parse value [-1] for setting [chunk_size], must be >= [1b]", e.getMessage());
e = expectThrows(SettingsException.class, () -> azureRepository(Settings.builder().put("chunk_size", "-1").build()));
assertEquals("failed to parse value [-1] for setting [chunk_size], must be >= [1b]", e.getCause().getMessage());

// greater than max chunk size not allowed
e = expectThrows(IllegalArgumentException.class, () -> azureRepository(Settings.builder().put("chunk_size", "6tb").build()));
e = expectThrows(SettingsException.class, () -> azureRepository(Settings.builder().put("chunk_size", "6tb").build()));
assertEquals(
"failed to parse value [6tb] for setting [chunk_size], must be <= [" + AzureStorageService.MAX_CHUNK_SIZE.getStringRep() + "]",
e.getMessage()
e.getCause().getMessage()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.opensearch.cluster.metadata.RepositoryMetadata;
import org.opensearch.common.settings.ClusterSettings;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.settings.SettingsException;
import org.opensearch.common.unit.ByteSizeUnit;
import org.opensearch.common.unit.ByteSizeValue;
import org.opensearch.common.xcontent.NamedXContentRegistry;
Expand Down Expand Up @@ -88,17 +89,16 @@ public void testInvalidChunkBufferSizeSettings() {
createS3Repo(getRepositoryMetadata(s3)).close();
// buffer < 5mb should fail
final Settings s4 = bufferAndChunkSettings(4, 10);
final IllegalArgumentException e2 = expectThrows(
IllegalArgumentException.class,
() -> createS3Repo(getRepositoryMetadata(s4)).close()
);
assertThat(e2.getMessage(), containsString("failed to parse value [4mb] for setting [buffer_size], must be >= [5mb]"));
final SettingsException e2 = expectThrows(SettingsException.class, () -> createS3Repo(getRepositoryMetadata(s4)).close());
assertThat(e2.getCause(), Matchers.instanceOf(IllegalArgumentException.class));
assertThat(e2.getCause().getMessage(), containsString("failed to parse value [4mb] for setting [buffer_size], must be >= [5mb]"));
final Settings s5 = bufferAndChunkSettings(5, 6000000);
final IllegalArgumentException e3 = expectThrows(
IllegalArgumentException.class,
() -> createS3Repo(getRepositoryMetadata(s5)).close()
final SettingsException e3 = expectThrows(SettingsException.class, () -> createS3Repo(getRepositoryMetadata(s5)).close());
assertThat(e3.getCause(), Matchers.instanceOf(IllegalArgumentException.class));
assertThat(
e3.getCause().getMessage(),
containsString("failed to parse value [6000000mb] for setting [chunk_size], must be <= [5tb]")
);
assertThat(e3.getMessage(), containsString("failed to parse value [6000000mb] for setting [chunk_size], must be <= [5tb]"));
}

private Settings bufferAndChunkSettings(long buffer, long chunk) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@

package org.opensearch.cluster.metadata;

import org.hamcrest.Matchers;
import org.opensearch.common.SuppressForbidden;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.settings.SettingsException;
import org.opensearch.test.OpenSearchTestCase;

import static org.opensearch.cluster.metadata.IndexMetadata.DEFAULT_NUMBER_OF_SHARDS;
Expand All @@ -43,20 +45,22 @@ public class EvilSystemPropertyTests extends OpenSearchTestCase {

@SuppressForbidden(reason = "manipulates system properties for testing")
public void testNumShards() {
IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () ->
SettingsException exception = expectThrows(SettingsException.class, () ->
IndexMetadata.buildNumberOfShardsSetting()
.get(Settings.builder().put(SETTING_NUMBER_OF_SHARDS, 1025).build()));
assertEquals("Failed to parse value [1025] for setting [" + SETTING_NUMBER_OF_SHARDS + "] must be <= 1024", exception.getMessage());
assertEquals("Failed to parse value [1025] for setting [" + SETTING_NUMBER_OF_SHARDS + "] must be <= 1024", exception.getCause().getMessage());

Integer numShards = IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.get(Settings.builder().put(SETTING_NUMBER_OF_SHARDS, 100).build());
assertEquals(100, numShards.intValue());
int limit = randomIntBetween(1, 10);
System.setProperty(MAX_NUMBER_OF_SHARDS, Integer.toString(limit));
try {
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () ->
SettingsException e = expectThrows(SettingsException.class, () ->
IndexMetadata.buildNumberOfShardsSetting()
.get(Settings.builder().put("index.number_of_shards", 11).build()));
assertEquals("Failed to parse value [11] for setting [index.number_of_shards] must be <= " + limit, e.getMessage());
Throwable cause = e.getCause();
assertThat(cause, Matchers.instanceOf(IllegalArgumentException.class));
assertEquals("Failed to parse value [11] for setting [index.number_of_shards] must be <= " + limit, cause.getMessage());
System.clearProperty(MAX_NUMBER_OF_SHARDS);

Integer defaultFromSetting = IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getDefault(Settings.EMPTY);
Expand All @@ -70,9 +74,10 @@ public void testNumShards() {
randomDefault = randomIntBetween(1, 10);
System.setProperty(MAX_NUMBER_OF_SHARDS, Integer.toString(randomDefault));
System.setProperty(DEFAULT_NUMBER_OF_SHARDS, Integer.toString(randomDefault + 1));
e = expectThrows(IllegalArgumentException.class, IndexMetadata::buildNumberOfShardsSetting);

cause = expectThrows(IllegalArgumentException.class, IndexMetadata::buildNumberOfShardsSetting);
assertEquals(DEFAULT_NUMBER_OF_SHARDS + " value [" + (randomDefault + 1) + "] must between " +
"1 and " + MAX_NUMBER_OF_SHARDS + " [" + randomDefault + "]", e.getMessage());
"1 and " + MAX_NUMBER_OF_SHARDS + " [" + randomDefault + "]", cause.getMessage());
} finally {
System.clearProperty(MAX_NUMBER_OF_SHARDS);
System.clearProperty(DEFAULT_NUMBER_OF_SHARDS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
cluster.remote.test_remote_cluster.proxy_address: $remote_ip

- match: { status: 400 }
- match: { error.root_cause.0.type: "illegal_argument_exception" }
- match: { error.root_cause.0.type: "settings_exception" }
- match: { error.root_cause.0.reason: "Setting \"cluster.remote.test_remote_cluster.node_connections\" cannot be
used with the configured \"cluster.remote.test_remote_cluster.mode\" [required=SNIFF, configured=PROXY]" }

Expand All @@ -32,7 +32,7 @@
cluster.remote.test_remote_cluster.proxy_address: $remote_ip

- match: { status: 400 }
- match: { error.root_cause.0.type: "illegal_argument_exception" }
- match: { error.root_cause.0.type: "settings_exception" }
- match: { error.root_cause.0.reason: "Setting \"cluster.remote.test_remote_cluster.seeds\" cannot be
used with the configured \"cluster.remote.test_remote_cluster.mode\" [required=SNIFF, configured=PROXY]" }

Expand All @@ -54,7 +54,7 @@
cluster.remote.test_remote_cluster.seeds: $remote_ip

- match: { status: 400 }
- match: { error.root_cause.0.type: "illegal_argument_exception" }
- match: { error.root_cause.0.type: "settings_exception" }
- match: { error.root_cause.0.reason: "Setting \"cluster.remote.test_remote_cluster.proxy_socket_connections\" cannot be
used with the configured \"cluster.remote.test_remote_cluster.mode\" [required=PROXY, configured=SNIFF]" }

Expand All @@ -68,7 +68,7 @@
cluster.remote.test_remote_cluster.seeds: $remote_ip

- match: { status: 400 }
- match: { error.root_cause.0.type: "illegal_argument_exception" }
- match: { error.root_cause.0.type: "settings_exception" }
- match: { error.root_cause.0.reason: "Setting \"cluster.remote.test_remote_cluster.proxy_address\" cannot be
used with the configured \"cluster.remote.test_remote_cluster.mode\" [required=PROXY, configured=SNIFF]" }

Expand Down Expand Up @@ -182,7 +182,7 @@
cluster.remote.test_remote_cluster.proxy_address: $remote_ip

- match: { status: 400 }
- match: { error.root_cause.0.type: "illegal_argument_exception" }
- match: { error.root_cause.0.type: "settings_exception" }
- match: { error.root_cause.0.reason: "Setting \"cluster.remote.test_remote_cluster.seeds\" cannot be
used with the configured \"cluster.remote.test_remote_cluster.mode\" [required=SNIFF, configured=PROXY]" }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import org.opensearch.common.regex.Regex;
import org.opensearch.common.settings.IndexScopedSettings;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.settings.SettingsException;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.common.util.set.Sets;
import org.opensearch.common.xcontent.NamedXContentRegistry;
Expand Down Expand Up @@ -220,7 +221,7 @@ ClusterState addComponentTemplate(
) throws Exception {
final ComponentTemplate existing = currentState.metadata().componentTemplates().get(name);
if (create && existing != null) {
throw new IllegalArgumentException("component template [" + name + "] already exists");
throw new SettingsException("component template [" + name + "] already exists");
}

CompressedXContent mappings = template.template().mappings();
Expand Down Expand Up @@ -256,7 +257,7 @@ ClusterState addComponentTemplate(
}
}
if (globalTemplatesThatUseThisComponent.isEmpty() == false) {
throw new IllegalArgumentException(
throw new SettingsException(
"cannot update component template ["
+ name
+ "] because the following global templates would resolve to specifying the ["
Expand Down
Loading