Search in sources :

Example 36 with ZooKeeperServer

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

the class ACLTest method testDisconnectedAddAuth.

@Test
public void testDisconnectedAddAuth() throws Exception {
    File tmpDir = ClientBase.createTmpDir();
    ClientBase.setupTestEnv();
    ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
    SyncRequestProcessor.setSnapCount(1000);
    final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]);
    ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1);
    f.startup(zks);
    try {
        LOG.info("starting up the zookeeper server .. waiting");
        assertTrue(ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT), "waiting for server being up");
        ZooKeeper zk = ClientBase.createZKClient(HOSTPORT);
        try {
            zk.addAuthInfo("digest", "pat:test".getBytes());
            zk.setACL("/", Ids.CREATOR_ALL_ACL, -1);
        } finally {
            zk.close();
        }
    } finally {
        f.shutdown();
        zks.shutdown();
        assertTrue(ClientBase.waitForServerDown(HOSTPORT, ClientBase.CONNECTION_TIMEOUT), "waiting for server down");
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) Test(org.junit.jupiter.api.Test)

Example 37 with ZooKeeperServer

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

the class ACLTest method testNullValueACL.

@Test
public void testNullValueACL() throws Exception {
    File tmpDir = ClientBase.createTmpDir();
    ClientBase.setupTestEnv();
    ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
    final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]);
    ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1);
    f.startup(zks);
    ZooKeeper zk = ClientBase.createZKClient(HOSTPORT);
    try {
        List<ACL> acls = new ArrayList<ACL>();
        acls.add(null);
        // case 1 : null value in ACL list with create
        try {
            zk.create("/foo", "foo".getBytes(), acls, CreateMode.PERSISTENT);
            fail("Expected InvalidACLException for null value in ACL List");
        } catch (InvalidACLException e) {
        // Expected. Do nothing
        }
        // case 2 : null value in ACL list with other create API
        try {
            zk.create("/foo", "foo".getBytes(), acls, CreateMode.PERSISTENT, null);
            fail("Expected InvalidACLException for null value in ACL List");
        } catch (InvalidACLException e) {
        // Expected. Do nothing
        }
        // case 3 : null value in ACL list with setACL
        try {
            zk.setACL("/foo", acls, -1);
            fail("Expected InvalidACLException for null value in ACL List");
        } catch (InvalidACLException e) {
        // Expected. Do nothing
        }
    } finally {
        zk.close();
        f.shutdown();
        zks.shutdown();
        assertTrue(ClientBase.waitForServerDown(HOSTPORT, ClientBase.CONNECTION_TIMEOUT), "waiting for server down");
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) ArrayList(java.util.ArrayList) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) ACL(org.apache.zookeeper.data.ACL) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) InvalidACLException(org.apache.zookeeper.KeeperException.InvalidACLException) Test(org.junit.jupiter.api.Test)

Example 38 with ZooKeeperServer

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

the class ACLTest method testAcls.

/**
 * Verify that acl optimization of storing just
 * a few acls and there references in the data
 * node is actually working.
 */
@Test
public void testAcls() throws Exception {
    File tmpDir = ClientBase.createTmpDir();
    ClientBase.setupTestEnv();
    ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
    SyncRequestProcessor.setSnapCount(1000);
    final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]);
    ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1);
    f.startup(zks);
    ZooKeeper zk;
    String path;
    try {
        LOG.info("starting up the zookeeper server .. waiting");
        assertTrue(ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT), "waiting for server being up");
        zk = ClientBase.createZKClient(HOSTPORT);
        LOG.info("starting creating acls");
        for (int i = 0; i < 100; i++) {
            path = "/" + i;
            zk.create(path, path.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }
        int size = zks.getZKDatabase().getAclSize();
        assertTrue((2 == zks.getZKDatabase().getAclSize()), "size of the acl map ");
        for (int j = 100; j < 200; j++) {
            path = "/" + j;
            ACL acl = new ACL();
            acl.setPerms(0);
            Id id = new Id();
            id.setId("1.1.1." + j);
            id.setScheme("ip");
            acl.setId(id);
            List<ACL> list = new ArrayList<ACL>();
            list.add(acl);
            zk.create(path, path.getBytes(), list, CreateMode.PERSISTENT);
        }
        assertTrue((102 == zks.getZKDatabase().getAclSize()), "size of the acl map ");
    } finally {
        // now shutdown the server and restart it
        f.shutdown();
        zks.shutdown();
        assertTrue(ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT), "waiting for server down");
    }
    zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
    f = ServerCnxnFactory.createFactory(PORT, -1);
    f.startup(zks);
    try {
        assertTrue(ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT), "waiting for server up");
        zk = ClientBase.createZKClient(HOSTPORT);
        assertTrue((102 == zks.getZKDatabase().getAclSize()), "acl map ");
        for (int j = 200; j < 205; j++) {
            path = "/" + j;
            ACL acl = new ACL();
            acl.setPerms(0);
            Id id = new Id();
            id.setId("1.1.1." + j);
            id.setScheme("ip");
            acl.setId(id);
            ArrayList<ACL> list = new ArrayList<ACL>();
            list.add(acl);
            zk.create(path, path.getBytes(), list, CreateMode.PERSISTENT);
        }
        assertTrue((107 == zks.getZKDatabase().getAclSize()), "acl map ");
        zk.close();
    } finally {
        f.shutdown();
        zks.shutdown();
        assertTrue(ClientBase.waitForServerDown(HOSTPORT, ClientBase.CONNECTION_TIMEOUT), "waiting for server down");
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) ArrayList(java.util.ArrayList) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) ACL(org.apache.zookeeper.data.ACL) Id(org.apache.zookeeper.data.Id) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) Test(org.junit.jupiter.api.Test)

