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.io.IOException; 022import java.util.Collection; 023import java.util.Collections; 024import java.util.EnumSet; 025import java.util.List; 026 027import org.apache.commons.lang3.RandomUtils; 028import org.apache.hadoop.hbase.ClusterMetrics.Option; 029import org.apache.hadoop.hbase.HRegionInfo; 030import org.apache.hadoop.hbase.ServerName; 031import org.apache.hadoop.hbase.TableName; 032import org.apache.hadoop.hbase.chaos.factories.MonkeyConstants; 033import org.apache.hadoop.hbase.client.Admin; 034import org.apache.hadoop.hbase.client.RegionInfo; 035import org.apache.hadoop.hbase.util.Bytes; 036import org.slf4j.Logger; 037import org.slf4j.LoggerFactory; 038 039/** 040* Action that tries to move every region of a table. 041*/ 042public class MoveRegionsOfTableAction extends Action { 043 private static final Logger LOG = 044 LoggerFactory.getLogger(MoveRegionsOfTableAction.class); 045 private final long sleepTime; 046 private final TableName tableName; 047 private final long maxTime; 048 049 public MoveRegionsOfTableAction(TableName tableName) { 050 this(-1, MonkeyConstants.DEFAULT_MOVE_REGIONS_MAX_TIME, tableName); 051 } 052 053 public MoveRegionsOfTableAction(long sleepTime, long maxSleepTime, TableName tableName) { 054 this.sleepTime = sleepTime; 055 this.tableName = tableName; 056 this.maxTime = maxSleepTime; 057 } 058 059 @Override 060 public void perform() throws Exception { 061 if (sleepTime > 0) { 062 Thread.sleep(sleepTime); 063 } 064 065 Admin admin = this.context.getHBaseIntegrationTestingUtility().getAdmin(); 066 ServerName[] servers = getServers(admin); 067 068 LOG.info("Performing action: Move regions of table {}", tableName); 069 List<HRegionInfo> regions = admin.getTableRegions(tableName); 070 if (regions == null || regions.isEmpty()) { 071 LOG.info("Table {} doesn't have regions to move", tableName); 072 return; 073 } 074 075 Collections.shuffle(regions); 076 077 long start = System.currentTimeMillis(); 078 for (HRegionInfo regionInfo:regions) { 079 080 // Don't try the move if we're stopping 081 if (context.isStopping()) { 082 return; 083 } 084 085 moveRegion(admin, servers, regionInfo); 086 if (sleepTime > 0) { 087 Thread.sleep(sleepTime); 088 } 089 090 // put a limit on max num regions. Otherwise, this won't finish 091 // with a sleep time of 10sec, 100 regions will finish in 16min 092 if (System.currentTimeMillis() - start > maxTime) { 093 break; 094 } 095 } 096 } 097 098 static ServerName [] getServers(Admin admin) throws IOException { 099 Collection<ServerName> serversList = 100 admin.getClusterMetrics(EnumSet.of(Option.LIVE_SERVERS)).getLiveServerMetrics().keySet(); 101 return serversList.toArray(new ServerName[serversList.size()]); 102 } 103 104 static void moveRegion(Admin admin, ServerName [] servers, RegionInfo regionInfo) { 105 try { 106 String destServerName = servers[RandomUtils.nextInt(0, servers.length)].getServerName(); 107 LOG.debug("Moving {} to {}", regionInfo.getRegionNameAsString(), destServerName); 108 admin.move(regionInfo.getEncodedNameAsBytes(), Bytes.toBytes(destServerName)); 109 } catch (Exception ex) { 110 LOG.warn("Move failed, might be caused by other chaos: {}", ex.getMessage()); 111 } 112 } 113}