Gearman Performance Tuning

Source: Internet
Author: User

Gearman is a general parallel task scheduling framework first developed and used internally by livejournal. It allows different languages to perform interoperability directly in a very simple way. The foreground submits tasks and parameters, and the background worker completes the actual work.

For example, the foreground submits the image to be rendered, and the gearman schedules the working process to provide the rendering service in the background. after the work is completed, the result is returned to the foreground for display. The code for submitting and completing work only needs to interact with each other through pre-negotiated parameter formats. The scheduling, load balancing, and reliability of specific tasks are ensured by the gearman server. For large-scale applications, it is easy to deploy clusters with multiple nodes.

After the official release, danga interactive used C to rewrite the entire server code, supported common script clients such as PHP, Perl, and python, and supported memcached,
SQLite, PostgreSQL, tokyocabinet, and so on are used as task persistence queues, which are cheap and sufficient.

Thread Model

During large-scale use, you need to set parameters for the application type to optimize gearman performance. First, you should understand the gearman thread model.

To ensure support for massive task scheduling, gearman chooses libevent as the Network Operation Support database. Therefore, gearman's server gearmand provides three types of thread roles:

    Port listening and Management threads, accepting new connection requests and handing them over to the IO thread, 1
    Io thread to complete the actual task processing, including command parsing, queue operations, and N
    Processing thread, complete internal data structure management, as simple as possible without system calls, one

Among them, 1st and 3 threads have no direct impact on the global processing performance. Although the processing thread may become a bottleneck, his work is simple enough and can be ignored, therefore, our main goal of performance tuning is to increase the number of I/O threads.
Each Io thread has a libevent instance. All gearman operations are submitted to the processing thread in asynchronous Task Mode, and the actual operations are obtained by the IO thread, therefore, the number of I/O threads is proportional to the number of parallel processing tasks. Gearmand provides the-t parameter to adjust the total number of I/O threads. You need to use libevent 1.4 or later to provide multithreading support.

Process Handle count

The number of process handles affects large-scale deployment. gearman assigns an FD (file descriptor) to each registered worker, and the total number of FD is limited by users, you can use the ulimit-N command to view the current limit.

Flier @ Debian :~ $ Ulimit-n
1024
Flier @ Debian :~ $ Ulimit-HSN 4096 // sets the maximum number of process handles.
4096

In other words, under the default gearman configuration, a maximum of less than 1024 workers can be registered at the same time. After the FD is used up, the worker and client may encounter connection timeout or no response exceptions. Therefore, in the case of a similar situation, we should first check whether the number in the/proc/[pid]/FD/directory has exceeded the limit of ulimit-N and adjust it as needed. You can refer to the/proc/sys/fs/file-Max file and modify it through sysctl-W Fs. File-max = [num.

Flier @ Debian :~ $ CAT/proc/sys/fs/file-max
24372
Flier @ Debian :~ # Sysctl-W fs. File-max = 100000
100000

For more detailed settings, see Linux increase the maximum number of open files or file descriptors.

Gearmand also provides the ability to adjust the number of handles. You can use the-file-descriptors parameter to specify the number of handles during startup, but the number of non-authorized processes cannot exceed soft limit.

-F,-file-descriptors = FDS number of file descriptors to allow for the Process
(Total connections will be slightly less). Default
Is Max allowed for user.

The soft limit is the value that the kernel enforces for the corresponding resource. The hard limit acts as
Ceiling for the soft limit: An unprivileged process may only set its soft limit to a value in the range from 0
Up to the hard limit, and (irreversibly) lower its hard limit. a privileged process (under Linux: one with
Cap_sys_resource capability) may make arbitrary changes to either limit value.

Round Robin Scheduling

In addition, gearmand also provides some parameters to improve the fairness of task scheduling, such as the round-robin mode added in 0.13, allowing the task to be evenly scheduled to multiple workers, instead of scheduling by default based on the order in which the worker registers the function, you can avoid concentrated work on a few devices.

-R,-round-robin assign work in round-robin order per
Workerconnection. The default is to assign work in
The order of functions added by the worker.

Gearmand uses a worker queue to dynamically adjust the scheduling order of the worker in RR mode.

if (server_con->thread->server->flags.round_robin)  {    GEARMAN_LIST_DEL(server_con->worker, server_worker, con_)    _server_con_worker_list_append(server_con->worker_list, server_worker);    ++server_con->worker_count;    if (server_con->worker_list == NULL) {      server_con->worker_list= server_worker;    }  } 

Limited wake-up

With the-worker-wakeup parameter, you can specify the number of workers to be awakened when a task is received to avoid sending a large number of unnecessary Noop packets when the number of workers is very large, try to wake up all workers.

-W,-worker-wakeup = workers number of workers to wakeup for each job already ed.
The default is to wakeup all available workers.

According to the gearman protocol design, if the worker finds that no task needs to be processed in the queue, it can send the pre_sleep command to the server, telling the server that it is going to sleep. In this status, the worker will not take the initiative to capture tasks. The normal task capturing and processing process will resume only after the server sends the Noop command to wake up. Therefore, when receiving a task, gearmand tries to wake up enough worker
In this case, if the total number of workers exceeds the number of possible tasks, a group effect may occur.

/* Queue NOOP for possible sleeping workers. */  if (job->function->worker_list != NULL)  {    worker= job->function->worker_list;    noop_sent= 0;    do    {      if (worker->con->options & GEARMAN_SERVER_CON_SLEEPING &&          !(worker->con->options & GEARMAN_SERVER_CON_NOOP_SENT))      {        ret= gearman_server_io_packet_add(worker->con, false,                                          GEARMAN_MAGIC_RESPONSE,                                          GEARMAN_COMMAND_NOOP, NULL);        if (ret != GEARMAN_SUCCESS)          return ret;          worker->con->options|= GEARMAN_SERVER_CON_NOOP_SENT;        noop_sent++;      }        worker= worker->function_next;    }    while (worker != job->function->worker_list &&           (job->server->worker_wakeup == 0 ||           noop_sent < job->server->worker_wakeup));      job->function->worker_list= worker;  }

In addition, the rational use of persistent queues based on application features can directly affect the performance of large concurrent tasks.

In the final analysis, you need to design some test cases and automated scripts based on your application scenarios, and adjust parameters through the actual running status.

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.