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