Redis source code parsing 1

Source: Internet
Author: User
Tags epoll
Preface Redis (REmoteDIctionaryServer) is a key-value storage system written by SalvatoreSanfilippo. It has the following features: the overall structure of Redis is a single-threaded server (except for writing a disk to open sub-processes, VM management to open threads, ignore these two blocks first, its server startup process is very clear.

Preface Redis (REmoteDIctionaryServer) is a key-value storage system written by Salvatore Sanfilippo. It has the following features: the overall structure of Redis is a single-threaded server (except for writing a disk to open sub-processes, VM management to open threads, ignore these two blocks first, its server startup process is very clear.

Preface

Redis (REmote DIctionary Server) is a key-value storage system written by Salvatore Sanfilippo.
It has the following features:

Overall Structure

Redis is a single-threaded server (in addition to writing a disk, sub-processes are opened, and VM management starts threads. Ignore the two threads first)
Therefore, its server startup process is very clear.

Event loop 1. Data Structure

An event framework is represented by the aeEventLoop structure. The analysis is as follows:

1 typedef struct aeEventLoop {timeEventNextId; aeFileEvent events [AE _SETSIZE]; aeFiredEvent fired [AE _SETSIZE]; aeTimeEvent * timeEventHead; stop; interval * beforesleep; 10} aeEventLoop;

2. Event function distribution process

Redis supports three network models: epoll, kqueue, and select.

The Code Implementation of the network framework is mainly in the following files:

AE. c/AE. h // network framework
Implementation of AE _epoll.c // epoll Model
Implementation of AE _kqueue.c // kqueue Model
Implementation of AE _select.c // select model

Which program is selected? The website space is determined during the compilation period.

1 1 file AE. c # ifdef HAVE_EPOLL # ifdef HAVE_KQUEUE # include

FRAMEWORK Functions are very simple. From initialization to completion, there are three main functions.
AeCreateEventLoop, aeMain, and aeDeleteEventLoop
AeMain is the main function of the event loop, which calls the aeProcessEvents function.

The three main functions call three interface functions: aeApiCreate, aeApiPool, and aeApiFree for processing.
These three interface functions are mapped to a specific network model, which is determined during the compilation period.

As shown in:

Add deletion events
The aeCreateFileEvent and aeDeleteFileEvent functions are completed. The function distribution process is as follows:

2. server socket creation process

1. During server initialization, establish a listening socket and bind the IP address and Port
Supports TCP connection and unixSocket connection on the local machine

1if (server. port! = 0) {2server. ipfd = anetTcpServer (server. neterr, server. port, server. bindaddr); 3 ...... 4} 5if (server. unixsocket! = NULL) {server. sofd = anetUnixServer (server. neterr, server. unixsocket, server. unixsocketperm); 8... 9}

2. Listen to the socket bound to the event handle

1if (server. ipfd> 0 & aeCreateFileEvent (server. el, server. ipfd, AE _READABLE,); 3if (server. sofd> 0 & aeCreateFileEvent (server. el, server. sofd, AE _READABLE ,);

"AcceptTcpHandler" and "acceptUnixHandler" are Event Callback functions.
When a new connection is established, the two functions are triggered.

3. Let's take a look at what accept *** Handler has done.

1 void acceptTcpHandler (aeEventLoop * el, int fd, void * privdata, int mask) {2int cport, cfd; 3 char cip [128]; 4 REDIS_NOTUSED (el ); 5 REDIS_NOTUSED (mask); 6 REDIS_NOTUSED (privdata); cfd = anetTcpAccept (server. neterr, fd, cip, & cport); 10if (cfd = AE _ERR) {, server. neterr); 12 return; 13}, cip, cport );}

What acceptCommonHandler does is simple. Call the CreateClient function to create a new redisClient object.

AcceptCommonHandler (int fd) {2 redisClient * c; 3if (c = createClient (fd) = NULL) {); 7} 8 ...... 9}

In the createClient function, bind the new connection to the event loop.

1 redisClient * createClient (int fd) {2 redisClient * c = zmalloc (sizeof (redisClient); 3c-> bufpos = 0; 4 5 anetNonBlock (NULL, fd ); 6 anetTcpNoDelay (NULL, fd); 7if (aeCreateFileEvent (server. el, fd, AE _READABLE, {10 close (fd); 11 zfree (c); 12 return NULL; 13}

When data arrives on the connection, the readQueryFromClient function is triggered to read and process the actual network data.

3. Timer event

Redis binds all timers to the event loop for processing.
Create a new Timer event using the aeCreateTimeEvent Function
In the event loop of each frame, the processTimeEvents function is used for processing.

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.