Search in sources :

Example 31 with ZKDatabase

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

the class TruncateTest method testTruncationStreamReset.

@Test
public void testTruncationStreamReset() throws Exception {
    File tmpdir = ClientBase.createTmpDir();
    FileTxnSnapLog snaplog = new FileTxnSnapLog(tmpdir, tmpdir);
    ZKDatabase zkdb = new ZKDatabase(snaplog);
    // make sure to snapshot, so that we have something there when
    // truncateLog reloads the db
    snaplog.save(zkdb.getDataTree(), zkdb.getSessionWithTimeOuts(), false);
    for (int i = 1; i <= 100; i++) {
        append(zkdb, i);
    }
    zkdb.truncateLog(1);
    append(zkdb, 200);
    zkdb.close();
    // verify that the truncation and subsequent append were processed
    // correctly
    FileTxnLog txnlog = new FileTxnLog(new File(tmpdir, "version-2"));
    TxnIterator iter = txnlog.read(1);
    TxnHeader hdr = iter.getHeader();
    Record txn = iter.getTxn();
    assertEquals(1, hdr.getZxid());
    assertTrue(txn instanceof SetDataTxn);
    iter.next();
    hdr = iter.getHeader();
    txn = iter.getTxn();
    assertEquals(200, hdr.getZxid());
    assertTrue(txn instanceof SetDataTxn);
    iter.close();
    ClientBase.recursiveDelete(tmpdir);
}
Also used : FileTxnLog(org.apache.zookeeper.server.persistence.FileTxnLog) Record(org.apache.jute.Record) SetDataTxn(org.apache.zookeeper.txn.SetDataTxn) File(java.io.File) ZKDatabase(org.apache.zookeeper.server.ZKDatabase) TxnIterator(org.apache.zookeeper.server.persistence.TxnLog.TxnIterator) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) TxnHeader(org.apache.zookeeper.txn.TxnHeader) Test(org.junit.jupiter.api.Test)

Example 32 with ZKDatabase

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

the class TruncateTest method testTruncate.

