Пример #1
0
  @Test
  public void testZkClientWhenZKIsDownAndRestarts() throws Exception {

    // Iterate updating the timestamp and check the final value
    long previousMaxTimestamp = INITIAL_MAX_TS_VALUE;
    for (int i = 0; i < ITERATION_COUNT; i++) {
      long newMaxTimestamp = previousMaxTimestamp + 1_000_000;
      storage.updateMaxTimestamp(previousMaxTimestamp, newMaxTimestamp);
      previousMaxTimestamp = newMaxTimestamp;
    }
    assertEquals(storage.getMaxTimestamp(), 1_000_000 * ITERATION_COUNT);

    // Stop ZK Server, expect the IO exception, reconnect and get the right value
    LOG.info("Stopping ZK Server");
    zkServer.stop();
    LOG.info("ZK Server Stopped");

    try {
      storage.getMaxTimestamp();
      fail();
    } catch (IOException ioe) {
      LOG.info("Expected exception", ioe);
    }

    LOG.info("Restarting ZK again");
    zkServer.restart();
    assertEquals(storage.getMaxTimestamp(), 1_000_000 * ITERATION_COUNT);
  }
Пример #2
0
  @Test
  public void testZkClientLosingSession() throws Exception {

    // Cut the session in the server through the client
    long sessionId = zkClient.getZookeeperClient().getZooKeeper().getSessionId();
    byte[] sessionPasswd = zkClient.getZookeeperClient().getZooKeeper().getSessionPasswd();
    ZooKeeper zk = new ZooKeeper(ZK_CLUSTER, 1000, null, sessionId, sessionPasswd);
    zk.close();
    LOG.info("ZKClient session closed");

    // Iterate updating the timestamp and check the final value
    long previousMaxTimestamp = INITIAL_MAX_TS_VALUE;
    for (int i = 0; i < ITERATION_COUNT; i++) {
      long newMaxTimestamp = previousMaxTimestamp + 1_000_000;
      storage.updateMaxTimestamp(previousMaxTimestamp, newMaxTimestamp);
      LOG.info("Updating timestamp. Previous/New {}/{}", previousMaxTimestamp, newMaxTimestamp);
      previousMaxTimestamp = newMaxTimestamp;
    }
    assertEquals(storage.getMaxTimestamp(), 1_000_000 * ITERATION_COUNT);
  }
Пример #3
0
  @Test
  public void testBasicFunctionality() throws Exception {

    // Check ZNode for timestamp exists (storage instantiation should create it)
    Stat zNodeStats = zkClient.checkExists().forPath(TIMESTAMP_ZNODE);
    assertEquals(zNodeStats.getVersion(), 0);

    // Initial checks
    assertEquals(storage.getMaxTimestamp(), INITIAL_MAX_TS_VALUE);
    byte[] data = zkClient.getData().forPath(TIMESTAMP_ZNODE);
    assertEquals(data.length, BYTES_IN_LONG);

    // Check new timestamp does not allow negative values...
    try {
      storage.updateMaxTimestamp(INITIAL_MAX_TS_VALUE, NEGATIVE_TS);
      fail();
    } catch (IllegalArgumentException e) {
      LOG.info("Expected exception", e);
    }

    // ...nor is less than previous timestamp
    try {
      storage.updateMaxTimestamp(1, 0);
      fail();
    } catch (IllegalArgumentException e) {
      LOG.info("Expected exception", e);
    }

    // Check that the original version is still there
    zNodeStats = zkClient.checkExists().forPath(TIMESTAMP_ZNODE);
    assertEquals(zNodeStats.getVersion(), 0);

    // Iterate updating the timestamp and check the final value
    long previousMaxTimestamp = INITIAL_MAX_TS_VALUE;
    for (int i = 0; i < ITERATION_COUNT; i++) {
      long newMaxTimestamp = previousMaxTimestamp + 1_000_000;
      storage.updateMaxTimestamp(previousMaxTimestamp, newMaxTimestamp);
      previousMaxTimestamp = newMaxTimestamp;
    }
    assertEquals(storage.getMaxTimestamp(), 1_000_000 * ITERATION_COUNT);
    // Check the znode version has changed accordingly
    zNodeStats = zkClient.checkExists().forPath(TIMESTAMP_ZNODE);
    assertEquals(zNodeStats.getVersion(), ITERATION_COUNT);

    // Check exceptions
    doThrow(new RuntimeException()).when(storageInternalZKClient).getData();
    try {
      storage.getMaxTimestamp();
      fail();
    } catch (IOException e) {
      LOG.info("Expected exception", e);
    }

    doThrow(new RuntimeException()).when(storageInternalZKClient).setData();
    try {
      storage.updateMaxTimestamp(INITIAL_MAX_TS_VALUE, INITIAL_MAX_TS_VALUE + 1_000_000);
      fail();
    } catch (IOException e) {
      LOG.info("Expected exception", e);
    }

    // Reset the mock and double-check last result
    Mockito.reset(storageInternalZKClient);
    assertEquals(storage.getMaxTimestamp(), 1_000_000 * ITERATION_COUNT);

    // Finally check the znode version is still the same
    zNodeStats = zkClient.checkExists().forPath(TIMESTAMP_ZNODE);
    assertEquals(zNodeStats.getVersion(), ITERATION_COUNT);
  }