Search in sources :

Example 46 with FileTxnSnapLog

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

the class PurgeTxnTest method testSnapFilesGreaterThanToRetain.

/**
 * Tests purge where the data directory contains old snapshots and data
 * logs, newest snapshots and data logs, (newest + n) snapshots and data
 * logs
 */
@Test
public void testSnapFilesGreaterThanToRetain() throws Exception {
    int nRecentCount = 4;
    int fileAboveRecentCount = 4;
    int fileToPurgeCount = 2;
    AtomicInteger offset = new AtomicInteger(0);
    File version2 = new File(tmpDir.toString(), "version-2");
    assertTrue(version2.mkdir(), "Failed to create version_2 dir:" + version2.toString());
    List<File> snapsToPurge = new ArrayList<File>();
    List<File> logsToPurge = new ArrayList<File>();
    List<File> snaps = new ArrayList<File>();
    List<File> logs = new ArrayList<File>();
    List<File> snapsAboveRecentFiles = new ArrayList<File>();
    List<File> logsAboveRecentFiles = new ArrayList<File>();
    createDataDirFiles(offset, fileToPurgeCount, false, version2, snapsToPurge, logsToPurge);
    createDataDirFiles(offset, nRecentCount, false, version2, snaps, logs);
    // log that precedes first retained snapshot is also retained
    logs.add(logsToPurge.remove(0));
    createDataDirFiles(offset, fileAboveRecentCount, false, version2, snapsAboveRecentFiles, logsAboveRecentFiles);
    /**
     * The newest log file preceding the oldest retained snapshot is not removed as it may
     * contain transactions newer than the oldest snapshot.
     */
    logsToPurge.remove(0);
    FileTxnSnapLog txnLog = new FileTxnSnapLog(tmpDir, tmpDir);
    PurgeTxnLog.purgeOlderSnapshots(txnLog, snaps.get(snaps.size() - 1));
    txnLog.close();
    verifyFilesAfterPurge(snapsToPurge, false);
    verifyFilesAfterPurge(logsToPurge, false);
    verifyFilesAfterPurge(snaps, true);
    verifyFilesAfterPurge(logs, true);
    verifyFilesAfterPurge(snapsAboveRecentFiles, true);
    verifyFilesAfterPurge(logsAboveRecentFiles, true);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) File(java.io.File) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) Test(org.junit.jupiter.api.Test)

Example 47 with FileTxnSnapLog

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

the class PurgeTxnTest method testSnapFilesLessThanToRetain.

/**
 * Tests purge where the data directory contains old snapshots and data
 * logs, newest snapshots and data logs
 */
@Test
public void testSnapFilesLessThanToRetain() throws Exception {
    int nRecentCount = 4;
    int fileToPurgeCount = 2;
    AtomicInteger offset = new AtomicInteger(0);
    File version2 = new File(tmpDir.toString(), "version-2");
    assertTrue(version2.mkdir(), "Failed to create version_2 dir:" + version2.toString());
    List<File> snapsToPurge = new ArrayList<File>();
    List<File> logsToPurge = new ArrayList<File>();
    List<File> snaps = new ArrayList<File>();
    List<File> logs = new ArrayList<File>();
    createDataDirFiles(offset, fileToPurgeCount, false, version2, snapsToPurge, logsToPurge);
    createDataDirFiles(offset, nRecentCount, false, version2, snaps, logs);
    // log that precedes first retained snapshot is also retained
    logs.add(logsToPurge.remove(0));
    /**
     * The newest log file preceding the oldest retained snapshot is not removed as it may
     * contain transactions newer than the oldest snapshot.
     */
    logsToPurge.remove(0);
    FileTxnSnapLog txnLog = new FileTxnSnapLog(tmpDir, tmpDir);
    PurgeTxnLog.purgeOlderSnapshots(txnLog, snaps.get(snaps.size() - 1));
    txnLog.close();
    verifyFilesAfterPurge(snapsToPurge, false);
    verifyFilesAfterPurge(logsToPurge, false);
    verifyFilesAfterPurge(snaps, true);
    verifyFilesAfterPurge(logs, true);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) File(java.io.File) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) Test(org.junit.jupiter.api.Test)

