Search in sources :

Example 26 with ZooKeeperServer

use of org.apache.zookeeper.server.ZooKeeperServer in project zookeeper by apache.

the class StandaloneTest method testStandaloneReconfigFails.

/**
 * Verify that reconfiguration in standalone mode fails with
 * KeeperException.UnimplementedException.
 */
@Test
public void testStandaloneReconfigFails() throws Exception {
    ClientBase.setupTestEnv();
    final int CLIENT_PORT = PortAssignment.unique();
    final String HOSTPORT = "127.0.0.1:" + CLIENT_PORT;
    File tmpDir = ClientBase.createTmpDir();
    ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
    ServerCnxnFactory f = ServerCnxnFactory.createFactory(CLIENT_PORT, -1);
    f.startup(zks);
    assertTrue(ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT), "waiting for server being up ");
    CountdownWatcher watcher = new CountdownWatcher();
    ZooKeeper zk = new ZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, watcher);
    ZooKeeperAdmin zkAdmin = new ZooKeeperAdmin(HOSTPORT, CONNECTION_TIMEOUT, watcher);
    watcher.waitForConnected(CONNECTION_TIMEOUT);
    List<String> joiners = new ArrayList<String>();
    joiners.add("server.2=localhost:1234:1235;1236");
    // generate some transactions that will get logged
    try {
        zkAdmin.addAuthInfo("digest", "super:test".getBytes());
        zkAdmin.reconfigure(joiners, null, null, -1, new Stat());
        fail("Reconfiguration in standalone should trigger " + "UnimplementedException");
    } catch (KeeperException.UnimplementedException ex) {
    // expected
    }
    zk.close();
    zks.shutdown();
    f.shutdown();
    assertTrue(ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT), "waiting for server being down ");
}
Also used : CountdownWatcher(org.apache.zookeeper.test.ClientBase.CountdownWatcher) ArrayList(java.util.ArrayList) ZooKeeperAdmin(org.apache.zookeeper.admin.ZooKeeperAdmin) ZooKeeper(org.apache.zookeeper.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) KeeperException(org.apache.zookeeper.KeeperException) Test(org.junit.jupiter.api.Test)

Example 27 with ZooKeeperServer

use of org.apache.zookeeper.server.ZooKeeperServer in project zookeeper by apache.

the class ResponseCacheTest method performCacheTest.

public void performCacheTest(ZooKeeper zk, String path, boolean useCache) throws Exception {
    ServerMetrics.getMetrics().resetAll();
    Stat writeStat = new Stat();
    Stat readStat = new Stat();
    byte[] readData = null;
    int reads = 10;
    long expectedHits = 0;
    long expectedMisses = 0;
    ZooKeeperServer zks = serverFactory.getZooKeeperServer();
    zks.setResponseCachingEnabled(useCache);
    LOG.info("caching: {}", useCache);
    if (useCache) {
        assertEquals(zks.getReadResponseCache().getCacheSize(), 32);
        assertEquals(zks.getGetChildrenResponseCache().getCacheSize(), 64);
    }
    byte[] writeData = "test1".getBytes();
    zk.create(path, writeData, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, writeStat);
    for (int i = 0; i < reads; ++i) {
        readData = zk.getData(path, false, readStat);
        assertArrayEquals(writeData, readData);
        assertEquals(writeStat, readStat);
    }
    if (useCache) {
        expectedMisses += 1;
        expectedHits += reads - 1;
    }
    checkCacheStatus(expectedHits, expectedMisses, "response_packet_cache_hits", "response_packet_cache_misses");
    writeData = "test2".getBytes();
    writeStat = zk.setData(path, writeData, -1);
    for (int i = 0; i < 10; ++i) {
        readData = zk.getData(path, false, readStat);
        assertArrayEquals(writeData, readData);
        assertEquals(writeStat, readStat);
    }
    if (useCache) {
        expectedMisses += 1;
        expectedHits += reads - 1;
    }
    checkCacheStatus(expectedHits, expectedMisses, "response_packet_cache_hits", "response_packet_cache_misses");
    // Create a child beneath the tested node. This won't change the data of
    // the tested node, but will change it's pzxid. The next read of the tested
    // node should miss in the cache. The data should still match what was written
    // before, but the stat information should not.
    zk.create(path + "/child", "child".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, null);
    readData = zk.getData(path, false, readStat);
    if (useCache) {
        expectedMisses++;
    }
    assertArrayEquals(writeData, readData);
    assertNotSame(writeStat, readStat);
    checkCacheStatus(expectedHits, expectedMisses, "response_packet_cache_hits", "response_packet_cache_misses");
    ServerMetrics.getMetrics().resetAll();
    expectedHits = 0;
    expectedMisses = 0;
    createPath(path + "/a", zk);
    createPath(path + "/a/b", zk);
    createPath(path + "/a/c", zk);
    createPath(path + "/a/b/d", zk);
    createPath(path + "/a/b/e", zk);
    createPath(path + "/a/b/e/f", zk);
    createPath(path + "/a/b/e/g", zk);
    createPath(path + "/a/b/e/h", zk);
    checkPath(path + "/a", zk, 2);
    checkPath(path + "/a/b", zk, 2);
    checkPath(path + "/a/c", zk, 0);
    checkPath(path + "/a/b/d", zk, 0);
    checkPath(path + "/a/b/e", zk, 3);
    checkPath(path + "/a/b/e/h", zk, 0);
    if (useCache) {
        expectedMisses += 6;
    }
    checkCacheStatus(expectedHits, expectedMisses, "response_packet_get_children_cache_hits", "response_packet_get_children_cache_misses");
    checkPath(path + "/a", zk, 2);
    checkPath(path + "/a/b", zk, 2);
    checkPath(path + "/a/c", zk, 0);
    if (useCache) {
        expectedHits += 3;
    }
    checkCacheStatus(expectedHits, expectedMisses, "response_packet_get_children_cache_hits", "response_packet_get_children_cache_misses");
}
Also used : Stat(org.apache.zookeeper.data.Stat) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer)

