Search in sources :

Example 11 with Request

use of org.apache.zookeeper.server.Request in project zookeeper by apache.

the class LeaderBeanTest method createMockRequest.

private Request createMockRequest() throws IOException {
    TxnHeader header = mock(TxnHeader.class);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            OutputArchive oa = (OutputArchive) args[0];
            oa.writeString("header", "test");
            return null;
        }
    }).when(header).serialize(any(OutputArchive.class), anyString());
    Record txn = mock(Record.class);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            OutputArchive oa = (OutputArchive) args[0];
            oa.writeString("record", "test");
            return null;
        }
    }).when(txn).serialize(any(OutputArchive.class), anyString());
    return new Request(1, 2, 3, header, txn, 4);
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) OutputArchive(org.apache.jute.OutputArchive) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Request(org.apache.zookeeper.server.Request) Record(org.apache.jute.Record) TxnHeader(org.apache.zookeeper.txn.TxnHeader)

Example 12 with Request

use of org.apache.zookeeper.server.Request in project zookeeper by apache.

the class LeaderBeanTest method testResetProposalStats.

@Test
public void testResetProposalStats() throws IOException, Leader.XidRolloverException {
    // Arrange
    int initialProposalSize = leaderBean.getLastProposalSize();
    Request req = createMockRequest();
    // Act
    leader.propose(req);
    // Assert
    assertNotEquals(initialProposalSize, leaderBean.getLastProposalSize());
    leaderBean.resetProposalStatistics();
    assertEquals(initialProposalSize, leaderBean.getLastProposalSize());
    assertEquals(initialProposalSize, leaderBean.getMinProposalSize());
    assertEquals(initialProposalSize, leaderBean.getMaxProposalSize());
}
Also used : Request(org.apache.zookeeper.server.Request) Test(org.junit.jupiter.api.Test)

Example 13 with Request

use of org.apache.zookeeper.server.Request in project zookeeper by apache.

the class ReadOnlyRequestProcessor method run.

public void run() {
    try {
        while (!finished) {
            Request request = queuedRequests.take();
            // log request
            if (LOG.isTraceEnabled()) {
                long traceMask = ZooTrace.CLIENT_REQUEST_TRACE_MASK;
                if (request.type == OpCode.ping) {
                    traceMask = ZooTrace.CLIENT_PING_TRACE_MASK;
                }
                ZooTrace.logRequest(LOG, traceMask, 'R', request, "");
            }
            if (Request.requestOfDeath == request) {
                break;
            }
            // filter read requests
            switch(request.type) {
                case OpCode.sync:
                case OpCode.create:
                case OpCode.create2:
                case OpCode.createTTL:
                case OpCode.createContainer:
                case OpCode.delete:
                case OpCode.deleteContainer:
                case OpCode.setData:
                case OpCode.reconfig:
                case OpCode.setACL:
                case OpCode.multi:
                case OpCode.check:
                    sendErrorResponse(request);
                    continue;
                case OpCode.closeSession:
                case OpCode.createSession:
                    if (!request.isLocalSession()) {
                        sendErrorResponse(request);
                        continue;
                    }
            }
            // proceed to the next processor
            if (nextProcessor != null) {
                nextProcessor.processRequest(request);
            }
        }
    } catch (Exception e) {
        handleException(this.getName(), e);
    }
    LOG.info("ReadOnlyRequestProcessor exited loop!");
}
Also used : Request(org.apache.zookeeper.server.Request) IOException(java.io.IOException)

Example 14 with Request

use of org.apache.zookeeper.server.Request in project zookeeper by apache.

the class LeaderSessionTrackerTest method testCreatePersistent.

/**
 * When local session is enabled, leader will allow persistent node
 * to be create for unknown session
 */
