/_cluster/reroute automatically reroute and rebalance all primary shards

Elasticsearch 6.5.4

I was wondering, if there is possibility that elasticsearch automatically rebalances shards on nodes.

For example, when I have 2 or 3 or or more node cluster, and I restart single node, then after service restart, this node has all the indexes replica shards only. And to get some primary shards routed to this node, I'd have to manually do the reroute, for example, I restarted elasticnode2, then I'd have to manually reroute some shards for every index like this:

POST /_cluster/reroute
{
   "commands": [
      {
         "move": {
            "index": "index_name1",
            "shard": 2,
            "from_node": "elasticnode1",
            "to_node": "elasticnode2"
         }
      }
   ]
}

https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-reroute.html

I am looking a way, that elasticsearch automatically allocates some primary shards to this restarted node, when it is back up and running.

Thanks.

Regards
Raul

I can't even manually achieve this, since elasticnode2 has all the shards already allocated (replica shards), which get automatically allocated after node rejoin cluster (after service restart).

I get error when executing this cluster reroute command:

{
  "decider" : "same_shard",
  "decision" : "NO",
  "explanation" : "the shard cannot be allocated to the same node on which a copy of the shard already exists [[index_name1][0], node[K81sF6LQRP6VJaFJOqnYFg], [R], s[STARTED], a[id=mFB2EofwRdW_Ec9YkMQkOQ]]"
},

On way of achieving this, is:

  • curl -sXPUT hostname':9200/_cluster/settings?pretty' -H 'Content-Type: application/json' -d '{"persistent": {"cluster.routing.allocation.enable": "none"}}'
  • elasticnode2 service restart
  • (shards won't get allocated to elasticnode2, since allocation is disabled)
  • then start manually allocating shards.
  • at the end curl -sXPUT hostname':9200/_cluster/settings?pretty' -H 'Content-Type: application/json' -d '{"persistent": {"cluster.routing.allocation.enable": "null"}}'

Regards
Raul

Why do you want to do this? There should be no need to balance primaries vs replicas.

Hi

Hmm, well, when I create a new index, then elasticsearch automatically balances primary shards between nodes, for example, with 2 node cluster, then one node has 2 and other has 3 primary shards.

I am thinking, that if this is elasticsearch default behaviour, then perhaps it is way to go..:slight_smile:

And would’nt the searxh operations be faster if primary shards are balanced between nodes..?

Regards

Raul

This is an interesting artefact of how Elasticsearch allocates shards: it will allocate the primaries first and then the replicas, one at a time, trying to maintain balance throughout the process, so when all the primaries are allocated and all of the replicas are unallocated then the balanced configuration is as you describe.

However it's not really necessary to maintain this balance, which is why it doesn't happen after a node restarts. Simpler just to leave the primaries where they are.

It shouldn't make a difference. All copies of a shard can service searches whether they be primary or replica.

Hi

Ok, thanks for clearing this.

Regards

Raul

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.