zookeeper-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anmolnar <...@git.apache.org>
Subject [GitHub] zookeeper pull request #439: ZOOKEEPER-1621: Delete and skip txn log with in...
Date Mon, 08 Jan 2018 20:15:28 GMT
Github user anmolnar commented on a diff in the pull request:

    https://github.com/apache/zookeeper/pull/439#discussion_r160244035
  
    --- Diff: src/java/test/org/apache/zookeeper/test/LoadFromLogTest.java ---
    @@ -307,4 +315,104 @@ public void testReloadSnapshotWithMissingParent() throws Exception
{
     
             startServer();
         }
    +
    +    /**
    +     * Verify that FileTxnIterator doesn't throw an EOFException when the
    +     * transaction log header is incomplete.
    +     */
    +    @Test
    +    public void testIncompleteHeader() throws Exception {
    +        ClientBase.setupTestEnv();
    +        File dataDir = ClientBase.createTmpDir();
    +        loadDatabase(dataDir, NUM_MESSAGES);
    +
    +        File logDir = new File(dataDir, FileTxnSnapLog.version +
    +                                        FileTxnSnapLog.VERSION);
    +        FileTxnLog.FileTxnIterator fileItr = new FileTxnLog.FileTxnIterator(logDir, 0);
    +        List<File> logFiles = fileItr.getStoredFiles();
    +        int numTransactions = 0;
    +        while (fileItr.next()) {
    +            numTransactions++;
    +        }
    +        Assert.assertTrue("Verify the number of log files",
    +                          logFiles.size() > 0);
    +        Assert.assertTrue("Verify the number of transactions",
    +                          numTransactions >= NUM_MESSAGES);
    +
    +        // Truncate the last log file.
    +        File lastLogFile = logFiles.get(logFiles.size() - 1);
    +        FileChannel channel = new FileOutputStream(lastLogFile).getChannel();
    +        channel.truncate(0);
    +        channel.close();
    +
    +        // This shouldn't thow Exception.
    +        fileItr = new FileTxnLog.FileTxnIterator(logDir, 0);
    +        logFiles = fileItr.getStoredFiles();
    +        numTransactions = 0;
    +        while (fileItr.next()) {
    +        }
    +
    +        // Verify that the truncated log file does not exist anymore.
    +        Assert.assertFalse("Verify truncated log file has been deleted",
    +                           lastLogFile.exists());
    +    }
    +
    +    /**
    +     * Verifies that FileTxnIterator throws CorruptedStreamException if the
    +     * magic number is corrupted.
    +     */
    +    @Test(expected = StreamCorruptedException.class)
    +    public void testCorruptMagicNumber() throws Exception {
    +        ClientBase.setupTestEnv();
    +        File dataDir = ClientBase.createTmpDir();
    +        loadDatabase(dataDir, NUM_MESSAGES);
    +
    +        File logDir = new File(dataDir, FileTxnSnapLog.version +
    +                                        FileTxnSnapLog.VERSION);
    +        FileTxnLog.FileTxnIterator fileItr = new FileTxnLog.FileTxnIterator(logDir, 0);
    +        List<File> logFiles = fileItr.getStoredFiles();
    +        Assert.assertTrue("Verify the number of log files",
    +                          logFiles.size() > 0);
    +
    +        // Corrupt the magic number.
    +        File lastLogFile = logFiles.get(logFiles.size() - 1);
    +        RandomAccessFile file = new RandomAccessFile(lastLogFile, "rw");
    +        file.seek(0);
    +        file.writeByte(123);
    +        file.close();
    +
    +        // This should throw CorruptedStreamException.
    +        while (fileItr.next()) {
    +        }
    +    }
    +
    +    /**
    +     * Starts a standalone server and create znodes.
    +     */
    +    public void loadDatabase(File dataDir, int numEntries) throws Exception {
    +        final String hostPort = HOST + PortAssignment.unique();
    +        // setup a single server cluster
    +        ZooKeeperServer zks = new ZooKeeperServer(dataDir, dataDir, 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 = ClientBase.createZKClient(hostPort);
    +
    +        // Generate some transactions that will get logged.
    +        try {
    +            for (int i = 0; i < numEntries; i++) {
    +                zk.create("/load-database-" + i, new byte[0],
    +                          Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    +            }
    +        } finally {
    +            zk.close();
    +        }
    +        f.shutdown();
    --- End diff --
    
    Starting the server is already implemented in base class' setUp() method and shutdown
is already in tearDown(). It's safer to let junit's infrastructure to deal with startup /
shutdown methods which makes sure the server(s) gets shutdown even if some error occurs.


---

Mime
View raw message