本文整理汇总了Java中org.apache.zookeeper.server.quorum.QuorumPeer.ServerState.LEADING属性的典型用法代码示例。如果您正苦于以下问题:Java ServerState.LEADING属性的具体用法?Java ServerState.LEADING怎么用?Java ServerState.LEADING使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类org.apache.zookeeper.server.quorum.QuorumPeer.ServerState
的用法示例。
在下文中一共展示了ServerState.LEADING属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: checkLeader
/**
* In the case there is a leader elected, and a quorum supporting this
* leader, we have to check if the leader has voted and acked that it is
* leading. We need this check to avoid that peers keep electing over and
* over a peer that has crashed and it is no longer leading.
*
* @param votes
* set of votes
* @param leader
* leader id
* @param electionEpoch
* epoch id
*/
protected boolean checkLeader(HashMap<Long, Vote> votes, long leader, long electionEpoch) {
boolean predicate = true;
/*
* If everyone else thinks I'm the leader, I must be the leader. The
* other two checks are just for the case in which I'm not the leader.
* If I'm not the leader and I haven't received a message from leader
* stating that it is leading, then predicate is false.
*/
if (leader != self.getId()) {
if (votes.get(leader) == null)
predicate = false;
else if (votes.get(leader).getState() != ServerState.LEADING)
predicate = false;
} else if (logicalclock != electionEpoch) {
predicate = false;
}
return predicate;
}
示例2: run
public void run() {
setName("VerifyState-" + peer.getId());
while (true) {
if(peer.getPeerState() == ServerState.FOLLOWING) {
LOG.info("I am following");
success = true;
break;
} else if (peer.getPeerState() == ServerState.LEADING) {
LOG.info("I am leading");
success = false;
break;
}
try {
Thread.sleep(250);
} catch (Exception e) {
LOG.warn("Sleep failed ", e);
}
}
}
示例3: checkLeader
/**
* In the case there is a leader elected, and a quorum supporting
* this leader, we have to check if the leader has voted and acked
* that it is leading. We need this check to avoid that peers keep
* electing over and over a peer that has crashed and it is no
* longer leading.
*
* @param votes set of votes
* @param leader leader id
* @param electionEpoch epoch id
*/
private boolean checkLeader(
Map<Long, Vote> votes,
long leader,
long electionEpoch){
boolean predicate = true;
/*
* If everyone else thinks I'm the leader, I must be the leader.
* The other two checks are just for the case in which I'm not the
* leader. If I'm not the leader and I haven't received a message
* from leader stating that it is leading, then predicate is false.
*/
if(leader != self.getId()){
if(votes.get(leader) == null) predicate = false;
else if(votes.get(leader).getState() != ServerState.LEADING) predicate = false;
} else if(logicalclock.get() != electionEpoch) {
predicate = false;
}
return predicate;
}
示例4: checkLeader
/**
* In the case there is a leader elected, and a quorum supporting
* this leader, we have to check if the leader has voted and acked
* that it is leading. We need this check to avoid that peers keep
* electing over and over a peer that has crashed and it is no
* longer leading.
*
* @param votes set of votes
* @param leader leader id
* @param electionEpoch epoch id
*/
protected boolean checkLeader(
HashMap<Long, Vote> votes,
long leader,
long electionEpoch){
boolean predicate = true;
/*
* If everyone else thinks I'm the leader, I must be the leader.
* The other two checks are just for the case in which I'm not the
* leader. If I'm not the leader and I haven't received a message
* from leader stating that it is leading, then predicate is false.
*/
if(leader != self.getId()){
if(votes.get(leader) == null) predicate = false;
else if(votes.get(leader).getState() != ServerState.LEADING) predicate = false;
} else if(logicalclock != electionEpoch) {
predicate = false;
}
return predicate;
}
示例5: verifyElectionTimeTakenJMXAttribute
private void verifyElectionTimeTakenJMXAttribute(List<QuorumPeer> peers)
throws Exception {
LOG.info("Verify QuorumPeer#electionTimeTaken jmx bean attribute");
for (int i = 1; i <= peers.size(); i++) {
QuorumPeer qp = peers.get(i - 1);
if (qp.getLearnerType() == LearnerType.OBSERVER) {
continue; // Observer don't have electionTimeTaken attribute.
}
Long electionTimeTaken = -1L;
String bean = "";
if (qp.getPeerState() == ServerState.FOLLOWING) {
bean = String.format(
"%s:name0=ReplicatedServer_id%d,name1=replica.%d,name2=Follower",
CommonNames.DOMAIN, i, i);
} else if (qp.getPeerState() == ServerState.LEADING) {
bean = String.format(
"%s:name0=ReplicatedServer_id%d,name1=replica.%d,name2=Leader",
CommonNames.DOMAIN, i, i);
}
electionTimeTaken = (Long) JMXEnv.ensureBeanAttribute(bean,
"ElectionTimeTaken");
Assert.assertTrue("Wrong electionTimeTaken value!",
electionTimeTaken >= 0);
}
}
示例6: checkLeader
/**
* In the case there is a leader elected, and a quorum supporting
* this leader, we have to check if the leader has voted and acked
* that it is leading. We need this check to avoid that peers keep
* electing over and over a peer that has crashed and it is no
* longer leading.
*
* @param votes set of votes
* @param leader leader id
* @param electionEpoch epoch id
*/
private boolean checkLeader(
HashMap<Long, Vote> votes,
long leader,
long electionEpoch){
boolean predicate = true;
/*
* If everyone else thinks I'm the leader, I must be the leader.
* The other two checks are just for the case in which I'm not the
* leader. If I'm not the leader and I haven't received a message
* from leader stating that it is leading, then predicate is false.
*/
if(leader != self.getId()){
if(votes.get(leader) == null) predicate = false;
else if(votes.get(leader).getState() != ServerState.LEADING) predicate = false;
}
return predicate;
}
示例7: checkLeader
/**
* In the case there is a leader elected, and a quorum supporting
* this leader, we have to check if the leader has voted and acked
* that it is leading. We need this check to avoid that peers keep
* electing over and over a peer that has crashed and it is no
* longer leading.
*
* @param votes set of votes
* @param leader leader id
* @param electionEpoch epoch id
*/
private boolean checkLeader(
HashMap<Long, Vote> votes,
long leader,
long electionEpoch){
boolean predicate = true;
/*
* If everyone else thinks I'm the leader, I must be the leader.
* The other two checks are just for the case in which I'm not the
* leader. If I'm not the leader and I haven't received a message
* from leader stating that it is leading, then predicate is false.
*/
if(leader != self.getId()){
if(votes.get(leader) == null) predicate = false;
else if(votes.get(leader).getState() != ServerState.LEADING) predicate = false;
} else if(logicalclock.get() != electionEpoch) {
predicate = false;
}
return predicate;
}
示例8: getLeaderQuorumPeer
private QuorumPeer getLeaderQuorumPeer(MainThread[] mt) {
for (int i = mt.length - 1; i >= 0; i--) {
QuorumPeer quorumPeer = mt[i].getQuorumPeer();
if (null != quorumPeer
&& ServerState.LEADING == quorumPeer.getPeerState()) {
return quorumPeer;
}
}
return null;
}
示例9: run
public void run() {
try {
Vote v = null;
fail = false;
while(true){
//while(true) {
peer.setPeerState(ServerState.LOOKING);
LOG.info("Going to call leader election.");
v = peer.getElectionAlg().lookForLeader();
if(v == null){
LOG.info("Thread " + i + " got a null vote");
return;
}
/*
* A real zookeeper would take care of setting the current vote. Here
* we do it manually.
*/
peer.setCurrentVote(v);
LOG.info("Finished election: " + i + ", " + v.getId());
votes[i] = v;
if((peer.getPeerState() == ServerState.LEADING) &&
(peer.getId() > 2)) fail = true;
if((peer.getPeerState() == ServerState.FOLLOWING) ||
(peer.getPeerState() == ServerState.LEADING)) break;
}
LOG.debug("Thread " + i + " votes " + v);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
示例10: getLeader
private QuorumPeer getLeader(MainThread[] mt) {
for (int i = mt.length - 1; i >= 0; i--) {
QuorumPeer quorumPeer = mt[i].getQuorumPeer();
if (quorumPeer != null && ServerState.LEADING == quorumPeer.getPeerState()) {
return quorumPeer;
}
}
return null;
}
示例11: getLeaderIndex
public int getLeaderIndex() {
if (s1.getPeerState() == ServerState.LEADING) {
return 0;
} else if (s2.getPeerState() == ServerState.LEADING) {
return 1;
} else if (s3.getPeerState() == ServerState.LEADING) {
return 2;
} else if (s4.getPeerState() == ServerState.LEADING) {
return 3;
} else if (s5.getPeerState() == ServerState.LEADING) {
return 4;
}
return -1;
}
示例12: getLeaderQuorumPeer
private QuorumPeer getLeaderQuorumPeer(List<MainThread> mtList) {
for (MainThread mt : mtList) {
QuorumPeer quorumPeer = mt.getQuorumPeer();
if (null != quorumPeer
&& ServerState.LEADING == quorumPeer.getPeerState()) {
return quorumPeer;
}
}
return null;
}
示例13: testCorruption
@Test
public void testCorruption() throws Exception {
ClientBase.waitForServerUp(qb.hostPort, 10000);
ClientBase.waitForServerUp(qb.hostPort, 10000);
ZooKeeper zk = new ZooKeeper(qb.hostPort, 10000, new Watcher() {
public void process(WatchedEvent event) {
}});
SyncRequestProcessor.setSnapCount(100);
for (int i = 0; i < 2000; i++) {
zk.create("/0-" + i, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT, new NoopStringCallback(), null);
}
zk.close();
long leaderSid = 1;
QuorumPeer leader = null;
//find out who is the leader and kill it
for (QuorumPeer quorumPeer : Arrays.asList(qb.s1, qb.s2, qb.s3, qb.s4, qb.s5)) {
if (quorumPeer.getPeerState() == ServerState.LEADING) {
leader = quorumPeer;
break;
}
++leaderSid;
}
Assert.assertNotNull("Cannot find the leader.", leader);
leader.shutdown();
// now corrupt the leader's database
FileTxnSnapLog snapLog = leader.getTxnFactory();
File snapDir= snapLog.getSnapDir();
//corrupt all the snapshot in the snapshot directory
corruptAllSnapshots(snapDir);
qb.shutdownServers();
qb.setupServers();
if (leaderSid != 1)qb.s1.start(); else leader = qb.s1;
if (leaderSid != 2)qb.s2.start(); else leader = qb.s2;
if (leaderSid != 3)qb.s3.start(); else leader = qb.s3;
if (leaderSid != 4)qb.s4.start(); else leader = qb.s4;
if (leaderSid != 5)qb.s5.start(); else leader = qb.s5;
try {
leader.start();
Assert.assertTrue(false);
} catch(RuntimeException re) {
LOG.info("Got an error: expected", re);
}
//wait for servers to be up
String[] list = qb.hostPort.split(",");
for (int i = 0; i < 5; i++) {
if(leaderSid != (i + 1)) {
String hp = list[i];
Assert.assertTrue("waiting for server up",
ClientBase.waitForServerUp(hp,
CONNECTION_TIMEOUT));
LOG.info("{} is accepting client connections", hp);
} else {
LOG.info("Skipping the leader");
}
}
zk = qb.createClient();
SyncRequestProcessor.setSnapCount(100);
for (int i = 2000; i < 4000; i++) {
zk.create("/0-" + i, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT, new NoopStringCallback(), null);
}
zk.close();
if (leaderSid != 1)QuorumBase.shutdown(qb.s1);
if (leaderSid != 2)QuorumBase.shutdown(qb.s2);
if (leaderSid != 3)QuorumBase.shutdown(qb.s3);
if (leaderSid != 4)QuorumBase.shutdown(qb.s4);
if (leaderSid != 5)QuorumBase.shutdown(qb.s5);
}
示例14: testCorruption
@Test
public void testCorruption() throws Exception {
ClientBase.waitForServerUp(qb.hostPort, 10000);
ClientBase.waitForServerUp(qb.hostPort, 10000);
ZooKeeper zk = ClientBase.createZKClient(qb.hostPort, 10000);
SyncRequestProcessor.setSnapCount(100);
for (int i = 0; i < 2000; i++) {
zk.create("/0-" + i, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT, new NoopStringCallback(), null);
}
zk.close();
long leaderSid = 1;
QuorumPeer leader = null;
//find out who is the leader and kill it
for (QuorumPeer quorumPeer : Arrays.asList(qb.s1, qb.s2, qb.s3, qb.s4, qb.s5)) {
if (quorumPeer.getPeerState() == ServerState.LEADING) {
leader = quorumPeer;
break;
}
++leaderSid;
}
Assert.assertNotNull("Cannot find the leader.", leader);
leader.shutdown();
// now corrupt the leader's database
FileTxnSnapLog snapLog = leader.getTxnFactory();
File snapDir= snapLog.getSnapDir();
//corrupt all the snapshot in the snapshot directory
corruptAllSnapshots(snapDir);
qb.shutdownServers();
qb.setupServers();
if (leaderSid != 1)qb.s1.start(); else leader = qb.s1;
if (leaderSid != 2)qb.s2.start(); else leader = qb.s2;
if (leaderSid != 3)qb.s3.start(); else leader = qb.s3;
if (leaderSid != 4)qb.s4.start(); else leader = qb.s4;
if (leaderSid != 5)qb.s5.start(); else leader = qb.s5;
try {
leader.start();
Assert.assertTrue(false);
} catch(RuntimeException re) {
LOG.info("Got an error: expected", re);
}
//wait for servers to be up
String[] list = qb.hostPort.split(",");
for (int i = 0; i < 5; i++) {
if(leaderSid != (i + 1)) {
String hp = list[i];
Assert.assertTrue("waiting for server up",
ClientBase.waitForServerUp(hp,
CONNECTION_TIMEOUT));
LOG.info("{} is accepting client connections", hp);
} else {
LOG.info("Skipping the leader");
}
}
zk = qb.createClient();
SyncRequestProcessor.setSnapCount(100);
for (int i = 2000; i < 4000; i++) {
zk.create("/0-" + i, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT, new NoopStringCallback(), null);
}
zk.close();
if (leaderSid != 1)QuorumBase.shutdown(qb.s1);
if (leaderSid != 2)QuorumBase.shutdown(qb.s2);
if (leaderSid != 3)QuorumBase.shutdown(qb.s3);
if (leaderSid != 4)QuorumBase.shutdown(qb.s4);
if (leaderSid != 5)QuorumBase.shutdown(qb.s5);
}
开发者ID:didichuxing2,项目名称:https-github.com-apache-zookeeper,代码行数:74,代码来源:ZkDatabaseCorruptionTest.java
示例15: testCorruption
@Test
public void testCorruption() throws Exception {
ClientBase.waitForServerUp(qb.hostPort, 10000);
ClientBase.waitForServerUp(qb.hostPort, 10000);
ZooKeeper zk = new ZooKeeper(qb.hostPort, 10000, new Watcher() {
public void process(WatchedEvent event) {
}});
SyncRequestProcessor.setSnapCount(100);
for (int i = 0; i < 2000; i++) {
zk.create("/0-" + i, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
zk.close();
QuorumPeer leader;
//find out who is the leader and kill it
if ( qb.s5.getPeerState() != ServerState.LEADING) {
throw new Exception("the last server is not the leader");
}
leader = qb.s5;
// now corrupt the qurompeer database
FileTxnSnapLog snapLog = leader.getTxnFactory();
File snapDir= snapLog.getSnapDir();
//corrupt all the snapshot in the snapshot directory
corruptAllSnapshots(snapDir);
qb.shutdownServers();
qb.setupServers();
qb.s1.start();
qb.s2.start();
qb.s3.start();
qb.s4.start();
try {
qb.s5.start();
Assert.assertTrue(false);
} catch(RuntimeException re) {
LOG.info("Got an error: expected", re);
}
//waut for servers to be up
String[] list = qb.hostPort.split(",");
for (int i =0; i < 4; i++) {
String hp = list[i];
Assert.assertTrue("waiting for server up",
ClientBase.waitForServerUp(hp,
CONNECTION_TIMEOUT));
LOG.info(hp + " is accepting client connections");
}
zk = qb.createClient();
SyncRequestProcessor.setSnapCount(100);
for (int i = 2000; i < 4000; i++) {
zk.create("/0-" + i, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
zk.close();
QuorumBase.shutdown(qb.s1);
QuorumBase.shutdown(qb.s2);
QuorumBase.shutdown(qb.s3);
QuorumBase.shutdown(qb.s4);
}