Search in sources :

Example 1 with IndexAbstraction

use of org.opensearch.cluster.metadata.IndexAbstraction in project OpenSearch by opensearch-project.

the class TransportBulkAction method prohibitAppendWritesInBackingIndices.

static void prohibitAppendWritesInBackingIndices(DocWriteRequest<?> writeRequest, Metadata metadata) {
    IndexAbstraction indexAbstraction = metadata.getIndicesLookup().get(writeRequest.index());
    if (indexAbstraction == null) {
        return;
    }
    if (indexAbstraction.getType() != IndexAbstraction.Type.CONCRETE_INDEX) {
        return;
    }
    if (indexAbstraction.getParentDataStream() == null) {
        return;
    }
    DataStream dataStream = indexAbstraction.getParentDataStream().getDataStream();
    // At this point with write op is targeting a backing index of a data stream directly,
    // so checking if write op is append-only and if so fail.
    // (Updates and deletes are allowed to target a backing index)
    DocWriteRequest.OpType opType = writeRequest.opType();
    // that an engine level change is needed and for now this check is sufficient.)
    if (opType == DocWriteRequest.OpType.CREATE) {
        throw new IllegalArgumentException("index request with op_type=create targeting backing indices is disallowed, " + "target corresponding data stream [" + dataStream.getName() + "] instead");
    }
    if (opType == DocWriteRequest.OpType.INDEX && writeRequest.ifPrimaryTerm() == UNASSIGNED_PRIMARY_TERM && writeRequest.ifSeqNo() == UNASSIGNED_SEQ_NO) {
        throw new IllegalArgumentException("index request with op_type=index and no if_primary_term and if_seq_no set " + "targeting backing indices is disallowed, target corresponding data stream [" + dataStream.getName() + "] instead");
    }
}
Also used : IndexAbstraction(org.opensearch.cluster.metadata.IndexAbstraction) DataStream(org.opensearch.cluster.metadata.DataStream) DocWriteRequest(org.opensearch.action.DocWriteRequest)

Example 2 with IndexAbstraction

use of org.opensearch.cluster.metadata.IndexAbstraction in project OpenSearch by opensearch-project.

the class MetadataRolloverService method rolloverClusterState.

public RolloverResult rolloverClusterState(ClusterState currentState, String rolloverTarget, String newIndexName, CreateIndexRequest createIndexRequest, List<Condition<?>> metConditions, boolean silent, boolean onlyValidate) throws Exception {
    validate(currentState.metadata(), rolloverTarget, newIndexName, createIndexRequest);
    final IndexAbstraction indexAbstraction = currentState.metadata().getIndicesLookup().get(rolloverTarget);
    switch(indexAbstraction.getType()) {
        case ALIAS:
            return rolloverAlias(currentState, (IndexAbstraction.Alias) indexAbstraction, rolloverTarget, newIndexName, createIndexRequest, metConditions, silent, onlyValidate);
        case DATA_STREAM:
            return rolloverDataStream(currentState, (IndexAbstraction.DataStream) indexAbstraction, rolloverTarget, createIndexRequest, metConditions, silent, onlyValidate);
        default:
            // the validate method above prevents this case
            throw new IllegalStateException("unable to roll over type [" + indexAbstraction.getType().getDisplayName() + "]");
    }
}
Also used : IndexAbstraction(org.opensearch.cluster.metadata.IndexAbstraction)

Example 3 with IndexAbstraction

use of org.opensearch.cluster.metadata.IndexAbstraction in project OpenSearch by opensearch-project.

the class PutMappingRequestTests method testResolveIndicesWithWriteIndexOnlyAndDataStreamAndIndex.

