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.*;
31 import org.apache.hadoop.hbase.testclassification.SmallTests;
32 import org.apache.hadoop.hbase.util.Bytes;
33 import org.apache.hadoop.hbase.util.FSUtils;
34 import org.apache.hadoop.hbase.wal.WALFactory;
35 import org.apache.hadoop.hbase.wal.WAL;
36 import org.apache.hadoop.hbase.wal.WALKey;
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 import org.junit.experimental.categories.Category;
42
43 import static org.junit.Assert.*;
44
45
46
47
48 @Category(SmallTests.class)
49 public class TestWALActionsListener {
50 private static final Log LOG = LogFactory.getLog(TestWALActionsListener.class);
51
52 private final static HBaseTestingUtility TEST_UTIL =
53 new HBaseTestingUtility();
54
55 private final static byte[] SOME_BYTES = Bytes.toBytes("t");
56 private static FileSystem fs;
57 private static Configuration conf;
58
59 @BeforeClass
60 public static void setUpBeforeClass() throws Exception {
61 conf = TEST_UTIL.getConfiguration();
62 conf.setInt("hbase.regionserver.maxlogs", 5);
63 fs = FileSystem.get(conf);
64 FSUtils.setRootDir(conf, TEST_UTIL.getDataTestDir());
65 }
66
67 @Before
68 public void setUp() throws Exception {
69 fs.delete(new Path(TEST_UTIL.getDataTestDir(), HConstants.HREGION_LOGDIR_NAME), true);
70 fs.delete(new Path(TEST_UTIL.getDataTestDir(), HConstants.HREGION_OLDLOGDIR_NAME), true);
71 }
72
73 @After
74 public void tearDown() throws Exception {
75 setUp();
76 }
77
78
79
80
81
82
83 @Test
84 public void testActionListener() throws Exception {
85 DummyWALActionsListener observer = new DummyWALActionsListener();
86 List<WALActionsListener> list = new ArrayList<WALActionsListener>();
87 list.add(observer);
88 final WALFactory wals = new WALFactory(conf, list, "testActionListener");
89 DummyWALActionsListener laterobserver = new DummyWALActionsListener();
90 HRegionInfo hri = new HRegionInfo(TableName.valueOf(SOME_BYTES),
91 SOME_BYTES, SOME_BYTES, false);
92 final WAL wal = wals.getWAL(hri.getEncodedNameAsBytes());
93
94 for (int i = 0; i < 20; i++) {
95 byte[] b = Bytes.toBytes(i+"");
96 KeyValue kv = new KeyValue(b,b,b);
97 WALEdit edit = new WALEdit();
98 edit.add(kv);
99 HTableDescriptor htd = new HTableDescriptor();
100 htd.addFamily(new HColumnDescriptor(b));
101
102 final long txid = wal.append(htd, hri, new WALKey(hri.getEncodedNameAsBytes(),
103 TableName.valueOf(b), 0), edit, true);
104 wal.sync(txid);
105 if (i == 10) {
106 wal.registerWALActionsListener(laterobserver);
107 }
108 if (i % 2 == 0) {
109 wal.rollWriter();
110 }
111 }
112
113 wal.close();
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 public static class DummyWALActionsListener extends WALActionsListener.Base {
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 logCloseRequested() {
143 closedCount++;
144 }
145 }
146
147 }
148