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(false);
045    metricsWAL.logRollRequested(true);
046
047    // Log roll was requested twice
048    verify(source, times(2)).incrementLogRollRequested();
049    // One was because of low replication on the hlog.
050    verify(source, times(1)).incrementLowReplicationLogRoll();
051  }
052
053  @Test
054  public void testPostSync() throws Exception {
055    long nanos = TimeUnit.MILLISECONDS.toNanos(145);
056    MetricsWALSource source = mock(MetricsWALSourceImpl.class);
057    MetricsWAL metricsWAL = new MetricsWAL(source);
058    metricsWAL.postSync(nanos, 1);
059    verify(source, times(1)).incrementSyncTime(145);
060  }
061
062  @Test
063  public void testSlowAppend() throws Exception {
064    MetricsWALSource source = new MetricsWALSourceImpl();
065    MetricsWAL metricsWAL = new MetricsWAL(source);
066    // One not so slow append (< 1000)
067    metricsWAL.postAppend(1, 900, null, null);
068    // Two slow appends (> 1000)
069    metricsWAL.postAppend(1, 1010, null, null);
070    metricsWAL.postAppend(1, 2000, null, null);
071    assertEquals(2, source.getSlowAppendCount());
072  }
073
074  @Test
075  public void testWalWrittenInBytes() throws Exception {
076    MetricsWALSource source = mock(MetricsWALSourceImpl.class);
077    MetricsWAL metricsWAL = new MetricsWAL(source);
078    metricsWAL.postAppend(100, 900, null, null);
079    metricsWAL.postAppend(200, 2000, null, null);
080    verify(source, times(1)).incrementWrittenBytes(100);
081    verify(source, times(1)).incrementWrittenBytes(200);
082  }
083
084}