Memcached source code analysis -- Analysis of change of state--reference
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
- The main thread initialization, connecting into conn_listening state, waiting for the connection.
- 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
- 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.
- 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.
- 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).
- 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
- 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.
- 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.
- If the update type command, also need to read the updated content, enter the conn_nread state.
- Other types of command has to wait for the results.
The 5 command returns
- 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.
- If the get command, you need to print the contents of get, into the conn_mwrite stage.
- The case statement conn_write without break, so the normal print will be going to conn_mwrite.
The 6 result output
- Conn_mwrite, call transmit () the response results are output. Transmit () to check the current state of the writing.
- If the data is finished, then change the state of waiting for the new command is conn_new_cmd.
- 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的更多相关文章
- Memcached source code analysis (threading model)--reference
Look under the start memcahced threading process memcached multi-threaded mainly by instantiating mu ...
- LIRE教程之源码分析 | LIRE Tutorial of Analysis of the Source Code
LIRE教程之源码分析 |LIRE Tutorial of Analysis of the Source Code 最近在做地理图像识别和检索的研究,发现了一个很好用的框架LIRE,遂研究了一通.网上 ...
- Eclipse Error: The refactoring does not change any source code
最近在做android项目的过程中遇到这样一个问题,新增一个activity的时候添加不成,eclipse提示The refactoring does not change any source co ...
- Steps of source code change to executable application
程序运行的整个过程,学习一下 源代码 (source code) → 预处理器 (preprocessor) → 编译器 (compiler) → 汇编程序 (assembler) → 目标代码 (o ...
- [转]Native Java Bytecode Debugging without Source Code
link from:http://www.crowdstrike.com/blog/native-java-bytecode-debugging-without-source-code/index.h ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Android 自动换行流式布局的RadioGroup
效果图 用法 使用FlowRadioGroup代替RadioGroup 代码 import android.content.Context; import android.util.Attribute ...
- Retrofit – Java(Android) 的REST 接口封装类库
Retrofit 和Java领域的ORM概念类似, ORM把结构化数据转换为Java对象,而Retrofit 把REST API返回的数据转化为Java对象方便操作.同时还封装了网络代码的调用. 例如 ...
- 29、activity横竖屏切换细节问题
1 import android.app.Activity; import android.content.Intent; import android.os.Bundle; import andro ...
- UVA 11488-Hyper Prefix Sets(Trie)
题意: 给一个01串的集合,一个集合的幸运值是串的个数*集合中串的最大公共前缀 ,求所有子集中最大幸运值 分析: val[N]表示经过每个节点串的个数求幸运值 求就是每个节点值*该节点的深度 搜一遍树 ...
- codeforces 672C - Recycling Bottles 贪心水题
感觉很简单,就是讨论一下 #include <stdio.h> #include <string.h> #include <algorithm> #include ...
- flash player 版本对照
- 《Genesis-3D开源游戏引擎完整实例教程-跑酷游戏篇02:内购如何实现》
2.内购如何实现 内购概述: 游戏内购是指玩家在游戏中,用金钱获取游戏道具的一种方式.开发者从玩家内购之中获得开发游戏的收益,因此大量游戏都有内购模块.下面我们以在手机IOS平台实现跑酷游戏内购为例, ...
- 最近升级了一下小老婆(8核 2x8G DDR3 128G SSD)
晒图(笔者的硬件知识属于小白级别, 且看且参考吧): 另外优化一下休眠&虚拟内存功能节省SSD空间: 1. 台式机, 休眠功能基本没必要, 果断关掉 C:\Windows\system32&g ...
- ListView自定义适配器--10.17
1. 添加button 2. ViewHolder 优化性能 就是一个持有者的类,他里面一般没有方法,只有属性,作用就是一个临时的储存器,把你getView方法中每次返回的View存起来,可以下次再用 ...
- Java修饰符关键词大全
所以我以此主题写了这篇文章.这也是一个可用于测试你的计算机科学知识的面试问题. Java修饰符是你添加到变量.类和方法以改变其含义的关键词.它们可分为两组: 访问控制修饰符 非访问修饰符 让我们先来看 ...