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

发布时间:2019-11-01 发布网站:大佬教程
大佬教程收集整理的这篇文章主要介绍了Memcached source code analysis -- Analysis of change of state--reference大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

<p align="left">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,/**

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.

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

2 receive connections are distributed to the worker thread

  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

  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

  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

  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

  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:


以上是大佬教程为你收集整理的Memcached source code analysis -- Analysis of change of state--reference全部内容,希望文章能够帮你解决Memcached source code analysis -- Analysis of change of state--reference所遇到的程序开发问题。