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.replication.regionserver;
019
020import static org.junit.Assert.assertEquals;
021import static org.mockito.Mockito.mock;
022
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.fs.Path;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.HBaseConfiguration;
027import org.apache.hadoop.hbase.testclassification.ReplicationTests;
028import org.apache.hadoop.hbase.testclassification.SmallTests;
029import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
030import org.apache.hadoop.hbase.util.ManualEnvironmentEdge;
031import org.junit.ClassRule;
032import org.junit.Test;
033import org.junit.experimental.categories.Category;
034import org.mockito.Mockito;
035
036@Category({ SmallTests.class, ReplicationTests.class })
037public class TestReplicationSourceLogQueue {
038
039  @ClassRule
040  public static final HBaseClassTestRule CLASS_RULE =
041    HBaseClassTestRule.forClass(TestReplicationSourceLogQueue.class);
042
043  /*
044   * Testing enqueue and dequeuing of wal and check age of oldest wal.
045   */
046  @Test
047  public void testEnqueueDequeue() {
048    try {
049      String walGroupId1 = "fake-walgroup-id-1";
050      String walGroupId2 = "fake-walgroup-id-2";
051
052      ManualEnvironmentEdge manualEdge = new ManualEnvironmentEdge();
053      EnvironmentEdgeManager.injectEdge(manualEdge);
054
055      MetricsSource metrics = new MetricsSource("1");
056      Configuration conf = HBaseConfiguration.create();
057      ReplicationSource source = mock(ReplicationSource.class);
058      Mockito.doReturn("peer").when(source).logPeerId();
059      ReplicationSourceLogQueue logQueue = new ReplicationSourceLogQueue(conf, metrics, source);
060      final Path log1 = new Path("log-walgroup-a.8");
061      manualEdge.setValue(10);
062      // Diff of current time (10) and log-walgroup-a.8 timestamp will be 2.
063      logQueue.enqueueLog(log1, walGroupId1);
064      assertEquals(2, logQueue.getOldestWalAge());
065
066      final Path log2 = new Path("log-walgroup-b.4");
067      // Diff of current time (10) and log-walgroup-b.4 will be 6 so oldestWalAge should be 6
068      logQueue.enqueueLog(log2, walGroupId2);
069      assertEquals(6, logQueue.getOldestWalAge());
070
071      // Remove an element from walGroupId2.
072      // After this op, there will be only one element in the queue log-walgroup-a.8
073      logQueue.remove(walGroupId2);
074      assertEquals(2, logQueue.getOldestWalAge());
075
076      // Remove last element from the queue.
077      logQueue.remove(walGroupId1);
078      // This will test the case where there are no elements in the queue.
079      assertEquals(0, logQueue.getOldestWalAge());
080    } finally {
081      EnvironmentEdgeManager.reset();
082    }
083  }
084}