1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.regionserver.wal;
20
21 import static junit.framework.Assert.assertEquals;
22
23 import java.io.IOException;
24 import java.util.List;
25 import java.util.Random;
26 import java.util.SortedMap;
27 import java.util.TreeMap;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.hadoop.hbase.HBaseTestingUtility;
32 import org.apache.hadoop.hbase.MediumTests;
33 import org.apache.hadoop.hbase.client.Delete;
34 import org.apache.hadoop.hbase.client.HTable;
35 import org.apache.hadoop.hbase.client.Put;
36 import org.apache.hadoop.hbase.master.HMaster;
37 import org.apache.hadoop.hbase.regionserver.HRegion;
38 import org.apache.hadoop.hbase.regionserver.HRegionServer;
39 import org.apache.hadoop.hbase.util.Bytes;
40 import org.junit.After;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.experimental.categories.Category;
44
45 import com.google.common.collect.Lists;
46 import com.google.protobuf.ServiceException;
47
48 @Category(MediumTests.class)
49 public class TestHLogFiltering {
50 private static final Log LOG = LogFactory.getLog(TestHLogFiltering.class);
51
52 private static final int NUM_MASTERS = 1;
53 private static final int NUM_RS = 4;
54
55 private static final byte[] TABLE_NAME = Bytes.toBytes("TestHLogFiltering");
56 private static final byte[] CF1 = Bytes.toBytes("MyCF1");
57 private static final byte[] CF2 = Bytes.toBytes("MyCF2");
58 private static final byte[][] FAMILIES = { CF1, CF2 };
59
60 private HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
61
62 @Before
63 public void setUp() throws Exception {
64 TEST_UTIL.startMiniCluster(NUM_MASTERS, NUM_RS);
65 fillTable();
66 }
67
68 @After
69 public void tearDown() throws Exception {
70 TEST_UTIL.shutdownMiniCluster();
71 }
72
73 private void fillTable() throws IOException, InterruptedException {
74 HTable table = TEST_UTIL.createTable(TABLE_NAME, FAMILIES, 3,
75 Bytes.toBytes("row0"), Bytes.toBytes("row99"), NUM_RS);
76 Random rand = new Random(19387129L);
77 for (int iStoreFile = 0; iStoreFile < 4; ++iStoreFile) {
78 for (int iRow = 0; iRow < 100; ++iRow) {
79 final byte[] row = Bytes.toBytes("row" + iRow);
80 Put put = new Put(row);
81 Delete del = new Delete(row);
82 for (int iCol = 0; iCol < 10; ++iCol) {
83 final byte[] cf = rand.nextBoolean() ? CF1 : CF2;
84 final long ts = rand.nextInt();
85 final byte[] qual = Bytes.toBytes("col" + iCol);
86 if (rand.nextBoolean()) {
87 final byte[] value = Bytes.toBytes("value_for_row_" + iRow +
88 "_cf_" + Bytes.toStringBinary(cf) + "_col_" + iCol + "_ts_" +
89 ts + "_random_" + rand.nextLong());
90 put.add(cf, qual, ts, value);
91 } else if (rand.nextDouble() < 0.8) {
92 del.deleteColumn(cf, qual, ts);
93 } else {
94 del.deleteColumns(cf, qual, ts);
95 }
96 }
97 table.put(put);
98 table.delete(del);
99 table.flushCommits();
100 }
101 }
102 TEST_UTIL.waitUntilAllRegionsAssigned(NUM_RS);
103 }
104
105 @Test
106 public void testFlushedSequenceIdsSentToHMaster()
107 throws IOException, InterruptedException, ServiceException {
108 SortedMap<byte[], Long> allFlushedSequenceIds =
109 new TreeMap<byte[], Long>(Bytes.BYTES_COMPARATOR);
110 for (int i = 0; i < NUM_RS; ++i) {
111 flushAllRegions(i);
112 }
113 Thread.sleep(10000);
114 HMaster master = TEST_UTIL.getMiniHBaseCluster().getMaster();
115 for (int i = 0; i < NUM_RS; ++i) {
116 for (byte[] regionName : getRegionsByServer(i)) {
117 if (allFlushedSequenceIds.containsKey(regionName)) {
118 assertEquals((long)allFlushedSequenceIds.get(regionName),
119 master.getLastFlushedSequenceId(regionName));
120 }
121 }
122 }
123 }
124
125 private List<byte[]> getRegionsByServer(int rsId) throws IOException {
126 List<byte[]> regionNames = Lists.newArrayList();
127 HRegionServer hrs = getRegionServer(rsId);
128 for (HRegion r : hrs.getOnlineRegions(TABLE_NAME)) {
129 regionNames.add(r.getRegionName());
130 }
131 return regionNames;
132 }
133
134 private HRegionServer getRegionServer(int rsId) {
135 return TEST_UTIL.getMiniHBaseCluster().getRegionServer(rsId);
136 }
137
138 private void flushAllRegions(int rsId) throws IOException {
139 HRegionServer hrs = getRegionServer(rsId);
140 for (byte[] regionName : getRegionsByServer(rsId)) {
141 hrs.flushRegion(regionName);
142 }
143 }
144
145 @org.junit.Rule
146 public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
147 new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
148 }