Search in sources :

Example 16 with Record

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 + ">");
}
Also used : BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) MultiTxn(org.apache.zookeeper.txn.MultiTxn) ArrayList(java.util.ArrayList) CreateTxn(org.apache.zookeeper.txn.CreateTxn) Txn(org.apache.zookeeper.txn.Txn) MultiTxn(org.apache.zookeeper.txn.MultiTxn) DeleteTxn(org.apache.zookeeper.txn.DeleteTxn) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) DeleteTxn(org.apache.zookeeper.txn.DeleteTxn) CreateTxn(org.apache.zookeeper.txn.CreateTxn) Record(org.apache.jute.Record) TxnHeader(org.apache.zookeeper.txn.TxnHeader)

Example 17 with Record

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");
}
Also used : BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Semaphore(java.util.concurrent.Semaphore) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Record(org.apache.jute.Record) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 18 with Record

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();
    }
}
Also used : Record(org.apache.jute.Record) SetDataTxn(org.apache.zookeeper.txn.SetDataTxn) File(java.io.File) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) TxnHeader(org.apache.zookeeper.txn.TxnHeader) Test(org.junit.jupiter.api.Test)

Example 19 with Record

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;
}
Also used : CreateRequest(org.apache.zookeeper.proto.CreateRequest) Record(org.apache.jute.Record) CreateTTLRequest(org.apache.zookeeper.proto.CreateTTLRequest)

Example 20 with 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);
    }
}
Also used : ReplyHeader(org.apache.zookeeper.proto.ReplyHeader) RequestHeader(org.apache.zookeeper.proto.RequestHeader) Record(org.apache.jute.Record)

Aggregations

Record (org.apache.jute.Record)42 TxnHeader (org.apache.zookeeper.txn.TxnHeader)28 IOException (java.io.IOException)13 SetDataTxn (org.apache.zookeeper.txn.SetDataTxn)13 CreateTxn (org.apache.zookeeper.txn.CreateTxn)12 ByteBuffer (java.nio.ByteBuffer)10 BinaryInputArchive (org.apache.jute.BinaryInputArchive)9 BinaryOutputArchive (org.apache.jute.BinaryOutputArchive)9 DeleteTxn (org.apache.zookeeper.txn.DeleteTxn)9 MultiTxn (org.apache.zookeeper.txn.MultiTxn)9 ErrorTxn (org.apache.zookeeper.txn.ErrorTxn)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 ArrayList (java.util.ArrayList)7 SetACLTxn (org.apache.zookeeper.txn.SetACLTxn)7 Txn (org.apache.zookeeper.txn.Txn)7 Test (org.junit.jupiter.api.Test)7 EOFException (java.io.EOFException)6 File (java.io.File)6 KeeperException (org.apache.zookeeper.KeeperException)6 CreateRequest (org.apache.zookeeper.proto.CreateRequest)6