@Test
public void testCreatePersistent() throws Exception {
    qu.enableLocalSession(true);
    qu.startAll();
    QuorumPeer leader = qu.getLeaderQuorumPeer();
    ZooKeeper zk = ClientBase.createZKClient(qu.getConnectString(leader));
    CreateRequest createRequest = new CreateRequest("/success", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT.toFlag());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos);
    createRequest.serialize(boa, "request");
    ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray());
    // Mimic sessionId generated by follower's local session tracker
    long sid = qu.getFollowerQuorumPeers().get(0).getActiveServer().getServerId();
    long locallSession = (sid << 56) + 1;
    LOG.info("Local session Id: {}", Long.toHexString(locallSession));
    Request request = new Request(null, locallSession, 0, OpCode.create, bb, new ArrayList<Id>());
    // Submit request directly to leader
    leader.getActiveServer().submitRequest(request);
    // Make sure that previous request is finished
    zk.create("/ok", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    Stat stat = zk.exists("/success", null);
    assertTrue(stat != null, "Request from local sesson failed");
}
Also used : BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) ZooKeeper(org.apache.zookeeper.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) CreateRequest(org.apache.zookeeper.proto.CreateRequest) QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) CreateRequest(org.apache.zookeeper.proto.CreateRequest) Request(org.apache.zookeeper.server.Request) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Id(org.apache.zookeeper.data.Id) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 15 with Request

use of org.apache.zookeeper.server.Request in project zookeeper by apache.

the class LeaderSessionTrackerTest method testCreateEphemeral.

/**
 * When we create ephemeral node, we need to check against global
 * session, so the leader never accept request from an expired session
 * (that we no longer track)
 *
 * This is not the same as SessionInvalidationTest since session
 * is not in closing state
 */
public void testCreateEphemeral(boolean localSessionEnabled) throws Exception {
    if (localSessionEnabled) {
        qu.enableLocalSession(true);
    }
    qu.startAll();
    QuorumPeer leader = qu.getLeaderQuorumPeer();
    ZooKeeper zk = ClientBase.createZKClient(qu.getConnectString(leader));
    CreateRequest createRequest = new CreateRequest("/impossible", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL.toFlag());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos);
    createRequest.serialize(boa, "request");
    ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray());
    // Mimic sessionId generated by follower's local session tracker
    long sid = qu.getFollowerQuorumPeers().get(0).getActiveServer().getServerId();
    long fakeSessionId = (sid << 56) + 1;
    LOG.info("Fake session Id: {}", Long.toHexString(fakeSessionId));
    Request request = new Request(null, fakeSessionId, 0, OpCode.create, bb, new ArrayList<Id>());
    // Submit request directly to leader
    leader.getActiveServer().submitRequest(request);
    // Make sure that previous request is finished
    zk.create("/ok", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    Stat stat = zk.exists("/impossible", null);
    assertEquals(null, stat, "Node from fake session get created");
}
Also used : BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) ZooKeeper(org.apache.zookeeper.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) CreateRequest(org.apache.zookeeper.proto.CreateRequest) QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) CreateRequest(org.apache.zookeeper.proto.CreateRequest) Request(org.apache.zookeeper.server.Request) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Id(org.apache.zookeeper.data.Id) ByteBuffer(java.nio.ByteBuffer)

Aggregations

Request (org.apache.zookeeper.server.Request)51 Test (org.junit.jupiter.api.Test)25 CreateRequest (org.apache.zookeeper.proto.CreateRequest)15 IOException (java.io.IOException)9 ByteBuffer (java.nio.ByteBuffer)9 GetDataRequest (org.apache.zookeeper.proto.GetDataRequest)9 SetDataRequest (org.apache.zookeeper.proto.SetDataRequest)9 TxnHeader (org.apache.zookeeper.txn.TxnHeader)9 HashSet (java.util.HashSet)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 BinaryOutputArchive (org.apache.jute.BinaryOutputArchive)6 Record (org.apache.jute.Record)6 KeeperException (org.apache.zookeeper.KeeperException)5 SetDataTxn (org.apache.zookeeper.txn.SetDataTxn)5 Id (org.apache.zookeeper.data.Id)4 ErrorTxn (org.apache.zookeeper.txn.ErrorTxn)4 OutputArchive (org.apache.jute.OutputArchive)3 ZooKeeper (org.apache.zookeeper.ZooKeeper)3 TxnLogEntry (org.apache.zookeeper.server.TxnLogEntry)3 QuorumVerifier (org.apache.zookeeper.server.quorum.flexible.QuorumVerifier)3