Example 39 with ZooKeeperServer

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

the class FileTxnLogTest method testLogSizeLimit.

/**
 * Test that the server can correctly load the data when there are multiple
 * txnlogs per snapshot
 */
@Test
public void testLogSizeLimit() throws Exception {
    File tmpDir = ClientBase.createTmpDir();
    ClientBase.setupTestEnv();
    // Need to override preallocate set by setupTestEnv()
    // We don't need to unset these values since each unit test run in
    // a separate JVM instance
    FileTxnLog.setPreallocSize(PREALLOCATE);
    FileTxnLog.setTxnLogSizeLimit(LOG_SIZE_LIMIT);
    ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
    final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]);
    ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1);
    f.startup(zks);
    assertTrue(ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT), "waiting for server being up ");
    ZooKeeper zk = new ZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, DummyWatcher.INSTANCE);
    // Generate transactions
    HashSet<Long> zxids = new HashSet<>();
    byte[] bytes = new byte[NODE_SIZE];
    Random random = new Random();
    random.nextBytes(bytes);
    // We will create enough txn to generate 3 logs
    long txnCount = LOG_SIZE_LIMIT / NODE_SIZE / 2 * 5;
    LOG.info("Creating {} txns", txnCount);
    try {
        for (long i = 0; i < txnCount; i++) {
            Stat stat = new Stat();
            zk.create("/node-" + i, bytes, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            zk.getData("/node-" + i, null, stat);
            zxids.add(stat.getCzxid());
        }
    } finally {
        zk.close();
    }
    // shutdown
    f.shutdown();
    assertTrue(ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT), "waiting for server to shutdown");
    File logDir = new File(tmpDir, FileTxnSnapLog.version + FileTxnSnapLog.VERSION);
    File[] txnLogs = FileTxnLog.getLogFiles(logDir.listFiles(), 0);
    assertEquals(3, txnLogs.length, "Unexpected number of logs");
    // Log size should not exceed limit by more than one node size;
    long threshold = LOG_SIZE_LIMIT + NODE_SIZE;
    LOG.info(txnLogs[0].getAbsolutePath());
    assertTrue(threshold > txnLogs[0].length(), "Exceed log size limit: " + txnLogs[0].length());
    LOG.info(txnLogs[1].getAbsolutePath());
    assertTrue(threshold > txnLogs[1].length(), "Exceed log size limit " + txnLogs[1].length());
    // Start database only
    zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
    zks.startdata();
    ZKDatabase db = zks.getZKDatabase();
    for (long i = 0; i < txnCount; i++) {
        Stat stat = new Stat();
        byte[] data = db.getData("/node-" + i, stat, null);
        assertArrayEquals(bytes, data, "Missmatch data");
        assertTrue(zxids.contains(stat.getMzxid()), "Unknown zxid ");
    }
}
Also used : ZKDatabase(org.apache.zookeeper.server.ZKDatabase) ZooKeeper(org.apache.zookeeper.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) Random(java.util.Random) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 40 with ZooKeeperServer

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

the class CommandsTest method testCommand.

/**
 * Checks that running a given Command returns the expected Map. Asserts
 * that all specified keys are present with values of the specified types
 * and that there are no extra entries.
 *
 * @param cmdName
 *            - the primary name of the command
 * @param kwargs
 *            - keyword arguments to the command
 * @param fields
 *            - the fields that are expected in the returned Map
 * @throws IOException
 * @throws InterruptedException
 */
public void testCommand(String cmdName, Map<String, String> kwargs, Field... fields) throws IOException, InterruptedException {
    ZooKeeperServer zks = serverFactory.getZooKeeperServer();
    Map<String, Object> result = Commands.runCommand(cmdName, zks, kwargs).toMap();
    assertTrue(result.containsKey("command"));
    // This is only true because we're setting cmdName to the primary name
    assertEquals(cmdName, result.remove("command"));
    assertTrue(result.containsKey("error"));
    assertNull(result.remove("error"), "error: " + result.get("error"));
    for (Field field : fields) {
        String k = field.key;
        assertTrue(result.containsKey(k), "Result from command " + cmdName + " missing field \"" + k + "\"" + "\n" + result);
        Class<?> t = field.type;
        Object v = result.remove(k);
        assertTrue(t.isAssignableFrom(v.getClass()), "\"" + k + "\" field from command " + cmdName + " should be of type " + t + ", is actually of type " + v.getClass());
    }
    assertTrue(result.isEmpty(), "Result from command " + cmdName + " contains extra fields: " + result);
}
Also used : ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer)

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