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.junit.Assert.assertTrue;
022import static org.mockito.Mockito.mock;
023import static org.mockito.Mockito.when;
024
025import java.util.ArrayList;
026import java.util.HashSet;
027import java.util.List;
028import java.util.Set;
029import org.apache.hadoop.conf.Configuration;
030import org.apache.hadoop.hbase.HBaseClassTestRule;
031import org.apache.hadoop.hbase.HBaseConfiguration;
032import org.apache.hadoop.hbase.testclassification.ReplicationTests;
033import org.apache.hadoop.hbase.testclassification.SmallTests;
034import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
035import org.apache.hadoop.hbase.zookeeper.RecoverableZooKeeper;
036import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
037import org.apache.hadoop.hbase.zookeeper.ZNodePaths;
038import org.junit.ClassRule;
039import org.junit.Test;
040import org.junit.experimental.categories.Category;
041
042/**
043 * Tests for DumpReplicationQueues tool
044 */
045@Category({ ReplicationTests.class, SmallTests.class })
046public class TestDumpReplicationQueues {
047
048  @ClassRule
049  public static final HBaseClassTestRule CLASS_RULE =
050    HBaseClassTestRule.forClass(TestDumpReplicationQueues.class);
051
052  /**
053   * Makes sure dumpQueues returns wals znodes ordered chronologically.
054   * @throws Exception if dumpqueues finds any error while handling list of znodes.
055   */
056  @Test
057  public void testDumpReplicationReturnsWalSorted() throws Exception {
058    Configuration config = HBaseConfiguration.create();
059    ZKWatcher zkWatcherMock = mock(ZKWatcher.class);
060    ZNodePaths zNodePath = new ZNodePaths(config);
061    RecoverableZooKeeper recoverableZooKeeperMock = mock(RecoverableZooKeeper.class);
062    when(zkWatcherMock.getRecoverableZooKeeper()).thenReturn(recoverableZooKeeperMock);
063    when(zkWatcherMock.getZNodePaths()).thenReturn(zNodePath);
064    List<String> nodes = new ArrayList<>();
065    String server = "rs1,60030," + EnvironmentEdgeManager.currentTime();
066    nodes.add(server);
067    when(recoverableZooKeeperMock.getChildren("/hbase/rs", null)).thenReturn(nodes);
068    when(recoverableZooKeeperMock.getChildren("/hbase/replication/rs", null)).thenReturn(nodes);
069    List<String> queuesIds = new ArrayList<>();
070    queuesIds.add("1");
071    when(recoverableZooKeeperMock.getChildren("/hbase/replication/rs/" + server, null))
072      .thenReturn(queuesIds);
073    List<String> wals = new ArrayList<>();
074    wals.add("rs1%2C60964%2C1549394085556.1549394101427");
075    wals.add("rs1%2C60964%2C1549394085556.1549394101426");
076    wals.add("rs1%2C60964%2C1549394085556.1549394101428");
077    when(recoverableZooKeeperMock.getChildren("/hbase/replication/rs/" + server + "/1", null))
078      .thenReturn(wals);
079    DumpReplicationQueues dumpQueues = new DumpReplicationQueues();
080    Set<String> peerIds = new HashSet<>();
081    peerIds.add("1");
082    dumpQueues.setConf(config);
083    String dump = dumpQueues.dumpQueues(zkWatcherMock, peerIds, false);
084    String[] parsedDump = dump.split("Replication position for");
085    assertEquals("Parsed dump should have 4 parts.", 4, parsedDump.length);
086    assertTrue(
087      "First wal should be rs1%2C60964%2C1549394085556.1549394101426, but got: " + parsedDump[1],
088      parsedDump[1].indexOf("rs1%2C60964%2C1549394085556.1549394101426") >= 0);
089    assertTrue(
090      "Second wal should be rs1%2C60964%2C1549394085556.1549394101427, but got: " + parsedDump[2],
091      parsedDump[2].indexOf("rs1%2C60964%2C1549394085556.1549394101427") >= 0);
092    assertTrue(
093      "Third wal should be rs1%2C60964%2C1549394085556.1549394101428, but got: " + parsedDump[3],
094      parsedDump[3].indexOf("rs1%2C60964%2C1549394085556.1549394101428") >= 0);
095  }
096
097}