Java 类org.apache.zookeeper.test.ClientBase 实例源码

项目:fuck_zookeeper    文件:QuorumPeerMainTest.java   
private void waitForAll(ZooKeeper[] zks, States state) throws InterruptedException {
    int iterations = 10;
    boolean someoneNotConnected = true;
       while(someoneNotConnected) {
        if (iterations-- == 0) {
            ClientBase.logAllStackTraces();
        throw new RuntimeException("Waiting too long");
        }

        someoneNotConnected = false;
        for(ZooKeeper zk: zks) {
            if (zk.getState() != state) {
                someoneNotConnected = true;
            }
        }
        Thread.sleep(1000);
       }
}
项目:fuck_zookeeper    文件:QuorumPeerMainTest.java   
/**
 * This is a helper function for launching a set of servers
 *  
 * @param numServers
 * @return
 * @throws IOException
 * @throws InterruptedException
 */
private Servers LaunchServers(int numServers) throws IOException, InterruptedException {
    int SERVER_COUNT = numServers;
    Servers svrs = new Servers();
    final int clientPorts[] = new int[SERVER_COUNT];
    StringBuilder sb = new StringBuilder();
    for(int i = 0; i < SERVER_COUNT; i++) {
        clientPorts[i] = PortAssignment.unique();
        sb.append("server."+i+"=127.0.0.1:"+PortAssignment.unique()+":"+PortAssignment.unique()+"\n");
    }
    String quorumCfgSection = sb.toString();

    MainThread mt[] = new MainThread[SERVER_COUNT];
    ZooKeeper zk[] = new ZooKeeper[SERVER_COUNT];
    for(int i = 0; i < SERVER_COUNT; i++) {
        mt[i] = new MainThread(i, clientPorts[i], quorumCfgSection);
        mt[i].start();
        zk[i] = new ZooKeeper("127.0.0.1:" + clientPorts[i], ClientBase.CONNECTION_TIMEOUT, this);
    }

    waitForAll(zk, States.CONNECTED);

    svrs.mt = mt;
    svrs.zk = zk;
    return svrs;
}
项目:https-github.com-apache-zookeeper    文件:PurgeTxnTest.java   
public void internalTestSnapFilesEqualsToRetain(boolean testWithPrecedingLogFile) throws Exception {
    int nRecentCount = 3;
    AtomicInteger offset = new AtomicInteger(0);
    tmpDir = ClientBase.createTmpDir();
    File version2 = new File(tmpDir.toString(), "version-2");
    Assert.assertTrue("Failed to create version_2 dir:" + version2.toString(),
            version2.mkdir());
    List<File> snaps = new ArrayList<File>();
    List<File> logs = new ArrayList<File>();
    createDataDirFiles(offset, nRecentCount, testWithPrecedingLogFile, version2, snaps, logs);

    FileTxnSnapLog txnLog = new FileTxnSnapLog(tmpDir, tmpDir);
    PurgeTxnLog.purgeOlderSnapshots(txnLog, snaps.get(snaps.size() - 1));
    txnLog.close();
    verifyFilesAfterPurge(snaps, true);
    verifyFilesAfterPurge(logs, true);
}
项目:fuck_zookeeper    文件:CnxManagerTest.java   
@Before
public void setUp() throws Exception {

    this.count = 3;
    this.peers = new HashMap<Long,QuorumServer>(count); 
    peerTmpdir = new File[count];
    peerQuorumPort = new int[count];
    peerClientPort = new int[count];

    for(int i = 0; i < count; i++) {
        peerQuorumPort[i] = PortAssignment.unique();
        peerClientPort[i] = PortAssignment.unique();
        peers.put(Long.valueOf(i),
                  new QuorumServer(i, "0.0.0.0",
                                   peerQuorumPort[i],
                                   PortAssignment.unique(), null));
        peerTmpdir[i] = ClientBase.createTmpDir();
    }
}
项目:ZooKeeper    文件:ZooKeeperServerStartupTest.java   
@After
public void teardown() throws Exception {
    // count down to avoid infinite blocking call due to this latch, if
    // any.
    startupDelayLatch.countDown();

    if (servcnxnf != null) {
        servcnxnf.shutdown();
    }
    if (zks != null) {
        zks.shutdown();
    }
    if (zks.getZKDatabase() != null) {
        zks.getZKDatabase().close();
    }
    if (tmpDir != null) {
        ClientBase.recursiveDelete(tmpDir);
    }
}
项目:fuck_zookeeper    文件:ZxidRolloverTest.java   
private void shutdown(int idx) throws Exception {
    qu.shutdown(idx);

    // leader will shutdown, remaining followers will elect a new leader
    PeerStruct peer = qu.getPeer(idx);
    Assert.assertTrue("Waiting for server down", ClientBase.waitForServerDown(
            "127.0.0.1:" + peer.clientPort, ClientBase.CONNECTION_TIMEOUT));

    // if idx is the the leader then everyone will get disconnected,
    // otherwise if idx is a follower then just that client will get
    // disconnected
    if (idx == idxLeader) {
        checkClientDisconnected(idx);
        try {
            checkClientsDisconnected();
        } catch (AssertionFailedError e) {
            // the clients may or may not have already reconnected
            // to the recovered cluster, force a check, but ignore
        }
    } else {
        checkClientDisconnected(idx);
    }
}
项目:fuck_zookeeper    文件:PurgeTxnTest.java   
/**
 * Tests purge where the data directory contains snap files equals to the
 * number of files to be retained
 */
