Search in sources :

Example 26 with ZKDatabase

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

the class Zab1_0Test method createFollower.

private ConversableFollower createFollower(File tmpDir, QuorumPeer peer) throws IOException {
    FileTxnSnapLog logFactory = new FileTxnSnapLog(tmpDir, tmpDir);
    peer.setTxnFactory(logFactory);
    ZKDatabase zkDb = new ZKDatabase(logFactory);
    FollowerZooKeeperServer zk = new FollowerZooKeeperServer(logFactory, peer, zkDb);
    peer.setZKDatabase(zkDb);
    return new ConversableFollower(peer, zk);
}
Also used : ZKDatabase(org.apache.zookeeper.server.ZKDatabase) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog)

Example 27 with ZKDatabase

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

the class Zab1_0Test method testPopulatedLeaderConversation.

public void testPopulatedLeaderConversation(PopulatedLeaderConversation conversation, int ops) throws Exception {
    Socket[] pair = getSocketPair();
    Socket leaderSocket = pair[0];
    Socket followerSocket = pair[1];
    File tmpDir = File.createTempFile("test", "dir", testData);
    tmpDir.delete();
    tmpDir.mkdir();
    LeadThread leadThread = null;
    Leader leader = null;
    try {
        // Setup a database with two znodes
        FileTxnSnapLog snapLog = new FileTxnSnapLog(tmpDir, tmpDir);
        ZKDatabase zkDb = new ZKDatabase(snapLog);
        assertTrue(ops >= 1);
        long zxid = ZxidUtils.makeZxid(1, 0);
        for (int i = 1; i <= ops; i++) {
            zxid = ZxidUtils.makeZxid(1, i);
            String path = "/foo-" + i;
            zkDb.processTxn(new TxnHeader(13, 1000 + i, zxid, 30 + i, ZooDefs.OpCode.create), new CreateTxn(path, "fpjwasalsohere".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, false, 1), null);
            Stat stat = new Stat();
            assertEquals("fpjwasalsohere", new String(zkDb.getData(path, stat, null)));
        }
        assertTrue(zxid > ZxidUtils.makeZxid(1, 0));
        // Generate snapshot and close files.
        snapLog.save(zkDb.getDataTree(), zkDb.getSessionWithTimeOuts(), false);
        snapLog.close();
        QuorumPeer peer = createQuorumPeer(tmpDir);
        leader = createLeader(tmpDir, peer);
        peer.leader = leader;
        // Set the last accepted epoch and current epochs to be 1
        peer.setAcceptedEpoch(1);
        peer.setCurrentEpoch(1);
        leadThread = new LeadThread(leader);
        leadThread.start();
        while (leader.cnxAcceptor == null || !leader.cnxAcceptor.isAlive()) {
            Thread.sleep(20);
        }
        LearnerHandler lh = new LearnerHandler(leaderSocket, new BufferedInputStream(leaderSocket.getInputStream()), leader);
        lh.start();
        leaderSocket.setSoTimeout(4000);
        InputArchive ia = BinaryInputArchive.getArchive(followerSocket.getInputStream());
        OutputArchive oa = BinaryOutputArchive.getArchive(followerSocket.getOutputStream());
        conversation.converseWithLeader(ia, oa, leader, zxid);
    } finally {
        if (leader != null) {
            leader.shutdown("end of test");
        }
        if (leadThread != null) {
            leadThread.interrupt();
            leadThread.join();
        }
        TestUtils.deleteFileRecursively(tmpDir);
    }
}
Also used : MockLeader(org.apache.zookeeper.server.quorum.ZabUtils.MockLeader) ZabUtils.createMockLeader(org.apache.zookeeper.server.quorum.ZabUtils.createMockLeader) ZabUtils.createLeader(org.apache.zookeeper.server.quorum.ZabUtils.createLeader) InputArchive(org.apache.jute.InputArchive) BinaryInputArchive(org.apache.jute.BinaryInputArchive) ZKDatabase(org.apache.zookeeper.server.ZKDatabase) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) CreateTxn(org.apache.zookeeper.txn.CreateTxn) Stat(org.apache.zookeeper.data.Stat) BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) OutputArchive(org.apache.jute.OutputArchive) BufferedInputStream(java.io.BufferedInputStream) ZabUtils.createQuorumPeer(org.apache.zookeeper.server.quorum.ZabUtils.createQuorumPeer) File(java.io.File) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) TxnHeader(org.apache.zookeeper.txn.TxnHeader)

Example 28 with ZKDatabase

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

the class LearnerHandler method syncFollower.

/**
 * Determine if we need to sync with follower using DIFF/TRUNC/SNAP
 * and setup follower to receive packets from commit processor
 *
 * @param peerLastZxid
 * @param learnerMaster
 * @return true if snapshot transfer is needed.
 */
