Search in sources :

Example 1 with GetDataRequest

use of org.apache.zookeeper.proto.GetDataRequest in project zookeeper by apache.

the class CommitProcessorConcurrencyTest method committedAndUncommittedOfTheSameSessionRaceTest.

/**
 * We place a read request followed by committed update request of the same
 * session in queuedRequests. We verify that both requests are processed,
 * according to the order of the session (first read, then the write).
 */
@Test
public void committedAndUncommittedOfTheSameSessionRaceTest() throws Exception {
    final String path = "/testCvsUCRace";
    Request readReq = newRequest(new GetDataRequest(path, false), OpCode.getData, 0x0, 0);
    Request writeReq = newRequest(new SetDataRequest(path, new byte[16], -1), OpCode.setData, 0x0, 1);
    processor.committedRequests.add(writeReq);
    processor.queuedRequests.add(readReq);
    processor.queuedRequests.add(writeReq);
    processor.initThreads(1);
    processor.stoppedMainLoop = true;
    processor.run();
    Assert.assertTrue("Request was not processed " + readReq + " instead " + processedRequests.peek(), processedRequests.peek() != null && processedRequests.peek().equals(readReq));
    processedRequests.poll();
    Assert.assertTrue("Request was not processed " + writeReq + " instead " + processedRequests.peek(), processedRequests.peek() != null && processedRequests.peek().equals(writeReq));
}
Also used : GetDataRequest(org.apache.zookeeper.proto.GetDataRequest) CreateRequest(org.apache.zookeeper.proto.CreateRequest) SetDataRequest(org.apache.zookeeper.proto.SetDataRequest) Request(org.apache.zookeeper.server.Request) SetDataRequest(org.apache.zookeeper.proto.SetDataRequest) GetDataRequest(org.apache.zookeeper.proto.GetDataRequest) Test(org.junit.Test)

Example 2 with GetDataRequest

use of org.apache.zookeeper.proto.GetDataRequest in project zookeeper by apache.

the class CommitProcessorConcurrencyTest method noStarvationOfReadRequestsTest.

/**
 * In the following test, we verify that committed writes are not causing
 * reads starvation. We populate the commit processor with the following
 * order of requests: 1 committed local updated, 1 read request, 100
 * committed non-local updates. 50 read requests. We verify that after the
 * first call to processor.run, only the first write is processed, then
 * after the second call, all reads are processed along with the second
 * write.
 */
@Test
public void noStarvationOfReadRequestsTest() throws Exception {
    final String path = "/noStarvationOfReadRequests";
    // +1 committed requests (also head of queuedRequests)
    Request firstCommittedReq = newRequest(new CreateRequest(path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL.toFlag()), OpCode.create, 0x3, 1);
    processor.queuedRequests.add(firstCommittedReq);
    processor.committedRequests.add(firstCommittedReq);
    Set<Request> allReads = new HashSet<Request>();
    // +1 read request to queuedRequests
    Request firstRead = newRequest(new GetDataRequest(path, false), OpCode.getData, 0x1, 0);
    allReads.add(firstRead);
    processor.queuedRequests.add(firstRead);
    // +1 non local commit
    Request secondCommittedReq = newRequest(new CreateRequest(path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL.toFlag()), OpCode.create, 0x99, 2);
    processor.committedRequests.add(secondCommittedReq);
    Set<Request> waitingCommittedRequests = new HashSet<Request>();
    // +99 non local committed requests
    for (int writeReqId = 3; writeReqId < 102; ++writeReqId) {
        Request writeReq = newRequest(new CreateRequest(path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL.toFlag()), OpCode.create, 0x8, writeReqId);
        processor.committedRequests.add(writeReq);
        waitingCommittedRequests.add(writeReq);
    }
    // +50 read requests to queuedRequests
    for (int readReqId = 1; readReqId <= 50; ++readReqId) {
        Request readReq = newRequest(new GetDataRequest(path, false), OpCode.getData, 0x5, readReqId);
        allReads.add(readReq);
        processor.queuedRequests.add(readReq);
    }
    processor.initThreads(defaultSizeOfThreadPool);
    processor.stoppedMainLoop = true;
    processor.run();
    Assert.assertTrue("Did not process the first write request", processedRequests.contains(firstCommittedReq));
    for (Request r : allReads) {
        Assert.assertTrue("Processed read request", !processedRequests.contains(r));
    }
    processor.run();
    Assert.assertTrue("did not processed all reads", processedRequests.containsAll(allReads));
    Assert.assertTrue("Did not process the second write request", processedRequests.contains(secondCommittedReq));
    for (Request r : waitingCommittedRequests) {
        Assert.assertTrue("Processed additional committed request", !processedRequests.contains(r));
    }
}
Also used : CreateRequest(org.apache.zookeeper.proto.CreateRequest) GetDataRequest(org.apache.zookeeper.proto.GetDataRequest) CreateRequest(org.apache.zookeeper.proto.CreateRequest) SetDataRequest(org.apache.zookeeper.proto.SetDataRequest) Request(org.apache.zookeeper.server.Request) GetDataRequest(org.apache.zookeeper.proto.GetDataRequest) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with GetDataRequest

