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 */
018package org.apache.hadoop.hbase.chaos.actions;
019
020import java.io.IOException;
021import java.util.Collection;
022import java.util.Collections;
023import java.util.List;
024import java.util.concurrent.ThreadLocalRandom;
025import org.apache.hadoop.hbase.ServerName;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.client.Admin;
028import org.apache.hadoop.hbase.client.RegionInfo;
029import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
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
049  protected Logger getLogger() {
050    return LOG;
051  }
052
053  @Override
054  public void perform() throws Exception {
055    if (sleepTime > 0) {
056      Thread.sleep(sleepTime);
057    }
058
059    Admin admin = this.context.getHBaseIntegrationTestingUtility().getAdmin();
060    ServerName[] servers = getServers(admin);
061
062    getLogger().info("Performing action: Move regions of table {}", tableName);
063    List<RegionInfo> regions = admin.getRegions(tableName);
064    if (regions == null || regions.isEmpty()) {
065      getLogger().info("Table {} doesn't have regions to move", tableName);
066      return;
067    }
068
069    Collections.shuffle(regions);
070
071    long start = EnvironmentEdgeManager.currentTime();
072    for (RegionInfo regionInfo : regions) {
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 (EnvironmentEdgeManager.currentTime() - 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[ThreadLocalRandom.current().nextInt(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}