1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.regionserver.wal;
21
22 import java.util.ArrayList;
23 import java.util.List;
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.*;
31 import org.apache.hadoop.hbase.util.Bytes;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import static org.junit.Assert.*;
37
38
39
40
41 public class TestWALActionsListener {
42 protected static final Log LOG = LogFactory.getLog(TestWALActionsListener.class);
43
44 private final static HBaseTestingUtility TEST_UTIL =
45 new HBaseTestingUtility();
46
47 private final static byte[] SOME_BYTES = Bytes.toBytes("t");
48 private static FileSystem fs;
49 private static Path oldLogDir;
50 private static Path logDir;
51 private static Configuration conf;
52
53 @BeforeClass
54 public static void setUpBeforeClass() throws Exception {
55 conf = TEST_UTIL.getConfiguration();
56 conf.setInt("hbase.regionserver.maxlogs", 5);
57 fs = FileSystem.get(conf);
58 oldLogDir = new Path(TEST_UTIL.getDataTestDir(),
59 HConstants.HREGION_OLDLOGDIR_NAME);
60 logDir = new Path(TEST_UTIL.getDataTestDir(),
61 HConstants.HREGION_LOGDIR_NAME);
62 }
63
64 @Before
65 public void setUp() throws Exception {
66 fs.delete(logDir, true);
67 fs.delete(oldLogDir, true);
68 }
69
70 @After
71 public void tearDown() throws Exception {
72 setUp();
73 }
74
75
76
77
78
79
80 @Test
81 public void testActionListener() throws Exception {
82 DummyWALActionsListener observer = new DummyWALActionsListener();
83 List<WALActionsListener> list = new ArrayList<WALActionsListener>();
84 list.add(observer);
85 DummyWALActionsListener laterobserver = new DummyWALActionsListener();
86 HLog hlog = new HLog(fs, logDir, oldLogDir, conf, list, null);
87 HRegionInfo hri = new HRegionInfo(SOME_BYTES,
88 SOME_BYTES, SOME_BYTES, false);
89
90 for (int i = 0; i < 20; i++) {
91 byte[] b = Bytes.toBytes(i+"");
92 KeyValue kv = new KeyValue(b,b,b);
93 WALEdit edit = new WALEdit();
94 edit.add(kv);
95 HTableDescriptor htd = new HTableDescriptor();
96 htd.addFamily(new HColumnDescriptor(b));
97
98 HLogKey key = new HLogKey(b,b, 0, 0, HConstants.DEFAULT_CLUSTER_ID);
99 hlog.append(hri, key, edit, htd);
100 if (i == 10) {
101 hlog.registerWALActionsListener(laterobserver);
102 }
103 if (i % 2 == 0) {
104 hlog.rollWriter();
105 }
106 }
107
108 hlog.close();
109 hlog.closeAndDelete();
110
111 assertEquals(11, observer.logRollCounter);
112 assertEquals(5, laterobserver.logRollCounter);
113 assertEquals(2, observer.closedCount);
114 }
115
116
117
118
119
120 static class DummyWALActionsListener implements WALActionsListener {
121 public int logRollCounter = 0;
122 public int closedCount = 0;
123
124 @Override
125 public void logRolled(Path newFile) {
126 logRollCounter++;
127 }
128
129 @Override
130 public void logRollRequested() {
131
132 }
133
134 @Override
135 public void visitLogEntryBeforeWrite(HRegionInfo info, HLogKey logKey,
136 WALEdit logEdit) {
137
138
139 }
140
141 @Override
142 public void logCloseRequested() {
143 closedCount++;
144 }
145
146 public void visitLogEntryBeforeWrite(HTableDescriptor htd, HLogKey logKey, WALEdit logEdit) {
147
148 }
149
150 }
151 }