Search in sources :

Example 11 with OutputArchive

use of org.apache.jute.OutputArchive in project zookeeper by apache.

the class Zab1_0Test method testPopulatedLeaderConversation.

public void testPopulatedLeaderConversation(PopulatedLeaderConversation conversation, int ops) throws Exception {
    Socket[] pair = getSocketPair();
    Socket leaderSocket = pair[0];
    Socket followerSocket = pair[1];
    File tmpDir = File.createTempFile("test", "dir", testData);
    tmpDir.delete();
    tmpDir.mkdir();
    LeadThread leadThread = null;
    Leader leader = null;
    try {
        // Setup a database with two znodes
        FileTxnSnapLog snapLog = new FileTxnSnapLog(tmpDir, tmpDir);
        ZKDatabase zkDb = new ZKDatabase(snapLog);
        assertTrue(ops >= 1);
        long zxid = ZxidUtils.makeZxid(1, 0);
        for (int i = 1; i <= ops; i++) {
            zxid = ZxidUtils.makeZxid(1, i);
            String path = "/foo-" + i;
            zkDb.processTxn(new TxnHeader(13, 1000 + i, zxid, 30 + i, ZooDefs.OpCode.create), new CreateTxn(path, "fpjwasalsohere".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, false, 1), null);
            Stat stat = new Stat();
            assertEquals("fpjwasalsohere", new String(zkDb.getData(path, stat, null)));
        }
        assertTrue(zxid > ZxidUtils.makeZxid(1, 0));
        // Generate snapshot and close files.
        snapLog.save(zkDb.getDataTree(), zkDb.getSessionWithTimeOuts(), false);
        snapLog.close();
        QuorumPeer peer = createQuorumPeer(tmpDir);
        leader = createLeader(tmpDir, peer);
        peer.leader = leader;
        // Set the last accepted epoch and current epochs to be 1
        peer.setAcceptedEpoch(1);
        peer.setCurrentEpoch(1);
        leadThread = new LeadThread(leader);
        leadThread.start();
        while (leader.cnxAcceptor == null || !leader.cnxAcceptor.isAlive()) {
            Thread.sleep(20);
        }
        LearnerHandler lh = new LearnerHandler(leaderSocket, new BufferedInputStream(leaderSocket.getInputStream()), leader);
        lh.start();
        leaderSocket.setSoTimeout(4000);
        InputArchive ia = BinaryInputArchive.getArchive(followerSocket.getInputStream());
        OutputArchive oa = BinaryOutputArchive.getArchive(followerSocket.getOutputStream());
        conversation.converseWithLeader(ia, oa, leader, zxid);
    } finally {
        if (leader != null) {
            leader.shutdown("end of test");
        }
        if (leadThread != null) {
            leadThread.interrupt();
            leadThread.join();
        }
        TestUtils.deleteFileRecursively(tmpDir);
    }
}
Also used : MockLeader(org.apache.zookeeper.server.quorum.ZabUtils.MockLeader) ZabUtils.createMockLeader(org.apache.zookeeper.server.quorum.ZabUtils.createMockLeader) ZabUtils.createLeader(org.apache.zookeeper.server.quorum.ZabUtils.createLeader) InputArchive(org.apache.jute.InputArchive) BinaryInputArchive(org.apache.jute.BinaryInputArchive) ZKDatabase(org.apache.zookeeper.server.ZKDatabase) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) CreateTxn(org.apache.zookeeper.txn.CreateTxn) Stat(org.apache.zookeeper.data.Stat) BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) OutputArchive(org.apache.jute.OutputArchive) BufferedInputStream(java.io.BufferedInputStream) ZabUtils.createQuorumPeer(org.apache.zookeeper.server.quorum.ZabUtils.createQuorumPeer) File(java.io.File) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) TxnHeader(org.apache.zookeeper.txn.TxnHeader)

Example 12 with OutputArchive

use of org.apache.jute.OutputArchive in project zookeeper by apache.

the class SerializeUtilsTest method testSerializeRequestWithTxn.

