use of org.apache.jute.BinaryOutputArchive in project zookeeper by apache.
the class MultiResponseTest method codeDecode.
private MultiResponse codeDecode(MultiResponse request) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos);
request.serialize(boa, "result");
baos.close();
ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray());
bb.rewind();
BinaryInputArchive bia = BinaryInputArchive.getArchive(new ByteBufferInputStream(bb));
MultiResponse decodedRequest = new MultiResponse();
decodedRequest.deserialize(bia, "result");
return decodedRequest;
}
use of org.apache.jute.BinaryOutputArchive in project zookeeper by apache.
the class LearnerTest method syncTest.
@Test
public void syncTest() throws Exception {
File tmpFile = File.createTempFile("test", ".dir", testData);
tmpFile.delete();
try {
FileTxnSnapLog ftsl = new FileTxnSnapLog(tmpFile, tmpFile);
SimpleLearner sl = new SimpleLearner(ftsl);
long startZxid = sl.zk.getLastProcessedZxid();
// Set up bogus streams
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BinaryOutputArchive oa = BinaryOutputArchive.getArchive(baos);
sl.leaderOs = BinaryOutputArchive.getArchive(new ByteArrayOutputStream());
// make streams and socket do something innocuous
sl.bufferedOutput = new BufferedOutputStream(System.out);
sl.sock = new Socket();
// fake messages from the server
QuorumPacket qp = new QuorumPacket(Leader.SNAP, 0, null, null);
oa.writeRecord(qp, null);
sl.zk.getZKDatabase().serializeSnapshot(oa);
oa.writeString("BenWasHere", "signature");
TxnHeader hdr = new TxnHeader(0, 0, 0, 0, ZooDefs.OpCode.create);
CreateTxn txn = new CreateTxn("/foo", new byte[0], new ArrayList<ACL>(), false, sl.zk.getZKDatabase().getNode("/").stat.getCversion());
ByteArrayOutputStream tbaos = new ByteArrayOutputStream();
BinaryOutputArchive boa = BinaryOutputArchive.getArchive(tbaos);
hdr.serialize(boa, "hdr");
txn.serialize(boa, "txn");
tbaos.close();
qp = new QuorumPacket(Leader.PROPOSAL, 1, tbaos.toByteArray(), null);
oa.writeRecord(qp, null);
// setup the messages to be streamed to follower
sl.leaderIs = BinaryInputArchive.getArchive(new ByteArrayInputStream(baos.toByteArray()));
try {
sl.syncWithLeader(3);
} catch (EOFException e) {
}
sl.zk.shutdown();
sl = new SimpleLearner(ftsl);
Assert.assertEquals(startZxid, sl.zk.getLastProcessedZxid());
} finally {
TestUtils.deleteFileRecursively(tmpFile);
}
}
use of org.apache.jute.BinaryOutputArchive 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);
Assert.assertEquals("Node from fake session get created", null, stat);
}
use of org.apache.jute.BinaryOutputArchive 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);
Assert.assertTrue("Request from local sesson failed", stat != null);
}
use of org.apache.jute.BinaryOutputArchive in project zookeeper by apache.
the class PrepRequestProcessorTest method createRequest.
private Request createRequest(Record record, int opCode) throws IOException {
// encoding
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos);
record.serialize(boa, "request");
baos.close();
// Id
List<Id> ids = Arrays.asList(Ids.ANYONE_ID_UNSAFE);
return new Request(null, 1l, 0, opCode, ByteBuffer.wrap(baos.toByteArray()), ids);
}
Aggregations