Search in sources :

Example 11 with ZKDatabase

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

the class Zab1_0Test method createObserver.

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

Example 12 with ZKDatabase

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

the class Zab1_0Test method testInitialAcceptedCurrent.

@Test
public void testInitialAcceptedCurrent() throws Exception {
    File tmpDir = File.createTempFile("test", ".dir", testData);
    tmpDir.delete();
    tmpDir.mkdir();
    try {
        FileTxnSnapLog logFactory = new FileTxnSnapLog(tmpDir, tmpDir);
        File version2 = new File(tmpDir, "version-2");
        version2.mkdir();
        logFactory.save(new DataTree(), new ConcurrentHashMap<Long, Integer>(), false);
        long zxid = ZxidUtils.makeZxid(3, 3);
        logFactory.append(new Request(1, 1, ZooDefs.OpCode.error, new TxnHeader(1, 1, zxid, 1, ZooDefs.OpCode.error), new ErrorTxn(1), zxid));
        logFactory.commit();
        ZKDatabase zkDb = new ZKDatabase(logFactory);
        QuorumPeer peer = QuorumPeer.testingQuorumPeer();
        peer.setZKDatabase(zkDb);
        peer.setTxnFactory(logFactory);
        peer.getLastLoggedZxid();
        assertEquals(3, peer.getAcceptedEpoch());
        assertEquals(3, peer.getCurrentEpoch());
        assertEquals(3, Integer.parseInt(readContentsOfFile(new File(version2, QuorumPeer.CURRENT_EPOCH_FILENAME))));
        assertEquals(3, Integer.parseInt(readContentsOfFile(new File(version2, QuorumPeer.ACCEPTED_EPOCH_FILENAME))));
    } finally {
        TestUtils.deleteFileRecursively(tmpDir);
    }
}
Also used : ErrorTxn(org.apache.zookeeper.txn.ErrorTxn) DataTree(org.apache.zookeeper.server.DataTree) Request(org.apache.zookeeper.server.Request) ZabUtils.createQuorumPeer(org.apache.zookeeper.server.quorum.ZabUtils.createQuorumPeer) File(java.io.File) ZKDatabase(org.apache.zookeeper.server.ZKDatabase) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) TxnHeader(org.apache.zookeeper.txn.TxnHeader) Test(org.junit.jupiter.api.Test)

Example 13 with ZKDatabase

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

the class ClientBase method shutdownServerInstance.

static void shutdownServerInstance(ServerCnxnFactory factory, String hostPort) {
    if (factory != null) {
        ZKDatabase zkDb = null;
        {
            ZooKeeperServer zs = factory.getZooKeeperServer();
            if (zs != null) {
                zkDb = zs.getZKDatabase();
            }
        }
        factory.shutdown();
        try {
            if (zkDb != null) {
                zkDb.close();
            }
        } catch (IOException ie) {
            LOG.warn("Error closing logs ", ie);
        }
        final int PORT = getPort(hostPort);
        assertTrue(ClientBase.waitForServerDown("127.0.0.1:" + PORT, CONNECTION_TIMEOUT, factory.isSecure()), "waiting for server down");
    }
}
Also used : IOException(java.io.IOException) ZKDatabase(org.apache.zookeeper.server.ZKDatabase) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer)

Example 14 with ZKDatabase

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

the class FileTxnLogTest method testLogSizeLimit.

/**
 * Test that the server can correctly load the data when there are multiple
 * txnlogs per snapshot
 */
@Test
public void testLogSizeLimit() throws Exception {
    File tmpDir = ClientBase.createTmpDir();
    ClientBase.setupTestEnv();
    // Need to override preallocate set by setupTestEnv()
    // We don't need to unset these values since each unit test run in
    // a separate JVM instance
    FileTxnLog.setPreallocSize(PREALLOCATE);
    FileTxnLog.setTxnLogSizeLimit(LOG_SIZE_LIMIT);
    ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
    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 = new ZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, DummyWatcher.INSTANCE);
    // Generate transactions
    HashSet<Long> zxids = new HashSet<>();
    byte[] bytes = new byte[NODE_SIZE];
    Random random = new Random();
    random.nextBytes(bytes);
    // We will create enough txn to generate 3 logs
    long txnCount = LOG_SIZE_LIMIT / NODE_SIZE / 2 * 5;
    LOG.info("Creating {} txns", txnCount);
    try {
        for (long i = 0; i < txnCount; i++) {
            Stat stat = new Stat();
            zk.create("/node-" + i, bytes, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            zk.getData("/node-" + i, null, stat);
            zxids.add(stat.getCzxid());
        }
    } finally {
        zk.close();
    }
    // shutdown
    f.shutdown();
    assertTrue(ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT), "waiting for server to shutdown");
    File logDir = new File(tmpDir, FileTxnSnapLog.version + FileTxnSnapLog.VERSION);
    File[] txnLogs = FileTxnLog.getLogFiles(logDir.listFiles(), 0);
    assertEquals(3, txnLogs.length, "Unexpected number of logs");
    // Log size should not exceed limit by more than one node size;
    long threshold = LOG_SIZE_LIMIT + NODE_SIZE;
    LOG.info(txnLogs[0].getAbsolutePath());
    assertTrue(threshold > txnLogs[0].length(), "Exceed log size limit: " + txnLogs[0].length());
    LOG.info(txnLogs[1].getAbsolutePath());
    assertTrue(threshold > txnLogs[1].length(), "Exceed log size limit " + txnLogs[1].length());
    // Start database only
    zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
    zks.startdata();
    ZKDatabase db = zks.getZKDatabase();
    for (long i = 0; i < txnCount; i++) {
        Stat stat = new Stat();
        byte[] data = db.getData("/node-" + i, stat, null);
        assertArrayEquals(bytes, data, "Missmatch data");
        assertTrue(zxids.contains(stat.getMzxid()), "Unknown zxid ");
    }
}
Also used : ZKDatabase(org.apache.zookeeper.server.ZKDatabase) ZooKeeper(org.apache.zookeeper.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) Random(java.util.Random) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 15 with ZKDatabase

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

the class ZooKeeperServiceRunner method stop.

public void stop() {
    try {
        ServerCnxnFactory sf = this.serverFactory.getAndSet(null);
        if (sf != null) {
            sf.closeAll(ServerCnxn.DisconnectReason.CLOSE_ALL_CONNECTIONS_FORCED);
            sf.shutdown();
        }
    } catch (Throwable e) {
        log.warn("Unable to cleanly shutdown ZooKeeper connection factory", e);
    }
    try {
        ZooKeeperServer zs = this.server.getAndSet(null);
        if (zs != null) {
            zs.shutdown();
            ZKDatabase zkDb = zs.getZKDatabase();
            if (zkDb != null) {
                // make ZK server close its log files
                zkDb.close();
            }
        }
    } catch (Throwable e) {
        log.warn("Unable to cleanly shutdown ZooKeeper server", e);
    }
    if (secureZK) {
        ZKTLSUtils.unsetSecureZKServerProperties();
    }
}
Also used : ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) NettyServerCnxnFactory(org.apache.zookeeper.server.NettyServerCnxnFactory) 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