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(BalancerConditionals balancerConditionals,
030    BalancerClusterState cluster) {
031    super(balancerConditionals.getConf(), cluster);
032
033    float slop = balancerConditionals.getConf().getFloat(BaseLoadBalancer.REGIONS_SLOP_KEY,
034      BaseLoadBalancer.REGIONS_SLOP_DEFAULT);
035    this.candidateGenerators =
036      List.of(new MetaTableIsolationCandidateGenerator(balancerConditionals),
037        new SlopFixingCandidateGenerator(balancerConditionals, slop));
038  }
039
040  abstract boolean isRegionToIsolate(RegionInfo regionInfo);
041
042  boolean isServerHostingIsolatedTables(BalancerClusterState cluster, int serverIdx) {
043    for (int regionIdx : cluster.regionsPerServer[serverIdx]) {
044      if (isRegionToIsolate(cluster.regions[regionIdx])) {
045        return true;
046      }
047    }
048    return false;
049  }
050
051  @Override
052  ValidationLevel getValidationLevel() {
053    return ValidationLevel.SERVER;
054  }
055
056  @Override
057  List<RegionPlanConditionalCandidateGenerator> getCandidateGenerators() {
058    return candidateGenerators;
059  }
060
061  @Override
062  public boolean isViolatingServer(RegionPlan regionPlan, Set<RegionInfo> serverRegions) {
063    RegionInfo regionBeingMoved = regionPlan.getRegionInfo();
064    boolean shouldIsolateMovingRegion = isRegionToIsolate(regionBeingMoved);
065    for (RegionInfo destinationRegion : serverRegions) {
066      if (destinationRegion.getEncodedName().equals(regionBeingMoved.getEncodedName())) {
067        // Skip the region being moved
068        continue;
069      }
070      if (shouldIsolateMovingRegion && !isRegionToIsolate(destinationRegion)) {
071        // Ensure every destination region is also a region to isolate
072        return true;
073      } else if (!shouldIsolateMovingRegion && isRegionToIsolate(destinationRegion)) {
074        // Ensure no destination region is a region to isolate
075        return true;
076      }
077    }
078    return false;
079  }
080
081}