Memcached State Machine Analysis

Source: Internet
Author: User
Tags memcached

Workerafter the thread has got this connection, it should be assigned to this connection to a struct, including all the states of this connection, all writtenBufand so on, this structure isConn, and then thisWorkerthread will be in its ownEvent_basejoin the listener for the event of this new connection. That's what it says.Workerof theEvent_baseThere are two sets of processing logic, one forNotify_ RECEIVE_FD, and a set of new connections. ThisNotify_ RECEIVE_FDThe processing logic is to deal with2events, one is to establish a connection, and the other is to change the granularity of the lock. The connection has a state machine:

1.listening: This state is the connection default state of the main thread , it only has this state, and it does the job of distributing the connection to the worker sub-thread.

2.Conn_new_cmd: The initial state of each new connection, this state clears the read-write buf.

3.conn_waiting: This state is to set the Read event in the Event_base, then the state machine pauses, hangs the current connection (function exits, The attachment of the callback function will record the connection), wait for the new information to come over, and then re-locate the connection through the attachment of the callback function , and then start the state machine.

4.conn_read: This state reads the client's instruction information from the SFD.

5.conn_parse_cmd: update conn_nread< Span style= "Font-size:small;" > byte< Span style= "Font-size:small;" > conn_mwrite

6.conn_nread: item hash lru Conn_write

7.conn_write: out_string< Span style= "Font-size:small;" > write_to_go

8.conn_mwrite: This write is to return the connection in the msglist to the client, this msglist is the data of the item , used for that kind of get returns data for the operation that gets the item .

9.conn_swallow:for thatUpdateoperation, if assignedItemfailed, apparently behind theNread, is invalid, the client is not aware of, so that the client continues to send a specific amount of data, you need to read the data is ignored, and then if the data specified later ignored (set two commits, the data part is ignored), thenConnectionJump toConn_new_cmd, if you readNreadthe specific amount of data is not read and jumps directly toConn_closing.

10.conn_closing: The server side actively closes the connection, calls the close function to close the file descriptor, and puts the conn struct in the idle queue for new connections to reuse the write conn Structural body.

Summary:Memcachedthe event model of the network module relies on theLibeventthe implementation,MemcachedputFdthe event of concern is registered toLibeventand registers a callback function,Libeventresponsible for callbacksMemcached, the main thread connectsDispatchto the specificWorkerthread, while registering the descriptor for this connectionGiveWorkerthread own set ofLibevent, soWorkerThe connection was received, and later thisFdon the event callback is the work done on this thread, each connection has its own set of state machine, if the data received through the state machine processing, State twist, if there is no data to temporarily suspend the connection, wait until the data continue to execute state machine.

Memcached State Machine Analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.