redis source code analysis (eight) - When you start the Redis, Redis did what

Boot process

  1. Initialization server structure
  2. From the configuration folder loading parameters
  3. Server initialization
  4. Loading persistence file
  5. Start listening event

Initialization server structure

  1. ID server running
  2. Redis default port number used in defined CONFIG_DEFAULT_SERVER_PORT = 6379 server.h
  3. LRU Clock
  4. From the main backup parameters
  5. Command Table
  6. Slow query parameters
  7. Then it will save path and parameters currently executing, using the same parameters in preparation for the server to restart after:server.executable = getAbsolutePath(argv[0]); server.exec_argv = zmalloc(sizeof(char*)*(argc+1)); server.exec_argv[argc] = NULL; for (j = 0; j < argc; j++) server.exec_argv[j] = zstrdup(argv[j]);

The main function calls the initialization function initServer Server Status

  1. Process ID
  2. The client list
  3. From the library list
  4. Create a shared object for common values
  5. Initialization event circulator
  6. Open TCP start listening socket
  7. Create a database server, and initializes the internal state
  8. Created event timer timer serverCron
  9. If you need to use to open the AOF, AOF open the file, and then restore the data
  10. Slow query log initialization module
  11. Initialization Background IO module

Loading persistence file, restore the database

After completion of the initialization state of the server, the server is already an activated state, because there is redis persistent characteristic, the server need to load the appropriate data file to restore the database before. Redis determine which mode is currently open, and if AOF, AOF restore data through a database, otherwise, the load RDB file, restore data by RDB database file.

Start listening event

main function will be to set beforeSleep and afterSleep callback function, and then call aeMain function to start an event loop, start listening for the event. aeMain function is an infinite loop, constantly monitor the arrival of new requests.

/*
 * server启动后,main函数的最终步骤,不断地调用beforesleep和aeProcessEvents
 */
void aeMain(aeEventLoop *eventLoop) {
    eventLoop->stop = 0;
    while (!eventLoop->stop) {
        if (eventLoop->beforesleep != NULL)
            eventLoop->beforesleep(eventLoop);
        aeProcessEvents(eventLoop, AE_ALL_EVENTS|AE_CALL_AFTER_SLEEP);
    }
}

flow chart

  1. Initialization structure
    1. Define the variable
    2. Initialization basic properties
  2. Loading a configuration file settings Properties
  3. Server initialization
    1. Initialize data structures
    2. Common shared objects
  4. Loading support program files, loading data
  5. Start listening event
Published 257 original articles · won praise 223 · views 320 000 +

Guess you like

Origin blog.csdn.net/csdn_kou/article/details/103049618