博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
beanstalk源码剖析——概述
阅读量:6261 次
发布时间:2019-06-22

本文共 23503 字,大约阅读时间需要 78 分钟。

 

beanstalk是什么?

官方网站给出的说明非常简单给力。

Beanstalk is a simple, fast work queue.

Its interface is generic, but was originally designed for reducing the latency of page views in high-volume web applications by running time-consuming tasks asynchronously.

beanstalk是一个简单快速的任务队列,它通过缓存耗时的任务,使其异步执行来减少高容量web应用中页面延迟。更简单的解释如下:

Beanstalkd is a big to-do list for your distributed application. If there is a unit of work that you want to defer to later (say, sending an email, pushing some data to a slow external service, pulling data from a slow external service, generating high-quality image thumbnails) you put a description of that work, a “job”, into beanstalkd. Some processes (such as web request handlers), “producers”, put jobs into the queue. Other processes, “workers”, take jobs out of the queue and run them.

 

beanstalk接口协议

beanstalk的协议是采用ascii编码的,运行在TCP协议之上。

基本的模式类似socket编程,客户端连接到服务器,发送数据或者命令,等待响应,关闭连接。

beanstalk的官方协议如下:

= Beanstalk Protocol =Protocol--------The beanstalk protocol runs over TCP using ASCII encoding. Clients connect,send commands and data, wait for responses, and close the connection. For eachconnection, the server processes commands serially in the order in which theywere received and sends responses in the same order. All integers in theprotocol are formatted in decimal and (unless otherwise indicated)nonnegative.
协议中的名字都采用ASCII编码。Names, in this protocol, are ASCII strings. They may contain letters (A-Z anda-z), numerals (0-9), hyphen ("-"), plus ("+"), slash ("/"), semicolon (";"),dot ("."), dollar-sign ("$"), underscore ("_"), and parentheses ("(" and ")"),but they may not begin with a hyphen. They are terminated by white space(either a space char or end of line). Each name must be at least one characterlong.
协议包含两类数据:文本行(命令)和非结构化数据块(数据)。The protocol contains two kinds of data: text lines and unstructured chunks ofdata. Text lines are used for client commands and server responses. Chunks areused to transfer job bodies and stats information. Each job body is an opaquesequence of bytes. The server never inspects or modifies a job body and alwayssends it back in its original form. It is up to the clients to agree on ameaningful interpretation of job bodies.
退出方式The client may issue the "quit" command, or simply close the TCP connectionwhen it no longer has use for the server. However, beanstalkd performs verywell with a large number of open connections, so it is usually better for theclient to keep its connection open and reuse it as much as possible. This alsoavoids the overhead of establishing new TCP connections.
出错处理If a client violates the protocol (such as by sending a request that is notwell-formed or a command that does not exist) or if the server has an error,the server will reply with one of the following error messages: - "OUT_OF_MEMORY\r\n" The server cannot allocate enough memory for the job.   The client should try again later. - "INTERNAL_ERROR\r\n" This indicates a bug in the server. It should never   happen. If it does happen, please report it at   http://groups.google.com/group/beanstalk-talk. - "BAD_FORMAT\r\n" The client sent a command line that was not well-formed.   This can happen if the line does not end with \r\n, if non-numeric   characters occur where an integer is expected, if the wrong number of   arguments are present, or if the command line is mal-formed in any other   way. - "UNKNOWN_COMMAND\r\n" The client sent a command that the server does not   know.These error responses will not be listed in this document for individualcommands in the following sections, but they are implicitly included in thedescription of all commands. Clients should be prepared to receive an errorresponse after any command.As a last resort, if the server has a serious error that prevents it fromcontinuing service to the current client, the server will close theconnection.
任务生命周期Job Lifecycle-------------A job in beanstalk gets created by a client with the "put" command. During itslife it can be in one of four states: "ready", "reserved", "delayed", or"buried". After the put command, a job typically starts out ready. It waits inthe ready queue until a worker comes along and runs the "reserve" command. Ifthis job is next in the queue, it will be reserved for the worker. The workerwill execute the job; when it is finished the worker will send a "delete"command to delete the job.Here is a picture of the typical job lifecycle:   put            reserve               delete  -----> [READY] ---------> [RESERVED] --------> *poof*Here is a picture with more possibilities:   put with delay               release with delay  ----------------> [DELAYED] <------------.                        |                   |                        | (time passes)     |                        |                   |   put                  v     reserve       |       delete  -----------------> [READY] ---------> [RESERVED] --------> *poof*                       ^  ^                |  |                       |   \  release      |  |                       |    `-------------'   |                       |                      |                       | kick                 |                       |                      |                       |       bury           |                    [BURIED] <---------------'                       |                       |  delete                        `--------> *poof*The system has one or more tubes. Each tube consists of a ready queue and adelay queue. Each job spends its entire life in one tube. Consumers can showinterest in tubes by sending the "watch" command; they can show disinterest bysending the "ignore" command. This set of interesting tubes is said to be aconsumer's "watch list". When a client reserves a job, it may come from any ofthe tubes in its watch list.When a client connects, its watch list is initially just the tube named"default". If it submits jobs without having sent a "use" command, they willlive in the tube named "default".Tubes are created on demand whenever they are referenced. If a tube is empty(that is, it contains no ready, delayed, or buried jobs) and no client refersto it, it will be deleted.
生成者命令Producer Commands-----------------The "put" command is for any process that wants to insert a job into the queue.It comprises a command line followed by the job body:put 
\r\n
\r\nIt inserts a job into the client's currently used tube (see the "use" commandbelow). -
is an integer < 2**32. Jobs with smaller priority values will be scheduled before jobs with larger priorities. The most urgent priority is 0; the least urgent priority is 4,294,967,295. -
is an integer number of seconds to wait before putting the job in the ready queue. The job will be in the "delayed" state during this time. -
-- time to run -- is an integer number of seconds to allow a worker to run this job. This time is counted from the moment a worker reserves this job. If the worker does not delete, release, or bury the job within
seconds, the job will time out and the server will release the job. The minimum ttr is 1. If the client sends 0, the server will silently increase the ttr to 1. -
is an integer indicating the size of the job body, not including the trailing "\r\n". This value must be less than max-job-size (default: 2**16). -
is the job body -- a sequence of bytes of length
from the previous line.After sending the command line and body, the client waits for a reply, whichmay be: - "INSERTED
\r\n" to indicate success. -
is the integer id of the new job - "BURIED
\r\n" if the server ran out of memory trying to grow the priority queue data structure. -
is the integer id of the new job - "EXPECTED_CRLF\r\n" The job body must be followed by a CR-LF pair, that is, "\r\n". These two bytes are not counted in the job size given by the client in the put command line. - "JOB_TOO_BIG\r\n" The client has requested to put a job with a body larger than max-job-size bytes. - "DRAINING\r\n" This means that the server has been put into "drain mode" and is no longer accepting new jobs. The client should try another server or disconnect and try again later.The "use" command is for producers. Subsequent put commands will put jobs intothe tube specified by this command. If no use command has been issued, jobswill be put into the tube named "default".use
\r\n -
is a name at most 200 bytes. It specifies the tube to use. If the tube does not exist, it will be created.The only reply is:USING
\r\n -
is the name of the tube now being used.
消费者命令Worker Commands---------------A process that wants to consume jobs from the queue uses "reserve", "delete","release", and "bury". The first worker command, "reserve", looks like this:reserve\r\nAlternatively, you can specify a timeout as follows:reserve-with-timeout 
\r\nThis will return a newly-reserved job. If no job is available to be reserved,beanstalkd will wait to send a response until one becomes available. Once ajob is reserved for the client, the client has limited time to run (TTR) thejob before the job times out. When the job times out, the server will put thejob back into the ready queue. Both the TTR and the actual time left can befound in response to the stats-job command.A timeout value of 0 will cause the server to immediately return either aresponse or TIMED_OUT. A positive value of timeout will limit the amount oftime the client will block on the reserve request until a job becomesavailable.During the TTR of a reserved job, the last second is kept by the server as asafety margin, during which the client will not be made to wait for anotherjob. If the client issues a reserve command during the safety margin, or ifthe safety margin arrives while the client is waiting on a reserve command,the server will respond with:DEADLINE_SOON\r\nThis gives the client a chance to delete or release its reserved job beforethe server automatically releases it.TIMED_OUT\r\nIf a non-negative timeout was specified and the timeout exceeded before a jobbecame available, the server will respond with TIMED_OUT.Otherwise, the only other response to this command is a successful reservationin the form of a text line followed by the job body:RESERVED
\r\n
\r\n -
is the job id -- an integer unique to this job in this instance of beanstalkd. -
is an integer indicating the size of the job body, not including the trailing "\r\n". -
is the job body -- a sequence of bytes of length
from the previous line. This is a verbatim copy of the bytes that were originally sent to the server in the put command for this job.The delete command removes a job from the server entirely. It is normally usedby the client when the job has successfully run to completion. A client candelete jobs that it has reserved, ready jobs, delayed jobs, and jobs that areburied. The delete command looks like this:delete
\r\n -
is the job id to delete.The client then waits for one line of response, which may be: - "DELETED\r\n" to indicate success. - "NOT_FOUND\r\n" if the job does not exist or is not either reserved by the client, ready, or buried. This could happen if the job timed out before the client sent the delete command.The release command puts a reserved job back into the ready queue (and marksits state as "ready") to be run by any client. It is normally used when the jobfails because of a transitory error. It looks like this:release
\r\n -
is the job id to release. -
is a new priority to assign to the job. -
is an integer number of seconds to wait before putting the job in the ready queue. The job will be in the "delayed" state during this time.The client expects one line of response, which may be: - "RELEASED\r\n" to indicate success. - "BURIED\r\n" if the server ran out of memory trying to grow the priority queue data structure. - "NOT_FOUND\r\n" if the job does not exist or is not reserved by the client.The bury command puts a job into the "buried" state. Buried jobs are put into aFIFO linked list and will not be touched by the server again until a clientkicks them with the "kick" command.The bury command looks like this:bury
\r\n -
is the job id to release. -
is a new priority to assign to the job.There are two possible responses: - "BURIED\r\n" to indicate success. - "NOT_FOUND\r\n" if the job does not exist or is not reserved by the client.The "touch" command allows a worker to request more time to work on a job.This is useful for jobs that potentially take a long time, but you still wantthe benefits of a TTR pulling a job away from an unresponsive worker. A workermay periodically tell the server that it's still alive and processing a job(e.g. it may do this on DEADLINE_SOON).The touch command looks like this:touch
\r\n -
is the ID of a job reserved by the current connection.There are two possible responses: - "TOUCHED\r\n" to indicate success. - "NOT_FOUND\r\n" if the job does not exist or is not reserved by the client.The "watch" command adds the named tube to the watch list for the currentconnection. A reserve command will take a job from any of the tubes in thewatch list. For each new connection, the watch list initially consists of onetube, named "default".watch
\r\n -
is a name at most 200 bytes. It specifies a tube to add to the watch list. If the tube doesn't exist, it will be created.The reply is:WATCHING
\r\n -
is the integer number of tubes currently in the watch list.The "ignore" command is for consumers. It removes the named tube from thewatch list for the current connection.ignore
\r\nThe reply is one of: - "WATCHING
\r\n" to indicate success. -
is the integer number of tubes currently in the watch list. - "NOT_IGNORED\r\n" if the client attempts to ignore the only tube in its watch list.
其他命令Other Commands--------------The peek commands let the client inspect a job in the system. There are fourvariations. All but the first operate only on the currently used tube. - "peek 
\r\n" - return job
. - "peek-ready\r\n" - return the next ready job. - "peek-delayed\r\n" - return the delayed job with the shortest delay left. - "peek-buried\r\n" - return the next job in the list of buried jobs.There are two possible responses, either a single line: - "NOT_FOUND\r\n" if the requested job doesn't exist or there are no jobs in the requested state.Or a line followed by a chunk of data, if the command was successful:FOUND
\r\n
\r\n -
is the job id. -
is an integer indicating the size of the job body, not including the trailing "\r\n". -
is the job body -- a sequence of bytes of length
from the previous line.The kick command applies only to the currently used tube. It moves jobs intothe ready queue. If there are any buried jobs, it will only kick buried jobs.Otherwise it will kick delayed jobs. It looks like:kick
\r\n -
is an integer upper bound on the number of jobs to kick. The server will kick no more than
jobs.The response is of the form:KICKED
\r\n -
is an integer indicating the number of jobs actually kicked.The stats-job command gives statistical information about the specified job ifit exists. Its form is:stats-job
\r\n -
is a job id.The response is one of: - "NOT_FOUND\r\n" if the job does not exist. - "OK
\r\n
\r\n" -
is the size of the following data section in bytes. -
is a sequence of bytes of length
from the previous line. It is a YAML file with statistical information represented a dictionary.The stats-job data is a YAML file representing a single dictionary of stringsto scalars. It contains these keys: - "id" is the job id - "tube" is the name of the tube that contains this job - "state" is "ready" or "delayed" or "reserved" or "buried" - "pri" is the priority value set by the put, release, or bury commands. - "age" is the time in seconds since the put command that created this job. - "time-left" is the number of seconds left until the server puts this job into the ready queue. This number is only meaningful if the job is reserved or delayed. If the job is reserved and this amount of time elapses before its state changes, it is considered to have timed out. - "file" is the number of the earliest binlog file containing this job. If -b wasn't used, this will be 0. - "reserves" is the number of times this job has been reserved. - "timeouts" is the number of times this job has timed out during a reservation. - "releases" is the number of times a client has released this job from a reservation. - "buries" is the number of times this job has been buried. - "kicks" is the number of times this job has been kicked.The stats-tube command gives statistical information about the specified tubeif it exists. Its form is:stats-tube
\r\n -
is a name at most 200 bytes. Stats will be returned for this tube.The response is one of: - "NOT_FOUND\r\n" if the tube does not exist. - "OK
\r\n
\r\n" -
is the size of the following data section in bytes. -
is a sequence of bytes of length
from the previous line. It is a YAML file with statistical information represented a dictionary.The stats-tube data is a YAML file representing a single dictionary of stringsto scalars. It contains these keys: - "name" is the tube's name. - "current-jobs-urgent" is the number of ready jobs with priority < 1024 in this tube. - "current-jobs-ready" is the number of jobs in the ready queue in this tube. - "current-jobs-reserved" is the number of jobs reserved by all clients in this tube. - "current-jobs-delayed" is the number of delayed jobs in this tube. - "current-jobs-buried" is the number of buried jobs in this tube. - "total-jobs" is the cumulative count of jobs created in this tube in the current beanstalkd process. - "current-waiting" is the number of open connections that have issued a reserve command while watching this tube but not yet received a response. - "pause" is the number of seconds the tube has been paused for. - "cmd-delete" is the cumulative number of delete commands for this tube - "cmd-pause-tube" is the cumulative number of pause-tube commands for this tube. - "pause-time-left" is the number of seconds until the tube is un-paused.The stats command gives statistical information about the system as a whole.Its form is:stats\r\nThe server will respond:OK
\r\n
\r\n -
is the size of the following data section in bytes. -
is a sequence of bytes of length
from the previous line. It is a YAML file with statistical information represented a dictionary.The stats data for the system is a YAML file representing a single dictionaryof strings to scalars. Entries described as "cumulative" are reset when thebeanstalkd process starts; they are not stored on disk with the -b flag. - "current-jobs-urgent" is the number of ready jobs with priority < 1024. - "current-jobs-ready" is the number of jobs in the ready queue. - "current-jobs-reserved" is the number of jobs reserved by all clients. - "current-jobs-delayed" is the number of delayed jobs. - "current-jobs-buried" is the number of buried jobs. - "cmd-put" is the cumulative number of put commands. - "cmd-peek" is the cumulative number of peek commands. - "cmd-peek-ready" is the cumulative number of peek-ready commands. - "cmd-peek-delayed" is the cumulative number of peek-delayed commands. - "cmd-peek-buried" is the cumulative number of peek-buried commands. - "cmd-reserve" is the cumulative number of reserve commands. - "cmd-use" is the cumulative number of use commands. - "cmd-watch" is the cumulative number of watch commands. - "cmd-ignore" is the cumulative number of ignore commands. - "cmd-delete" is the cumulative number of delete commands. - "cmd-release" is the cumulative number of release commands. - "cmd-bury" is the cumulative number of bury commands. - "cmd-kick" is the cumulative number of kick commands. - "cmd-stats" is the cumulative number of stats commands. - "cmd-stats-job" is the cumulative number of stats-job commands. - "cmd-stats-tube" is the cumulative number of stats-tube commands. - "cmd-list-tubes" is the cumulative number of list-tubes commands. - "cmd-list-tube-used" is the cumulative number of list-tube-used commands. - "cmd-list-tubes-watched" is the cumulative number of list-tubes-watched commands. - "cmd-pause-tube" is the cumulative number of pause-tube commands - "job-timeouts" is the cumulative count of times a job has timed out. - "total-jobs" is the cumulative count of jobs created. - "max-job-size" is the maximum number of bytes in a job. - "current-tubes" is the number of currently-existing tubes. - "current-connections" is the number of currently open connections. - "current-producers" is the number of open connections that have each issued at least one put command. - "current-workers" is the number of open connections that have each issued at least one reserve command. - "current-waiting" is the number of open connections that have issued a reserve command but not yet received a response. - "total-connections" is the cumulative count of connections. - "pid" is the process id of the server. - "version" is the version string of the server. - "rusage-utime" is the cumulative user CPU time of this process in seconds and microseconds. - "rusage-stime" is the cumulative system CPU time of this process in seconds and microseconds. - "uptime" is the number of seconds since this server process started running. - "binlog-oldest-index" is the index of the oldest binlog file needed to store the current jobs - "binlog-current-index" is the index of the current binlog file being written to. If binlog is not active this value will be 0 - "binlog-max-size" is the maximum size in bytes a binlog file is allowed to get before a new binlog file is opened - "binlog-records-written" is the cumulative number of records written to the binlog - "binlog-records-migrated" is the cumulative number of records written as part of compactionThe list-tubes command returns a list of all existing tubes. Its form is:list-tubes\r\nThe response is:OK
\r\n
\r\n -
is the size of the following data section in bytes. -
is a sequence of bytes of length
from the previous line. It is a YAML file containing all tube names as a list of strings.The list-tube-used command returns the tube currently being used by theclient. Its form is:list-tube-used\r\nThe response is:USING
\r\n -
is the name of the tube being used.The list-tubes-watched command returns a list tubes currently being watched bythe client. Its form is:list-tubes-watched\r\nThe response is:OK
\r\n
\r\n -
is the size of the following data section in bytes. -
is a sequence of bytes of length
from the previous line. It is a YAML file containing watched tube names as a list of strings.The quit command simply closes the connection. Its form is:quit\r\nThe pause-tube command can delay any new job being reserved for a given time. Its form is:pause-tube
\r\n -
is the tube to pause -
is an integer number of seconds to wait before reserving any more jobs from the queueThere are two possible responses: - "PAUSED\r\n" to indicate success. - "NOT_FOUND\r\n" if the tube does not exist.