public void testResolveIndicesWithWriteIndexOnlyAndDataStreamAndIndex() {
    String[] dataStreamNames = { "foo", "bar", "baz" };
    List<Tuple<String, Integer>> dsMetadata = org.opensearch.common.collect.List.of(tuple(dataStreamNames[0], randomIntBetween(1, 3)), tuple(dataStreamNames[1], randomIntBetween(1, 3)), tuple(dataStreamNames[2], randomIntBetween(1, 3)));
    ClusterState cs = DeleteDataStreamRequestTests.getClusterStateWithDataStreams(dsMetadata, org.opensearch.common.collect.List.of("index1", "index2", "index3"));
    cs = addAliases(cs, org.opensearch.common.collect.List.of(tuple("alias1", org.opensearch.common.collect.List.of(tuple("index1", false), tuple("index2", true))), tuple("alias2", org.opensearch.common.collect.List.of(tuple("index2", false), tuple("index3", true)))));
    PutMappingRequest request = new PutMappingRequest().indices("foo", "index3").writeIndexOnly(true);
    Index[] indices = TransportPutMappingAction.resolveIndices(cs, request, new IndexNameExpressionResolver(new ThreadContext(Settings.EMPTY)));
    List<String> indexNames = Arrays.stream(indices).map(Index::getName).collect(Collectors.toList());
    IndexAbstraction expectedDs = cs.metadata().getIndicesLookup().get("foo");
    List<String> expectedIndices = expectedDs.getIndices().stream().map(im -> im.getIndex().getName()).collect(Collectors.toList());
    expectedIndices.addAll(org.opensearch.common.collect.List.of("index1", "index2", "index3"));
    // should resolve the data stream and each alias to _all_ their respective indices
    assertThat(indexNames, containsInAnyOrder(expectedDs.getWriteIndex().getIndex().getName(), "index3"));
}
Also used : Arrays(java.util.Arrays) IndexAbstraction(org.opensearch.cluster.metadata.IndexAbstraction) Metadata(org.opensearch.cluster.metadata.Metadata) BytesReference(org.opensearch.common.bytes.BytesReference) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) ActionRequestValidationException(org.opensearch.action.ActionRequestValidationException) Strings(org.opensearch.common.Strings) XContentParser(org.opensearch.common.xcontent.XContentParser) ClusterState(org.opensearch.cluster.ClusterState) RandomCreateIndexGenerator(org.opensearch.index.RandomCreateIndexGenerator) DeleteDataStreamRequestTests(org.opensearch.action.admin.indices.datastream.DeleteDataStreamRequestTests) Index(org.opensearch.index.Index) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) Tuple.tuple(org.opensearch.common.collect.Tuple.tuple) EMPTY_PARAMS(org.opensearch.common.xcontent.ToXContent.EMPTY_PARAMS) Collectors(java.util.stream.Collectors) Tuple(org.opensearch.common.collect.Tuple) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) List(java.util.List) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) JsonXContent(org.opensearch.common.xcontent.json.JsonXContent) XContentType(org.opensearch.common.xcontent.XContentType) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) ClusterState(org.opensearch.cluster.ClusterState) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) Index(org.opensearch.index.Index) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexAbstraction(org.opensearch.cluster.metadata.IndexAbstraction) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) Tuple(org.opensearch.common.collect.Tuple)

Example 4 with IndexAbstraction

use of org.opensearch.cluster.metadata.IndexAbstraction in project OpenSearch by opensearch-project.

the class TransportBulkActionTests method testOnlySystem.

public void testOnlySystem() {
    SortedMap<String, IndexAbstraction> indicesLookup = new TreeMap<>();
    Settings settings = Settings.builder().put("index.version.created", Version.CURRENT).build();
    indicesLookup.put(".foo", new Index(IndexMetadata.builder(".foo").settings(settings).system(true).numberOfShards(1).numberOfReplicas(0).build()));
    indicesLookup.put(".bar", new Index(IndexMetadata.builder(".bar").settings(settings).system(true).numberOfShards(1).numberOfReplicas(0).build()));
    SystemIndices systemIndices = new SystemIndices(singletonMap("plugin", singletonList(new SystemIndexDescriptor(".test", ""))));
    List<String> onlySystem = Arrays.asList(".foo", ".bar");
    assertTrue(bulkAction.isOnlySystem(buildBulkRequest(onlySystem), indicesLookup, systemIndices));
    onlySystem = Arrays.asList(".foo", ".bar", ".test");
    assertTrue(bulkAction.isOnlySystem(buildBulkRequest(onlySystem), indicesLookup, systemIndices));
    List<String> nonSystem = Arrays.asList("foo", "bar");
    assertFalse(bulkAction.isOnlySystem(buildBulkRequest(nonSystem), indicesLookup, systemIndices));
    List<String> mixed = Arrays.asList(".foo", ".test", "other");
    assertFalse(bulkAction.isOnlySystem(buildBulkRequest(mixed), indicesLookup, systemIndices));
}
Also used : IndexAbstraction(org.opensearch.cluster.metadata.IndexAbstraction) SystemIndexDescriptor(org.opensearch.indices.SystemIndexDescriptor) Index(org.opensearch.cluster.metadata.IndexAbstraction.Index) AutoCreateIndex(org.opensearch.action.support.AutoCreateIndex) TreeMap(java.util.TreeMap) SystemIndices(org.opensearch.indices.SystemIndices) Settings(org.opensearch.common.settings.Settings)

Example 5 with IndexAbstraction

use of org.opensearch.cluster.metadata.IndexAbstraction in project OpenSearch by opensearch-project.

the class TransportIndicesAliasesAction method masterOperation.

