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.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,"+System.currentTimeMillis();
066    nodes.add(server);
067    when(recoverableZooKeeperMock.getChildren("/hbase/rs", null)).thenReturn(nodes);
068    when(recoverableZooKeeperMock.getChildren("/hbase/replication/rs", null)).
069        thenReturn(nodes);
070    List<String> queuesIds = new ArrayList<>();
071    queuesIds.add("1");
072    when(recoverableZooKeeperMock.getChildren("/hbase/replication/rs/"+server, null)).
073        thenReturn(queuesIds);
074    List<String> wals = new ArrayList<>();
075    wals.add("rs1%2C60964%2C1549394085556.1549394101427");
076    wals.add("rs1%2C60964%2C1549394085556.1549394101426");
077    wals.add("rs1%2C60964%2C1549394085556.1549394101428");
078    when(recoverableZooKeeperMock.getChildren("/hbase/replication/rs/"+server+"/1",
079        null)).thenReturn(wals);
080    DumpReplicationQueues dumpQueues = new DumpReplicationQueues();
081    Set<String> peerIds = new HashSet<>();
082    peerIds.add("1");
083    dumpQueues.setConf(config);
084    String dump = dumpQueues.dumpQueues(zkWatcherMock, peerIds, false);
085    String[] parsedDump = dump.split("Replication position for");
086    assertEquals("Parsed dump should have 4 parts.", 4, parsedDump.length);
087    assertTrue("First wal should be rs1%2C60964%2C1549394085556.1549394101426, but got: "
088        + parsedDump[1],
089        parsedDump[1].indexOf("rs1%2C60964%2C1549394085556.1549394101426")>=0);
090    assertTrue("Second wal should be rs1%2C60964%2C1549394085556.1549394101427, but got: "
091            + parsedDump[2],
092        parsedDump[2].indexOf("rs1%2C60964%2C1549394085556.1549394101427")>=0);
093    assertTrue("Third wal should be rs1%2C60964%2C1549394085556.1549394101428, but got: "
094            + parsedDump[3],
095        parsedDump[3].indexOf("rs1%2C60964%2C1549394085556.1549394101428")>=0);
096  }
097
098}