View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.replication.regionserver;
19  
20  import static org.junit.Assert.assertEquals;
21  import org.apache.hadoop.conf.Configuration;
22  import org.apache.hadoop.fs.Path;
23  import org.apache.hadoop.hbase.HBaseConfiguration;
24  import org.apache.hadoop.hbase.testclassification.ReplicationTests;
25  import org.apache.hadoop.hbase.testclassification.SmallTests;
26  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
27  import org.apache.hadoop.hbase.util.ManualEnvironmentEdge;
28  import org.junit.Test;
29  import org.junit.experimental.categories.Category;
30  
31  @Category({SmallTests.class,ReplicationTests.class})
32  public class TestReplicationSourceLogQueue {
33  
34    /*
35      Testing enqueue and dequeuing of wal and check age of oldest wal.
36    */
37    @Test
38    public void testEnqueueDequeue() {
39      try {
40        String walGroupId1 = "fake-walgroup-id-1";
41        String walGroupId2 = "fake-walgroup-id-2";
42  
43        ManualEnvironmentEdge manualEdge = new ManualEnvironmentEdge();
44        EnvironmentEdgeManager.injectEdge(manualEdge);
45  
46        MetricsSource metrics = new MetricsSource("1");
47        Configuration conf = HBaseConfiguration.create();
48        ReplicationSourceLogQueue logQueue = new ReplicationSourceLogQueue(conf, metrics);
49        final Path log1 = new Path("log-walgroup-a.8");
50        manualEdge.setValue(10);
51        // Diff of current time (10) and  log-walgroup-a.8 timestamp will be 2.
52        logQueue.enqueueLog(log1, walGroupId1);
53        assertEquals(2, logQueue.getOldestWalAge());
54  
55        final Path log2 = new Path("log-walgroup-b.4");
56        // Diff of current time (10) and log-walgroup-b.4 will be 6 so oldestWalAge should be 6
57        logQueue.enqueueLog(log2, walGroupId2);
58        assertEquals(6, logQueue.getOldestWalAge());
59  
60        // Remove an element from walGroupId2.
61        // After this op, there will be only one element in the queue log-walgroup-a.8
62        logQueue.remove(walGroupId2);
63        assertEquals(2, logQueue.getOldestWalAge());
64  
65        // Remove last element from the queue.
66        logQueue.remove(walGroupId1);
67        // This will test the case where there are no elements in the queue.
68        assertEquals(0, logQueue.getOldestWalAge());
69      } finally {
70        EnvironmentEdgeManager.reset();
71      }
72    }
73  }