Example usage for org.apache.zookeeper.server ZooKeeperServer closeSession

List of usage examples for org.apache.zookeeper.server ZooKeeperServer closeSession

Introduction

In this page you can find the example usage for org.apache.zookeeper.server ZooKeeperServer closeSession.

Prototype

public void closeSession(long sessionId) 

Source Link

Usage

From source file:org.apache.hadoop.ha.TestActiveStandbyElectorRealZK.java

License:Apache License

@Test(timeout = 15000)
public void testHandleSessionExpiration() throws Exception {
    ActiveStandbyElectorCallback cb = cbs[0];
    byte[] appData = appDatas[0];
    ActiveStandbyElector elector = electors[0];

    // Let the first elector become active
    elector.ensureParentZNode();// www .j av a 2s  .  c  om
    elector.joinElection(appData);
    ZooKeeperServer zks = getServer(serverFactory);
    ActiveStandbyElectorTestUtil.waitForActiveLockData(null, zks, PARENT_DIR, appData);
    Mockito.verify(cb, Mockito.timeout(1000)).becomeActive();
    checkFatalsAndReset();

    LOG.info("========================== Expiring session");
    zks.closeSession(elector.getZKSessionIdForTests());

    // Should enter neutral mode when disconnected
    Mockito.verify(cb, Mockito.timeout(1000)).enterNeutralMode();

    // Should re-join the election and regain active
    ActiveStandbyElectorTestUtil.waitForActiveLockData(null, zks, PARENT_DIR, appData);
    Mockito.verify(cb, Mockito.timeout(1000)).becomeActive();
    checkFatalsAndReset();

    LOG.info("========================== Quitting election");
    elector.quitElection(false);
    ActiveStandbyElectorTestUtil.waitForActiveLockData(null, zks, PARENT_DIR, null);

    // Double check that we don't accidentally re-join the election
    // due to receiving the "expired" event.
    Thread.sleep(1000);
    Mockito.verify(cb, Mockito.never()).becomeActive();
    ActiveStandbyElectorTestUtil.waitForActiveLockData(null, zks, PARENT_DIR, null);

    checkFatalsAndReset();
}

From source file:org.apache.hadoop.ha.TestActiveStandbyElectorRealZK.java

License:Apache License

@Test(timeout = 15000)
public void testHandleSessionExpirationOfStandby() throws Exception {
    // Let elector 0 be active
    electors[0].ensureParentZNode();/*from   w ww  .j av a2 s  .c  o m*/
    electors[0].joinElection(appDatas[0]);
    ZooKeeperServer zks = getServer(serverFactory);
    ActiveStandbyElectorTestUtil.waitForActiveLockData(null, zks, PARENT_DIR, appDatas[0]);
    Mockito.verify(cbs[0], Mockito.timeout(1000)).becomeActive();
    checkFatalsAndReset();

    // Let elector 1 be standby
    electors[1].joinElection(appDatas[1]);
    ActiveStandbyElectorTestUtil.waitForElectorState(null, electors[1], State.STANDBY);

    LOG.info("========================== Expiring standby's session");
    zks.closeSession(electors[1].getZKSessionIdForTests());

    // Should enter neutral mode when disconnected
    Mockito.verify(cbs[1], Mockito.timeout(1000)).enterNeutralMode();

    // Should re-join the election and go back to STANDBY
    ActiveStandbyElectorTestUtil.waitForElectorState(null, electors[1], State.STANDBY);
    checkFatalsAndReset();

    LOG.info("========================== Quitting election");
    electors[1].quitElection(false);

    // Double check that we don't accidentally re-join the election
    // by quitting elector 0 and ensuring elector 1 doesn't become active
    electors[0].quitElection(false);

    // due to receiving the "expired" event.
    Thread.sleep(1000);
    Mockito.verify(cbs[1], Mockito.never()).becomeActive();
    ActiveStandbyElectorTestUtil.waitForActiveLockData(null, zks, PARENT_DIR, null);

    checkFatalsAndReset();
}

From source file:org.apache.samza.processor.TestZkStreamProcessorBase.java

License:Apache License

protected void expireSession(ZkClient zkClient) {
    ZkConnection zkConnection = null;//from w w w . j  a v  a2 s  .com
    try {
        Field privateField = ZkClient.class.getDeclaredField("_connection");
        privateField.setAccessible(true);
        zkConnection = (ZkConnection) privateField.get(zkClient);
    } catch (NoSuchFieldException | IllegalAccessException e) {
        Assert.fail(e.toString());
    }

    ZooKeeper zookeeper = zkConnection.getZookeeper();
    long sessionId = zookeeper.getSessionId();

    LOG.info("Closing/expiring session:" + sessionId);
    ZooKeeperServer zkServer = zookeeper().zookeeper();
    zkServer.closeSession(sessionId);
}