use of org.apache.jute.Record in project zookeeper by apache.
the class LoadFromLogNoServerTest method doOp.
/*
* 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: {} for {}", childStr, 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: {} for {}", childStr, parentName);
LOG.info("(cverions, pzxid): {}, {}", newCversion, newPzxid);
assertTrue((newCversion == prevCversion + 1 && newPzxid == prevPzxid + 1), type + " <cversion, pzxid> verification failed. Expected: <" + (prevCversion + 1) + ", " + (prevPzxid + 1) + ">, found: <" + newCversion + ", " + newPzxid + ">");
}
use of org.apache.jute.Record in project zookeeper by apache.
the class DataTreeTest method testSerializeDoesntLockDataNodeWhileWriting.
/*
* ZOOKEEPER-2201 - OutputArchive.writeRecord can block for long periods of
* time, we must call it outside of the node lock.
* We call tree.serialize, which calls our modified writeRecord method that
* blocks until it can verify that a separate thread can lock the DataNode
* currently being written, i.e. that DataTree.serializeNode does not hold
* the DataNode lock while calling OutputArchive.writeRecord.
*/
@Test
@Timeout(value = 60)
public void testSerializeDoesntLockDataNodeWhileWriting() throws Exception {
DataTree tree = new DataTree();
tree.createNode("/marker", new byte[] { 42 }, null, -1, 1, 1, 1);
final DataNode markerNode = tree.getNode("/marker");
final AtomicBoolean ranTestCase = new AtomicBoolean();
DataOutputStream out = new DataOutputStream(new ByteArrayOutputStream());
BinaryOutputArchive oa = new BinaryOutputArchive(out) {
@Override
public void writeRecord(Record r, String tag) throws IOException {
// which adds default ACL to config node.
if (r instanceof DataNode) {
DataNode node = (DataNode) r;
if (node.data.length == 1 && node.data[0] == 42) {
final Semaphore semaphore = new Semaphore(0);
new Thread(new Runnable() {
@Override
public void run() {
synchronized (markerNode) {
// When we lock markerNode, allow writeRecord to continue
semaphore.release();
}
}
}).start();
try {
boolean acquired = semaphore.tryAcquire(30, TimeUnit.SECONDS);
// This is the real assertion - could another thread lock
// the DataNode we're currently writing
assertTrue(acquired, "Couldn't acquire a lock on the DataNode while we were calling tree.serialize");
} catch (InterruptedException e1) {
throw new RuntimeException(e1);
}
ranTestCase.set(true);
}
}
super.writeRecord(r, tag);
}
};
tree.serialize(oa, "test");
// Let's make sure that we hit the code that ran the real assertion above
assertTrue(ranTestCase.get(), "Didn't find the expected node");
}
use of org.apache.jute.Record 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.jute.Record in project zookeeper by apache.
the class ZooKeeper method makeCreateRecord.
private Record makeCreateRecord(CreateMode createMode, String serverPath, byte[] data, List<ACL> acl, long ttl) {
Record record;
if (createMode.isTTL()) {
CreateTTLRequest request = new CreateTTLRequest();
request.setData(data);
request.setFlags(createMode.toFlag());
request.setPath(serverPath);
request.setAcl(acl);
request.setTtl(ttl);
record = request;
} else {
CreateRequest request = new CreateRequest();
request.setData(data);
request.setFlags(createMode.toFlag());
request.setPath(serverPath);
request.setAcl(acl);
record = request;
}
return record;
}
use of org.apache.jute.Record in project zookeeper by apache.
the class ZooKeeper method removeWatches.
private void removeWatches(int opCode, String path, Watcher watcher, WatcherType watcherType, boolean local) throws InterruptedException, KeeperException {
PathUtils.validatePath(path);
final String clientPath = path;
final String serverPath = prependChroot(clientPath);
WatchDeregistration wcb = new WatchDeregistration(clientPath, watcher, watcherType, local, getWatchManager());
RequestHeader h = new RequestHeader();
h.setType(opCode);
Record request = getRemoveWatchesRequest(opCode, watcherType, serverPath);
ReplyHeader r = cnxn.submitRequest(h, request, null, null, wcb);
if (r.getErr() != 0) {
throw KeeperException.create(KeeperException.Code.get(r.getErr()), clientPath);
}
}
Aggregations