1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.testclassification.LargeTests;
30 import org.apache.hadoop.hbase.MasterNotRunningException;
31 import org.apache.hadoop.hbase.TableName;
32 import org.apache.hadoop.hbase.ZooKeeperConnectionException;
33 import org.apache.hadoop.hbase.client.HBaseAdmin;
34 import org.apache.hadoop.hbase.client.HTable;
35 import org.apache.hadoop.hbase.client.Put;
36 import org.apache.hadoop.hbase.client.Result;
37 import org.apache.hadoop.hbase.client.ResultScanner;
38 import org.apache.hadoop.hbase.client.Scan;
39 import org.apache.hadoop.hbase.util.Bytes;
40 import org.junit.AfterClass;
41 import org.junit.Assert;
42 import org.junit.BeforeClass;
43 import org.junit.Test;
44 import org.junit.experimental.categories.Category;
45
46 @Category(LargeTests.class)
47 public class TestDefaultMobStoreFlusher {
48
49 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
50 private final static byte [] row1 = Bytes.toBytes("row1");
51 private final static byte [] row2 = Bytes.toBytes("row2");
52 private final static byte [] family = Bytes.toBytes("family");
53 private final static byte [] qf1 = Bytes.toBytes("qf1");
54 private final static byte [] qf2 = Bytes.toBytes("qf2");
55 private final static byte [] value1 = Bytes.toBytes("value1");
56 private final static byte [] value2 = Bytes.toBytes("value2");
57
58 @BeforeClass
59 public static void setUpBeforeClass() throws Exception {
60 TEST_UTIL.getConfiguration().setInt("hfile.format.version", 3);
61 TEST_UTIL.getConfiguration().setInt("hbase.master.info.port", 0);
62 TEST_UTIL.getConfiguration().setBoolean("hbase.regionserver.info.port.auto", true);
63
64 TEST_UTIL.startMiniCluster(1);
65 }
66
67 @AfterClass
68 public static void tearDownAfterClass() throws Exception {
69 TEST_UTIL.shutdownMiniCluster();
70 }
71
72 @Test
73 public void testFlushNonMobFile() throws InterruptedException {
74 String TN = "testFlushNonMobFile";
75 HTable table = null;
76 HBaseAdmin admin = null;
77
78 try {
79 HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(TN));
80 HColumnDescriptor hcd = new HColumnDescriptor(family);
81 hcd.setMaxVersions(4);
82 desc.addFamily(hcd);
83
84 admin = new HBaseAdmin(TEST_UTIL.getConfiguration());
85 admin.createTable(desc);
86 table = new HTable(TEST_UTIL.getConfiguration(), TN);
87
88
89 Put put0 = new Put(row1);
90 put0.add(family, qf1, 1, value1);
91 table.put(put0);
92
93
94 Put put1 = new Put(row2);
95 put1.add(family, qf2, 1, value2);
96 table.put(put1);
97
98
99 table.flushCommits();
100 admin.flush(TN);
101
102 Scan scan = new Scan();
103 scan.addColumn(family, qf1);
104 scan.setMaxVersions(4);
105 ResultScanner scanner = table.getScanner(scan);
106
107
108 Result result = scanner.next();
109 int size = 0;
110 while (result != null) {
111 size++;
112 List<Cell> cells = result.getColumnCells(family, qf1);
113
114 Assert.assertEquals(1, cells.size());
115
116 Assert.assertEquals(Bytes.toString(value1),
117 Bytes.toString(CellUtil.cloneValue(cells.get(0))));
118 result = scanner.next();
119 }
120 scanner.close();
121 Assert.assertEquals(1, size);
122 admin.close();
123 } catch (MasterNotRunningException e1) {
124 e1.printStackTrace();
125 } catch (ZooKeeperConnectionException e2) {
126 e2.printStackTrace();
127 } catch (IOException e3) {
128 e3.printStackTrace();
129 }
130 }
131
132 @Test
133 public void testFlushMobFile() throws InterruptedException {
134 String TN = "testFlushMobFile";
135 HTable table = null;
136 HBaseAdmin admin = null;
137
138 try {
139 HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(TN));
140 HColumnDescriptor hcd = new HColumnDescriptor(family);
141 hcd.setMobEnabled(true);
142 hcd.setMobThreshold(3L);
143 hcd.setMaxVersions(4);
144 desc.addFamily(hcd);
145
146 admin = new HBaseAdmin(TEST_UTIL.getConfiguration());
147 admin.createTable(desc);
148 table = new HTable(TEST_UTIL.getConfiguration(), TN);
149
150
151 Put put0 = new Put(row1);
152 put0.add(family, qf1, 1, value1);
153 table.put(put0);
154
155
156 Put put1 = new Put(row2);
157 put1.add(family, qf2, 1, value2);
158 table.put(put1);
159
160
161 table.flushCommits();
162 admin.flush(TN);
163
164
165 Scan scan = new Scan();
166 scan.addColumn(family, qf1);
167 scan.setMaxVersions(4);
168 ResultScanner scanner = table.getScanner(scan);
169
170
171 Result result = scanner.next();
172 int size = 0;
173 while (result != null) {
174 size++;
175 List<Cell> cells = result.getColumnCells(family, qf1);
176
177 Assert.assertEquals(1, cells.size());
178
179 Assert.assertEquals(Bytes.toString(value1),
180 Bytes.toString(CellUtil.cloneValue(cells.get(0))));
181 result = scanner.next();
182 }
183 scanner.close();
184 Assert.assertEquals(1, size);
185 admin.close();
186 } catch (MasterNotRunningException e1) {
187 e1.printStackTrace();
188 } catch (ZooKeeperConnectionException e2) {
189 e2.printStackTrace();
190 } catch (IOException e3) {
191 e3.printStackTrace();
192 }
193 }
194 }