當前位置: 首頁>>代碼示例>>Java>>正文


Java FileTxnSnapLog.close方法代碼示例

本文整理匯總了Java中org.apache.zookeeper.server.persistence.FileTxnSnapLog.close方法的典型用法代碼示例。如果您正苦於以下問題:Java FileTxnSnapLog.close方法的具體用法?Java FileTxnSnapLog.close怎麽用?Java FileTxnSnapLog.close使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.apache.zookeeper.server.persistence.FileTxnSnapLog的用法示例。


在下文中一共展示了FileTxnSnapLog.close方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: internalTestSnapFilesEqualsToRetain

import org.apache.zookeeper.server.persistence.FileTxnSnapLog; //導入方法依賴的package包/類
public void internalTestSnapFilesEqualsToRetain(boolean testWithPrecedingLogFile) throws Exception {
    int nRecentCount = 3;
    AtomicInteger offset = new AtomicInteger(0);
    tmpDir = ClientBase.createTmpDir();
    File version2 = new File(tmpDir.toString(), "version-2");
    Assert.assertTrue("Failed to create version_2 dir:" + version2.toString(),
            version2.mkdir());
    List<File> snaps = new ArrayList<File>();
    List<File> logs = new ArrayList<File>();
    createDataDirFiles(offset, nRecentCount, testWithPrecedingLogFile, version2, snaps, logs);

    FileTxnSnapLog txnLog = new FileTxnSnapLog(tmpDir, tmpDir);
    PurgeTxnLog.purgeOlderSnapshots(txnLog, snaps.get(snaps.size() - 1));
    txnLog.close();
    verifyFilesAfterPurge(snaps, true);
    verifyFilesAfterPurge(logs, true);
}
 
開發者ID:l294265421,項目名稱:ZooKeeper,代碼行數:18,代碼來源:PurgeTxnTest.java

示例2: testSnapFilesEqualsToRetain

import org.apache.zookeeper.server.persistence.FileTxnSnapLog; //導入方法依賴的package包/類
/**
 * Tests purge where the data directory contains snap files equals to the
 * number of files to be retained
 */
@Test
public void testSnapFilesEqualsToRetain() throws Exception {
    int nRecentCount = 3;
    AtomicInteger offset = new AtomicInteger(0);
    tmpDir = ClientBase.createTmpDir();
    File version2 = new File(tmpDir.toString(), "version-2");
    Assert.assertTrue("Failed to create version_2 dir:" + version2.toString(),
            version2.mkdir());
    List<File> snaps = new ArrayList<File>();
    List<File> logs = new ArrayList<File>();
    createDataDirFiles(offset, nRecentCount, version2, snaps, logs);

    FileTxnSnapLog txnLog = new FileTxnSnapLog(tmpDir, tmpDir);
    PurgeTxnLog.retainNRecentSnapshots(txnLog, snaps);
    txnLog.close();
    verifyFilesAfterPurge(snaps, true);
    verifyFilesAfterPurge(logs, true);
}
 
開發者ID:maoling,項目名稱:fuck_zookeeper,代碼行數:23,代碼來源:PurgeTxnTest.java

示例3: testSnapFilesLessThanToRetain

import org.apache.zookeeper.server.persistence.FileTxnSnapLog; //導入方法依賴的package包/類
/**
 * 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);
    tmpDir = ClientBase.createTmpDir();
    File version2 = new File(tmpDir.toString(), "version-2");
    Assert.assertTrue("Failed to create version_2 dir:" + version2.toString(),
            version2.mkdir());
    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, version2, snapsToPurge,
            logsToPurge);
    createDataDirFiles(offset, nRecentCount, version2, snaps, logs);

    FileTxnSnapLog txnLog = new FileTxnSnapLog(tmpDir, tmpDir);
    PurgeTxnLog.retainNRecentSnapshots(txnLog, snaps);
    txnLog.close();
    verifyFilesAfterPurge(snapsToPurge, false);
    verifyFilesAfterPurge(logsToPurge, false);
    verifyFilesAfterPurge(snaps, true);
    verifyFilesAfterPurge(logs, true);
}
 
開發者ID:maoling,項目名稱:fuck_zookeeper,代碼行數:30,代碼來源:PurgeTxnTest.java

示例4: testPurge

import org.apache.zookeeper.server.persistence.FileTxnSnapLog; //導入方法依賴的package包/類
/**
 * test the purge
 * @throws Exception an exception might be thrown here
 */
