Search in sources :

Example 1 with ValidationException

use of org.opensearch.common.ValidationException in project OpenSearch by opensearch-project.

the class MetadataCreateIndexService method validateIndexSettings.

public void validateIndexSettings(String indexName, final Settings settings, final boolean forbidPrivateIndexSettings) throws IndexCreationException {
    List<String> validationErrors = getIndexSettingsValidationErrors(settings, forbidPrivateIndexSettings);
    if (validationErrors.isEmpty() == false) {
        ValidationException validationException = new ValidationException();
        validationException.addValidationErrors(validationErrors);
        throw new IndexCreationException(indexName, validationException);
    }
}
Also used : ValidationException(org.opensearch.common.ValidationException) IndexCreationException(org.opensearch.indices.IndexCreationException)

Example 2 with ValidationException

use of org.opensearch.common.ValidationException in project OpenSearch by opensearch-project.

the class MetadataIndexTemplateService method validate.

private void validate(String name, @Nullable Settings settings, List<String> indexPatterns, List<Alias> aliases) {
    List<String> validationErrors = new ArrayList<>();
    if (name.contains(" ")) {
        validationErrors.add("name must not contain a space");
    }
    if (name.contains(",")) {
        validationErrors.add("name must not contain a ','");
    }
    if (name.contains("#")) {
        validationErrors.add("name must not contain a '#'");
    }
    if (name.contains("*")) {
        validationErrors.add("name must not contain a '*'");
    }
    if (name.startsWith("_")) {
        validationErrors.add("name must not start with '_'");
    }
    if (name.toLowerCase(Locale.ROOT).equals(name) == false) {
        validationErrors.add("name must be lower cased");
    }
    for (String indexPattern : indexPatterns) {
        if (indexPattern.contains(" ")) {
            validationErrors.add("index_patterns [" + indexPattern + "] must not contain a space");
        }
        if (indexPattern.contains(",")) {
            validationErrors.add("index_pattern [" + indexPattern + "] must not contain a ','");
        }
        if (indexPattern.contains("#")) {
            validationErrors.add("index_pattern [" + indexPattern + "] must not contain a '#'");
        }
        if (indexPattern.contains(":")) {
            validationErrors.add("index_pattern [" + indexPattern + "] must not contain a ':'");
        }
        if (indexPattern.startsWith("_")) {
            validationErrors.add("index_pattern [" + indexPattern + "] must not start with '_'");
        }
        if (Strings.validFileNameExcludingAstrix(indexPattern) == false) {
            validationErrors.add("index_pattern [" + indexPattern + "] must not contain the following characters " + Strings.INVALID_FILENAME_CHARS);
        }
    }
    if (settings != null) {
        try {
            // templates must be consistent with regards to dependencies
            indexScopedSettings.validate(settings, true);
        } catch (IllegalArgumentException iae) {
            validationErrors.add(iae.getMessage());
            for (Throwable t : iae.getSuppressed()) {
                validationErrors.add(t.getMessage());
            }
        }
        List<String> indexSettingsValidation = metadataCreateIndexService.getIndexSettingsValidationErrors(settings, true);
        validationErrors.addAll(indexSettingsValidation);
    }
    if (indexPatterns.stream().anyMatch(Regex::isMatchAllPattern)) {
        if (settings != null && IndexMetadata.INDEX_HIDDEN_SETTING.exists(settings)) {
            validationErrors.add("global templates may not specify the setting " + IndexMetadata.INDEX_HIDDEN_SETTING.getKey());
        }
    }
    if (validationErrors.size() > 0) {
        ValidationException validationException = new ValidationException();
        validationException.addValidationErrors(validationErrors);
        throw new InvalidIndexTemplateException(name, validationException.getMessage());
    }
    for (Alias alias : aliases) {
        // we validate the alias only partially, as we don't know yet to which index it'll get applied to
        aliasValidator.validateAliasStandalone(alias);
        if (indexPatterns.contains(alias.name())) {
            throw new IllegalArgumentException("alias [" + alias.name() + "] cannot be the same as any pattern in [" + String.join(", ", indexPatterns) + "]");
        }
    }
}
Also used : ValidationException(org.opensearch.common.ValidationException) Regex(org.opensearch.common.regex.Regex) Alias(org.opensearch.action.admin.indices.alias.Alias) ArrayList(java.util.ArrayList) InvalidIndexTemplateException(org.opensearch.indices.InvalidIndexTemplateException)

Example 3 with ValidationException

use of org.opensearch.common.ValidationException in project OpenSearch by opensearch-project.

the class ShardLimitValidatorTests method testValidateShardLimit.

