Search in sources :

Example 6 with InputArchive

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

the class CRCTest method getCheckSum.

/** return if checksum matches for a snapshot **/
private boolean getCheckSum(FileSnap snap, File snapFile) throws IOException {
    DataTree dt = new DataTree();
    Map<Long, Integer> sessions = new ConcurrentHashMap<Long, Integer>();
    InputStream snapIS = new BufferedInputStream(new FileInputStream(snapFile));
    CheckedInputStream crcIn = new CheckedInputStream(snapIS, new Adler32());
    InputArchive ia = BinaryInputArchive.getArchive(crcIn);
    try {
        snap.deserialize(dt, sessions, ia);
    } catch (IOException ie) {
        // we failed on the most recent snapshot
        // must be incomplete
        // try reading the next one
        // after corrupting
        snapIS.close();
        crcIn.close();
        throw ie;
    }
    long checksum = crcIn.getChecksum().getValue();
    long val = ia.readLong("val");
    snapIS.close();
    crcIn.close();
    return (val != checksum);
}
Also used : CheckedInputStream(java.util.zip.CheckedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) InputArchive(org.apache.jute.InputArchive) BinaryInputArchive(org.apache.jute.BinaryInputArchive) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) CheckedInputStream(java.util.zip.CheckedInputStream) Adler32(java.util.zip.Adler32) BufferedInputStream(java.io.BufferedInputStream) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 7 with InputArchive

use of org.apache.jute.InputArchive 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);
        Assert.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));
            Stat stat = new Stat();
            Assert.assertEquals("fpjwasalsohere", new String(zkDb.getData(path, stat, null)));
        }
        Assert.assertTrue(zxid > ZxidUtils.makeZxid(1, 0));
        // Generate snapshot and close files.
        snapLog.save(zkDb.getDataTree(), zkDb.getSessionWithTimeOuts());
        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, 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 : 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) File(java.io.File) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) TxnHeader(org.apache.zookeeper.txn.TxnHeader)

Example 8 with InputArchive

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

the class Zab1_0Test method testObserverConversation.

public void testObserverConversation(ObserverConversation conversation) throws Exception {
    File tmpDir = File.createTempFile("test", "dir", testData);
    tmpDir.delete();
    tmpDir.mkdir();
    Thread observerThread = null;
    ConversableObserver observer = null;
    QuorumPeer peer = null;
    try {
        peer = createQuorumPeer(tmpDir);
        peer.setSyncEnabled(true);
        observer = createObserver(tmpDir, peer);
        peer.observer = observer;
        ServerSocket ss = new ServerSocket(0, 50, InetAddress.getByName("127.0.0.1"));
        observer.setLeaderSocketAddress((InetSocketAddress) ss.getLocalSocketAddress());
        final Observer observerForThread = observer;
        observerThread = new Thread() {

            public void run() {
                try {
                    observerForThread.observeLeader();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        observerThread.start();
        Socket leaderSocket = ss.accept();
        InputArchive ia = BinaryInputArchive.getArchive(leaderSocket.getInputStream());
        OutputArchive oa = BinaryOutputArchive.getArchive(leaderSocket.getOutputStream());
        conversation.converseWithObserver(ia, oa, observer);
    } finally {
        if (observer != null) {
            observer.shutdown();
        }
        if (observerThread != null) {
            observerThread.interrupt();
            observerThread.join();
        }
        if (peer != null) {
            peer.shutdown();
        }
        TestUtils.deleteFileRecursively(tmpDir);
    }
}
Also used : BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) OutputArchive(org.apache.jute.OutputArchive) InputArchive(org.apache.jute.InputArchive) BinaryInputArchive(org.apache.jute.BinaryInputArchive) ServerSocket(java.net.ServerSocket) File(java.io.File) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket)

Example 9 with InputArchive

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

the class FileTxnLog method readHeader.

/**
     * read the header of the transaction file
     * @param file the transaction file to read
     * @return header that was read fomr the file
     * @throws IOException
     */
private static FileHeader readHeader(File file) throws IOException {
    InputStream is = null;
    try {
        is = new BufferedInputStream(new FileInputStream(file));
        InputArchive ia = BinaryInputArchive.getArchive(is);
        FileHeader hdr = new FileHeader();
        hdr.deserialize(ia, "fileheader");
        return hdr;
    } finally {
        try {
            if (is != null)
                is.close();
        } catch (IOException e) {
            LOG.warn("Ignoring exception during close", e);
        }
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FilterInputStream(java.io.FilterInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) InputArchive(org.apache.jute.InputArchive) BinaryInputArchive(org.apache.jute.BinaryInputArchive) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 10 with InputArchive

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

the class FileSnap method deserialize.

/**
     * deserialize a data tree from the most recent snapshot
     * @return the zxid of the snapshot
     */
public long deserialize(DataTree dt, Map<Long, Integer> sessions) throws IOException {
    // we run through 100 snapshots (not all of them)
    // if we cannot get it running within 100 snapshots
    // we should  give up
    List<File> snapList = findNValidSnapshots(100);
    if (snapList.size() == 0) {
        return -1L;
    }
    File snap = null;
    boolean foundValid = false;
    for (int i = 0; i < snapList.size(); i++) {
        snap = snapList.get(i);
        InputStream snapIS = null;
        CheckedInputStream crcIn = null;
        try {
            LOG.info("Reading snapshot " + snap);
            snapIS = new BufferedInputStream(new FileInputStream(snap));
            crcIn = new CheckedInputStream(snapIS, new Adler32());
            InputArchive ia = BinaryInputArchive.getArchive(crcIn);
            deserialize(dt, sessions, ia);
            long checkSum = crcIn.getChecksum().getValue();
            long val = ia.readLong("val");
            if (val != checkSum) {
                throw new IOException("CRC corruption in snapshot :  " + snap);
            }
            foundValid = true;
            break;
        } catch (IOException e) {
            LOG.warn("problem reading snap file " + snap, e);
        } finally {
            if (snapIS != null)
                snapIS.close();
            if (crcIn != null)
                crcIn.close();
        }
    }
    if (!foundValid) {
        throw new IOException("Not able to find valid snapshots in " + snapDir);
    }
    dt.lastProcessedZxid = Util.getZxidFromName(snap.getName(), "snapshot");
    return dt.lastProcessedZxid;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) CheckedInputStream(java.util.zip.CheckedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) InputArchive(org.apache.jute.InputArchive) BinaryInputArchive(org.apache.jute.BinaryInputArchive) IOException(java.io.IOException) File(java.io.File) CheckedInputStream(java.util.zip.CheckedInputStream) FileInputStream(java.io.FileInputStream) Adler32(java.util.zip.Adler32)

Aggregations

BinaryInputArchive (org.apache.jute.BinaryInputArchive)10 InputArchive (org.apache.jute.InputArchive)10 IOException (java.io.IOException)7 File (java.io.File)5 BufferedInputStream (java.io.BufferedInputStream)4 EOFException (java.io.EOFException)4 FileInputStream (java.io.FileInputStream)4 InputStream (java.io.InputStream)4 ServerSocket (java.net.ServerSocket)4 Socket (java.net.Socket)4 Adler32 (java.util.zip.Adler32)4 BinaryOutputArchive (org.apache.jute.BinaryOutputArchive)4 OutputArchive (org.apache.jute.OutputArchive)4 CheckedInputStream (java.util.zip.CheckedInputStream)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 Record (org.apache.jute.Record)2 CreateTxn (org.apache.zookeeper.txn.CreateTxn)2 TxnHeader (org.apache.zookeeper.txn.TxnHeader)2 FilterInputStream (java.io.FilterInputStream)1