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  package org.apache.hadoop.hbase.regionserver;
19  
20  import static org.junit.Assert.*;
21  
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.hbase.HBaseTestingUtility;
29  import org.apache.hadoop.hbase.HColumnDescriptor;
30  import org.apache.hadoop.hbase.HRegionInfo;
31  import org.apache.hadoop.hbase.HTableDescriptor;
32  import org.apache.hadoop.hbase.MiniHBaseCluster;
33  import org.apache.hadoop.hbase.TableName;
34  import org.apache.hadoop.hbase.client.Admin;
35  import org.apache.hadoop.hbase.client.HTable;
36  import org.apache.hadoop.hbase.client.Put;
37  import org.apache.hadoop.hbase.client.Result;
38  import org.apache.hadoop.hbase.client.ResultScanner;
39  import org.apache.hadoop.hbase.client.Scan;
40  import org.apache.hadoop.hbase.client.Table;
41  import org.apache.hadoop.hbase.master.HMaster;
42  import org.apache.hadoop.hbase.util.Bytes;
43  import org.apache.hadoop.hbase.testclassification.MediumTests;
44  import org.junit.Test;
45  import org.junit.experimental.categories.Category;
46  
47  /**
48   * Tests that need to spin up a cluster testing an {@link HRegion}.  Use
49   * {@link TestHRegion} if you don't need a cluster, if you can test w/ a
50   * standalone {@link HRegion}.
51   */
52  @Category(MediumTests.class)
53  public class TestHRegionOnCluster {
54  
55    private static final Log LOG = LogFactory.getLog(TestHRegionOnCluster.class);
56    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
57  
58    @Test (timeout=300000)
59    public void testDataCorrectnessReplayingRecoveredEdits() throws Exception {
60      final int NUM_MASTERS = 1;
61      final int NUM_RS = 3;
62      Admin hbaseAdmin = null;
63      TEST_UTIL.startMiniCluster(NUM_MASTERS, NUM_RS);
64  
65      try {
66        final TableName TABLENAME = TableName.valueOf("testDataCorrectnessReplayingRecoveredEdits");
67        final byte[] FAMILY = Bytes.toBytes("family");
68        MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
69        HMaster master = cluster.getMaster();
70  
71        // Create table
72        HTableDescriptor desc = new HTableDescriptor(TABLENAME);
73        desc.addFamily(new HColumnDescriptor(FAMILY));
74        hbaseAdmin = master.getConnection().getAdmin();
75        hbaseAdmin.createTable(desc);
76  
77        assertTrue(hbaseAdmin.isTableAvailable(TABLENAME));
78  
79        // Put data: r1->v1
80        LOG.info("Loading r1 to v1 into " + TABLENAME);
81        HTable table = new HTable(TEST_UTIL.getConfiguration(), TABLENAME);
82        putDataAndVerify(table, "r1", FAMILY, "v1", 1);
83  
84        TEST_UTIL.waitUntilAllRegionsAssigned(table.getName());
85        // Move region to target server
86        HRegionInfo regionInfo = table.getRegionLocation("r1").getRegionInfo();
87        int originServerNum = cluster.getServerWith(regionInfo.getRegionName());
88        HRegionServer originServer = cluster.getRegionServer(originServerNum);
89        int targetServerNum = (originServerNum + 1) % NUM_RS;
90        HRegionServer targetServer = cluster.getRegionServer(targetServerNum);
91        assertFalse(originServer.equals(targetServer));
92  
93        TEST_UTIL.waitUntilAllRegionsAssigned(table.getName());
94        LOG.info("Moving " + regionInfo.getEncodedName() + " to " + targetServer.getServerName());
95        hbaseAdmin.move(regionInfo.getEncodedNameAsBytes(),
96            Bytes.toBytes(targetServer.getServerName().getServerName()));
97        do {
98          Thread.sleep(1);
99        } while (cluster.getServerWith(regionInfo.getRegionName()) == originServerNum);
100 
101       // Put data: r2->v2
102       LOG.info("Loading r2 to v2 into " + TABLENAME);
103       putDataAndVerify(table, "r2", FAMILY, "v2", 2);
104 
105       TEST_UTIL.waitUntilAllRegionsAssigned(table.getName());
106       // Move region to origin server
107       LOG.info("Moving " + regionInfo.getEncodedName() + " to " + originServer.getServerName());
108       hbaseAdmin.move(regionInfo.getEncodedNameAsBytes(),
109           Bytes.toBytes(originServer.getServerName().getServerName()));
110       do {
111         Thread.sleep(1);
112       } while (cluster.getServerWith(regionInfo.getRegionName()) == targetServerNum);
113 
114       // Put data: r3->v3
115       LOG.info("Loading r3 to v3 into " + TABLENAME);
116       putDataAndVerify(table, "r3", FAMILY, "v3", 3);
117 
118       // Kill target server
119       LOG.info("Killing target server " + targetServer.getServerName());
120       targetServer.kill();
121       cluster.getRegionServerThreads().get(targetServerNum).join();
122       // Wait until finish processing of shutdown
123       while (master.getServerManager().areDeadServersInProgress()) {
124         Thread.sleep(5);
125       }
126       // Kill origin server
127       LOG.info("Killing origin server " + targetServer.getServerName());
128       originServer.kill();
129       cluster.getRegionServerThreads().get(originServerNum).join();
130 
131       // Put data: r4->v4
132       LOG.info("Loading r4 to v4 into " + TABLENAME);
133       putDataAndVerify(table, "r4", FAMILY, "v4", 4);
134 
135     } finally {
136       if (hbaseAdmin != null) hbaseAdmin.close();
137       TEST_UTIL.shutdownMiniCluster();
138     }
139   }
140 
141   private void putDataAndVerify(Table table, String row, byte[] family,
142       String value, int verifyNum) throws IOException {
143     System.out.println("=========Putting data :" + row);
144     Put put = new Put(Bytes.toBytes(row));
145     put.add(family, Bytes.toBytes("q1"), Bytes.toBytes(value));
146     table.put(put);
147     ResultScanner resultScanner = table.getScanner(new Scan());
148     List<Result> results = new ArrayList<Result>();
149     while (true) {
150       Result r = resultScanner.next();
151       if (r == null)
152         break;
153       results.add(r);
154     }
155     resultScanner.close();
156     if (results.size() != verifyNum) {
157       System.out.println(results);
158     }
159     assertEquals(verifyNum, results.size());
160   }
161 
162 }