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;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotEquals;
24
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.Map;
28
29 import org.apache.hadoop.conf.Configuration;
30 import org.apache.hadoop.fs.FileSystem;
31 import org.apache.hadoop.fs.Path;
32 import org.apache.hadoop.hbase.HBaseTestingUtility;
33 import org.apache.hadoop.hbase.HConstants;
34 import org.apache.hadoop.hbase.regionserver.wal.FSHLog;
35 import org.apache.hadoop.hbase.testclassification.MediumTests;
36 import org.apache.hadoop.hbase.testclassification.RegionServerTests;
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.experimental.categories.Category;
41 import org.mockito.Mockito;
42
43 @Category({RegionServerTests.class, MediumTests.class})
44 public class TestLogRoller {
45
46 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
47
48 private static final int LOG_ROLL_PERIOD = 20 * 1000;
49 private static final String LOG_DIR = "WALs";
50 private static final String ARCHIVE_DIR = "archiveWALs";
51 private static final String WAL_PREFIX = "test-log-roller";
52 private static Configuration CONF;
53 private static LogRoller ROLLER;
54 private static Path ROOT_DIR;
55 private static FileSystem FS;
56
57 @Before
58 public void setup() throws Exception {
59 CONF = TEST_UTIL.getConfiguration();
60 CONF.setInt("hbase.regionserver.logroll.period", LOG_ROLL_PERIOD);
61 CONF.setInt(HConstants.THREAD_WAKE_FREQUENCY, 300);
62 ROOT_DIR = TEST_UTIL.getRandomDir();
63 FS = FileSystem.get(CONF);
64 HRegionServer server = Mockito.mock(HRegionServer.class);
65 Mockito.when(server.getConfiguration()).thenReturn(CONF);
66 RegionServerServices services = Mockito.mock(RegionServerServices.class);
67 ROLLER = new LogRoller(server, services);
68 ROLLER.start();
69 }
70
71 @After
72 public void tearDown() throws Exception {
73 ROLLER.interrupt();
74 FS.close();
75 TEST_UTIL.shutdownMiniCluster();
76 }
77
78
79
80
81 @Test
82 public void testRequestRollWithMultiWal() throws Exception {
83
84 Map<FSHLog, Path> wals = new HashMap<FSHLog, Path>();
85 for (int i = 1; i <= 3; i++) {
86 FSHLog wal = new FSHLog(FS, ROOT_DIR, LOG_DIR, ARCHIVE_DIR, CONF, null,
87 true, WAL_PREFIX, "." + i);
88 wal.rollWriter(true);
89 wals.put(wal, wal.getCurrentFileName());
90 ROLLER.addWAL(wal);
91 Thread.sleep(1000);
92 }
93
94
95 Iterator<Map.Entry<FSHLog, Path>> it = wals.entrySet().iterator();
96 Map.Entry<FSHLog, Path> walEntry = it.next();
97 walEntry.getKey().requestLogRoll();
98 Thread.sleep(5000);
99
100 assertNotEquals(walEntry.getValue(), walEntry.getKey().getCurrentFileName());
101 walEntry.setValue(walEntry.getKey().getCurrentFileName());
102 while (it.hasNext()) {
103 walEntry = it.next();
104 assertEquals(walEntry.getValue(), walEntry.getKey().getCurrentFileName());
105 }
106
107
108 Thread.sleep(LOG_ROLL_PERIOD + 5000);
109 for (Map.Entry<FSHLog, Path> entry : wals.entrySet()) {
110 assertNotEquals(entry.getValue(), entry.getKey().getCurrentFileName());
111 entry.getKey().close();
112 }
113 }
114 }