Search in sources :

Example 86 with Tuple

use of org.opensearch.common.collect.Tuple in project OpenSearch by opensearch-project.

the class SettingsUpdater method partitionKnownAndValidSettings.

/**
 * Partitions the settings into those that are known and valid versus those that are unknown or invalid. The resulting tuple contains
 * the known and valid settings in the first component and the unknown or invalid settings in the second component. Note that archived
 * settings contained in the settings to partition are included in the first component.
 *
 * @param settings     the settings to partition
 * @param settingsType a string to identify the settings (for logging)
 * @param logger       a logger to sending warnings to
 * @return the partitioned settings
 */
private Tuple<Settings, Settings> partitionKnownAndValidSettings(final Settings settings, final String settingsType, final Logger logger) {
    final Settings existingArchivedSettings = settings.filter(k -> k.startsWith(ARCHIVED_SETTINGS_PREFIX));
    final Settings settingsExcludingExistingArchivedSettings = settings.filter(k -> k.startsWith(ARCHIVED_SETTINGS_PREFIX) == false);
    final Settings settingsWithUnknownOrInvalidArchived = clusterSettings.archiveUnknownOrInvalidSettings(settingsExcludingExistingArchivedSettings, e -> logUnknownSetting(settingsType, e, logger), (e, ex) -> logInvalidSetting(settingsType, e, ex, logger));
    return Tuple.tuple(Settings.builder().put(settingsWithUnknownOrInvalidArchived.filter(k -> k.startsWith(ARCHIVED_SETTINGS_PREFIX) == false)).put(existingArchivedSettings).build(), settingsWithUnknownOrInvalidArchived.filter(k -> k.startsWith(ARCHIVED_SETTINGS_PREFIX)));
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) Logger(org.apache.logging.log4j.Logger) Metadata(org.opensearch.cluster.metadata.Metadata) Supplier(org.apache.logging.log4j.util.Supplier) ARCHIVED_SETTINGS_PREFIX(org.opensearch.common.settings.AbstractScopedSettings.ARCHIVED_SETTINGS_PREFIX) Map(java.util.Map) Settings(org.opensearch.common.settings.Settings) ClusterState.builder(org.opensearch.cluster.ClusterState.builder) ClusterSettings(org.opensearch.common.settings.ClusterSettings) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ClusterBlocks(org.opensearch.cluster.block.ClusterBlocks) Tuple(org.opensearch.common.collect.Tuple) Settings(org.opensearch.common.settings.Settings) ClusterSettings(org.opensearch.common.settings.ClusterSettings)

Example 87 with Tuple

use of org.opensearch.common.collect.Tuple in project OpenSearch by opensearch-project.

the class MetadataIndexStateService method addIndexBlock.

/**
 * Adds an index block based on the given request, and notifies the listener upon completion.
 * Adding blocks is done in three steps:
 * - First, a temporary UUID-based block is added to the index
 *   (see {@link #addIndexBlock(Index[], ClusterState, APIBlock)}.
 * - Second, shards are checked to have properly applied the UUID-based block.
 *   (see {@link WaitForBlocksApplied}).
 * - Third, the temporary UUID-based block is turned into a full block
 *   (see {@link #finalizeBlock(ClusterState, Map, Map, APIBlock)}.
 * Using this three-step process ensures non-interference by other operations in case where
 * we notify successful completion here.
 */
