Search in sources :

Example 51 with FileTxnSnapLog

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

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

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

Example 54 with FileTxnSnapLog

use of org.apache.zookeeper.server.persistence.FileTxnSnapLog in project coprhd-controller by CoprHD.

the class CoordinatorImpl method runFromConfig.

// Start Zookeeper peer in cluster mode
private void runFromConfig(SpringQuorumPeerConfig config) throws Exception {
    _log.info(String.format("Starting quorum peer from config for %d", config.getServerId()));
    ServerCnxnFactory cnxnFactory = ServerCnxnFactory.createFactory();
    cnxnFactory.configure(config.getClientPortAddress(), config.getMaxClientCnxns());
    QuorumPeer quorumPeer = new QuorumPeer();
    quorumPeer.setClientPortAddress(config.getClientPortAddress());
    quorumPeer.setTxnFactory(new FileTxnSnapLog(new File(config.getDataLogDir()), new File(config.getDataDir())));
    quorumPeer.setQuorumPeers(config.getServers());
    quorumPeer.setElectionType(config.getElectionAlg());
    quorumPeer.setMyid(config.getServerId());
    quorumPeer.setTickTime(config.getTickTime());
    quorumPeer.setMinSessionTimeout(config.getMinSessionTimeout());
    quorumPeer.setMaxSessionTimeout(config.getMaxSessionTimeout());
    quorumPeer.setInitLimit(config.getInitLimit());
    quorumPeer.setSyncLimit(config.getSyncLimit());
    quorumPeer.setQuorumVerifier(config.getQuorumVerifier());
    quorumPeer.setCnxnFactory(cnxnFactory);
    quorumPeer.setZKDatabase(new ZKDatabase(quorumPeer.getTxnFactory()));
    quorumPeer.setLearnerType(config.getPeerType());
    quorumPeer.setSyncEnabled(config.getSyncEnabled());
    quorumPeer.setQuorumListenOnAllIPs(config.getQuorumListenOnAllIPs());
    quorumPeer.start();
}
Also used : QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) File(java.io.File) ZKDatabase(org.apache.zookeeper.server.ZKDatabase) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog)

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