Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of learner in zk

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what is the role of learner in zk". In daily operation, I believe that many people have doubts about the role of learner in zk. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubt of "what is the role of learner in zk?" Next, please follow the editor to study!

Learner is the parent class of observer,follower, defining public properties and methods

Subclasses Follower and Observer

Inner class:

PacketInFlight said there was no news of commit in the proposal.

Static class PacketInFlight {TxnHeader hdr; Record rec;}

Attributes:

QuorumPeer

Server node

LearnerZooKeeperServer

Service node of learner

BufferedOutputStream

Output stream

Socket

Port socket

InetSocketAddress

Address information

InputArchive

Input Archive

OutputArchive

Output Archive

LeaderProtocolVersion

Leader protocol version

BUFFERED_MESSAGE_SIZE

Cache information size

MessageTracker

Receive and send messages sequentially

Method

ValidateSession (ServerCnxn cnxn, long clientId, int timeout)

Verify the validity of session

WritePacket (QuorumPacket pp, boolean flush)

Send a packet to leader

ReadPacket (QuorumPacket pp)

Read message from leader

Request (Request request)

Send request to leader

FindLeader

Find the address information that is thought to be leader

CreateSocket ()

Create a socket object

RegisterWithLeader (int pktType)

Execute handshake protocal to establish a follower/observer connection

Verify the validity of session to the server

Void validateSession (ServerCnxn cnxn, long clientId, int timeout) throws IOException {LOG.info ("Revalidating client: 0x" + Long.toHexString (clientId)); ByteArrayOutputStream baos = new ByteArrayOutputStream (); DataOutputStream dos = new DataOutputStream (baos); dos.writeLong (clientId); dos.writeInt (timeout); dos.close (); QuorumPacket qp = new QuorumPacket (Leader.REVALIDATE,-1, baos.toByteArray (), null); pendingRevalidations.put (clientId, cnxn) If (LOG.isTraceEnabled ()) {ZooTrace.logTraceMessage (LOG, ZooTrace.SESSION_TRACE_MASK, "To validate session 0x" + Long.toHexString (clientId));} writePacket (qp, true);} void writePacket (QuorumPacket pp, boolean flush) throws IOException {synchronized (leaderOs) {if (pp! = null) {messageTracker.trackSent (pp.getType ()) LeaderOs.writeRecord (pp, "packet");} if (flush) {bufferedOutput.flush ();} 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);} find the current leader information protected QuorumServer findLeader () {QuorumServer leaderServer = null; / / Find the leader by id Vote current = self.getCurrentVote (); for (QuorumServer s: self.getView (). Values ()) {if (s.id = = current.getId ()) {/ / Ensure we have the leader's correct IP address before / / attempting to connect. S.recreateSocketAddresses (); leaderServer = s; break;}} if (leaderServer = = null) {LOG.warn ("Couldn't find the leader with id =" + current.getId ();} return leaderServer;} connection socket sockConnect (Socket sock, InetSocketAddress addr, int timeout) establish a connection with leader / * Establish a connection with the LearnerMaster found by findLearnerMaster. * Followers only connect to Leaders, Observers can connect to any active LearnerMaster. * Retries until either initLimit time has elapsed or 5 tries have happened. * @ param addr-the address of the Peer to connect to. * @ throws IOException-if the socket connection fails on the 5th attempt * if there is an authentication failure while connecting to leader * @ throws X509Exception * @ throws InterruptedException * / protected void connectToLeader (InetSocketAddress addr, String hostname) throws IOException, InterruptedException, X509Exception {this.sock = createSocket (); this.leaderAddr = addr; / / leader connection timeout defaults to tickTime * initLimit int connectTimeout = self.tickTime * self.initLimit / / but if connectToLearnerMasterLimit is specified, use that value to calculate / / timeout instead of using the initLimit value if (self.connectToLearnerMasterLimit > 0) {connectTimeout = self.tickTime * self.connectToLearnerMasterLimit;} int remainingTimeout; long startNanoTime = nanoTime (); for (int tries = 0; tries < 5) Tries++) {try {/ / recalculate the init limit time because retries sleep for 1000 milliseconds remainingTimeout = connectTimeout-(int) ((nanoTime ()-startNanoTime) / 1000000); if (remainingTimeout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report