Search in sources :

Example 36 with FileTxnSnapLog

use of org.apache.zookeeper.server.persistence.FileTxnSnapLog in project fabric8 by jboss-fuse.

the class ZKServerFactoryBean method afterPropertiesSet.

public void afterPropertiesSet() throws Exception {
    if (purge) {
        deleteFilesInDir(getDataLogDir());
        deleteFilesInDir(getDataDir());
    }
    FileTxnSnapLog ftxn = new FileTxnSnapLog(getDataLogDir(), getDataDir());
    zooKeeperServer.setTxnLogFactory(ftxn);
    zooKeeperServer.setTickTime(getTickTime());
    zooKeeperServer.setMinSessionTimeout(getMinSessionTimeout());
    zooKeeperServer.setMaxSessionTimeout(getMaxSessionTimeout());
    connectionFactory = new NIOServerCnxnFactory();
    connectionFactory.configure(getClientPortAddress(), getMaxClientConnections());
    connectionFactory.startup(zooKeeperServer);
}
Also used : NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog)

Example 37 with FileTxnSnapLog

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

the class PurgeTxnLog method purge.

/**
 * Purges the snapshot and logs keeping the last num snapshots and the
 * corresponding logs. If logs are rolling or a new snapshot is created
 * during this process, these newest N snapshots or any data logs will be
 * excluded from current purging cycle.
 *
 * @param dataDir the dir that has the logs
 * @param snapDir the dir that has the snapshots
 * @param num the number of snapshots to keep
 * @throws IOException
 */
public static void purge(File dataDir, File snapDir, int num) throws IOException {
    if (num < 3) {
        throw new IllegalArgumentException(COUNT_ERR_MSG);
    }
    FileTxnSnapLog txnLog = new FileTxnSnapLog(dataDir, snapDir);
    List<File> snaps = txnLog.findNValidSnapshots(num);
    int numSnaps = snaps.size();
    if (numSnaps > 0) {
        purgeOlderSnapshots(txnLog, snaps.get(numSnaps - 1));
    }
}
Also used : File(java.io.File) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog)

Example 38 with FileTxnSnapLog

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

the class QuorumPeerMainTest method testDataDirAndDataLogDir.

@Test
public void testDataDirAndDataLogDir() throws Exception {
    File dataDir = createEmptyTestDir();
    File dataLogDir = createEmptyTestDir();
    // Arrange
    try {
        QuorumPeerConfig configMock = mock(QuorumPeerConfig.class);
        when(configMock.getDataDir()).thenReturn(dataDir);
        when(configMock.getDataLogDir()).thenReturn(dataLogDir);
        when(configMock.getMetricsProviderClassName()).thenReturn(NullMetricsProvider.class.getName());
        QuorumPeer qpMock = mock(QuorumPeer.class);
        doCallRealMethod().when(qpMock).setTxnFactory(any(FileTxnSnapLog.class));
        when(qpMock.getTxnFactory()).thenCallRealMethod();
        InjectableQuorumPeerMain qpMain = new InjectableQuorumPeerMain(qpMock);
        // Act
        qpMain.runFromConfig(configMock);
        // Assert
        FileTxnSnapLog txnFactory = qpMain.getQuorumPeer().getTxnFactory();
        assertEquals(Paths.get(dataLogDir.getAbsolutePath(), "version-2").toString(), txnFactory.getDataDir().getAbsolutePath());
        assertEquals(Paths.get(dataDir.getAbsolutePath(), "version-2").toString(), txnFactory.getSnapDir().getAbsolutePath());
    } finally {
        FileUtils.deleteDirectory(dataDir);
        FileUtils.deleteDirectory(dataLogDir);
    }
}
Also used : File(java.io.File) NullMetricsProvider(org.apache.zookeeper.metrics.impl.NullMetricsProvider) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) Test(org.junit.jupiter.api.Test)

Example 39 with FileTxnSnapLog

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

Example 40 with FileTxnSnapLog

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

the class LearnerTest method truncFailTest.

@Test
public void truncFailTest() throws Exception {
    final boolean[] exitProcCalled = { false };
    ServiceUtils.setSystemExitProcedure(new Consumer<Integer>() {

        @Override
        public void accept(Integer exitCode) {
            exitProcCalled[0] = true;
            assertThat("System.exit() was called with invalid exit code", exitCode, equalTo(ExitCode.QUORUM_PACKET_ERROR.getValue()));
        }
    });
    File tmpFile = File.createTempFile("test", ".dir", testData);
    tmpFile.delete();
    try {
        FileTxnSnapLog txnSnapLog = new FileTxnSnapLog(tmpFile, tmpFile);
        SimpleLearner sl = new SimpleLearner(txnSnapLog);
        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.TRUNC, 0, null, 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();
        assertThat("System.exit() should have been called", exitProcCalled[0], is(true));
    } finally {
        TestUtils.deleteFileRecursively(tmpFile);
    }
}
Also used : BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) ByteArrayInputStream(java.io.ByteArrayInputStream) EOFException(java.io.EOFException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) Socket(java.net.Socket) 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