1
votes

While investigating a node with shards on disk not referenced as "active" by the cluster, I found an allocation state: unused. This state is referenced in the documentation, however I found no explanation about it.

The status of the store copy, whether it is used as a primary, replica or not used at all

Can someone please explain the purpose(s) of it?

Edit: Given comments it should be noted that the cluster was green and all primaries and replicas of all indices were present at the time I found those unused shards.

As of now all these shards in unused state (some were older than a year) disappeared. The operation that happened was to reduce replication on an index. This operation removed all unused shards even if they belonged to other indices. It should be noted that every month a new index is created and another is deleted so previously mentioned operation does not seem to explain the behavior solely.

2

2 Answers

1
votes

i think this could happen in an index with 2 shards and 1 replica per shard, so in a 2 node cluster you'd have (shard1, replica2) on one node and (shard2, replica1) on the other. a new node is added to the cluster and ES decides it is "better" or "faster" than one of the nodes and migrates one replica to it, so the previous copy of the replica becomes "unused" (it won't receive updates). or perhaps if your index settings changed to reduce the number of replicas, some of them become "unused" and don't receive updates.

1
votes

You are right, that there is not much information on the unused shards in elasticsearch documentation so only way to get some information is by looking at the source code.

If you look at the getAllocationStatus method which decide the shard status you will get to know how ES deciding shard is unused.

Above method code is below

 private IndicesShardStoresResponse.StoreStatus.AllocationStatus getAllocationStatus(String index, int shardID,
                                                                                                DiscoveryNode node) {
                for (ShardRouting shardRouting : routingNodes.node(node.getId())) {
                    ShardId shardId = shardRouting.shardId();
                    if (shardId.id() == shardID && shardId.getIndexName().equals(index)) {
                        if (shardRouting.primary()) {
                            return IndicesShardStoresResponse.StoreStatus.AllocationStatus.PRIMARY;
                        } else if (shardRouting.assignedToNode()) {
                            return IndicesShardStoresResponse.StoreStatus.AllocationStatus.REPLICA;
                        } else {
                            return IndicesShardStoresResponse.StoreStatus.AllocationStatus.UNUSED;
                        }
                    }
                }
                return IndicesShardStoresResponse.StoreStatus.AllocationStatus.UNUSED;
            }

As you can note, if shard is having a primary flag, its marked as primary shard, after that if shard is assigned to a node, it's marked as replica shard and if both of above conditions don't match its marked as unused.

I believe unless shard is copied completely Elasticsearch won't assign it a node(refer shard allocation for more details) so it will be marked as unused, but for some reason if node from which its being copied is died or disconnected and can't be recovered it may always be in unused status.