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 org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.hadoop.hbase.HRegionInfo;
24  import org.apache.hadoop.hbase.classification.InterfaceAudience;
25  import org.apache.hadoop.hbase.client.Admin;
26  
27  import java.io.IOException;
28  
29  /**
30   * Normalization plan to merge regions (smallest region in the table with its smallest neighbor).
31   */
32  @InterfaceAudience.Private
33  public class MergeNormalizationPlan implements NormalizationPlan {
34    private static final Log LOG = LogFactory.getLog(MergeNormalizationPlan.class.getName());
35  
36    private final HRegionInfo firstRegion;
37    private final HRegionInfo secondRegion;
38  
39    public MergeNormalizationPlan(HRegionInfo firstRegion, HRegionInfo secondRegion) {
40      this.firstRegion = firstRegion;
41      this.secondRegion = secondRegion;
42    }
43  
44    @Override
45    public PlanType getType() {
46      return PlanType.MERGE;
47    }
48  
49    HRegionInfo getFirstRegion() {
50      return firstRegion;
51    }
52  
53    HRegionInfo getSecondRegion() {
54      return secondRegion;
55    }
56  
57    @Override
58    public String toString() {
59      return "MergeNormalizationPlan{" +
60        "firstRegion=" + firstRegion +
61        ", secondRegion=" + secondRegion +
62        '}';
63    }
64  
65    /**
66     * {@inheritDoc}
67     */
68    @Override
69    public void execute(Admin admin) {
70      LOG.info("Executing merging normalization plan: " + this);
71      try {
72        admin.mergeRegions(firstRegion.getEncodedNameAsBytes(),
73          secondRegion.getEncodedNameAsBytes(), true);
74      } catch (IOException ex) {
75        LOG.error("Error during region merge: ", ex);
76      }
77    }
78  }