@Test
public void testPurge() throws Exception {
    tmpDir = ClientBase.createTmpDir();
    ClientBase.setupTestEnv();
    ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
    SyncRequestProcessor.setSnapCount(100);
    final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]);
    ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1);
    f.startup(zks);
    Assert.assertTrue("waiting for server being up ",
            ClientBase.waitForServerUp(HOSTPORT,CONNECTION_TIMEOUT));
    ZooKeeper zk = new ZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, this);
    try {
        for (int i = 0; i< 2000; i++) {
            zk.create("/invalidsnap-" + i, new byte[0], Ids.OPEN_ACL_UNSAFE,
                    CreateMode.PERSISTENT);
        }
    } finally {
        zk.close();
    }
    f.shutdown();
    zks.getTxnLogFactory().close();
    Assert.assertTrue("waiting for server to shutdown",
            ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT));
    // now corrupt the snapshot
    PurgeTxnLog.purge(tmpDir, tmpDir, 3);
    FileTxnSnapLog snaplog = new FileTxnSnapLog(tmpDir, tmpDir);
    List<File> listLogs = snaplog.findNRecentSnapshots(4);
    int numSnaps = 0;
    for (File ff: listLogs) {
        if (ff.getName().startsWith("snapshot")) {
            numSnaps++;
        }
    }
    Assert.assertTrue("exactly 3 snapshots ", (numSnaps == 3));
    snaplog.close();
    zks.shutdown();
}
 
開發者ID:maoling,項目名稱:fuck_zookeeper,代碼行數:43,代碼來源:PurgeTxnTest.java

示例5: testSnapFilesGreaterThanToRetain

