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 java.util.ArrayList;
22 import java.util.List;
23 import java.util.concurrent.atomic.AtomicLong;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.hadoop.conf.Configuration;
28 import org.apache.hadoop.fs.FileSystem;
29 import org.apache.hadoop.fs.Path;
30 import org.apache.hadoop.hbase.HBaseTestingUtility;
31 import org.apache.hadoop.hbase.HColumnDescriptor;
32 import org.apache.hadoop.hbase.HConstants;
33 import org.apache.hadoop.hbase.HRegionInfo;
34 import org.apache.hadoop.hbase.HTableDescriptor;
35 import org.apache.hadoop.hbase.KeyValue;
36 import org.apache.hadoop.hbase.TableName;
37 import org.apache.hadoop.hbase.testclassification.SmallTests;
38 import org.apache.hadoop.hbase.util.Bytes;
39 import org.apache.hadoop.hbase.util.FSUtils;
40 import org.apache.hadoop.hbase.wal.WALFactory;
41 import org.apache.hadoop.hbase.wal.WAL;
42 import org.apache.hadoop.hbase.wal.WALKey;
43 import org.junit.After;
44 import org.junit.Before;
45 import org.junit.BeforeClass;
46 import org.junit.Test;
47 import org.junit.experimental.categories.Category;
48
49 import static org.junit.Assert.*;
50
51
52
53
54 @Category(SmallTests.class)
55 public class TestWALActionsListener {
56 protected static final Log LOG = LogFactory.getLog(TestWALActionsListener.class);
57
58 private final static HBaseTestingUtility TEST_UTIL =
59 new HBaseTestingUtility();
60
61 private final static byte[] SOME_BYTES = Bytes.toBytes("t");
62 private static Configuration conf;
63 private static Path rootDir;
64 private static Path walRootDir;
65 private static FileSystem fs;
66 private static FileSystem walFs;
67
68 @BeforeClass
69 public static void setUpBeforeClass() throws Exception {
70 conf = TEST_UTIL.getConfiguration();
71 conf.setInt("hbase.regionserver.maxlogs", 5);
72 rootDir = TEST_UTIL.createRootDir();
73 walRootDir = TEST_UTIL.createWALRootDir();
74 fs = FSUtils.getRootDirFileSystem(conf);
75 walFs = FSUtils.getWALFileSystem(conf);
76 }
77
78 @Before
79 public void setUp() throws Exception {
80 fs.delete(rootDir, true);
81 walFs.delete(new Path(walRootDir, HConstants.HREGION_LOGDIR_NAME), true);
82 walFs.delete(new Path(walRootDir, HConstants.HREGION_OLDLOGDIR_NAME), true);
83 }
84
85 @After
86 public void tearDown() throws Exception {
87 setUp();
88 }
89
90
91
92
93
94
95 @Test
96 public void testActionListener() throws Exception {
97 DummyWALActionsListener observer = new DummyWALActionsListener();
98 List<WALActionsListener> list = new ArrayList<WALActionsListener>();
99 list.add(observer);
100 final WALFactory wals = new WALFactory(conf, list, "testActionListener");
101 DummyWALActionsListener laterobserver = new DummyWALActionsListener();
102 final AtomicLong sequenceId = new AtomicLong(1);
103 HRegionInfo hri = new HRegionInfo(TableName.valueOf(SOME_BYTES),
104 SOME_BYTES, SOME_BYTES, false);
105 final WAL wal = wals.getWAL(hri.getEncodedNameAsBytes());
106
107 for (int i = 0; i < 20; i++) {
108 byte[] b = Bytes.toBytes(i+"");
109 KeyValue kv = new KeyValue(b,b,b);
110 WALEdit edit = new WALEdit();
111 edit.add(kv);
112 HTableDescriptor htd = new HTableDescriptor();
113 htd.addFamily(new HColumnDescriptor(b));
114
115 final long txid = wal.append(htd, hri, new WALKey(hri.getEncodedNameAsBytes(),
116 TableName.valueOf(b), 0), edit, sequenceId, true, null);
117 wal.sync(txid);
118 if (i == 10) {
119 wal.registerWALActionsListener(laterobserver);
120 }
121 if (i % 2 == 0) {
122 wal.rollWriter();
123 }
124 }
125
126 wal.close();
127
128 assertEquals(11, observer.preLogRollCounter);
129 assertEquals(11, observer.postLogRollCounter);
130 assertEquals(5, laterobserver.preLogRollCounter);
131 assertEquals(5, laterobserver.postLogRollCounter);
132 assertEquals(1, observer.closedCount);
133 }
134
135
136
137
138
139 public static class DummyWALActionsListener extends WALActionsListener.Base {
140 public int preLogRollCounter = 0;
141 public int postLogRollCounter = 0;
142 public int closedCount = 0;
143
144 @Override
145 public void preLogRoll(Path oldFile, Path newFile) {
146 preLogRollCounter++;
147 }
148
149 @Override
150 public void postLogRoll(Path oldFile, Path newFile) {
151 postLogRollCounter++;
152 }
153
154 @Override
155 public void logCloseRequested() {
156 closedCount++;
157 }
158 }
159
160 }
161