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.assertEquals;
021import static org.mockito.Mockito.mock;
022import static org.mockito.Mockito.times;
023import static org.mockito.Mockito.verify;
024
025import java.util.concurrent.TimeUnit;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.testclassification.MiscTests;
028import org.apache.hadoop.hbase.testclassification.SmallTests;
029import org.junit.ClassRule;
030import org.junit.Test;
031import org.junit.experimental.categories.Category;
032
033@Category({MiscTests.class, SmallTests.class})
034public class TestMetricsWAL {
035
036  @ClassRule
037  public static final HBaseClassTestRule CLASS_RULE =
038      HBaseClassTestRule.forClass(TestMetricsWAL.class);
039
040  @Test
041  public void testLogRollRequested() throws Exception {
042    MetricsWALSource source = mock(MetricsWALSourceImpl.class);
043    MetricsWAL metricsWAL = new MetricsWAL(source);
044    metricsWAL.logRollRequested(WALActionsListener.RollRequestReason.ERROR);
045    metricsWAL.logRollRequested(WALActionsListener.RollRequestReason.LOW_REPLICATION);
046    metricsWAL.logRollRequested(WALActionsListener.RollRequestReason.SLOW_SYNC);
047    metricsWAL.logRollRequested(WALActionsListener.RollRequestReason.SIZE);
048
049    // Log roll was requested four times
050    verify(source, times(4)).incrementLogRollRequested();
051    // One was because of an IO error.
052    verify(source, times(1)).incrementErrorLogRoll();
053    // One was because of low replication on the hlog.
054    verify(source, times(1)).incrementLowReplicationLogRoll();
055    // One was because of slow sync on the hlog.
056    verify(source, times(1)).incrementSlowSyncLogRoll();
057    // One was because of hlog file length limit.
058    verify(source, times(1)).incrementSizeLogRoll();
059  }
060
061  @Test
062  public void testPostSync() throws Exception {
063    long nanos = TimeUnit.MILLISECONDS.toNanos(145);
064    MetricsWALSource source = mock(MetricsWALSourceImpl.class);
065    MetricsWAL metricsWAL = new MetricsWAL(source);
066    metricsWAL.postSync(nanos, 1);
067    verify(source, times(1)).incrementSyncTime(145);
068  }
069
070  @Test
071  public void testSlowAppend() throws Exception {
072    MetricsWALSource source = new MetricsWALSourceImpl();
073    MetricsWAL metricsWAL = new MetricsWAL(source);
074    // One not so slow append (< 1000)
075    metricsWAL.postAppend(1, 900, null, null);
076    // Two slow appends (> 1000)
077    metricsWAL.postAppend(1, 1010, null, null);
078    metricsWAL.postAppend(1, 2000, null, null);
079    assertEquals(2, source.getSlowAppendCount());
080  }
081
082  @Test
083  public void testWalWrittenInBytes() throws Exception {
084    MetricsWALSource source = mock(MetricsWALSourceImpl.class);
085    MetricsWAL metricsWAL = new MetricsWAL(source);
086    metricsWAL.postAppend(100, 900, null, null);
087    metricsWAL.postAppend(200, 2000, null, null);
088    verify(source, times(1)).incrementWrittenBytes(100);
089    verify(source, times(1)).incrementWrittenBytes(200);
090  }
091
092}