View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
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     * verify that each wal roll separately
80     */
81    @Test
82    public void testRequestRollWithMultiWal() throws Exception {
83      // add multiple wal
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      // request roll
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     // period roll
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 }