public void addIndexBlock(AddIndexBlockClusterStateUpdateRequest request, ActionListener<AddIndexBlockResponse> listener) {
    final Index[] concreteIndices = request.indices();
    if (concreteIndices == null || concreteIndices.length == 0) {
        throw new IllegalArgumentException("Index name is required");
    }
    List<String> writeIndices = new ArrayList<>();
    SortedMap<String, IndexAbstraction> lookup = clusterService.state().metadata().getIndicesLookup();
    for (Index index : concreteIndices) {
        IndexAbstraction ia = lookup.get(index.getName());
        if (ia != null && ia.getParentDataStream() != null && ia.getParentDataStream().getWriteIndex().getIndex().equals(index)) {
            writeIndices.add(index.getName());
        }
    }
    if (writeIndices.size() > 0) {
        throw new IllegalArgumentException("cannot add a block to the following data stream write indices [" + Strings.collectionToCommaDelimitedString(writeIndices) + "]");
    }
    clusterService.submitStateUpdateTask("add-index-block-[" + request.getBlock().name + "]-" + Arrays.toString(concreteIndices), new ClusterStateUpdateTask(Priority.URGENT) {

        private Map<Index, ClusterBlock> blockedIndices;

        @Override
        public ClusterState execute(final ClusterState currentState) {
            final Tuple<ClusterState, Map<Index, ClusterBlock>> tup = addIndexBlock(concreteIndices, currentState, request.getBlock());
            blockedIndices = tup.v2();
            return tup.v1();
        }

        @Override
        public void clusterStateProcessed(final String source, final ClusterState oldState, final ClusterState newState) {
            if (oldState == newState) {
                assert blockedIndices.isEmpty() : "List of blocked indices is not empty but cluster state wasn't changed";
                listener.onResponse(new AddIndexBlockResponse(true, false, Collections.emptyList()));
            } else {
                assert blockedIndices.isEmpty() == false : "List of blocked indices is empty but cluster state was changed";
                threadPool.executor(ThreadPool.Names.MANAGEMENT).execute(new WaitForBlocksApplied(blockedIndices, request, ActionListener.wrap(verifyResults -> clusterService.submitStateUpdateTask("finalize-index-block-[" + request.getBlock().name + "]-[" + blockedIndices.keySet().stream().map(Index::getName).collect(Collectors.joining(", ")) + "]", new ClusterStateUpdateTask(Priority.URGENT) {

                    private final List<AddBlockResult> indices = new ArrayList<>();

                    @Override
                    public ClusterState execute(final ClusterState currentState) throws Exception {
                        Tuple<ClusterState, Collection<AddBlockResult>> addBlockResult = finalizeBlock(currentState, blockedIndices, verifyResults, request.getBlock());
                        assert verifyResults.size() == addBlockResult.v2().size();
                        indices.addAll(addBlockResult.v2());
                        return addBlockResult.v1();
                    }

                    @Override
                    public void onFailure(final String source, final Exception e) {
                        listener.onFailure(e);
                    }

                    @Override
                    public void clusterStateProcessed(final String source, final ClusterState oldState, final ClusterState newState) {
                        final boolean acknowledged = indices.stream().noneMatch(AddBlockResult::hasFailures);
                        listener.onResponse(new AddIndexBlockResponse(acknowledged, acknowledged, indices));
                    }
                }), listener::onFailure)));
            }
        }

        @Override
        public void onFailure(final String source, final Exception e) {
            listener.onFailure(e);
        }

        @Override
        public TimeValue timeout() {
            return request.masterNodeTimeout();
        }
    });
}
Also used : Arrays(java.util.Arrays) CountDown(org.opensearch.common.util.concurrent.CountDown) NotifyOnceListener(org.opensearch.action.NotifyOnceListener) AllocationService(org.opensearch.cluster.routing.allocation.AllocationService) Version(org.opensearch.Version) OpenSearchException(org.opensearch.OpenSearchException) SnapshotsService(org.opensearch.snapshots.SnapshotsService) Strings(org.opensearch.common.Strings) CloseIndexResponse(org.opensearch.action.admin.indices.close.CloseIndexResponse) ConcurrentCollections(org.opensearch.common.util.concurrent.ConcurrentCollections) ClusterBlock(org.opensearch.cluster.block.ClusterBlock) Collections.singleton(java.util.Collections.singleton) TransportVerifyShardIndexBlockAction(org.opensearch.action.admin.indices.readonly.TransportVerifyShardIndexBlockAction) Map(java.util.Map) Inject(org.opensearch.common.inject.Inject) AddBlockShardResult(org.opensearch.action.admin.indices.readonly.AddIndexBlockResponse.AddBlockShardResult) ActionListener(org.opensearch.action.ActionListener) EnumSet(java.util.EnumSet) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) TimeValue(org.opensearch.common.unit.TimeValue) ImmutableOpenIntMap(org.opensearch.common.collect.ImmutableOpenIntMap) Index(org.opensearch.index.Index) Collection(java.util.Collection) IndicesService(org.opensearch.indices.IndicesService) SnapshotInProgressException(org.opensearch.snapshots.SnapshotInProgressException) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) RestStatus(org.opensearch.rest.RestStatus) ShardResult(org.opensearch.action.admin.indices.close.CloseIndexResponse.ShardResult) Collectors(java.util.stream.Collectors) Tuple(org.opensearch.common.collect.Tuple) List(java.util.List) Logger(org.apache.logging.log4j.Logger) ClusterStateUpdateTask(org.opensearch.cluster.ClusterStateUpdateTask) IndexResult(org.opensearch.action.admin.indices.close.CloseIndexResponse.IndexResult) ReplicationResponse(org.opensearch.action.support.replication.ReplicationResponse) AddIndexBlockClusterStateUpdateRequest(org.opensearch.action.admin.indices.readonly.AddIndexBlockClusterStateUpdateRequest) ClusterStateUpdateResponse(org.opensearch.cluster.ack.ClusterStateUpdateResponse) SortedMap(java.util.SortedMap) IntObjectCursor(com.carrotsearch.hppc.cursors.IntObjectCursor) APIBlock(org.opensearch.cluster.metadata.IndexMetadata.APIBlock) ActionRunnable(org.opensearch.action.ActionRunnable) ThreadPool(org.opensearch.threadpool.ThreadPool) Priority(org.opensearch.common.Priority) HashMap(java.util.HashMap) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) TransportVerifyShardBeforeCloseAction(org.opensearch.action.admin.indices.close.TransportVerifyShardBeforeCloseAction) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) CloseIndexClusterStateUpdateRequest(org.opensearch.action.admin.indices.close.CloseIndexClusterStateUpdateRequest) ClusterState(org.opensearch.cluster.ClusterState) AddIndexBlockResponse(org.opensearch.action.admin.indices.readonly.AddIndexBlockResponse) LegacyESVersion(org.opensearch.LegacyESVersion) RestoreService(org.opensearch.snapshots.RestoreService) AckedClusterStateUpdateTask(org.opensearch.cluster.AckedClusterStateUpdateTask) UUIDs(org.opensearch.common.UUIDs) ClusterBlocks(org.opensearch.cluster.block.ClusterBlocks) Setting(org.opensearch.common.settings.Setting) ShardLimitValidator(org.opensearch.indices.ShardLimitValidator) ClusterBlockLevel(org.opensearch.cluster.block.ClusterBlockLevel) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) TaskId(org.opensearch.tasks.TaskId) ShardId(org.opensearch.index.shard.ShardId) Consumer(java.util.function.Consumer) AtomicArray(org.opensearch.common.util.concurrent.AtomicArray) AddBlockResult(org.opensearch.action.admin.indices.readonly.AddIndexBlockResponse.AddBlockResult) ActiveShardsObserver(org.opensearch.action.support.ActiveShardsObserver) ClusterService(org.opensearch.cluster.service.ClusterService) RoutingTable(org.opensearch.cluster.routing.RoutingTable) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap) OpenIndexClusterStateUpdateResponse(org.opensearch.cluster.ack.OpenIndexClusterStateUpdateResponse) OpenIndexClusterStateUpdateRequest(org.opensearch.action.admin.indices.open.OpenIndexClusterStateUpdateRequest) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) ClusterState(org.opensearch.cluster.ClusterState) ArrayList(java.util.ArrayList) ClusterStateUpdateTask(org.opensearch.cluster.ClusterStateUpdateTask) AckedClusterStateUpdateTask(org.opensearch.cluster.AckedClusterStateUpdateTask) Index(org.opensearch.index.Index) AddBlockResult(org.opensearch.action.admin.indices.readonly.AddIndexBlockResponse.AddBlockResult) OpenSearchException(org.opensearch.OpenSearchException) SnapshotInProgressException(org.opensearch.snapshots.SnapshotInProgressException) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) ClusterBlock(org.opensearch.cluster.block.ClusterBlock) AddIndexBlockResponse(org.opensearch.action.admin.indices.readonly.AddIndexBlockResponse) Tuple(org.opensearch.common.collect.Tuple) TimeValue(org.opensearch.common.unit.TimeValue)

