use of org.apache.zookeeper.proto.CreateRequest in project hbase by apache.
the class RecoverableZooKeeper method prepareZKMulti.
/**
* Convert Iterable of {@link ZKOp} we got into the ZooKeeper.Op
* instances to actually pass to multi (need to do this in order to appendMetaData).
*/
private Iterable<Op> prepareZKMulti(Iterable<Op> ops) throws UnsupportedOperationException {
if (ops == null)
return null;
List<Op> preparedOps = new LinkedList<>();
for (Op op : ops) {
if (op.getType() == ZooDefs.OpCode.create) {
CreateRequest create = (CreateRequest) op.toRequestRecord();
preparedOps.add(Op.create(create.getPath(), appendMetaData(create.getData()), create.getAcl(), create.getFlags()));
} else if (op.getType() == ZooDefs.OpCode.delete) {
// no need to appendMetaData for delete
preparedOps.add(op);
} else if (op.getType() == ZooDefs.OpCode.setData) {
SetDataRequest setData = (SetDataRequest) op.toRequestRecord();
preparedOps.add(Op.setData(setData.getPath(), appendMetaData(setData.getData()), setData.getVersion()));
} else {
throw new UnsupportedOperationException("Unexpected ZKOp type: " + op.getClass().getName());
}
}
return preparedOps;
}
use of org.apache.zookeeper.proto.CreateRequest in project zookeeper by apache.
the class MultiOpSessionUpgradeTest method makeCreateRequest.
private Request makeCreateRequest(String path, long sessionId) throws IOException {
ByteArrayOutputStream boas = new ByteArrayOutputStream();
BinaryOutputArchive boa = BinaryOutputArchive.getArchive(boas);
CreateRequest createRequest = new CreateRequest(path, "data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL.toFlag());
createRequest.serialize(boa, "request");
ByteBuffer bb = ByteBuffer.wrap(boas.toByteArray());
return new Request(null, sessionId, 1, ZooDefs.OpCode.create2, bb, new ArrayList<Id>());
}
use of org.apache.zookeeper.proto.CreateRequest in project zookeeper by apache.
the class CommitProcessorConcurrencyTest method processAsMuchUncommittedRequestsAsPossibleTest.
/**
* Here we create the following requests queue structure: R1_1, W1_2, R1_3,
* R2_1, R2_2, W2_3, R2_4, R3_1, R3_2, R3_3, W3_4, R3_5, ... , W5_6, R5_7
* i.e., 5 sessions, each has different amount or read requests, followed by
* single write and afterwards single read. The idea is to check that all of
* the reads that can be processed concurrently do so, and that none of the
* uncommited requests, followed by the reads are processed.
*/
@Test
public void processAsMuchUncommittedRequestsAsPossibleTest() throws Exception {
final String path = "/testAsMuchAsPossible";
List<Request> shouldBeProcessed = new LinkedList<Request>();
Set<Request> shouldNotBeProcessed = new HashSet<Request>();
for (int sessionId = 1; sessionId <= 5; ++sessionId) {
for (int readReqId = 1; readReqId <= sessionId; ++readReqId) {
Request readReq = newRequest(new GetDataRequest(path, false), OpCode.getData, sessionId, readReqId);
shouldBeProcessed.add(readReq);
processor.queuedRequests.add(readReq);
}
Request writeReq = newRequest(new CreateRequest(path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL.toFlag()), OpCode.create, sessionId, sessionId + 1);
Request readReq = newRequest(new GetDataRequest(path, false), OpCode.getData, sessionId, sessionId + 2);
processor.queuedRequests.add(writeReq);
processor.queuedRequests.add(readReq);
shouldNotBeProcessed.add(writeReq);
shouldNotBeProcessed.add(readReq);
}
processor.initThreads(defaultSizeOfThreadPool);
processor.stoppedMainLoop = true;
processor.run();
Thread.sleep(1000);
shouldBeProcessed.removeAll(processedRequests);
for (Request r : shouldBeProcessed) {
LOG.error("Did not process " + r);
}
Assert.assertTrue("Not all requests were processed", shouldBeProcessed.isEmpty());
Assert.assertFalse("Processed a wrong request", shouldNotBeProcessed.removeAll(processedRequests));
}
use of org.apache.zookeeper.proto.CreateRequest in project zookeeper by apache.
the class CommitProcessorConcurrencyTest method processAllFollowingUncommittedAfterFirstCommitTest.
/**
* In the following test, we add a write request followed by several read
* requests of the same session, and we verify several things - 1. The write
* is not processed until commit arrives. 2. Once the write is processed,
* all the read requests are processed as well. 3. All read requests are
* executed after the write, before any other write, along with new reads.
*/
@Test
public void processAllFollowingUncommittedAfterFirstCommitTest() throws Exception {
final String path = "/testUncommittedFollowingCommited";
Set<Request> shouldBeInPending = new HashSet<Request>();
Set<Request> shouldBeProcessedAfterPending = new HashSet<Request>();
Request writeReq = newRequest(new CreateRequest(path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL.toFlag()), OpCode.create, 0x1, 1);
processor.queuedRequests.add(writeReq);
shouldBeInPending.add(writeReq);
for (int readReqId = 2; readReqId <= 5; ++readReqId) {
Request readReq = newRequest(new GetDataRequest(path, false), OpCode.getData, 0x1, readReqId);
processor.queuedRequests.add(readReq);
shouldBeInPending.add(readReq);
shouldBeProcessedAfterPending.add(readReq);
}
processor.initThreads(defaultSizeOfThreadPool);
processor.stoppedMainLoop = true;
processor.run();
Assert.assertTrue("Processed without waiting for commit", processedRequests.isEmpty());
Assert.assertTrue("Did not handled all of queuedRequests' requests", processor.queuedRequests.isEmpty());
shouldBeInPending.removeAll(processor.pendingRequests.get(writeReq.sessionId));
for (Request r : shouldBeInPending) {
LOG.error("Should be in pending " + r);
}
Assert.assertTrue("Not all requests moved to pending from queuedRequests", shouldBeInPending.isEmpty());
processor.committedRequests.add(writeReq);
processor.stoppedMainLoop = true;
processor.run();
processor.initThreads(defaultSizeOfThreadPool);
Thread.sleep(500);
Assert.assertTrue("Did not process committed request", processedRequests.peek() == writeReq);
Assert.assertTrue("Did not process following read request", processedRequests.containsAll(shouldBeProcessedAfterPending));
Assert.assertTrue("Did not process committed request", processor.committedRequests.isEmpty());
Assert.assertTrue("Did not process committed request", processor.pendingRequests.isEmpty());
}
use of org.apache.zookeeper.proto.CreateRequest in project zookeeper by apache.
the class CommitProcessorConcurrencyTest method noStarvationOfNonLocalCommittedRequestsTest.
/**
* In the following test, we verify that committed requests are processed
* even when queuedRequests never gets empty. We add 10 committed request
* and use infinite queuedRequests. We verify that the committed request was
* processed.
*/
@Test(timeout = 1000)
public void noStarvationOfNonLocalCommittedRequestsTest() throws Exception {
final String path = "/noStarvationOfCommittedRequests";
processor.queuedRequests = new MockRequestsQueue();
Set<Request> nonLocalCommits = new HashSet<Request>();
for (int i = 0; i < 10; i++) {
Request nonLocalCommitReq = newRequest(new CreateRequest(path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL.toFlag()), OpCode.create, 51, i + 1);
processor.committedRequests.add(nonLocalCommitReq);
nonLocalCommits.add(nonLocalCommitReq);
}
for (int i = 0; i < 10; i++) {
processor.initThreads(defaultSizeOfThreadPool);
processor.stoppedMainLoop = true;
processor.run();
}
Assert.assertTrue("commit request was not processed", processedRequests.containsAll(nonLocalCommits));
}
Aggregations