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.io.IOException;
021import java.util.Collection;
022import java.util.Collections;
023import java.util.List;
024import java.util.Map;
025import java.util.stream.Collectors;
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.hbase.ClusterMetrics;
028import org.apache.hadoop.hbase.ServerName;
029import org.apache.hadoop.hbase.TableName;
030import org.apache.hadoop.hbase.client.RegionInfo;
031import org.apache.hadoop.hbase.master.LoadBalancer;
032import org.apache.hadoop.hbase.master.MasterServices;
033import org.apache.hadoop.hbase.master.RegionPlan;
034import org.apache.yetus.audience.InterfaceAudience;
035
036/**
037 * a balancer which is only used in maintenance mode.
038 */
039@InterfaceAudience.Private
040public class MaintenanceLoadBalancer implements LoadBalancer {
041
042  private volatile boolean stopped = false;
043
044  @Override
045  public void stop(String why) {
046    stopped = true;
047  }
048
049  @Override
050  public boolean isStopped() {
051    return stopped;
052  }
053
054  @Override
055  public void updateClusterMetrics(ClusterMetrics st) {
056  }
057
058  @Override
059  public void setMasterServices(MasterServices masterServices) {
060  }
061
062  @Override
063  public List<RegionPlan> balanceCluster(
064    Map<TableName, Map<ServerName, List<RegionInfo>>> loadOfAllTable) throws IOException {
065    // do not need to balance in maintenance mode
066    return Collections.emptyList();
067  }
068
069  private Map<ServerName, List<RegionInfo>> assign(Collection<RegionInfo> regions,
070    List<ServerName> servers) {
071    // should only have 1 region server in maintenance mode
072    assert servers.size() == 1;
073    List<RegionInfo> systemRegions =
074      regions.stream().filter(r -> r.getTable().isSystemTable()).collect(Collectors.toList());
075    if (!systemRegions.isEmpty()) {
076      return Collections.singletonMap(servers.get(0), systemRegions);
077    } else {
078      return Collections.emptyMap();
079    }
080  }
081
082  @Override
083  public Map<ServerName, List<RegionInfo>> roundRobinAssignment(List<RegionInfo> regions,
084    List<ServerName> servers) {
085    return assign(regions, servers);
086  }
087
088  @Override
089  public Map<ServerName, List<RegionInfo>> retainAssignment(Map<RegionInfo, ServerName> regions,
090    List<ServerName> servers) {
091    return assign(regions.keySet(), servers);
092  }
093
094  @Override
095  public ServerName randomAssignment(RegionInfo regionInfo, List<ServerName> servers) {
096    // should only have 1 region server in maintenance mode
097    assert servers.size() == 1;
098    return regionInfo.getTable().isSystemTable() ? servers.get(0) : null;
099  }
100
101  @Override
102  public void initialize() {
103  }
104
105  @Override
106  public void regionOnline(RegionInfo regionInfo, ServerName sn) {
107  }
108
109  @Override
110  public void regionOffline(RegionInfo regionInfo) {
111  }
112
113  @Override
114  public void onConfigurationChange(Configuration conf) {
115  }
116
117  @Override
118  public void postMasterStartupInitialize() {
119  }
120
121  @Override
122  public void updateBalancerStatus(boolean status) {
123  }
124}