Example 88 with Tuple

use of org.opensearch.common.collect.Tuple in project OpenSearch by opensearch-project.

the class TemplateUpgradeService method clusterChanged.

@Override
public void clusterChanged(ClusterChangedEvent event) {
    ClusterState state = event.state();
    if (state.nodes().isLocalNodeElectedMaster() == false) {
        return;
    }
    if (state.blocks().hasGlobalBlock(GatewayService.STATE_NOT_RECOVERED_BLOCK)) {
        // while they actually do exist
        return;
    }
    if (upgradesInProgress.get() > 0) {
        // we are already running some upgrades - skip this cluster state update
        return;
    }
    ImmutableOpenMap<String, IndexTemplateMetadata> templates = state.getMetadata().getTemplates();
    if (templates == lastTemplateMetadata) {
        // if there were no changes
        return;
    }
    lastTemplateMetadata = templates;
    Optional<Tuple<Map<String, BytesReference>, Set<String>>> changes = calculateTemplateChanges(templates);
    if (changes.isPresent()) {
        if (upgradesInProgress.compareAndSet(0, changes.get().v1().size() + changes.get().v2().size() + 1)) {
            logger.info("Starting template upgrade to version {}, {} templates will be updated and {} will be removed", Version.CURRENT, changes.get().v1().size(), changes.get().v2().size());
            assert threadPool.getThreadContext().isSystemContext();
            threadPool.generic().execute(() -> upgradeTemplates(changes.get().v1(), changes.get().v2()));
        }
    }
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) ClusterState(org.opensearch.cluster.ClusterState) Tuple(org.opensearch.common.collect.Tuple)

