001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019package org.apache.hadoop.hbase.regionserver; 020 021import static org.junit.Assert.assertEquals; 022import static org.junit.Assert.assertNotEquals; 023 024import org.apache.hadoop.conf.Configuration; 025import org.apache.hadoop.fs.FileSystem; 026import org.apache.hadoop.fs.Path; 027import org.apache.hadoop.hbase.HBaseClassTestRule; 028import org.apache.hadoop.hbase.HBaseTestingUtility; 029import org.apache.hadoop.hbase.HConstants; 030import org.apache.hadoop.hbase.regionserver.wal.FSHLog; 031import org.apache.hadoop.hbase.testclassification.MediumTests; 032import org.apache.hadoop.hbase.testclassification.RegionServerTests; 033import org.junit.After; 034import org.junit.Before; 035import org.junit.ClassRule; 036import org.junit.Test; 037import org.junit.experimental.categories.Category; 038import org.mockito.Mockito; 039import java.util.HashMap; 040import java.util.Iterator; 041import java.util.Map; 042 043@Category({RegionServerTests.class, MediumTests.class}) 044public class TestLogRoller { 045 046 @ClassRule 047 public static final HBaseClassTestRule CLASS_RULE = 048 HBaseClassTestRule.forClass(TestLogRoller.class); 049 050 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 051 052 private static final int LOG_ROLL_PERIOD = 20 * 1000; 053 private static final String LOG_DIR = "WALs"; 054 private static final String ARCHIVE_DIR = "archiveWALs"; 055 private static final String WAL_PREFIX = "test-log-roller"; 056 private static Configuration CONF; 057 private static LogRoller ROLLER; 058 private static Path ROOT_DIR; 059 private static FileSystem FS; 060 061 @Before 062 public void setup() throws Exception { 063 CONF = TEST_UTIL.getConfiguration(); 064 CONF.setInt("hbase.regionserver.logroll.period", LOG_ROLL_PERIOD); 065 CONF.setInt(HConstants.THREAD_WAKE_FREQUENCY, 300); 066 ROOT_DIR = TEST_UTIL.getRandomDir(); 067 FS = FileSystem.get(CONF); 068 RegionServerServices services = Mockito.mock(RegionServerServices.class); 069 Mockito.when(services.getConfiguration()).thenReturn(CONF); 070 ROLLER = new LogRoller(services); 071 ROLLER.start(); 072 } 073 074 @After 075 public void tearDown() throws Exception { 076 ROLLER.close(); 077 FS.close(); 078 TEST_UTIL.shutdownMiniCluster(); 079 } 080 081 /** 082 * verify that each wal roll separately 083 */ 084 @Test 085 public void testRequestRollWithMultiWal() throws Exception { 086 // add multiple wal 087 Map<FSHLog, Path> wals = new HashMap<>(); 088 for (int i = 1; i <= 3; i++) { 089 FSHLog wal = new FSHLog(FS, ROOT_DIR, LOG_DIR, ARCHIVE_DIR, CONF, null, 090 true, WAL_PREFIX, "." + i); 091 wal.init(); 092 wals.put(wal, wal.getCurrentFileName()); 093 ROLLER.addWAL(wal); 094 Thread.sleep(1000); 095 } 096 097 // request roll 098 Iterator<Map.Entry<FSHLog, Path>> it = wals.entrySet().iterator(); 099 Map.Entry<FSHLog, Path> walEntry = it.next(); 100 walEntry.getKey().requestLogRoll(); 101 Thread.sleep(5000); 102 103 assertNotEquals(walEntry.getValue(), walEntry.getKey().getCurrentFileName()); 104 walEntry.setValue(walEntry.getKey().getCurrentFileName()); 105 while (it.hasNext()) { 106 walEntry = it.next(); 107 assertEquals(walEntry.getValue(), walEntry.getKey().getCurrentFileName()); 108 } 109 110 // period roll 111 Thread.sleep(LOG_ROLL_PERIOD + 5000); 112 for (Map.Entry<FSHLog, Path> entry : wals.entrySet()) { 113 assertNotEquals(entry.getValue(), entry.getKey().getCurrentFileName()); 114 entry.getKey().close(); 115 } 116 } 117}