import org.apache.zookeeper.server.persistence.FileTxnSnapLog; //導入方法依賴的package包/類
/**
 * 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);
    tmpDir = ClientBase.createTmpDir();
    File version2 = new File(tmpDir.toString(), "version-2");
    Assert.assertTrue("Failed to create version_2 dir:" + version2.toString(),
            version2.mkdir());
    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, version2, snapsToPurge,
            logsToPurge);
    createDataDirFiles(offset, nRecentCount, version2, snaps, logs);
    createDataDirFiles(offset, fileAboveRecentCount, version2,
            snapsAboveRecentFiles, logsAboveRecentFiles);

    FileTxnSnapLog txnLog = new FileTxnSnapLog(tmpDir, tmpDir);
    PurgeTxnLog.retainNRecentSnapshots(txnLog, snaps);
    txnLog.close();
    verifyFilesAfterPurge(snapsToPurge, false);
    verifyFilesAfterPurge(logsToPurge, false);
    verifyFilesAfterPurge(snaps, true);
    verifyFilesAfterPurge(logs, true);
    verifyFilesAfterPurge(snapsAboveRecentFiles, true);
    verifyFilesAfterPurge(logsAboveRecentFiles, true);
}
 
開發者ID:maoling,項目名稱:fuck_zookeeper,代碼行數:38,代碼來源:PurgeTxnTest.java

示例6: testTxnLogElapsedSyncTime

import org.apache.zookeeper.server.persistence.FileTxnSnapLog; //導入方法依賴的package包/類
@Test
public void testTxnLogElapsedSyncTime() throws IOException {

    File tmpDir = ClientBase.createEmptyTestDir();
    FileTxnSnapLog fileTxnSnapLog = new FileTxnSnapLog(new File(tmpDir, "data"),
            new File(tmpDir, "data_txnlog"));

    ZooKeeperServer zks = new ZooKeeperServer();
    zks.setTxnLogFactory(fileTxnSnapLog);

    ZooKeeperServerBean serverBean = new ZooKeeperServerBean(zks);
    long elapsedTime = serverBean.getTxnLogElapsedSyncTime();
    assertEquals(-1, elapsedTime);

    TxnHeader hdr = new TxnHeader(1, 1, 1, 1, ZooDefs.OpCode.setData);
    Record txn = new SetDataTxn("/foo", new byte[0], 1);
    Request req = new Request(0, 0, 0, hdr, txn, 0);

    try {

        zks.getTxnLogFactory().append(req);
        zks.getTxnLogFactory().commit();
        elapsedTime = serverBean.getTxnLogElapsedSyncTime();

        assertNotEquals(-1, elapsedTime);

        assertEquals(elapsedTime, serverBean.getTxnLogElapsedSyncTime());

    } finally {
        fileTxnSnapLog.close();
    }
}
 
開發者ID:didichuxing2,項目名稱:https-github.com-apache-zookeeper,代碼行數:33,代碼來源:ZooKeeperServerBeanTest.java

示例7: testSnapFilesLessThanToRetain

import org.apache.zookeeper.server.persistence.FileTxnSnapLog; //導入方法依賴的package包/類
/**
 * 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);
    tmpDir = ClientBase.createTmpDir();
    File version2 = new File(tmpDir.toString(), "version-2");
    Assert.assertTrue("Failed to create version_2 dir:" + version2.toString(),
            version2.mkdir());
    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);
    logs.add(logsToPurge.remove(0)); // log that precedes first retained snapshot is also retained

    /**
     * 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);
}
 
開發者ID:l294265421,項目名稱:ZooKeeper,代碼行數:37,代碼來源:PurgeTxnTest.java

示例8: testSnapFilesGreaterThanToRetain

import org.apache.zookeeper.server.persistence.FileTxnSnapLog; //導入方法依賴的package包/類
/**
 * 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);
    tmpDir = ClientBase.createTmpDir();
    File version2 = new File(tmpDir.toString(), "version-2");
    Assert.assertTrue("Failed to create version_2 dir:" + version2.toString(),
            version2.mkdir());
    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);
    logs.add(logsToPurge.remove(0)); // log that precedes first retained snapshot is also retained
    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);
}
 
開發者ID:didichuxing2,項目名稱:https-github.com-apache-zookeeper,代碼行數:45,代碼來源:PurgeTxnTest.java

示例9: testPopulatedLeaderConversation

import org.apache.zookeeper.server.persistence.FileTxnSnapLog; //導入方法依賴的package包/類
public void testPopulatedLeaderConversation(PopulatedLeaderConversation conversation, int ops) throws Exception {
    Socket pair[] = getSocketPair();
    Socket leaderSocket = pair[0];
    Socket followerSocket = pair[1];
    File tmpDir = File.createTempFile("test", "dir", testData);
    tmpDir.delete();
    tmpDir.mkdir();
    LeadThread leadThread = null;
    Leader leader = null;
    try {              
        // Setup a database with two znodes
        FileTxnSnapLog snapLog = new FileTxnSnapLog(tmpDir, tmpDir);
        ZKDatabase zkDb = new ZKDatabase(snapLog);
        
        Assert.assertTrue(ops >= 1);
        long zxid = ZxidUtils.makeZxid(1, 0);            
        for(int i = 1; i <= ops; i++){
            zxid = ZxidUtils.makeZxid(1, i);
            String path = "/foo-"+ i;
            zkDb.processTxn(new TxnHeader(13,1000+i,zxid,30+i,ZooDefs.OpCode.create), 
                                            new CreateTxn(path, "fpjwasalsohere".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, false, 1));
            Stat stat = new Stat();
            Assert.assertEquals("fpjwasalsohere", new String(zkDb.getData(path, stat, null)));
        }                
        Assert.assertTrue(zxid > ZxidUtils.makeZxid(1, 0));
        
        // Generate snapshot and close files.
        snapLog.save(zkDb.getDataTree(), zkDb.getSessionWithTimeOuts());
        snapLog.close();
        
        QuorumPeer peer = createQuorumPeer(tmpDir);
                    
        leader = createLeader(tmpDir, peer);
        peer.leader = leader;
        
        // Set the last accepted epoch and current epochs to be 1
        peer.setAcceptedEpoch(1);
        peer.setCurrentEpoch(1);

        
        leadThread = new LeadThread(leader);
        leadThread.start();

        while(leader.cnxAcceptor == null || !leader.cnxAcceptor.isAlive()) {
            Thread.sleep(20);
        }
        
        LearnerHandler lh = new LearnerHandler(leaderSocket, leader);
        lh.start();
        leaderSocket.setSoTimeout(4000);

        InputArchive ia = BinaryInputArchive.getArchive(followerSocket
                .getInputStream());
        OutputArchive oa = BinaryOutputArchive.getArchive(followerSocket
                .getOutputStream());

        conversation.converseWithLeader(ia, oa, leader, zxid);
    } finally {
        if (leader != null) {
            leader.shutdown("end of test");
        }
        if (leadThread != null) {
            leadThread.interrupt();
            leadThread.join();
        }
        recursiveDelete(tmpDir);
    }
}
 
開發者ID:maoling,項目名稱:fuck_zookeeper,代碼行數:69,代碼來源:Zab1_0Test.java

示例10: testDirtySnapshot

import org.apache.zookeeper.server.persistence.FileTxnSnapLog; //導入方法依賴的package包/類
/**
 * verify that a peer with dirty snapshot joining an established cluster
 * does not go into an inconsistent state.
 *
 * {@link https://issues.apache.org/jira/browse/ZOOKEEPER-1558}
 */
