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.apache.hadoop.hbase.HBaseTestingUtility.fam1;
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.io.IOException;
25  
26  import org.apache.hadoop.hbase.HBaseTestingUtility;
27  import org.apache.hadoop.hbase.HColumnDescriptor;
28  import org.apache.hadoop.hbase.HTableDescriptor;
29  import org.apache.hadoop.hbase.MiniHBaseCluster;
30  import org.apache.hadoop.hbase.TableName;
31  import org.apache.hadoop.hbase.client.Admin;
32  import org.apache.hadoop.hbase.client.Connection;
33  import org.apache.hadoop.hbase.client.ConnectionFactory;
34  import org.apache.hadoop.hbase.client.Durability;
35  import org.apache.hadoop.hbase.client.Get;
36  import org.apache.hadoop.hbase.client.Put;
37  import org.apache.hadoop.hbase.client.Result;
38  import org.apache.hadoop.hbase.client.RowMutations;
39  import org.apache.hadoop.hbase.client.Table;
40  import org.apache.hadoop.hbase.testclassification.MediumTests;
41  import org.apache.hadoop.hbase.util.Bytes;
42  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
43  import org.junit.After;
44  import org.junit.AfterClass;
45  import org.junit.Before;
46  import org.junit.BeforeClass;
47  import org.junit.Test;
48  import org.junit.experimental.categories.Category;
49  
50  @Category(MediumTests.class)
51  public class TestMutateRowsRecovery {
52    private MiniHBaseCluster cluster = null;
53    private Connection connection = null;
54    private static final int NB_SERVERS = 3;
55  
56    static final byte[] qual1 = Bytes.toBytes("qual1");
57    static final byte[] qual2 = Bytes.toBytes("qual2");
58    static final byte[] value1 = Bytes.toBytes("value1");
59    static final byte[] value2 = Bytes.toBytes("value2");
60    static final byte[] row1 = Bytes.toBytes("rowA");
61    static final byte[] row2 = Bytes.toBytes("rowB");
62  
63    static final HBaseTestingUtility TESTING_UTIL = new HBaseTestingUtility();
64  
65    @BeforeClass
66    public static void before() throws Exception {
67      TESTING_UTIL.startMiniCluster(NB_SERVERS);
68    }
69  
70    @AfterClass
71    public static void after() throws Exception {
72      TESTING_UTIL.shutdownMiniCluster();
73    }
74  
75    @Before
76    public void setup() throws IOException {
77      TESTING_UTIL.ensureSomeNonStoppedRegionServersAvailable(NB_SERVERS);
78      this.connection = ConnectionFactory.createConnection(TESTING_UTIL.getConfiguration());
79      this.cluster = TESTING_UTIL.getMiniHBaseCluster();
80    }
81  
82    @After
83    public void tearDown() throws IOException {
84      if (this.connection != null) {
85        this.connection.close();
86      }
87    }
88  
89    @Test
90    public void MutateRowsAndCheckPostKill() throws IOException, InterruptedException {
91      final TableName tableName = TableName.valueOf("test");
92      Admin admin = null;
93      Table hTable = null;
94      try {
95        admin = connection.getAdmin();
96        hTable = connection.getTable(tableName);
97        HTableDescriptor desc = new HTableDescriptor(tableName);
98        desc.addFamily(new HColumnDescriptor(fam1));
99        admin.createTable(desc);
100 
101       // Add a multi
102       RowMutations rm = new RowMutations(row1);
103       Put p1 = new Put(row1);
104       p1.addColumn(fam1, qual1, value1);
105       p1.setDurability(Durability.SYNC_WAL);
106       rm.add(p1);
107       hTable.mutateRow(rm);
108 
109       // Add a put
110       Put p2 = new Put(row1);
111       p2.addColumn(fam1, qual2, value2);
112       p2.setDurability(Durability.SYNC_WAL);
113       hTable.put(p2);
114 
115       HRegionServer rs1 = TESTING_UTIL.getRSForFirstRegionInTable(tableName);
116       long now = EnvironmentEdgeManager.currentTime();
117       // Send the RS Load to ensure correct lastflushedseqid for stores
118       rs1.tryRegionServerReport(now - 30000, now);
119       // Kill the RS to trigger wal replay
120       cluster.killRegionServer(rs1.serverName);
121 
122       // Ensure correct data exists
123       Get g1 = new Get(row1);
124       Result result = hTable.get(g1);
125       assertTrue(result.getValue(fam1, qual1) != null);
126       assertEquals(0, Bytes.compareTo(result.getValue(fam1, qual1), value1));
127       assertTrue(result.getValue(fam1, qual2) != null);
128       assertEquals(0, Bytes.compareTo(result.getValue(fam1, qual2), value2));
129     } finally {
130       if (admin != null) {
131         admin.close();
132       }
133       if (hTable != null) {
134         hTable.close();
135       }
136     }
137   }
138 }