Search in sources :

Example 11 with BinaryOutputArchive

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;
}
Also used : BinaryInputArchive(org.apache.jute.BinaryInputArchive) BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) ByteBufferInputStream(org.apache.zookeeper.server.ByteBufferInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer)

Example 12 with BinaryOutputArchive

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);
    }
}
Also used : BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) ACL(org.apache.zookeeper.data.ACL) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) CreateTxn(org.apache.zookeeper.txn.CreateTxn) ByteArrayInputStream(java.io.ByteArrayInputStream) EOFException(java.io.EOFException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) Socket(java.net.Socket) TxnHeader(org.apache.zookeeper.txn.TxnHeader) Test(org.junit.Test)

Example 13 with BinaryOutputArchive

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);
}
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)

Example 14 with BinaryOutputArchive

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);
}
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.Test)

Example 15 with BinaryOutputArchive

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

Aggregations

BinaryOutputArchive (org.apache.jute.BinaryOutputArchive)24 ByteArrayOutputStream (java.io.ByteArrayOutputStream)22 ByteBuffer (java.nio.ByteBuffer)11 IOException (java.io.IOException)8 Test (org.junit.Test)7 BinaryInputArchive (org.apache.jute.BinaryInputArchive)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 CreateRequest (org.apache.zookeeper.proto.CreateRequest)5 Record (org.apache.jute.Record)4 Id (org.apache.zookeeper.data.Id)4 TxnHeader (org.apache.zookeeper.txn.TxnHeader)4 ZooKeeper (org.apache.zookeeper.ZooKeeper)3 ACL (org.apache.zookeeper.data.ACL)3 SetDataRequest (org.apache.zookeeper.proto.SetDataRequest)3 Request (org.apache.zookeeper.server.Request)3 CreateTxn (org.apache.zookeeper.txn.CreateTxn)3 Socket (java.net.Socket)2 ArrayList (java.util.ArrayList)2 KeeperException (org.apache.zookeeper.KeeperException)2 Stat (org.apache.zookeeper.data.Stat)2