Some hinky code in the shard balancer?

(Pierce Wetter) #1

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:

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});
                case BALANCE:
                case ALLOCATE:
                case MOVE:
                    thetaMap.put(operation, defaultTheta);
                    assert false;
        this.indexBalance = indexBalance;
        this.shardBalance = shardBalance;
        this.primaryBalance = primaryBalance;



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
To view this discussion on the web visit
For more options, visit

(system) #2