使用方法

beanstalk服务器启动采用了命令行参数,没有配置文件。

例如

beanstalkd -l 127.0.0.1 -p 11300

客户端

>>> import beanstalkc>>> beanstalk = beanstalkc.Connection(host='localhost', port=11300)
>>> beanstalk.tubes()
>>> beanstalk.using()
>>> beanstalk.put('hello, beanstalk!')
>>> job = beanstalk.reserve()
>>> job.body
>>> job.delete()

 

 

 

转载于:https://www.cnblogs.com/blockcipher/archive/2012/06/03/2912982.html

你可能感兴趣的文章
11月20日学习内容整理:jquery插件
查看>>
SVN与TortoiseSVN实战:补丁详解
查看>>
获取页面中所有dropdownlist类型控件
查看>>
读《淘宝数据魔方技术架构解析》有感
查看>>
[转载]如何破解Excel VBA密码
查看>>
【BZOJ】2563: 阿狸和桃子的游戏
查看>>
redis 中文字符显示
查看>>
顺序图【6】--☆☆
查看>>
Docker Swarm 让你事半功倍
查看>>
javaScript事件(四)event的公共成员(属性和方法)
查看>>
An easy to use android color picker library
查看>>
Oracle SID爆破工具SidGuess
查看>>
批处理常用命令总结2
查看>>
Android -- 自定义View小Demo,绘制钟表时间(一)
查看>>
信息检索Reading List
查看>>
自动精简配置&重复数据删除核心技术点及其经济效应探究
查看>>
cncert网络安全周报35期 境内被植入后门的政府网站112个 环比上涨24.4%
查看>>
物联网到底是不是泡沫,且看英特尔交出的答案
查看>>
IPv6太落后了:中国加速服务器援建
查看>>
物理引擎中velocity的单位是个什么鬼?
查看>>