@Test
public void testDirtySnapshot()
throws IOException,
    InterruptedException,
    KeeperException,
    NoSuchFieldException,
    IllegalAccessException {
    Socket pair[] = getSocketPair();
    Socket leaderSocket = pair[0];
    Socket followerSocket = pair[1];
    File tmpDir = File.createTempFile("test", "dir");
    tmpDir.delete();
    tmpDir.mkdir();
    LeadThread leadThread = null;
    Leader leader = null;
    try {
        // Setup a database with two znodes
        FileTxnSnapLog snapLog = new FileTxnSnapLog(tmpDir, tmpDir);
        ZKDatabase zkDb = new ZKDatabase(snapLog);

        long zxid = ZxidUtils.makeZxid(0, 1);
        String path = "/foo";
        zkDb.processTxn(new TxnHeader(13,1000,zxid,30,ZooDefs.OpCode.create),
                                        new CreateTxn(path, "fpjwasalsohere".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, false, 1));
        Stat stat = new Stat();
        Assert.assertEquals("fpjwasalsohere", new String(zkDb.getData(path, stat, null)));

        // Close files
        snapLog.close();

        QuorumPeer peer = createQuorumPeer(tmpDir);

        leader = createLeader(tmpDir, peer);
        peer.leader = leader;

        // Set the last accepted epoch and current epochs to be 1
        peer.setAcceptedEpoch(0);
        peer.setCurrentEpoch(0);

        leadThread = new LeadThread(leader);
        leadThread.start();

        while(leader.cnxAcceptor == null || !leader.cnxAcceptor.isAlive()) {
            Thread.sleep(20);
        }

        leader.shutdown("Shutting down the leader");

        // Check if there is a valid snapshot (we better not have it)
        File snapDir = new File (tmpDir, FileTxnSnapLog.version + FileTxnSnapLog.VERSION);
        List<File> files = Util.sortDataDir(snapDir.listFiles(),"snapshot", false);

        for (File f : files) {
            try {
                Assert.assertFalse("Found a valid snapshot", Util.isValidSnapshot(f));
            } catch (IOException e) {
                LOG.info("invalid snapshot " + f, e);
            }
        }

    } finally {
        if (leader != null) {
            leader.shutdown("end of test");
        }
        if (leadThread != null) {
            leadThread.interrupt();
            leadThread.join();
        }
        recursiveDelete(tmpDir);
    }
}
 
開發者ID:maoling,項目名稱:fuck_zookeeper,代碼行數:78,代碼來源:Zab1_0Test.java

示例11: testNonRecoverableError

import org.apache.zookeeper.server.persistence.FileTxnSnapLog; //導入方法依賴的package包/類
/**
 * 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) on ZooKeeper service will be available
 */
@Test(timeout = 30000)
public void testNonRecoverableError() throws Exception {
    ClientBase.setupTestEnv();

    final int CLIENT_PORT = PortAssignment.unique();

    MainThread main = new MainThread(CLIENT_PORT, true);
    main.start();

    Assert.assertTrue("waiting for server being up",
            ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT,
                    CONNECTION_TIMEOUT));


    ZooKeeper zk = new ZooKeeper("127.0.0.1:" + CLIENT_PORT,
            ClientBase.CONNECTION_TIMEOUT, this);

    zk.create("/foo1", "foobar".getBytes(), Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT);
    Assert.assertEquals(new String(zk.getData("/foo1", null, null)), "foobar");

    // inject problem in server
    ZooKeeperServer zooKeeperServer = main.getCnxnFactory()
            .getZooKeeperServer();
    FileTxnSnapLog snapLog = zooKeeperServer.getTxnLogFactory();
    FileTxnSnapLog fileTxnSnapLogWithError = new FileTxnSnapLog(
            snapLog.getDataDir(), snapLog.getSnapDir()) {
        @Override
        public void commit() throws IOException {
            throw new IOException("Input/output error");
        }
    };
    ZKDatabase newDB = new ZKDatabase(fileTxnSnapLogWithError);
    zooKeeperServer.setZKDatabase(newDB);

    try {
        // do create operation, so that injected IOException is thrown
        zk.create("/foo2", "foobar".getBytes(), Ids.OPEN_ACL_UNSAFE,
                CreateMode.PERSISTENT);
        fail("IOException is expected as error is injected in transaction log commit funtionality");
    } catch (Exception e) {
        // do nothing
    }
    zk.close();
    Assert.assertTrue("waiting for server down",
            ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT,
                    ClientBase.CONNECTION_TIMEOUT));
    fileTxnSnapLogWithError.close();
    main.shutdown();
    main.deleteDirs();
}
 
