Skip to content

Commit

Permalink
Use consistent shard map type in IndexService (#88465)
Browse files Browse the repository at this point in the history
It's faster and easier to reason about if we always have
an immutable collections map here and not have the type depend
on what the last operation on the index service was.
  • Loading branch information
original-brownbear authored Jul 12, 2022
1 parent 4af02b8 commit 47ecd20
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions server/src/main/java/org/elasticsearch/index/IndexService.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@
import java.util.function.Supplier;

import static java.util.Collections.emptyMap;
import static java.util.Collections.unmodifiableMap;
import static org.elasticsearch.core.Strings.format;

public class IndexService extends AbstractIndexComponent implements IndicesClusterStateService.AllocatedIndex<IndexShard> {
Expand All @@ -118,7 +117,7 @@ public class IndexService extends AbstractIndexComponent implements IndicesClust
private final SimilarityService similarityService;
private final EngineFactory engineFactory;
private final IndexWarmer warmer;
private volatile Map<Integer, IndexShard> shards = emptyMap();
private volatile Map<Integer, IndexShard> shards = Map.of();
private final AtomicBoolean closed = new AtomicBoolean(false);
private final AtomicBoolean deleted = new AtomicBoolean(false);
private final IndexSettings indexSettings;
Expand Down Expand Up @@ -523,16 +522,13 @@ public synchronized IndexShard createShard(

@Override
public synchronized void removeShard(int shardId, String reason) {
final ShardId sId = new ShardId(index(), shardId);
final IndexShard indexShard;
if (shards.containsKey(shardId) == false) {
final IndexShard indexShard = shards.get(shardId);
if (indexShard == null) {
return;
}
logger.debug("[{}] closing... (reason: [{}])", shardId, reason);
HashMap<Integer, IndexShard> newShards = new HashMap<>(shards);
indexShard = newShards.remove(shardId);
shards = unmodifiableMap(newShards);
closeShard(reason, sId, indexShard, indexShard.store(), indexShard.getIndexEventListener());
shards = Maps.copyMapWithRemovedEntry(shards, shardId);
closeShard(reason, indexShard.shardId(), indexShard, indexShard.store(), indexShard.getIndexEventListener());
logger.debug("[{}] closed (reason: [{}])", shardId, reason);
}

Expand Down

0 comments on commit 47ecd20

Please sign in to comment.