Search in sources :

Example 16 with ZKDatabase

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

the class Zab1_0Test method prepareLeader.

private 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 17 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
 */
@Test
public void testWatchesLeak() 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("Session is not valid so there should be no watches", 0, watchCount);
        } else {
            // Session has been re-validated
            LOG.info("session is valid, watches = {}", watchCount);
            assertEquals("Session is valid so the watch should be there", 1, watchCount);
        }
    } 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) Test(org.junit.Test)

Example 18 with ZKDatabase

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

the class ZooKeeperStateServer method startDistributed.

private void startDistributed() throws IOException {
    logger.info("Starting Embedded ZooKeeper Peer");
    try {
        transactionLog = new FileTxnSnapLog(new File(quorumPeerConfig.getDataLogDir()), new File(quorumPeerConfig.getDataDir()));
        connectionFactory = ServerCnxnFactory.createFactory();
        connectionFactory.configure(quorumPeerConfig.getClientPortAddress(), quorumPeerConfig.getMaxClientCnxns());
        quorumPeer = new QuorumPeer();
        quorumPeer.setClientPortAddress(quorumPeerConfig.getClientPortAddress());
        quorumPeer.setTxnFactory(new FileTxnSnapLog(new File(quorumPeerConfig.getDataLogDir()), new File(quorumPeerConfig.getDataDir())));
        quorumPeer.setQuorumPeers(quorumPeerConfig.getServers());
        quorumPeer.setElectionType(quorumPeerConfig.getElectionAlg());
        quorumPeer.setMyid(quorumPeerConfig.getServerId());
        quorumPeer.setTickTime(quorumPeerConfig.getTickTime());
        quorumPeer.setMinSessionTimeout(quorumPeerConfig.getMinSessionTimeout());
        quorumPeer.setMaxSessionTimeout(quorumPeerConfig.getMaxSessionTimeout());
        quorumPeer.setInitLimit(quorumPeerConfig.getInitLimit());
        quorumPeer.setSyncLimit(quorumPeerConfig.getSyncLimit());
        quorumPeer.setQuorumVerifier(quorumPeerConfig.getQuorumVerifier());
        quorumPeer.setCnxnFactory(connectionFactory);
        quorumPeer.setZKDatabase(new ZKDatabase(quorumPeer.getTxnFactory()));
        quorumPeer.setLearnerType(quorumPeerConfig.getPeerType());
        quorumPeer.setSyncEnabled(quorumPeerConfig.getSyncEnabled());
        quorumPeer.setQuorumListenOnAllIPs(quorumPeerConfig.getQuorumListenOnAllIPs());
        quorumPeer.start();
    } catch (final IOException ioe) {
        throw new IOException("Failed to start embedded ZooKeeper Peer", ioe);
    } catch (final Exception e) {
        throw new RuntimeException("Failed to start embedded ZooKeeper Peer", e);
    }
}
Also used : QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) IOException(java.io.IOException) File(java.io.File) ZKDatabase(org.apache.zookeeper.server.ZKDatabase) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) ConfigException(org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException) IOException(java.io.IOException)

Example 19 with ZKDatabase

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

the class StatCommandTest method setUp.

@BeforeEach
public void setUp() {
    outputWriter = new StringWriter();
    ServerCnxn serverCnxnMock = mock(ServerCnxn.class);
    LeaderZooKeeperServer zks = mock(LeaderZooKeeperServer.class);
    when(zks.isRunning()).thenReturn(true);
    providerMock = mock(ServerStats.Provider.class);
    when(zks.serverStats()).thenReturn(new ServerStats(providerMock));
    ZKDatabase zkDatabaseMock = mock(ZKDatabase.class);
    when(zks.getZKDatabase()).thenReturn(zkDatabaseMock);
    Leader leaderMock = mock(Leader.class);
    when(leaderMock.getProposalStats()).thenReturn(new BufferStats());
    when(zks.getLeader()).thenReturn(leaderMock);
    ServerCnxnFactory serverCnxnFactory = mock(ServerCnxnFactory.class);
    ServerCnxn serverCnxn = mock(ServerCnxn.class);
    List<ServerCnxn> connections = new ArrayList<>();
    connections.add(serverCnxn);
    when(serverCnxnFactory.getConnections()).thenReturn(connections);
    statCommand = new StatCommand(new PrintWriter(outputWriter), serverCnxnMock, FourLetterCommands.statCmd);
    statCommand.setZkServer(zks);
    statCommand.setFactory(serverCnxnFactory);
}
Also used : ArrayList(java.util.ArrayList) ZKDatabase(org.apache.zookeeper.server.ZKDatabase) ServerCnxn(org.apache.zookeeper.server.ServerCnxn) StringWriter(java.io.StringWriter) ServerStats(org.apache.zookeeper.server.ServerStats) StatCommand(org.apache.zookeeper.server.command.StatCommand) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) PrintWriter(java.io.PrintWriter) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 20 with ZKDatabase

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

the class LeaderBeanTest method setUp.

@BeforeEach
public void setUp() throws IOException, X509Exception {
    qp = new QuorumPeer();
    quorumVerifierMock = mock(QuorumVerifier.class);
    when(quorumVerifierMock.getAllMembers()).thenReturn(getMockedPeerViews(qp.getId()));
    qp.setQuorumVerifier(quorumVerifierMock, false);
    File tmpDir = ClientBase.createEmptyTestDir();
    fileTxnSnapLog = new FileTxnSnapLog(new File(tmpDir, "data"), new File(tmpDir, "data_txnlog"));
    ZKDatabase zkDb = new ZKDatabase(fileTxnSnapLog);
    zks = new LeaderZooKeeperServer(fileTxnSnapLog, qp, zkDb);
    leader = new Leader(qp, zks);
    leaderBean = new LeaderBean(leader, zks);
}
Also used : File(java.io.File) ZKDatabase(org.apache.zookeeper.server.ZKDatabase) QuorumVerifier(org.apache.zookeeper.server.quorum.flexible.QuorumVerifier) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) BeforeEach(org.junit.jupiter.api.BeforeEach)

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