Implementation of redis cluster-initialization (2)

Source: Internet
Author: User
Tags redis cluster

 

2) on the listener set interface, you can call aecreatefileevent () to set the event processor. From the signature call, you can see that the set accept event processor is clusteraccepthandler (),

aeCreateFileEvent(server.el, server.cfd[j], AE_READABLE, clusterAcceptHandler, NULL);

 

// Set the event processor int aecreatefileevent (aeeventloop * eventloop, int FD, int mask, aefileproc * proc, void * clientdata) {If (FD> = eventloop-> setsize) {errno = erange; return AE _err;} aefileevent * Fe = & eventloop-> events [FD]; // Add events on the file descriptor FD // select the corresponding I/O model based on different system platforms // For example, epoll if (aeapiaddevent (eventloop, FD, mask) =-1) return AE _err; Fe-> mask | = mask; // set the event processor for the read/write events respectively. // here, clusteraccepthandler () if (mask & AE _readable) Fe-> rfileproc = proc is used to process the read event; if (mask & AE _writable) Fe-> wfileproc = proc; Fe-> clientdata = clientdata; If (FD> eventloop-> maxfd) eventloop-> maxfd = FD; return AE _ OK ;}


After the accept event processor is added as above, when a new connection comes over, clusteraccepthandler () is called back to handle the new connection.

// Set a maximum of 1000 connections to be processed during each callback # define max_cluster_accepts_per_call 1000 // FD is the clientdata parameter set when listening socket // privdata is called aecreatefileevent, here nullvoid clusteraccepthandler (aeeventloop * El, int FD, void * privdata, int mask) {int cport, CFD; int max = bytes; char CIP [redis_ip_str_len]; clusterlink * link; redis_notused (EL); redis_notused (mask); redis_notused (privdata);/* If the Ser Ver is starting up, don't accept cluster connections: * update messages may interact with the database content. */If (server. masterhost = NULL & server. loading) return; // loop max_cluster_accepts_per_call times to accept new connections while (max --) {// the descriptor of the client connection is CFG // CIP and the client address // cport is the local port of the connected socket. CFD = anettcpaccept (server. neterr, FD, CIP, sizeof (CIP), & cport); If (CFD = anet_err) {If (errno! = Ewouldblock) redislog (redis_verbose, "accepting cluster node: % s", server. neterr); return;} // here the connection socket is also set to non-blocking anetnonblock (null, CFD); // and the socket option tcp_nodelay is enabled (disable the TCP Nagle algorithm) anetenabletcpnodelay (null, CFD);/* use non-blocking I/O for cluster messages. */redislog (redis_verbose, "accepted cluster node % s: % d", CIP, cport);/* create a link object we use to handle the connection. * It gets passed to the readable handler when data is available. * initiallly the link-> node pointer is set to null as we don't know * Which node is, but the right node is references once we know the * node identity. */link = createclusterlink (null); Link-> FD = CFD; aecreatefileevent (server. el, CFD, AE _readable, clusterreadhandler, link );}}

 

After successfully accepting the client connection (which actually represents the connection of other nodes), create a cluster link and initialize it to set the read event processor clusterreadhandler.

The initialization of each node of the redis cluster has basically ended. The following is the cluster component waiting to accept the connection and process the received data.

[The next article is about to analyze the implementation of clusterreadhandler.]

This article is from the "quiet Madman" blog, please be sure to keep this source http://quietmadman.blog.51cto.com/3269500/1558685

Implementation of redis cluster-initialization (2)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.