@Test
public void testSnapFilesEqualsToRetain() throws Exception {
    int nRecentCount = 3;
    AtomicInteger offset = new AtomicInteger(0);
    tmpDir = ClientBase.createTmpDir();
    File version2 = new File(tmpDir.toString(), "version-2");
    Assert.assertTrue("Failed to create version_2 dir:" + version2.toString(),
            version2.mkdir());
    List<File> snaps = new ArrayList<File>();
    List<File> logs = new ArrayList<File>();
    createDataDirFiles(offset, nRecentCount, version2, snaps, logs);

    FileTxnSnapLog txnLog = new FileTxnSnapLog(tmpDir, tmpDir);
    PurgeTxnLog.retainNRecentSnapshots(txnLog, snaps);
    txnLog.close();
    verifyFilesAfterPurge(snaps, true);
    verifyFilesAfterPurge(logs, true);
}
项目:fuck_zookeeper    文件:PurgeTxnTest.java   
/**
 * Tests purge where the data directory contains old snapshots and data
 * logs, newest snapshots and data logs
 */
@Test
public void testSnapFilesLessThanToRetain() throws Exception {
    int nRecentCount = 4;
    int fileToPurgeCount = 2;
    AtomicInteger offset = new AtomicInteger(0);
    tmpDir = ClientBase.createTmpDir();
    File version2 = new File(tmpDir.toString(), "version-2");
    Assert.assertTrue("Failed to create version_2 dir:" + version2.toString(),
            version2.mkdir());
    List<File> snapsToPurge = new ArrayList<File>();
    List<File> logsToPurge = new ArrayList<File>();
    List<File> snaps = new ArrayList<File>();
    List<File> logs = new ArrayList<File>();
    createDataDirFiles(offset, fileToPurgeCount, version2, snapsToPurge,
            logsToPurge);
    createDataDirFiles(offset, nRecentCount, version2, snaps, logs);

    FileTxnSnapLog txnLog = new FileTxnSnapLog(tmpDir, tmpDir);
    PurgeTxnLog.retainNRecentSnapshots(txnLog, snaps);
    txnLog.close();
    verifyFilesAfterPurge(snapsToPurge, false);
    verifyFilesAfterPurge(logsToPurge, false);
    verifyFilesAfterPurge(snaps, true);
    verifyFilesAfterPurge(logs, true);
}
项目:https-github.com-apache-zookeeper    文件:CustomHostProviderTest.java   
@Test
public void testZooKeeperWithCustomHostProvider() throws IOException,
        InterruptedException {
    final int CLIENT_PORT = PortAssignment.unique();
    final HostProvider specialHostProvider = new SpecialHostProvider();
    int expectedCounter = 3;
    counter.set(expectedCounter);

    ZooKeeper zkDefaults = new ZooKeeper("127.0.0.1:" + CLIENT_PORT,
            ClientBase.CONNECTION_TIMEOUT, this, false);

    ZooKeeper zkSpecial = new ZooKeeper("127.0.0.1:" + CLIENT_PORT,
            ClientBase.CONNECTION_TIMEOUT, this, false, specialHostProvider);

    Assert.assertTrue(counter.get() == expectedCounter);
    zkDefaults.updateServerList("127.0.0.1:" + PortAssignment.unique());
    Assert.assertTrue(counter.get() == expectedCounter);

    zkSpecial.updateServerList("127.0.0.1:" + PortAssignment.unique());
    expectedCounter--;
    Assert.assertTrue(counter.get() == expectedCounter);
}
项目:https-github.com-apache-zookeeper    文件:QuorumPeerMainTest.java   
/**
 * This is a helper function for launching a set of servers
 *
 * @param numServers
 * @return
 * @throws IOException
 * @throws InterruptedException
 */
private Servers LaunchServers(int numServers) throws IOException, InterruptedException {
    int SERVER_COUNT = numServers;
    Servers svrs = new Servers();
    final int clientPorts[] = new int[SERVER_COUNT];
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < SERVER_COUNT; i++) {
        clientPorts[i] = PortAssignment.unique();
        sb.append("server."+i+"=127.0.0.1:"+PortAssignment.unique()+":"+PortAssignment.unique()+";"+clientPorts[i]+"\n");
    }
    String quorumCfgSection = sb.toString();

    MainThread mt[] = new MainThread[SERVER_COUNT];
    ZooKeeper zk[] = new ZooKeeper[SERVER_COUNT];
    for (int i = 0; i < SERVER_COUNT; i++) {
        mt[i] = new MainThread(i, clientPorts[i], quorumCfgSection);
        mt[i].start();
        zk[i] = new ZooKeeper("127.0.0.1:" + clientPorts[i], ClientBase.CONNECTION_TIMEOUT, this);
    }

    waitForAll(zk, States.CONNECTED);

    svrs.mt = mt;
    svrs.zk = zk;
    return svrs;
}
项目:ZooKeeper    文件:QuorumCnxManagerTest.java   
/**
 * Test verifies that the LearnerHandler should authenticate the connecting
 * quorumpeer. Here its simulating authentication failure and it should throw
 * SaslException
 */
