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