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. 【转】提示框第三方库之MBProgressHUD iOS toast效果 动态提示框效果

    原文网址:http://www.zhimengzhe.com/IOSkaifa/37910.html MBProgressHUD是一个开源项目,实现了很多种样式的提示框,使用上简单.方便,并且可以对显 ...

  2. Android Material Design简单使用

    吐槽 作为一个 Android developer,没有什么比拿着 UI 设计的一堆 iOS 风格的设计 来做需求更恶心的了,基本所有空间都要照着 iOS 来画一遍,Material Design 辣 ...

  3. 关于Memcache mutex设计模式的.net实现

    之前在网上看过memcache-mutex的场景分析和实现代码,这里将.net方式加以实现,当然这里主要是依据原文的伪代码照猫画虎,以此做为总结及记录.如果您对相应实现感兴趣可以尝试使用本文提供的代码 ...

  4. SVN - 主干/分支

    一个大项目在开发中可能会拆分成几个小项目,分别分去,同时共通的部分再由人做,做完后再统一合并.同时,在开发中,共通的部分修改后,其它人要保持同步. 这种情况反应到SVN的分支/合并功能上,再贴切不过了 ...

  5. java实现多继承

    方法:  接口+组合 理由:通过接口实现客户端的使用时多继承类的多类, 通过组合实现客户端内部类的实现相关功能(而且有些共用的功能可以不总是多次实现). public interface GMapOb ...

  6. IOS 多线程 NSOperation GCD

    1.NSInvocationOperation NSInvocationOperation * op; NSOperationQueue * que = [[NSOperationQueuealloc ...

  7. JDBCTemplate.java

    package com.pk.xjgs.util; import java.sql.Connection; import java.sql.SQLException; import java.util ...

  8. Python引用传值总结

    Python函数的参数传值使用的是引用传值,也就是说传的是参数的内存地址值,因此在函数中改变参数的值,函数外也会改变. 这里需要注意的是如果传的参数类型是不可改变的,如String类型.元组类型,函数 ...

  9. leetcode@ [34] Search for a Range (STL Binary Search)

    https://leetcode.com/problems/search-for-a-range/ Given a sorted array of integers, find the startin ...

  10. CM 部署bigdata测试环境群集机器报错

    CM repo库info;