1   /**
2    * Copyright 2007 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase;
21  
22  import java.io.IOException;
23  
24  import org.apache.hadoop.hbase.regionserver.HRegion;
25  import org.apache.hadoop.hbase.util.Bytes;
26  
27  /**
28   * Utility class to build a table of multiple regions.
29   */
30  public class MultiRegionTable extends HBaseClusterTestCase {
31    protected static final byte [][] KEYS = {
32      HConstants.EMPTY_BYTE_ARRAY,
33      Bytes.toBytes("bbb"),
34      Bytes.toBytes("ccc"),
35      Bytes.toBytes("ddd"),
36      Bytes.toBytes("eee"),
37      Bytes.toBytes("fff"),
38      Bytes.toBytes("ggg"),
39      Bytes.toBytes("hhh"),
40      Bytes.toBytes("iii"),
41      Bytes.toBytes("jjj"),
42      Bytes.toBytes("kkk"),
43      Bytes.toBytes("lll"),
44      Bytes.toBytes("mmm"),
45      Bytes.toBytes("nnn"),
46      Bytes.toBytes("ooo"),
47      Bytes.toBytes("ppp"),
48      Bytes.toBytes("qqq"),
49      Bytes.toBytes("rrr"),
50      Bytes.toBytes("sss"),
51      Bytes.toBytes("ttt"),
52      Bytes.toBytes("uuu"),
53      Bytes.toBytes("vvv"),
54      Bytes.toBytes("www"),
55      Bytes.toBytes("xxx"),
56      Bytes.toBytes("yyy")
57    };
58  
59    protected final byte [] columnFamily;
60    protected HTableDescriptor desc;
61  
62    /**
63     * @param familyName the family to populate.
64     */
65    public MultiRegionTable(final String familyName) {
66      this(1, familyName);
67    }
68  
69    public MultiRegionTable(int nServers, final String familyName) {
70      super(nServers);
71  
72       this.columnFamily = Bytes.toBytes(familyName);
73      // These are needed for the new and improved Map/Reduce framework
74      System.setProperty("hadoop.log.dir", conf.get("hadoop.log.dir"));
75      conf.set("mapred.output.dir", conf.get("hadoop.tmp.dir"));
76    }
77  
78    /**
79     * Run after dfs is ready but before hbase cluster is started up.
80     */
81    @Override
82    protected void preHBaseClusterSetup() throws Exception {
83      try {
84        // Create a bunch of regions
85        HRegion[] regions = new HRegion[KEYS.length];
86        for (int i = 0; i < regions.length; i++) {
87          int j = (i + 1) % regions.length;
88          regions[i] = createARegion(KEYS[i], KEYS[j]);
89        }
90  
91        // Now create the root and meta regions and insert the data regions
92        // created above into the meta
93  
94        createRootAndMetaRegions();
95  
96        for(int i = 0; i < regions.length; i++) {
97          HRegion.addRegionToMETA(meta, regions[i]);
98        }
99  
100       closeRootAndMeta();
101     } catch (Exception e) {
102       shutdownDfs(dfsCluster);
103       throw e;
104     }
105   }
106 
107   private HRegion createARegion(byte [] startKey, byte [] endKey) throws IOException {
108     HRegion region = createNewHRegion(desc, startKey, endKey);
109     addContent(region, this.columnFamily);
110     closeRegionAndDeleteLog(region);
111     return region;
112   }
113 
114   private void closeRegionAndDeleteLog(HRegion region) throws IOException {
115     region.close();
116     region.getLog().closeAndDelete();
117   }
118 }