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.RegionPlan;
033import org.apache.yetus.audience.InterfaceAudience;
034
035/**
036 * a balancer which is only used in maintenance mode.
037 */
038@InterfaceAudience.Private
039public class MaintenanceLoadBalancer implements LoadBalancer {
040
041  private volatile boolean stopped = false;
042
043  @Override
044  public void stop(String why) {
045    stopped = true;
046  }
047
048  @Override
049  public boolean isStopped() {
050    return stopped;
051  }
052
053  @Override
054  public void updateClusterMetrics(ClusterMetrics st) {
055  }
056
057  @Override
058  public void setClusterInfoProvider(ClusterInfoProvider provider) {
059  }
060
061  @Override
062  public List<RegionPlan> balanceCluster(
063    Map<TableName, Map<ServerName, List<RegionInfo>>> loadOfAllTable) throws IOException {
064    // do not need to balance in maintenance mode
065    return Collections.emptyList();
066  }
067
068  private Map<ServerName, List<RegionInfo>> assign(Collection<RegionInfo> regions,
069    List<ServerName> servers) {
070    // should only have 1 region server in maintenance mode
071    assert servers.size() == 1;
072    List<RegionInfo> systemRegions =
073      regions.stream().filter(r -> r.getTable().isSystemTable()).collect(Collectors.toList());
074    if (!systemRegions.isEmpty()) {
075      return Collections.singletonMap(servers.get(0), systemRegions);
076    } else {
077      return Collections.emptyMap();
078    }
079  }
080
081  @Override
082  public Map<ServerName, List<RegionInfo>> roundRobinAssignment(List<RegionInfo> regions,
083    List<ServerName> servers) throws IOException {
084    return assign(regions, servers);
085  }
086
087  @Override
088  public Map<ServerName, List<RegionInfo>> retainAssignment(Map<RegionInfo, ServerName> regions,
089    List<ServerName> servers) throws IOException {
090    return assign(regions.keySet(), servers);
091  }
092
093  @Override
094  public ServerName randomAssignment(RegionInfo regionInfo, List<ServerName> servers)
095    throws IOException {
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}