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;
22  
23  import static org.junit.Assert.assertEquals;
24  
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.hbase.client.HTable;
27  import org.apache.hadoop.hbase.client.Result;
28  import org.apache.hadoop.hbase.client.ResultScanner;
29  import org.apache.hadoop.hbase.client.Scan;
30  import org.apache.hadoop.hbase.util.Bytes;
31  import org.junit.After;
32  import org.junit.AfterClass;
33  import org.junit.Before;
34  import org.junit.BeforeClass;
35  import org.junit.Test;
36  
37  public class TestFullLogReconstruction {
38  
39    private final static HBaseTestingUtility
40        TEST_UTIL = new HBaseTestingUtility();
41  
42    private final static byte[] TABLE_NAME = Bytes.toBytes("tabletest");
43    private final static byte[] FAMILY = Bytes.toBytes("family");
44  
45    /**
46     * @throws java.lang.Exception
47     */
48    @BeforeClass
49    public static void setUpBeforeClass() throws Exception {
50      Configuration c = TEST_UTIL.getConfiguration();
51      c.setInt("hbase.regionserver.flushlogentries", 1);
52      c.setBoolean("dfs.support.append", true);
53      // quicker heartbeat interval for faster DN death notification
54      c.setInt("heartbeat.recheck.interval", 5000);
55      c.setInt("dfs.heartbeat.interval", 1);
56      c.setInt("dfs.socket.timeout", 5000);
57      // faster failover with cluster.shutdown();fs.close() idiom
58      c.setInt("ipc.client.connect.max.retries", 1);
59      c.setInt("dfs.client.block.recovery.retries", 1);
60      c.setInt("hbase.regionserver.flushlogentries", 1);
61      TEST_UTIL.startMiniCluster(2);
62    }
63  
64    /**
65     * @throws java.lang.Exception
66     */
67    @AfterClass
68    public static void tearDownAfterClass() throws Exception {
69      TEST_UTIL.shutdownMiniCluster();
70    }
71  
72    /**
73     * @throws java.lang.Exception
74     */
75    @Before
76    public void setUp() throws Exception {
77    }
78  
79    /**
80     * @throws java.lang.Exception
81     */
82    @After
83    public void tearDown() throws Exception {
84    }
85  
86    /**
87     * Test the whole reconstruction loop. Build a table with regions aaa to zzz
88     * and load every one of them multiple times with the same date and do a flush
89     * at some point. Kill one of the region servers and scan the table. We should
90     * see all the rows.
91     * @throws Exception
92     */
93    @Test
94    public void testReconstruction() throws Exception {
95  
96      HTable table = TEST_UTIL.createTable(TABLE_NAME, FAMILY);
97  
98      TEST_UTIL.createMultiRegions(table, Bytes.toBytes("family"));
99  
100     // Load up the table with simple rows and count them
101     int initialCount = TEST_UTIL.loadTable(table, FAMILY);
102     Scan scan = new Scan();
103     ResultScanner results = table.getScanner(scan);
104     int count = 0;
105     for (Result res : results) {
106       count++;
107     }
108     results.close();
109 
110     assertEquals(initialCount, count);
111 
112     for(int i = 0; i < 4; i++) {
113       TEST_UTIL.loadTable(table, FAMILY);
114     }
115 
116     TEST_UTIL.expireRegionServerSession(0);
117     scan = new Scan();
118     results = table.getScanner(scan);
119     int newCount = 0;
120     for (Result res : results) {
121       newCount++;
122     }
123     assertEquals(count, newCount);
124   }
125 }