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