@Override
protected void masterOperation(final IndicesAliasesRequest request, final ClusterState state, final ActionListener<AcknowledgedResponse> listener) {
    // Expand the indices names
    List<IndicesAliasesRequest.AliasActions> actions = request.aliasActions();
    List<AliasAction> finalActions = new ArrayList<>();
    // Resolve all the AliasActions into AliasAction instances and gather all the aliases
    Set<String> aliases = new HashSet<>();
    for (IndicesAliasesRequest.AliasActions action : actions) {
        final Index[] concreteIndices = indexNameExpressionResolver.concreteIndices(state, request.indicesOptions(), false, action.indices());
        for (Index concreteIndex : concreteIndices) {
            IndexAbstraction indexAbstraction = state.metadata().getIndicesLookup().get(concreteIndex.getName());
            assert indexAbstraction != null : "invalid cluster metadata. index [" + concreteIndex.getName() + "] was not found";
            if (indexAbstraction.getParentDataStream() != null) {
                throw new IllegalArgumentException("The provided expressions [" + String.join(",", action.indices()) + "] match a backing index belonging to data stream [" + indexAbstraction.getParentDataStream().getName() + "]. Data streams and their backing indices don't support aliases.");
            }
        }
        final Optional<Exception> maybeException = requestValidators.validateRequest(request, state, concreteIndices);
        if (maybeException.isPresent()) {
            listener.onFailure(maybeException.get());
            return;
        }
        Collections.addAll(aliases, action.getOriginalAliases());
        for (final Index index : concreteIndices) {
            switch(action.actionType()) {
                case ADD:
                    for (String alias : concreteAliases(action, state.metadata(), index.getName())) {
                        finalActions.add(new AliasAction.Add(index.getName(), alias, action.filter(), action.indexRouting(), action.searchRouting(), action.writeIndex(), action.isHidden()));
                    }
                    break;
                case REMOVE:
                    for (String alias : concreteAliases(action, state.metadata(), index.getName())) {
                        finalActions.add(new AliasAction.Remove(index.getName(), alias, action.mustExist()));
                    }
                    break;
                case REMOVE_INDEX:
                    finalActions.add(new AliasAction.RemoveIndex(index.getName()));
                    break;
                default:
                    throw new IllegalArgumentException("Unsupported action [" + action.actionType() + "]");
            }
        }
    }
    if (finalActions.isEmpty() && false == actions.isEmpty()) {
        throw new AliasesNotFoundException(aliases.toArray(new String[aliases.size()]));
    }
    request.aliasActions().clear();
    IndicesAliasesClusterStateUpdateRequest updateRequest = new IndicesAliasesClusterStateUpdateRequest(unmodifiableList(finalActions)).ackTimeout(request.timeout()).masterNodeTimeout(request.masterNodeTimeout());
    indexAliasesService.indicesAliases(updateRequest, new ActionListener<ClusterStateUpdateResponse>() {

        @Override
        public void onResponse(ClusterStateUpdateResponse response) {
            listener.onResponse(new AcknowledgedResponse(response.isAcknowledged()));
        }

        @Override
        public void onFailure(Exception t) {
            logger.debug("failed to perform aliases", t);
            listener.onFailure(t);
        }
    });
}
Also used : ArrayList(java.util.ArrayList) Index(org.opensearch.index.Index) ClusterStateUpdateResponse(org.opensearch.cluster.ack.ClusterStateUpdateResponse) HashSet(java.util.HashSet) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) AliasesNotFoundException(org.opensearch.rest.action.admin.indices.AliasesNotFoundException) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) IOException(java.io.IOException) IndexAbstraction(org.opensearch.cluster.metadata.IndexAbstraction) AliasAction(org.opensearch.cluster.metadata.AliasAction) AliasesNotFoundException(org.opensearch.rest.action.admin.indices.AliasesNotFoundException)

Aggregations

IndexAbstraction (org.opensearch.cluster.metadata.IndexAbstraction)11 Matchers.containsString (org.hamcrest.Matchers.containsString)5 ClusterState (org.opensearch.cluster.ClusterState)5 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)5 IndexNameExpressionResolver (org.opensearch.cluster.metadata.IndexNameExpressionResolver)5 Settings (org.opensearch.common.settings.Settings)5 Index (org.opensearch.index.Index)5 AliasMetadata (org.opensearch.cluster.metadata.AliasMetadata)4 Metadata (org.opensearch.cluster.metadata.Metadata)4 SystemIndices (org.opensearch.indices.SystemIndices)4 IOException (java.io.IOException)3 List (java.util.List)3 IndexTemplateMetadata (org.opensearch.cluster.metadata.IndexTemplateMetadata)3 Tuple (org.opensearch.common.collect.Tuple)3 ThreadContext (org.opensearch.common.util.concurrent.ThreadContext)3 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 TreeMap (java.util.TreeMap)2 Collectors (java.util.stream.Collectors)2 Matchers.containsInAnyOrder (org.hamcrest.Matchers.containsInAnyOrder)2