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 */
018package org.apache.hadoop.hbase.regionserver.wal;
019
020import static org.junit.Assert.assertFalse;
021
022import java.util.ArrayList;
023import java.util.List;
024
025import org.apache.hadoop.fs.Path;
026import org.apache.hadoop.hbase.HBaseTestingUtil;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.client.Put;
029import org.apache.hadoop.hbase.client.Table;
030import org.apache.hadoop.hbase.regionserver.HRegionServer;
031import org.apache.hadoop.hbase.util.Bytes;
032import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
033import org.apache.hadoop.hbase.wal.WAL;
034import org.junit.AfterClass;
035import org.junit.BeforeClass;
036import org.junit.Test;
037import org.slf4j.Logger;
038import org.slf4j.LoggerFactory;
039
040/**
041 * Tests that verifies that the log is forced to be rolled every "hbase.regionserver.logroll.period"
042 */
043public abstract class AbstractTestLogRollPeriod {
044  private static final Logger LOG = LoggerFactory.getLogger(AbstractTestLogRollPeriod.class);
045
046  protected final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
047
048  private final static long LOG_ROLL_PERIOD = 4000;
049
050  @BeforeClass
051  public static void setUpBeforeClass() throws Exception {
052    // disable the ui
053    TEST_UTIL.getConfiguration().setInt("hbase.regionsever.info.port", -1);
054
055    TEST_UTIL.getConfiguration().setLong("hbase.regionserver.logroll.period", LOG_ROLL_PERIOD);
056
057    TEST_UTIL.startMiniCluster();
058  }
059
060  @AfterClass
061  public static void tearDownAfterClass() throws Exception {
062    TEST_UTIL.shutdownMiniCluster();
063  }
064
065  /**
066   * Tests that the LogRoller perform the roll even if there are no edits
067   */
068  @Test
069  public void testNoEdits() throws Exception {
070    TableName tableName = TableName.valueOf("TestLogRollPeriodNoEdits");
071    TEST_UTIL.createTable(tableName, "cf");
072    try {
073      Table table = TEST_UTIL.getConnection().getTable(tableName);
074      try {
075        HRegionServer server = TEST_UTIL.getRSForFirstRegionInTable(tableName);
076        WAL log = server.getWAL(null);
077        checkMinLogRolls(log, 5);
078      } finally {
079        table.close();
080      }
081    } finally {
082      TEST_UTIL.deleteTable(tableName);
083    }
084  }
085
086  /**
087   * Tests that the LogRoller perform the roll with some data in the log
088   */
089  @Test
090  public void testWithEdits() throws Exception {
091    final TableName tableName = TableName.valueOf("TestLogRollPeriodWithEdits");
092    final String family = "cf";
093
094    TEST_UTIL.createTable(tableName, family);
095    try {
096      HRegionServer server = TEST_UTIL.getRSForFirstRegionInTable(tableName);
097      WAL log = server.getWAL(null);
098      final Table table = TEST_UTIL.getConnection().getTable(tableName);
099
100      Thread writerThread = new Thread("writer") {
101        @Override
102        public void run() {
103          try {
104            long row = 0;
105            while (!interrupted()) {
106              Put p = new Put(Bytes.toBytes(String.format("row%d", row)));
107              p.addColumn(Bytes.toBytes(family), Bytes.toBytes("col"), Bytes.toBytes(row));
108              table.put(p);
109              row++;
110
111              Thread.sleep(LOG_ROLL_PERIOD / 16);
112            }
113          } catch (Exception e) {
114            LOG.warn(e.toString(), e);
115          }
116        }
117      };
118
119      try {
120        writerThread.start();
121        checkMinLogRolls(log, 5);
122      } finally {
123        writerThread.interrupt();
124        writerThread.join();
125        table.close();
126      }
127    } finally {
128      TEST_UTIL.deleteTable(tableName);
129    }
130  }
131
132  private void checkMinLogRolls(final WAL log, final int minRolls)
133      throws Exception {
134    final List<Path> paths = new ArrayList<>();
135    log.registerWALActionsListener(new WALActionsListener() {
136      @Override
137      public void postLogRoll(Path oldFile, Path newFile) {
138        LOG.debug("postLogRoll: oldFile="+oldFile+" newFile="+newFile);
139        paths.add(newFile);
140      }
141    });
142
143    // Sleep until we should get at least min-LogRoll events
144    long wtime = EnvironmentEdgeManager.currentTime();
145    Thread.sleep((minRolls + 1) * LOG_ROLL_PERIOD);
146    // Do some extra sleep in case the machine is slow,
147    // and the log-roll is not triggered exactly on LOG_ROLL_PERIOD.
148    final int NUM_RETRIES = 1 + 8 * (minRolls - paths.size());
149    for (int retry = 0; paths.size() < minRolls && retry < NUM_RETRIES; ++retry) {
150      Thread.sleep(LOG_ROLL_PERIOD / 4);
151    }
152    wtime = EnvironmentEdgeManager.currentTime() - wtime;
153    LOG.info(String.format("got %d rolls after %dms (%dms each) - expected at least %d rolls",
154                           paths.size(), wtime, wtime / paths.size(), minRolls));
155    assertFalse(paths.size() < minRolls);
156  }
157}