Search in sources :

Example 1 with IndexGraveyard

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

the class DanglingIndicesState method findNewAndAddDanglingIndices.

/**
 * Finds (@{link #findNewAndAddDanglingIndices}) and adds the new dangling indices
 * to the currently tracked dangling indices.
 */
void findNewAndAddDanglingIndices(final Metadata metadata) {
    final IndexGraveyard graveyard = metadata.indexGraveyard();
    // If a tombstone is created for a dangling index, we need to make sure that the
    // index is no longer considered dangling.
    danglingIndices.keySet().removeIf(graveyard::containsIndex);
    danglingIndices.putAll(findNewDanglingIndices(danglingIndices, metadata));
}
Also used : IndexGraveyard(org.opensearch.cluster.metadata.IndexGraveyard)

Example 2 with IndexGraveyard

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

the class ClusterChangedEvent method indicesDeletedFromClusterState.

// Get the deleted indices by comparing the index metadatas in the previous and new cluster states.
// If an index exists in the previous cluster state, but not in the new cluster state, it must have been deleted.
private List<Index> indicesDeletedFromClusterState() {
    // https://github.com/elastic/elasticsearch/issues/11665
    if (metadataChanged() == false || isNewCluster()) {
        return Collections.emptyList();
    }
    Set<Index> deleted = null;
    final Metadata previousMetadata = previousState.metadata();
    final Metadata currentMetadata = state.metadata();
    for (ObjectCursor<IndexMetadata> cursor : previousMetadata.indices().values()) {
        IndexMetadata index = cursor.value;
        IndexMetadata current = currentMetadata.index(index.getIndex());
        if (current == null) {
            if (deleted == null) {
                deleted = new HashSet<>();
            }
            deleted.add(index.getIndex());
        }
    }
    final IndexGraveyard currentGraveyard = currentMetadata.indexGraveyard();
    final IndexGraveyard previousGraveyard = previousMetadata.indexGraveyard();
    // each node should make sure to delete any related data.
    if (currentGraveyard != previousGraveyard) {
        final IndexGraveyardDiff indexGraveyardDiff = (IndexGraveyardDiff) currentGraveyard.diff(previousGraveyard);
        final List<IndexGraveyard.Tombstone> added = indexGraveyardDiff.getAdded();
        if (added.isEmpty() == false) {
            if (deleted == null) {
                deleted = new HashSet<>();
            }
            for (IndexGraveyard.Tombstone tombstone : added) {
                deleted.add(tombstone.getIndex());
            }
        }
    }
    return deleted == null ? Collections.<Index>emptyList() : new ArrayList<>(deleted);
}
Also used : IndexGraveyardDiff(org.opensearch.cluster.metadata.IndexGraveyard.IndexGraveyardDiff) IndexGraveyard(org.opensearch.cluster.metadata.IndexGraveyard) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) Index(org.opensearch.index.Index) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata)

Example 3 with IndexGraveyard

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

the class ClusterModule method getNamedWriteables.

public static List<Entry> getNamedWriteables() {
    List<Entry> entries = new ArrayList<>();
    // Cluster State
    registerClusterCustom(entries, SnapshotsInProgress.TYPE, SnapshotsInProgress::new, SnapshotsInProgress::readDiffFrom);
    registerClusterCustom(entries, RestoreInProgress.TYPE, RestoreInProgress::new, RestoreInProgress::readDiffFrom);
    registerClusterCustom(entries, SnapshotDeletionsInProgress.TYPE, SnapshotDeletionsInProgress::new, SnapshotDeletionsInProgress::readDiffFrom);
    registerClusterCustom(entries, RepositoryCleanupInProgress.TYPE, RepositoryCleanupInProgress::new, RepositoryCleanupInProgress::readDiffFrom);
    // Metadata
    registerMetadataCustom(entries, RepositoriesMetadata.TYPE, RepositoriesMetadata::new, RepositoriesMetadata::readDiffFrom);
    registerMetadataCustom(entries, IngestMetadata.TYPE, IngestMetadata::new, IngestMetadata::readDiffFrom);
    registerMetadataCustom(entries, ScriptMetadata.TYPE, ScriptMetadata::new, ScriptMetadata::readDiffFrom);
    registerMetadataCustom(entries, IndexGraveyard.TYPE, IndexGraveyard::new, IndexGraveyard::readDiffFrom);
    registerMetadataCustom(entries, PersistentTasksCustomMetadata.TYPE, PersistentTasksCustomMetadata::new, PersistentTasksCustomMetadata::readDiffFrom);
    registerMetadataCustom(entries, ComponentTemplateMetadata.TYPE, ComponentTemplateMetadata::new, ComponentTemplateMetadata::readDiffFrom);
    registerMetadataCustom(entries, ComposableIndexTemplateMetadata.TYPE, ComposableIndexTemplateMetadata::new, ComposableIndexTemplateMetadata::readDiffFrom);
    registerMetadataCustom(entries, DataStreamMetadata.TYPE, DataStreamMetadata::new, DataStreamMetadata::readDiffFrom);
    // Task Status (not Diffable)
    entries.add(new Entry(Task.Status.class, PersistentTasksNodeService.Status.NAME, PersistentTasksNodeService.Status::new));
    return entries;
}
Also used : PersistentTasksNodeService(org.opensearch.persistent.PersistentTasksNodeService) ComponentTemplateMetadata(org.opensearch.cluster.metadata.ComponentTemplateMetadata) ArrayList(java.util.ArrayList) PersistentTasksCustomMetadata(org.opensearch.persistent.PersistentTasksCustomMetadata) DataStreamMetadata(org.opensearch.cluster.metadata.DataStreamMetadata) RepositoriesMetadata(org.opensearch.cluster.metadata.RepositoriesMetadata) Entry(org.opensearch.common.io.stream.NamedWriteableRegistry.Entry) ScriptMetadata(org.opensearch.script.ScriptMetadata) IngestMetadata(org.opensearch.ingest.IngestMetadata) ComposableIndexTemplateMetadata(org.opensearch.cluster.metadata.ComposableIndexTemplateMetadata) IndexGraveyard(org.opensearch.cluster.metadata.IndexGraveyard)