boolean syncFollower(long peerLastZxid, LearnerMaster learnerMaster) {
    /*
         * When leader election is completed, the leader will set its
         * lastProcessedZxid to be (epoch < 32). There will be no txn associated
         * with this zxid.
         *
         * The learner will set its lastProcessedZxid to the same value if
         * it get DIFF or SNAP from the learnerMaster. If the same learner come
         * back to sync with learnerMaster using this zxid, we will never find this
         * zxid in our history. In this case, we will ignore TRUNC logic and
         * always send DIFF if we have old enough history
         */
    boolean isPeerNewEpochZxid = (peerLastZxid & 0xffffffffL) == 0;
    // Keep track of the latest zxid which already queued
    long currentZxid = peerLastZxid;
    boolean needSnap = true;
    ZKDatabase db = learnerMaster.getZKDatabase();
    boolean txnLogSyncEnabled = db.isTxnLogSyncEnabled();
    ReentrantReadWriteLock lock = db.getLogLock();
    ReadLock rl = lock.readLock();
    try {
        rl.lock();
        long maxCommittedLog = db.getmaxCommittedLog();
        long minCommittedLog = db.getminCommittedLog();
        long lastProcessedZxid = db.getDataTreeLastProcessedZxid();
        LOG.info("Synchronizing with Learner sid: {} maxCommittedLog=0x{}" + " minCommittedLog=0x{} lastProcessedZxid=0x{}" + " peerLastZxid=0x{}", getSid(), Long.toHexString(maxCommittedLog), Long.toHexString(minCommittedLog), Long.toHexString(lastProcessedZxid), Long.toHexString(peerLastZxid));
        if (db.getCommittedLog().isEmpty()) {
            /*
                 * It is possible that committedLog is empty. In that case
                 * setting these value to the latest txn in learnerMaster db
                 * will reduce the case that we need to handle
                 *
                 * Here is how each case handle by the if block below
                 * 1. lastProcessZxid == peerZxid -> Handle by (2)
                 * 2. lastProcessZxid < peerZxid -> Handle by (3)
                 * 3. lastProcessZxid > peerZxid -> Handle by (5)
                 */
            minCommittedLog = lastProcessedZxid;
            maxCommittedLog = lastProcessedZxid;
        }
        if (forceSnapSync) {
            // Force learnerMaster to use snapshot to sync with follower
            LOG.warn("Forcing snapshot sync - should not see this in production");
        } else if (lastProcessedZxid == peerLastZxid) {
            // Follower is already sync with us, send empty diff
            LOG.info("Sending DIFF zxid=0x{} for peer sid: {}", Long.toHexString(peerLastZxid), getSid());
            queueOpPacket(Leader.DIFF, peerLastZxid);
            needOpPacket = false;
            needSnap = false;
        } else if (peerLastZxid > maxCommittedLog && !isPeerNewEpochZxid) {
            // Newer than committedLog, send trunc and done
            LOG.debug("Sending TRUNC to follower zxidToSend=0x{} for peer sid:{}", Long.toHexString(maxCommittedLog), getSid());
            queueOpPacket(Leader.TRUNC, maxCommittedLog);
            currentZxid = maxCommittedLog;
            needOpPacket = false;
            needSnap = false;
        } else if ((maxCommittedLog >= peerLastZxid) && (minCommittedLog <= peerLastZxid)) {
            // Follower is within commitLog range
            LOG.info("Using committedLog for peer sid: {}", getSid());
            Iterator<Proposal> itr = db.getCommittedLog().iterator();
            currentZxid = queueCommittedProposals(itr, peerLastZxid, null, maxCommittedLog);
            needSnap = false;
        } else if (peerLastZxid < minCommittedLog && txnLogSyncEnabled) {
            // Use txnlog and committedLog to sync
            // Calculate sizeLimit that we allow to retrieve txnlog from disk
            long sizeLimit = db.calculateTxnLogSizeLimit();
            // This method can return empty iterator if the requested zxid
            // is older than on-disk txnlog
            Iterator<Proposal> txnLogItr = db.getProposalsFromTxnLog(peerLastZxid, sizeLimit);
            if (txnLogItr.hasNext()) {
                LOG.info("Use txnlog and committedLog for peer sid: {}", getSid());
                currentZxid = queueCommittedProposals(txnLogItr, peerLastZxid, minCommittedLog, maxCommittedLog);
                if (currentZxid < minCommittedLog) {
                    LOG.info("Detected gap between end of txnlog: 0x{} and start of committedLog: 0x{}", Long.toHexString(currentZxid), Long.toHexString(minCommittedLog));
                    currentZxid = peerLastZxid;
                    // Clear out currently queued requests and revert
                    // to sending a snapshot.
                    queuedPackets.clear();
                    needOpPacket = true;
                } else {
                    LOG.debug("Queueing committedLog 0x{}", Long.toHexString(currentZxid));
                    Iterator<Proposal> committedLogItr = db.getCommittedLog().iterator();
                    currentZxid = queueCommittedProposals(committedLogItr, currentZxid, null, maxCommittedLog);
                    needSnap = false;
                }
            }
            // closing the resources
            if (txnLogItr instanceof TxnLogProposalIterator) {
                TxnLogProposalIterator txnProposalItr = (TxnLogProposalIterator) txnLogItr;
                txnProposalItr.close();
            }
        } else {
            LOG.warn("Unhandled scenario for peer sid: {} maxCommittedLog=0x{}" + " minCommittedLog=0x{} lastProcessedZxid=0x{}" + " peerLastZxid=0x{} txnLogSyncEnabled={}", getSid(), Long.toHexString(maxCommittedLog), Long.toHexString(minCommittedLog), Long.toHexString(lastProcessedZxid), Long.toHexString(peerLastZxid), txnLogSyncEnabled);
        }
        if (needSnap) {
            currentZxid = db.getDataTreeLastProcessedZxid();
        }
        LOG.debug("Start forwarding 0x{} for peer sid: {}", Long.toHexString(currentZxid), getSid());
        leaderLastZxid = learnerMaster.startForwarding(this, currentZxid);
    } finally {
        rl.unlock();
    }
    if (needOpPacket && !needSnap) {
        // This should never happen, but we should fall back to sending
        // snapshot just in case.
        LOG.error("Unhandled scenario for peer sid: {} fall back to use snapshot", getSid());
        needSnap = true;
    }
    return needSnap;
}
Also used : ReadLock(java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock) TxnLogProposalIterator(org.apache.zookeeper.server.TxnLogProposalIterator) Iterator(java.util.Iterator) TxnLogProposalIterator(org.apache.zookeeper.server.TxnLogProposalIterator) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ZKDatabase(org.apache.zookeeper.server.ZKDatabase) Proposal(org.apache.zookeeper.server.quorum.Leader.Proposal)

