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