@Test(timeout = 30000)
public void testLearnerHandlerAuthFailed() throws Exception {
    File testData = ClientBase.createTmpDir();
    Socket leaderSocket = getSocketPair();
    File tmpDir = File.createTempFile("test", ".dir", testData);
    tmpDir.delete();
    tmpDir.mkdir();
    Leader leader = null;
    QuorumPeer peer = createQuorumPeer(tmpDir, true, false, true,
            "QuorumLearner", "QuorumServer",
            QuorumAuth.QUORUM_KERBEROS_SERVICE_PRINCIPAL_DEFAULT_VALUE);
    leader = createLeader(tmpDir, peer);
    peer.leader = leader;

    // authentication failed as qpserver didn't get auth packet from qpclient.
    try {
        new LearnerHandler(leaderSocket,
                new BufferedInputStream(leaderSocket.getInputStream()), leader);
        Assert.fail("Must throw exception as there is an authentication failure");
    } catch (SaslException e){
        Assert.assertEquals("Mistakely added to learners", 0,
                leader.getLearners().size());
    }
    ClientBase.recursiveDelete(testData);
}
项目:https-github.com-apache-zookeeper    文件:CommitProcessorTest.java   
public void setUp(int numCommitThreads, int numClientThreads, int writePercent)
        throws Exception {
    stopped = false;
    System.setProperty(
        CommitProcessor.ZOOKEEPER_COMMIT_PROC_NUM_WORKER_THREADS,
        Integer.toString(numCommitThreads));
    tmpDir = ClientBase.createTmpDir();
    ClientBase.setupTestEnv();
    zks = new TestZooKeeperServer(tmpDir, tmpDir, 4000);
    zks.startup();
    for(int i=0; i<numClientThreads; ++i) {
        TestClientThread client = new TestClientThread(writePercent);
        testClients.add(client);
        client.start();
    }
}
项目:ZooKeeper    文件:ZxidRolloverTest.java   
private void shutdown(int idx) throws Exception {
    qu.shutdown(idx);

    // leader will shutdown, remaining followers will elect a new leader
    PeerStruct peer = qu.getPeer(idx);
    Assert.assertTrue("Waiting for server down", ClientBase.waitForServerDown(
            "127.0.0.1:" + peer.clientPort, ClientBase.CONNECTION_TIMEOUT));

    // if idx is the the leader then everyone will get disconnected,
    // otherwise if idx is a follower then just that client will get
    // disconnected
    if (idx == idxLeader) {
        checkClientDisconnected(idx);
        try {
            checkClientsDisconnected();
        } catch (AssertionFailedError e) {
            // the clients may or may not have already reconnected
            // to the recovered cluster, force a check, but ignore
        }
    } else {
        checkClientDisconnected(idx);
    }
}
项目:ZooKeeper    文件:PurgeTxnTest.java   
public void internalTestSnapFilesEqualsToRetain(boolean testWithPrecedingLogFile) throws Exception {
    int nRecentCount = 3;
    AtomicInteger offset = new AtomicInteger(0);
    tmpDir = ClientBase.createTmpDir();
    File version2 = new File(tmpDir.toString(), "version-2");
    Assert.assertTrue("Failed to create version_2 dir:" + version2.toString(),
            version2.mkdir());
    List<File> snaps = new ArrayList<File>();
    List<File> logs = new ArrayList<File>();
    createDataDirFiles(offset, nRecentCount, testWithPrecedingLogFile, version2, snaps, logs);

    FileTxnSnapLog txnLog = new FileTxnSnapLog(tmpDir, tmpDir);
    PurgeTxnLog.purgeOlderSnapshots(txnLog, snaps.get(snaps.size() - 1));
    txnLog.close();
    verifyFilesAfterPurge(snaps, true);
    verifyFilesAfterPurge(logs, true);
}
项目:ZooKeeper    文件:QuorumPeerMainTest.java   
/**
 * This is a helper function for launching a set of servers
 *  
 * @param numServers
 * @return
 * @throws IOException
 * @throws InterruptedException
 */
private Servers LaunchServers(int numServers) throws IOException, InterruptedException {
    int SERVER_COUNT = numServers;
    Servers svrs = new Servers();
    final int clientPorts[] = new int[SERVER_COUNT];
    StringBuilder sb = new StringBuilder();
    for(int i = 0; i < SERVER_COUNT; i++) {
        clientPorts[i] = PortAssignment.unique();
        sb.append("server."+i+"=127.0.0.1:"+PortAssignment.unique()+":"+PortAssignment.unique()+"\n");
    }
    String quorumCfgSection = sb.toString();

    MainThread mt[] = new MainThread[SERVER_COUNT];
    ZooKeeper zk[] = new ZooKeeper[SERVER_COUNT];
    for(int i = 0; i < SERVER_COUNT; i++) {
        mt[i] = new MainThread(i, clientPorts[i], quorumCfgSection);
        mt[i].start();
        zk[i] = new ZooKeeper("127.0.0.1:" + clientPorts[i], ClientBase.CONNECTION_TIMEOUT, this);
    }

    waitForAll(zk, States.CONNECTED);

    svrs.mt = mt;
    svrs.zk = zk;
    return svrs;
}
项目:ZooKeeper    文件:QuorumAuthUpgradeTest.java   
/**
 * Test to verify that servers are able to form quorum.
 * peer0 -> quorum.auth.enableSasl=true, quorum.auth.learnerRequireSasl=true, quorum.auth.serverRequireSasl=true
 * peer1 -> quorum.auth.enableSasl=true, quorum.auth.learnerRequireSasl=true, quorum.auth.serverRequireSasl=true
 */
