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; 024 025import org.apache.commons.lang3.RandomUtils; 026import org.apache.hadoop.hbase.ClusterMetrics; 027import org.apache.hadoop.hbase.ServerName; 028import org.slf4j.Logger; 029import org.slf4j.LoggerFactory; 030 031/** 032* Action that tries to unbalance the regions of a cluster. 033*/ 034public class UnbalanceRegionsAction extends Action { 035 private static final Logger LOG = 036 LoggerFactory.getLogger(UnbalanceRegionsAction.class); 037 private double fractionOfRegions; 038 private double fractionOfServers; 039 040 /** 041 * Unbalances the regions on the cluster by choosing "target" servers, and moving 042 * some regions from each of the non-target servers to random target servers. 043 * @param fractionOfRegions Fraction of regions to move from each server. 044 * @param fractionOfServers Fraction of servers to be chosen as targets. 045 */ 046 public UnbalanceRegionsAction(double fractionOfRegions, double fractionOfServers) { 047 this.fractionOfRegions = fractionOfRegions; 048 this.fractionOfServers = fractionOfServers; 049 } 050 051 @Override 052 public void perform() throws Exception { 053 LOG.info("Unbalancing regions"); 054 ClusterMetrics status = this.cluster.getClusterMetrics(); 055 List<ServerName> victimServers = new LinkedList<>(status.getLiveServerMetrics().keySet()); 056 int targetServerCount = (int)Math.ceil(fractionOfServers * victimServers.size()); 057 List<ServerName> targetServers = new ArrayList<>(targetServerCount); 058 for (int i = 0; i < targetServerCount; ++i) { 059 int victimIx = RandomUtils.nextInt(0, victimServers.size()); 060 targetServers.add(victimServers.remove(victimIx)); 061 } 062 unbalanceRegions(status, victimServers, targetServers, fractionOfRegions); 063 } 064}