Example 29 with ZKDatabase

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

the class CommandsTest method testStatCommandSecureOnly.

/**
 * testing Stat command, when only SecureClientPort is defined by the user and there is no
 * regular (non-SSL port) open. In this case zkServer.getServerCnxnFactory === null
 * see: ZOOKEEPER-3633
 */
@Test
public void testStatCommandSecureOnly() {
    Commands.StatCommand cmd = new Commands.StatCommand();
    ZooKeeperServer zkServer = mock(ZooKeeperServer.class);
    ServerCnxnFactory cnxnFactory = mock(ServerCnxnFactory.class);
    ServerStats serverStats = mock(ServerStats.class);
    ZKDatabase zkDatabase = mock(ZKDatabase.class);
    when(zkServer.getSecureServerCnxnFactory()).thenReturn(cnxnFactory);
    when(zkServer.serverStats()).thenReturn(serverStats);
    when(zkServer.getZKDatabase()).thenReturn(zkDatabase);
    when(zkDatabase.getNodeCount()).thenReturn(0);
    CommandResponse response = cmd.run(zkServer, null);
    assertThat(response.toMap().containsKey("connections"), is(true));
    assertThat(response.toMap().containsKey("secure_connections"), is(true));
}
Also used : ServerStats(org.apache.zookeeper.server.ServerStats) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) ZKDatabase(org.apache.zookeeper.server.ZKDatabase) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) Test(org.junit.jupiter.api.Test)

Example 30 with ZKDatabase

use of org.apache.zookeeper.server.ZKDatabase in project ignite by apache.

the class ZookeeperDiscoverySpiSaslAuthAbstractTest method shutdownServerInstance.

/**
 */
private void shutdownServerInstance(ServerCnxnFactory factory) {
    if (factory != null) {
        ZKDatabase zkDb = null;
        {
            ZooKeeperServer zs = getServer(factory);
            if (zs != null)
                zkDb = zs.getZKDatabase();
        }
        factory.shutdown();
        try {
            if (zkDb != null)
                zkDb.close();
        } catch (IOException ie) {
        // ignore
        }
    }
}
Also used : IOException(java.io.IOException) ZKDatabase(org.apache.zookeeper.server.ZKDatabase) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer)

Aggregations

ZKDatabase (org.apache.zookeeper.server.ZKDatabase)38 FileTxnSnapLog (org.apache.zookeeper.server.persistence.FileTxnSnapLog)17 File (java.io.File)13 IOException (java.io.IOException)9 ZooKeeperServer (org.apache.zookeeper.server.ZooKeeperServer)9 Test (org.junit.jupiter.api.Test)9 ServerCnxnFactory (org.apache.zookeeper.server.ServerCnxnFactory)8 Stat (org.apache.zookeeper.data.Stat)6 QuorumPeer (org.apache.zookeeper.server.quorum.QuorumPeer)5 ZooKeeper (org.apache.zookeeper.ZooKeeper)4 TxnHeader (org.apache.zookeeper.txn.TxnHeader)4 ServerStats (org.apache.zookeeper.server.ServerStats)3 Proposal (org.apache.zookeeper.server.quorum.Leader.Proposal)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 TestEndpoint (com.twitter.common.zookeeper.testing.angrybird.gen.TestEndpoint)2 InetSocketAddress (java.net.InetSocketAddress)2 SelectionKey (java.nio.channels.SelectionKey)2 SocketChannel (java.nio.channels.SocketChannel)2 ParseException (java.text.ParseException)2 ArrayList (java.util.ArrayList)2