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.HConstants;
27  import org.apache.hadoop.hbase.HRegionInfo;
28  import org.apache.hadoop.hbase.KeyValue;
29  import org.apache.hadoop.hbase.ServerName;
30  import org.apache.hadoop.hbase.client.Result;
31  import org.apache.hadoop.hbase.util.Bytes;
32  
33  /**
34   * Mocking utility for common META functionality
35   */
36  public class MetaMockingUtil {
37  
38    /**
39     * Returns a Result object constructed from the given region information simulating
40     * a catalog table result.
41     * @param region the HRegionInfo object or null
42     * @return A mocked up Result that fakes a Get on a row in the <code>.META.</code> table.
43     * @throws IOException
44     */
45    public static Result getMetaTableRowResult(final HRegionInfo region)
46        throws IOException {
47      return getMetaTableRowResult(region, null, null, null);
48    }
49  
50    /**
51     * Returns a Result object constructed from the given region information simulating
52     * a catalog table result.
53     * @param region the HRegionInfo object or null
54     * @param ServerName to use making startcode and server hostname:port in meta or null
55     * @return A mocked up Result that fakes a Get on a row in the <code>.META.</code> table.
56     * @throws IOException
57     */
58    public static Result getMetaTableRowResult(final HRegionInfo region, final ServerName sn)
59        throws IOException {
60      return getMetaTableRowResult(region, sn, null, null);
61    }
62  
63    /**
64     * Returns a Result object constructed from the given region information simulating
65     * a catalog table result.
66     * @param region the HRegionInfo object or null
67     * @param ServerName to use making startcode and server hostname:port in meta or null
68     * @param splita daughter region or null
69     * @param splitb  daughter region or null
70     * @return A mocked up Result that fakes a Get on a row in the <code>.META.</code> table.
71     * @throws IOException
72     */
73    public static Result getMetaTableRowResult(HRegionInfo region, final ServerName sn,
74        HRegionInfo splita, HRegionInfo splitb) throws IOException {
75      List<KeyValue> kvs = new ArrayList<KeyValue>();
76      if (region != null) {
77        kvs.add(new KeyValue(
78          region.getRegionName(),
79          HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER,
80          region.toByteArray()));
81      }
82  
83      if (sn != null) {
84        kvs.add(new KeyValue(region.getRegionName(),
85          HConstants.CATALOG_FAMILY, HConstants.SERVER_QUALIFIER,
86          Bytes.toBytes(sn.getHostAndPort())));
87        kvs.add(new KeyValue(region.getRegionName(),
88          HConstants.CATALOG_FAMILY, HConstants.STARTCODE_QUALIFIER,
89          Bytes.toBytes(sn.getStartcode())));
90      }
91  
92      if (splita != null) {
93        kvs.add(new KeyValue(
94            region.getRegionName(),
95            HConstants.CATALOG_FAMILY, HConstants.SPLITA_QUALIFIER,
96            splita.toByteArray()));
97      }
98  
99      if (splitb != null) {
100       kvs.add(new KeyValue(
101           region.getRegionName(),
102           HConstants.CATALOG_FAMILY, HConstants.SPLITB_QUALIFIER,
103           splitb.toByteArray()));
104     }
105 
106     //important: sort the kvs so that binary search work
107     Collections.sort(kvs, KeyValue.META_COMPARATOR);
108 
109     return new Result(kvs);
110   }
111 
112   /**
113    * @param sn  ServerName to use making startcode and server in meta
114    * @param hri Region to serialize into HRegionInfo
115    * @return A mocked up Result that fakes a Get on a row in the <code>.META.</code> table.
116    * @throws IOException
117    */
118   public static Result getMetaTableRowResultAsSplitRegion(final HRegionInfo hri, final ServerName sn)
119     throws IOException {
120     hri.setOffline(true);
121     hri.setSplit(true);
122     return getMetaTableRowResult(hri, sn);
123   }
124 
125 }