View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.chaos.actions;
20  
21  import java.util.ArrayList;
22  import java.util.LinkedList;
23  import java.util.List;
24  
25  import org.apache.commons.lang.math.RandomUtils;
26  import org.apache.hadoop.hbase.ClusterStatus;
27  import org.apache.hadoop.hbase.ServerName;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  /**
32  * Action that tries to unbalance the regions of a cluster.
33  */
34  public class UnbalanceRegionsAction extends Action {
35    private static final Logger LOG =
36        LoggerFactory.getLogger(UnbalanceRegionsAction.class);
37    private double fractionOfRegions;
38    private double fractionOfServers;
39  
40    /**
41     * Unbalances the regions on the cluster by choosing "target" servers, and moving
42     * some regions from each of the non-target servers to random target servers.
43     * @param fractionOfRegions Fraction of regions to move from each server.
44     * @param fractionOfServers Fraction of servers to be chosen as targets.
45     */
46    public UnbalanceRegionsAction(double fractionOfRegions, double fractionOfServers) {
47      this.fractionOfRegions = fractionOfRegions;
48      this.fractionOfServers = fractionOfServers;
49    }
50  
51    @Override protected Logger getLogger() {
52      return LOG;
53    }
54  
55    @Override
56    public void perform() throws Exception {
57      getLogger().info("Unbalancing regions");
58      ClusterStatus status = this.cluster.getClusterStatus();
59      List<ServerName> victimServers = new LinkedList<ServerName>(status.getServers());
60      int targetServerCount = (int)Math.ceil(fractionOfServers * victimServers.size());
61      List<ServerName> targetServers = new ArrayList<ServerName>(targetServerCount);
62      for (int i = 0; i < targetServerCount; ++i) {
63        int victimIx = RandomUtils.nextInt(victimServers.size());
64        targetServers.add(victimServers.remove(victimIx));
65      }
66      unbalanceRegions(status, victimServers, targetServers, fractionOfRegions);
67    }
68  }