View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.catalog;
20  
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import org.apache.hadoop.hbase.Cell;
27  import org.apache.hadoop.hbase.HConstants;
28  import org.apache.hadoop.hbase.HRegionInfo;
29  import org.apache.hadoop.hbase.KeyValue;
30  import org.apache.hadoop.hbase.ServerName;
31  import org.apache.hadoop.hbase.client.Result;
32  import org.apache.hadoop.hbase.util.Bytes;
33  
34  /**
35   * Mocking utility for common hbase:meta functionality
36   */
37  public class MetaMockingUtil {
38  
39    /**
40     * Returns a Result object constructed from the given region information simulating
41     * a catalog table result.
42     * @param region the HRegionInfo object or null
43     * @return A mocked up Result that fakes a Get on a row in the <code>hbase:meta</code> table.
44     * @throws IOException
45     */
46    public static Result getMetaTableRowResult(final HRegionInfo region)
47        throws IOException {
48      return getMetaTableRowResult(region, null, null, null);
49    }
50  
51    /**
52     * Returns a Result object constructed from the given region information simulating
53     * a catalog table result.
54     * @param region the HRegionInfo object or null
55     * @param ServerName to use making startcode and server hostname:port in meta or null
56     * @return A mocked up Result that fakes a Get on a row in the <code>hbase:meta</code> table.
57     * @throws IOException
58     */
59    public static Result getMetaTableRowResult(final HRegionInfo region, final ServerName sn)
60        throws IOException {
61      return getMetaTableRowResult(region, sn, null, null);
62    }
63  
64    /**
65     * Returns a Result object constructed from the given region information simulating
66     * a catalog table result.
67     * @param region the HRegionInfo object or null
68     * @param ServerName to use making startcode and server hostname:port in meta or null
69     * @param splita daughter region or null
70     * @param splitb  daughter region or null
71     * @return A mocked up Result that fakes a Get on a row in the <code>hbase:meta</code> table.
72     * @throws IOException
73     */
74    public static Result getMetaTableRowResult(HRegionInfo region, final ServerName sn,
75        HRegionInfo splita, HRegionInfo splitb) throws IOException {
76      List<Cell> kvs = new ArrayList<Cell>();
77      if (region != null) {
78        kvs.add(new KeyValue(
79          region.getRegionName(),
80          HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER,
81          region.toByteArray()));
82      }
83  
84      if (sn != null) {
85        kvs.add(new KeyValue(region.getRegionName(),
86          HConstants.CATALOG_FAMILY, HConstants.SERVER_QUALIFIER,
87          Bytes.toBytes(sn.getHostAndPort())));
88        kvs.add(new KeyValue(region.getRegionName(),
89          HConstants.CATALOG_FAMILY, HConstants.STARTCODE_QUALIFIER,
90          Bytes.toBytes(sn.getStartcode())));
91      }
92  
93      if (splita != null) {
94        kvs.add(new KeyValue(
95            region.getRegionName(),
96            HConstants.CATALOG_FAMILY, HConstants.SPLITA_QUALIFIER,
97            splita.toByteArray()));
98      }
99  
100     if (splitb != null) {
101       kvs.add(new KeyValue(
102           region.getRegionName(),
103           HConstants.CATALOG_FAMILY, HConstants.SPLITB_QUALIFIER,
104           splitb.toByteArray()));
105     }
106 
107     //important: sort the kvs so that binary search work
108     Collections.sort(kvs, KeyValue.META_COMPARATOR);
109 
110     return Result.create(kvs);
111   }
112 
113   /**
114    * @param sn  ServerName to use making startcode and server in meta
115    * @param hri Region to serialize into HRegionInfo
116    * @return A mocked up Result that fakes a Get on a row in the <code>hbase:meta</code> table.
117    * @throws IOException
118    */
119   public static Result getMetaTableRowResultAsSplitRegion(final HRegionInfo hri, final ServerName sn)
120     throws IOException {
121     hri.setOffline(true);
122     hri.setSplit(true);
123     return getMetaTableRowResult(hri, sn);
124   }
125 
126 }