Search in sources :

Example 26 with CreateRequest

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);
}
Also used : BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) CreateRequest(org.apache.zookeeper.proto.CreateRequest) SetDataRequest(org.apache.zookeeper.proto.SetDataRequest) ReconfigRequest(org.apache.zookeeper.proto.ReconfigRequest) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Id(org.apache.zookeeper.data.Id)

Example 27 with CreateRequest

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);
    }
}
Also used : CreateRequest(org.apache.zookeeper.proto.CreateRequest) SetDataRequest(org.apache.zookeeper.proto.SetDataRequest) ReconfigRequest(org.apache.zookeeper.proto.ReconfigRequest) RequestHeader(org.apache.zookeeper.proto.RequestHeader) CountDownLatch(java.util.concurrent.CountDownLatch) LeaderBeanTest(org.apache.zookeeper.server.quorum.LeaderBeanTest) Test(org.junit.jupiter.api.Test)

Example 28 with CreateRequest

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);
}
Also used : Leader(org.apache.zookeeper.server.quorum.Leader) CreateRequest(org.apache.zookeeper.proto.CreateRequest) CountDownLatch(java.util.concurrent.CountDownLatch) QuorumVerifier(org.apache.zookeeper.server.quorum.flexible.QuorumVerifier) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) ReconfigRequest(org.apache.zookeeper.proto.ReconfigRequest) QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) Record(org.apache.jute.Record) ChangeRecord(org.apache.zookeeper.server.ZooKeeperServer.ChangeRecord) MultiOperationRecord(org.apache.zookeeper.MultiOperationRecord) LeaderZooKeeperServer(org.apache.zookeeper.server.quorum.LeaderZooKeeperServer) LeaderBeanTest(org.apache.zookeeper.server.quorum.LeaderBeanTest) Test(org.junit.jupiter.api.Test)

Example 29 with CreateRequest

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>());
}
Also used : BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) CreateRequest(org.apache.zookeeper.proto.CreateRequest) GetDataRequest(org.apache.zookeeper.proto.GetDataRequest) CreateRequest(org.apache.zookeeper.proto.CreateRequest) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Id(org.apache.zookeeper.data.Id) ByteBuffer(java.nio.ByteBuffer)

Example 30 with CreateRequest

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);
}
Also used : ReplyHeader(org.apache.zookeeper.proto.ReplyHeader) CreateRequest(org.apache.zookeeper.proto.CreateRequest) CreateResponse(org.apache.zookeeper.proto.CreateResponse) RequestHeader(org.apache.zookeeper.proto.RequestHeader)

Aggregations

CreateRequest (org.apache.zookeeper.proto.CreateRequest)38 SetDataRequest (org.apache.zookeeper.proto.SetDataRequest)21 Test (org.junit.jupiter.api.Test)14 GetDataRequest (org.apache.zookeeper.proto.GetDataRequest)11 Request (org.apache.zookeeper.server.Request)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 BinaryOutputArchive (org.apache.jute.BinaryOutputArchive)9 DeleteRequest (org.apache.zookeeper.proto.DeleteRequest)9 ByteBuffer (java.nio.ByteBuffer)7 HashSet (java.util.HashSet)7 KeeperException (org.apache.zookeeper.KeeperException)7 ReconfigRequest (org.apache.zookeeper.proto.ReconfigRequest)7 SetACLRequest (org.apache.zookeeper.proto.SetACLRequest)7 ChangeRecord (org.apache.zookeeper.server.ZooKeeperServer.ChangeRecord)7 Record (org.apache.jute.Record)6 Op (org.apache.zookeeper.Op)6 CheckVersionRequest (org.apache.zookeeper.proto.CheckVersionRequest)6 TxnHeader (org.apache.zookeeper.txn.TxnHeader)6 MultiOperationRecord (org.apache.zookeeper.MultiOperationRecord)5 Id (org.apache.zookeeper.data.Id)5