本文整理汇总了Java中org.apache.zookeeper.server.util.ZxidUtils.makeZxid方法的典型用法代码示例。如果您正苦于以下问题:Java ZxidUtils.makeZxid方法的具体用法?Java ZxidUtils.makeZxid怎么用?Java ZxidUtils.makeZxid使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.zookeeper.server.util.ZxidUtils
的用法示例。
在下文中一共展示了ZxidUtils.makeZxid方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addRequestToSyncProcessor
import org.apache.zookeeper.server.util.ZxidUtils; //导入方法依赖的package包/类
private void addRequestToSyncProcessor() {
long zxid = ZxidUtils.makeZxid(3, 7);
TxnHeader hdr = new TxnHeader(1, 1, zxid, 1,
ZooDefs.OpCode.setData);
Record txn = new SetDataTxn("/foo" + zxid, new byte[0], 1);
byte[] buf;
try {
buf = Util.marshallTxnEntry(hdr, txn);
} catch (IOException e) {
LOG.error("IOException while adding request to SyncRequestProcessor", e);
Assert.fail("IOException while adding request to SyncRequestProcessor!");
return;
}
NettyServerCnxnFactory factory = new NettyServerCnxnFactory();
final MockNettyServerCnxn nettyCnxn = new MockNettyServerCnxn(null,
this, factory);
Request req = new Request(nettyCnxn, 1, 1, ZooDefs.OpCode.setData,
ByteBuffer.wrap(buf), null);
req.hdr = hdr;
req.txn = txn;
syncProcessor.processRequest(req);
}
示例2: testInitialAcceptedCurrent
import org.apache.zookeeper.server.util.ZxidUtils; //导入方法依赖的package包/类
@Test
public void testInitialAcceptedCurrent() throws Exception {
File tmpDir = File.createTempFile("test", ".dir", testData);
tmpDir.delete();
tmpDir.mkdir();
try {
FileTxnSnapLog logFactory = new FileTxnSnapLog(tmpDir, tmpDir);
File version2 = new File(tmpDir, "version-2");
version2.mkdir();
long zxid = ZxidUtils.makeZxid(3, 3);
TxnHeader hdr = new TxnHeader(1, 1, zxid, 1, ZooDefs.OpCode.error);
ErrorTxn txn = new ErrorTxn(1);
byte[] buf = Util.marshallTxnEntry(hdr, txn);
Request req = new Request(null, 1, 1, ZooDefs.OpCode.error,
ByteBuffer.wrap(buf), null);
req.hdr = hdr;
req.txn = txn;
logFactory.append(req);
logFactory.commit();
ZKDatabase zkDb = new ZKDatabase(logFactory);
QuorumPeer peer = new QuorumPeer();
peer.setZKDatabase(zkDb);
peer.setTxnFactory(logFactory);
peer.getLastLoggedZxid();
Assert.assertEquals(3, peer.getAcceptedEpoch());
Assert.assertEquals(3, peer.getCurrentEpoch());
Assert.assertEquals(3, Integer
.parseInt(readContentsOfFile(new File(version2,
QuorumPeer.CURRENT_EPOCH_FILENAME))));
Assert.assertEquals(3, Integer
.parseInt(readContentsOfFile(new File(version2,
QuorumPeer.ACCEPTED_EPOCH_FILENAME))));
} finally {
recursiveDelete(tmpDir);
}
}
示例3: testInitialAcceptedCurrent
import org.apache.zookeeper.server.util.ZxidUtils; //导入方法依赖的package包/类
@Test
public void testInitialAcceptedCurrent() throws Exception {
File tmpDir = File.createTempFile("test", ".dir", testData);
tmpDir.delete();
tmpDir.mkdir();
try {
FileTxnSnapLog logFactory = new FileTxnSnapLog(tmpDir, tmpDir);
File version2 = new File(tmpDir, "version-2");
version2.mkdir();
logFactory.save(new DataTree(), new ConcurrentHashMap<Long, Integer>(), false);
long zxid = ZxidUtils.makeZxid(3, 3);
logFactory.append(new Request(1, 1, ZooDefs.OpCode.error,
new TxnHeader(1, 1, zxid, 1, ZooDefs.OpCode.error),
new ErrorTxn(1), zxid));
logFactory.commit();
ZKDatabase zkDb = new ZKDatabase(logFactory);
QuorumPeer peer = new QuorumPeer();
peer.setZKDatabase(zkDb);
peer.setTxnFactory(logFactory);
peer.getLastLoggedZxid();
Assert.assertEquals(3, peer.getAcceptedEpoch());
Assert.assertEquals(3, peer.getCurrentEpoch());
Assert.assertEquals(3, Integer
.parseInt(readContentsOfFile(new File(version2,
QuorumPeer.CURRENT_EPOCH_FILENAME))));
Assert.assertEquals(3, Integer
.parseInt(readContentsOfFile(new File(version2,
QuorumPeer.ACCEPTED_EPOCH_FILENAME))));
} finally {
TestUtils.deleteFileRecursively(tmpDir);
}
}
示例4: testInitialAcceptedCurrent
import org.apache.zookeeper.server.util.ZxidUtils; //导入方法依赖的package包/类
@Test
public void testInitialAcceptedCurrent() throws Exception {
File tmpDir = File.createTempFile("test", ".dir", testData);
tmpDir.delete();
tmpDir.mkdir();
try {
FileTxnSnapLog logFactory = new FileTxnSnapLog(tmpDir, tmpDir);
File version2 = new File(tmpDir, "version-2");
version2.mkdir();
long zxid = ZxidUtils.makeZxid(3, 3);
TxnHeader hdr = new TxnHeader(1, 1, zxid, 1, ZooDefs.OpCode.error);
ErrorTxn txn = new ErrorTxn(1);
byte[] buf = Util.marshallTxnEntry(hdr, txn);
Request req = new Request(null, 1, 1, ZooDefs.OpCode.error,
ByteBuffer.wrap(buf), null);
req.hdr = hdr;
req.txn = txn;
logFactory.append(req);
logFactory.commit();
ZKDatabase zkDb = new ZKDatabase(logFactory);
QuorumPeer peer = QuorumPeer.testingQuorumPeer();
peer.setZKDatabase(zkDb);
peer.setTxnFactory(logFactory);
peer.getLastLoggedZxid();
Assert.assertEquals(3, peer.getAcceptedEpoch());
Assert.assertEquals(3, peer.getCurrentEpoch());
Assert.assertEquals(3, Integer
.parseInt(readContentsOfFile(new File(version2,
QuorumPeer.CURRENT_EPOCH_FILENAME))));
Assert.assertEquals(3, Integer
.parseInt(readContentsOfFile(new File(version2,
QuorumPeer.ACCEPTED_EPOCH_FILENAME))));
} finally {
recursiveDelete(tmpDir);
}
}
示例5: testInitialAcceptedCurrent
import org.apache.zookeeper.server.util.ZxidUtils; //导入方法依赖的package包/类
@Test
public void testInitialAcceptedCurrent() throws Exception {
File tmpDir = File.createTempFile("test", ".dir", testData);
tmpDir.delete();
tmpDir.mkdir();
try {
FileTxnSnapLog logFactory = new FileTxnSnapLog(tmpDir, tmpDir);
File version2 = new File(tmpDir, "version-2");
version2.mkdir();
long zxid = ZxidUtils.makeZxid(3, 3);
logFactory.append(new Request(1, 1, ZooDefs.OpCode.error,
new TxnHeader(1, 1, zxid, 1, ZooDefs.OpCode.error),
new ErrorTxn(1), zxid));
logFactory.commit();
ZKDatabase zkDb = new ZKDatabase(logFactory);
QuorumPeer peer = new QuorumPeer();
peer.setZKDatabase(zkDb);
peer.setTxnFactory(logFactory);
peer.getLastLoggedZxid();
Assert.assertEquals(3, peer.getAcceptedEpoch());
Assert.assertEquals(3, peer.getCurrentEpoch());
Assert.assertEquals(3, Integer
.parseInt(readContentsOfFile(new File(version2,
QuorumPeer.CURRENT_EPOCH_FILENAME))));
Assert.assertEquals(3, Integer
.parseInt(readContentsOfFile(new File(version2,
QuorumPeer.ACCEPTED_EPOCH_FILENAME))));
} finally {
recursiveDelete(tmpDir);
}
}
示例6: testInitialAcceptedCurrent
import org.apache.zookeeper.server.util.ZxidUtils; //导入方法依赖的package包/类
@Test
public void testInitialAcceptedCurrent() throws Exception {
File tmpDir = File.createTempFile("test", ".dir");
tmpDir.delete();
tmpDir.mkdir();
try {
FileTxnSnapLog logFactory = new FileTxnSnapLog(tmpDir, tmpDir);
File version2 = new File(tmpDir, "version-2");
version2.mkdir();
long zxid = ZxidUtils.makeZxid(3, 3);
TxnHeader hdr = new TxnHeader(1, 1, zxid, 1, ZooDefs.OpCode.error);
ErrorTxn txn = new ErrorTxn(1);
byte[] buf = Util.marshallTxnEntry(hdr, txn);
Request req = new Request(null, 1, 1, ZooDefs.OpCode.error,
ByteBuffer.wrap(buf), null);
req.hdr = hdr;
req.txn = txn;
logFactory.append(req);
logFactory.commit();
ZKDatabase zkDb = new ZKDatabase(logFactory);
QuorumPeer peer = new QuorumPeer();
peer.setZKDatabase(zkDb);
peer.setTxnFactory(logFactory);
peer.getLastLoggedZxid();
Assert.assertEquals(3, peer.getAcceptedEpoch());
Assert.assertEquals(3, peer.getCurrentEpoch());
Assert.assertEquals(3, Integer
.parseInt(readContentsOfFile(new File(version2,
QuorumPeer.CURRENT_EPOCH_FILENAME))));
Assert.assertEquals(3, Integer
.parseInt(readContentsOfFile(new File(version2,
QuorumPeer.ACCEPTED_EPOCH_FILENAME))));
} finally {
recursiveDelete(tmpDir);
}
}
示例7: registerWithLeader
import org.apache.zookeeper.server.util.ZxidUtils; //导入方法依赖的package包/类
/**
* Once connected to the leader, perform the handshake protocol to
* establish a following / observing connection.
* @param pktType
* @return the zxid the Leader sends for synchronization purposes.
* @throws IOException
*/
protected long registerWithLeader(int pktType) throws IOException{
/*
* Send follower info, including last zxid and sid
*/
long lastLoggedZxid = self.getLastLoggedZxid();
QuorumPacket qp = new QuorumPacket();
qp.setType(pktType);
qp.setZxid(ZxidUtils.makeZxid(self.getAcceptedEpoch(), 0));
/*
* Add sid to payload
*/
LearnerInfo li = new LearnerInfo(self.getId(), 0x10000);
ByteArrayOutputStream bsid = new ByteArrayOutputStream();
BinaryOutputArchive boa = BinaryOutputArchive.getArchive(bsid);
boa.writeRecord(li, "LearnerInfo");
qp.setData(bsid.toByteArray());
writePacket(qp, true);
readPacket(qp);
final long newEpoch = ZxidUtils.getEpochFromZxid(qp.getZxid());
if (qp.getType() == Leader.LEADERINFO) {
// we are connected to a 1.0 server so accept the new epoch and read the next packet
leaderProtocolVersion = ByteBuffer.wrap(qp.getData()).getInt();
byte epochBytes[] = new byte[4];
final ByteBuffer wrappedEpochBytes = ByteBuffer.wrap(epochBytes);
if (newEpoch > self.getAcceptedEpoch()) {
wrappedEpochBytes.putInt((int)self.getCurrentEpoch());
self.setAcceptedEpoch(newEpoch);
} else if (newEpoch == self.getAcceptedEpoch()) {
// since we have already acked an epoch equal to the leaders, we cannot ack
// again, but we still need to send our lastZxid to the leader so that we can
// sync with it if it does assume leadership of the epoch.
// the -1 indicates that this reply should not count as an ack for the new epoch
wrappedEpochBytes.putInt(-1);
} else {
throw new IOException("Leaders epoch, " + newEpoch + " is less than accepted epoch, " + self.getAcceptedEpoch());
}
QuorumPacket ackNewEpoch = new QuorumPacket(Leader.ACKEPOCH, lastLoggedZxid, epochBytes, null);
writePacket(ackNewEpoch, true);
return ZxidUtils.makeZxid(newEpoch, 0);
} else {
if (newEpoch > self.getAcceptedEpoch()) {
self.setAcceptedEpoch(newEpoch);
}
if (qp.getType() != Leader.NEWLEADER) {
LOG.error("First packet should have been NEWLEADER");
throw new IOException("First packet should have been NEWLEADER");
}
return qp.getZxid();
}
}
示例8: testPopulatedLeaderConversation
import org.apache.zookeeper.server.util.ZxidUtils; //导入方法依赖的package包/类
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();
}
recursiveDelete(tmpDir);
}
}
示例9: testDirtySnapshot
import org.apache.zookeeper.server.util.ZxidUtils; //导入方法依赖的package包/类
/**
* verify that a peer with dirty snapshot joining an established cluster
* does not go into an inconsistent state.
*
* {@link https://issues.apache.org/jira/browse/ZOOKEEPER-1558}
*/
@Test
public void testDirtySnapshot()
throws IOException,
InterruptedException,
KeeperException,
NoSuchFieldException,
IllegalAccessException {
Socket pair[] = getSocketPair();
Socket leaderSocket = pair[0];
Socket followerSocket = pair[1];
File tmpDir = File.createTempFile("test", "dir");
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);
long zxid = ZxidUtils.makeZxid(0, 1);
String path = "/foo";
zkDb.processTxn(new TxnHeader(13,1000,zxid,30,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)));
// Close files
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(0);
peer.setCurrentEpoch(0);
leadThread = new LeadThread(leader);
leadThread.start();
while(leader.cnxAcceptor == null || !leader.cnxAcceptor.isAlive()) {
Thread.sleep(20);
}
leader.shutdown("Shutting down the leader");
// Check if there is a valid snapshot (we better not have it)
File snapDir = new File (tmpDir, FileTxnSnapLog.version + FileTxnSnapLog.VERSION);
List<File> files = Util.sortDataDir(snapDir.listFiles(),"snapshot", false);
for (File f : files) {
try {
Assert.assertFalse("Found a valid snapshot", Util.isValidSnapshot(f));
} catch (IOException e) {
LOG.info("invalid snapshot " + f, e);
}
}
} finally {
if (leader != null) {
leader.shutdown("end of test");
}
if (leadThread != null) {
leadThread.interrupt();
leadThread.join();
}
recursiveDelete(tmpDir);
}
}
示例10: registerWithLeader
import org.apache.zookeeper.server.util.ZxidUtils; //导入方法依赖的package包/类
/**
* Once connected to the leader, perform the handshake protocol to
* establish a following / observing connection.
* @param pktType
* @return the zxid the Leader sends for synchronization purposes.
* @throws IOException
*/
protected long registerWithLeader(int pktType) throws IOException{
/*
* Send follower info, including last zxid and sid
*/
long lastLoggedZxid = self.getLastLoggedZxid();
QuorumPacket qp = new QuorumPacket();
qp.setType(pktType);
qp.setZxid(ZxidUtils.makeZxid(self.getAcceptedEpoch(), 0));
/*
* Add sid to payload
*/
LearnerInfo li = new LearnerInfo(self.getId(), 0x10000, self.getQuorumVerifier().getVersion());
ByteArrayOutputStream bsid = new ByteArrayOutputStream();
BinaryOutputArchive boa = BinaryOutputArchive.getArchive(bsid);
boa.writeRecord(li, "LearnerInfo");
qp.setData(bsid.toByteArray());
writePacket(qp, true);
readPacket(qp);
final long newEpoch = ZxidUtils.getEpochFromZxid(qp.getZxid());
if (qp.getType() == Leader.LEADERINFO) {
// we are connected to a 1.0 server so accept the new epoch and read the next packet
leaderProtocolVersion = ByteBuffer.wrap(qp.getData()).getInt();
byte epochBytes[] = new byte[4];
final ByteBuffer wrappedEpochBytes = ByteBuffer.wrap(epochBytes);
if (newEpoch > self.getAcceptedEpoch()) {
wrappedEpochBytes.putInt((int)self.getCurrentEpoch());
self.setAcceptedEpoch(newEpoch);
} else if (newEpoch == self.getAcceptedEpoch()) {
// since we have already acked an epoch equal to the leaders, we cannot ack
// again, but we still need to send our lastZxid to the leader so that we can
// sync with it if it does assume leadership of the epoch.
// the -1 indicates that this reply should not count as an ack for the new epoch
wrappedEpochBytes.putInt(-1);
} else {
throw new IOException("Leaders epoch, " + newEpoch + " is less than accepted epoch, " + self.getAcceptedEpoch());
}
QuorumPacket ackNewEpoch = new QuorumPacket(Leader.ACKEPOCH, lastLoggedZxid, epochBytes, null);
writePacket(ackNewEpoch, true);
return ZxidUtils.makeZxid(newEpoch, 0);
} else {
if (newEpoch > self.getAcceptedEpoch()) {
self.setAcceptedEpoch(newEpoch);
}
if (qp.getType() != Leader.NEWLEADER) {
LOG.error("First packet should have been NEWLEADER");
throw new IOException("First packet should have been NEWLEADER");
}
return qp.getZxid();
}
}
示例11: testPopulatedLeaderConversation
import org.apache.zookeeper.server.util.ZxidUtils; //导入方法依赖的package包/类
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(), 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, 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);
}
}
示例12: getZxid
import org.apache.zookeeper.server.util.ZxidUtils; //导入方法依赖的package包/类
long getZxid(long epoch, long counter){
return ZxidUtils.makeZxid(epoch, counter);
}
示例13: testPopulatedLeaderConversation
import org.apache.zookeeper.server.util.ZxidUtils; //导入方法依赖的package包/类
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,
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();
}
recursiveDelete(tmpDir);
}
}
示例14: testPopulatedLeaderConversation
import org.apache.zookeeper.server.util.ZxidUtils; //导入方法依赖的package包/类
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");
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();
}
recursiveDelete(tmpDir);
}
}