@Test(timeout = 30000)
public void testAuthLearnerServer() throws Exception {
    Map<String, String> authConfigs = new HashMap<String, String>();
    authConfigs.put(QuorumAuth.QUORUM_SASL_AUTH_ENABLED, "true");
    authConfigs.put(QuorumAuth.QUORUM_SERVER_SASL_AUTH_REQUIRED, "true");
    authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_AUTH_REQUIRED, "true");

    String connectStr = startQuorum(2, authConfigs, 2, false);
    CountdownWatcher watcher = new CountdownWatcher();
    ZooKeeper zk = new ZooKeeper(connectStr, ClientBase.CONNECTION_TIMEOUT,
            watcher);
    watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT);
    zk.create("/foo", new byte[0], Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT);
    zk.close();
}
项目:https-github.com-apache-zookeeper    文件:JettyAdminServerTest.java   
/**
 * Starts a standalone server and tests that we can query its AdminServer.
 */
@Test
public void testStandalone() throws Exception {
    ClientBase.setupTestEnv();

    final int CLIENT_PORT = PortAssignment.unique();

    ZooKeeperServerMainTest.MainThread main = new ZooKeeperServerMainTest.MainThread(CLIENT_PORT, false, null);
    main.start();

    Assert.assertTrue("waiting for server being up",
            ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT,
            ClientBase.CONNECTION_TIMEOUT));

    queryAdminServer(jettyAdminPort);

    main.shutdown();

    Assert.assertTrue("waiting for server down",
            ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT,
                    ClientBase.CONNECTION_TIMEOUT));
}
项目:ZooKeeper    文件:CnxManagerTest.java   
@Before
public void setUp() throws Exception {

    this.count = 3;
    this.peers = new HashMap<Long,QuorumServer>(count); 
    peerTmpdir = new File[count];
    peerQuorumPort = new int[count];
    peerClientPort = new int[count];

    for(int i = 0; i < count; i++) {
        peerQuorumPort[i] = PortAssignment.unique();
        peerClientPort[i] = PortAssignment.unique();
        peers.put(Long.valueOf(i),
                  new QuorumServer(i, "0.0.0.0",
                                   peerQuorumPort[i],
                                   PortAssignment.unique(), null));
        peerTmpdir[i] = ClientBase.createTmpDir();
    }
}
项目:ZooKeeper    文件:QuorumDigestAuthTest.java   
/**
 * Test to verify that server shouldn't start with invalid credentials
 * if the configuration is set to quorum.auth.serverRequireSasl=true,
 * quorum.auth.learnerRequireSasl=true
 */
@Test(timeout = 30000)
public void testSaslRequiredInvalidCredentials() throws Exception {
    Map<String, String> authConfigs = new HashMap<String, String>();
    authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_LOGIN_CONTEXT, "QuorumLearnerInvalid");
    authConfigs.put(QuorumAuth.QUORUM_SASL_AUTH_ENABLED, "true");
    authConfigs.put(QuorumAuth.QUORUM_SERVER_SASL_AUTH_REQUIRED, "true");
    authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_AUTH_REQUIRED, "true");
    int serverCount = 2;
    final int[] clientPorts = startQuorum(serverCount, 0,
            new StringBuilder(), authConfigs, serverCount, false);
    for (int i = 0; i < serverCount; i++) {
        boolean waitForServerUp = ClientBase.waitForServerUp(
                "127.0.0.1:" + clientPorts[i], QuorumPeerTestBase.TIMEOUT);
        Assert.assertFalse("Shouldn't start server with invalid credentials",
                waitForServerUp);
    }
}
项目:ZooKeeper    文件:QuorumKerberosAuthTest.java   
/**
 * Test to verify that server is able to start with valid credentials
 */
@Test(timeout = 120000)
public void testValidCredentials() throws Exception {
    String serverPrincipal = KerberosTestUtils.getServerPrincipal();
    serverPrincipal = serverPrincipal.substring(0, serverPrincipal.lastIndexOf("@"));
    Map<String, String> authConfigs = new HashMap<String, String>();
    authConfigs.put(QuorumAuth.QUORUM_SASL_AUTH_ENABLED, "true");
    authConfigs.put(QuorumAuth.QUORUM_SERVER_SASL_AUTH_REQUIRED, "true");
    authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_AUTH_REQUIRED, "true");
    authConfigs.put(QuorumAuth.QUORUM_KERBEROS_SERVICE_PRINCIPAL, serverPrincipal);
    String connectStr = startQuorum(3, authConfigs, 3, true);
    CountdownWatcher watcher = new CountdownWatcher();
    ZooKeeper zk = new ZooKeeper(connectStr, ClientBase.CONNECTION_TIMEOUT, watcher);
    watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT);
    for (int i = 0; i < 10; i++) {
        zk.create("/" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }
    zk.close();
}
项目:https-github.com-apache-zookeeper    文件:FileTxnSnapLogTest.java   
/**
 * Test verifies server should fail when data dir or data log dir doesn't
 * exists. Sets "zookeeper.datadir.autocreate" to false.
 */
