Package com.xiaomi.infra.chronos.zookeeper

Examples of com.xiaomi.infra.chronos.zookeeper.FailoverWatcher


    properties.setProperty(FailoverServer.BASE_ZNODE, "/test-failover");
    properties.setProperty(FailoverServer.ZK_QUORUM,
      ZKConfig.getZKQuorumServersString(TEST_UTIL.getConfiguration()));
    properties.setProperty(FailoverServer.SESSION_TIMEOUT, String.valueOf(3000));

    FailoverWatcher failoverWatcher = new FailoverWatcher(properties);
    return new TestableFailoverServer(failoverWatcher);
  }
View Full Code Here


    TEST_UTIL.shutdownMiniZKCluster();
  }

  @Before
  public void resetZooKeeper() throws IOException, KeeperException {
    FailoverWatcher failoverWatcher = createFailoverWatcher(new HostPort("127.0.0.1", 10086));
    ZooKeeperUtil.deleteNodeRecursively(failoverWatcher, failoverWatcher.getBaseZnode());
    failoverWatcher.close();
  }
View Full Code Here

    properties.setProperty(FailoverServer.ZK_QUORUM,
      ZKConfig.getZKQuorumServersString(TEST_UTIL.getConfiguration()));
    properties.setProperty(FailoverServer.SESSION_TIMEOUT, String.valueOf(3000));
    properties.setProperty(FailoverServer.CONNECT_RETRY_TIMES, String.valueOf(10));

    return new FailoverWatcher(properties);
  }
View Full Code Here

    return new FailoverWatcher(properties);
  }

  @Test
  public void testInitZnode() throws IOException, KeeperException {
    FailoverWatcher failoverWatcher = createFailoverWatcher(new HostPort("127.0.0.1", 10086));

    assertTrue(ZooKeeperUtil.watchAndCheckExists(failoverWatcher, failoverWatcher.getBaseZnode()));
    assertTrue(ZooKeeperUtil.watchAndCheckExists(failoverWatcher,
      failoverWatcher.getBackupServersZnode()));

    failoverWatcher.close();
  }
View Full Code Here

  @Test
  public void testBlockUntilActive() throws IOException, KeeperException, InterruptedException {
    // the first server, expect to be active master
    HostPort hostPort1 = new HostPort("127.0.0.1", 11111);
    FailoverWatcher failoverWatcher1 = createFailoverWatcher(hostPort1);
    failoverWatcher1.blockUntilActive();

    assertTrue(failoverWatcher1.hasActiveServer());
    String activeServer = new String(ZooKeeperUtil.getDataAndWatch(failoverWatcher1,
      failoverWatcher1.masterZnode));
    assertTrue(activeServer.equals(hostPort1.getHostPort()));

    // the second server, expect to be backup master
    HostPort hostPort2 = new HostPort("127.0.0.1", 22222);
    final FailoverWatcher failoverWatcher2 = createFailoverWatcher(hostPort2);
    Thread thread2 = new Thread() {
      @Override
      public void run() {
        failoverWatcher2.blockUntilActive();
      }
    };
    thread2.start();

    Thread.sleep(500); // wait for second watcher to create znode
    List<String> backupMastersList = ZooKeeperUtil.listChildrenAndWatchForNewChildren(
      failoverWatcher1, failoverWatcher1.backupServersZnode);
    assertTrue(failoverWatcher2.hasActiveServer());
    assertTrue(backupMastersList.contains(failoverWatcher2.getHostPort().getHostPort()));

    thread2.interrupt();
    failoverWatcher1.close();
    failoverWatcher2.close();
  }
View Full Code Here

  @Test
  public void testRestartActiveMaster() throws IOException, KeeperException {
    // the first server, expect to be active master
    HostPort hostPort1 = new HostPort("127.0.0.1", 11111);
    FailoverWatcher failoverWatcher1 = createFailoverWatcher(hostPort1);
    failoverWatcher1.blockUntilActive();

    assertTrue(failoverWatcher1.hasActiveServer());
    String activeServer = new String(ZooKeeperUtil.getDataAndWatch(failoverWatcher1,
      failoverWatcher1.masterZnode));
    assertTrue(activeServer.equals(hostPort1.getHostPort()));

    // the same server start, expect to restart active master
    FailoverWatcher failoverWatcher2 = createFailoverWatcher(hostPort1);
    failoverWatcher2.blockUntilActive();
    assertTrue(failoverWatcher2.hasActiveServer());
    String activeServer2 = new String(ZooKeeperUtil.getDataAndWatch(failoverWatcher1,
      failoverWatcher1.masterZnode));
    assertTrue(activeServer2.equals(hostPort1.getHostPort()));

    failoverWatcher1.close();
    failoverWatcher2.close();
  }
