Interpretation of BEANSTALKD protocol in Linux (Chinese translation plus personal understanding)

Source: Internet
Author: User
Tags reserved touch

Recently there is a need to do a full platform timer, business complex, high performance requirements, Linux crontab way does not apply, research some support for the timing of the memory queue system, which beanstalkd more appropriate. It's not a problem to study the protocol first.
Umbrella
The BEANSTALKD protocol runs on TCP based on ASCII encoding. The client connects to the server and sends instructions and data, then waits for a response and closes the connection. For each connection, the server processes and responds according to the sequence of receive commands. All integer values are nonnegative decimal digits, unless there is a special declaration.
Name conventions
All names must be ASCII strings, which include:
Letters (A-Z and A-Z)
Number (0-9)
Plus +
Slash
Hyphen-
Semicolon
Point.
Dollar sign $
Underline _
Brackets ()
Note: The name cannot start with a hyphen and ends with a blank character, with at least one character for each name.
Error description
1
Description of the error returned
2

3
The out_of_memory\r\n server does not have enough memory allocated to a particular job, and the client should try again later
4

5
internal_error\r\n Server Internal error, this error should not occur, if it occurs, please report: Http://groups.google.com/group/beanstalk-talk.
6

7
bad_format\r\n format is incorrect, the instructions sent by the client are malformed, may not end with \ r \ n, or require integer values, etc.
8

9
unknown_command\r\n Unknown command, the client sent the instruction server does not understand

The life cycle of a job
A job task job is created when the client uses the put command. The job may have four working statuses throughout the lifecycle: ready,reserved,delayed,buried. After putting, the typical state of a job is ready, and in the ready queue, it waits for a worker to take out the job and set it to the reserved state. The worker takes possession of the job and executes it, and when the job completes, the worker can send a delete instruction to delete the job.
1
Status Code Description
2

3
Ready waiting to be taken out and processed
4

5
Reserved if a job is taken out by a worker, the worker will subscribe to it and the worker performs the job
6

7
Delayed wait for a specific time, the state is migrated to the ready state
8

9
Buried wait for wakeup, usually when job processing fails
Job-typical life cycle
Put reserve Delete
---------> [READY]----------------> [RESERVED]------------> *poof*
Job possible State Migration

Tubes
A well-known task queue, a server has one or I tubes, used to store a unified type of job. Each tube consists of a ready queue and a deferred queue. All state migrations for each job are completed in one tube. Consumers consumers can monitor the tube of interest by sending watch instructions. Consumers consumers can cancel the monitoring tube by sending ignore commands. Returns all monitored tubes through the watch List command, and when the client subscribes to a job, the job may come from any one of the tube it monitors.
When a client connects to the server, the client-monitored tube defaults to defaut, and if the client submits the job without using the use command, the job is stored in the tube named default.
Tube are created on demand, wherever they are referenced in the place. If a tube becomes empty (that is, no ready jobs,no delayed jobs,no jobs) and no client references, it will be automatically deleted.

