Redis 2.8.9 Source-AE Module

Source: Internet
Author: User
Tags epoll

This article is the author original, reproduced please indicate the source:http://my.oschina.net/fuckphp/blog/505956

The code of the AE module of Redis is mainly distributed in ae.c ae.h and ae_*.c, respectively, it realizes several network models of Epoll, Evport, Kqueue and select, and this paper will take Epoll as an example to study the AE module of Redis.

Redis ae.c Event Module notesCore structure of the 1.ae moduleAeeventloop

The Aeeveltloop main definition describes the main loop information of the Redis AE module, which, during the entire event cycle (aeeveltloop.stop!=0), holds information such as the event List of IO events and time events.

typedef struct aeeventloop {     int maxfd;   /*  maximum descriptor for the current registration  */    int  setsize; /*  Maximum number of file descriptors  */    long long timeEventNextId;   //self-increment id  is used to assign each event id    time_t lasttime;     /*   to calibrate the system time, update the time before each event event is executed  */    aeFileEvent *events; /*  store registered events  */    aeFiredEvent *fired; /*  events that have been triggered are saved here  */     aeTimeEvent *timeEventHead; /*  Time Event unidirectional link list   events that contain future events  */     int stop;    void *apidata; /*  user's custom data is used to pass to the callback function  */     aeBeforeSleepProc *beforesleep; /*   */} aeeventloop is executed once for each event loop; 
Aefileevent

Aefileevent describes the main information of the Redis AE module on IO class events, including the read-write mode callback function for the current event, and the aefileevent structure is recorded in the events property of Aeeventloop after registering an IO event. Determine the position of the structure in the events property according to the file description

/* File event structure */typedef struct aefileevent {int mask;/* One of Ae_ (readable| writable) Definition of read/write mode */Aefileproc *rfileproc; /* Definition of Read operation callback function */Aefileproc *wfileproc; /* Definition of Write action callback function */void *clientdata; /* User-defined data is used to pass to the callback function */} aefileevent;
Aetimeevent

Aetimeevent describes the main information of Redis time events, the event structure of Redis is a linked list node, and multiple time events form a linked list. Each Redis cycle cycles through the linked list (saved in Aeeventloop.timeeventhead), and after the time calibration, the trigger time event is executed.

/* Time Event structure */typedef struct Aetimeevent {long long ID;/* The ID of the events, generated based on the Aeeventloop.timeeventnextid attribute increment */ Long when_sec; /* The time unit to execute the current event in seconds */long When_ms; /* The time unit to execute the current event in milliseconds */Aetimeproc *timeproc; /* Event Trigger Time execution */Aeeventfinalizerproc *finalizerproc; /* Execute/Void *clientdata when the event execution completes and is removed from the time event list; /* User-defined data is used to pass to the callback function */struct aetimeevent *next; /* point to the next node of the list */} aetimeevent;
Aefiredevent

Aefiredevent holds the file descriptor and read-write mode information for each triggered IO event

/* A fired event */typedef struct aefiredevent {int fd; /* file descriptor for Trigger IO event */int mask; /* Describes the currently triggered read/write mode */} aefiredevent;
Relationship between core structures of 2.ae modules

This section uses a simple picture to express, the specific content can be combined with the above comments to understand (Pictures stolen from @C_Z article:) )

3.ae Module Execution Main process

Create Aeeventloop simultaneously initialize the IO Event list and event event linked lists and related state information while creating Epoll objects

Create time event Set time Event trigger callback function, execution time and add to timeeventhead linked list of main loop

The Create IO event is updated to the corresponding location in the events list of Aeeventloop (based on the file descriptor to locate the offset of the pointer), based on the Epoll listener's read and write status of the file identifier that is set to listen

Mainstream Process Execution

Lazy.. A little alternate chart ....

Executing the AEBEFORESLEEPPROC callback function

Read the distance event for the most recent time event

Execute epoll_wait Listener file descriptor, monitoring timeout until the next time event is triggered or an IO event is triggered

Put the triggered IO event into the aeeventloop.firedevent

Reads the list of triggered IO events and executes the corresponding read and write events

Calibrate the system time and mark all events in the time event list as immediate if the system time and lasttime do not match the actual situation

Executes a callback function for every one by one time events

Depending on the return value of the callback function, whether the current event should be removed from the time event list or reset the next execution time

Execute the AEEVENTFINALIZERPROC callback function if the time event is removed from both tables

Go to the next cycle know aeeventloop.stop = = 0


Redis2.8.9 source src/ae.h src/ae.c src/ae_epoll.c

Redis 2.8.9 Source-AE Module

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.