Search in sources :

Example 1 with OutputArchive

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

the class FileSnap method serialize.

/**
     * serialize the datatree and session into the file snapshot
     * @param dt the datatree to be serialized
     * @param sessions the sessions to be serialized
     * @param snapShot the file to store snapshot into
     */
public synchronized void serialize(DataTree dt, Map<Long, Integer> sessions, File snapShot) throws IOException {
    if (!close) {
        OutputStream sessOS = new BufferedOutputStream(new FileOutputStream(snapShot));
        CheckedOutputStream crcOut = new CheckedOutputStream(sessOS, new Adler32());
        //CheckedOutputStream cout = new CheckedOutputStream()
        OutputArchive oa = BinaryOutputArchive.getArchive(crcOut);
        FileHeader header = new FileHeader(SNAP_MAGIC, VERSION, dbId);
        serialize(dt, sessions, oa, header);
        long val = crcOut.getChecksum().getValue();
        oa.writeLong(val, "val");
        oa.writeString("/", "path");
        sessOS.flush();
        crcOut.close();
        sessOS.close();
    }
}
Also used : BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) OutputArchive(org.apache.jute.OutputArchive) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) CheckedOutputStream(java.util.zip.CheckedOutputStream) FileOutputStream(java.io.FileOutputStream) CheckedOutputStream(java.util.zip.CheckedOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) Adler32(java.util.zip.Adler32)

Example 2 with OutputArchive

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

the class FileSnap method serialize.

/**
 * serialize the datatree and session into the file snapshot
 * @param dt the datatree to be serialized
 * @param sessions the sessions to be serialized
 * @param snapShot the file to store snapshot into
 * @param fsync sync the file immediately after write
 */
public synchronized void serialize(DataTree dt, Map<Long, Integer> sessions, File snapShot, boolean fsync) throws IOException {
    if (!close) {
        try (CheckedOutputStream snapOS = SnapStream.getOutputStream(snapShot, fsync)) {
            OutputArchive oa = BinaryOutputArchive.getArchive(snapOS);
            FileHeader header = new FileHeader(SNAP_MAGIC, VERSION, dbId);
            serialize(dt, sessions, oa, header);
            SnapStream.sealStream(snapOS, oa);
            // CRC check.
            if (dt.serializeZxidDigest(oa)) {
                SnapStream.sealStream(snapOS, oa);
            }
            lastSnapshotInfo = new SnapshotInfo(Util.getZxidFromName(snapShot.getName(), SNAPSHOT_FILE_PREFIX), snapShot.lastModified() / 1000);
        }
    } else {
        throw new IOException("FileSnap has already been closed");
    }
}
Also used : BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) OutputArchive(org.apache.jute.OutputArchive) CheckedOutputStream(java.util.zip.CheckedOutputStream) IOException(java.io.IOException)

Example 3 with OutputArchive

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

the class Util method marshallTxnEntry.

public static byte[] marshallTxnEntry(TxnHeader hdr, Record txn, TxnDigest digest) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    OutputArchive boa = BinaryOutputArchive.getArchive(baos);
    hdr.serialize(boa, "hdr");
    if (txn != null) {
        txn.serialize(boa, "txn");
    }
    if (digest != null) {
        digest.serialize(boa, "digest");
    }
    return baos.toByteArray();
}
Also used : BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) OutputArchive(org.apache.jute.OutputArchive) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 4 with OutputArchive

use of org.apache.jute.OutputArchive 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 5 with OutputArchive

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

the class PurgeTxnTest method makeValidSnapshot.

private void makeValidSnapshot(File snapFile) throws IOException {
    SnapStream.setStreamMode(SnapStream.StreamMode.CHECKED);
    CheckedOutputStream os = SnapStream.getOutputStream(snapFile, true);
    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(snapFile));
}
Also used : BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) OutputArchive(org.apache.jute.OutputArchive) CheckedOutputStream(java.util.zip.CheckedOutputStream) FileHeader(org.apache.zookeeper.server.persistence.FileHeader)

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