@Test
public void testSerializeRequestWithTxn() throws IOException {
    // Arrange
    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());
    Request request = new Request(1, 2, 3, header, txn, 4);
    // Act
    byte[] data = SerializeUtils.serializeRequest(request);
    // Assert
    assertNotNull(data);
    InOrder inOrder = inOrder(header, txn);
    inOrder.verify(header).serialize(any(OutputArchive.class), eq("hdr"));
    inOrder.verify(txn).serialize(any(OutputArchive.class), eq("txn"));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos);
    boa.writeString("header", "test");
    boa.writeString("record", "test");
    baos.close();
    assertArrayEquals(baos.toByteArray(), data);
}
Also used : BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) InOrder(org.mockito.InOrder) Request(org.apache.zookeeper.server.Request) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) OutputArchive(org.apache.jute.OutputArchive) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Record(org.apache.jute.Record) TxnHeader(org.apache.zookeeper.txn.TxnHeader) Test(org.junit.jupiter.api.Test)

Example 13 with OutputArchive

use of org.apache.jute.OutputArchive in project zookeeper by apache.

the class ReferenceCountedACLCacheTest method testNPEInDeserialize.

@Test
public void testNPEInDeserialize() throws IOException {
    ReferenceCountedACLCache serializeCache = new ReferenceCountedACLCache() {

        @Override
        public synchronized void serialize(OutputArchive oa) throws IOException {
            oa.writeInt(1, "map");
            oa.writeLong(1, "long");
            oa.startVector(null, "acls");
            oa.endVector(null, "acls");
        }
    };
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BinaryOutputArchive archive = BinaryOutputArchive.getArchive(baos);
    serializeCache.serialize(archive);
    BinaryInputArchive inArchive = BinaryInputArchive.getArchive(new ByteArrayInputStream(baos.toByteArray()));
    ReferenceCountedACLCache deserializedCache = new ReferenceCountedACLCache();
    try {
        deserializedCache.deserialize(inArchive);
    } catch (NullPointerException e) {
        fail("should not throw NPE while do deserialized");
    } catch (RuntimeException e) {
    // do nothing.
    }
}
Also used : BinaryInputArchive(org.apache.jute.BinaryInputArchive) BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) OutputArchive(org.apache.jute.OutputArchive) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 14 with OutputArchive

use of org.apache.jute.OutputArchive in project zookeeper by apache.

the class SnapStreamTest method testSerializeDeserialize.

private void testSerializeDeserialize(StreamMode mode, String fileSuffix, boolean fsync) throws IOException {
    SnapStream.setStreamMode(mode);
    // serialize with gzip stream
    File tmpDir = createTmpDir();
    File file = new File(tmpDir, "snapshot.180000e3a2" + fileSuffix);
    CheckedOutputStream os = SnapStream.getOutputStream(file, fsync);
    OutputArchive oa = BinaryOutputArchive.getArchive(os);
    FileHeader header = new FileHeader(FileSnap.SNAP_MAGIC, 2, 1);
    header.serialize(oa, "fileheader");
    SnapStream.sealStream(os, oa);
    os.flush();
    os.close();
    assertTrue(SnapStream.isValidSnapshot(file));
    // deserialize with gzip stream
    CheckedInputStream is = SnapStream.getInputStream(file);
    InputArchive ia = BinaryInputArchive.getArchive(is);
    FileHeader restoredHeader = new FileHeader();
    restoredHeader.deserialize(ia, "fileheader");
    assertEquals(restoredHeader, header, "magic not the same");
    SnapStream.checkSealIntegrity(is, ia);
}
Also used : BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) OutputArchive(org.apache.jute.OutputArchive) InputArchive(org.apache.jute.InputArchive) BinaryInputArchive(org.apache.jute.BinaryInputArchive) CheckedOutputStream(java.util.zip.CheckedOutputStream) File(java.io.File) CheckedInputStream(java.util.zip.CheckedInputStream)

Example 15 with OutputArchive

use of org.apache.jute.OutputArchive in project zookeeper by apache.

the class FileTxnSnapLogTest method testACLCreatedDuringFuzzySnapshotSync.