Example 4 with IndexGraveyard

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

the class TransportDeleteDanglingIndexAction method deleteDanglingIndex.

private ClusterState deleteDanglingIndex(ClusterState currentState, Index indexToDelete) {
    final Metadata metaData = currentState.getMetadata();
    for (ObjectObjectCursor<String, IndexMetadata> each : metaData.indices()) {
        if (indexToDelete.getUUID().equals(each.value.getIndexUUID())) {
            throw new IllegalArgumentException("Refusing to delete dangling index " + indexToDelete + " as an index with UUID [" + indexToDelete.getUUID() + "] already exists in the cluster state");
        }
    }
    // that a tombstone doesn't already exist for this index.
    if (metaData.indexGraveyard().containsIndex(indexToDelete)) {
        return currentState;
    }
    Metadata.Builder metaDataBuilder = Metadata.builder(metaData);
    final IndexGraveyard newGraveyard = IndexGraveyard.builder(metaDataBuilder.indexGraveyard()).addTombstone(indexToDelete).build(settings);
    metaDataBuilder.indexGraveyard(newGraveyard);
    return ClusterState.builder(currentState).metadata(metaDataBuilder.build()).build();
}
Also used : IndexGraveyard(org.opensearch.cluster.metadata.IndexGraveyard) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata)

Example 5 with IndexGraveyard

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

the class ClusterChangedEventTests method executeIndicesChangesTest.

// execute the indices changes test by generating random index additions and deletions and
// checking the values on the cluster changed event.
private static ClusterState executeIndicesChangesTest(final ClusterState previousState, final TombstoneDeletionQuantity deletionQuantity) {
    // add random # of indices to the next cluster state
    final int numAdd = randomIntBetween(0, 5);
    final List<Index> stateIndices = new ArrayList<>();
    for (Iterator<IndexMetadata> iter = previousState.metadata().indices().valuesIt(); iter.hasNext(); ) {
        stateIndices.add(iter.next().getIndex());
    }
    final int numDel;
    switch(deletionQuantity) {
        case DELETE_ALL:
            {
                numDel = stateIndices.size();
                break;
            }
        case DELETE_NONE:
            {
                numDel = 0;
                break;
            }
        case DELETE_RANDOM:
            {
                numDel = randomIntBetween(0, Math.max(stateIndices.size() - 1, 0));
                break;
            }
        default:
            throw new AssertionError("Unhandled mode [" + deletionQuantity + "]");
    }
    final boolean changeClusterUUID = randomBoolean();
    final List<Index> addedIndices = addIndices(numAdd, randomAlphaOfLengthBetween(5, 10));
    List<Index> delIndices;
    if (changeClusterUUID) {
        delIndices = new ArrayList<>();
    } else {
        delIndices = delIndices(numDel, stateIndices);
    }
    final ClusterState newState = nextState(previousState, changeClusterUUID, addedIndices, delIndices, 0);
    ClusterChangedEvent event = new ClusterChangedEvent("_na_", newState, previousState);
    final List<String> addsFromEvent = event.indicesCreated();
    List<Index> delsFromEvent = event.indicesDeleted();
    assertThat(new HashSet<>(addsFromEvent), equalTo(addedIndices.stream().map(Index::getName).collect(Collectors.toSet())));
    assertThat(new HashSet<>(delsFromEvent), equalTo(new HashSet<>(delIndices)));
    assertThat(event.metadataChanged(), equalTo(changeClusterUUID || addedIndices.size() > 0 || delIndices.size() > 0));
    final IndexGraveyard newGraveyard = event.state().metadata().indexGraveyard();
    final IndexGraveyard oldGraveyard = event.previousState().metadata().indexGraveyard();
    assertThat(((IndexGraveyard.IndexGraveyardDiff) newGraveyard.diff(oldGraveyard)).getAdded().size(), equalTo(delIndices.size()));
    return newState;
}
Also used : ArrayList(java.util.ArrayList) Index(org.opensearch.index.Index) IndexGraveyard(org.opensearch.cluster.metadata.IndexGraveyard) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) HashSet(java.util.HashSet)

Aggregations

IndexGraveyard (org.opensearch.cluster.metadata.IndexGraveyard)9 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)7 Index (org.opensearch.index.Index)6 Metadata (org.opensearch.cluster.metadata.Metadata)5 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Settings (org.opensearch.common.settings.Settings)2 IOException (java.io.IOException)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Matchers.hasToString (org.hamcrest.Matchers.hasToString)1 ClusterName (org.opensearch.cluster.ClusterName)1 ClusterState (org.opensearch.cluster.ClusterState)1 AliasMetadata (org.opensearch.cluster.metadata.AliasMetadata)1 ComponentTemplateMetadata (org.opensearch.cluster.metadata.ComponentTemplateMetadata)1 ComposableIndexTemplateMetadata (org.opensearch.cluster.metadata.ComposableIndexTemplateMetadata)1 DataStreamMetadata (org.opensearch.cluster.metadata.DataStreamMetadata)1 IndexGraveyardDiff (org.opensearch.cluster.metadata.IndexGraveyard.IndexGraveyardDiff)1 Manifest (org.opensearch.cluster.metadata.Manifest)1 RepositoriesMetadata (org.opensearch.cluster.metadata.RepositoriesMetadata)1