View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.master.normalizer;
20  
21  import java.io.IOException;
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.hadoop.hbase.HRegionInfo;
25  import org.apache.hadoop.hbase.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.client.Admin;
27  
28  /**
29   * Normalization plan to merge regions (smallest region in the table with its smallest neighbor).
30   */
31  @InterfaceAudience.Private
32  public class MergeNormalizationPlan implements NormalizationPlan {
33    private static final Log LOG = LogFactory.getLog(MergeNormalizationPlan.class.getName());
34  
35    private final HRegionInfo firstRegion;
36    private final HRegionInfo secondRegion;
37  
38    public MergeNormalizationPlan(HRegionInfo firstRegion, HRegionInfo secondRegion) {
39      this.firstRegion = firstRegion;
40      this.secondRegion = secondRegion;
41    }
42  
43    @Override
44    public PlanType getType() {
45      return PlanType.MERGE;
46    }
47  
48    HRegionInfo getFirstRegion() {
49      return firstRegion;
50    }
51  
52    HRegionInfo getSecondRegion() {
53      return secondRegion;
54    }
55  
56    @Override
57    public String toString() {
58      return "MergeNormalizationPlan{" +
59        "firstRegion=" + firstRegion +
60        ", secondRegion=" + secondRegion +
61        '}';
62    }
63  
64    /**
65     * {@inheritDoc}
66     */
67    @Override
68    public void execute(Admin admin) {
69      LOG.info("Executing merging normalization plan: " + this);
70      try {
71        admin.mergeRegions(firstRegion.getEncodedNameAsBytes(),
72          secondRegion.getEncodedNameAsBytes(), false);
73      } catch (IOException ex) {
74        LOG.error("Error during region merge of " + firstRegion.getEncodedName() + " and "
75            + secondRegion.getEncodedName() + " : ", ex);
76      }
77    }
78  }