use of org.apache.zookeeper.proto.GetDataRequest in project zookeeper by apache.

the class CommitProcessorConcurrencyTest method noCrashOnOutofOrderCommittedRequestTest.

/**
 * In the following test, we verify if we handle the case in which we get a commit
 * for a request that has higher Cxid than the one we are waiting. This can happen
 * when a session connection is lost but there is a request waiting to be committed in the
 * session queue. However, since the session has moved, new requests can get to
 * the leader out of order. Hence, the commits can also arrive "out of order" w.r.t. cxid.
 * We should commit the requests according to the order we receive from the leader, i.e., wait for the relevant commit.
 */
@Test(timeout = 5000)
public void noCrashOnOutofOrderCommittedRequestTest() throws Exception {
    final String path = "/noCrash/OnCommittedRequests/OfUnSeenRequestTest";
    final int sessionid = 0x123456;
    final int lastCXid = 0x100;
    final int numberofReads = 10;
    int readReqId = lastCXid;
    processor.stoppedMainLoop = true;
    HashSet<Request> localRequests = new HashSet<Request>();
    // queue the blocking write request to queuedRequests
    Request orphanCommittedReq = newRequest(new CreateRequest(path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL.toFlag()), OpCode.create, sessionid, lastCXid);
    processor.queuedRequests.add(orphanCommittedReq);
    localRequests.add(orphanCommittedReq);
    // queue read requests to queuedRequests
    for (; readReqId <= numberofReads + lastCXid; ++readReqId) {
        Request readReq = newRequest(new GetDataRequest(path, false), OpCode.getData, sessionid, readReqId);
        processor.queuedRequests.add(readReq);
        localRequests.add(readReq);
    }
    // run once
    processor.initThreads(defaultSizeOfThreadPool);
    processor.run();
    Thread.sleep(1000);
    // We verify that the processor is waiting for the commit
    Assert.assertTrue(processedRequests.isEmpty());
    // We add a commit that belongs to the same session but with larger cxid,
    // i.e., commit of an update from the next connection of this session.
    Request otherSessionCommittedReq = newRequest(new CreateRequest(path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL.toFlag()), OpCode.create, sessionid, lastCXid + 10);
    processor.committedRequests.add(otherSessionCommittedReq);
    processor.committedRequests.add(orphanCommittedReq);
    processor.run();
    Thread.sleep(1000);
    // We verify that the commit processor processed the old commit prior to the newer messages
    Assert.assertTrue(processedRequests.size() == 1);
    Assert.assertTrue(processedRequests.contains(otherSessionCommittedReq));
    processor.run();
    Thread.sleep(1000);
    // We verify that the commit processor handle all messages.
    Assert.assertTrue(processedRequests.containsAll(localRequests));
}
Also used : CreateRequest(org.apache.zookeeper.proto.CreateRequest) GetDataRequest(org.apache.zookeeper.proto.GetDataRequest) CreateRequest(org.apache.zookeeper.proto.CreateRequest) SetDataRequest(org.apache.zookeeper.proto.SetDataRequest) Request(org.apache.zookeeper.server.Request) GetDataRequest(org.apache.zookeeper.proto.GetDataRequest) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with GetDataRequest

use of org.apache.zookeeper.proto.GetDataRequest in project zookeeper by apache.

the class CommitProcessorConcurrencyTest method noCrashOnCommittedRequestsOfUnseenRequestTest.

/**
 * In the following test, we verify that we can handle the case that we got a commit
 * of a request we never seen since the session that we just established. This can happen
 * when a session is just established and there is request waiting to be committed in the
 * session queue but it sees a commit for a request that belongs to the previous connection.
 */