開發者ID:maoling,項目名稱:fuck_zookeeper,代碼行數:57,代碼來源:ZooKeeperServerMainTest.java

示例12: testPopulatedLeaderConversation

import org.apache.zookeeper.server.persistence.FileTxnSnapLog; //導入方法依賴的package包/類
public void testPopulatedLeaderConversation(PopulatedLeaderConversation conversation, int ops) throws Exception {
    Socket pair[] = getSocketPair();
    Socket leaderSocket = pair[0];
    Socket followerSocket = pair[1];
    File tmpDir = File.createTempFile("test", "dir", testData);
    tmpDir.delete();
    tmpDir.mkdir();
    LeadThread leadThread = null;
    Leader leader = null;
    try {              
        // Setup a database with two znodes
        FileTxnSnapLog snapLog = new FileTxnSnapLog(tmpDir, tmpDir);
        ZKDatabase zkDb = new ZKDatabase(snapLog);
        
        Assert.assertTrue(ops >= 1);
        long zxid = ZxidUtils.makeZxid(1, 0);            
        for(int i = 1; i <= ops; i++){
            zxid = ZxidUtils.makeZxid(1, i);
            String path = "/foo-"+ i;
            zkDb.processTxn(new TxnHeader(13,1000+i,zxid,30+i,ZooDefs.OpCode.create), 
                                            new CreateTxn(path, "fpjwasalsohere".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, false, 1));
            Stat stat = new Stat();
            Assert.assertEquals("fpjwasalsohere", new String(zkDb.getData(path, stat, null)));
        }                
        Assert.assertTrue(zxid > ZxidUtils.makeZxid(1, 0));
        
        // Generate snapshot and close files.
        snapLog.save(zkDb.getDataTree(), zkDb.getSessionWithTimeOuts(), false);
        snapLog.close();
        
        QuorumPeer peer = createQuorumPeer(tmpDir);
                    
        leader = createLeader(tmpDir, peer);
        peer.leader = leader;
        
        // Set the last accepted epoch and current epochs to be 1
        peer.setAcceptedEpoch(1);
        peer.setCurrentEpoch(1);

        
        leadThread = new LeadThread(leader);
        leadThread.start();

        while(leader.cnxAcceptor == null || !leader.cnxAcceptor.isAlive()) {
            Thread.sleep(20);
        }
        
        LearnerHandler lh = new LearnerHandler(leaderSocket, leader);
        lh.start();
        leaderSocket.setSoTimeout(4000);

        InputArchive ia = BinaryInputArchive.getArchive(followerSocket
                .getInputStream());
        OutputArchive oa = BinaryOutputArchive.getArchive(followerSocket
                .getOutputStream());

        conversation.converseWithLeader(ia, oa, leader, zxid);
    } finally {
        if (leader != null) {
            leader.shutdown("end of test");
        }
        if (leadThread != null) {
            leadThread.interrupt();
            leadThread.join();
        }
        TestUtils.deleteFileRecursively(tmpDir);
    }
}
 
開發者ID:didichuxing2,項目名稱:https-github.com-apache-zookeeper,代碼行數:69,代碼來源:Zab1_0Test.java

示例13: testNonRecoverableError