@Test
public void testWithoutAutoCreateDataLogDir() throws Exception {
    File tmpDir = ClientBase.createEmptyTestDir();
    File dataDir = new File(tmpDir, "data");
    File snapDir = new File(tmpDir, "data_txnlog");
    Assert.assertFalse("data directory already exists", dataDir.exists());
    Assert.assertFalse("snapshot directory already exists", snapDir.exists());

    String priorAutocreateDirValue = System.getProperty(FileTxnSnapLog.ZOOKEEPER_DATADIR_AUTOCREATE);
    System.setProperty(FileTxnSnapLog.ZOOKEEPER_DATADIR_AUTOCREATE, "false");
    try {
        FileTxnSnapLog fileTxnSnapLog = new FileTxnSnapLog(dataDir, snapDir);
    } catch (FileTxnSnapLog.DatadirException e) {
        Assert.assertFalse(dataDir.exists());
        Assert.assertFalse(snapDir.exists());
        return;
    } finally {
        if (priorAutocreateDirValue == null) {
            System.clearProperty(FileTxnSnapLog.ZOOKEEPER_DATADIR_AUTOCREATE);
        } else {
            System.setProperty(FileTxnSnapLog.ZOOKEEPER_DATADIR_AUTOCREATE, priorAutocreateDirValue);
        }
    }
    Assert.fail("Expected exception from FileTxnSnapLog");
}
项目:https-github.com-apache-zookeeper    文件:FileTxnSnapLogTest.java   
@Test
public void testAutoCreateDb() throws IOException {
    File tmpDir = ClientBase.createEmptyTestDir();
    File dataDir = new File(tmpDir, "data");
    File snapDir = new File(tmpDir, "data_txnlog");
    Assert.assertTrue("cannot create data directory", dataDir.mkdir());
    Assert.assertTrue("cannot create snapshot directory", snapDir.mkdir());
    File initFile = new File(dataDir, "initialize");
    Assert.assertFalse("initialize file already exists", initFile.exists());

    String priorAutocreateDbValue = System.getProperty(FileTxnSnapLog.ZOOKEEPER_DB_AUTOCREATE);
    Map<Long, Integer> sessions = new ConcurrentHashMap<>();

    attemptAutoCreateDb(dataDir, snapDir, sessions, priorAutocreateDbValue, "false", -1L);

    attemptAutoCreateDb(dataDir, snapDir, sessions, priorAutocreateDbValue, "true", 0L);

    Assert.assertTrue("cannot create initialize file", initFile.createNewFile());
    attemptAutoCreateDb(dataDir, snapDir, sessions, priorAutocreateDbValue, "false", 0L);
}
项目:https-github.com-apache-zookeeper    文件:FileTxnSnapLogTest.java   
@Test
public void testGetTxnLogSyncElapsedTime() throws IOException {
    File tmpDir = ClientBase.createEmptyTestDir();
    FileTxnSnapLog fileTxnSnapLog = new FileTxnSnapLog(new File(tmpDir, "data"),
            new File(tmpDir, "data_txnlog"));

    TxnHeader hdr = new TxnHeader(1, 1, 1, 1, ZooDefs.OpCode.setData);
    Record txn = new SetDataTxn("/foo", new byte[0], 1);
    Request req = new Request(0, 0, 0, hdr, txn, 0);

    try {
        fileTxnSnapLog.append(req);
        fileTxnSnapLog.commit();
        long syncElapsedTime = fileTxnSnapLog.getTxnLogElapsedSyncTime();
        Assert.assertNotEquals("Did not update syncElapsedTime!", -1L, syncElapsedTime);
    } finally {
        fileTxnSnapLog.close();
    }
}
项目:https-github.com-apache-zookeeper    文件:ZooKeeperServerStartupTest.java   
@After
public void teardown() throws Exception {
    // count down to avoid infinite blocking call due to this latch, if
    // any.
    startupDelayLatch.countDown();

    if (servcnxnf != null) {
        servcnxnf.shutdown();
    }
    if (zks != null) {
        zks.shutdown();
    }
    if (zks.getZKDatabase() != null) {
        zks.getZKDatabase().close();
    }
    ClientBase.recursiveDelete(tmpDir);
}
项目:https-github.com-apache-zookeeper    文件:InvalidSnapCountTest.java   
public MainThread(int clientPort) throws IOException {
    super("Standalone server with clientPort:" + clientPort);
    File tmpDir = ClientBase.createTmpDir();
    confFile = new File(tmpDir, "zoo.cfg");

    FileWriter fwriter = new FileWriter(confFile);
    fwriter.write("tickTime=2000\n");
    fwriter.write("initLimit=10\n");
    fwriter.write("syncLimit=5\n");
    fwriter.write("snapCount=1\n");

    File dataDir = new File(tmpDir, "data");
    if (!dataDir.mkdir()) {
        throw new IOException("unable to mkdir " + dataDir);
    }

    // Convert windows path to UNIX to avoid problems with "\"
    String dir = PathUtils.normalizeFileSystemPath(dataDir.toString());
    fwriter.write("dataDir=" + dir + "\n");

    fwriter.write("clientPort=" + clientPort + "\n");
    fwriter.flush();
    fwriter.close();

    main = new TestMain();
}
项目:https-github.com-apache-zookeeper    文件:ZxidRolloverTest.java   
/**
 * Ensure the client is able to talk to the server.
 * 
 * @param idx the idx of the server the client is talking to
 */