public void testValidateShardLimit() {
    int nodesInCluster = randomIntBetween(2, 90);
    ShardCounts counts = forDataNodeCount(nodesInCluster);
    ClusterState state = createClusterForShardLimitTest(nodesInCluster, counts.getFirstIndexShards(), counts.getFirstIndexReplicas(), counts.getFailingIndexShards(), counts.getFailingIndexReplicas());
    Index[] indices = Arrays.stream(state.metadata().indices().values().toArray(IndexMetadata.class)).map(IndexMetadata::getIndex).collect(Collectors.toList()).toArray(new Index[2]);
    int totalShards = counts.getFailingIndexShards() * (1 + counts.getFailingIndexReplicas());
    int currentShards = counts.getFirstIndexShards() * (1 + counts.getFirstIndexReplicas());
    int maxShards = counts.getShardsPerNode() * nodesInCluster;
    ShardLimitValidator shardLimitValidator = createTestShardLimitService(counts.getShardsPerNode());
    ValidationException exception = expectThrows(ValidationException.class, () -> shardLimitValidator.validateShardLimit(state, indices));
    assertEquals("Validation Failed: 1: this action would add [" + totalShards + "] total shards, but this cluster currently has [" + currentShards + "]/[" + maxShards + "] maximum shards open;", exception.getMessage());
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) ValidationException(org.opensearch.common.ValidationException) ShardCounts(org.opensearch.cluster.shards.ShardCounts) Index(org.opensearch.index.Index) MetadataIndexStateServiceTests.addClosedIndex(org.opensearch.cluster.metadata.MetadataIndexStateServiceTests.addClosedIndex) MetadataIndexStateServiceTests.addOpenedIndex(org.opensearch.cluster.metadata.MetadataIndexStateServiceTests.addOpenedIndex) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata)

Example 4 with ValidationException

use of org.opensearch.common.ValidationException in project OpenSearch by opensearch-project.

the class ShardLimitValidator method validateShardLimit.

/**
 * Validates whether a list of indices can be opened without going over the cluster shard limit.  Only counts indices which are
 * currently closed and will be opened, ignores indices which are already open.
 *
 * @param currentState The current cluster state.
 * @param indicesToOpen The indices which are to be opened.
 * @throws ValidationException If this operation would take the cluster over the limit and enforcement is enabled.
 */
public void validateShardLimit(ClusterState currentState, Index[] indicesToOpen) {
    int shardsToOpen = Arrays.stream(indicesToOpen).filter(index -> currentState.metadata().index(index).getState().equals(IndexMetadata.State.CLOSE)).mapToInt(index -> getTotalShardCount(currentState, index)).sum();
    Optional<String> error = checkShardLimit(shardsToOpen, currentState);
    if (error.isPresent()) {
        ValidationException ex = new ValidationException();
        ex.addValidationError(error.get());
        throw ex;
    }
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) Arrays(java.util.Arrays) Setting(org.opensearch.common.settings.Setting) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Index(org.opensearch.index.Index) ClusterService(org.opensearch.cluster.service.ClusterService) Optional(java.util.Optional) INDEX_NUMBER_OF_SHARDS_SETTING(org.opensearch.cluster.metadata.IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING) Settings(org.opensearch.common.settings.Settings) ValidationException(org.opensearch.common.ValidationException) ValidationException(org.opensearch.common.ValidationException)

Example 5 with ValidationException

use of org.opensearch.common.ValidationException in project OpenSearch by opensearch-project.

the class ShardLimitValidator method validateShardLimit.

/**
 * Checks whether an index can be created without going over the cluster shard limit.
 *
 * @param settings       the settings of the index to be created
 * @param state          the current cluster state
 * @throws ValidationException if creating this index would put the cluster over the cluster shard limit
 */
public void validateShardLimit(final Settings settings, final ClusterState state) {
    final int numberOfShards = INDEX_NUMBER_OF_SHARDS_SETTING.get(settings);
    final int numberOfReplicas = IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.get(settings);
    final int shardsToCreate = numberOfShards * (1 + numberOfReplicas);
    final Optional<String> shardLimit = checkShardLimit(shardsToCreate, state);
    if (shardLimit.isPresent()) {
        final ValidationException e = new ValidationException();
        e.addValidationError(shardLimit.get());
        throw e;
    }
}
Also used : ValidationException(org.opensearch.common.ValidationException)

Aggregations

ValidationException (org.opensearch.common.ValidationException)7 ClusterState (org.opensearch.cluster.ClusterState)3 Index (org.opensearch.index.Index)3 Arrays (java.util.Arrays)2 Optional (java.util.Optional)2 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)2 ClusterService (org.opensearch.cluster.service.ClusterService)2 Regex (org.opensearch.common.regex.Regex)2 Setting (org.opensearch.common.settings.Setting)2 Settings (org.opensearch.common.settings.Settings)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Set (java.util.Set)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1 ExceptionsHelper (org.opensearch.ExceptionsHelper)1