Search in sources :

Example 66 with ZooKeeperServer

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

the class FuzzySnapshotRelatedTest method testGlobalSessionConsistency.

@Test
public void testGlobalSessionConsistency() throws Exception {
    LOG.info("Hook to catch the commitSession event on followerA");
    CustomizedQPMain followerAMain = (CustomizedQPMain) mt[followerA].main;
    final ZooKeeperServer zkServer = followerAMain.quorumPeer.getActiveServer();
    // only take snapshot for the next global session we're going to create
    final AtomicBoolean shouldTakeSnapshot = new AtomicBoolean(true);
    followerAMain.setCommitSessionListener(new CommitSessionListener() {

        @Override
        public void process(long sessionId) {
            LOG.info("Take snapshot");
            if (shouldTakeSnapshot.getAndSet(false)) {
                zkServer.takeSnapshot(true);
            }
        }
    });
    LOG.info("Create a global session");
    ZooKeeper globalClient = new ZooKeeper("127.0.0.1:" + clientPorts[followerA], ClientBase.CONNECTION_TIMEOUT, this);
    QuorumPeerMainTest.waitForOne(globalClient, States.CONNECTED);
    LOG.info("Restart followerA to load the data from disk");
    mt[followerA].shutdown();
    QuorumPeerMainTest.waitForOne(zk[followerA], States.CONNECTING);
    mt[followerA].start();
    QuorumPeerMainTest.waitForOne(zk[followerA], States.CONNECTED);
    LOG.info("Make sure the global sessions are consistent with leader");
    Map<Long, Integer> globalSessionsOnLeader = mt[leaderId].main.quorumPeer.getZkDb().getSessionWithTimeOuts();
    Map<Long, Integer> globalSessionsOnFollowerA = mt[followerA].main.quorumPeer.getZkDb().getSessionWithTimeOuts();
    LOG.info("sessions are {}, {}", globalSessionsOnLeader.keySet(), globalSessionsOnFollowerA.keySet());
    assertTrue(globalSessionsOnFollowerA.keySet().containsAll(globalSessionsOnLeader.keySet()));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ZooKeeper(org.apache.zookeeper.ZooKeeper) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) Test(org.junit.jupiter.api.Test)

Example 67 with ZooKeeperServer

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

the class FuzzySnapshotRelatedTest method testPZxidUpdatedWhenLoadingSnapshot.

/**
 * It's possible during taking fuzzy snapshot, the parent is serialized
 * before the child get deleted in the fuzzy range.
 *
 * In which case, we need to make sure the pzxid get correctly updated
 * when replaying the txns.
 */
@Test
public void testPZxidUpdatedWhenLoadingSnapshot() throws Exception {
    final String parent = "/testPZxidUpdatedDuringTakingSnapshot";
    final String child = parent + "/child";
    createEmptyNode(zk[followerA], parent, CreateMode.PERSISTENT);
    createEmptyNode(zk[followerA], child, CreateMode.EPHEMERAL);
    // create another child to test closeSession
    createEmptyNode(zk[leaderId], child + "1", CreateMode.EPHEMERAL);
    LOG.info("Set up ZKDatabase to catch the node serializing in DataTree");
    addSerializeListener(followerA, parent, child);
    LOG.info("Take snapshot on follower A");
    ZooKeeperServer zkServer = mt[followerA].main.quorumPeer.getActiveServer();
    zkServer.takeSnapshot(true);
    LOG.info("Restarting follower A to load snapshot");
    mt[followerA].shutdown();
    QuorumPeerMainTest.waitForOne(zk[followerA], States.CLOSED);
    mt[followerA].start();
    // zk[followerA] will be closed in addSerializeListener, re-create it
    zk[followerA] = new ZooKeeper("127.0.0.1:" + clientPorts[followerA], ClientBase.CONNECTION_TIMEOUT, this);
    QuorumPeerMainTest.waitForOne(zk[followerA], States.CONNECTED);
    LOG.info("Check and make sure the pzxid of the parent is the same on leader and follower A");
    compareStat(parent, leaderId, followerA);
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) Test(org.junit.jupiter.api.Test)

