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.zookeeper;
019
020import static org.hamcrest.CoreMatchers.hasItems;
021import static org.hamcrest.MatcherAssert.assertThat;
022import static org.junit.Assert.assertEquals;
023
024import java.io.IOException;
025import org.apache.hadoop.hbase.Abortable;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.HBaseZKTestingUtility;
028import org.apache.hadoop.hbase.HConstants;
029import org.apache.hadoop.hbase.ServerName;
030import org.apache.hadoop.hbase.ZooKeeperConnectionException;
031import org.apache.hadoop.hbase.testclassification.MediumTests;
032import org.apache.hadoop.hbase.testclassification.ZKTests;
033import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
034import org.apache.zookeeper.KeeperException;
035import org.junit.After;
036import org.junit.AfterClass;
037import org.junit.Before;
038import org.junit.BeforeClass;
039import org.junit.ClassRule;
040import org.junit.Rule;
041import org.junit.Test;
042import org.junit.experimental.categories.Category;
043import org.junit.rules.TestName;
044import org.slf4j.Logger;
045import org.slf4j.LoggerFactory;
046
047import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
048
049@Category({ ZKTests.class, MediumTests.class })
050public class TestRegionServerAddressTracker {
051
052  @ClassRule
053  public static final HBaseClassTestRule CLASS_RULE =
054    HBaseClassTestRule.forClass(TestRegionServerAddressTracker.class);
055
056  private static final Logger LOG = LoggerFactory.getLogger(TestRegionServerAddressTracker.class);
057
058  private static final HBaseZKTestingUtility TEST_UTIL = new HBaseZKTestingUtility();
059
060  private ZKWatcher zk;
061
062  private RegionServerAddressTracker tracker;
063
064  @Rule
065  public TestName name = new TestName();
066
067  @BeforeClass
068  public static void setUpBeforeClass() throws Exception {
069    TEST_UTIL.startMiniZKCluster();
070  }
071
072  @AfterClass
073  public static void tearDownAfterClass() throws Exception {
074    TEST_UTIL.shutdownMiniZKCluster();
075  }
076
077  @Before
078  public void setUp() throws ZooKeeperConnectionException, IOException, KeeperException {
079    TEST_UTIL.getConfiguration().set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/" + name.getMethodName());
080    zk = new ZKWatcher(TEST_UTIL.getConfiguration(), name.getMethodName(), null);
081    ZKUtil.createWithParents(zk, zk.getZNodePaths().rsZNode);
082    tracker = new RegionServerAddressTracker(zk, new WarnOnlyAbortable());
083  }
084
085  @After
086  public void tearDown() throws IOException {
087    Closeables.close(zk, true);
088  }
089
090  @Test
091  public void test() throws KeeperException {
092    ServerName rs1 = ServerName.valueOf("127.0.0.1", 16000, EnvironmentEdgeManager.currentTime());
093    ZKUtil.createWithParents(zk, ZNodePaths.joinZNode(zk.getZNodePaths().rsZNode, rs1.toString()));
094    TEST_UTIL.waitFor(10000, () -> tracker.getRegionServers().size() == 1);
095    assertEquals(rs1, tracker.getRegionServers().get(0));
096
097    ServerName rs2 = ServerName.valueOf("127.0.0.2", 16000, EnvironmentEdgeManager.currentTime());
098    ZKUtil.createWithParents(zk, ZNodePaths.joinZNode(zk.getZNodePaths().rsZNode, rs2.toString()));
099    TEST_UTIL.waitFor(10000, () -> tracker.getRegionServers().size() == 2);
100    assertThat(tracker.getRegionServers(), hasItems(rs1, rs2));
101
102    ZKUtil.deleteNode(zk, ZNodePaths.joinZNode(zk.getZNodePaths().rsZNode, rs1.toString()));
103    TEST_UTIL.waitFor(10000, () -> tracker.getRegionServers().size() == 1);
104    assertEquals(rs2, tracker.getRegionServers().get(0));
105
106    ZKUtil.deleteNode(zk, ZNodePaths.joinZNode(zk.getZNodePaths().rsZNode, rs2.toString()));
107    TEST_UTIL.waitFor(10000, () -> tracker.getRegionServers().isEmpty());
108  }
109
110  private static final class WarnOnlyAbortable implements Abortable {
111    @Override
112    public void abort(String why, Throwable e) {
113      LOG.warn("RegionServerAddressTracker received abort, ignoring.  Reason: {}", why, e);
114    }
115
116    @Override
117    public boolean isAborted() {
118      return false;
119    }
120  }
121}