Example 48 with FileTxnSnapLog

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

the class PrepRequestProcessorTest method testReconfigWithAnotherOutstandingChange.

@Test
public void testReconfigWithAnotherOutstandingChange() throws Exception {
    QuorumPeerConfig.setReconfigEnabled(true);
    QuorumPeerConfig.setStandaloneEnabled(false);
    QuorumPeer qp = new QuorumPeer();
    QuorumVerifier quorumVerifierMock = mock(QuorumVerifier.class);
    when(quorumVerifierMock.getAllMembers()).thenReturn(LeaderBeanTest.getMockedPeerViews(qp.getId()));
    qp.setQuorumVerifier(quorumVerifierMock, false);
    FileTxnSnapLog snapLog = new FileTxnSnapLog(tmpDir, tmpDir);
    LeaderZooKeeperServer lzks = new LeaderZooKeeperServer(snapLog, qp, new ZKDatabase(snapLog));
    qp.leader = new Leader(qp, lzks);
    lzks.sessionTracker = new MySessionTracker();
    ZooKeeperServer.setDigestEnabled(true);
    processor = new PrepRequestProcessor(lzks, new MyRequestProcessor());
    Record record = new CreateRequest("/foo", "data".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT.toFlag());
    pLatch = new CountDownLatch(1);
    processor.pRequest(createRequest(record, OpCode.create, false));
    assertTrue(pLatch.await(5, TimeUnit.SECONDS), "request hasn't been processed in chain");
    String newMember = "server.0=localhost:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ":participant";
    record = new ReconfigRequest(null, null, newMember, 0);
    pLatch = new CountDownLatch(1);
    processor.pRequest(createRequest(record, OpCode.reconfig, true));
    assertTrue(pLatch.await(5, TimeUnit.SECONDS), "request hasn't been processed in chain");
    // Verifies that there was no error.
    assertEquals(outcome.getHdr().getType(), OpCode.reconfig);
}
Also used : Leader(org.apache.zookeeper.server.quorum.Leader) CreateRequest(org.apache.zookeeper.proto.CreateRequest) CountDownLatch(java.util.concurrent.CountDownLatch) QuorumVerifier(org.apache.zookeeper.server.quorum.flexible.QuorumVerifier) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) ReconfigRequest(org.apache.zookeeper.proto.ReconfigRequest) QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) Record(org.apache.jute.Record) ChangeRecord(org.apache.zookeeper.server.ZooKeeperServer.ChangeRecord) MultiOperationRecord(org.apache.zookeeper.MultiOperationRecord) LeaderZooKeeperServer(org.apache.zookeeper.server.quorum.LeaderZooKeeperServer) LeaderBeanTest(org.apache.zookeeper.server.quorum.LeaderBeanTest) Test(org.junit.jupiter.api.Test)

Example 49 with FileTxnSnapLog

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

the class TxnLogCountTest method testTxnLogCount.

/**
 * Test ZkDatabase's txnCount
 */
@Test
public void testTxnLogCount() throws IOException {
    File tmpDir = ClientBase.createTmpDir();
    FileTxnSnapLog snapLog = new FileTxnSnapLog(tmpDir, tmpDir);
    ZKDatabase zkDatabase = new ZKDatabase(snapLog);
    int txnRequestCnt = 10;
    int nonTxnRequestCnt = 10;
    for (int i = 0; i < txnRequestCnt && zkDatabase.append(mockTxnRequest()); i++) {
    }
    assertEquals(txnRequestCnt, zkDatabase.getTxnCount());
    for (int i = 0; i < nonTxnRequestCnt && !zkDatabase.append(mockNonTxnRequest()); i++) {
    }
    assertEquals(txnRequestCnt, zkDatabase.getTxnCount());
}
Also used : File(java.io.File) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) Test(org.junit.Test)

Example 50 with FileTxnSnapLog

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

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