PROTOCOL 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. # Gearman server and library
  2. # Copyright (C) 2008 Brian Aker, Eric Day
  3. # All rights reserved.
  4. #
  5. # Use and distribution licensed under the BSD license. See
  6. # the COPYING file in this directory for full text.
  7. Gearman Protocol
  8. ----------------
  9. The Gearman protocol operates over TCP, port 4730 by default. It
  10. previously operated on port 7003, but this conflicted with the AFS
  11. port range and the new port (4730) was assigned by IANA. Communication
  12. happens between either a client and job server, or between a worker
  13. and job server. In either case, the protocol consists of packets
  14. containing requests and responses. All packets sent to a job server
  15. are considered requests, and all packets sent from a job server are
  16. considered responses. A simple configuration may look like:
  17. ---------- ---------- ---------- ----------
  18. | Client | | Client | | Client | | Client |
  19. ---------- ---------- ---------- ----------
  20. \ / \ /
  21. \ / \ /
  22. -------------- --------------
  23. | Job Server | | Job Server |
  24. -------------- --------------
  25. | |
  26. ----------------------------------------------
  27. | | | |
  28. ---------- ---------- ---------- ----------
  29. | Worker | | Worker | | Worker | | Worker |
  30. ---------- ---------- ---------- ----------
  31. Initially, the workers register functions they can perform with each
  32. job server. Clients will then connect to a job server and issue a
  33. request to a job to be run. The job server then notifies each worker
  34. that can perform that job (based on the function it registered) that
  35. a new job is ready. The first worker to wake up and retrieve the job
  36. will then execute it.
  37. All communication between workers or clients and the job server
  38. are binary. There is also a line-based text protocol used by
  39. administrative clients. This part of the protocol is text based so a
  40. custom administrative utility is not required (instead, 'telnet' or
  41. 'nc' can be used). This is documented under "Administrative Protocol".
  42. Binary Packet
  43. -------------
  44. Requests and responses are encapsulated by a binary packet. A binary
  45. packet consists of a header which is optionally followed by data. The
  46. header is:
  47. 4 byte magic code - This is either "\0REQ" for requests or "\0RES"
  48. for responses.
  49. 4 byte type - A big-endian (network-order) integer containing
  50. an enumerated packet type. Possible values are:
  51. # Name Magic Type
  52. 1 CAN_DO REQ Worker
  53. 2 CANT_DO REQ Worker
  54. 3 RESET_ABILITIES REQ Worker
  55. 4 PRE_SLEEP REQ Worker
  56. 5 (unused) - -
  57. 6 NOOP RES Worker
  58. 7 SUBMIT_JOB REQ Client
  59. 8 JOB_CREATED RES Client
  60. 9 GRAB_JOB REQ Worker
  61. 10 NO_JOB RES Worker
  62. 11 JOB_ASSIGN RES Worker
  63. 12 WORK_STATUS REQ Worker
  64. RES Client
  65. 13 WORK_COMPLETE REQ Worker
  66. RES Client
  67. 14 WORK_FAIL REQ Worker
  68. RES Client
  69. 15 GET_STATUS REQ Client
  70. 16 ECHO_REQ REQ Client/Worker
  71. 17 ECHO_RES RES Client/Worker
  72. 18 SUBMIT_JOB_BG REQ Client
  73. 19 ERROR RES Client/Worker
  74. 20 STATUS_RES RES Client
  75. 21 SUBMIT_JOB_HIGH REQ Client
  76. 22 SET_CLIENT_ID REQ Worker
  77. 23 CAN_DO_TIMEOUT REQ Worker
  78. 24 ALL_YOURS REQ Worker
  79. 25 WORK_EXCEPTION REQ Worker
  80. RES Client
  81. 26 OPTION_REQ REQ Client/Worker
  82. 27 OPTION_RES RES Client/Worker
  83. 28 WORK_DATA REQ Worker
  84. RES Client
  85. 29 WORK_WARNING REQ Worker
  86. RES Client
  87. 30 GRAB_JOB_UNIQ REQ Worker
  88. 31 JOB_ASSIGN_UNIQ RES Worker
  89. 32 SUBMIT_JOB_HIGH_BG REQ Client
  90. 33 SUBMIT_JOB_LOW REQ Client
  91. 34 SUBMIT_JOB_LOW_BG REQ Client
  92. 35 SUBMIT_JOB_SCHED REQ Client
  93. 36 SUBMIT_JOB_EPOCH REQ Client
  94. 37 SUBMIT_REDUCE_JOB REQ Client
  95. 38 SUBMIT_REDUCE_JOB_BACKGROUND REQ Client
  96. 39 GRAB_JOB_ALL REQ Worker
  97. 40 JOB_ASSIGN_ALL RES Worker
  98. 41 GET_STATUS_UNIQUE REQ Client
  99. 42 STATUS_RES_UNIQUE RES Client
  100. 4 byte size - A big-endian (network-order) integer containing
  101. the size of the data being sent after the header.
  102. Arguments given in the data part are separated by a NULL byte, and
  103. the last argument is determined by the size of data after the last
  104. NULL byte separator. All job handle arguments must not be longer than
  105. 64 bytes, including NULL terminator.
  106. Client/Worker Requests
  107. ----------------------
  108. These request types may be sent by either a client or a worker:
  109. ECHO_REQ
  110. When a job server receives this request, it simply generates a
  111. ECHO_RES packet with the data. This is primarily used for testing
  112. or debugging.
  113. Arguments:
  114. - Opaque data that is echoed back in response.
  115. Client/Worker Responses
  116. -----------------------
  117. These response types may be sent to either a client or a worker:
  118. ECHO_RES
  119. This is sent in response to a ECHO_REQ request. The server doesn't
  120. look at or modify the data argument, it just sends it back.
  121. Arguments:
  122. - Opaque data that is echoed back in response.
  123. ERROR
  124. This is sent whenever the server encounters an error and needs
  125. to notify a client or worker.
  126. Arguments:
  127. - NULL byte terminated error code string.
  128. - Error text.
  129. Client Requests
  130. ---------------
  131. These request types may only be sent by a client:
  132. SUBMIT_JOB, SUBMIT_JOB_BG,
  133. SUBMIT_JOB_HIGH, SUBMIT_JOB_HIGH_BG,
  134. SUBMIT_JOB_LOW, SUBMIT_JOB_LOW_BG
  135. A client issues one of these when a job needs to be run. The
  136. server will then assign a job handle and respond with a JOB_CREATED
  137. packet.
  138. If one of the BG versions is used, the client is not updated with
  139. status or notified when the job has completed (it is detached).
  140. The Gearman job server queue is implemented with three levels:
  141. normal, high, and low. Jobs submitted with one of the HIGH versions
  142. always take precedence, and jobs submitted with the normal versions
  143. take precedence over the LOW versions.
  144. The unique ID can be used by the server to reduce queue length. If a
  145. job with the same Unique ID has already been submitted, the server
  146. may attach this request to the already existing job. This includes
  147. jobs already in progress, in which case non-background jobs will be
  148. sent the same result as background jobs. This is known commonly as
  149. "coalescing".
  150. Arguments:
  151. - NULL byte terminated function name.
  152. - NULL byte terminated unique ID.
  153. - Opaque data that is given to the function as an argument.
  154. SUBMIT_REDUCE_JOB, SUBMIT_REDUCE_JOB_BACKGROUND
  155. Works like the other SUBMIT_JOB commands, but adds a reducer argument.
  156. Arguments:
  157. - NULL byte terminated function name.
  158. - NULL byte terminated unique ID.
  159. - NULL byte terminated reducer.
  160. - Opaque data that is given to the function as an argument.
  161. SUBMIT_JOB_SCHED
  162. Just like SUBMIT_JOB_BG, but run job at given time instead of
  163. immediately. This is not currently used and may be removed.
  164. Arguments:
  165. - NULL byte terminated function name.
  166. - NULL byte terminated unique ID.
  167. - NULL byte terminated minute (0-59).
  168. - NULL byte terminated hour (0-23).
  169. - NULL byte terminated day of month (1-31).
  170. - NULL byte terminated month (1-12).
  171. - NULL byte terminated day of week (0-6, 0 = Monday).
  172. - Opaque data that is given to the function as an argument.
  173. SUBMIT_JOB_EPOCH
  174. Just like SUBMIT_JOB_BG, but run job at given time instead of
  175. immediately. This is not currently used and may be removed.
  176. Arguments:
  177. - NULL byte terminated function name.
  178. - NULL byte terminated unique ID.
  179. - NULL byte terminated epoch time.
  180. - Opaque data that is given to the function as an argument.
  181. GET_STATUS
  182. A client issues this to get status information for a submitted job.
  183. Arguments:
  184. - Job handle that was given in JOB_CREATED packet.
  185. GET_STATUS_UNIQUE
  186. A client issues this to get status information for a submitted job.
  187. Arguments:
  188. - Unique value that was given when job was submitted.
  189. OPTION_REQ
  190. A client issues this to set an option for the connection in the
  191. job server. Returns a OPTION_RES packet on success, or an ERROR
  192. packet on failure.
  193. Arguments:
  194. - Name of the option to set. Possibilities are:
  195. * "exceptions" - Forward WORK_EXCEPTION packets to the client.
  196. Client Responses
  197. ----------------
  198. These response types may only be sent to a client:
  199. JOB_CREATED
  200. This is sent in response to one of the SUBMIT_JOB* packets. It
  201. signifies to the client that a the server successfully received
  202. the job and queued it to be run by a worker.
  203. Arguments:
  204. - Job handle assigned by server.
  205. WORK_DATA, WORK_WARNING, WORK_STATUS, WORK_COMPLETE,
  206. WORK_FAIL, WORK_EXCEPTION
  207. For non-background jobs, the server forwards these packets from
  208. the worker to clients. See "Worker Requests" for more information
  209. and arguments.
  210. STATUS_RES
  211. This is sent in response to a GET_STATUS request. This is used by
  212. clients that have submitted a job with SUBMIT_JOB_BG to see if the
  213. job has been completed, and if not, to get the percentage complete.
  214. Arguments:
  215. - NULL byte terminated job handle.
  216. - NULL byte terminated known status, this is 0 (false) or 1 (true).
  217. - NULL byte terminated running status, this is 0 (false) or 1
  218. (true).
  219. - NULL byte terminated percent complete numerator.
  220. - Percent complete denominator.
  221. STATUS_RES_UNIQUE
  222. This is sent in response to a GET_STATUS_UNIQUE request. This is
  223. used by clients that have submitted a job with SUBMIT_JOB_BG to see
  224. if the job has been completed, and if not, to get the percentage
  225. complete.
  226. Arguments:
  227. - NULL byte terminated job handle.
  228. - NULL byte terminated known status, this is 0 (false) or 1 (true).
  229. - NULL byte terminated running status, this is 0 (false) or 1
  230. (true).
  231. - NULL byte terminated percent complete numerator.
  232. - NULL byte terminated percent complete denominator.
  233. - Count of clients waiting.
  234. OPTION_RES
  235. Successful response to the OPTION_REQ request.
  236. Arguments:
  237. - Name of the option that was set, see OPTION_REQ for possibilities.
  238. Worker Requests
  239. ---------------
  240. These request types may only be sent by a worker:
  241. CAN_DO
  242. This is sent to notify the server that the worker is able to
  243. perform the given function. The worker is then put on a list to be
  244. woken up whenever the job server receives a job for that function.
  245. Arguments:
  246. - Function name.
  247. CAN_DO_TIMEOUT
  248. Same as CAN_DO, but with a timeout value on how long the job
  249. is allowed to run. After the timeout value, the job server will
  250. mark the job as failed and notify any listening clients.
  251. Arguments:
  252. - NULL byte terminated Function name.
  253. - Timeout value (in milliseconds).
  254. CANT_DO
  255. This is sent to notify the server that the worker is no longer
  256. able to perform the given function.
  257. Arguments:
  258. - Function name.
  259. RESET_ABILITIES
  260. This is sent to notify the server that the worker is no longer
  261. able to do any functions it previously registered with CAN_DO or
  262. CAN_DO_TIMEOUT.
  263. Arguments:
  264. - None.
  265. PRE_SLEEP
  266. This is sent to notify the server that the worker is about to
  267. sleep, and that it should be woken up with a NOOP packet if a
  268. job comes in for a function the worker is able to perform.
  269. Arguments:
  270. - None.
  271. GRAB_JOB
  272. This is sent to the server to request any available jobs on the
  273. queue. The server will respond with either NO_JOB or JOB_ASSIGN,
  274. depending on whether a job is available.
  275. Arguments:
  276. - None.
  277. GRAB_JOB_UNIQ
  278. Just like GRAB_JOB, but return JOB_ASSIGN_UNIQ when there is a job.
  279. Arguments:
  280. - None.
  281. GRAB_JOB_ALL
  282. Just like GRAB_JOB_UNIQ, but return JOB_ASSIGN_ALL when there is a job.
  283. Arguments:
  284. - None.
  285. WORK_DATA
  286. This is sent to update the client with data from a running job. A
  287. worker should use this when it needs to send updates, send partial
  288. results, or flush data during long running jobs. It can also be
  289. used to break up a result so the worker does not need to buffer
  290. the entire result before sending in a WORK_COMPLETE packet.
  291. Arguments:
  292. - NULL byte terminated job handle.
  293. - Opaque data that is returned to the client.
  294. WORK_WARNING
  295. This is sent to update the client with a warning. It acts just
  296. like a WORK_DATA response, but should be treated as a warning
  297. instead of normal response data.
  298. Arguments:
  299. - NULL byte terminated job handle.
  300. - Opaque data that is returned to the client.
  301. WORK_STATUS
  302. This is sent to update the server (and any listening clients)
  303. of the status of a running job. The worker should send these
  304. periodically for long running jobs to update the percentage
  305. complete. The job server should store this information so a client
  306. who issued a background command may retrieve it later with a
  307. GET_STATUS request.
  308. Arguments:
  309. - NULL byte terminated job handle.
  310. - NULL byte terminated percent complete numerator.
  311. - Percent complete denominator.
  312. WORK_COMPLETE
  313. This is to notify the server (and any listening clients) that
  314. the job completed successfully.
  315. Arguments:
  316. - NULL byte terminated job handle.
  317. - Opaque data that is returned to the client as a response.
  318. WORK_FAIL
  319. This is to notify the server (and any listening clients) that
  320. the job failed.
  321. Arguments:
  322. - Job handle.
  323. WORK_EXCEPTION
  324. This is to notify the server (and any listening clients) that
  325. the job failed with the given exception.
  326. Arguments:
  327. - NULL byte terminated job handle.
  328. - Opaque data that is returned to the client as an exception.
  329. SET_CLIENT_ID
  330. This sets the worker ID in a job server so monitoring and reporting
  331. commands can uniquely identify the various workers, and different
  332. connections to job servers from the same worker.
  333. Arguments:
  334. - Unique string to identify the worker instance.
  335. ALL_YOURS
  336. Not yet implemented. This looks like it is used to notify a job
  337. server that this is the only job server it is connected to, so
  338. a job can be given directly to this worker with a JOB_ASSIGN and
  339. no worker wake-up is required.
  340. Arguments:
  341. - None.
  342. Worker Responses
  343. ----------------
  344. These response types may only be sent to a worker:
  345. NOOP
  346. This is used to wake up a sleeping worker so that it may grab a
  347. pending job.
  348. Arguments:
  349. - None.
  350. NO_JOB
  351. This is given in response to a GRAB_JOB request to notify the
  352. worker there are no pending jobs that need to run.
  353. Arguments:
  354. - None.
  355. JOB_ASSIGN
  356. This is given in response to a GRAB_JOB request to give the worker
  357. information needed to run the job. All communication about the
  358. job (such as status updates and completion response) should use
  359. the handle, and the worker should run the given function with
  360. the argument.
  361. Arguments:
  362. - NULL byte terminated job handle.
  363. - NULL byte terminated function name.
  364. - Opaque data that is given to the function as an argument.
  365. JOB_ASSIGN_UNIQ
  366. This is given in response to a GRAB_JOB_UNIQ request and acts
  367. just like JOB_ASSIGN but with the client assigned unique ID.
  368. Arguments:
  369. - NULL byte terminated job handle.
  370. - NULL byte terminated function name.
  371. - NULL byte terminated unique ID.
  372. - Opaque data that is given to the function as an argument.
  373. JOB_ASSIGN_ALL
  374. This is given in response to a GRAB_JOB_ALL request and acts
  375. just like JOB_ASSIGN_UNIQ but with the reducer returned.
  376. Arguments:
  377. - NULL byte terminated job handle.
  378. - NULL byte terminated function name.
  379. - NULL byte terminated unique ID.
  380. - NULL byte terminated reducer.
  381. - Opaque data that is given to the function as an argument.
  382. Administrative Protocol
  383. -----------------------
  384. The Gearman job server also supports a text-based protocol to pull
  385. information and run some administrative tasks. This runs on the same
  386. port as the binary protocol, and the server differentiates between
  387. the two by looking at the first character. If it is a NULL (\0),
  388. then it is binary, if it is non-NULL, that it attempts to parse it
  389. as a text command. The following commands are supported:
  390. workers
  391. This sends back a list of all workers, their file descriptors,
  392. their IPs, their IDs, and a list of registered functions they can
  393. perform. The list is terminated with a line containing a single
  394. '.' (period). The format is:
  395. FD IP-ADDRESS CLIENT-ID : FUNCTION ...
  396. Arguments:
  397. - None.
  398. status
  399. This sends back a list of all registered functions. Next to
  400. each function is the number of jobs in the queue, the number of
  401. running jobs, and the number of capable workers. The columns are
  402. tab separated, and the list is terminated with a line containing
  403. a single '.' (period). The format is:
  404. FUNCTION\tTOTAL\tRUNNING\tAVAILABLE_WORKERS
  405. Arguments:
  406. - None.
  407. prioritystatus
  408. This sends back a list of all registered functions. Next to each
  409. function is the number of queued jobs that are not running, broken down
  410. by priority, and the number of capable workers. The columns are tab
  411. separated, and the list is terminated with a line containing
  412. a single '.' (period). The format is:
  413. FUNCTION\tHIGH-QUEUED\tNORMAL-QUEUED\tLOW-QUEUED\tAVAILABLE_WORKERS
  414. Columns:
  415. - Function name.
  416. - Number of queued high priority jobs.
  417. - Number of queued normal priority jobs.
  418. - Number of queued low priority jobs.
  419. - Available workers registered for this function.
  420. Arguments:
  421. - None.
  422. maxqueue
  423. This sets the maximum queue size for a function. If no size is
  424. given, the default is used. If one size is given, it is applied to
  425. jobs regardless of priority. If three sizes are given, the sizes
  426. are used when testing high-priority, normal, and low-priority jobs,
  427. respectively. A zero or negative size indicates no limit. This
  428. command sends back a single line with "OK".
  429. Arguments:
  430. - Function name.
  431. - Optional maximum queue size (to apply one maximum at all priorities), or
  432. three optional maximum queue sizes (to enforce for high-, normal-, and
  433. low-priority job submissions).
  434. version
  435. Send back the version of the server.
  436. Arguments:
  437. - None.
  438. The Perl version also has a 'gladiator' command that uses the
  439. 'Devel::Gladiator' Perl module and is used for debugging.
  440. Binary Protocol Example
  441. -----------------------
  442. This example will step through a simple interaction where a worker
  443. connects and registers for a function named "reverse", the client
  444. connects and submits a job for this function, and the worker performs
  445. this job and responds with a result. This shows every byte that needs
  446. to be sent over the wire in order for the job to be run to completion.
  447. Worker registration:
  448. Worker -> Job Server
  449. 00 52 45 51 \0REQ (Magic)
  450. 00 00 00 01 1 (Packet type: CAN_DO)
  451. 00 00 00 07 7 (Packet length)
  452. 72 65 76 65 72 73 65 reverse (Function)
  453. Worker check for job:
  454. Worker -> Job Server
  455. 00 52 45 51 \0REQ (Magic)
  456. 00 00 00 09 9 (Packet type: GRAB_JOB)
  457. 00 00 00 00 0 (Packet length)
  458. Job Server -> Worker
  459. 00 52 45 53 \0RES (Magic)
  460. 00 00 00 0a 10 (Packet type: NO_JOB)
  461. 00 00 00 00 0 (Packet length)
  462. Worker -> Job Server
  463. 00 52 45 51 \0REQ (Magic)
  464. 00 00 00 04 4 (Packet type: PRE_SLEEP)
  465. 00 00 00 00 0 (Packet length)
  466. Client job submission:
  467. Client -> Job Server
  468. 00 52 45 51 \0REQ (Magic)
  469. 00 00 00 07 7 (Packet type: SUBMIT_JOB)
  470. 00 00 00 0d 13 (Packet length)
  471. 72 65 76 65 72 73 65 00 reverse\0 (Function)
  472. 00 \0 (Unique ID)
  473. 74 65 73 74 test (Workload)
  474. Job Server -> Client
  475. 00 52 45 53 \0RES (Magic)
  476. 00 00 00 08 8 (Packet type: JOB_CREATED)
  477. 00 00 00 07 7 (Packet length)
  478. 48 3a 6c 61 70 3a 31 H:lap:1 (Job handle)
  479. Worker wakeup:
  480. Job Server -> Worker
  481. 00 52 45 53 \0RES (Magic)
  482. 00 00 00 06 6 (Packet type: NOOP)
  483. 00 00 00 00 0 (Packet length)
  484. Worker check for job:
  485. Worker -> Job Server
  486. 00 52 45 51 \0REQ (Magic)
  487. 00 00 00 09 9 (Packet type: GRAB_JOB)
  488. 00 00 00 00 0 (Packet length)
  489. Job Server -> Worker
  490. 00 52 45 53 \0RES (Magic)
  491. 00 00 00 0b 11 (Packet type: JOB_ASSIGN)
  492. 00 00 00 14 20 (Packet length)
  493. 48 3a 6c 61 70 3a 31 00 H:lap:1\0 (Job handle)
  494. 72 65 76 65 72 73 65 00 reverse\0 (Function)
  495. 74 65 73 74 test (Workload)
  496. Worker response for job:
  497. Worker -> Job Server
  498. 00 52 45 51 \0REQ (Magic)
  499. 00 00 00 0d 13 (Packet type: WORK_COMPLETE)
  500. 00 00 00 0c 12 (Packet length)
  501. 48 3a 6c 61 70 3a 31 00 H:lap:1\0 (Job handle)
  502. 74 73 65 74 tset (Response)
  503. Job server response to client:
  504. Job Server -> Client
  505. 00 52 45 53 \0RES (Magic)
  506. 00 00 00 0d 13 (Packet type: WORK_COMPLETE)
  507. 00 00 00 0c 12 (Packet length)
  508. 48 3a 6c 61 70 3a 31 00 H:lap:1\0 (Job handle)
  509. 74 73 65 74 tset (Response)
  510. At this point, the worker would then ask for more jobs to run (the
  511. "Check for job" state above), and the client could submit more
  512. jobs. Note that the client is full duplex and could have multiple
  513. jobs being run over a single socket at the same time. The result
  514. packets may not be sent in the same order the jobs were submitted
  515. and instead interleaved with other job result packets.