Example 68 with ZooKeeperServer

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

the class ClientPortBindTest method testBindByAddress.

/**
 * Verify that the server binds to the specified address
 */
@Test
public void testBindByAddress() throws Exception {
    String bindAddress = null;
    Enumeration<NetworkInterface> intfs = NetworkInterface.getNetworkInterfaces();
    // if we have a loopback and it has an address use it
    while (intfs.hasMoreElements()) {
        NetworkInterface i = intfs.nextElement();
        try {
            if (i.isLoopback()) {
                Enumeration<InetAddress> addrs = i.getInetAddresses();
                while (addrs.hasMoreElements()) {
                    InetAddress a = addrs.nextElement();
                    if (a.isLoopbackAddress()) {
                        bindAddress = a.getHostAddress();
                        if (a instanceof Inet6Address) {
                            bindAddress = "[" + bindAddress + "]";
                        }
                        break;
                    }
                }
            }
        } catch (SocketException se) {
            LOG.warn("Couldn't find loopback interface", se);
        }
    }
    if (bindAddress == null) {
        LOG.warn("Unable to determine loop back address, skipping test");
        return;
    }
    final int PORT = PortAssignment.unique();
    LOG.info("Using {} as the bind address", bindAddress);
    final String HOSTPORT = bindAddress + ":" + PORT;
    LOG.info("Using {} as the host/port", HOSTPORT);
    File tmpDir = ClientBase.createTmpDir();
    ClientBase.setupTestEnv();
    ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
    ServerCnxnFactory f = ServerCnxnFactory.createFactory(new InetSocketAddress(bindAddress, PORT), -1);
    f.startup(zks);
    LOG.info("starting up the the server, waiting");
    assertTrue(ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT), "waiting for server up");
    ZooKeeper zk = ClientBase.createZKClient(HOSTPORT);
    try {
        zk.close();
    } finally {
        f.shutdown();
        zks.shutdown();
        assertTrue(ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT), "waiting for server down");
    }
}
Also used : SocketException(java.net.SocketException) InetSocketAddress(java.net.InetSocketAddress) NetworkInterface(java.net.NetworkInterface) Inet6Address(java.net.Inet6Address) ZooKeeper(org.apache.zookeeper.ZooKeeper) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) InetAddress(java.net.InetAddress) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) Test(org.junit.jupiter.api.Test)

Example 69 with ZooKeeperServer

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

the class GetProposalFromTxnTest method testGetProposalFromTxn.

/**
 * Test loading proposal from txnlog
 *
 * @throws Exception
 *             an exception might be thrown here
 */
