Search in sources :

Example 6 with ZKDatabase

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

the class SyncRequestProcessorMetricTest method setup.

@BeforeEach
public void setup() throws Exception {
    ZKDatabase db = mock(ZKDatabase.class);
    when(db.append(any(Request.class))).thenReturn(true);
    doAnswer(invocation -> {
        Thread.sleep(100);
        return null;
    }).when(db).commit();
    zks = mock(ZooKeeperServer.class);
    when(zks.getZKDatabase()).thenReturn(db);
    nextProcessor = mock(RequestProcessor.class);
    doAnswer(invocationOnMock -> {
        allRequestsFlushed.countDown();
        return null;
    }).when(nextProcessor).processRequest(any(Request.class));
}
Also used : Request(org.apache.zookeeper.server.Request) ZKDatabase(org.apache.zookeeper.server.ZKDatabase) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) SyncRequestProcessor(org.apache.zookeeper.server.SyncRequestProcessor) RequestProcessor(org.apache.zookeeper.server.RequestProcessor) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 7 with ZKDatabase

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

the class WatchLeakTest method testWatchesLeak.

/**
 * Check that if session has expired then no watch can be set
 */
@ParameterizedTest
@ValueSource(booleans = { true, false })
public void testWatchesLeak(boolean sessionTimedout) throws Exception {
    NIOServerCnxnFactory serverCnxnFactory = mock(NIOServerCnxnFactory.class);
    final SelectionKey sk = new FakeSK();
    MockSelectorThread selectorThread = mock(MockSelectorThread.class);
    when(selectorThread.addInterestOpsUpdateRequest(any(SelectionKey.class))).thenAnswer(new Answer<Boolean>() {

        @Override
        public Boolean answer(InvocationOnMock invocation) throws Throwable {
            SelectionKey sk = (SelectionKey) invocation.getArguments()[0];
            NIOServerCnxn nioSrvCnx = (NIOServerCnxn) sk.attachment();
            sk.interestOps(nioSrvCnx.getInterestOps());
            return true;
        }
    });
    ZKDatabase database = new ZKDatabase(null);
    database.setlastProcessedZxid(2L);
    QuorumPeer quorumPeer = mock(QuorumPeer.class);
    FileTxnSnapLog logfactory = mock(FileTxnSnapLog.class);
    // Directories are not used but we need it to avoid NPE
    when(logfactory.getDataDir()).thenReturn(new File(""));
    when(logfactory.getSnapDir()).thenReturn(new File(""));
    FollowerZooKeeperServer fzks = null;
    try {
        // Create a new follower
        fzks = new FollowerZooKeeperServer(logfactory, quorumPeer, database);
        fzks.startup();
        fzks.setServerCnxnFactory(serverCnxnFactory);
        quorumPeer.follower = new MyFollower(quorumPeer, fzks);
        LOG.info("Follower created");
        // Simulate a socket channel between a client and a follower
        final SocketChannel socketChannel = createClientSocketChannel();
        // Create the NIOServerCnxn that will handle the client requests
        final MockNIOServerCnxn nioCnxn = new MockNIOServerCnxn(fzks, socketChannel, sk, serverCnxnFactory, selectorThread);
        sk.attach(nioCnxn);
        // Send the connection request as a client do
        nioCnxn.doIO(sk);
        LOG.info("Client connection sent");
        // Send the valid or invalid session packet to the follower
        QuorumPacket qp = createValidateSessionPacketResponse(!sessionTimedout);
        quorumPeer.follower.processPacket(qp);
        LOG.info("Session validation sent");
        // OK, now the follower knows that the session is valid or invalid, let's try
        // to send the watches
        nioCnxn.doIO(sk);
        // wait for the the request processor to do his job
        Thread.sleep(1000L);
        LOG.info("Watches processed");
        // If session has not been validated, there must be NO watches
        int watchCount = database.getDataTree().getWatchCount();
        if (sessionTimedout) {
            // Session has not been re-validated !
            LOG.info("session is not valid, watches = {}", watchCount);
            assertEquals(0, watchCount, "Session is not valid so there should be no watches");
        } else {
            // Session has been re-validated
            LOG.info("session is valid, watches = {}", watchCount);
            assertEquals(1, watchCount, "Session is valid so the watch should be there");
        }
    } finally {
        if (fzks != null) {
            fzks.shutdown();
        }
    }
}
Also used : SelectionKey(java.nio.channels.SelectionKey) SocketChannel(java.nio.channels.SocketChannel) MockNIOServerCnxn(org.apache.zookeeper.server.MockNIOServerCnxn) NIOServerCnxn(org.apache.zookeeper.server.NIOServerCnxn) ZKDatabase(org.apache.zookeeper.server.ZKDatabase) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) MockNIOServerCnxn(org.apache.zookeeper.server.MockNIOServerCnxn) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MockSelectorThread(org.apache.zookeeper.server.MockSelectorThread) NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory) File(java.io.File) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 8 with ZKDatabase

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

the class ZabUtils method prepareLeader.

