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);
}
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());
}
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!");
}
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");
}
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");
}
Aggregations