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.

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:http://www.programering.com/a/MzM3MjMwATU.html

Memcached source code analysis -- Analysis of change of state--reference的更多相关文章

  1. Memcached source code analysis (threading model)--reference

    Look under the start memcahced threading process memcached multi-threaded mainly by instantiating mu ...

  2. LIRE教程之源码分析 | LIRE Tutorial of Analysis of the Source Code

    LIRE教程之源码分析 |LIRE Tutorial of Analysis of the Source Code 最近在做地理图像识别和检索的研究,发现了一个很好用的框架LIRE,遂研究了一通.网上 ...

  3. Eclipse Error: The refactoring does not change any source code

    最近在做android项目的过程中遇到这样一个问题,新增一个activity的时候添加不成,eclipse提示The refactoring does not change any source co ...

  4. Steps of source code change to executable application

    程序运行的整个过程,学习一下 源代码 (source code) → 预处理器 (preprocessor) → 编译器 (compiler) → 汇编程序 (assembler) → 目标代码 (o ...

  5. [转]Native Java Bytecode Debugging without Source Code

    link from:http://www.crowdstrike.com/blog/native-java-bytecode-debugging-without-source-code/index.h ...

  6. How to compile and install Snort from source code on Ubuntu

    http://www.tuicool.com/articles/v6j2Ab Snort is by far the most popular open-source network intrusio ...

  7. Source Code Reading for Vue 3: How does `hasChanged` work?

    Hey, guys! The next generation of Vue has released already. There are not only the brand new composi ...

  8. How to build the Robotics Library from source code on Windows

    The Robotics Library is an open source C++ library for robot kinematics, motion planning and control ...

  9. Artistic Style 3.1 A Free, Fast, and Small Automatic Formatter for C, C++, C++/CLI, Objective‑C, C#, and Java Source Code

    Artistic Style - Index http://astyle.sourceforge.net/ Artistic Style 3.1 A Free, Fast, and Small Aut ...

随机推荐

  1. Mysql主从复制的实现

    MySQL是一个开放源码的小型关联式数据库管理系统,开发者为瑞典MySQL AB公司.MySQL被广泛地应用在Internet上的中小型网站中.由于其体积小.速度快.总体拥有成本低,尤其是开放源码这一 ...

  2. Hibernate4.x之映射关系--一对一映射

    Hibernate的1-1映射关系主要分为两类: 1.Hibernate基于外键映射的1对1关联关系 对于基于外键的1-1关联,其外键可以存放在任意一边,在需要存放外键一端,增加many-to-one ...

  3. ASP.NET工作笔记之一:图片上传预览及无刷新上传

    转自:http://www.cnblogs.com/sibiyellow/archive/2012/04/27/jqueryformjs.html 最近项目里面涉及到无刷新上传图片的功能,其实也就是上 ...

  4. 将java应用程序打包成可执行文件

    准备工作:我用的打包软件是installanywhere.因此要先下载这个软件,这里提供一个下载地址http://www.52z.com/soft/21747.html.(网页上写的有破解方法) 详细 ...

  5. 积和式Permanent在Mathematica中的运算

    Permanent[m_List] := With[{v = Array[x, Length[m]]}, Coefficient[Times @@ (m.v), Times @@ v]] 参考资料:  ...

  6. [九度OJ]1113.二叉树(求完全二叉树任意结点所在子树的结点数)

    原题链接:http://ac.jobdu.com/problem.php?pid=1113 题目描述: 如上所示,由正整数1,2,3……组成了一颗特殊二叉树.我们已知这个二叉树的最后一个结点是n.现在 ...

  7. NOIP2004 合并石子

    二.合并果子 (fruit.pas/dpr/c/cpp) [问题描述] 在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.多多决定把所有的果子合成一堆. 每一次合并,多多 ...

  8. 28个MongoDB NoSQL数据库的面试问答

    MongoDB是目前最好的面向文档的免费开源NoSQL数据库.如果你正准备参加MongoDB NoSQL数据库的技术面试,你最好看看下面的MongoDB NoSQL面试问答.这些MongoDB NoS ...

  9. [转]jquery.vTicker(垂直滚动)

    转至:http://www.w3ci.com/plugin/660.html 简介 vTicker 是一款非常小巧的 jQuery 垂直滚动插件,压缩后只有 2KB.vTicker 支持自定义滚动时间 ...

  10. SCALA编程实例

    SCALA与JAVA很相似,包括类.函数.集合等等的使用.如果你是一个JAVA程序员,你应该会很快上手. 需要注意的是SCALA特有的一些奇葩标志,比如->,比如=>,遇到要注意下. 使用 ...