use of org.apache.zookeeper.server.ZKDatabase 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.ZKDatabase 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.ZKDatabase in project zookeeper by apache.
the class ClientBase method shutdownServerInstance.
static void shutdownServerInstance(ServerCnxnFactory factory, String hostPort) {
if (factory != null) {
ZKDatabase zkDb = null;
{
ZooKeeperServer zs = factory.getZooKeeperServer();
if (zs != null) {
zkDb = zs.getZKDatabase();
}
}
factory.shutdown();
try {
if (zkDb != null) {
zkDb.close();
}
} catch (IOException ie) {
LOG.warn("Error closing logs ", ie);
}
final int PORT = getPort(hostPort);
assertTrue(ClientBase.waitForServerDown("127.0.0.1:" + PORT, CONNECTION_TIMEOUT, factory.isSecure()), "waiting for server down");
}
}
use of org.apache.zookeeper.server.ZKDatabase in project zookeeper by apache.
the class FileTxnLogTest method testLogSizeLimit.
/**
* Test that the server can correctly load the data when there are multiple
* txnlogs per snapshot
*/
@Test
public void testLogSizeLimit() throws Exception {
File tmpDir = ClientBase.createTmpDir();
ClientBase.setupTestEnv();
// Need to override preallocate set by setupTestEnv()
// We don't need to unset these values since each unit test run in
// a separate JVM instance
FileTxnLog.setPreallocSize(PREALLOCATE);
FileTxnLog.setTxnLogSizeLimit(LOG_SIZE_LIMIT);
ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]);
ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1);
f.startup(zks);
assertTrue(ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT), "waiting for server being up ");
ZooKeeper zk = new ZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, DummyWatcher.INSTANCE);
// Generate transactions
HashSet<Long> zxids = new HashSet<>();
byte[] bytes = new byte[NODE_SIZE];
Random random = new Random();
random.nextBytes(bytes);
// We will create enough txn to generate 3 logs
long txnCount = LOG_SIZE_LIMIT / NODE_SIZE / 2 * 5;
LOG.info("Creating {} txns", txnCount);
try {
for (long i = 0; i < txnCount; i++) {
Stat stat = new Stat();
zk.create("/node-" + i, bytes, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zk.getData("/node-" + i, null, stat);
zxids.add(stat.getCzxid());
}
} finally {
zk.close();
}
// shutdown
f.shutdown();
assertTrue(ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT), "waiting for server to shutdown");
File logDir = new File(tmpDir, FileTxnSnapLog.version + FileTxnSnapLog.VERSION);
File[] txnLogs = FileTxnLog.getLogFiles(logDir.listFiles(), 0);
assertEquals(3, txnLogs.length, "Unexpected number of logs");
// Log size should not exceed limit by more than one node size;
long threshold = LOG_SIZE_LIMIT + NODE_SIZE;
LOG.info(txnLogs[0].getAbsolutePath());
assertTrue(threshold > txnLogs[0].length(), "Exceed log size limit: " + txnLogs[0].length());
LOG.info(txnLogs[1].getAbsolutePath());
assertTrue(threshold > txnLogs[1].length(), "Exceed log size limit " + txnLogs[1].length());
// Start database only
zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
zks.startdata();
ZKDatabase db = zks.getZKDatabase();
for (long i = 0; i < txnCount; i++) {
Stat stat = new Stat();
byte[] data = db.getData("/node-" + i, stat, null);
assertArrayEquals(bytes, data, "Missmatch data");
assertTrue(zxids.contains(stat.getMzxid()), "Unknown zxid ");
}
}
use of org.apache.zookeeper.server.ZKDatabase in project pravega by pravega.
the class ZooKeeperServiceRunner method stop.
public void stop() {
try {
ServerCnxnFactory sf = this.serverFactory.getAndSet(null);
if (sf != null) {
sf.closeAll(ServerCnxn.DisconnectReason.CLOSE_ALL_CONNECTIONS_FORCED);
sf.shutdown();
}
} catch (Throwable e) {
log.warn("Unable to cleanly shutdown ZooKeeper connection factory", e);
}
try {
ZooKeeperServer zs = this.server.getAndSet(null);
if (zs != null) {
zs.shutdown();
ZKDatabase zkDb = zs.getZKDatabase();
if (zkDb != null) {
// make ZK server close its log files
zkDb.close();
}
}
} catch (Throwable e) {
log.warn("Unable to cleanly shutdown ZooKeeper server", e);
}
if (secureZK) {
ZKTLSUtils.unsetSecureZKServerProperties();
}
}
Aggregations