1   /*
2    * Copyright 2009 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  
21  package org.apache.hadoop.hbase.master;
22  
23  import org.apache.hadoop.hbase.HBaseClusterTestCase;
24  import org.apache.hadoop.hbase.HConstants;
25  import org.apache.hadoop.hbase.HRegionInfo;
26  import org.apache.hadoop.hbase.HServerAddress;
27  import org.apache.hadoop.hbase.HTableDescriptor;
28  import org.apache.hadoop.hbase.client.HTable;
29  import org.apache.hadoop.hbase.util.Bytes;
30  
31  public class TestRegionManager extends HBaseClusterTestCase {
32     public void testGetFirstMetaRegionForRegionAfterMetaSplit()
33     throws Exception {
34       HTable meta = new HTable(HConstants.META_TABLE_NAME);
35       HMaster master = this.cluster.getMaster();
36       HServerAddress address = master.getMasterAddress();
37       HTableDescriptor tableDesc = new HTableDescriptor(Bytes.toBytes("_MY_TABLE_"));
38       HTableDescriptor metaTableDesc = meta.getTableDescriptor();
39       // master.regionManager.onlineMetaRegions already contains first .META. region at key Bytes.toBytes("")
40       byte[] startKey0 = Bytes.toBytes("f");
41       byte[] endKey0 = Bytes.toBytes("h");
42       HRegionInfo regionInfo0 = new HRegionInfo(tableDesc, startKey0, endKey0);
43  
44       // 1st .META. region will be something like .META.,,1253625700761
45       HRegionInfo metaRegionInfo0 = new HRegionInfo(metaTableDesc, Bytes.toBytes(""), regionInfo0.getRegionName());
46       MetaRegion meta0 = new MetaRegion(address, metaRegionInfo0);
47  
48       byte[] startKey1 = Bytes.toBytes("j");
49       byte[] endKey1 = Bytes.toBytes("m");
50       HRegionInfo regionInfo1 = new HRegionInfo(tableDesc, startKey1, endKey1);
51       // 2nd .META. region will be something like .META.,_MY_TABLE_,f,1253625700761,1253625700761
52       HRegionInfo metaRegionInfo1 = new HRegionInfo(metaTableDesc, regionInfo0.getRegionName(), regionInfo1.getRegionName());
53       MetaRegion meta1 = new MetaRegion(address, metaRegionInfo1);
54  
55  
56       // 3rd .META. region will be something like .META.,_MY_TABLE_,j,1253625700761,1253625700761
57       HRegionInfo metaRegionInfo2 = new HRegionInfo(metaTableDesc, regionInfo1.getRegionName(), Bytes.toBytes(""));
58       MetaRegion meta2 = new MetaRegion(address, metaRegionInfo2);
59  
60       byte[] startKeyX = Bytes.toBytes("h");
61       byte[] endKeyX = Bytes.toBytes("j");
62       HRegionInfo regionInfoX = new HRegionInfo(tableDesc, startKeyX, endKeyX);
63  
64  
65       master.getRegionManager().offlineMetaRegionWithStartKey(startKey0);
66       master.getRegionManager().putMetaRegionOnline(meta0);
67       master.getRegionManager().putMetaRegionOnline(meta1);
68       master.getRegionManager().putMetaRegionOnline(meta2);
69  
70  //    for (byte[] b : master.regionManager.getOnlineMetaRegions().keySet()) {
71  //      System.out.println("FROM TEST KEY " + b +"  " +new String(b));
72  //    }
73  
74       assertEquals(metaRegionInfo1.getStartKey(),
75         master.getRegionManager().getFirstMetaRegionForRegion(regionInfoX).getStartKey());
76       assertEquals(metaRegionInfo1.getRegionName(),
77        master.getRegionManager().getFirstMetaRegionForRegion(regionInfoX).getRegionName());
78     }
79  }