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);
}
}
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);
}
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.
}
}
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);
}
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 -> (ACL_1, COUNT: 1),
* and data tree N1 -> ID1
* 4. On server A, it will be empty ACL map, and N1 -> 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));
}
Aggregations