View Full Code Here

  @Test
  public void testHandleMasterNodeChange() throws IOException, KeeperException,
      InterruptedException {
    HostPort hostPort1 = new HostPort("127.0.0.1", 11111);
    FailoverWatcher failoverWatcher1 = createFailoverWatcher(hostPort1);
    failoverWatcher1.blockUntilActive();

    // hostPort2 is backup master
    HostPort hostPort2 = new HostPort("127.0.0.1", 22222);
    final FailoverWatcher failoverWatcher2 = createFailoverWatcher(hostPort2);
    Thread thread2 = new Thread() {
      @Override
      public void run() {
        failoverWatcher2.blockUntilActive();
      }
    };
    thread2.start();

    // hostPort3 is backup master
    HostPort hostPort3 = new HostPort("127.0.0.1", 33333);
    final FailoverWatcher failoverWatcher3 = createFailoverWatcher(hostPort3);
    Thread thread3 = new Thread() {
      @Override
      public void run() {
        failoverWatcher3.blockUntilActive();
      }
    };
    thread3.start();

    // delete the master node, then one of them will become master, the other will be backup master
View Full Code Here

  }

  @Test
  public void testCloseMaster() throws KeeperException, IOException {
    HostPort hostPort1 = new HostPort("127.0.0.1", 11111);
    FailoverWatcher failoverWatcher1 = createFailoverWatcher(hostPort1);
    failoverWatcher1.blockUntilActive();

    HostPort hostPort2 = new HostPort("127.0.0.1", 22222);
    final FailoverWatcher failoverWatcher2 = createFailoverWatcher(hostPort2);
    Thread thread2 = new Thread() {
      @Override
      public void run() {
        failoverWatcher2.blockUntilActive();
      }
    };
    thread2.start();

    // delete master znode, then failoverWatcher2 will be master
    ZooKeeperUtil.deleteNode(failoverWatcher2, failoverWatcher2.getMasterZnode());
    String masterZnodeData = new String(ZooKeeperUtil.getDataAndWatch(failoverWatcher2,
      failoverWatcher2.masterZnode));
    List<String> backupMasterList = ZooKeeperUtil.listChildrenAndWatchForNewChildren(
      failoverWatcher2, failoverWatcher2.backupServersZnode);
    assertTrue(masterZnodeData.equals(failoverWatcher2.getHostPort().getHostPort()));
    assertTrue(backupMasterList.size() == 0);

    failoverWatcher1.close();
    failoverWatcher2.close();
  }
View Full Code Here

  }

  @Test
  public void testCloseBackupMaster() throws IOException, KeeperException, InterruptedException {
    HostPort hostPort1 = new HostPort("127.0.0.1", 11111);
    FailoverWatcher failoverWatcher1 = createFailoverWatcher(hostPort1);
    failoverWatcher1.blockUntilActive();

    HostPort hostPort2 = new HostPort("127.0.0.1", 22222);
    final FailoverWatcher failoverWatcher2 = createFailoverWatcher(hostPort2);
    Thread thread2 = new Thread() {
      @Override
      public void run() {
        failoverWatcher2.blockUntilActive();
      }
    };
    thread2.start();

    Thread.sleep(500); // wait for backup master to init
    thread2.interrupt(); // close backup master and failoverWatcher1 is still active master
    ZooKeeperUtil.deleteNode(failoverWatcher1, failoverWatcher1.getBackupServersZnode() + "/"
        + hostPort2.getHostPort());

    String masterZnodeData = new String(ZooKeeperUtil.getDataAndWatch(failoverWatcher1,
      failoverWatcher1.masterZnode));
    List<String> backupMasterList = ZooKeeperUtil.listChildrenAndWatchForNewChildren(
      failoverWatcher1, failoverWatcher1.backupServersZnode);
    assertTrue(masterZnodeData.equals(failoverWatcher1.getHostPort().getHostPort()));
    assertTrue(backupMasterList.size() == 0);

    failoverWatcher1.close();
    failoverWatcher2.close();
  }
View Full Code Here

TOP

Related Classes of com.xiaomi.infra.chronos.zookeeper.FailoverWatcher

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.