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