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.namequeues;
019
020import static org.apache.hadoop.hbase.master.waleventtracker.WALEventTrackerTableCreator.WAL_EVENT_TRACKER_ENABLED_KEY;
021import static org.apache.hadoop.hbase.namequeues.WALEventTrackerTableAccessor.WAL_EVENT_TRACKER_TABLE_NAME;
022import static org.junit.Assert.assertEquals;
023import static org.mockito.Mockito.doReturn;
024import static org.mockito.Mockito.doThrow;
025import static org.mockito.Mockito.mock;
026import static org.mockito.Mockito.times;
027import static org.mockito.Mockito.verify;
028
029import java.io.IOException;
030import org.apache.hadoop.conf.Configuration;
031import org.apache.hadoop.hbase.HBaseClassTestRule;
032import org.apache.hadoop.hbase.HBaseConfiguration;
033import org.apache.hadoop.hbase.client.Connection;
034import org.apache.hadoop.hbase.regionserver.wal.WALEventTrackerListener;
035import org.apache.hadoop.hbase.testclassification.SmallTests;
036import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
037import org.junit.ClassRule;
038import org.junit.Rule;
039import org.junit.Test;
040import org.junit.experimental.categories.Category;
041import org.junit.rules.TestName;
042
043@Category(SmallTests.class)
044public class TestWalEventTrackerQueueService {
045
046  @ClassRule
047  public static final HBaseClassTestRule CLASS_RULE =
048    HBaseClassTestRule.forClass(TestWalEventTrackerQueueService.class);
049
050  @Rule
051  public TestName name = new TestName();
052
053  /*
054   * Test whether wal event tracker metrics are being incremented.
055   */
056  @Test
057  public void testMetrics() throws Exception {
058    String rsName = "test-region-server";
059    String walName = "test-wal-0";
060    long timeStamp = EnvironmentEdgeManager.currentTime();
061    String walState = WALEventTrackerListener.WalState.ACTIVE.name();
062    long walLength = 100L;
063    WALEventTrackerPayload payload =
064      new WALEventTrackerPayload(rsName, walName, timeStamp, walState, walLength);
065    Configuration conf = HBaseConfiguration.create();
066    conf.setBoolean(WAL_EVENT_TRACKER_ENABLED_KEY, true);
067    conf.setLong(WALEventTrackerTableAccessor.SLEEP_INTERVAL_KEY, 100);
068    MetricsWALEventTrackerSourceImpl source = new MetricsWALEventTrackerSourceImpl(
069      name.getMethodName(), name.getMethodName(), name.getMethodName(), name.getMethodName());
070    WALEventTrackerQueueService service = new WALEventTrackerQueueService(conf, source);
071    service.addToQueue(payload);
072    Connection mockConnection = mock(Connection.class);
073    doReturn(conf).when(mockConnection).getConfiguration();
074    // Always throw IOException whenever mock connection is being used.
075    doThrow(new IOException()).when(mockConnection).getTable(WAL_EVENT_TRACKER_TABLE_NAME);
076    assertEquals(0L, source.getFailedPuts());
077    assertEquals(0L, source.getNumRecordsFailedPuts());
078    // Persist all the events.
079    service.persistAll(mockConnection);
080    assertEquals(1L, source.getFailedPuts());
081    assertEquals(1L, source.getNumRecordsFailedPuts());
082    // Verify that we tried MAX_RETRY_ATTEMPTS retry attempts to persist.
083    verify(mockConnection, times(1 + WALEventTrackerTableAccessor.DEFAULT_MAX_ATTEMPTS))
084      .getTable(WAL_EVENT_TRACKER_TABLE_NAME);
085  }
086}