Search in sources :

Example 11 with FileTxnSnapLog

use of org.apache.zookeeper.server.persistence.FileTxnSnapLog in project zookeeper by apache.

the class ZooKeeperServerMain method runFromConfig.

/**
 * Run from a ServerConfig.
 * @param config ServerConfig to use.
 * @throws IOException
 * @throws AdminServerException
 */
public void runFromConfig(ServerConfig config) throws IOException, AdminServerException {
    LOG.info("Starting server");
    FileTxnSnapLog txnLog = null;
    try {
        try {
            metricsProvider = MetricsProviderBootstrap.startMetricsProvider(config.getMetricsProviderClassName(), config.getMetricsProviderConfiguration());
        } catch (MetricsProviderLifeCycleException error) {
            throw new IOException("Cannot boot MetricsProvider " + config.getMetricsProviderClassName(), error);
        }
        ServerMetrics.metricsProviderInitialized(metricsProvider);
        ProviderRegistry.initialize();
        // Note that this thread isn't going to be doing anything else,
        // so rather than spawning another thread, we will just call
        // run() in this thread.
        // create a file logger url from the command line args
        txnLog = new FileTxnSnapLog(config.dataLogDir, config.dataDir);
        JvmPauseMonitor jvmPauseMonitor = null;
        if (config.jvmPauseMonitorToRun) {
            jvmPauseMonitor = new JvmPauseMonitor(config);
        }
        final ZooKeeperServer zkServer = new ZooKeeperServer(jvmPauseMonitor, txnLog, config.tickTime, config.minSessionTimeout, config.maxSessionTimeout, config.listenBacklog, null, config.initialConfig);
        txnLog.setServerStats(zkServer.serverStats());
        // Registers shutdown handler which will be used to know the
        // server error or shutdown state changes.
        final CountDownLatch shutdownLatch = new CountDownLatch(1);
        zkServer.registerServerShutdownHandler(new ZooKeeperServerShutdownHandler(shutdownLatch));
        // Start Admin server
        adminServer = AdminServerFactory.createAdminServer();
        adminServer.setZooKeeperServer(zkServer);
        adminServer.start();
        boolean needStartZKServer = true;
        if (config.getClientPortAddress() != null) {
            cnxnFactory = ServerCnxnFactory.createFactory();
            cnxnFactory.configure(config.getClientPortAddress(), config.getMaxClientCnxns(), config.getClientPortListenBacklog(), false);
            cnxnFactory.startup(zkServer);
            // zkServer has been started. So we don't need to start it again in secureCnxnFactory.
            needStartZKServer = false;
        }
        if (config.getSecureClientPortAddress() != null) {
            secureCnxnFactory = ServerCnxnFactory.createFactory();
            secureCnxnFactory.configure(config.getSecureClientPortAddress(), config.getMaxClientCnxns(), config.getClientPortListenBacklog(), true);
            secureCnxnFactory.startup(zkServer, needStartZKServer);
        }
        containerManager = new ContainerManager(zkServer.getZKDatabase(), zkServer.firstProcessor, Integer.getInteger("znode.container.checkIntervalMs", (int) TimeUnit.MINUTES.toMillis(1)), Integer.getInteger("znode.container.maxPerMinute", 10000), Long.getLong("znode.container.maxNeverUsedIntervalMs", 0));
        containerManager.start();
        ZKAuditProvider.addZKStartStopAuditLog();
        serverStarted();
        // Watch status of ZooKeeper server. It will do a graceful shutdown
        // if the server is not running or hits an internal error.
        shutdownLatch.await();
        shutdown();
        if (cnxnFactory != null) {
            cnxnFactory.join();
        }
        if (secureCnxnFactory != null) {
            secureCnxnFactory.join();
        }
        if (zkServer.canShutdown()) {
            zkServer.shutdown(true);
        }
    } catch (InterruptedException e) {
        // warn, but generally this is ok
        LOG.warn("Server interrupted", e);
    } finally {
        if (txnLog != null) {
            txnLog.close();
        }
        if (metricsProvider != null) {
            try {
                metricsProvider.stop();
            } catch (Throwable error) {
                LOG.warn("Error while stopping metrics", error);
            }
        }
    }
}
Also used : IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) MetricsProviderLifeCycleException(org.apache.zookeeper.metrics.MetricsProviderLifeCycleException) JvmPauseMonitor(org.apache.zookeeper.server.util.JvmPauseMonitor)

Example 12 with FileTxnSnapLog

use of org.apache.zookeeper.server.persistence.FileTxnSnapLog in project zookeeper by apache.

the class LearnerTest method syncTest.

