View Javadoc

1   /**
2   *
3   * Licensed to the Apache Software Foundation (ASF) under one
4   * or more contributor license agreements.  See the NOTICE file
5   * distributed with this work for additional information
6   * regarding copyright ownership.  The ASF licenses this file
7   * to you under the Apache License, Version 2.0 (the
8   * "License"); you may not use this file except in compliance
9   * with the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19  package org.apache.hadoop.hbase.mob;
20  
21  import java.io.IOException;
22  import java.util.List;
23  
24  import org.apache.hadoop.hbase.Cell;
25  import org.apache.hadoop.hbase.CellUtil;
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.MasterNotRunningException;
30  import org.apache.hadoop.hbase.TableName;
31  import org.apache.hadoop.hbase.ZooKeeperConnectionException;
32  import org.apache.hadoop.hbase.client.*;
33  import org.apache.hadoop.hbase.testclassification.LargeTests;
34  import org.apache.hadoop.hbase.util.Bytes;
35  import org.junit.AfterClass;
36  import org.junit.Assert;
37  import org.junit.BeforeClass;
38  import org.junit.Test;
39  import org.junit.experimental.categories.Category;
40  
41  @Category(LargeTests.class)
42  public class TestDefaultMobStoreFlusher {
43  
44   private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
45   private final static byte [] row1 = Bytes.toBytes("row1");
46   private final static byte [] row2 = Bytes.toBytes("row2");
47   private final static byte [] family = Bytes.toBytes("family");
48   private final static byte [] qf1 = Bytes.toBytes("qf1");
49   private final static byte [] qf2 = Bytes.toBytes("qf2");
50   private final static byte [] value1 = Bytes.toBytes("value1");
51   private final static byte [] value2 = Bytes.toBytes("value2");
52  
53   @BeforeClass
54   public static void setUpBeforeClass() throws Exception {
55     TEST_UTIL.getConfiguration().setInt("hbase.master.info.port", 0);
56     TEST_UTIL.getConfiguration().setBoolean("hbase.regionserver.info.port.auto", true);
57  
58     TEST_UTIL.startMiniCluster(1);
59   }
60  
61   @AfterClass
62   public static void tearDownAfterClass() throws Exception {
63     TEST_UTIL.shutdownMiniCluster();
64   }
65  
66   @Test
67   public void testFlushNonMobFile() throws InterruptedException {
68     String TN = "testFlushNonMobFile";
69     TableName tn = TableName.valueOf(TN);
70     Table table = null;
71     HBaseAdmin admin = null;
72  
73     try {
74       HTableDescriptor desc = new HTableDescriptor(tn);
75       HColumnDescriptor hcd = new HColumnDescriptor(family);
76       hcd.setMaxVersions(4);
77       desc.addFamily(hcd);
78  
79       admin = TEST_UTIL.getHBaseAdmin();
80       admin.createTable(desc);
81       table = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration())
82               .getTable(TableName.valueOf(TN));
83  
84       //Put data
85       Put put0 = new Put(row1);
86       put0.addColumn(family, qf1, 1, value1);
87       table.put(put0);
88  
89       //Put more data
90       Put put1 = new Put(row2);
91       put1.addColumn(family, qf2, 1, value2);
92       table.put(put1);
93  
94       //Flush
95       admin.flush(tn);
96  
97       Scan scan = new Scan();
98       scan.addColumn(family, qf1);
99       scan.setMaxVersions(4);
100      ResultScanner scanner = table.getScanner(scan);
101 
102      //Compare
103      Result result = scanner.next();
104      int size = 0;
105      while (result != null) {
106        size++;
107        List<Cell> cells = result.getColumnCells(family, qf1);
108        // Verify the cell size
109        Assert.assertEquals(1, cells.size());
110        // Verify the value
111        Assert.assertEquals(Bytes.toString(value1),
112            Bytes.toString(CellUtil.cloneValue(cells.get(0))));
113        result = scanner.next();
114      }
115      scanner.close();
116      Assert.assertEquals(1, size);
117      admin.close();
118    } catch (MasterNotRunningException e1) {
119      e1.printStackTrace();
120    } catch (ZooKeeperConnectionException e2) {
121      e2.printStackTrace();
122    } catch (IOException e3) {
123      e3.printStackTrace();
124    }
125  }
126 
127  @Test
128  public void testFlushMobFile() throws InterruptedException {
129    String TN = "testFlushMobFile";
130    TableName tn = TableName.valueOf(TN);
131    Table table = null;
132    Admin admin = null;
133 
134    try {
135      HTableDescriptor desc = new HTableDescriptor(tn);
136      HColumnDescriptor hcd = new HColumnDescriptor(family);
137      hcd.setMobEnabled(true);
138      hcd.setMobThreshold(3L);
139      hcd.setMaxVersions(4);
140      desc.addFamily(hcd);
141 
142      Connection c = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration());
143      admin = c.getAdmin();
144      admin.createTable(desc);
145      table = c.getTable(TableName.valueOf(TN));
146 
147      //put data
148      Put put0 = new Put(row1);
149      put0.addColumn(family, qf1, 1, value1);
150      table.put(put0);
151 
152      //put more data
153      Put put1 = new Put(row2);
154      put1.addColumn(family, qf2, 1, value2);
155      table.put(put1);
156 
157      //flush
158      admin.flush(tn);
159 
160      //Scan
161      Scan scan = new Scan();
162      scan.addColumn(family, qf1);
163      scan.setMaxVersions(4);
164      ResultScanner scanner = table.getScanner(scan);
165 
166      //Compare
167      Result result = scanner.next();
168      int size = 0;
169      while (result != null) {
170        size++;
171        List<Cell> cells = result.getColumnCells(family, qf1);
172        // Verify the the cell size
173        Assert.assertEquals(1, cells.size());
174        // Verify the value
175        Assert.assertEquals(Bytes.toString(value1),
176            Bytes.toString(CellUtil.cloneValue(cells.get(0))));
177        result = scanner.next();
178      }
179      scanner.close();
180      Assert.assertEquals(1, size);
181      admin.close();
182    } catch (MasterNotRunningException e1) {
183      e1.printStackTrace();
184    } catch (ZooKeeperConnectionException e2) {
185      e2.printStackTrace();
186    } catch (IOException e3) {
187      e3.printStackTrace();
188    }
189  }
190 }