# What's the logic or algorithm for allocation of primary shard or replica when node joins cluster?

**URL:** <https://discuss.elastic.co/t/whats-the-logic-or-algorithm-for-allocation-of-primary-shard-or-replica-when-node-joins-cluster/9442>\
**Category:** Elasticsearch\
**Created:** [October 23, 2012, 2:53am UTC](https://discuss.elastic.co/t/whats-the-logic-or-algorithm-for-allocation-of-primary-shard-or-replica-when-node-joins-cluster/9442 "2012-10-23T02:53:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SuoNayi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/suonayi/32/2569_2.png) [@SuoNayi](https://discuss.elastic.co/u/SuoNayi)\
**Post date:** [October 23, 2012, 2:53am UTC](https://discuss.elastic.co/t/whats-the-logic-or-algorithm-for-allocation-of-primary-shard-or-replica-when-node-joins-cluster/9442/1 "2012-10-23T02:53:02Z")

</div>

Hi all,  
I have googled a lot but found nothing about the logic for  
allocation of shard and replica when node joins the cluster.  
If it's not evenly distributed shards and replicas, can I controll  
allocation of shards and replicas?  
What I mean manually controll the allocation of shards and replicas  
in order to making a balance.  
any pointer is a great help, thanks.

SuoNayi

--

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [October 23, 2012, 3:27am UTC](https://discuss.elastic.co/t/whats-the-logic-or-algorithm-for-allocation-of-primary-shard-or-replica-when-node-joins-cluster/9442/2 "2012-10-23T03:27:23Z")

</div>

I believe the main algorithm is defined in EvenShardsCountAllocator:

[https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/cluster/routing/allocation/allocator/EvenShardsCountAllocator.java](https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/cluster/routing/allocation/allocator/EvenShardsCountAllocator.java)

You can create your own Allocator, but there is nothing to manual assign  
shards. I think there is something new in 0.20

--  
Ivan

On Mon, Oct 22, 2012 at 7:53 PM, SuoNayi [suonayi@gmail.com](mailto:suonayi@gmail.com) wrote:

> Hi all,  
> I have googled a lot but found nothing about the logic for  
> allocation of shard and replica when node joins the cluster.  
> If it's not evenly distributed shards and replicas, can I controll  
> allocation of shards and replicas?  
> What I mean manually controll the allocation of shards and replicas  
> in order to making a balance.  
> any pointer is a great help, thanks.
> 
> SuoNayi
> 
> --

--

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [October 23, 2012, 10:32am UTC](https://discuss.elastic.co/t/whats-the-logic-or-algorithm-for-allocation-of-primary-shard-or-replica-when-node-joins-cluster/9442/3 "2012-10-23T10:32:57Z")

</div>

As Ivan mentioned, the default algorithm is in EvenShardsCountAllocator. As  
you can see from the name, it tries to allocate shards evenly across all  
nodes in the cluster. In other words it tries to allocate the same number  
of shards on each node.

You can control shards allocation explicitly by using Cluster Reroute API[http://www.elasticsearch.org/guide/reference/api/admin-cluster-reroute.html](http://www.elasticsearch.org/guide/reference/api/admin-cluster-reroute.html)and declaratively by using index  
routing allocation[http://www.elasticsearch.org/guide/reference/index-modules/allocation.html](http://www.elasticsearch.org/guide/reference/index-modules/allocation.html)settings.

On Monday, October 22, 2012 11:27:27 PM UTC-4, Ivan Brusic wrote:

> I believe the main algorithm is defined in EvenShardsCountAllocator:
> 
> [https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/cluster/routing/allocation/allocator/EvenShardsCountAllocator.java](https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/cluster/routing/allocation/allocator/EvenShardsCountAllocator.java)
> 
> You can create your own Allocator, but there is nothing to manual assign  
> shards. I think there is something new in 0.20
> 
> --  
> Ivan
> 
> On Mon, Oct 22, 2012 at 7:53 PM, SuoNayi \<[suo...@gmail.com](mailto:suo...@gmail.com) \<javascript:\>\>wrote:
> 
> > Hi all,  
> > I have googled a lot but found nothing about the logic for  
> > allocation of shard and replica when node joins the cluster.  
> > If it's not evenly distributed shards and replicas, can I controll  
> > allocation of shards and replicas?  
> > What I mean manually controll the allocation of shards and replicas  
> > in order to making a balance.  
> > any pointer is a great help, thanks.
> > 
> > SuoNayi
> > 
> > --

--

---

<div class="post-metadata">

**Author:** ![SuoNayi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/suonayi/32/2569_2.png) [@SuoNayi](https://discuss.elastic.co/u/SuoNayi)\
**Post date:** [October 25, 2012, 2:54am UTC](https://discuss.elastic.co/t/whats-the-logic-or-algorithm-for-allocation-of-primary-shard-or-replica-when-node-joins-cluster/9442/4 "2012-10-25T02:54:18Z")

</div>

thanks all.

在 2012年10月23日星期二UTC+8下午6时32分57秒，Igor Motov写道：

> As Ivan mentioned, the default algorithm is in EvenShardsCountAllocator.  
> As you can see from the name, it tries to allocate shards evenly across all  
> nodes in the cluster. In other words it tries to allocate the same number  
> of shards on each node.
> 
> You can control shards allocation explicitly by using Cluster Reroute API[http://www.elasticsearch.org/guide/reference/api/admin-cluster-reroute.html](http://www.elasticsearch.org/guide/reference/api/admin-cluster-reroute.html)and declaratively by using index  
> routing allocation[http://www.elasticsearch.org/guide/reference/index-modules/allocation.html](http://www.elasticsearch.org/guide/reference/index-modules/allocation.html)settings.
> 
> On Monday, October 22, 2012 11:27:27 PM UTC-4, Ivan Brusic wrote:
> 
> > I believe the main algorithm is defined in EvenShardsCountAllocator:
> > 
> > [https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/cluster/routing/allocation/allocator/EvenShardsCountAllocator.java](https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/cluster/routing/allocation/allocator/EvenShardsCountAllocator.java)
> > 
> > You can create your own Allocator, but there is nothing to manual assign  
> > shards. I think there is something new in 0.20
> > 
> > --  
> > Ivan
> > 
> > On Mon, Oct 22, 2012 at 7:53 PM, SuoNayi [suo...@gmail.com](mailto:suo...@gmail.com) wrote:
> > 
> > > Hi all,  
> > > I have googled a lot but found nothing about the logic for  
> > > allocation of shard and replica when node joins the cluster.  
> > > If it's not evenly distributed shards and replicas, can I  
> > > controll allocation of shards and replicas?  
> > > What I mean manually controll the allocation of shards and  
> > > replicas in order to making a balance.  
> > > any pointer is a great help, thanks.
> > > 
> > > SuoNayi
> > > 
> > > --

--

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 3:07am UTC](https://discuss.elastic.co/t/whats-the-logic-or-algorithm-for-allocation-of-primary-shard-or-replica-when-node-joins-cluster/9442/5 "2017-07-06T03:07:20Z")

</div>


