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.normalizer;
019
020import org.apache.hadoop.conf.Configuration;
021import org.apache.hadoop.hbase.HConstants;
022import org.apache.hadoop.hbase.TableName;
023import org.apache.hadoop.hbase.master.HMaster;
024import org.apache.hadoop.hbase.zookeeper.RegionNormalizerTracker;
025import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
026import org.apache.hadoop.util.ReflectionUtils;
027import org.apache.yetus.audience.InterfaceAudience;
028
029/**
030 * Factory to create instance of {@link RegionNormalizer} as configured.
031 */
032@InterfaceAudience.Private
033public final class RegionNormalizerFactory {
034
035  private RegionNormalizerFactory() {
036  }
037
038  public static RegionNormalizerManager createNormalizerManager(final Configuration conf,
039    final ZKWatcher zkWatcher, final HMaster master // TODO: consolidate this down to MasterServices
040  ) {
041    final RegionNormalizer regionNormalizer = getRegionNormalizer(conf);
042    regionNormalizer.setMasterServices(master);
043    final RegionNormalizerTracker tracker = new RegionNormalizerTracker(zkWatcher, master);
044    final RegionNormalizerChore chore =
045      master.isInMaintenanceMode() ? null : new RegionNormalizerChore(master);
046    final RegionNormalizerWorkQueue<TableName> workQueue =
047      master.isInMaintenanceMode() ? null : new RegionNormalizerWorkQueue<>();
048    final RegionNormalizerWorker worker = master.isInMaintenanceMode()
049      ? null
050      : new RegionNormalizerWorker(conf, master, regionNormalizer, workQueue);
051    return new RegionNormalizerManager(tracker, chore, workQueue, worker);
052  }
053
054  /**
055   * Create a region normalizer from the given conf.
056   * @param conf configuration
057   * @return {@link RegionNormalizer} implementation
058   */
059  private static RegionNormalizer getRegionNormalizer(Configuration conf) {
060    // Create instance of Region Normalizer
061    Class<? extends RegionNormalizer> balancerKlass =
062      conf.getClass(HConstants.HBASE_MASTER_NORMALIZER_CLASS, SimpleRegionNormalizer.class,
063        RegionNormalizer.class);
064    return ReflectionUtils.newInstance(balancerKlass, conf);
065  }
066}