private void assertPrimaryBalance(
      ESLogger logger,
      RoutingNodes nodes,
      int numberOfNodes,
      int numberOfIndices,
      int numberOfReplicas,
      int numberOfShards,
      float treshold) {

    final int numShards = numberOfShards;
    final float avgNumShards = (float) (numShards) / (float) (numberOfNodes);
    final int minAvgNumberOfShards = Math.round(Math.round(Math.floor(avgNumShards - treshold)));
    final int maxAvgNumberOfShards = Math.round(Math.round(Math.ceil(avgNumShards + treshold)));

    for (String index : nodes.getRoutingTable().indicesRouting().keySet()) {
      for (RoutingNode node : nodes) {
        int primaries = 0;
        for (ShardRouting shard : node.shardsWithState(index, STARTED)) {
          primaries += shard.primary() ? 1 : 0;
        }
        //                logger.info(node.nodeId() + ": " + primaries + " primaries
        // ("+minAvgNumberOfShards+" to "+maxAvgNumberOfShards+")");
        assertThat(primaries, Matchers.greaterThanOrEqualTo(minAvgNumberOfShards));
        assertThat(primaries, Matchers.lessThanOrEqualTo(maxAvgNumberOfShards));
      }
    }
  }
  private void assertIndexBalance(
      ESLogger logger,
      RoutingNodes nodes,
      int numberOfNodes,
      int numberOfIndices,
      int numberOfReplicas,
      int numberOfShards,
      float treshold) {

    final int numShards = numberOfShards * (numberOfReplicas + 1);
    final float avgNumShards = (float) (numShards) / (float) (numberOfNodes);
    final int minAvgNumberOfShards = Math.round(Math.round(Math.floor(avgNumShards - treshold)));
    final int maxAvgNumberOfShards = Math.round(Math.round(Math.ceil(avgNumShards + treshold)));

    for (String index : nodes.getRoutingTable().indicesRouting().keySet()) {
      for (RoutingNode node : nodes) {
        //              logger.info(node.nodeId() +":"+index+ ": " + node.shardsWithState(index,
        // INITIALIZING, STARTED).size() + " shards ("+minAvgNumberOfShards+" to
        // "+maxAvgNumberOfShards+")");
        assertThat(
            node.shardsWithState(index, STARTED).size(),
            Matchers.greaterThanOrEqualTo(minAvgNumberOfShards));
        assertThat(
            node.shardsWithState(index, STARTED).size(),
            Matchers.lessThanOrEqualTo(maxAvgNumberOfShards));
      }
    }
  }