@Test
public void testTruncate() throws Exception {
    // Prime the server that is going to come in late with 50 txns
    String hostPort = "127.0.0.1:" + PortAssignment.unique();
    int maxCnxns = 100;
    ServerCnxnFactory factory = ClientBase.createNewServerInstance(null, hostPort, maxCnxns);
    ClientBase.startServerInstance(dataDir1, factory, hostPort, 1);
    ClientBase.shutdownServerInstance(factory, hostPort);
    // standalone starts with 0 epoch while quorum starts with 1
    File origfile = new File(new File(dataDir1, "version-2"), "snapshot.0");
    File newfile = new File(new File(dataDir1, "version-2"), "snapshot.100000000");
    origfile.renameTo(newfile);
    factory = ClientBase.createNewServerInstance(null, hostPort, maxCnxns);
    ClientBase.startServerInstance(dataDir1, factory, hostPort, 1);
    ZooKeeper zk = ClientBase.createZKClient(hostPort, 15000);
    for (int i = 0; i < 50; i++) {
        zk.create("/" + i, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }
    zk.close();
    ZKDatabase zkDb;
    {
        ZooKeeperServer zs = factory.getZooKeeperServer();
        zkDb = zs.getZKDatabase();
    }
    factory.shutdown();
    try {
        zkDb.close();
    } catch (IOException ie) {
        LOG.warn("Error closing logs ", ie);
    }
    int tickTime = 2000;
    int initLimit = 3;
    int syncLimit = 3;
    int connectToLearnerMasterLimit = 3;
    int port1 = PortAssignment.unique();
    int port2 = PortAssignment.unique();
    int port3 = PortAssignment.unique();
    // Start up two of the quorum and add 10 txns
    Map<Long, QuorumServer> peers = new HashMap<Long, QuorumServer>();
    peers.put(Long.valueOf(1), new QuorumServer(1, new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", port1)));
    peers.put(Long.valueOf(2), new QuorumServer(2, new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", port2)));
    peers.put(Long.valueOf(3), new QuorumServer(3, new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", port3)));
    QuorumPeer s2 = new QuorumPeer(peers, dataDir2, dataDir2, port2, 3, 2, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit);
    s2.start();
    QuorumPeer s3 = new QuorumPeer(peers, dataDir3, dataDir3, port3, 3, 3, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit);
    s3.start();
    zk = ClientBase.createZKClient("127.0.0.1:" + port2, 15000);
    for (int i = 0; i < 10; i++) {
        zk.create("/" + i, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }
    zk.close();
    final ZooKeeper zk2 = ClientBase.createZKClient("127.0.0.1:" + port2, 15000);
    zk2.getData("/9", false, new Stat());
    try {
        zk2.getData("/10", false, new Stat());
        fail("Should have gotten an error");
    } catch (KeeperException.NoNodeException e) {
    // this is what we want
    }
    QuorumPeer s1 = new QuorumPeer(peers, dataDir1, dataDir1, port1, 3, 1, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit);
    s1.start();
    ZooKeeper zk1 = ClientBase.createZKClient("127.0.0.1:" + port1, 15000);
    zk1.getData("/9", false, new Stat());
    try {
        // /10 wont work because the session expiration
        // will match the zxid for /10 and so we wont
        // actually truncate the zxid for /10 creation
        // due to an artifact of switching the xid of the standalone
        // /11 is the last entry in the log for the xid
        // as a result /12 is the first of the truncated znodes to check for
        zk1.getData("/12", false, new Stat());
        fail("Should have gotten an error");
    } catch (KeeperException.NoNodeException e) {
    // this is what we want
    }
    zk1.close();
    QuorumBase.shutdown(s1);
    QuorumBase.shutdown(s2);
    QuorumBase.shutdown(s3);
}
Also used : HashMap(java.util.HashMap) QuorumServer(org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) ZKDatabase(org.apache.zookeeper.server.ZKDatabase) ZooKeeper(org.apache.zookeeper.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) KeeperException(org.apache.zookeeper.KeeperException) Test(org.junit.jupiter.api.Test)

Example 33 with ZKDatabase

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

the class TruncateTest method testTruncationNullLog.

@Test
public void testTruncationNullLog() throws Exception {
    File tmpdir = ClientBase.createTmpDir();
    FileTxnSnapLog snaplog = new FileTxnSnapLog(tmpdir, tmpdir);
    ZKDatabase zkdb = new ZKDatabase(snaplog);
    for (int i = 1; i <= 100; i++) {
        append(zkdb, i);
    }
    zkdb.close();
    File[] logs = snaplog.getDataDir().listFiles();
    for (int i = 0; i < logs.length; i++) {
        LOG.debug("Deleting: {}", logs[i].getName());
        assertTrue(logs[i].delete(), "Failed to delete log file: " + logs[i].getName());
    }
    try {
        assertThat("truncateLog() should return false if truncation fails instead of throwing exception", zkdb.truncateLog(1), is(false));
    } catch (NullPointerException npe) {
        fail("This should not throw NPE!");
    }
    ClientBase.recursiveDelete(tmpdir);
}
Also used : File(java.io.File) ZKDatabase(org.apache.zookeeper.server.ZKDatabase) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) Test(org.junit.jupiter.api.Test)

Example 34 with ZKDatabase

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

the class ZkDatabaseCorruptionTest method testAbsentRecentSnapshot.

@Test
public void testAbsentRecentSnapshot() throws IOException {
    ZKDatabase zkDatabase = new ZKDatabase(new FileTxnSnapLog(new File("foo"), new File("bar")) {

        @Override
        public File findMostRecentSnapshot() throws IOException {
            return null;
        }
    });
    assertEquals(0, zkDatabase.calculateTxnLogSizeLimit());
}
Also used : IOException(java.io.IOException) ZKDatabase(org.apache.zookeeper.server.ZKDatabase) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) Test(org.junit.jupiter.api.Test)

Example 35 with ZKDatabase

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

the class NonRecoverableErrorTest method testZooKeeperServiceAvailableOnLeader.

/**
 * Test case for https://issues.apache.org/jira/browse/ZOOKEEPER-2247.
 * Test to verify that even after non recoverable error (error while
 * writing transaction log), ZooKeeper is still available.
 */
@Test
@Timeout(value = 30)
public void testZooKeeperServiceAvailableOnLeader() throws Exception {
    int SERVER_COUNT = 3;
    final int[] clientPorts = new int[SERVER_COUNT];
    StringBuilder sb = new StringBuilder();
    String server;
    for (int i = 0; i < SERVER_COUNT; i++) {
        clientPorts[i] = PortAssignment.unique();
        server = "server." + i + "=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ":participant;127.0.0.1:" + clientPorts[i];
        sb.append(server + "\n");
    }
    String currentQuorumCfgSection = sb.toString();
    MainThread[] mt = new MainThread[SERVER_COUNT];
    for (int i = 0; i < SERVER_COUNT; i++) {
        mt[i] = new MainThread(i, clientPorts[i], currentQuorumCfgSection, false);
        mt[i].start();
    }
    // ensure server started
    for (int i = 0; i < SERVER_COUNT; i++) {
        assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], CONNECTION_TIMEOUT), "waiting for server " + i + " being up");
    }
    CountdownWatcher watcher = new CountdownWatcher();
    ZooKeeper zk = new ZooKeeper("127.0.0.1:" + clientPorts[0], ClientBase.CONNECTION_TIMEOUT, watcher);
    watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT);
    String data = "originalData";
    zk.create(NODE_PATH, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    // get information of current leader
    QuorumPeer leader = getLeaderQuorumPeer(mt);
    assertNotNull(leader, "Leader must have been elected by now");
    // inject problem in leader
    FileTxnSnapLog snapLog = leader.getActiveServer().getTxnLogFactory();
    FileTxnSnapLog fileTxnSnapLogWithError = new FileTxnSnapLog(snapLog.getDataDir(), snapLog.getSnapDir()) {

        @Override
        public void commit() throws IOException {
            throw new IOException("Input/output error");
        }
    };
    ZKDatabase originalZKDatabase = leader.getActiveServer().getZKDatabase();
    long leaderCurrentEpoch = leader.getCurrentEpoch();
    ZKDatabase newDB = new ZKDatabase(fileTxnSnapLogWithError);
    leader.getActiveServer().setZKDatabase(newDB);
    try {
        // do create operation, so that injected IOException is thrown
        zk.create(uniqueZnode(), data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        fail("IOException is expected due to error injected to transaction log commit");
    } catch (Exception e) {
    // do nothing
    }
    // resetting watcher so that this watcher can be again used to ensure
    // that the zkClient is able to re-establish connection with the
    // newly elected zookeeper quorum.
    watcher.reset();
    waitForNewLeaderElection(leader, leaderCurrentEpoch);
    // takes place
    for (int i = 0; i < SERVER_COUNT; i++) {
        assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], CONNECTION_TIMEOUT), "waiting for server " + i + " being up");
    }
    // revert back the error
    leader.getActiveServer().setZKDatabase(originalZKDatabase);
    // verify that now ZooKeeper service is up and running
    leader = getLeaderQuorumPeer(mt);
    assertNotNull(leader, "New leader must have been elected by now");
    String uniqueNode = uniqueZnode();
    watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT);
    String createNode = zk.create(uniqueNode, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    // if node is created successfully then it means that ZooKeeper service
    // is available
    assertEquals(uniqueNode, createNode, "Failed to create znode");
    zk.close();
    // stop all severs
    for (int i = 0; i < SERVER_COUNT; i++) {
        mt[i].shutdown();
    }
}
Also used : CountdownWatcher(org.apache.zookeeper.test.ClientBase.CountdownWatcher) IOException(java.io.IOException) ZKDatabase(org.apache.zookeeper.server.ZKDatabase) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) IOException(java.io.IOException) ZooKeeper(org.apache.zookeeper.ZooKeeper) QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

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