Repository: zookeeper Updated Branches: refs/heads/master 75411ab34 -> d857e042f
ZOOKEEPER-2924: Refactor tests of LoadFromLogTest.java This patch is the trunk version of https://github.com/apache/zookeeper/pull/409 Author: Andor Molnár <[email protected]> Reviewers: [email protected] Closes #420 from anmolnar/ZOOKEEPER-2924-trunk Change-Id: I2260247e271eb86a6d37fe297e9fb5a7be3babc0 Project: http://git-wip-us.apache.org/repos/asf/zookeeper/repo Commit: http://git-wip-us.apache.org/repos/asf/zookeeper/commit/d857e042 Tree: http://git-wip-us.apache.org/repos/asf/zookeeper/tree/d857e042 Diff: http://git-wip-us.apache.org/repos/asf/zookeeper/diff/d857e042 Branch: refs/heads/master Commit: d857e042fe4d188a7ba750ea6947f93f39cb7ed1 Parents: 75411ab Author: Andor Molnár <[email protected]> Authored: Wed Dec 6 14:14:10 2017 -0800 Committer: Patrick Hunt <[email protected]> Committed: Wed Dec 6 14:14:10 2017 -0800 ---------------------------------------------------------------------- .../zookeeper/test/LoadFromLogNoServerTest.java | 176 ++++++++++ .../apache/zookeeper/test/LoadFromLogTest.java | 317 +++---------------- 2 files changed, 212 insertions(+), 281 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/zookeeper/blob/d857e042/src/java/test/org/apache/zookeeper/test/LoadFromLogNoServerTest.java ---------------------------------------------------------------------- diff --git a/src/java/test/org/apache/zookeeper/test/LoadFromLogNoServerTest.java b/src/java/test/org/apache/zookeeper/test/LoadFromLogNoServerTest.java new file mode 100644 index 0000000..4d56f60 --- /dev/null +++ b/src/java/test/org/apache/zookeeper/test/LoadFromLogNoServerTest.java @@ -0,0 +1,176 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.zookeeper.test; + +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.BinaryOutputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.common.Time; +import org.apache.zookeeper.server.DataNode; +import org.apache.zookeeper.server.DataTree; +import org.apache.zookeeper.server.persistence.FileHeader; +import org.apache.zookeeper.server.persistence.FileTxnLog; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.txn.CreateTxn; +import org.apache.zookeeper.txn.DeleteTxn; +import org.apache.zookeeper.txn.MultiTxn; +import org.apache.zookeeper.txn.Txn; +import org.apache.zookeeper.txn.TxnHeader; +import org.junit.Assert; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.List; + +public class LoadFromLogNoServerTest extends ZKTestCase { + protected static final Logger LOG = LoggerFactory.getLogger(LoadFromLogNoServerTest.class); + + /** + * For ZOOKEEPER-1046. Verify if cversion and pzxid if incremented + * after create/delete failure during restore. + */ + @Test + public void testTxnFailure() throws Exception { + long count = 1; + File tmpDir = ClientBase.createTmpDir(); + FileTxnSnapLog logFile = new FileTxnSnapLog(tmpDir, tmpDir); + DataTree dt = new DataTree(); + dt.createNode("/test", new byte[0], null, 0, -1, 1, 1); + for (count = 1; count <= 3; count++) { + dt.createNode("/test/" + count, new byte[0], null, 0, -1, count, + Time.currentElapsedTime()); + } + DataNode zk = dt.getNode("/test"); + + // Make create to fail, then verify cversion. + LOG.info("Attempting to create " + "/test/" + (count - 1)); + doOp(logFile, ZooDefs.OpCode.create, "/test/" + (count - 1), dt, zk, -1); + + LOG.info("Attempting to create " + "/test/" + (count - 1)); + doOp(logFile, ZooDefs.OpCode.create, "/test/" + (count - 1), dt, zk, + zk.stat.getCversion() + 1); + + LOG.info("Attempting to create " + "/test/" + (count - 1)); + doOp(logFile, ZooDefs.OpCode.multi, "/test/" + (count - 1), dt, zk, + zk.stat.getCversion() + 1); + + LOG.info("Attempting to create " + "/test/" + (count - 1)); + doOp(logFile, ZooDefs.OpCode.multi, "/test/" + (count - 1), dt, zk, + -1); + + // Make delete fo fail, then verify cversion. + // this doesn't happen anymore, we only set the cversion on create + // LOG.info("Attempting to delete " + "/test/" + (count + 1)); + // doOp(logFile, OpCode.delete, "/test/" + (count + 1), dt, zk); + } + + /* + * Does create/delete depending on the type and verifies + * if cversion before the operation is 1 less than cversion afer. + */ + private void doOp(FileTxnSnapLog logFile, int type, String path, + DataTree dt, DataNode parent, int cversion) throws Exception { + int lastSlash = path.lastIndexOf('/'); + String parentName = path.substring(0, lastSlash); + + int prevCversion = parent.stat.getCversion(); + long prevPzxid = parent.stat.getPzxid(); + List<String> child = dt.getChildren(parentName, null, null); + StringBuilder childStr = new StringBuilder(); + for (String s : child) { + childStr.append(s).append(" "); + } + LOG.info("Children: " + childStr + " for " + parentName); + LOG.info("(cverions, pzxid): " + prevCversion + ", " + prevPzxid); + + Record txn = null; + TxnHeader txnHeader = null; + if (type == ZooDefs.OpCode.delete) { + txn = new DeleteTxn(path); + txnHeader = new TxnHeader(0xabcd, 0x123, prevPzxid + 1, + Time.currentElapsedTime(), ZooDefs.OpCode.delete); + } else if (type == ZooDefs.OpCode.create) { + txnHeader = new TxnHeader(0xabcd, 0x123, prevPzxid + 1, + Time.currentElapsedTime(), ZooDefs.OpCode.create); + txn = new CreateTxn(path, new byte[0], null, false, cversion); + } + else if (type == ZooDefs.OpCode.multi) { + txnHeader = new TxnHeader(0xabcd, 0x123, prevPzxid + 1, + Time.currentElapsedTime(), ZooDefs.OpCode.create); + txn = new CreateTxn(path, new byte[0], null, false, cversion); + List<Txn> txnList = new ArrayList<Txn>(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos); + txn.serialize(boa, "request") ; + ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray()); + Txn txact = new Txn(ZooDefs.OpCode.create, bb.array()); + txnList.add(txact); + txn = new MultiTxn(txnList); + txnHeader = new TxnHeader(0xabcd, 0x123, prevPzxid + 1, + Time.currentElapsedTime(), ZooDefs.OpCode.multi); + } + logFile.processTransaction(txnHeader, dt, null, txn); + + int newCversion = parent.stat.getCversion(); + long newPzxid = parent.stat.getPzxid(); + child = dt.getChildren(parentName, null, null); + childStr = new StringBuilder(); + for (String s : child) { + childStr.append(s).append(" "); + } + LOG.info("Children: " + childStr + " for " + parentName); + LOG.info("(cverions, pzxid): " +newCversion + ", " + newPzxid); + Assert.assertTrue(type + " <cversion, pzxid> verification failed. Expected: <" + + (prevCversion + 1) + ", " + (prevPzxid + 1) + ">, found: <" + + newCversion + ", " + newPzxid + ">", + (newCversion == prevCversion + 1 && newPzxid == prevPzxid + 1)); + } + + /** + * Simulates ZOOKEEPER-1069 and verifies that flush() before padLogFile + * fixes it. + */ + @Test + public void testPad() throws Exception { + File tmpDir = ClientBase.createTmpDir(); + FileTxnLog txnLog = new FileTxnLog(tmpDir); + TxnHeader txnHeader = new TxnHeader(0xabcd, 0x123, 0x123, + Time.currentElapsedTime(), ZooDefs.OpCode.create); + Record txn = new CreateTxn("/Test", new byte[0], null, false, 1); + txnLog.append(txnHeader, txn); + FileInputStream in = new FileInputStream(tmpDir.getPath() + "/log." + + Long.toHexString(txnHeader.getZxid())); + BinaryInputArchive ia = BinaryInputArchive.getArchive(in); + FileHeader header = new FileHeader(); + header.deserialize(ia, "fileheader"); + LOG.info("Received magic : " + header.getMagic() + + " Expected : " + FileTxnLog.TXNLOG_MAGIC); + Assert.assertTrue("Missing magic number ", + header.getMagic() == FileTxnLog.TXNLOG_MAGIC); + } + +} http://git-wip-us.apache.org/repos/asf/zookeeper/blob/d857e042/src/java/test/org/apache/zookeeper/test/LoadFromLogTest.java ---------------------------------------------------------------------- diff --git a/src/java/test/org/apache/zookeeper/test/LoadFromLogTest.java b/src/java/test/org/apache/zookeeper/test/LoadFromLogTest.java index 591cf23..90de755 100644 --- a/src/java/test/org/apache/zookeeper/test/LoadFromLogTest.java +++ b/src/java/test/org/apache/zookeeper/test/LoadFromLogTest.java @@ -18,50 +18,29 @@ package org.apache.zookeeper.test; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.List; - -import org.apache.zookeeper.common.Time; -import org.apache.jute.BinaryInputArchive; -import org.apache.jute.BinaryOutputArchive; -import org.apache.jute.Record; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException.NoNodeException; -import org.apache.zookeeper.PortAssignment; -import org.apache.zookeeper.ZKTestCase; import org.apache.zookeeper.ZooDefs.Ids; -import org.apache.zookeeper.ZooDefs.OpCode; import org.apache.zookeeper.ZooKeeper; import org.apache.zookeeper.data.Stat; -import org.apache.zookeeper.server.DataNode; -import org.apache.zookeeper.server.DataTree; -import org.apache.zookeeper.server.ServerCnxnFactory; import org.apache.zookeeper.server.SyncRequestProcessor; import org.apache.zookeeper.server.ZooKeeperServer; -import org.apache.zookeeper.server.persistence.FileHeader; import org.apache.zookeeper.server.persistence.FileTxnLog; -import org.apache.zookeeper.server.persistence.FileTxnSnapLog; -import org.apache.zookeeper.server.persistence.Util; import org.apache.zookeeper.server.persistence.FileTxnLog.FileTxnIterator; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; import org.apache.zookeeper.server.persistence.TxnLog.TxnIterator; -import org.apache.zookeeper.txn.CreateTxn; -import org.apache.zookeeper.txn.DeleteTxn; -import org.apache.zookeeper.txn.MultiTxn; -import org.apache.zookeeper.txn.Txn; +import org.apache.zookeeper.server.persistence.Util; import org.apache.zookeeper.txn.TxnHeader; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class LoadFromLogTest extends ZKTestCase { - private static final String HOST = "127.0.0.1:"; - private static final int CONNECTION_TIMEOUT = 3000; +import java.io.File; +import java.io.IOException; + +public class LoadFromLogTest extends ClientBase { private static final int NUM_MESSAGES = 300; protected static final Logger LOG = LoggerFactory.getLogger(LoadFromLogTest.class); @@ -69,26 +48,20 @@ public class LoadFromLogTest extends ZKTestCase { private static final int TRANSACTION_OVERHEAD = 2; private static final int TOTAL_TRANSACTIONS = NUM_MESSAGES + TRANSACTION_OVERHEAD; + @Before + public void setUp() throws Exception { + SyncRequestProcessor.setSnapCount(50); + super.setUp(); + } + /** * test that all transactions from the Log are loaded, and only once * @throws Exception an exception might be thrown here */ @Test public void testLoad() throws Exception { - final String hostPort = HOST + PortAssignment.unique(); - // setup a single server cluster - File 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 = ClientBase.createZKClient(hostPort); - // generate some transactions that will get logged + ZooKeeper zk = createZKClient(hostPort); try { for (int i = 0; i< NUM_MESSAGES; i++) { zk.create("/invalidsnap-" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, @@ -97,14 +70,11 @@ public class LoadFromLogTest extends ZKTestCase { } finally { zk.close(); } - f.shutdown(); - Assert.assertTrue("waiting for server to shutdown", - ClientBase.waitForServerDown(hostPort, CONNECTION_TIMEOUT)); + stopServer(); // now verify that the FileTxnLog reads every transaction only once File logDir = new File(tmpDir, FileTxnSnapLog.version + FileTxnSnapLog.VERSION); FileTxnLog txnLog = new FileTxnLog(logDir); - TxnIterator itr = txnLog.read(0); // Check that storage space return some value @@ -126,7 +96,6 @@ public class LoadFromLogTest extends ZKTestCase { }while(itr.next()); Assert.assertTrue("processed all transactions. " + expectedZxid + " == " + TOTAL_TRANSACTIONS, (expectedZxid == TOTAL_TRANSACTIONS)); - zks.shutdown(); } /** @@ -136,21 +105,8 @@ public class LoadFromLogTest extends ZKTestCase { */ @Test public void testLoadFailure() throws Exception { - final String hostPort = HOST + PortAssignment.unique(); - // setup a single server cluster - File tmpDir = ClientBase.createTmpDir(); - ClientBase.setupTestEnv(); - ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); - // So we have at least 4 logs - SyncRequestProcessor.setSnapCount(50); - 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 + ZooKeeper zk = createZKClient(hostPort); try { for (int i = 0; i< NUM_MESSAGES; i++) { zk.create("/data-", new byte[0], Ids.OPEN_ACL_UNSAFE, @@ -159,14 +115,12 @@ public class LoadFromLogTest extends ZKTestCase { } finally { zk.close(); } - f.shutdown(); - Assert.assertTrue("waiting for server to shutdown", - ClientBase.waitForServerDown(hostPort, CONNECTION_TIMEOUT)); + stopServer(); File logDir = new File(tmpDir, FileTxnSnapLog.version + FileTxnSnapLog.VERSION); File[] logFiles = FileTxnLog.getLogFiles(logDir.listFiles(), 0); - // Verify that we have at least 4 txnlog - Assert.assertTrue(logFiles.length > 4); + // Verify that we have at least NUM_MESSAGES / SNAPCOUNT txnlog + Assert.assertTrue(logFiles.length > NUM_MESSAGES / 100); // Delete the first log file, so we will fail to read it back from disk Assert.assertTrue("delete the first log file", logFiles[0].delete()); @@ -201,129 +155,6 @@ public class LoadFromLogTest extends ZKTestCase { nextZxid = itr.getHeader().getZxid(); itr = txnLog.read(nextZxid, false); Assert.assertEquals(secondStartZxid, itr.getHeader().getZxid()); - - } - - /** - * For ZOOKEEPER-1046. Verify if cversion and pzxid if incremented - * after create/delete failure during restore. - */ - @Test - public void testTxnFailure() throws Exception { - long count = 1; - File tmpDir = ClientBase.createTmpDir(); - FileTxnSnapLog logFile = new FileTxnSnapLog(tmpDir, tmpDir); - DataTree dt = new DataTree(); - dt.createNode("/test", new byte[0], null, 0, -1, 1, 1); - for (count = 1; count <= 3; count++) { - dt.createNode("/test/" + count, new byte[0], null, 0, -1, count, - Time.currentElapsedTime()); - } - DataNode zk = dt.getNode("/test"); - - // Make create to fail, then verify cversion. - LOG.info("Attempting to create " + "/test/" + (count - 1)); - doOp(logFile, OpCode.create, "/test/" + (count - 1), dt, zk, -1); - - LOG.info("Attempting to create " + "/test/" + (count - 1)); - doOp(logFile, OpCode.create, "/test/" + (count - 1), dt, zk, - zk.stat.getCversion() + 1); - - LOG.info("Attempting to create " + "/test/" + (count - 1)); - doOp(logFile, OpCode.multi, "/test/" + (count - 1), dt, zk, - zk.stat.getCversion() + 1); - - LOG.info("Attempting to create " + "/test/" + (count - 1)); - doOp(logFile, OpCode.multi, "/test/" + (count - 1), dt, zk, - -1); - - // Make delete fo fail, then verify cversion. - // this doesn't happen anymore, we only set the cversion on create - // LOG.info("Attempting to delete " + "/test/" + (count + 1)); - // doOp(logFile, OpCode.delete, "/test/" + (count + 1), dt, zk); - } - /* - * Does create/delete depending on the type and verifies - * if cversion before the operation is 1 less than cversion afer. - */ - private void doOp(FileTxnSnapLog logFile, int type, String path, - DataTree dt, DataNode parent, int cversion) throws Exception { - int lastSlash = path.lastIndexOf('/'); - String parentName = path.substring(0, lastSlash); - - int prevCversion = parent.stat.getCversion(); - long prevPzxid = parent.stat.getPzxid(); - List<String> child = dt.getChildren(parentName, null, null); - StringBuilder childStr = new StringBuilder(); - for (String s : child) { - childStr.append(s).append(" "); - } - LOG.info("Children: " + childStr + " for " + parentName); - LOG.info("(cverions, pzxid): " + prevCversion + ", " + prevPzxid); - - Record txn = null; - TxnHeader txnHeader = null; - if (type == OpCode.delete) { - txn = new DeleteTxn(path); - txnHeader = new TxnHeader(0xabcd, 0x123, prevPzxid + 1, - Time.currentElapsedTime(), OpCode.delete); - } else if (type == OpCode.create) { - txnHeader = new TxnHeader(0xabcd, 0x123, prevPzxid + 1, - Time.currentElapsedTime(), OpCode.create); - txn = new CreateTxn(path, new byte[0], null, false, cversion); - } - else if (type == OpCode.multi) { - txnHeader = new TxnHeader(0xabcd, 0x123, prevPzxid + 1, - Time.currentElapsedTime(), OpCode.create); - txn = new CreateTxn(path, new byte[0], null, false, cversion); - List<Txn> txnList = new ArrayList<Txn>(); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos); - txn.serialize(boa, "request") ; - ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray()); - Txn txact = new Txn(OpCode.create, bb.array()); - txnList.add(txact); - txn = new MultiTxn(txnList); - txnHeader = new TxnHeader(0xabcd, 0x123, prevPzxid + 1, - Time.currentElapsedTime(), OpCode.multi); - } - logFile.processTransaction(txnHeader, dt, null, txn); - - int newCversion = parent.stat.getCversion(); - long newPzxid = parent.stat.getPzxid(); - child = dt.getChildren(parentName, null, null); - childStr = new StringBuilder(); - for (String s : child) { - childStr.append(s).append(" "); - } - LOG.info("Children: " + childStr + " for " + parentName); - LOG.info("(cverions, pzxid): " +newCversion + ", " + newPzxid); - Assert.assertTrue(type + " <cversion, pzxid> verification failed. Expected: <" + - (prevCversion + 1) + ", " + (prevPzxid + 1) + ">, found: <" + - newCversion + ", " + newPzxid + ">", - (newCversion == prevCversion + 1 && newPzxid == prevPzxid + 1)); - } - /** - * Simulates ZOOKEEPER-1069 and verifies that flush() before padLogFile - * fixes it. - */ - @Test - public void testPad() throws Exception { - File tmpDir = ClientBase.createTmpDir(); - FileTxnLog txnLog = new FileTxnLog(tmpDir); - TxnHeader txnHeader = new TxnHeader(0xabcd, 0x123, 0x123, - Time.currentElapsedTime(), OpCode.create); - Record txn = new CreateTxn("/Test", new byte[0], null, false, 1); - txnLog.append(txnHeader, txn); - FileInputStream in = new FileInputStream(tmpDir.getPath() + "/log." + - Long.toHexString(txnHeader.getZxid())); - BinaryInputArchive ia = BinaryInputArchive.getArchive(in); - FileHeader header = new FileHeader(); - header.deserialize(ia, "fileheader"); - LOG.info("Received magic : " + header.getMagic() + - " Expected : " + FileTxnLog.TXNLOG_MAGIC); - Assert.assertTrue("Missing magic number ", - header.getMagic() == FileTxnLog.TXNLOG_MAGIC); } /** @@ -332,20 +163,8 @@ public class LoadFromLogTest extends ZKTestCase { */ @Test public void testRestore() throws Exception { - final String hostPort = HOST + PortAssignment.unique(); - // setup a single server cluster - File tmpDir = ClientBase.createTmpDir(); - ClientBase.setupTestEnv(); - ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); - SyncRequestProcessor.setSnapCount(10000); - 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 = getConnectedZkClient(hostPort); - // generate some transactions + ZooKeeper zk = createZKClient(hostPort); String lastPath = null; try { zk.create("/invalidsnap", new byte[0], Ids.OPEN_ACL_UNSAFE, @@ -361,6 +180,7 @@ public class LoadFromLogTest extends ZKTestCase { String expectedPath = "/invalidsnap/test-" + String.format("%010d", (new Integer(tokens[1])).intValue() + 1); + ZooKeeperServer zks = getServer(serverFactory); long eZxid = zks.getZKDatabase().getDataTreeLastProcessedZxid(); // force the zxid to be behind the content zks.getZKDatabase().setlastProcessedZxid( @@ -370,20 +190,16 @@ public class LoadFromLogTest extends ZKTestCase { // Force snapshot and restore zks.takeSnapshot(); zks.shutdown(); - f.shutdown(); + stopServer(); - zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); - SyncRequestProcessor.setSnapCount(10000); - f = ServerCnxnFactory.createFactory(PORT, -1); - f.startup(zks); - Assert.assertTrue("waiting for server being up ", ClientBase - .waitForServerUp(hostPort, CONNECTION_TIMEOUT)); + startServer(); + zks = getServer(serverFactory); long fZxid = zks.getZKDatabase().getDataTreeLastProcessedZxid(); // Verify lastProcessedZxid is set correctly Assert.assertTrue("Restore failed expected zxid=" + eZxid + " found=" + fZxid, fZxid == eZxid); - zk = getConnectedZkClient(hostPort); + zk = createZKClient(hostPort); // Verify correctness of data and whether sequential znode creation // proceeds correctly after this point @@ -403,8 +219,6 @@ public class LoadFromLogTest extends ZKTestCase { Assert.assertTrue("Unexpected number of children " + children.length + " expected " + NUM_MESSAGES, (children.length == NUM_MESSAGES)); - f.shutdown(); - zks.shutdown(); } /** @@ -413,20 +227,8 @@ public class LoadFromLogTest extends ZKTestCase { */ @Test public void testRestoreWithTransactionErrors() throws Exception { - final String hostPort = HOST + PortAssignment.unique(); - // setup a single server cluster - File tmpDir = ClientBase.createTmpDir(); - ClientBase.setupTestEnv(); - ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); - SyncRequestProcessor.setSnapCount(10000); - 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 = getConnectedZkClient(hostPort); - // generate some transactions + ZooKeeper zk = createZKClient(hostPort); try { for (int i = 0; i < NUM_MESSAGES; i++) { try { @@ -441,6 +243,7 @@ public class LoadFromLogTest extends ZKTestCase { } // force the zxid to be behind the content + ZooKeeperServer zks = getServer(serverFactory); zks.getZKDatabase().setlastProcessedZxid( zks.getZKDatabase().getDataTreeLastProcessedZxid() - 10); LOG.info("Set lastProcessedZxid to " @@ -449,17 +252,10 @@ public class LoadFromLogTest extends ZKTestCase { // Force snapshot and restore zks.takeSnapshot(); zks.shutdown(); - f.shutdown(); + stopServer(); zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); - SyncRequestProcessor.setSnapCount(10000); - f = ServerCnxnFactory.createFactory(PORT, -1); - f.startup(zks); - Assert.assertTrue("waiting for server being up ", ClientBase - .waitForServerUp(hostPort, CONNECTION_TIMEOUT)); - - f.shutdown(); - zks.shutdown(); + startServer(); } /** @@ -467,32 +263,13 @@ public class LoadFromLogTest extends ZKTestCase { */ @Test public void testDatadirAutocreate() throws Exception { - ClientBase.setupTestEnv(); - final String hostPort = HOST + PortAssignment.unique(); - // first verify the default (autocreate on) works - File tmpDir = ClientBase.createTmpDir(); - ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); - 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)); - zks.shutdown(); - f.shutdown(); - Assert.assertTrue("waiting for server being down ", ClientBase - .waitForServerDown(hostPort, CONNECTION_TIMEOUT)); + stopServer(); try { // now verify autocreate off works System.setProperty(FileTxnSnapLog.ZOOKEEPER_DATADIR_AUTOCREATE, "false"); - - tmpDir = ClientBase.createTmpDir(); - zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); - f = ServerCnxnFactory.createFactory(PORT, -1); - f.startup(zks); - Assert.assertTrue("waiting for server being up ", ClientBase - .waitForServerUp(hostPort, CONNECTION_TIMEOUT)); - + tmpDir = createTmpDir(); + startServer(); Assert.fail("Server should not have started without datadir"); } catch (IOException e) { LOG.info("Server failed to start - correct behavior " + e); @@ -508,20 +285,8 @@ public class LoadFromLogTest extends ZKTestCase { */ @Test public void testReloadSnapshotWithMissingParent() throws Exception { - final String hostPort = HOST + PortAssignment.unique(); - // setup a single server cluster - File tmpDir = ClientBase.createTmpDir(); - ClientBase.setupTestEnv(); - ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); - SyncRequestProcessor.setSnapCount(10000); - 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 = getConnectedZkClient(hostPort); - // create transactions to create the snapshot with create/delete pattern + ZooKeeper zk = createZKClient(hostPort); zk.create("/a", "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); Stat stat = zk.exists("/a", false); @@ -531,25 +296,15 @@ public class LoadFromLogTest extends ZKTestCase { zk.delete("/a/b", -1); zk.delete("/a", -1); // force the zxid to be behind the content + ZooKeeperServer zks = getServer(serverFactory); zks.getZKDatabase().setlastProcessedZxid(createZxId); LOG.info("Set lastProcessedZxid to {}", zks.getZKDatabase() .getDataTreeLastProcessedZxid()); // Force snapshot and restore zks.takeSnapshot(); zks.shutdown(); - f.shutdown(); - - zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); - SyncRequestProcessor.setSnapCount(10000); - f = ServerCnxnFactory.createFactory(PORT, -1); - f.startup(zks); - Assert.assertTrue("waiting for server being up ", - ClientBase.waitForServerUp(hostPort, CONNECTION_TIMEOUT)); - f.shutdown(); - } + stopServer(); - private ZooKeeper getConnectedZkClient(String host) throws Exception { - ZooKeeper zk = ClientBase.createZKClient(host); - return zk; + startServer(); } }