@Test
public void syncTest() throws Exception {
    File tmpFile = File.createTempFile("test", ".dir", testData);
    tmpFile.delete();
    try {
        FileTxnSnapLog ftsl = new FileTxnSnapLog(tmpFile, tmpFile);
        SimpleLearner sl = new SimpleLearner(ftsl);
        long startZxid = sl.zk.getLastProcessedZxid();
        // Set up bogus streams
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BinaryOutputArchive oa = BinaryOutputArchive.getArchive(baos);
        sl.leaderOs = BinaryOutputArchive.getArchive(new ByteArrayOutputStream());
        // make streams and socket do something innocuous
        sl.bufferedOutput = new BufferedOutputStream(System.out);
        sl.sock = new Socket();
        // fake messages from the server
        QuorumPacket qp = new QuorumPacket(Leader.SNAP, 0, null, null);
        oa.writeRecord(qp, null);
        sl.zk.getZKDatabase().serializeSnapshot(oa);
        oa.writeString("BenWasHere", "signature");
        TxnHeader hdr = new TxnHeader(0, 0, 0, 0, ZooDefs.OpCode.create);
        CreateTxn txn = new CreateTxn("/foo", new byte[0], new ArrayList<ACL>(), false, sl.zk.getZKDatabase().getNode("/").stat.getCversion());
        ByteArrayOutputStream tbaos = new ByteArrayOutputStream();
        BinaryOutputArchive boa = BinaryOutputArchive.getArchive(tbaos);
        hdr.serialize(boa, "hdr");
        txn.serialize(boa, "txn");
        tbaos.close();
        qp = new QuorumPacket(Leader.PROPOSAL, 1, tbaos.toByteArray(), null);
        oa.writeRecord(qp, null);
        // setup the messages to be streamed to follower
        sl.leaderIs = BinaryInputArchive.getArchive(new ByteArrayInputStream(baos.toByteArray()));
        try {
            sl.syncWithLeader(3);
        } catch (EOFException e) {
        }
        sl.zk.shutdown();
        sl = new SimpleLearner(ftsl);
        assertEquals(startZxid, sl.zk.getLastProcessedZxid());
    } finally {
        TestUtils.deleteFileRecursively(tmpFile);
    }
}
Also used : BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) ACL(org.apache.zookeeper.data.ACL) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) CreateTxn(org.apache.zookeeper.txn.CreateTxn) ByteArrayInputStream(java.io.ByteArrayInputStream) EOFException(java.io.EOFException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) Socket(java.net.Socket) TxnHeader(org.apache.zookeeper.txn.TxnHeader) Test(org.junit.jupiter.api.Test)

Example 13 with FileTxnSnapLog

use of org.apache.zookeeper.server.persistence.FileTxnSnapLog 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 14 with FileTxnSnapLog

use of org.apache.zookeeper.server.persistence.FileTxnSnapLog 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 15 with FileTxnSnapLog

use of org.apache.zookeeper.server.persistence.FileTxnSnapLog in project zookeeper by apache.

the class LoadFromLogNoServerTest method testTxnFailure.

/**
 * For ZOOKEEPER-1046. Verify if cversion and pzxid if incremented
 * after create/delete failure during restore.
 */
@Test
public void testTxnFailure() throws Exception {
    try {
        ZooKeeperServer.setDigestEnabled(true);
        long count = 1;
        File tmpDir = ClientBase.createTmpDir();
        FileTxnSnapLog logFile = new FileTxnSnapLog(tmpDir, tmpDir);
        DataTree dt = new DataTree();
        dt.createNode("/test", new byte[0], null, 0, -1, 1, 1);
        for (count = 1; count <= 3; count++) {
            dt.createNode("/test/" + count, new byte[0], null, 0, -1, count, Time.currentElapsedTime());
        }
        long digestBefore = dt.getTreeDigest();
        DataNode zk = dt.getNode("/test");
        // Make create to fail, then verify cversion.
        LOG.info("Attempting to create /test/{}", (count - 1));
        doOp(logFile, ZooDefs.OpCode.create, "/test/" + (count - 1), dt, zk, -1);
        assertNotEquals(digestBefore, dt.getTreeDigest());
        LOG.info("Attempting to create /test/{}", (count - 1));
        digestBefore = dt.getTreeDigest();
        doOp(logFile, ZooDefs.OpCode.create, "/test/" + (count - 1), dt, zk, zk.stat.getCversion() + 1);
        assertNotEquals(digestBefore, dt.getTreeDigest());
        LOG.info("Attempting to create /test/{}", (count - 1));
        digestBefore = dt.getTreeDigest();
        doOp(logFile, ZooDefs.OpCode.multi, "/test/" + (count - 1), dt, zk, zk.stat.getCversion() + 1);
        assertNotEquals(digestBefore, dt.getTreeDigest());
        LOG.info("Attempting to create /test/{}", (count - 1));
        digestBefore = dt.getTreeDigest();
        doOp(logFile, ZooDefs.OpCode.multi, "/test/" + (count - 1), dt, zk, -1);
        assertNotEquals(digestBefore, dt.getTreeDigest());
    // Make delete fo fail, then verify cversion.
    // this doesn't happen anymore, we only set the cversion on create
    // LOG.info("Attempting to delete " + "/test/" + (count + 1));
    // doOp(logFile, OpCode.delete, "/test/" + (count + 1), dt, zk);
    } finally {
        ZooKeeperServer.setDigestEnabled(false);
    }
}
Also used : DataTree(org.apache.zookeeper.server.DataTree) DataNode(org.apache.zookeeper.server.DataNode) File(java.io.File) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) Test(org.junit.jupiter.api.Test)

Aggregations

FileTxnSnapLog (org.apache.zookeeper.server.persistence.FileTxnSnapLog)54 File (java.io.File)37 Test (org.junit.jupiter.api.Test)20 ZKDatabase (org.apache.zookeeper.server.ZKDatabase)17 IOException (java.io.IOException)13 NIOServerCnxnFactory (org.apache.zookeeper.server.NIOServerCnxnFactory)10 ZooKeeperServer (org.apache.zookeeper.server.ZooKeeperServer)10 ServerConfig (org.apache.zookeeper.server.ServerConfig)8 ZooKeeper (org.apache.zookeeper.ZooKeeper)6 QuorumPeer (org.apache.zookeeper.server.quorum.QuorumPeer)6 ArrayList (java.util.ArrayList)5 ServerCnxnFactory (org.apache.zookeeper.server.ServerCnxnFactory)5 TxnHeader (org.apache.zookeeper.txn.TxnHeader)5 BinaryOutputArchive (org.apache.jute.BinaryOutputArchive)4 ConfigException (org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Socket (java.net.Socket)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Record (org.apache.jute.Record)3 Test (org.junit.Test)3