@Test(timeout = 5000)
public void noCrashOnCommittedRequestsOfUnseenRequestTest() throws Exception {
    final String path = "/noCrash/OnCommittedRequests/OfUnseenRequestTest";
    final int numberofReads = 10;
    final int sessionid = 0x123456;
    final int firstCXid = 0x100;
    int readReqId = firstCXid;
    processor.stoppedMainLoop = true;
    HashSet<Request> localRequests = new HashSet<Request>();
    // queue the blocking write request to queuedRequests
    Request firstCommittedReq = newRequest(new CreateRequest(path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL.toFlag()), OpCode.create, sessionid, readReqId++);
    processor.queuedRequests.add(firstCommittedReq);
    localRequests.add(firstCommittedReq);
    // queue read requests to queuedRequests
    for (; readReqId <= numberofReads + firstCXid; ++readReqId) {
        Request readReq = newRequest(new GetDataRequest(path, false), OpCode.getData, sessionid, readReqId);
        processor.queuedRequests.add(readReq);
        localRequests.add(readReq);
    }
    // run once
    Assert.assertTrue(processor.queuedRequests.containsAll(localRequests));
    processor.initThreads(defaultSizeOfThreadPool);
    processor.run();
    Thread.sleep(1000);
    // We verify that the processor is waiting for the commit
    Assert.assertTrue(processedRequests.isEmpty());
    // We add a commit that belongs to the same session but with smaller cxid,
    // i.e., commit of an update from previous connection of this session.
    Request preSessionCommittedReq = newRequest(new CreateRequest(path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL.toFlag()), OpCode.create, sessionid, firstCXid - 2);
    processor.committedRequests.add(preSessionCommittedReq);
    processor.committedRequests.add(firstCommittedReq);
    processor.run();
    Thread.sleep(1000);
    // We verify that the commit processor processed the old commit prior to the newer messages
    Assert.assertTrue(processedRequests.peek() == preSessionCommittedReq);
    processor.run();
    Thread.sleep(1000);
    // We verify that the commit processor handle all messages.
    Assert.assertTrue(processedRequests.containsAll(localRequests));
}
Also used : CreateRequest(org.apache.zookeeper.proto.CreateRequest) GetDataRequest(org.apache.zookeeper.proto.GetDataRequest) CreateRequest(org.apache.zookeeper.proto.CreateRequest) SetDataRequest(org.apache.zookeeper.proto.SetDataRequest) Request(org.apache.zookeeper.server.Request) GetDataRequest(org.apache.zookeeper.proto.GetDataRequest) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with GetDataRequest

use of org.apache.zookeeper.proto.GetDataRequest in project zookeeper by apache.

the class ZooKeeper method getData.

/**
 * Return the data and the stat of the node of the given path.
 * <p>
 * If the watch is non-null and the call is successful (no exception is
 * thrown), a watch will be left on the node with the given path. The watch
 * will be triggered by a successful operation that sets data on the node, or
 * deletes the node.
 * <p>
 * A KeeperException with error code KeeperException.NoNode will be thrown
 * if no node with the given path exists.
 *
 * @param path the given path
 * @param watcher explicit watcher
 * @param stat the stat of the node
 * @return the data of the node
 * @throws KeeperException If the server signals an error with a non-zero error code
 * @throws InterruptedException If the server transaction is interrupted.
 * @throws IllegalArgumentException if an invalid path is specified
 */
public byte[] getData(final String path, Watcher watcher, Stat stat) throws KeeperException, InterruptedException {
    final String clientPath = path;
    PathUtils.validatePath(clientPath);
    // the watch contains the un-chroot path
    WatchRegistration wcb = null;
    if (watcher != null) {
        wcb = new DataWatchRegistration(watcher, clientPath);
    }
    final String serverPath = prependChroot(clientPath);
    RequestHeader h = new RequestHeader();
    h.setType(ZooDefs.OpCode.getData);
    GetDataRequest request = new GetDataRequest();
    request.setPath(serverPath);
    request.setWatch(watcher != null);
    GetDataResponse response = new GetDataResponse();
    ReplyHeader r = cnxn.submitRequest(h, request, response, wcb);
    if (r.getErr() != 0) {
        throw KeeperException.create(KeeperException.Code.get(r.getErr()), clientPath);
    }
    if (stat != null) {
        DataTree.copyStat(response.getStat(), stat);
    }
    return response.getData();
}
Also used : ReplyHeader(org.apache.zookeeper.proto.ReplyHeader) RequestHeader(org.apache.zookeeper.proto.RequestHeader) GetDataResponse(org.apache.zookeeper.proto.GetDataResponse) GetDataRequest(org.apache.zookeeper.proto.GetDataRequest)

Aggregations

GetDataRequest (org.apache.zookeeper.proto.GetDataRequest)12 CreateRequest (org.apache.zookeeper.proto.CreateRequest)7 SetDataRequest (org.apache.zookeeper.proto.SetDataRequest)6 Request (org.apache.zookeeper.server.Request)6 Test (org.junit.Test)6 HashSet (java.util.HashSet)5 GetDataResponse (org.apache.zookeeper.proto.GetDataResponse)5 ReplyHeader (org.apache.zookeeper.proto.ReplyHeader)5 RequestHeader (org.apache.zookeeper.proto.RequestHeader)3 ByteBuffer (java.nio.ByteBuffer)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 BinaryOutputArchive (org.apache.jute.BinaryOutputArchive)1 Record (org.apache.jute.Record)1 KeeperException (org.apache.zookeeper.KeeperException)1 Code (org.apache.zookeeper.KeeperException.Code)1 SessionMovedException (org.apache.zookeeper.KeeperException.SessionMovedException)1 MultiResponse (org.apache.zookeeper.MultiResponse)1