Http://www.cnblogs.com/bonelee/p/6078947.html analyzes the implementation of ES bulk, where the routing code:
Shardid Shardid = Clusterservice. operationrouting (). Indexshards (clusterstate, concreteindex, Request . ID (), request. Routing ()). Shardid ();
Its implementation: https://github.com/elastic/elasticsearch/blob/master/core/src/main/java/org/elasticsearch/cluster/routing/ Operationrouting.java
Public Sharditerator indexshards (clusterstate clusterstate, string index, string id, @Nullable string Routing) {return shards (clusterstate, index, id, routing). shardsit (); } protected indexshardroutingtable shards (clusterstate clusterstate, string index, string id, string Routing) { int shardid = generateshardid (indexmetadata (clusterstate, index), id, routing); return clusterstate.getroutingtable (). shardroutingtable (index, shardid); } static int generateshardid (indexmetadata indexmetadata, string id, @Nullable string Routing) {final int Hash if (routing = = null ) {hash = murmur3hashfunction.hash (id);} else {hash = murmur3hashfunction.ha SH (routing); }//we don ' t use imd#getnumberofshards since the index might has been shrunk such that we need to use the size//of the Ori Ginal Index to hash documents return Math.floormod (hash, indexmetadata.getroutingnumshards ())/ Indexmetadata.getroutingfactor (); }
You can see that the latest ES code implementation routes Are:
Math.floormod (hash, indexmetadata.getroutingnumshards ())/indexmetadata.getroutingfactor ();
In https://github.com/elastic/elasticsearch/blob/master/core/src/main/java/org/elasticsearch/cluster/metadata/ Indexmetadata.java can be seen in Getroutingfactor implementations:
/** * Returns The routing factor for this Index. The default is <tt>1</tt>. * * @see #getRoutingFactor (indexmetadata, int) for details */public int getroutingfactor () { return routingfactor; }
In the constructor are:
Assert numberofshards * Routingfactor = = routingnumshards: routingnumshards + "must Be a multiple of" + NUMBEROFSH Ards
anyway, The default is 1, that is, all shard nodes will be responsible for routing!
beware, the ES2.4 version of the routing implementation: https://github.com/elastic/elasticsearch/blob/2.4/core/src/main/java/org/elasticsearch/cluster/routing/
@SuppressForbidden (reason = "math#abs is trappy") Private intgenerateshardid (clusterstate clusterstate, string index, string type, string id, @Nullable string Routing) {I Ndexmetadata Indexmetadata=clusterstate.metadata (). Index (index); if(indexmetadata = =NULL) { Throw Newindexnotfoundexception (index); } FinalVersion createdversion =indexmetadata.getcreationversion (); FinalHashfunction hashfunction =indexmetadata.getroutinghashfunction (); Final BooleanUsetype =Indexmetadata.getroutingusetype (); Final inthash; if(routing = =NULL) { if(!Usetype) {hash=Hash (hashfunction, id); } Else{hash=Hash (hashfunction, type, id); } } Else{hash=Hash (hashfunction, routing); } if(createdversion.onorafter (version.v_2_0_0_beta1)) {returnmathutils.mod (hash, indexmetadata.getnumberofshards ()); } Else { returnMath.Abs (hash%Indexmetadata.getnumberofshards ()); } }
@Deprecated protectedint hash (hashfunction hashfunction, String type, String id) { ifnull | | "_all". Equals (type)) { thrownew illegalargumentexception ("Can ' t route an Operation with no type and have type part of the routing (for backward Comp); } return hashfunction.hash (type, id); }
And the hash function is implemented by:
Djbhashfunction.java
Simplehashfunction.java
Murmur3hashfunction.java
Three KINDS.
Murmur3hashfunction.java
The newest ES 5.0 routing algorithm underlying implementation