Skip to content

Commit cba210c

Browse files
committed
apply review comments
1 parent b772b51 commit cba210c

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

core/src/main/java/org/elasticsearch/index/shard/IndexShard.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public class IndexShard extends AbstractIndexShardComponent implements IndexSett
166166
private final MeanMetric refreshMetric = new MeanMetric();
167167
private final MeanMetric flushMetric = new MeanMetric();
168168

169-
private final ShardEngineFailListener engineEventListener = new ShardEngineFailListener();
169+
private final ShardEventListener shardEventListener = new ShardEventListener();
170170
private volatile boolean flushOnClose = true;
171171
private volatile int flushThresholdOperations;
172172
private volatile ByteSizeValue flushThresholdSize;
@@ -979,7 +979,7 @@ private void startScheduledTasksIfNeeded() {
979979
public static final String INDEX_REFRESH_INTERVAL = "index.refresh_interval";
980980

981981
public void addShardFailureCallback(Callback<ShardFailure> onShardFailure) {
982-
this.engineEventListener.delegates.add(onShardFailure);
982+
this.shardEventListener.delegates.add(onShardFailure);
983983
}
984984

985985
/** Change the indexing and translog buffer sizes. If {@code IndexWriter} is currently using more than
@@ -1368,13 +1368,13 @@ protected Engine getEngineOrNull() {
13681368
return this.currentEngineReference.get();
13691369
}
13701370

1371-
class ShardEngineFailListener implements Engine.EventListener {
1371+
class ShardEventListener implements Engine.EventListener {
13721372
private final CopyOnWriteArrayList<Callback<ShardFailure>> delegates = new CopyOnWriteArrayList<>();
13731373

13741374
// called by the current engine
13751375
@Override
13761376
public void onFailedEngine(String reason, @Nullable Throwable failure) {
1377-
final ShardFailure shardFailure = new ShardFailure(shardRouting, reason, failure);
1377+
final ShardFailure shardFailure = new ShardFailure(shardRouting, reason, failure, getIndexUUID());
13781378
for (Callback<ShardFailure> listener : delegates) {
13791379
try {
13801380
listener.handle(shardFailure);
@@ -1457,7 +1457,7 @@ protected void operationProcessed() {
14571457
};
14581458
return new EngineConfig(shardId,
14591459
threadPool, indexingService, indexSettings, warmer, store, deletionPolicy, mergePolicyConfig.getMergePolicy(), mergeSchedulerConfig,
1460-
mapperService.indexAnalyzer(), similarityService.similarity(mapperService), codecService, engineEventListener, translogRecoveryPerformer, indexCache.query(), cachingPolicy, translogConfig);
1460+
mapperService.indexAnalyzer(), similarityService.similarity(mapperService), codecService, shardEventListener, translogRecoveryPerformer, indexCache.query(), cachingPolicy, translogConfig);
14611461
}
14621462

14631463
private static class IndexShardOperationCounter extends AbstractRefCounted {
@@ -1578,12 +1578,15 @@ public void onAfter() {
15781578
public static final class ShardFailure {
15791579
public final ShardRouting routing;
15801580
public final String reason;
1581+
@Nullable
15811582
public final Throwable cause;
1583+
public final String indexUUID;
15821584

1583-
public ShardFailure(ShardRouting routing, String reason, Throwable cause) {
1585+
public ShardFailure(ShardRouting routing, String reason, @Nullable Throwable cause, String indexUUID) {
15841586
this.routing = routing;
15851587
this.reason = reason;
15861588
this.cause = cause;
1589+
this.indexUUID = indexUUID;
15871590
}
15881591
}
15891592

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ static class FailedShard {
9898
}
9999

100100
private final Object mutex = new Object();
101-
private final FailedEngineHandler failedEngineHandler = new FailedEngineHandler();
101+
private final FailedShardHandler failedShardHandler = new FailedShardHandler();
102102

103103
private final boolean sendRefreshMapping;
104104

@@ -381,7 +381,7 @@ private void applyMappings(ClusterChangedEvent event) {
381381
// so this failure typically means wrong node level configuration or something similar
382382
for (IndexShard indexShard : indexService) {
383383
ShardRouting shardRouting = indexShard.routingEntry();
384-
failAndRemoveShard(shardRouting, indexService, true, "failed to update mappings", t);
384+
failAndRemoveShard(shardRouting, indexService.indexUUID(), indexService, true, "failed to update mappings", t);
385385
}
386386
}
387387
}
@@ -637,11 +637,11 @@ private void applyInitializingShard(final ClusterState state, final IndexMetaDat
637637
}
638638
IndexShard indexShard = indexService.createShard(shardId, shardRouting);
639639
indexShard.updateRoutingEntry(shardRouting, state.blocks().disableStatePersistence() == false);
640-
indexShard.addShardFailureCallback(failedEngineHandler);
640+
indexShard.addShardFailureCallback(failedShardHandler);
641641
} catch (IndexShardAlreadyExistsException e) {
642642
// ignore this, the method call can happen several times
643643
} catch (Throwable e) {
644-
failAndRemoveShard(shardRouting, indexService, true, "failed to create shard", e);
644+
failAndRemoveShard(shardRouting, indexService.indexUUID(), indexService, true, "failed to create shard", e);
645645
return;
646646
}
647647
}
@@ -768,7 +768,7 @@ public void onRecoveryFailure(RecoveryState state, RecoveryFailedException e, bo
768768

769769
private void handleRecoveryFailure(IndexService indexService, ShardRouting shardRouting, boolean sendShardFailure, Throwable failure) {
770770
synchronized (mutex) {
771-
failAndRemoveShard(shardRouting, indexService, sendShardFailure, "failed recovery", failure);
771+
failAndRemoveShard(shardRouting, indexService.indexUUID(), indexService, sendShardFailure, "failed recovery", failure);
772772
}
773773
}
774774

@@ -802,8 +802,10 @@ private void deleteIndex(String index, String reason) {
802802

803803
}
804804

805-
private void failAndRemoveShard(ShardRouting shardRouting, IndexService indexService, boolean sendShardFailure, String message, @Nullable Throwable failure) {
806-
if (indexService.hasShard(shardRouting.getId())) {
805+
private void failAndRemoveShard(ShardRouting shardRouting, String indexUUID, @Nullable IndexService indexService, boolean sendShardFailure, String message, @Nullable Throwable failure) {
806+
if (indexService != null && indexService.hasShard(shardRouting.getId())) {
807+
// if the indexService is null we can't remove the shard, that's fine since we might have a failure
808+
// when the index is remove and then we already removed the index service for that shard...
807809
try {
808810
indexService.removeShard(shardRouting.getId(), message);
809811
} catch (ShardNotFoundException e) {
@@ -813,7 +815,7 @@ private void failAndRemoveShard(ShardRouting shardRouting, IndexService indexSer
813815
}
814816
}
815817
if (sendShardFailure) {
816-
sendFailShard(shardRouting, indexService.indexUUID(), message, failure);
818+
sendFailShard(shardRouting, indexUUID, message, failure);
817819
}
818820
}
819821

@@ -827,14 +829,14 @@ private void sendFailShard(ShardRouting shardRouting, String indexUUID, String m
827829
}
828830
}
829831

830-
private class FailedEngineHandler implements Callback<IndexShard.ShardFailure> {
832+
private class FailedShardHandler implements Callback<IndexShard.ShardFailure> {
831833
@Override
832834
public void handle(final IndexShard.ShardFailure shardFailure) {
833835
final IndexService indexService = indicesService.indexService(shardFailure.routing.shardId().index().name());
834836
final ShardRouting shardRouting = shardFailure.routing;
835837
threadPool.generic().execute(() -> {
836838
synchronized (mutex) {
837-
failAndRemoveShard(shardRouting, indexService, true, "engine failure, reason [" + shardFailure.reason + "]", shardFailure.cause);
839+
failAndRemoveShard(shardRouting, shardFailure.indexUUID, indexService, true, "shard failure, reason [" + shardFailure.reason + "]", shardFailure.cause);
838840
}
839841
});
840842
}

0 commit comments

Comments
 (0)