001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase.master.normalizer;
020
021import java.io.IOException;
022
023import org.apache.hadoop.hbase.client.Admin;
024import org.apache.hadoop.hbase.client.RegionInfo;
025import org.apache.yetus.audience.InterfaceAudience;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029/**
030 * Normalization plan to merge regions (smallest region in the table with its smallest neighbor).
031 */
032@InterfaceAudience.Private
033public class MergeNormalizationPlan implements NormalizationPlan {
034  private static final Logger LOG = LoggerFactory.getLogger(MergeNormalizationPlan.class.getName());
035
036  private final RegionInfo firstRegion;
037  private final RegionInfo secondRegion;
038
039  public MergeNormalizationPlan(RegionInfo firstRegion, RegionInfo secondRegion) {
040    this.firstRegion = firstRegion;
041    this.secondRegion = secondRegion;
042  }
043
044  @Override
045  public PlanType getType() {
046    return PlanType.MERGE;
047  }
048
049  RegionInfo getFirstRegion() {
050    return firstRegion;
051  }
052
053  RegionInfo getSecondRegion() {
054    return secondRegion;
055  }
056
057  @Override
058  public String toString() {
059    return "MergeNormalizationPlan{" +
060      "firstRegion=" + firstRegion +
061      ", secondRegion=" + secondRegion +
062      '}';
063  }
064
065  /**
066   * {@inheritDoc}
067   */
068  @Override
069  public void execute(Admin admin) {
070    LOG.info("Executing merging normalization plan: " + this);
071    try {
072      // Do not use force=true as corner cases can happen, non adjacent regions,
073      // merge with a merged child region with no GC done yet, it is going to
074      // cause all different issues.
075      admin.mergeRegionsAsync(firstRegion.getEncodedNameAsBytes(),
076        secondRegion.getEncodedNameAsBytes(), false);
077    } catch (IOException ex) {
078      LOG.error("Error during region merge: ", ex);
079    }
080  }
081}