Skip to content

Commit effdd52

Browse files
committed
sync changes done on the indices cluster service with changes happening in an async manner during recovery
1 parent 4b06eeb commit effdd52

File tree

1 file changed

+34
-26
lines changed

1 file changed

+34
-26
lines changed

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

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ public class IndicesClusterStateService extends AbstractLifecycleComponent<Indic
8686
// a map of mappings type we have seen per index
8787
private final ConcurrentMap<Tuple<String, String>, Boolean> seenMappings = ConcurrentCollections.newConcurrentMap();
8888

89+
private final Object mutex = new Object();
90+
8991
@Inject public IndicesClusterStateService(Settings settings, IndicesService indicesService, ClusterService clusterService,
9092
ThreadPool threadPool, RecoveryTarget recoveryTarget, ShardStateAction shardStateAction,
9193
NodeIndexCreatedAction nodeIndexCreatedAction, NodeIndexDeletedAction nodeIndexDeletedAction,
@@ -116,12 +118,14 @@ public class IndicesClusterStateService extends AbstractLifecycleComponent<Indic
116118
if (!indicesService.changesAllowed())
117119
return;
118120

119-
applyNewIndices(event);
120-
applyMappings(event);
121-
applyNewOrUpdatedShards(event);
122-
applyDeletedIndices(event);
123-
applyDeletedShards(event);
124-
applyCleanedIndices(event);
121+
synchronized (mutex) {
122+
applyNewIndices(event);
123+
applyMappings(event);
124+
applyNewOrUpdatedShards(event);
125+
applyDeletedIndices(event);
126+
applyDeletedShards(event);
127+
applyCleanedIndices(event);
128+
}
125129
}
126130

127131
private void applyCleanedIndices(final ClusterChangedEvent event) {
@@ -426,13 +430,15 @@ private PeerRecoveryListener(StartRecoveryRequest request, ShardRouting shardRou
426430
if (!removeShard) {
427431
return;
428432
}
429-
if (indexService.hasShard(shardRouting.shardId().id())) {
430-
try {
431-
indexService.removeShard(shardRouting.shardId().id());
432-
} catch (IndexShardMissingException e) {
433-
// the node got closed on us, ignore it
434-
} catch (Exception e1) {
435-
logger.warn("[{}][{}] failed to delete shard after ignore recovery", e1, indexService.index().name(), shardRouting.shardId().id());
433+
synchronized (mutex) {
434+
if (indexService.hasShard(shardRouting.shardId().id())) {
435+
try {
436+
indexService.removeShard(shardRouting.shardId().id());
437+
} catch (IndexShardMissingException e) {
438+
// the node got closed on us, ignore it
439+
} catch (Exception e1) {
440+
logger.warn("[{}][{}] failed to delete shard after ignore recovery", e1, indexService.index().name(), shardRouting.shardId().id());
441+
}
436442
}
437443
}
438444
}
@@ -444,20 +450,22 @@ private PeerRecoveryListener(StartRecoveryRequest request, ShardRouting shardRou
444450

445451
private void handleRecoveryFailure(IndexService indexService, ShardRouting shardRouting, boolean sendShardFailure, Throwable failure) {
446452
logger.warn("[{}][{}] failed to start shard", failure, indexService.index().name(), shardRouting.shardId().id());
447-
if (indexService.hasShard(shardRouting.shardId().id())) {
448-
try {
449-
indexService.cleanShard(shardRouting.shardId().id());
450-
} catch (IndexShardMissingException e) {
451-
// the node got closed on us, ignore it
452-
} catch (Exception e1) {
453-
logger.warn("[{}][{}] failed to delete shard after failed startup", e1, indexService.index().name(), shardRouting.shardId().id());
453+
synchronized (mutex) {
454+
if (indexService.hasShard(shardRouting.shardId().id())) {
455+
try {
456+
indexService.cleanShard(shardRouting.shardId().id());
457+
} catch (IndexShardMissingException e) {
458+
// the node got closed on us, ignore it
459+
} catch (Exception e1) {
460+
logger.warn("[{}][{}] failed to delete shard after failed startup", e1, indexService.index().name(), shardRouting.shardId().id());
461+
}
454462
}
455-
}
456-
if (sendShardFailure) {
457-
try {
458-
shardStateAction.shardFailed(shardRouting, "Failed to start shard, message [" + detailedMessage(failure) + "]");
459-
} catch (Exception e1) {
460-
logger.warn("[{}][{}] failed to mark shard as failed after a failed start", e1, indexService.index().name(), shardRouting.id());
463+
if (sendShardFailure) {
464+
try {
465+
shardStateAction.shardFailed(shardRouting, "Failed to start shard, message [" + detailedMessage(failure) + "]");
466+
} catch (Exception e1) {
467+
logger.warn("[{}][{}] failed to mark shard as failed after a failed start", e1, indexService.index().name(), shardRouting.id());
468+
}
461469
}
462470
}
463471
}

0 commit comments

Comments
 (0)