use of org.apache.zookeeper.server.persistence.FileTxnSnapLog in project zookeeper by apache.
the class ZooKeeperServerBeanTest method testTxnLogElapsedSyncTime.
@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();
}
}
use of org.apache.zookeeper.server.persistence.FileTxnSnapLog in project zookeeper by apache.
the class ZooKeeperServerCreationTest method testDefaultConstructor.
/**
* Test the default ZooKeeperServer and call processConnectRequest() to make sure
* that all needed fields are initialized properly, etc.
*/
@Test
public void testDefaultConstructor() throws Exception {
File tmpDir = ClientBase.createEmptyTestDir();
FileTxnSnapLog fileTxnSnapLog = new FileTxnSnapLog(new File(tmpDir, "data"), new File(tmpDir, "data_txnlog"));
ZooKeeperServer zks = new ZooKeeperServer() {
@Override
public void submitRequest(Request si) {
// NOP
}
};
zks.setTxnLogFactory(fileTxnSnapLog);
zks.setZKDatabase(new ZKDatabase(fileTxnSnapLog));
zks.createSessionTracker();
ServerCnxnFactory cnxnFactory = ServerCnxnFactory.createFactory();
ServerCnxn cnxn = new MockServerCnxn();
ConnectRequest connReq = new ConnectRequest();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos);
connReq.serialize(boa, "connect");
zks.processConnectRequest(cnxn, ByteBuffer.wrap(baos.toByteArray()));
}
use of org.apache.zookeeper.server.persistence.FileTxnSnapLog in project zookeeper by apache.
the class Zab1_0Test method createObserver.
private ConversableObserver createObserver(File tmpDir, QuorumPeer peer) throws IOException {
FileTxnSnapLog logFactory = new FileTxnSnapLog(tmpDir, tmpDir);
peer.setTxnFactory(logFactory);
ZKDatabase zkDb = new ZKDatabase(logFactory);
ObserverZooKeeperServer zk = new ObserverZooKeeperServer(logFactory, peer, zkDb);
peer.setZKDatabase(zkDb);
return new ConversableObserver(peer, zk);
}
use of org.apache.zookeeper.server.persistence.FileTxnSnapLog in project zookeeper by apache.
the class Zab1_0Test method testInitialAcceptedCurrent.
@Test
public void testInitialAcceptedCurrent() throws Exception {
File tmpDir = File.createTempFile("test", ".dir", testData);
tmpDir.delete();
tmpDir.mkdir();
try {
FileTxnSnapLog logFactory = new FileTxnSnapLog(tmpDir, tmpDir);
File version2 = new File(tmpDir, "version-2");
version2.mkdir();
logFactory.save(new DataTree(), new ConcurrentHashMap<Long, Integer>(), false);
long zxid = ZxidUtils.makeZxid(3, 3);
logFactory.append(new Request(1, 1, ZooDefs.OpCode.error, new TxnHeader(1, 1, zxid, 1, ZooDefs.OpCode.error), new ErrorTxn(1), zxid));
logFactory.commit();
ZKDatabase zkDb = new ZKDatabase(logFactory);
QuorumPeer peer = QuorumPeer.testingQuorumPeer();
peer.setZKDatabase(zkDb);
peer.setTxnFactory(logFactory);
peer.getLastLoggedZxid();
assertEquals(3, peer.getAcceptedEpoch());
assertEquals(3, peer.getCurrentEpoch());
assertEquals(3, Integer.parseInt(readContentsOfFile(new File(version2, QuorumPeer.CURRENT_EPOCH_FILENAME))));
assertEquals(3, Integer.parseInt(readContentsOfFile(new File(version2, QuorumPeer.ACCEPTED_EPOCH_FILENAME))));
} finally {
TestUtils.deleteFileRecursively(tmpDir);
}
}
use of org.apache.zookeeper.server.persistence.FileTxnSnapLog in project zookeeper by apache.
the class ZooKeeperServerMainTest method testNonRecoverableError.
/**
* 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(value = 30)
public void testNonRecoverableError() throws Exception {
ClientBase.setupTestEnv();
final int CLIENT_PORT = PortAssignment.unique();
MainThread main = new MainThread(CLIENT_PORT, true, null);
main.start();
assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT, CONNECTION_TIMEOUT), "waiting for server being up");
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);
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();
assertTrue(ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT, ClientBase.CONNECTION_TIMEOUT), "waiting for server down");
fileTxnSnapLogWithError.close();
main.shutdown();
main.deleteDirs();
}
Aggregations