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