The newest ES 5.0 routing algorithm underlying implementation

Source: Internet
Author: User
Tags abs

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.