import org.apache.zookeeper.server.persistence.FileTxnSnapLog; //導入方法依賴的package包/類
/**
 * 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 = 30000)
public void testNonRecoverableError() throws Exception {
    ClientBase.setupTestEnv();

    final int CLIENT_PORT = PortAssignment.unique();

    MainThread main = new MainThread(CLIENT_PORT, true, null);
    main.start();

    Assert.assertTrue("waiting for server being up",
            ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT,
                    CONNECTION_TIMEOUT));


    ZooKeeper zk = new ZooKeeper("127.0.0.1:" + CLIENT_PORT,
            ClientBase.CONNECTION_TIMEOUT, this);

    zk.create("/foo1", "foobar".getBytes(), Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT);
    Assert.assertEquals(new String(zk.getData("/foo1", null, null)), "foobar");

    // inject problem in server
    ZooKeeperServer zooKeeperServer = main.getCnxnFactory()
            .getZooKeeperServer();
    FileTxnSnapLog snapLog = zooKeeperServer.getTxnLogFactory();
    FileTxnSnapLog fileTxnSnapLogWithError = new FileTxnSnapLog(
            snapLog.getDataDir(), snapLog.getSnapDir()) {
        @Override
        public void commit() throws IOException {
            throw new IOException("Input/output error");
        }
    };
    ZKDatabase newDB = new ZKDatabase(fileTxnSnapLogWithError);
    zooKeeperServer.setZKDatabase(newDB);

    try {
        // do create operation, so that injected IOException is thrown
        zk.create("/foo2", "foobar".getBytes(), Ids.OPEN_ACL_UNSAFE,
                CreateMode.PERSISTENT);
        fail("IOException is expected as error is injected in transaction log commit funtionality");
    } catch (Exception e) {
        // do nothing
    }
    zk.close();
    Assert.assertTrue("waiting for server down",
            ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT,
                    ClientBase.CONNECTION_TIMEOUT));
    fileTxnSnapLogWithError.close();
    main.shutdown();
    main.deleteDirs();
}
 
開發者ID:didichuxing2,項目名稱:https-github.com-apache-zookeeper,代碼行數:57,代碼來源:ZooKeeperServerMainTest.java

示例14: testFindNRecentSnapshots

import org.apache.zookeeper.server.persistence.FileTxnSnapLog; //導入方法依賴的package包/類
/**
 * Tests finding n recent snapshots from set of snapshots and data logs
 */
@Test
public void testFindNRecentSnapshots() throws Exception {
    int nRecentSnap = 4; // n recent snap shots
    int nRecentCount = 30;
    int offset = 0;

    tmpDir = ClientBase.createTmpDir();
    File version2 = new File(tmpDir.toString(), "version-2");
    Assert.assertTrue("Failed to create version_2 dir:" + version2.toString(),
            version2.mkdir());

    // Test that with no snaps, findNRecentSnapshots returns empty list
    FileTxnSnapLog txnLog = new FileTxnSnapLog(tmpDir, tmpDir);
    List<File> foundSnaps = txnLog.findNRecentSnapshots(1);
    assertEquals(0, foundSnaps.size());

    List<File> expectedNRecentSnapFiles = new ArrayList<File>();
    int counter = offset + (2 * nRecentCount);
    for (int i = 0; i < nRecentCount; i++) {
        // simulate log file
        File logFile = new File(version2 + "/log." + Long.toHexString(--counter));
        Assert.assertTrue("Failed to create log File:" + logFile.toString(),
                logFile.createNewFile());
        // simulate snapshot file
        File snapFile = new File(version2 + "/snapshot."
                + Long.toHexString(--counter));
        Assert.assertTrue("Failed to create snap File:" + snapFile.toString(),
                snapFile.createNewFile());
        // add the n recent snap files for assertion
        if(i < nRecentSnap){
            expectedNRecentSnapFiles.add(snapFile);
        }
    }

    // Test that when we ask for recent snaps we get the number we asked for and
    // the files we expected
    List<File> nRecentSnapFiles = txnLog.findNRecentSnapshots(nRecentSnap);
    Assert.assertEquals("exactly 4 snapshots ", 4,
            nRecentSnapFiles.size());
    expectedNRecentSnapFiles.removeAll(nRecentSnapFiles);
    Assert.assertEquals("Didn't get the recent snap files", 0,
            expectedNRecentSnapFiles.size());

    // Test that when asking for more snaps than we created, we still only get snaps
    // not logs or anything else (per ZOOKEEPER-2420)
    nRecentSnapFiles = txnLog.findNRecentSnapshots(nRecentCount + 5);
    assertEquals(nRecentCount, nRecentSnapFiles.size());
    for (File f: nRecentSnapFiles) {
        Assert.assertTrue("findNRecentSnapshots() returned a non-snapshot: " + f.getPath(),
               (Util.getZxidFromName(f.getName(), "snapshot") != -1));
    }

    txnLog.close();
}
 
開發者ID:didichuxing2,項目名稱:https-github.com-apache-zookeeper,代碼行數:58,代碼來源:PurgeTxnTest.java


注:本文中的org.apache.zookeeper.server.persistence.FileTxnSnapLog.close方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。