private void checkClientConnected(int idx) throws Exception {
    ZooKeeper zk = getClient(idx);
    if (zk == null) {
        return;
    }
    try {
        Assert.assertNull(zk.exists("/foofoofoo-connected", false));
    } catch (ConnectionLossException e) {
        // second chance...
        // in some cases, leader change in particular, the timing is
        // very tricky to get right in order to assure that the client has
        // disconnected and reconnected. In some cases the client will
        // disconnect, then attempt to reconnect before the server is
        // back, in which case we'll see another connloss on the operation
        // in the try, this catches that case and waits for the server
        // to come back
        PeerStruct peer = qu.getPeer(idx);
        Assert.assertTrue("Waiting for server down", ClientBase.waitForServerUp(
                "127.0.0.1:" + peer.clientPort, ClientBase.CONNECTION_TIMEOUT));

        Assert.assertNull(zk.exists("/foofoofoo-connected", false));
    }
}
项目:ZooKeeper    文件:QuorumDigestAuthTest.java   
/**
 * Test to verify that server is able to reform quorum if the Leader goes
 * down.
 */
@Test(timeout = 30000)
public void testRelectionWithValidCredentials() throws Exception {
    Map<String, String> authConfigs = new HashMap<String, String>();
    authConfigs.put(QuorumAuth.QUORUM_SASL_AUTH_ENABLED, "true");
    authConfigs.put(QuorumAuth.QUORUM_SERVER_SASL_AUTH_REQUIRED, "true");
    authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_AUTH_REQUIRED, "true");

    String connectStr = startQuorum(3, authConfigs, 3, false);
    CountdownWatcher watcher = new CountdownWatcher();
    zk = new ZooKeeper(connectStr, ClientBase.CONNECTION_TIMEOUT, watcher);
    watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT);
    zk.create("/myTestRoot", new byte[0], Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT_SEQUENTIAL);
    watcher.reset();

    // Shutdown Leader to trigger re-election
    QuorumPeer leaderQP = getLeaderQuorumPeer(mt);
    LOG.info("Shutdown Leader sid:{} to trigger quorum leader-election",
            leaderQP.getId());
    shutdownQP(leaderQP);

    // Wait for quorum formation
    QuorumPeer newLeaderQP = waitForLeader();
    assertNotNull("New leader must have been elected by now", newLeaderQP);
    watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT);
    zk.create("/myTestRoot", new byte[0], Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT_SEQUENTIAL);
}
项目:ZooKeeper    文件:PurgeTxnTest.java   
/**
 * PurgeTxnLog is called with dataLogDir -n count This test case verify
 * these values are parsed properly and functionality works fine
 */
@Test
public void testPurgeTxnLogWithoutDataDir()
        throws Exception {
    tmpDir = ClientBase.createTmpDir();
    File dataDir = new File(tmpDir, "dataDir");
    File dataLogDir = new File(tmpDir, "dataLogDir");

    File dataDirVersion2 = new File(dataDir, "version-2");
    dataDirVersion2.mkdirs();
    File dataLogDirVersion2 = new File(dataLogDir, "version-2");
    dataLogDirVersion2.mkdirs();

    // create dummy log and transaction file
    int totalFiles = 20;

    // create transaction and snapshot files in data directory
    for (int i = 0; i < totalFiles; i++) {
        // simulate log file
        File logFile = new File(dataLogDirVersion2, "log."
                + Long.toHexString(i));
        logFile.createNewFile();
        // simulate snapshot file
        File snapFile = new File(dataLogDirVersion2, "snapshot."
                + Long.toHexString(i));
        snapFile.createNewFile();
    }

    int numberOfSnapFilesToKeep = 10;
    // scenario where only three parameter are passed
    String[] args = new String[] { dataLogDir.getAbsolutePath(), "-n",
            Integer.toString(numberOfSnapFilesToKeep) };
    PurgeTxnLog.main(args);
    assertEquals(numberOfSnapFilesToKeep * 2, // Since for each snapshot we have a log file with same zxid, expect same # logs as snaps to be kept
            dataLogDirVersion2.listFiles().length);
    ClientBase.recursiveDelete(tmpDir);

}
项目:fuck_zookeeper    文件:CnxManagerTest.java   
@Test
public void testCnxManagerTimeout() throws Exception {
    Random rand = new Random();
    byte b = (byte) rand.nextInt();
    int finalOctet = b & 0xFF;
    int deadPort = PortAssignment.unique();
    String deadAddress = new String("192.0.2." + finalOctet);

    LOG.info("This is the dead address I'm trying: " + deadAddress);

    peers.put(Long.valueOf(2),
              new QuorumServer(2, deadAddress, deadPort, PortAssignment.unique(), null));
    peerTmpdir[2] = ClientBase.createTmpDir();

    QuorumPeer peer = new QuorumPeer(peers, peerTmpdir[1], peerTmpdir[1], peerClientPort[1], 3, 1, 1000, 2, 2);
    QuorumCnxManager cnxManager = new QuorumCnxManager(peer);
    QuorumCnxManager.Listener listener = cnxManager.listener;
    if(listener != null){
        listener.start();
    } else {
        LOG.error("Null listener when initializing cnx manager");
    }

    long begin = System.currentTimeMillis();
    cnxManager.toSend(new Long(2), createMsg(ServerState.LOOKING.ordinal(), 1, -1, 1));
    long end = System.currentTimeMillis();

    if((end - begin) > 6000) Assert.fail("Waited more than necessary");

}
项目:fuck_zookeeper    文件:FLELostMessageTest.java   
@Test
public void testLostMessage() throws Exception {
    FastLeaderElection le[] = new FastLeaderElection[count];

    LOG.info("TestLE: " + getTestName()+ ", " + count);
    for(int i = 0; i < count; i++) {
        int clientport = PortAssignment.unique();
        peers.put(Long.valueOf(i),
                  new QuorumServer(i, "0.0.0.0", clientport,
                                   PortAssignment.unique(), null));
        tmpdir[i] = ClientBase.createTmpDir();
        port[i] = clientport;
    }

    /*
     * Start server 0
     */

    QuorumPeer peer = new QuorumPeer(peers, tmpdir[1], tmpdir[1], port[1], 3, 1, 1000, 2, 2);
    peer.startLeaderElection();
    FLETestUtils.LEThread thread = new FLETestUtils.LEThread(peer, 1);
    thread.start();

    /*
     * Start mock server 1
     */
    mockServer();
    thread.join(5000);
    if (thread.isAlive()) {
        Assert.fail("Threads didn't join");
    }
}
项目:ZooKeeper    文件:PurgeTxnTest.java   
/**
 * test the purge
 * @throws Exception an exception might be thrown here
 */
