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