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.jupiter.api.Assertions.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.HBaseConfiguration;
032import org.apache.hadoop.hbase.client.Connection;
033import org.apache.hadoop.hbase.regionserver.wal.WALEventTrackerListener;
034import org.apache.hadoop.hbase.testclassification.SmallTests;
035import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
036import org.junit.jupiter.api.Tag;
037import org.junit.jupiter.api.Test;
038import org.junit.jupiter.api.TestInfo;
039
040@Tag(SmallTests.TAG)
041public class TestWalEventTrackerQueueService {
042
043  /*
044   * Test whether wal event tracker metrics are being incremented.
045   */
046  @Test
047  public void testMetrics(TestInfo testInfo) throws Exception {
048    String methodName = testInfo.getTestMethod().get().getName();
049    String rsName = "test-region-server";
050    String walName = "test-wal-0";
051    long timeStamp = EnvironmentEdgeManager.currentTime();
052    String walState = WALEventTrackerListener.WalState.ACTIVE.name();
053    long walLength = 100L;
054    WALEventTrackerPayload payload =
055      new WALEventTrackerPayload(rsName, walName, timeStamp, walState, walLength);
056    Configuration conf = HBaseConfiguration.create();
057    conf.setBoolean(WAL_EVENT_TRACKER_ENABLED_KEY, true);
058    conf.setLong(WALEventTrackerTableAccessor.SLEEP_INTERVAL_KEY, 100);
059    MetricsWALEventTrackerSourceImpl source =
060      new MetricsWALEventTrackerSourceImpl(methodName, methodName, methodName, methodName);
061    WALEventTrackerQueueService service = new WALEventTrackerQueueService(conf, source);
062    service.addToQueue(payload);
063    Connection mockConnection = mock(Connection.class);
064    doReturn(conf).when(mockConnection).getConfiguration();
065    // Always throw IOException whenever mock connection is being used.
066    doThrow(new IOException()).when(mockConnection).getTable(WAL_EVENT_TRACKER_TABLE_NAME);
067    assertEquals(0L, source.getFailedPuts());
068    assertEquals(0L, source.getNumRecordsFailedPuts());
069    // Persist all the events.
070    service.persistAll(mockConnection);
071    assertEquals(1L, source.getFailedPuts());
072    assertEquals(1L, source.getNumRecordsFailedPuts());
073    // Verify that we tried MAX_RETRY_ATTEMPTS retry attempts to persist.
074    verify(mockConnection, times(1 + WALEventTrackerTableAccessor.DEFAULT_MAX_ATTEMPTS))
075      .getTable(WAL_EVENT_TRACKER_TABLE_NAME);
076  }
077}