gearmand.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. ==============
  2. Server Options
  3. ==============
  4. --------
  5. SYNOPSIS
  6. --------
  7. .. program:: gearmand
  8. **General options**
  9. .. option:: -b [ --backlog ] arg (=32)
  10. Number of backlog connections for listen.
  11. .. option:: --check-args
  12. Check command line and configuration file argments and then exit.
  13. .. option:: -d [ --daemon ]
  14. Daemon, detach and run in the background.
  15. .. option:: -f [ --file-descriptors ] arg
  16. Number of file descriptors to allow for the process (total connections will be slightly less). Default is max allowed for user.
  17. .. option:: -h [ --help ]
  18. Print this help menu.
  19. .. option:: -j [ --job-retries ] arg (=0)
  20. Number of attempts to run the job before the job server removes it. This is helpful to ensure a bad job does not crash all available workers. Default is no limit.
  21. .. option:: -l [ --log-file ] arg
  22. Log file to write errors and information to. Turning this option on also forces the first verbose level to be enabled.
  23. .. option:: -L [ --listen ] arg
  24. Address the server should listen on. Default is INADDR_ANY.
  25. .. option:: -p [ --port ] arg (=4730)
  26. Port the server should listen on.
  27. .. option:: -P [ --pid-file ] arg
  28. File to write process ID out to.
  29. .. option:: -r [ --protocol ] arg
  30. Load protocol module.
  31. .. option:: -R [ --round-robin ]
  32. Assign work in round-robin order per worker connection. The default is to assign work in the order of functions added by the worker.
  33. .. option:: -q [ --queue-type ] arg
  34. Persistent queue type to use.
  35. .. option:: -t [ --threads ] arg (=4)
  36. Number of I/O threads to use. Default=4.
  37. .. option:: -u [ --user ] arg
  38. Switch to given user after startup.
  39. .. option:: -v [ --verbose ] arg (=v)
  40. Set verbosity level. Defaults to ERROR. Accepts FATAL, ALERT, CRITICAL, ERROR, WARNING, NOTICE, INFO, and DEBUG.
  41. .. option:: -V [ --version ]
  42. Display the version of gearmand and exit.
  43. .. option:: -w [ --worker-wakeup ] arg (=0)
  44. Number of workers to wakeup for each job received. The default is to wakeup all available workers.
  45. .. option:: --keepalive
  46. Enable keepalive on sockets.
  47. .. option:: --keepalive-idle arg (=-1)
  48. If keepalive is enabled, set the value for TCP_KEEPIDLE for systems that support it. A value of -1 means that either the system does not support it or an error occurred when trying to retrieve the default value.
  49. .. option:: --keepalive-interval arg (=-1)
  50. If keepalive is enabled, set the value for TCP_KEEPINTVL for systems that support it. A value of -1 means that either the system does not support it or an error occurred when trying to retrieve the default value.
  51. .. option:: --keepalive-count arg (=-1)
  52. If keepalive is enabled, set the value for TCP_KEEPCNT for systems that support it. A value of -1 means that either the system does not support it or an error occurred when trying to retrieve the default value.
  53. **HTTP:**
  54. .. option:: --http-port arg (=8080)
  55. Port to listen on.
  56. **sqlite**
  57. .. option:: --libsqlite3-db arg
  58. Database file to use.
  59. .. option:: --libsqlite3-table arg (=gearman_queue)
  60. Table to use.
  61. **Memcached(libmemcached)**
  62. .. option:: --libmemcached-servers arg
  63. List of Memcached servers to use.
  64. **Drizzle/MySQL(libdrizzle)**
  65. .. option:: --libdrizzle-host arg
  66. Host of server.
  67. .. option:: --libdrizzle-port arg
  68. Port of server. (by default Drizzle)
  69. .. option:: --libdrizzle-uds arg
  70. Unix domain socket for server.
  71. .. option:: --libdrizzle-user arg
  72. User name for authentication.
  73. .. option:: --libdrizzle-password arg
  74. Password for authentication.
  75. .. option:: --libdrizzle-db arg
  76. Schema/Database to use.
  77. .. option:: --libdrizzle-table arg
  78. Table to use.
  79. .. option:: --libdrizzle-mysql arg
  80. Use MySQL protocol.
  81. **Postgres**
  82. .. option:: --libpq-conninfo arg
  83. PostgreSQL connection information string.
  84. .. option:: --libpq-table arg (=queue)
  85. Table to use.
  86. **tokyocabinet**
  87. .. option:: --libtokyocabinet-file arg
  88. File name of the database. [see: man tcadb, tcadbopen() for name guidelines]
  89. .. option:: --libtokyocabinet-optimize
  90. Optimize database on open. [default=true]
  91. -----------
  92. DESCRIPTION
  93. -----------
  94. Gearman provides a generic application framework to farm out work to other machines or processes that are better suited to do the work. It allows you to do work in parallel, to load balance processing, and to call functions between languages. It can be used in a variety of applications, from high-availability web sites to the transport of database replication events. In other words, it is the nervous system for how distributed processing communicates. A few strong points about Gearman:
  95. * Open Source - It's free! (in both meanings of the word) Gearman has an active open source community that is easy to get involved with if you need help or want to contribute.
  96. * Multi-language - There are interfaces for a number of languages, and this list is growing. You also have the option to write heterogeneous applications with clients submitting work in one language and workers performing that work in another.
  97. * Flexible - You are not tied to any specific design pattern. You can quickly put together distributed applications using any model you choose, one of those options being Map/Reduce.
  98. * Fast - Gearman has a simple protocol and interface with a new optimized server in C to minimize your application overhead.
  99. * Embeddable - Since Gearman is fast and lightweight, it is great for applications of all sizes. It is also easy to introduce into existing applications with minimal overhead.
  100. * No single point of failure - Gearman can not only help scale systems, but can do it in a fault tolerant way.
  101. Thread Model
  102. ++++++++++++
  103. The -t option to gearmand allows you to specify multiple I/O threads, this is enabled by default. There are currently three types of threads in the job server:
  104. Listening and management thread - only one
  105. I/O thread - can have many
  106. Processing thread - only one
  107. When no -t option is given or -t 0 is given, all of three thread types happen within a single thread. When -t 1 is given, there is a thread for listening/management and a thread for I/O and processing. When -t 2 is given, there is a thread for each type of thread above. For all -t option values above 2, more I/O threads are created.
  108. The listening and management thread is mainly responsible for accepting new connections and assigning those connections to an I/O thread (if there are many). It also coordinates startup and shutdown within the server. This thread will have an instance of libevent for managing socket events and signals on an internal pipe. This pipe is used to wakeup the thread or to coordinate shutdown.
  109. The I/O thread is responsible for doing the read and write system calls on the sockets and initial packet parsing. Once the packet has been parsed it it put into an asynchronous queue for the processing thread (each thread has it's own queue so there is very little contention). Each I/O thread has it's own instance of libevent for managing socket events and signals on an internal pipe like the listening thread.
  110. The processing thread should have no system calls within it (except for the occasional brk() for more memory), and manages the various lists and hash tables used for tracking unique keys, job handles, functions, and job queues. All packets that need to be sent back to connections are put into an asynchronous queue for the I/O thread. The I/O thread will pick these up and send them back over the connected socket. All packets flow through the processing thread since it contains the information needed to process the packets. This is due to the complex nature of the various lists and hash tables. If multiple threads were modifying them the locking overhead would most likely cause worse performance than having it in a single thread (and would also complicate the code). In the future more work may be pushed to the I/O threads, and the processing thread can retain minimal functionality to manage those tables and lists. So far this has not been a significant bottleneck, a 16 core Intel machine is able to process upwards of 50k jobs per second.
  111. For thread safety to work when UUID are generated, you must be running the uuidd daemon.
  112. Persistent Queues
  113. +++++++++++++++++
  114. Inside the Gearman job server, all job queues are stored in memory. This means if a server restarts or crashes with pending jobs, they will be lost and are never run by a worker. Persistent queues were added to allow background jobs to be stored in an external durable queue so they may live between server restarts and crashes. The persistent queue is only enabled for background jobs because foreground jobs have an attached client. If a job server goes away, the client can detect this and restart the foreground job somewhere else (or report an error back to the original caller). Background jobs on the other hand have no attached client and are simply expected to be run when submitted.
  115. The persistent queue works by calling a module callback function right before putting a new job in the internal queue for pending jobs to be run. This allows the module to store the job about to be run in some persistent way so that it can later be replayed during a restart. Once it is stored through the module, the job is put onto the active runnable queue, waking up available workers if needed. Once the job has been successfully completed by a worker, another module callback function is called to notify the module the job is done and can be removed. If a job server crashes or is restarted between these two calls for a job, the jobs are reloaded during the next job server start. When the job server starts up, it will call a replay callback function in the module to provide a list of all jobs that were not complete. This is used to populate the internal memory queue of jobs to be run. Once this replay is complete, the job server finishes its initialization and the jobs are now runnable once workers connect (the queue should be in the same state as when it crashed). These jobs are removed from the persistent queue when completed as normal. NOTE: Deleting jobs from the persistent queue storage will not remove them from the in-memory queue while the server is running.
  116. The queues are implemented using a modular interface so it is easy to add new data stores for the persistent queue.
  117. A persistent queue module is enabled by passing the -q or –queue-type option to gearmand. Run gearmand –help to see which queue modules are supported on your system. If you are missing options for one you would like to use, you will need to install any dependencies and then recompile the gearmand package.
  118. Extended Protocols
  119. ------------------
  120. The protocol plugin interface allows you to take over the packet send and receive functions, allowing you to pack the buffers as required by the protocol. The core read and write functions can (and should) be used by the protocol plugin.
  121. HTTP
  122. ++++
  123. This protocol plugin allows you to map HTTP requests to Gearman jobs. It only provides client job submission currently, but it may be extended to support other request types in the future. The plugin can handle both GET and POST data, the latter being used to send a workload to the job server. The URL being requested is translated into the function being called.
  124. For example, the request::
  125. POST /reverse HTTP/1.1
  126. Content-Length: 12
  127. Hello world!
  128. Is translated into a job submission request for the function “reverse” and workload “Hello world!”. This will respond with::
  129. HTTP/1.0 200 OK
  130. X-Gearman-Job-Handle: H:lap:4
  131. Content-Length: 12
  132. Server: Gearman/0.8
  133. !dlrow olleH
  134. The following headers can be passed to change the behavior of the job::
  135. * X-Gearman-Unique: <unique key>
  136. * X-Gearman-Background: true
  137. * X-Gearman-Priority: <high|low>
  138. For example, to run a low priority background job, the following request can be sent::
  139. POST /reverse HTTP/1.1
  140. Content-Length: 12
  141. X-Gearman-Background: true
  142. X-Gearman-Priority: low
  143. Hello world!
  144. The response for this request will not have any data associated with it since it was a background job::
  145. HTTP/1.0 200 OK
  146. X-Gearman-Job-Handle: H:lap:6
  147. Content-Length: 0
  148. Server: Gearman/0.8
  149. The HTTP protocol should be considered experimental.
  150. ----
  151. HOME
  152. ----
  153. To find out more information please check:
  154. `http://gearman.info/ <http://gearman.info/>`_
  155. --------
  156. SEE ALSO
  157. --------
  158. :manpage:`gearman(1)` :manpage:`gearadmin(1)` :manpage:`libgearmand(3)`