@Test
public void testPurge() throws Exception {
    tmpDir = ClientBase.createTmpDir();
    ClientBase.setupTestEnv();
    ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
    SyncRequestProcessor.setSnapCount(100);
    final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]);
    ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1);
    f.startup(zks);
    Assert.assertTrue("waiting for server being up ",
            ClientBase.waitForServerUp(HOSTPORT,CONNECTION_TIMEOUT));
    ZooKeeper zk = new ZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, this);
    try {
        for (int i = 0; i< 2000; i++) {
            zk.create("/invalidsnap-" + i, new byte[0], Ids.OPEN_ACL_UNSAFE,
                    CreateMode.PERSISTENT);
        }
    } finally {
        zk.close();
    }
    f.shutdown();
    zks.getTxnLogFactory().close();
    Assert.assertTrue("waiting for server to shutdown",
            ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT));
    // now corrupt the snapshot
    PurgeTxnLog.purge(tmpDir, tmpDir, 3);
    FileTxnSnapLog snaplog = new FileTxnSnapLog(tmpDir, tmpDir);
    List<File> listLogs = snaplog.findNRecentSnapshots(4);
    int numSnaps = 0;
    for (File ff: listLogs) {
        if (ff.getName().startsWith("snapshot")) {
            numSnaps++;
        }
    }
    Assert.assertTrue("exactly 3 snapshots ", (numSnaps == 3));
    snaplog.close();
    zks.shutdown();
}
项目:ZooKeeper    文件:ZxidRolloverTest.java   
private void start(int idx) throws Exception {
    qu.start(idx);
    for (String hp : qu.getConnString().split(",")) {
        Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp(hp,
                ClientTest.CONNECTION_TIMEOUT));
    }

    checkLeader();
    // all clients should be connected
    checkClientsConnected();
}
项目:fuck_zookeeper    文件:ZooKeeperServerMainTest.java   
@Test
public void testJMXRegistrationWithNIO() throws Exception {
    ClientBase.setupTestEnv();
    File tmpDir_1 = ClientBase.createTmpDir();
    ServerCnxnFactory server_1 = startServer(tmpDir_1);
    File tmpDir_2 = ClientBase.createTmpDir();
    ServerCnxnFactory server_2 = startServer(tmpDir_2);

    server_1.shutdown();
    server_2.shutdown();

    deleteFile(tmpDir_1);
    deleteFile(tmpDir_2);
}
项目:ZooKeeper    文件:SessionTrackerTest.java   
private ZooKeeperServer setupSessionTracker() throws IOException {
    File tmpDir = ClientBase.createTmpDir();
    ClientBase.setupTestEnv();
    ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
    zks.setupRequestProcessors();
    firstProcessor = new FirstProcessor(zks, null);
    zks.firstProcessor = firstProcessor;

    // setup session tracker
    zks.createSessionTracker();
    zks.startSessionTracker();
    return zks;
}
项目:https-github.com-apache-zookeeper    文件:PurgeTxnTest.java   
/**
 * PurgeTxnLog is called with dataLogDir -n count This test case verify
 * these values are parsed properly and functionality works fine
 */
