本文整理了Java中org.apache.zookeeper.server.quorum.QuorumPacket.<init>
方法的一些代码示例,展示了QuorumPacket.<init>
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。QuorumPacket.<init>
方法的具体详情如下:
包路径:org.apache.zookeeper.server.quorum.QuorumPacket
类名称:QuorumPacket
方法名:<init>
暂无
代码示例来源:origin: apache/zookeeper
public static QuorumPacket buildInformAndActivePacket(long zxid,
long designatedLeader, byte[] proposalData) {
byte[] data = new byte[proposalData.length + 8];
ByteBuffer buffer = ByteBuffer.wrap(data);
buffer.putLong(designatedLeader);
buffer.put(proposalData);
return new QuorumPacket(Leader.INFORMANDACTIVATE, zxid, data, null);
}
代码示例来源:origin: apache/zookeeper
/**
* Queue leader packet of a given type
* @param type
* @param zxid
*/
private void queueOpPacket(int type, long zxid) {
QuorumPacket packet = new QuorumPacket(type, zxid, null, null);
queuePacket(packet);
}
代码示例来源:origin: apache/zookeeper
public void commitAndActivate(long zxid, long designatedLeader) {
synchronized(this){
lastCommitted = zxid;
}
byte data[] = new byte[8];
ByteBuffer buffer = ByteBuffer.wrap(data);
buffer.putLong(designatedLeader);
QuorumPacket qp = new QuorumPacket(Leader.COMMITANDACTIVATE, zxid, data, null);
sendPacket(qp);
}
代码示例来源:origin: apache/zookeeper
/**
* Sends a sync message to the appropriate server
*/
public void sendSync(LearnerSyncRequest r){
QuorumPacket qp = new QuorumPacket(Leader.SYNC, 0, null, null);
r.fh.queuePacket(qp);
}
代码示例来源:origin: apache/zookeeper
synchronized public void sync(){
if(pendingSyncs.size() == 0) {
LOG.warn("Not expecting a sync.");
return;
}
Request r = pendingSyncs.remove();
if (r instanceof LearnerSyncRequest) {
LearnerSyncRequest lsr = (LearnerSyncRequest)r;
lsr.fh.queuePacket(new QuorumPacket(Leader.SYNC, 0, null, null));
}
commitProcessor.commit(r);
}
代码示例来源:origin: org.apache.zookeeper/zookeeper
/**
* Create a commit packet and send it to all the members of the quorum
*
* @param zxid
*/
public void commit(long zxid) {
synchronized(this){
lastCommitted = zxid;
}
QuorumPacket qp = new QuorumPacket(Leader.COMMIT, zxid, null, null);
sendPacket(qp);
}
代码示例来源:origin: org.apache.zookeeper/zookeeper
/**
* Sends a sync message to the appropriate server
*
* @param f
* @param r
*/
public void sendSync(LearnerSyncRequest r){
QuorumPacket qp = new QuorumPacket(Leader.SYNC, 0, null, null);
r.fh.queuePacket(qp);
}
代码示例来源:origin: apache/zookeeper
void proposalReceived(QuorumPacket qp) {
proposedPkts.add(new QuorumPacket(Leader.INFORM, qp.getZxid(), qp.getData(), null));
}
代码示例来源:origin: apache/zookeeper
/**
* send a request packet to the leader
*
* @param request
* the request from the client
* @throws IOException
*/
void request(Request request) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream oa = new DataOutputStream(baos);
oa.writeLong(request.sessionId);
oa.writeInt(request.cxid);
oa.writeInt(request.type);
if (request.request != null) {
request.request.rewind();
int len = request.request.remaining();
byte b[] = new byte[len];
request.request.get(b);
request.request.rewind();
oa.write(b);
}
oa.close();
QuorumPacket qp = new QuorumPacket(Leader.REQUEST, -1, baos
.toByteArray(), request.authInfo);
writePacket(qp, true);
}
代码示例来源:origin: apache/zookeeper
/**
* Create an inform packet and send it to all observers.
*/
public void inform(Proposal proposal) {
QuorumPacket qp = new QuorumPacket(Leader.INFORM, proposal.request.zxid,
proposal.packet.getData(), null);
sendObserverPacket(qp);
}
代码示例来源:origin: apache/zookeeper
/**
* Create a commit packet and send it to all the members of the quorum
*
* @param zxid
*/
public void commit(long zxid) {
synchronized(this){
lastCommitted = zxid;
}
QuorumPacket qp = new QuorumPacket(Leader.COMMIT, zxid, null, null);
sendPacket(qp);
ServerMetrics.COMMIT_COUNT.add(1);
}
代码示例来源:origin: apache/zookeeper
public void processRequest(Request si) {
if(si.type != OpCode.sync){
QuorumPacket qp = new QuorumPacket(Leader.ACK, si.getHdr().getZxid(), null,
null);
try {
learner.writePacket(qp, false);
} catch (IOException e) {
LOG.warn("Closing connection to leader, exception during packet send", e);
try {
if (!learner.sock.isClosed()) {
learner.sock.close();
}
} catch (IOException e1) {
// Nothing to do, we are shutting things down, so an exception here is irrelevant
LOG.debug("Ignoring error closing the connection", e1);
}
}
}
}
代码示例来源:origin: apache/zookeeper
/**
* maintains a list of last <i>committedLog</i>
* or so committed requests. This is used for
* fast follower synchronization.
* @param request committed request
*/
public void addCommittedProposal(Request request) {
WriteLock wl = logLock.writeLock();
try {
wl.lock();
if (committedLog.size() > commitLogCount) {
committedLog.removeFirst();
minCommittedLog = committedLog.getFirst().packet.getZxid();
}
if (committedLog.isEmpty()) {
minCommittedLog = request.zxid;
maxCommittedLog = request.zxid;
}
byte[] data = SerializeUtils.serializeRequest(request);
QuorumPacket pp = new QuorumPacket(Leader.PROPOSAL, request.zxid, data, null);
Proposal p = new Proposal();
p.packet = pp;
p.request = request;
committedLog.add(p);
maxCommittedLog = p.packet.getZxid();
} finally {
wl.unlock();
}
}
代码示例来源:origin: org.apache.zookeeper/zookeeper
/**
* Create an inform packet and send it to all observers.
* @param zxid
* @param proposal
*/
public void inform(Proposal proposal) {
QuorumPacket qp = new QuorumPacket(Leader.INFORM, proposal.request.zxid,
proposal.packet.getData(), null);
sendObserverPacket(qp);
}
代码示例来源:origin: apache/zookeeper
/**
* Forge an validate session packet as a LEARNER do
*
* @return
* @throws Exception
*/
private QuorumPacket createValidateSessionPacket() throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeLong(SESSION_ID);
dos.writeInt(3000);
dos.close();
QuorumPacket qp = new QuorumPacket(Leader.REVALIDATE, -1,
baos.toByteArray(), null);
return qp;
}
代码示例来源:origin: apache/zookeeper
/**
* ping calls from the learnerMaster to the peers
*/
public void ping() {
// If learner hasn't sync properly yet, don't send ping packet
// otherwise, the learner will crash
if (!sendingThreadStarted) {
return;
}
long id;
if (syncLimitCheck.check(System.nanoTime())) {
id = learnerMaster.getLastProposed();
QuorumPacket ping = new QuorumPacket(Leader.PING, id, null, null);
queuePacket(ping);
} else {
LOG.warn("Closing connection to peer due to transaction timeout.");
shutdown();
}
}
代码示例来源:origin: apache/zookeeper
boolean revalidateLearnerSession(QuorumPacket qp) throws IOException {
ByteArrayInputStream bis = new ByteArrayInputStream(qp.getData());
DataInputStream dis = new DataInputStream(bis);
long id = dis.readLong();
boolean valid = dis.readBoolean();
Iterator<Revalidation> itr = pendingRevalidations.iterator();
if (!itr.hasNext()) {
// not a learner session, handle locally
return false;
}
Revalidation revalidation = itr.next();
if (revalidation.sessionId != id) {
// not a learner session, handle locally
return false;
}
itr.remove();
LearnerHandler learnerHandler = revalidation.handler;
// create a copy here as the qp object is reused by the Follower and may be mutated
QuorumPacket deepCopy = new QuorumPacket(qp.getType(), qp.getZxid(),
Arrays.copyOf(qp.getData(), qp.getData().length),
qp.getAuthinfo() == null ? null : new ArrayList<>(qp.getAuthinfo()));
learnerHandler.queuePacket(deepCopy);
// To keep consistent as leader, touch the session when it's
// revalidating the session, only update if it's a valid session.
if (valid) {
touch(revalidation.sessionId, revalidation.timeout);
}
return true;
}
代码示例来源:origin: apache/zookeeper
Proposal createProposal(long zxid) {
Proposal p = new Proposal();
p.packet = new QuorumPacket();
p.packet.setZxid(zxid);
p.packet.setType(Leader.PROPOSAL);
return p;
}
代码示例来源:origin: apache/zookeeper
public void converseWithLeader(InputArchive ia, OutputArchive oa, Leader l)
throws IOException, InterruptedException {
/* we test a normal run. everything should work out well. */
LearnerInfo li = new LearnerInfo(1, 0x10000, 0);
byte liBytes[] = new byte[20];
ByteBufferOutputStream.record2ByteBuffer(li,
ByteBuffer.wrap(liBytes));
QuorumPacket qp = new QuorumPacket(Leader.FOLLOWERINFO, 0,
liBytes, null);
oa.writeRecord(qp, null);
readPacketSkippingPing(ia, qp);
Assert.assertEquals(Leader.LEADERINFO, qp.getType());
Assert.assertEquals(ZxidUtils.makeZxid(1, 0), qp.getZxid());
Assert.assertEquals(ByteBuffer.wrap(qp.getData()).getInt(),
0x10000);
Thread.sleep(l.self.getInitLimit()*l.self.getTickTime() + 5000);
// The leader didn't get a quorum of acks - make sure that leader's current epoch is not advanced
Assert.assertEquals(0, l.self.getCurrentEpoch());
}
});
代码示例来源:origin: apache/zookeeper
public void converseWithLeader(InputArchive ia, OutputArchive oa, Leader l)
throws IOException {
/* we test a normal run. everything should work out well. */
LearnerInfo li = new LearnerInfo(1, 0x10000, 0);
byte liBytes[] = new byte[20];
ByteBufferOutputStream.record2ByteBuffer(li,
ByteBuffer.wrap(liBytes));
/* we are going to say we last acked epoch 20 */
QuorumPacket qp = new QuorumPacket(Leader.FOLLOWERINFO, ZxidUtils.makeZxid(20, 0),
liBytes, null);
oa.writeRecord(qp, null);
readPacketSkippingPing(ia, qp);
Assert.assertEquals(Leader.LEADERINFO, qp.getType());
Assert.assertEquals(ZxidUtils.makeZxid(21, 0), qp.getZxid());
Assert.assertEquals(ByteBuffer.wrap(qp.getData()).getInt(),
0x10000);
qp = new QuorumPacket(Leader.ACKEPOCH, 0, new byte[4], null);
oa.writeRecord(qp, null);
readPacketSkippingPing(ia, qp);
Assert.assertEquals(Leader.DIFF, qp.getType());
readPacketSkippingPing(ia, qp);
Assert.assertEquals(Leader.NEWLEADER, qp.getType());
Assert.assertEquals(ZxidUtils.makeZxid(21, 0), qp.getZxid());
qp = new QuorumPacket(Leader.ACK, qp.getZxid(), null, null);
oa.writeRecord(qp, null);
readPacketSkippingPing(ia, qp);
Assert.assertEquals(Leader.UPTODATE, qp.getType());
}
});
内容来源于网络,如有侵权,请联系作者删除!