Instruction description (Commands)
Producer instruction Note (Producer Commands)
Put
Description
Insert a job to queue
Format
1
Put <pri> <delay> <ttr> <bytes>\r\n
2
<data>\r\n
The PRI integer value, which is the priority, can be 0-2^32, and the smaller the value, the higher the priority, and the default is 1024.
Delay integer value, delay ready number of seconds, during which job is delayed state.
TTR integer value that allows the maximum number of seconds that a worker can perform, if the worker cannot delete,release,bury a job during this period, the job times out, the server will release the job, and the job's status is migrated to ready. The minimum is 1 seconds, and if the client specifies 0, it will be reset to 1.
Bytes integer value, the job body length, does not contain \ r \ n, this value must be less than max-job-size, default to 2^16.
Data Job Body
Response
Inserted\r\n indicates that the insert job was successful, the ID is the task identification for the new job, and the integer value
Buried\r\n such as the server to increase the priority of the queue, when there is not enough memory to return, ID for the new Job task identification, integer value
expected_crlf\r\n job body must end with \ r \ n
The length of the job_too_big\r\n job body exceeds max-job-size
Draining\r\n indicates that the server is running out of resources, indicating that the server has entered "drain mode", that the server can no longer accept the connection, that the client should use another server or disconnect and try again later
Use
Description
Producer producers use, and then use the put command to place the job in the corresponding tube
Format
1
Use <tube>\r\n
The name of the tube tube, with a maximum of 200 bytes, which is automatically created when it does not exist
Response
1
Using <tube>\r\n tube is the name of the tube being used
Consumer instruction Note (Worker Commands)
Reserve
Description
Remove (subscribe) job, pending processing. It will return a newly subscribed job and send a response if there is no job,beanstalkd until there is a job. Once the job state is migrated to reserved, the client that takes out the job is limited to the specified time (if TTR is set), otherwise the timeout, the job state reload is migrated to ready.
Format
1
reserve\r\n
Optional one similar command
reserve-with-timeout\r\n sets the timeout for the fetch job, timeout is set to 0 o'clock, the server responds immediately or timed_out, and a positive setting timeout limits the time that the client will block the request for the job.
Response
deadline_soon\r\n During the TTR of a reserved job, the last second was kept by the server as a safety margin, During which The client is not being made to a for another job. If the client issues a reserve command during the safety margin, or if the safety margin arrives while the client is Waiti Ng on a reserve command.
timed_out\r\n Timeout
1
RESERVED <id> <bytes>\r\n
2
<data>\r\n
Successfully removed Job,id for job ID, integer value, job body length, does not contain \r\n,data for job body
Delete
Description
Remove a job from the queue
Format
1
Delete <id>\r\n
ID is job ID
Response
deleted\r\n Delete Succeeded
When the not_found\r\n job does not exist, or the job's status is not ready and buried (in this case, the client sends the delete instruction before the job execution times out)
Release
Description
The release instruction puts a reserved job back in the ready queue. It is usually used when job execution fails.
Format
1
Release <id> <pri> <delay>\r\n
The ID is the job Id,pri as the job priority, delay is the number of seconds to delay ready
Response
Released\r\n shows success
Buried\r\n such as the server to increase the priority of the queue and not enough memory to return
not_found\r\n if the job does not exist or the client does not subscribe to this job
Bury
Description
Migrate the state of a job to buried and wake with the kick command
Format
1
Bury <id> <pri>\r\n
ID is job ID,PRI priority
Response
Buried\r\n shows success
not_found\r\n if the job does not exist or the client does not subscribe to this job
Touch
Description
Allow the worker to request more time to execute the job, which is useful when the job takes a long time to execute, the worker can tell the server that it is still executing the job (which could be triggered by Deadline_soon).
Format
1
Touch <id>\r\n
ID is job ID
Response
Touched\r\n shows success
not_found\r\n if the job does not exist or the client does not subscribe to this job
Watch
Description
Add the monitored tube to the watch list, the reserve directive will get the job from the monitored tube list, and for each connection, the list of controls defaults to default
Format
1
Watch <tube>\r\n
Tube is the tube name for monitoring, with a maximum name of 200 bytes and automatically created if tube does not exist
Response
1
Watching <count>\r\n indicates success
Count integer value, number of monitored tube
Ignore
Description
To remove a specific tube from the list of monitored watch list
Format
1
Ignore <tube>\r\n
Tube is the tube name to be removed, with a maximum name of 200 bytes and automatically created if tube does not exist
Response
1
Watching <count>\r\n indicates success
Count integer value, number of monitored tube
not_ignored\r\n If the client attempts to ignore its only tube response
Other instructions (other command)
Peek
Description
Let the client check the job in the system, there are four forms of command, where the first form of instruction is for the currently used tube
Format
1
Peek <id>\r\n returns the job for ID
2

3
peek-ready\r\n return to the next ready job
4

5
peek-delayed\r\n returns the job with the shortest delay remaining time
6

7
peek-buried\r\n returns the next job in the buried list
Response
not_found\r\n if the job does not exist, or if there is no job in the corresponding state
1
FOUND <id> <bytes>\r\n <data>\r\n
ID is the corresponding job ID
Number of bytes in bytes job body
Data is Job body
Kick
Description
This directive is applied in the currently used tube, which migrates the state of the job to ready or delayed
Format
kick\r\n
Bound integer value, wake up job cap
Response
1
Kicked <count>\r\n
Count is the number of jobs that are actually awakened
Kick-job
Description
A variant of the kick directive that enables a single job to be awakened so that a job with a status of buried or delayed is migrated to ready and all state migrations are done in the same tube
Format
1
Kick-job <id>\r\n
ID is job ID
Response
not_found\r\n if the job does not exist, or the job is not awakened
Kicked\r\n shows success
Stats-job
Description
Statistics Job related information
Format
1
Stats-job <id>\r\n
ID is job ID
Response
1
not_found\r\n if the job does not exist
2

3
OK <bytes>\r\n<data>\r\n
Bytes is the length of the next data block
Data is Yaml file's statistics
Where Yaml file includes the key:
ID indicates job ID
Tube represents the name of the tube
State indicates the current status of the job
The PRI represents the priority of the job
Age indicates the time unit seconds that the job was created
Time-left represents the time when the status of a job is migrated to ready, only if the job state is reserved or delayed, and the remaining timeout when the job status is reserved.
File represents the Binlog ordinal that contains the job, or 0 if it is not turned on
Reserves indicates the number of times the job was reserved
Timeouts indicates the timeout for job processing
Releases indicates the number of times the job was released
Buries indicates the number of times the job was buried
Kicks indicates the number of times the job was kiced
Stats-tube
Description
Statistics Tube Related information
Format
1
Stats-tube <tube>\r\n
Tube is the name of the corresponding tube, up to 200 bytes
Response
1
not_found\r\n If tube does not exist
2