private static LeaderZooKeeperServer prepareLeader(File tmpDir, QuorumPeer peer) throws IOException, NoSuchFieldException, IllegalAccessException {
    FileTxnSnapLog logFactory = new FileTxnSnapLog(tmpDir, tmpDir);
    peer.setTxnFactory(logFactory);
    ZKDatabase zkDb = new ZKDatabase(logFactory);
    LeaderZooKeeperServer zk = new LeaderZooKeeperServer(logFactory, peer, zkDb);
    return zk;
}
Also used : ZKDatabase(org.apache.zookeeper.server.ZKDatabase) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog)

Example 9 with ZKDatabase

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

the class FollowerResyncConcurrencyTest method verifyState.

private void verifyState(QuorumUtil qu, int index, Leader leader) {
    LOG.info("Verifying state");
    assertTrue(qu.getPeer(index).peer.follower != null, "Not following");
    long epochF = (qu.getPeer(index).peer.getActiveServer().getZxid() >> 32L);
    long epochL = (leader.getEpoch() >> 32L);
    assertTrue(epochF == epochL, "Zxid: " + qu.getPeer(index).peer.getActiveServer().getZKDatabase().getDataTreeLastProcessedZxid() + "Current epoch: " + epochF);
    int leaderIndex = (index == 1) ? 2 : 1;
    Collection<Long> sessionsRestarted = qu.getPeer(index).peer.getActiveServer().getZKDatabase().getSessions();
    Collection<Long> sessionsNotRestarted = qu.getPeer(leaderIndex).peer.getActiveServer().getZKDatabase().getSessions();
    for (Long l : sessionsRestarted) {
        assertTrue(sessionsNotRestarted.contains(l), "Should have same set of sessions in both servers, did not expect: " + l);
    }
    assertEquals(sessionsNotRestarted.size(), sessionsRestarted.size(), "Should have same number of sessions");
    ZKDatabase restarted = qu.getPeer(index).peer.getActiveServer().getZKDatabase();
    ZKDatabase clean = qu.getPeer(3).peer.getActiveServer().getZKDatabase();
    ZKDatabase lead = qu.getPeer(leaderIndex).peer.getActiveServer().getZKDatabase();
    for (Long l : sessionsRestarted) {
        LOG.info("Validating ephemeral for session id 0x{}", Long.toHexString(l));
        assertTrue(sessionsNotRestarted.contains(l), "Should have same set of sessions in both servers, did not expect: " + l);
        Set<String> ephemerals = restarted.getEphemerals(l);
        Set<String> cleanEphemerals = clean.getEphemerals(l);
        for (String o : cleanEphemerals) {
            if (!ephemerals.contains(o)) {
                LOG.info("Restarted follower doesn't contain ephemeral {} zxid 0x{}", o, Long.toHexString(clean.getDataTree().getNode(o).stat.getMzxid()));
            }
        }
        for (String o : ephemerals) {
            if (!cleanEphemerals.contains(o)) {
                LOG.info("Restarted follower has extra ephemeral {} zxid 0x{}", o, Long.toHexString(restarted.getDataTree().getNode(o).stat.getMzxid()));
            }
        }
        Set<String> leadEphemerals = lead.getEphemerals(l);
        for (String o : leadEphemerals) {
            if (!cleanEphemerals.contains(o)) {
                LOG.info("Follower doesn't contain ephemeral from leader {} zxid 0x{}", o, Long.toHexString(lead.getDataTree().getNode(o).stat.getMzxid()));
            }
        }
        for (String o : cleanEphemerals) {
            if (!leadEphemerals.contains(o)) {
                LOG.info("Leader doesn't contain ephemeral from follower {} zxid 0x{}", o, Long.toHexString(clean.getDataTree().getNode(o).stat.getMzxid()));
            }
        }
        assertEquals(ephemerals.size(), cleanEphemerals.size(), "Should have same number of ephemerals in both followers");
        assertEquals(lead.getEphemerals(l).size(), cleanEphemerals.size(), "Leader should equal follower");
    }
}
Also used : ZKDatabase(org.apache.zookeeper.server.ZKDatabase)

Example 10 with ZKDatabase

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

the class LocalSessionRequestTest method validateRequestLog.

/**
 * Walk through the target peer commmittedLog.
 * @param sessionId
 * @param peerId
 */
private void validateRequestLog(long sessionId, int peerId) {
    String session = Long.toHexString(sessionId);
    LOG.info("Searching for txn of session 0x " + session + " on peer " + peerId);
    String peerType = peerId == qb.getLeaderIndex() ? "leader" : "follower";
    QuorumPeer peer = qb.getPeerList().get(peerId);
    ZKDatabase db = peer.getActiveServer().getZKDatabase();
    for (Proposal p : db.getCommittedLog()) {
        assertFalse(p.request.sessionId == sessionId, "Should not see " + Request.op2String(p.request.type) + " request from local session 0x" + session + " on the " + peerType);
    }
}
Also used : QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) ZKDatabase(org.apache.zookeeper.server.ZKDatabase) Proposal(org.apache.zookeeper.server.quorum.Leader.Proposal)

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