This is the 14th day of my participation in the August More Text Challenge. For details, see:August is more challenging

A brief introduction to time events

Redis time events fall into the following two categories:

  • Timed event: To allow a program to be executed once after a specified time.
  • Periodic event: Causes a program to be executed at specified intervals.

A time event consists of the following three attributes: 1. Id: A globally unique ID (ID) created by the server for the time event. The ID number increases from small to large. The ID number of the new event is larger than that of the old event. 2. When: A UNIX timestamp with millisecond accuracy that records the time when a time event arrives. TimeProc: time event handler, a function. When the time arrives, the server calls the appropriate handler to handle the event.

Whether a time event is timed or periodic depends on the value returned by the time event handler:

  • If the event handler returns Ae. h/AE_NOMORE, the event is a timed event; The event will be deleted after reaching it once and will not reach it again.
  • If the event handler returns an integer value other than AE_NOMORE, then the event is periodic; When a time event arrives, the server updates the when property of the time event based on the value returned by the event handler, making the event arrive again after a certain period of time, and keeps updating and running in this manner. For example, if the handler of a time event returns the integer value 30, the server should update the time event so that it arrives again after 30 milliseconds.

1.1 Implementation Mode

The server puts all the time events in an unordered list, and whenever the time event executor runs, it traverses the list to find all the time events that have arrived and calls the corresponding event handler.

The following figure shows an example of a linked list of time events. The list contains three different time events; Because new time events are always inserted into the list header, the three time events are sorted by ID in reverse order.

Note that an unordered list does not mean that the list is not sorted by ID, but rather that the list is not sorted by the size of the WHEN attribute. Because the list is not sorted by the when attribute, when the time event executor runs, it must traverse all the time events in the list to ensure that all the time events that have arrived in the server are processed.

1.2 Implementation of time event application: serverCron function

The Redis server needs to check and adjust its own resources and status regularly, so as to ensure that the server can run for a long time and stably. This periodic operation is performed by the Redis. C /serverCron function, which mainly includes:

  • Update server statistics such as time, memory usage, database usage, and so on.
  • Clean up expired key-value pairs in the database.
  • Close and clean the client with connection failure.
  • An AOF or RDB persistence operation is attempted.
  • If the server is the master, synchronize the slave server periodically.
  • If you are in cluster mode, perform periodic synchronization and connection tests on the cluster.

The Redis server runs the serverCron function as a periodic event, and serverCron executes at intervals during the server run until the server is shut down.

2. Event scheduling and execution

Since there are both file and time event types in the server, the server must schedule them, decide when to process file events, when to process time events, how much time to process them, and so on.

The height and execution of events are taken care of by the ae.c/aeProcessEvents function, which is represented in pseudocode as follows:

def aeProcessEvents(a): # Get the event time_event whose arrival time is closest to the current timeRemaing_ms = time_event. -unix_ts_now () # If the event has arrived, Then the value of reming_ms might be negative. Set it to0
    if remind_ms < 0:
        remind_ms = 0Timeval = create_timeval_with_ms(remaind_ms) Maximum blocking time is determined by the timeval structure passed in # If the value of remaind_ms is0AeApiPoll (timeval) # Process all generated file events processFileEvents() # Process all arrived time events processTimeEvent()Copy the code

The main function of the Redis server is created by putting the aeProcessEvents function inside a loop, along with the initialization and cleanup functions. Here is the pseudo-code representation of the function:

def main(a): # Initialize the serverinit_server(a)# Handle events until the server is shut downwhile server_is_not_shutdown(a):
        aeProcessEvents(a)# The server is down and the cleanup operation is performedclean_server(a)
Copy the code

From the perspective of event processing, the running process of Redis server can be summarized in the following flowchart: