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.List;
021import java.util.Set;
022import org.apache.hadoop.hbase.client.RegionInfo;
023import org.apache.hadoop.hbase.master.RegionPlan;
024
025abstract class TableIsolationConditional extends RegionPlanConditional {
026
027  private final List<RegionPlanConditionalCandidateGenerator> candidateGenerators;
028
029  TableIsolationConditional(TableIsolationCandidateGenerator generator,
030    BalancerConditionals balancerConditionals, BalancerClusterState cluster) {
031    super(balancerConditionals.getConf(), cluster);
032
033    this.candidateGenerators =
034      List.of(generator, new SlopFixingCandidateGenerator(balancerConditionals));
035  }
036
037  abstract boolean isRegionToIsolate(RegionInfo regionInfo);
038
039  boolean isServerHostingIsolatedTables(BalancerClusterState cluster, int serverIdx) {
040    for (int regionIdx : cluster.regionsPerServer[serverIdx]) {
041      if (isRegionToIsolate(cluster.regions[regionIdx])) {
042        return true;
043      }
044    }
045    return false;
046  }
047
048  @Override
049  ValidationLevel getValidationLevel() {
050    return ValidationLevel.SERVER;
051  }
052
053  @Override
054  List<RegionPlanConditionalCandidateGenerator> getCandidateGenerators() {
055    return candidateGenerators;
056  }
057
058  @Override
059  public boolean isViolatingServer(RegionPlan regionPlan, Set<RegionInfo> serverRegions) {
060    RegionInfo regionBeingMoved = regionPlan.getRegionInfo();
061    boolean shouldIsolateMovingRegion = isRegionToIsolate(regionBeingMoved);
062    for (RegionInfo destinationRegion : serverRegions) {
063      if (destinationRegion.getEncodedName().equals(regionBeingMoved.getEncodedName())) {
064        // Skip the region being moved
065        continue;
066      }
067      if (shouldIsolateMovingRegion && !isRegionToIsolate(destinationRegion)) {
068        // Ensure every destination region is also a region to isolate
069        return true;
070      } else if (!shouldIsolateMovingRegion && isRegionToIsolate(destinationRegion)) {
071        // Ensure no destination region is a region to isolate
072        return true;
073      }
074    }
075    return false;
076  }
077
078}