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.conf.Configuration;
023import org.apache.hadoop.hbase.client.RegionInfo;
024import org.apache.hadoop.hbase.master.RegionPlan;
025import org.apache.hadoop.hbase.master.balancer.replicas.ReplicaKey;
026import org.apache.hadoop.hbase.master.balancer.replicas.ReplicaKeyCache;
027import org.apache.yetus.audience.InterfaceAudience;
028
029import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableList;
030
031/**
032 * If enabled, this class will help the balancer ensure that replicas aren't placed on the same
033 * servers or racks as their primary. Configure this via
034 * {@link BalancerConditionals#DISTRIBUTE_REPLICAS_KEY}
035 */
036@InterfaceAudience.Private
037public class DistributeReplicasConditional extends RegionPlanConditional {
038
039  private final List<RegionPlanConditionalCandidateGenerator> candidateGenerators;
040
041  public DistributeReplicasConditional(BalancerConditionals balancerConditionals,
042    BalancerClusterState cluster) {
043    super(balancerConditionals.getConf(), cluster);
044    Configuration conf = balancerConditionals.getConf();
045    float slop =
046      conf.getFloat(BaseLoadBalancer.REGIONS_SLOP_KEY, BaseLoadBalancer.REGIONS_SLOP_DEFAULT);
047    this.candidateGenerators =
048      ImmutableList.of(new DistributeReplicasCandidateGenerator(balancerConditionals),
049        new SlopFixingCandidateGenerator(balancerConditionals, slop));
050  }
051
052  @Override
053  public ValidationLevel getValidationLevel() {
054    return ValidationLevel.SERVER_HOST_RACK;
055  }
056
057  @Override
058  List<RegionPlanConditionalCandidateGenerator> getCandidateGenerators() {
059    return candidateGenerators;
060  }
061
062  @Override
063  boolean isViolatingServer(RegionPlan regionPlan, Set<RegionInfo> serverRegions) {
064    return checkViolation(regionPlan.getRegionInfo(), getReplicaKey(regionPlan.getRegionInfo()),
065      serverRegions);
066  }
067
068  @Override
069  boolean isViolatingHost(RegionPlan regionPlan, Set<RegionInfo> hostRegions) {
070    return checkViolation(regionPlan.getRegionInfo(), getReplicaKey(regionPlan.getRegionInfo()),
071      hostRegions);
072  }
073
074  @Override
075  boolean isViolatingRack(RegionPlan regionPlan, Set<RegionInfo> rackRegions) {
076    return checkViolation(regionPlan.getRegionInfo(), getReplicaKey(regionPlan.getRegionInfo()),
077      rackRegions);
078  }
079
080  private boolean checkViolation(RegionInfo movingRegion, ReplicaKey movingReplicaKey,
081    Set<RegionInfo> destinationRegions) {
082    for (RegionInfo regionInfo : destinationRegions) {
083      if (regionInfo.equals(movingRegion)) {
084        continue;
085      }
086      if (getReplicaKey(regionInfo).equals(movingReplicaKey)) {
087        return true;
088      }
089    }
090    return false;
091  }
092
093  static ReplicaKey getReplicaKey(RegionInfo regionInfo) {
094    return ReplicaKeyCache.getInstance().getReplicaKey(regionInfo);
095  }
096
097}