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