001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019package org.apache.hadoop.hbase.chaos.actions; 020 021import java.util.ArrayList; 022import java.util.LinkedList; 023import java.util.List; 024import org.apache.commons.lang3.RandomUtils; 025import org.apache.hadoop.hbase.ClusterMetrics; 026import org.apache.hadoop.hbase.ServerName; 027 028/** 029* Action that tries to unbalance the regions of a cluster. 030*/ 031public class UnbalanceRegionsAction extends Action { 032 private double fractionOfRegions; 033 private double fractionOfServers; 034 035 /** 036 * Unbalances the regions on the cluster by choosing "target" servers, and moving 037 * some regions from each of the non-target servers to random target servers. 038 * @param fractionOfRegions Fraction of regions to move from each server. 039 * @param fractionOfServers Fraction of servers to be chosen as targets. 040 */ 041 public UnbalanceRegionsAction(double fractionOfRegions, double fractionOfServers) { 042 this.fractionOfRegions = fractionOfRegions; 043 this.fractionOfServers = fractionOfServers; 044 } 045 046 @Override 047 public void perform() throws Exception { 048 LOG.info("Unbalancing regions"); 049 ClusterMetrics status = this.cluster.getClusterMetrics(); 050 List<ServerName> victimServers = new LinkedList<>(status.getLiveServerMetrics().keySet()); 051 int targetServerCount = (int)Math.ceil(fractionOfServers * victimServers.size()); 052 List<ServerName> targetServers = new ArrayList<>(targetServerCount); 053 for (int i = 0; i < targetServerCount; ++i) { 054 int victimIx = RandomUtils.nextInt(0, victimServers.size()); 055 targetServers.add(victimServers.remove(victimIx)); 056 } 057 unbalanceRegions(status, victimServers, targetServers, fractionOfRegions); 058 } 059}