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