Some hinky code in the shard balancer?

I was digging into the code this morning to try to figure out why my primary shards are unbalanced.

Look at the code below around THRESHOLD_CHECK: (From line 194 here: https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/cluster/routing/allocation/allocator/BalancedShardsAllocator.java#L205)

Looks like the intent here is rescale the sum to be just the indexBalance + shardBalance, and remove the primaryBalance weight from the application. I'm not sure why that's necessary, but what I'm noticing below thats hinky is that the code inside the if statement doesn't do anything. If the if executes, you have a put, but the very next line overwrites the results with a different put. Perhaps there should be an else there?

If so, then this code is protection to make sure that the thetaMap never ends up with values that add up < 0 if you remove balancing the primary as a factor from checking against the threshold doesn't produce a thetaMap that adds up to less than 0.

   public WeightFunction(float indexBalance, float shardBalance, float primaryBalance) {
        float sum = indexBalance + shardBalance + primaryBalance;
        if (sum <= 0.0f) {
            throw new ElasticSearchIllegalArgumentException("Balance factors must sum to a value > 0 but was: " + sum);
        }
        final float[] defaultTheta = new float[]{shardBalance / sum, indexBalance / sum, primaryBalance / sum};
        for (Operation operation : Operation.values()) {
            switch (operation) {
                case THRESHOLD_CHECK:
                    sum = indexBalance + shardBalance;
                    if (sum <= 0.0f) {
                        thetaMap.put(operation, defaultTheta);
                    }
                    thetaMap.put(operation, new float[]{shardBalance / sum, indexBalance / sum, 0});
                    break;
                case BALANCE:
                case ALLOCATE:
                case MOVE:
                    thetaMap.put(operation, defaultTheta);
                    break;
                default:
                    assert false;
            }
        }
        this.indexBalance = indexBalance;
        this.shardBalance = shardBalance;
        this.primaryBalance = primaryBalance;
    }

Pierce

P.S.

Love some hints in how to nudge my cluster to rebalance the primary shards. It will move primary shards off the current "I have all the primary shards" node to move shards to nodes that need some, but it won't switch the primaries otherwise.

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/d7091f13-b34f-4f56-ac86-0aa32a9cc070%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.