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