码迷,mamicode.com
首页 > 其他好文 > 详细

Memcached source code analysis -- Analysis of change of state--reference

时间:2014-05-01 02:44:07      阅读:571      评论:0      收藏:0      [点我收藏+]

标签:des   cWeb   com   http   style   div   img   code   c   log   t   

This article mainly introduces the process of Memcached, libevent structure of the main thread and worker thread based on the processing of the connection state of mutual conversion (not involving data access operations), the main business logic is the drive_machine. State transition process does not involve all the state, at the same time, because of his ability, some state transition may be wrong, also please predecessors. Conversion conditions: TCP, ASCII protocol. (not including conn_swallow, binary protocols will use this state)

1 Overview

First introduces the connection, connection is connected to the conn Memcached definition of their own. All state, drive_machine () is the main conversion of the state of operation:

enum conn_states { conn_listening, /**<the socket which listens for connections /The main thread waits on the link*/ conn_new_cmd, /**<Prepare connection for next command /workerThreads are waiting for the command*/ conn_waiting, /**<waiting for a readable socket /workerThe thread is waitingsdfA readable information*/ conn_read, /**<reading in a command line /workerThread the read command*/ conn_parse_cmd, /**<try to parse a command from the input buffer /wokerThread*/ conn_write, /**<writing out a simple response /workerThread*/ conn_nread, /**<reading in a fixed number of bytes /workerBecause the command finished thread, Continue to read / conn_swallow, /**<swallowing unnecessary bytes w/o storing /worker thread, ASCIIThe agreement is not involved in this state*/ conn_closing, /**<closing this connection */ conn_mwrite, /**<writing out many items sequentially /workerThread, The response content to * / conn_max_state /**<Max state value (used for assertion) / marker for conn_states boundary, Beyond that states is wrong*/ };

In the Memcached.c (main), the main thread and worker thread to complete the necessary initialization, bind the corresponding libevent events, then the main thread monitor bind monitoring ports, do a good job recycling for formal. Below is the total conversion connection state graph.

mamicode.com,码迷

2 receive connections are distributed to the worker thread

mamicode.com,码迷

 

  1. The main thread initialization, connecting into conn_listening state, waiting for the connection.
  2. When there is a connection, the main thread through the dispatch_conn_new () to pipeline writes characters C, connect the distributed to a worker thread worker thread connection, receive, enter the conn_new_cmd wait state.

3 data read

mamicode.com,码迷

  1. Conn_new_cmd is waiting for the new connection, when the connection is written, if the current request too many, may cause starvation on other worker threads, change event type is EV_WRITE, exit.
  2. If the connection is not much, and have not read data, shows the pending command, a trip to the conn_parse_cmd, in order to continue processing the command.
  3. If the connection is not much, and is a new connection, showed no unread data, is connected into the conn_waiting state waiting for data (command).
  4. conn_waiting, Threads are waiting for subsequent data connection, if a data set to the current socket FD for reading EV_READ, ensure the following content of reading. In conn_read, call try_read_network () to read from socket FD, if no data is returned to conn_waiting, if the data is entered into the conn_parse_cmd, waiting for processing command.

The 4 command parsing

mamicode.com,码迷

  1. After entering the conn_parse_cmd, received the order, call try_read_command () to analyze the user command, this analysis of the ASCII protocol commands, which can call process_command () to handle user command.
  2. According to the user‘s command, will call for different types of orders. Such as set, replace, update_command); incr (command corresponds, decr corresponds to process_arithmetic_command (gets); process_get_command (corresponding) etc.
  3. If the update type command, also need to read the updated content, enter the conn_nread state.
  4. Other types of command has to wait for the results.

The 5 command returns

mamicode.com,码迷

  1. There will be in response to a command execution, if the command only need feedback an execution results, through the out_string () function to output the results, enter conn_write.
  2. If the get command, you need to print the contents of get, into the conn_mwrite stage.
  3. The case statement conn_write without break, so the normal print will be going to conn_mwrite.

The 6 result output

mamicode.com,码迷

  1. Conn_mwrite, call transmit () the response results are output. Transmit () to check the current state of the writing.
  2. If the data is finished, then change the state of waiting for the new command is conn_new_cmd.
  3. If not finished, then continue to wait, until finished.

7 Summary

Above is the conversion and connection status, analysis is not in place or wrong place please. In general, feel the Memcached application of libevent writes very standard, follow up with libevent, but also can learn from Memcached to a lot of knowledge.

reference from:http://www.programering.com/a/MzM3MjMwATU.html

Memcached source code analysis -- Analysis of change of state--reference,码迷,mamicode.com

Memcached source code analysis -- Analysis of change of state--reference

标签:des   cWeb   com   http   style   div   img   code   c   log   t   

原文地址:http://www.cnblogs.com/davidwang456/p/3701881.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!