Example 28 with ZooKeeperServer

use of org.apache.zookeeper.server.ZooKeeperServer in project zookeeper by apache.

the class ThrottledOpQuorumTest method testThrottledOpLeader.

@Test
public void testThrottledOpLeader() throws IOException, InterruptedException, KeeperException {
    ZooKeeper zk = null;
    try {
        zk = createClient("localhost:" + getLeaderClientPort());
        ZooKeeperServer zs = getLeaderQuorumPeer().getActiveServer();
        ThrottledOpHelper test = new ThrottledOpHelper();
        test.testThrottledOp(zk, zs);
    } finally {
        if (zk != null) {
            zk.close();
        }
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) Test(org.junit.jupiter.api.Test)

Example 29 with ZooKeeperServer

use of org.apache.zookeeper.server.ZooKeeperServer in project zookeeper by apache.

the class ThrottledOpQuorumTest method testThrottledAclLeader.

@Test
public void testThrottledAclLeader() throws Exception {
    ZooKeeper zk = null;
    try {
        zk = createClient("localhost:" + getLeaderClientPort());
        ZooKeeperServer zs = getLeaderQuorumPeer().getActiveServer();
        ThrottledOpHelper test = new ThrottledOpHelper();
        test.testThrottledAcl(zk, zs);
    } finally {
        if (zk != null) {
            zk.close();
        }
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) Test(org.junit.jupiter.api.Test)

Example 30 with ZooKeeperServer

use of org.apache.zookeeper.server.ZooKeeperServer in project zookeeper by apache.

the class LoadFromLogTest method testRestoreWithTransactionErrors.

/**
 * Test we can restore a snapshot that has errors and data ahead of the zxid
 * of the snapshot file.
 */
@Test
public void testRestoreWithTransactionErrors() throws Exception {
    // generate some transactions
    ZooKeeper zk = createZKClient(hostPort);
    try {
        for (int i = 0; i < NUM_MESSAGES; i++) {
            try {
                zk.create("/invaliddir/test-", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
            } catch (NoNodeException e) {
            // Expected
            }
        }
    } finally {
        zk.close();
    }
    // force the zxid to be behind the content
    ZooKeeperServer zks = serverFactory.getZooKeeperServer();
    zks.getZKDatabase().setlastProcessedZxid(zks.getZKDatabase().getDataTreeLastProcessedZxid() - 10);
    LOG.info("Set lastProcessedZxid to {}", zks.getZKDatabase().getDataTreeLastProcessedZxid());
    // Force snapshot and restore
    zks.takeSnapshot();
    zks.shutdown();
    stopServer();
    zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
    startServer();
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) Test(org.junit.jupiter.api.Test)

Aggregations

ZooKeeperServer (org.apache.zookeeper.server.ZooKeeperServer)96 File (java.io.File)39 Test (org.junit.jupiter.api.Test)33 ZooKeeper (org.apache.zookeeper.ZooKeeper)31 InetSocketAddress (java.net.InetSocketAddress)28 IOException (java.io.IOException)27 ServerCnxnFactory (org.apache.zookeeper.server.ServerCnxnFactory)26 NIOServerCnxnFactory (org.apache.zookeeper.server.NIOServerCnxnFactory)25 FileTxnSnapLog (org.apache.zookeeper.server.persistence.FileTxnSnapLog)10 Stat (org.apache.zookeeper.data.Stat)9 ZKDatabase (org.apache.zookeeper.server.ZKDatabase)8 ArrayList (java.util.ArrayList)6 ServerConfig (org.apache.zookeeper.server.ServerConfig)6 InterruptedIOException (java.io.InterruptedIOException)4 BindException (java.net.BindException)4 KeeperException (org.apache.zookeeper.KeeperException)4 Test (org.junit.Test)4 Field (java.lang.reflect.Field)3 ACL (org.apache.zookeeper.data.ACL)3 Proposal (org.apache.zookeeper.server.quorum.Leader.Proposal)3