HBASE-26700 The way we bypass broken track file is not enough in StoreFileListFile...
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / regionserver / TestLogRoller.java
blob1405e40a55eafccd4195d2b96659ddc343ae6f2d
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.hadoop.hbase.regionserver;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertNotEquals;
23 import static org.junit.Assert.assertTrue;
25 import org.apache.hadoop.conf.Configuration;
26 import org.apache.hadoop.fs.FileSystem;
27 import org.apache.hadoop.fs.Path;
28 import org.apache.hadoop.hbase.HBaseClassTestRule;
29 import org.apache.hadoop.hbase.HBaseTestingUtil;
30 import org.apache.hadoop.hbase.HConstants;
31 import org.apache.hadoop.hbase.regionserver.wal.FSHLog;
32 import org.apache.hadoop.hbase.testclassification.MediumTests;
33 import org.apache.hadoop.hbase.testclassification.RegionServerTests;
34 import org.apache.hadoop.hbase.wal.WAL;
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.ClassRule;
38 import org.junit.Test;
39 import org.junit.experimental.categories.Category;
40 import org.mockito.Mockito;
41 import java.util.HashMap;
42 import java.util.Iterator;
43 import java.util.Map;
45 @Category({RegionServerTests.class, MediumTests.class})
46 public class TestLogRoller {
48 @ClassRule
49 public static final HBaseClassTestRule CLASS_RULE =
50 HBaseClassTestRule.forClass(TestLogRoller.class);
52 private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
54 private static final int LOG_ROLL_PERIOD = 20 * 1000;
55 private static final String LOG_DIR = "WALs";
56 private static final String ARCHIVE_DIR = "archiveWALs";
57 private static final String WAL_PREFIX = "test-log-roller";
58 private static Configuration CONF;
59 private static LogRoller ROLLER;
60 private static Path ROOT_DIR;
61 private static FileSystem FS;
63 @Before
64 public void setup() throws Exception {
65 CONF = TEST_UTIL.getConfiguration();
66 CONF.setInt("hbase.regionserver.logroll.period", LOG_ROLL_PERIOD);
67 CONF.setInt(HConstants.THREAD_WAKE_FREQUENCY, 300);
68 ROOT_DIR = TEST_UTIL.getRandomDir();
69 FS = FileSystem.get(CONF);
70 RegionServerServices services = Mockito.mock(RegionServerServices.class);
71 Mockito.when(services.getConfiguration()).thenReturn(CONF);
72 ROLLER = new LogRoller(services);
73 ROLLER.start();
76 @After
77 public void tearDown() throws Exception {
78 ROLLER.close();
79 FS.close();
80 TEST_UTIL.shutdownMiniCluster();
83 @Test
84 public void testRemoveClosedWAL() throws Exception {
85 assertEquals(0, ROLLER.getWalNeedsRoll().size());
86 for (int i = 1; i <= 3; i++) {
87 FSHLog wal = new FSHLog(FS, ROOT_DIR, LOG_DIR, ARCHIVE_DIR, CONF, null,
88 true, WAL_PREFIX, getWALSuffix(i));
89 ROLLER.addWAL(wal);
92 assertEquals(3, ROLLER.getWalNeedsRoll().size());
93 Iterator<WAL> it = ROLLER.getWalNeedsRoll().keySet().iterator();
94 WAL wal = it.next();
95 assertTrue(ROLLER.getWalNeedsRoll().containsKey(wal));
97 wal.close();
98 Thread.sleep(LOG_ROLL_PERIOD + 5000);
100 assertEquals(2, ROLLER.getWalNeedsRoll().size());
101 assertFalse(ROLLER.getWalNeedsRoll().containsKey(wal));
103 wal = it.next();
104 wal.close();
105 wal = it.next();
106 wal.close();
107 Thread.sleep(LOG_ROLL_PERIOD + 5000);
109 assertEquals(0, ROLLER.getWalNeedsRoll().size());
113 * verify that each wal roll separately
115 @Test
116 public void testRequestRollWithMultiWal() throws Exception {
117 // add multiple wal
118 Map<FSHLog, Path> wals = new HashMap<>();
119 for (int i = 1; i <= 3; i++) {
120 FSHLog wal = new FSHLog(FS, ROOT_DIR, LOG_DIR, ARCHIVE_DIR, CONF, null,
121 true, WAL_PREFIX, getWALSuffix(i));
122 wal.init();
123 wals.put(wal, wal.getCurrentFileName());
124 ROLLER.addWAL(wal);
125 Thread.sleep(1000);
128 // request roll
129 Iterator<Map.Entry<FSHLog, Path>> it = wals.entrySet().iterator();
130 Map.Entry<FSHLog, Path> walEntry = it.next();
131 walEntry.getKey().requestLogRoll();
132 Thread.sleep(5000);
134 assertNotEquals(walEntry.getValue(), walEntry.getKey().getCurrentFileName());
135 walEntry.setValue(walEntry.getKey().getCurrentFileName());
136 while (it.hasNext()) {
137 walEntry = it.next();
138 assertEquals(walEntry.getValue(), walEntry.getKey().getCurrentFileName());
141 // period roll
142 Thread.sleep(LOG_ROLL_PERIOD + 5000);
143 for (Map.Entry<FSHLog, Path> entry : wals.entrySet()) {
144 assertNotEquals(entry.getValue(), entry.getKey().getCurrentFileName());
145 entry.getKey().close();
149 private static String getWALSuffix(int id) {
150 return "." + id;