@Test
public void testPurgeTxnLogWithoutDataDir()
        throws Exception {
    tmpDir = ClientBase.createTmpDir();
    File dataDir = new File(tmpDir, "dataDir");
    File dataLogDir = new File(tmpDir, "dataLogDir");

    File dataDirVersion2 = new File(dataDir, "version-2");
    dataDirVersion2.mkdirs();
    File dataLogDirVersion2 = new File(dataLogDir, "version-2");
    dataLogDirVersion2.mkdirs();

    // create dummy log and transaction file
    int totalFiles = 20;

    // create transaction and snapshot files in data directory
    for (int i = 0; i < totalFiles; i++) {
        // simulate log file
        File logFile = new File(dataLogDirVersion2, "log."
                + Long.toHexString(i));
        logFile.createNewFile();
        // simulate snapshot file
        File snapFile = new File(dataLogDirVersion2, "snapshot."
                + Long.toHexString(i));
        snapFile.createNewFile();
    }

    int numberOfSnapFilesToKeep = 10;
    // scenario where only three parameter are passed
    String[] args = new String[] { dataLogDir.getAbsolutePath(), "-n",
            Integer.toString(numberOfSnapFilesToKeep) };
    PurgeTxnLog.main(args);
    assertEquals(numberOfSnapFilesToKeep * 2, // Since for each snapshot we have a log file with same zxid, expect same # logs as snaps to be kept
            dataLogDirVersion2.listFiles().length);
    ClientBase.recursiveDelete(tmpDir);

}
项目:https-github.com-apache-zookeeper    文件:SessionTrackerTest.java   
private ZooKeeperServer setupSessionTracker() throws IOException {
    File tmpDir = ClientBase.createTmpDir();
    ClientBase.setupTestEnv();
    ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
    zks.setupRequestProcessors();
    firstProcessor = new FirstProcessor(zks, null);
    zks.firstProcessor = firstProcessor;

    // setup session tracker
    zks.createSessionTracker();
    zks.startSessionTracker();
    return zks;
}
项目:fuck_zookeeper    文件:ZooKeeperServerMainTest.java   
private ServerCnxnFactory startServer(File tmpDir) throws IOException,
        InterruptedException {
    final int CLIENT_PORT = PortAssignment.unique();
    ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
    ServerCnxnFactory f = ServerCnxnFactory.createFactory(CLIENT_PORT, -1);
    f.startup(zks);
    Assert.assertNotNull("JMX initialization failed!", zks.jmxServerBean);
    Assert.assertTrue("waiting for server being up",
            ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT,
                    CONNECTION_TIMEOUT));
    return f;
}
项目:ZooKeeper    文件:ZooKeeperServerMainTest.java   
private ServerCnxnFactory startServer(File tmpDir) throws IOException,
        InterruptedException {
    final int CLIENT_PORT = PortAssignment.unique();
    ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
    ServerCnxnFactory f = ServerCnxnFactory.createFactory(CLIENT_PORT, -1);
    f.startup(zks);
    Assert.assertNotNull("JMX initialization failed!", zks.jmxServerBean);
    Assert.assertTrue("waiting for server being up",
            ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT,
                    CONNECTION_TIMEOUT));
    return f;
}
项目:ZooKeeper    文件:QuorumCnxManagerTest.java   
/**
 * Test verifies that the No Auth enabled Learner is connecting to a No Auth
 * Leader server. Learner should be able to establish a connection with
 * Leader as auth is not required.
 */
@Test(timeout = 30000)
public void testNoAuthLearnerConnectsToServerWithAuthNotRequired()
        throws Exception {
    File testDataLearner = ClientBase.createTmpDir();
    File tmpDir = File.createTempFile("test", ".dir", testDataLearner);
    tmpDir.delete();
    FileTxnSnapLog ftsl = new FileTxnSnapLog(tmpDir, tmpDir);
    QuorumPeer learnerPeer = createQuorumPeer(tmpDir, true, false, false,
            "QuorumLearner", "QuorumServer", "");
    SimpleLearner sl = new SimpleLearner(ftsl, learnerPeer);

    File testDataLeader = ClientBase.createTmpDir();
    tmpDir = File.createTempFile("test", ".dir", testDataLeader);
    tmpDir.delete();
    tmpDir.mkdir();
    Leader leader = null;
    QuorumPeer peer = createQuorumPeer(tmpDir, true, false, false, "QuorumLearner",
            "QuorumServer",
            QuorumAuth.QUORUM_KERBEROS_SERVICE_PRINCIPAL_DEFAULT_VALUE);
    CountDownLatch learnerLatch = new CountDownLatch(1);
    leader = createSimpleLeader(tmpDir, peer, learnerLatch);
    peer.leader = leader;

    startLearnerCnxAcceptorThread(leader);
    LOG.info("Start establishing a connection with the Leader");
    String hostname = getLeaderHostname(peer);
    sl.connectToLeader(peer.getQuorumAddress(), hostname);

    Assert.assertTrue("Leader should accept no auth learner connection",
            learnerLatch.await(leader.self.tickTime * leader.self.initLimit + 1000,
                    TimeUnit.MILLISECONDS));
    ClientBase.recursiveDelete(testDataLearner);
    ClientBase.recursiveDelete(testDataLeader);
}
项目:fuck_zookeeper    文件:ZxidRolloverTest.java   
private void start(int idx) throws Exception {
    qu.start(idx);
    for (String hp : qu.getConnString().split(",")) {
        Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp(hp,
                ClientTest.CONNECTION_TIMEOUT));
    }

    checkLeader();
    // all clients should be connected
    checkClientsConnected();
}