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));
}
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);
}
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;
}
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();
}
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;
}
Aggregations