use of org.apache.zookeeper.proto.CreateRequest in project zookeeper by apache.
the class PrepRequestProcessorTest method createRequest.
private Request createRequest(Record record, int opCode, long sessionId, boolean admin) throws IOException {
// encoding
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos);
record.serialize(boa, "request");
baos.close();
// Id
List<Id> ids = Arrays.asList(admin ? new Id("super", "super user") : Ids.ANYONE_ID_UNSAFE);
return new Request(null, sessionId, 0, opCode, ByteBuffer.wrap(baos.toByteArray()), ids);
}
use of org.apache.zookeeper.proto.CreateRequest in project zookeeper by apache.
the class PrepRequestProcessorTest method testCloseSessionTxn.
/**
* Test ephemerals are deleted when the session is closed with
* the newly added CloseSessionTxn in ZOOKEEPER-3145.
*/
@Test
public void testCloseSessionTxn() throws Exception {
boolean before = ZooKeeperServer.isCloseSessionTxnEnabled();
ZooKeeperServer.setCloseSessionTxnEnabled(true);
try {
// create a few ephemerals
long ephemeralOwner = 1;
DataTree dt = zks.getZKDatabase().dataTree;
dt.createNode("/foo", new byte[0], Ids.OPEN_ACL_UNSAFE, ephemeralOwner, 0, 0, 0);
dt.createNode("/bar", new byte[0], Ids.OPEN_ACL_UNSAFE, ephemeralOwner, 0, 0, 0);
// close session
RequestHeader header = new RequestHeader();
header.setType(OpCode.closeSession);
final FinalRequestProcessor frq = new FinalRequestProcessor(zks);
final CountDownLatch latch = new CountDownLatch(1);
processor = new PrepRequestProcessor(zks, new RequestProcessor() {
@Override
public void processRequest(Request request) {
frq.processRequest(request);
latch.countDown();
}
@Override
public void shutdown() {
// TODO Auto-generated method stub
}
});
processor.pRequest(createRequest(header, OpCode.closeSession, ephemeralOwner));
assertTrue(latch.await(3, TimeUnit.SECONDS));
// assert ephemerals are deleted
assertEquals(null, dt.getNode("/foo"));
assertEquals(null, dt.getNode("/bar"));
} finally {
ZooKeeperServer.setCloseSessionTxnEnabled(before);
}
}
use of org.apache.zookeeper.proto.CreateRequest in project zookeeper by apache.
the class PrepRequestProcessorTest method testReconfigWithAnotherOutstandingChange.
@Test
public void testReconfigWithAnotherOutstandingChange() throws Exception {
QuorumPeerConfig.setReconfigEnabled(true);
QuorumPeerConfig.setStandaloneEnabled(false);
QuorumPeer qp = new QuorumPeer();
QuorumVerifier quorumVerifierMock = mock(QuorumVerifier.class);
when(quorumVerifierMock.getAllMembers()).thenReturn(LeaderBeanTest.getMockedPeerViews(qp.getId()));
qp.setQuorumVerifier(quorumVerifierMock, false);
FileTxnSnapLog snapLog = new FileTxnSnapLog(tmpDir, tmpDir);
LeaderZooKeeperServer lzks = new LeaderZooKeeperServer(snapLog, qp, new ZKDatabase(snapLog));
qp.leader = new Leader(qp, lzks);
lzks.sessionTracker = new MySessionTracker();
ZooKeeperServer.setDigestEnabled(true);
processor = new PrepRequestProcessor(lzks, new MyRequestProcessor());
Record record = new CreateRequest("/foo", "data".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT.toFlag());
pLatch = new CountDownLatch(1);
processor.pRequest(createRequest(record, OpCode.create, false));
assertTrue(pLatch.await(5, TimeUnit.SECONDS), "request hasn't been processed in chain");
String newMember = "server.0=localhost:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ":participant";
record = new ReconfigRequest(null, null, newMember, 0);
pLatch = new CountDownLatch(1);
processor.pRequest(createRequest(record, OpCode.reconfig, true));
assertTrue(pLatch.await(5, TimeUnit.SECONDS), "request hasn't been processed in chain");
// Verifies that there was no error.
assertEquals(outcome.getHdr().getType(), OpCode.reconfig);
}
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 ZooKeeper method create.
/**
* The asynchronous version of create.
*
* @see #create(String, byte[], List, CreateMode)
*/
public void create(final String path, byte[] data, List<ACL> acl, CreateMode createMode, StringCallback cb, Object ctx) {
final String clientPath = path;
PathUtils.validatePath(clientPath, createMode.isSequential());
EphemeralType.validateTTL(createMode, -1);
final String serverPath = prependChroot(clientPath);
RequestHeader h = new RequestHeader();
h.setType(createMode.isContainer() ? ZooDefs.OpCode.createContainer : ZooDefs.OpCode.create);
CreateRequest request = new CreateRequest();
CreateResponse response = new CreateResponse();
ReplyHeader r = new ReplyHeader();
request.setData(data);
request.setFlags(createMode.toFlag());
request.setPath(serverPath);
request.setAcl(acl);
cnxn.queuePacket(h, r, request, response, cb, clientPath, serverPath, ctx, null);
}
Aggregations