@Test
public void testGetProposalFromTxn() throws Exception {
    File 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);
    assertTrue(ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT), "waiting for server being up ");
    ZooKeeper zk = ClientBase.createZKClient(HOSTPORT);
    // Generate transaction so we will have some txnlog
    Long[] zxids = new Long[MSG_COUNT];
    try {
        String data = "data";
        byte[] bytes = data.getBytes();
        for (int i = 0; i < MSG_COUNT; i++) {
            Stat stat = new Stat();
            zk.create("/invalidsnap-" + i, bytes, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            zk.getData("/invalidsnap-" + i, null, stat);
            zxids[i] = stat.getCzxid();
        }
    } finally {
        zk.close();
    }
    // shutdown and start zookeeper again
    f.shutdown();
    zks.shutdown();
    assertTrue(ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT), "waiting for server to shutdown");
    zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
    zks.startdata();
    ZKDatabase db = zks.getZKDatabase();
    // Set sizeLimit to be very high number, so we can pull all transactions
    // from txnlog
    Iterator<Proposal> itr = db.getProposalsFromTxnLog(zxids[0], 10000000);
    int createCount = 0;
    ArrayList<Long> retrievedZxids = new ArrayList<Long>(MSG_COUNT);
    // Get zxid of create requests
    while (itr.hasNext()) {
        Proposal proposal = itr.next();
        TxnLogEntry logEntry = SerializeUtils.deserializeTxn(proposal.packet.getData());
        TxnHeader hdr = logEntry.getHeader();
        Record rec = logEntry.getTxn();
        if (hdr.getType() == OpCode.create) {
            retrievedZxids.add(hdr.getZxid());
            createCount++;
        }
    }
    // All zxid should match what we created
    assertTrue(Arrays.equals(zxids, retrievedZxids.toArray(new Long[0])), "Zxids missmatches");
    // There should be 2000 create requests
    assertTrue((createCount == MSG_COUNT), "create proposal count == " + MSG_COUNT);
    // We are requesting half the number of transaction from the snapshot
    // this should exceed threshold (ZKDatabase.snapshotSizeFactor)
    db.setSnapshotSizeFactor(0.33);
    long sizeLimit = db.calculateTxnLogSizeLimit();
    itr = db.getProposalsFromTxnLog(zxids[MSG_COUNT / 2], sizeLimit);
    assertFalse((itr.hasNext()), "Expect empty proposal");
    f.shutdown();
    zks.shutdown();
}
Also used : ArrayList(java.util.ArrayList) ZKDatabase(org.apache.zookeeper.server.ZKDatabase) ZooKeeper(org.apache.zookeeper.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) TxnLogEntry(org.apache.zookeeper.server.TxnLogEntry) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) Record(org.apache.jute.Record) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) Proposal(org.apache.zookeeper.server.quorum.Leader.Proposal) TxnHeader(org.apache.zookeeper.txn.TxnHeader) Test(org.junit.jupiter.api.Test)

Example 70 with ZooKeeperServer

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

the class LoadFromLogTest method testRestore.

/**
 * Test we can restore the snapshot that has data ahead of the zxid
 * of the snapshot file.
 */
@Test
public void testRestore() throws Exception {
    // generate some transactions
    ZooKeeper zk = createZKClient(hostPort);
    String lastPath = null;
    try {
        zk.create("/invalidsnap", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        for (int i = 0; i < NUM_MESSAGES; i++) {
            lastPath = zk.create("/invalidsnap/test-", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
        }
    } finally {
        zk.close();
    }
    String[] tokens = lastPath.split("-");
    String expectedPath = "/invalidsnap/test-" + String.format("%010d", (Integer.parseInt(tokens[1])) + 1);
    ZooKeeperServer zks = serverFactory.getZooKeeperServer();
    long eZxid = zks.getZKDatabase().getDataTreeLastProcessedZxid();
    // force the zxid to be behind the content
    zks.getZKDatabase().setlastProcessedZxid(zks.getZKDatabase().getDataTreeLastProcessedZxid() - 10);
    LOG.info("Set lastProcessedZxid to {}", zks.getZKDatabase().getDataTreeLastProcessedZxid());
    // Force snapshot and restore
    zks.takeSnapshot();
    zks.shutdown();
    stopServer();
    startServer();
    zks = serverFactory.getZooKeeperServer();
    long fZxid = zks.getZKDatabase().getDataTreeLastProcessedZxid();
    // Verify lastProcessedZxid is set correctly
    assertTrue(fZxid == eZxid, "Restore failed expected zxid=" + eZxid + " found=" + fZxid);
    zk = createZKClient(hostPort);
    // Verify correctness of data and whether sequential znode creation
    // proceeds correctly after this point
    String[] children;
    String path;
    try {
        children = zk.getChildren("/invalidsnap", false).toArray(new String[0]);
        path = zk.create("/invalidsnap/test-", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
    } finally {
        zk.close();
    }
    LOG.info("Expected {} found {}", expectedPath, path);
    assertTrue(path.equals(expectedPath), "Error in sequential znode creation expected " + expectedPath + " found " + path);
    assertTrue((children.length == NUM_MESSAGES), "Unexpected number of children " + children.length + " expected " + NUM_MESSAGES);
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) 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