Example 89 with Tuple

use of org.opensearch.common.collect.Tuple in project OpenSearch by opensearch-project.

the class OpenSearchExceptionTests method randomExceptions.

public static Tuple<Throwable, OpenSearchException> randomExceptions() {
    Throwable actual;
    OpenSearchException expected;
    int type = randomIntBetween(0, 5);
    switch(type) {
        case 0:
            actual = new ClusterBlockException(singleton(NoMasterBlockService.NO_MASTER_BLOCK_WRITES));
            expected = new OpenSearchException("OpenSearch exception [type=cluster_block_exception, " + "reason=blocked by: [SERVICE_UNAVAILABLE/2/no master];]");
            break;
        case // Simple opensearch exception with headers (other metadata of type number are not parsed)
        1:
            actual = new ParsingException(3, 2, "Unknown identifier", null);
            expected = new OpenSearchException("OpenSearch exception [type=parsing_exception, reason=Unknown identifier]");
            break;
        case 2:
            actual = new SearchParseException(SHARD_TARGET, "Parse failure", new XContentLocation(12, 98));
            expected = new OpenSearchException("OpenSearch exception [type=search_parse_exception, reason=Parse failure]");
            break;
        case 3:
            actual = new IllegalArgumentException("Closed resource", new RuntimeException("Resource"));
            expected = new OpenSearchException("OpenSearch exception [type=illegal_argument_exception, reason=Closed resource]", new OpenSearchException("OpenSearch exception [type=runtime_exception, reason=Resource]"));
            break;
        case 4:
            actual = new SearchPhaseExecutionException("search", "all shards failed", new ShardSearchFailure[] { new ShardSearchFailure(new ParsingException(1, 2, "foobar", null), new SearchShardTarget("node_1", new ShardId("foo", "_na_", 1), null, OriginalIndices.NONE)) });
            expected = new OpenSearchException("OpenSearch exception [type=search_phase_execution_exception, " + "reason=all shards failed]");
            expected.addMetadata("opensearch.phase", "search");
            break;
        case 5:
            actual = new OpenSearchException("Parsing failed", new ParsingException(9, 42, "Wrong state", new NullPointerException("Unexpected null value")));
            OpenSearchException expectedCause = new OpenSearchException("OpenSearch exception [type=parsing_exception, " + "reason=Wrong state]", new OpenSearchException("OpenSearch exception [type=null_pointer_exception, " + "reason=Unexpected null value]"));
            expected = new OpenSearchException("OpenSearch exception [type=exception, reason=Parsing failed]", expectedCause);
            break;
        default:
            throw new UnsupportedOperationException("No randomized exceptions generated for type [" + type + "]");
    }
    if (actual instanceof OpenSearchException) {
        OpenSearchException actualException = (OpenSearchException) actual;
        if (randomBoolean()) {
            int nbHeaders = randomIntBetween(1, 5);
            Map<String, List<String>> randomHeaders = new HashMap<>(nbHeaders);
            for (int i = 0; i < nbHeaders; i++) {
                List<String> values = new ArrayList<>();
                int nbValues = randomIntBetween(1, 3);
                for (int j = 0; j < nbValues; j++) {
                    values.add(frequently() ? randomAlphaOfLength(5) : "");
                }
                randomHeaders.put("header_" + i, values);
            }
            for (Map.Entry<String, List<String>> entry : randomHeaders.entrySet()) {
                actualException.addHeader(entry.getKey(), entry.getValue());
                expected.addHeader(entry.getKey(), entry.getValue());
            }
            if (rarely()) {
                // Empty or null headers are not printed out by the toXContent method
                actualException.addHeader("ignored", randomBoolean() ? emptyList() : null);
            }
        }
        if (randomBoolean()) {
            int nbMetadata = randomIntBetween(1, 5);
            Map<String, List<String>> randomMetadata = new HashMap<>(nbMetadata);
            for (int i = 0; i < nbMetadata; i++) {
                List<String> values = new ArrayList<>();
                int nbValues = randomIntBetween(1, 3);
                for (int j = 0; j < nbValues; j++) {
                    values.add(frequently() ? randomAlphaOfLength(5) : "");
                }
                randomMetadata.put("opensearch.metadata_" + i, values);
            }
            for (Map.Entry<String, List<String>> entry : randomMetadata.entrySet()) {
                actualException.addMetadata(entry.getKey(), entry.getValue());
                expected.addMetadata(entry.getKey(), entry.getValue());
            }
            if (rarely()) {
                // Empty or null metadata are not printed out by the toXContent method
                actualException.addMetadata("opensearch.ignored", randomBoolean() ? emptyList() : null);
            }
        }
        if (randomBoolean()) {
            int nbResources = randomIntBetween(1, 5);
            for (int i = 0; i < nbResources; i++) {
                String resourceType = "type_" + i;
                String[] resourceIds = new String[randomIntBetween(1, 3)];
                for (int j = 0; j < resourceIds.length; j++) {
                    resourceIds[j] = frequently() ? randomAlphaOfLength(5) : "";
                }
                actualException.setResources(resourceType, resourceIds);
                expected.setResources(resourceType, resourceIds);
            }
        }
    }
    return new Tuple<>(actual, expected);
}
Also used : HashMap(java.util.HashMap) SearchPhaseExecutionException(org.opensearch.action.search.SearchPhaseExecutionException) ArrayList(java.util.ArrayList) ShardId(org.opensearch.index.shard.ShardId) ParsingException(org.opensearch.common.ParsingException) Collections.singletonList(java.util.Collections.singletonList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) ArrayList(java.util.ArrayList) ShardSearchFailure(org.opensearch.action.search.ShardSearchFailure) XContentLocation(org.opensearch.common.xcontent.XContentLocation) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) SearchParseException(org.opensearch.search.SearchParseException) SearchShardTarget(org.opensearch.search.SearchShardTarget) Map(java.util.Map) HashMap(java.util.HashMap) Tuple(org.opensearch.common.collect.Tuple)

