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