Example usage for org.apache.zookeeper.server DataTree DataTree

List of usage examples for org.apache.zookeeper.server DataTree DataTree

Introduction

In this page you can find the example usage for org.apache.zookeeper.server DataTree DataTree.

Prototype

public DataTree() 

Source Link

Usage

From source file:com.linkedin.helix.tools.ZKLogFormatter.java

License:Apache License

private static void readSnapshotLog(String snapshotPath) throws Exception {
    FileInputStream fis = new FileInputStream(snapshotPath);
    BinaryInputArchive ia = BinaryInputArchive.getArchive(fis);
    Map<Long, Integer> sessions = new HashMap<Long, Integer>();
    DataTree dt = new DataTree();
    FileHeader header = new FileHeader();
    header.deserialize(ia, "fileheader");
    if (header.getMagic() != FileSnap.SNAP_MAGIC) {
        throw new IOException("mismatching magic headers " + header.getMagic() + " !=  " + FileSnap.SNAP_MAGIC);
    }/*from  ww  w  . jav a 2 s.c o  m*/
    SerializeUtils.deserializeSnapshot(dt, ia, sessions);

    if (bw != null) {
        bw.write(sessions.toString());
        bw.newLine();
    } else {
        System.out.println(sessions);
    }
    traverse(dt, 1, "/");

}

From source file:com.zklogtool.reader.SnapshotFileReader.java

License:Apache License

/**
 * Reads snapshot file and returns <code>DataState</code> based on it.
 *
 * @return Returns <code>DataState</code> based only on snapshot file.
 * @throws CRCValidationException Thrown if CRC validation failed. Snapshot
 * is probably corrupted.// w ww  . j  a  va  2 s  .c  o  m
 * @throws IOException Thrown if there is a problem with reading snapshot
 * file.
 */
public DataState readFuzzySnapshot() throws CRCValidationException, IOException {

    DataTree dt = new DataTree();
    Map<Long, Integer> sessions = new HashMap<Long, Integer>();

    InputStream snapIS = null;
    CheckedInputStream crcIn = null;

    try {
        logger.info("Reading snapshot " + snapshotFile);
        snapIS = new BufferedInputStream(new FileInputStream(snapshotFile));
        crcIn = new CheckedInputStream(snapIS, new Adler32());
        InputArchive ia = getArchive(crcIn);

        FileHeader header = new FileHeader();
        header.deserialize(ia, "fileheader");
        if (header.getMagic() != SNAP_MAGIC) {
            throw new IOException("Mismatching magic headers " + header.getMagic() + " !=  " + SNAP_MAGIC);
        }

        int count = ia.readInt("count");

        while (count > 0) {
            long id = ia.readLong("id");
            int to = ia.readInt("timeout");
            sessions.put(id, to);
            count--;
        }

        dt.deserialize(ia, "tree");

        long checkSum = crcIn.getChecksum().getValue();
        long val = ia.readLong("val");
        if (val != checkSum) {
            throw new CRCValidationException("CRC corruption in snapshot");
        }

    } catch (IOException e) {
        logger.warn("Problem reading snap file " + snapshotFile, e);

        throw new IOException(e);

    } finally {
        if (snapIS != null) {
            snapIS.close();
        }
        if (crcIn != null) {
            crcIn.close();
        }
    }

    long lastZxid = -1;

    DataState dataState = new DataState(dt, sessions, lastZxid);

    Iterator<Entry<String, DataNode>> it = dataState.getNodes().entrySet().iterator();
    while (it.hasNext()) {

        Entry<String, DataNode> e = it.next();

        long tempZxid = e.getValue().stat.getCzxid();

        if (tempZxid > lastZxid) {
            lastZxid = tempZxid;
        }

        tempZxid = e.getValue().stat.getPzxid();
        if (tempZxid > lastZxid) {
            lastZxid = tempZxid;
        }

        tempZxid = e.getValue().stat.getMzxid();
        if (tempZxid > lastZxid) {
            lastZxid = tempZxid;
        }

    }

    dataState.setLastZxid(lastZxid);

    return dataState;

}

From source file:com.zklogtool.reader.SnapshotFileReader.java

License:Apache License

private DataState getDataState() {

    try {//from   w ww  .j  a v  a 2s .c om

        //create data tree
        DataTree dataTree = new DataTree();

        //inject logger
        Field field = dataTree.getClass().getDeclaredField("LOG");
        field.setAccessible(true);
        //field.set(TS, field);

        //create data state
        Constructor ctor = DataState.class.getDeclaredConstructors()[0];

        ctor.setAccessible(true);
        DataState dataState = (DataState) ctor.newInstance(null, null, null);

        return dataState;

    } catch (Exception x) {
        //
    }

    return null;

}