Example 90 with Tuple

use of org.opensearch.common.collect.Tuple in project OpenSearch by opensearch-project.

the class IndexResponseTests method doFromXContentTestWithRandomFields.

private void doFromXContentTestWithRandomFields(boolean addRandomFields) throws IOException {
    final Tuple<IndexResponse, IndexResponse> tuple = randomIndexResponse();
    IndexResponse indexResponse = tuple.v1();
    IndexResponse expectedIndexResponse = tuple.v2();
    boolean humanReadable = randomBoolean();
    XContentType xContentType = randomFrom(XContentType.values());
    BytesReference originalBytes = toShuffledXContent(indexResponse, xContentType, ToXContent.EMPTY_PARAMS, humanReadable);
    BytesReference mutated;
    if (addRandomFields) {
        // The ShardInfo.Failure's exception is rendered out in a "reason" object. We shouldn't add anything random there
        // because exception rendering and parsing are very permissive: any extra object or field would be rendered as
        // a exception custom metadata and be parsed back as a custom header, making it impossible to compare the results
        // in this test.
        Predicate<String> excludeFilter = path -> path.contains("reason");
        mutated = insertRandomFields(xContentType, originalBytes, excludeFilter, random());
    } else {
        mutated = originalBytes;
    }
    IndexResponse parsedIndexResponse;
    try (XContentParser parser = createParser(xContentType.xContent(), mutated)) {
        parsedIndexResponse = IndexResponse.fromXContent(parser);
        assertNull(parser.nextToken());
    }
    // We can't use equals() to compare the original and the parsed index response
    // because the random index response can contain shard failures with exceptions,
    // and those exceptions are not parsed back with the same types.
    assertDocWriteResponse(expectedIndexResponse, parsedIndexResponse);
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) SequenceNumbers(org.opensearch.index.seqno.SequenceNumbers) BytesReference(org.opensearch.common.bytes.BytesReference) XContentTestUtils.insertRandomFields(org.opensearch.test.XContentTestUtils.insertRandomFields) Predicate(java.util.function.Predicate) ToXContent(org.opensearch.common.xcontent.ToXContent) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) IOException(java.io.IOException) ReplicationResponseTests.assertShardInfo(org.opensearch.action.support.replication.ReplicationResponseTests.assertShardInfo) INDEX_UUID_NA_VALUE(org.opensearch.cluster.metadata.IndexMetadata.INDEX_UUID_NA_VALUE) Strings(org.opensearch.common.Strings) Tuple(org.opensearch.common.collect.Tuple) XContentParser(org.opensearch.common.xcontent.XContentParser) ShardId(org.opensearch.index.shard.ShardId) ReplicationResponse(org.opensearch.action.support.replication.ReplicationResponse) DocWriteResponse(org.opensearch.action.DocWriteResponse) XContentType(org.opensearch.common.xcontent.XContentType) RandomObjects(org.opensearch.test.RandomObjects) XContentType(org.opensearch.common.xcontent.XContentType) XContentParser(org.opensearch.common.xcontent.XContentParser)

Aggregations

Tuple (org.opensearch.common.collect.Tuple)151 ArrayList (java.util.ArrayList)65 List (java.util.List)49 IOException (java.io.IOException)45 Collections (java.util.Collections)44 HashMap (java.util.HashMap)40 Map (java.util.Map)40 Settings (org.opensearch.common.settings.Settings)38 ClusterState (org.opensearch.cluster.ClusterState)34 HashSet (java.util.HashSet)28 ShardId (org.opensearch.index.shard.ShardId)28 Arrays (java.util.Arrays)27 Collectors (java.util.stream.Collectors)26 Set (java.util.Set)25 Index (org.opensearch.index.Index)25 BytesReference (org.opensearch.common.bytes.BytesReference)24 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)24 CountDownLatch (java.util.concurrent.CountDownLatch)22 Version (org.opensearch.Version)21 Strings (org.opensearch.common.Strings)21