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.master.balancer;
019
020import java.util.ArrayList;
021import java.util.HashSet;
022import java.util.List;
023import java.util.Set;
024import org.apache.hadoop.hbase.client.RegionInfo;
025import org.apache.yetus.audience.InterfaceAudience;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029@InterfaceAudience.Private
030public abstract class TableIsolationCandidateGenerator
031  extends RegionPlanConditionalCandidateGenerator {
032
033  private static final Logger LOG = LoggerFactory.getLogger(TableIsolationCandidateGenerator.class);
034
035  TableIsolationCandidateGenerator(BalancerConditionals balancerConditionals) {
036    super(balancerConditionals);
037  }
038
039  abstract boolean shouldBeIsolated(RegionInfo regionInfo);
040
041  @Override
042  BalanceAction generate(BalancerClusterState cluster) {
043    return generateCandidate(cluster, false);
044  }
045
046  BalanceAction generateCandidate(BalancerClusterState cluster, boolean isWeighing) {
047    if (!getBalancerConditionals().isTableIsolationEnabled()) {
048      return BalanceAction.NULL_ACTION;
049    }
050
051    List<MoveRegionAction> moves = new ArrayList<>();
052    List<Integer> serverIndicesHoldingIsolatedRegions = new ArrayList<>();
053    int isolatedTableMaxReplicaCount = 1;
054    for (int serverIdx : cluster.getShuffledServerIndices()) {
055      if (cluster.isStopRequested()) {
056        break;
057      }
058      boolean hasRegionsToIsolate = false;
059      Set<Integer> regionsToMove = new HashSet<>();
060
061      // Move non-target regions away from target regions,
062      // and track replica counts so we know how many isolated hosts we need
063      for (int regionIdx : cluster.regionsPerServer[serverIdx]) {
064        RegionInfo regionInfo = cluster.regions[regionIdx];
065        if (shouldBeIsolated(regionInfo)) {
066          hasRegionsToIsolate = true;
067          int replicaCount = regionInfo.getReplicaId() + 1;
068          if (replicaCount > isolatedTableMaxReplicaCount) {
069            isolatedTableMaxReplicaCount = replicaCount;
070          }
071        } else {
072          regionsToMove.add(regionIdx);
073        }
074      }
075
076      if (hasRegionsToIsolate) {
077        serverIndicesHoldingIsolatedRegions.add(serverIdx);
078      }
079
080      // Generate non-system regions to move, if applicable
081      if (hasRegionsToIsolate && !regionsToMove.isEmpty()) {
082        for (int regionToMove : regionsToMove) {
083          for (int i = 0; i < cluster.numServers; i++) {
084            int targetServer = pickOtherRandomServer(cluster, serverIdx);
085            MoveRegionAction possibleMove =
086              new MoveRegionAction(regionToMove, serverIdx, targetServer);
087            if (!getBalancerConditionals().isViolating(cluster, possibleMove)) {
088              if (isWeighing) {
089                return possibleMove;
090              }
091              cluster.doAction(possibleMove); // Update cluster state to reflect move
092              moves.add(possibleMove);
093              break;
094            }
095          }
096        }
097      }
098    }
099
100    // Try to consolidate regions on only n servers, where n is the number of replicas
101    if (serverIndicesHoldingIsolatedRegions.size() > isolatedTableMaxReplicaCount) {
102      // One target per replica
103      List<Integer> targetServerIndices = new ArrayList<>();
104      for (int i = 0; i < isolatedTableMaxReplicaCount; i++) {
105        targetServerIndices.add(serverIndicesHoldingIsolatedRegions.get(i));
106      }
107      // Move all isolated regions from non-targets to targets
108      for (int i = isolatedTableMaxReplicaCount; i
109          < serverIndicesHoldingIsolatedRegions.size(); i++) {
110        int fromServer = serverIndicesHoldingIsolatedRegions.get(i);
111        for (int regionIdx : cluster.regionsPerServer[fromServer]) {
112          RegionInfo regionInfo = cluster.regions[regionIdx];
113          if (shouldBeIsolated(regionInfo)) {
114            int targetServer = targetServerIndices.get(i % isolatedTableMaxReplicaCount);
115            MoveRegionAction possibleMove =
116              new MoveRegionAction(regionIdx, fromServer, targetServer);
117            if (!getBalancerConditionals().isViolating(cluster, possibleMove)) {
118              if (isWeighing) {
119                return possibleMove;
120              }
121              cluster.doAction(possibleMove); // Update cluster state to reflect move
122              moves.add(possibleMove);
123            }
124          }
125        }
126      }
127    }
128    return batchMovesAndResetClusterState(cluster, moves);
129  }
130}