/**
 * Make sure the ACL is exist in the ACL map after SNAP syncing.
 *
 * ZooKeeper uses ACL reference id and count to save the space in snapshot.
 * During fuzzy snapshot sync, the reference count may not be updated
 * correctly in case like the znode is already exist.
 *
 * When ACL reference count reaches 0, it will be deleted from the cache,
 * but actually there might be other nodes still using it. When visiting
 * a node with the deleted ACL id, it will be rejected because it doesn't
 * exist anymore.
 *
 * Here is the detailed flow for one of the scenario here:
 *   1. Server A starts to have snap sync with leader
 *   2. After serializing the ACL map to Server A, there is a txn T1 to
 *      create a node N1 with new ACL_1 which was not exist in ACL map
 *   3. On leader, after this txn, the ACL map will be ID1 -&gt; (ACL_1, COUNT: 1),
 *      and data tree N1 -&gt; ID1
 *   4. On server A, it will be empty ACL map, and N1 -&gt; ID1 in fuzzy snapshot
 *   5. When replaying the txn T1, it will skip at the beginning since the
 *      node is already exist, which leaves an empty ACL map, and N1 is
 *      referencing to a non-exist ACL ID1
 *   6. Node N1 will be not accessible because the ACL not exist, and if it
 *      became leader later then all the write requests will be rejected as
 *      well with marshalling error.
 */
@Test
public void testACLCreatedDuringFuzzySnapshotSync() throws IOException {
    DataTree leaderDataTree = new DataTree();
    // Start the simulated snap-sync by serializing ACL cache.
    File file = File.createTempFile("snapshot", "zk");
    FileOutputStream os = new FileOutputStream(file);
    OutputArchive oa = BinaryOutputArchive.getArchive(os);
    leaderDataTree.serializeAcls(oa);
    // Add couple of transaction in-between.
    TxnHeader hdr1 = new TxnHeader(1, 2, 2, 2, ZooDefs.OpCode.create);
    Record txn1 = new CreateTxn("/a1", "foo".getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL, false, -1);
    leaderDataTree.processTxn(hdr1, txn1);
    // Finish the snapshot.
    leaderDataTree.serializeNodes(oa);
    os.close();
    // Simulate restore on follower and replay.
    FileInputStream is = new FileInputStream(file);
    InputArchive ia = BinaryInputArchive.getArchive(is);
    DataTree followerDataTree = new DataTree();
    followerDataTree.deserialize(ia, "tree");
    followerDataTree.processTxn(hdr1, txn1);
    DataNode a1 = leaderDataTree.getNode("/a1");
    assertNotNull(a1);
    assertEquals(ZooDefs.Ids.CREATOR_ALL_ACL, leaderDataTree.getACL(a1));
    assertEquals(ZooDefs.Ids.CREATOR_ALL_ACL, followerDataTree.getACL(a1));
}
Also used : CreateTxn(org.apache.zookeeper.txn.CreateTxn) BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) OutputArchive(org.apache.jute.OutputArchive) DataTree(org.apache.zookeeper.server.DataTree) InputArchive(org.apache.jute.InputArchive) BinaryInputArchive(org.apache.jute.BinaryInputArchive) DataNode(org.apache.zookeeper.server.DataNode) FileOutputStream(java.io.FileOutputStream) Record(org.apache.jute.Record) File(java.io.File) FileInputStream(java.io.FileInputStream) TxnHeader(org.apache.zookeeper.txn.TxnHeader) Test(org.junit.jupiter.api.Test)

Aggregations

OutputArchive (org.apache.jute.OutputArchive)16 BinaryOutputArchive (org.apache.jute.BinaryOutputArchive)14 BinaryInputArchive (org.apache.jute.BinaryInputArchive)7 File (java.io.File)6 InputArchive (org.apache.jute.InputArchive)6 TxnHeader (org.apache.zookeeper.txn.TxnHeader)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 ServerSocket (java.net.ServerSocket)4 Socket (java.net.Socket)4 CheckedOutputStream (java.util.zip.CheckedOutputStream)4 ZabUtils.createQuorumPeer (org.apache.zookeeper.server.quorum.ZabUtils.createQuorumPeer)4 Test (org.junit.jupiter.api.Test)4 Mockito.doAnswer (org.mockito.Mockito.doAnswer)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 Answer (org.mockito.stubbing.Answer)4 IOException (java.io.IOException)3 Record (org.apache.jute.Record)3 Request (org.apache.zookeeper.server.Request)3 BufferedInputStream (java.io.BufferedInputStream)2 EOFException (java.io.EOFException)2