3
OK <bytes>\r\n<data>\r\n
Bytes is the length of the next data block
Data is Yaml file's statistics
Where Yaml file includes the key:
Name denotes tube
Current-jobs-urgent the number of jobs in this tube with a priority less than 1024 status ready
Current-jobs-ready the number of jobs in this tube with status ready
Current-jobs-reserved the number of jobs in this tube with status reserved
Current-jobs-delayed the number of jobs in this tube with status delayed
Current-jobs-bureid the number of jobs in this tube with status buried
Total-jobs all job numbers created in this tube
Current-using number of connections opened using this tube
Current-wating the number of connections that use this tube to open a connection and wait for a response
Current-watching an open connection to monitor the number of this tube
Pause the number of seconds this tube pauses
Cmd-delete The total number of delete instructions executed in this tube
Cmd-pause-tube the total number of pause-tube instructions performed in this tube
Pause-time-left This tube pause the remaining number of seconds

Stats
Description
Returns the overall information for the entire Message Queuing system
Format
1
stats\r\n
Response
1
OK <bytes>\r\n<data>\r\n
Bytes is the length of the next data block
Data is Yaml file's statistics
Where Yaml file includes the key (all the information is cumulative, since the BEANSTALKD process started, this information is not stored in the Binlog):
Current-jobs-urgent priority is less than 1024 job number with Ready status
Current-jobs-ready status is ready job number
Current-jobs-reserved status is reserved job number
Current-jobs-delayed status is delayed job number
Current-jobs-bureid status is buried job number
Cmd-put The total number of put instructions executed
Cmd-peek Total number of PEEK instructions executed
Cmd-peek-ready total number of peek-ready instructions executed
cmd-peek-delayed total number of peek-delayed instructions executed
Cmd-peek-buried total number of peek-buried instructions executed
Cmd-reserve number of total execution of the reserve directive
Cmd-use The total number of execution use instructions
Cmd-watch total number of watch instructions executed
Cmd-ignore total number of ignore instructions executed
Cmd-release Total number of release instructions executed
Cmd-bury total number of Bury instructions executed
Cmd-kick total number of kick instructions executed
Cmd-stats total number of stats instructions executed
Cmd-stats-job total number of stats-job instructions executed
Cmd-stats-tube total number of stats-tube instructions executed
Cmd-list-tubes total number of list-tubes instructions executed
cmd-list-tube-used total number of list-tube-used instructions executed
cmd-list-butes-watched total number of list-tubes-watched instructions executed
Cmd-pause-tube total number of pause-tube instructions executed
Job-timeouts The total number of all job timeouts
Total-jobs all job Created
Maximum length of data portion of Max-job-size job
Current-tubes Current number of tube
Current-connections the number of connections currently open
Current-producers the number of connections at least one put instruction is executed at the current all open connections
Current-workers the number of connections currently executing at least one reserve instruction in all open connections
Current-waiting the number of connections executing the Reserve directive but not responding in all current open connections
Total-connections total number of connections processed
ID of the PID server process
Version Server Edition number
Total user CPU time consumed by the rusage-utime process
Total system CPU time consumed by the rusage-stime process
Uptime the number of seconds a server process is running
Binlog-oldest-index began to store Jobs ' Binlog index number
Binlog-current-index currently stores Jobs ' Binlog index number
Maximum capacity of Binlog-max-size Binlog
Binlog-records-written Binlog Cumulative Write number of records
Binlog-records-migrated is the cumulative number of records written as part of compaction.
ID A random string that is generated when the BEANSTALKD process starts
Hostname Host Name
List-tubes
Description
List of all existing tube
Format
1
list-tubes\r\n
Response
1
OK <bytes>\r\n
2
<data>\r\n
Bytes is the length of the next data block
Data is Yaml file, containing all tube names
List-tube-used
Description
List of tube The current client is trying to use
Format
1
list-tube-used\r\n
Response
1
USING <tube>\r\n
Tube for Tube name
List-tubes-watched
Description
List of current client watch tube
Format
1
list-tubes-watched\r\n
Response
1
OK <bytes>\r\n
2
<data>\r\n
Bytes is the length of the next data block
Data is Yaml file, containing all tube names
Quit
Description
Close connection
Format
1
quit\r\n
Pause-tube
Description
This directive tube the given number of seconds for all new job delays within a particular
Format
1
Pause-tube <tube-name> <delay>\r\n
Response
Paused\r\n said success
not_found\r\n Tube does not exist

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.