Skip to content

Commit eaa7329

Browse files
authored
Fix deleting unassigned indices in MP (#127750)
In some cases, deleting unassigned indices in multi-project mode would result in an error log due to a `MultiProjectPendingException` which was caused by the use of `Metadata#getProject()`. We fix that by passing the correct (potentially null) project metadata instead.
1 parent 31efea7 commit eaa7329

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

server/src/main/java/org/elasticsearch/indices/IndicesService.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,19 +1064,25 @@ public void afterIndexShardClosed(ShardId shardId, IndexShard indexShard, Settin
10641064
/**
10651065
* Deletes an index that is not assigned to this node. This method cleans up all disk folders relating to the index
10661066
* but does not deal with in-memory structures. For those call {@link #removeIndex}
1067+
*
1068+
* @param reason the reason why this index should be deleted
1069+
* @param oldIndexMetadata the index metadata of the index that should be deleted
1070+
* @param currentProject the <i>current</i> project metadata which is used to verify that the index does not exist in the project
1071+
* anymore - can be null in case the whole project got deleted while there were still indices in it
10671072
*/
10681073
@Override
1069-
public void deleteUnassignedIndex(String reason, IndexMetadata oldIndexMetadata, ClusterState clusterState) {
1074+
public void deleteUnassignedIndex(String reason, IndexMetadata oldIndexMetadata, @Nullable ProjectMetadata currentProject) {
10701075
if (nodeEnv.hasNodeFile()) {
10711076
Index index = oldIndexMetadata.getIndex();
10721077
try {
1073-
if (clusterState.metadata().getProject().hasIndex(index)) {
1074-
final IndexMetadata currentMetadata = clusterState.metadata().getProject().index(index);
1078+
if (currentProject != null && currentProject.hasIndex(index)) {
1079+
final IndexMetadata currentMetadata = currentProject.index(index);
10751080
throw new IllegalStateException(
10761081
"Can't delete unassigned index store for ["
10771082
+ index.getName()
1078-
+ "] - it's still part "
1079-
+ "of the cluster state ["
1083+
+ "] - it's still part of project ["
1084+
+ currentProject.id()
1085+
+ "] with UUIDs ["
10801086
+ currentMetadata.getIndexUUID()
10811087
+ "] ["
10821088
+ oldIndexMetadata.getIndexUUID()

server/src/main/java/org/elasticsearch/indices/cluster/IndicesClusterStateService.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,12 @@ private void deleteIndices(final ClusterChangedEvent event) {
400400
indexServiceClosedListener = SubscribableListener.nullSuccess();
401401
final IndexMetadata metadata = project.get().index(index);
402402
indexSettings = new IndexSettings(metadata, settings);
403-
indicesService.deleteUnassignedIndex("deleted index was not assigned to local node", metadata, state);
403+
final var projectId = project.get().id();
404+
indicesService.deleteUnassignedIndex(
405+
"deleted index in project [" + projectId + "] was not assigned to local node",
406+
metadata,
407+
state.metadata().projects().get(projectId)
408+
);
404409
} else {
405410
// The previous cluster state's metadata also does not contain the index,
406411
// which is what happens on node startup when an index was deleted while the
@@ -1257,8 +1262,13 @@ U createIndex(IndexMetadata indexMetadata, List<IndexEventListener> builtInIndex
12571262
/**
12581263
* Deletes an index that is not assigned to this node. This method cleans up all disk folders relating to the index
12591264
* but does not deal with in-memory structures. For those call {@link #removeIndex}
1265+
*
1266+
* @param reason the reason why this index should be deleted
1267+
* @param oldIndexMetadata the index metadata of the index that should be deleted
1268+
* @param currentProject the <i>current</i> project metadata which is used to verify that the index does not exist in the project
1269+
* anymore - can be null in case the whole project got deleted while there were still indices in it
12601270
*/
1261-
void deleteUnassignedIndex(String reason, IndexMetadata metadata, ClusterState clusterState);
1271+
void deleteUnassignedIndex(String reason, IndexMetadata oldIndexMetadata, @Nullable ProjectMetadata currentProject);
12621272

12631273
/**
12641274
* Removes the given index from this service and releases all associated resources. Persistent parts of the index

test/framework/src/main/java/org/elasticsearch/indices/cluster/AbstractIndicesClusterStateServiceTestCase.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.elasticsearch.action.ActionListener;
1313
import org.elasticsearch.cluster.ClusterState;
1414
import org.elasticsearch.cluster.metadata.IndexMetadata;
15+
import org.elasticsearch.cluster.metadata.ProjectMetadata;
1516
import org.elasticsearch.cluster.node.DiscoveryNode;
1617
import org.elasticsearch.cluster.routing.IndexShardRoutingTable;
1718
import org.elasticsearch.cluster.routing.RoutingNode;
@@ -213,7 +214,7 @@ public IndexMetadata verifyIndexIsDeleted(Index index, ClusterState state) {
213214
}
214215

215216
@Override
216-
public void deleteUnassignedIndex(String reason, IndexMetadata metadata, ClusterState clusterState) {
217+
public void deleteUnassignedIndex(String reason, IndexMetadata oldIndexMetadata, ProjectMetadata currentProject) {
217218

218219
}
219220

0 commit comments

Comments
 (0)