Skip to content

Commit b649522

Browse files
pythonerdoghuamxu
andauthored
Job assign priority (#431) [BREAKING CHANGE]
* make sure higher priority job assigned first across all functions This may break anyone not expecting for priority in one queue to affect priority in another. As such this will trigger a major version bump. --------- Co-authored-by: huamxu <huamin.xu@nokia-sbell.com>
1 parent 7ebc546 commit b649522

File tree

4 files changed

+382
-12
lines changed

4 files changed

+382
-12
lines changed

libgearman-server/gearmand_con.cc

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -359,14 +359,34 @@ gearman_server_job_st * gearman_server_job_peek(gearman_server_con_st *server_co
359359

360360
gearman_server_job_st *gearman_server_job_take(gearman_server_con_st *server_con)
361361
{
362-
for (gearman_server_worker_st *server_worker= server_con->worker_list; server_worker; server_worker= server_worker->con_next)
362+
/* Select jobs by global priority across all workers. This ensures a high
363+
priority job on any worker is preferred to lower priority jobs on earlier
364+
workers in the list. */
365+
for (gearman_job_priority_t priority= GEARMAN_JOB_PRIORITY_HIGH;
366+
priority != GEARMAN_JOB_PRIORITY_MAX;
367+
priority= gearman_job_priority_t(int(priority) +1))
363368
{
364-
if (server_worker->function and server_worker->function->job_count)
369+
for (gearman_server_worker_st *server_worker= server_con->worker_list;
370+
server_worker; server_worker= server_worker->con_next)
365371
{
372+
if (server_worker->function == NULL || server_worker->function->job_count == 0)
373+
{
374+
continue;
375+
}
376+
377+
/* Only consider workers that have jobs for this priority. */
378+
if (server_worker->function->job_list[priority] == NULL)
379+
{
380+
continue;
381+
}
382+
366383
gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM, "Jobs available for %.*s: %lu",
367384
(int)server_worker->function->function_name_size, server_worker->function->function_name,
368385
(unsigned long)(server_worker->function->job_count));
369386

387+
/* Preserve the original round-robin behavior: when we select a worker
388+
to hand out a job, move it to the end of the connection's worker
389+
list so subsequent selection is fair. */
370390
if (Server->flags.round_robin)
371391
{
372392
GEARMAND_LIST_DEL(server_con->worker, server_worker, con_)
@@ -378,16 +398,6 @@ gearman_server_job_st *gearman_server_job_take(gearman_server_con_st *server_con
378398
}
379399
}
380400

381-
gearman_job_priority_t priority;
382-
for (priority= GEARMAN_JOB_PRIORITY_HIGH; priority < GEARMAN_JOB_PRIORITY_LOW;
383-
priority= gearman_job_priority_t(int(priority) +1))
384-
{
385-
if (server_worker->function->job_list[priority])
386-
{
387-
break;
388-
}
389-
}
390-
391401
gearman_server_job_st *server_job= server_worker->function->job_list[priority];
392402
gearman_server_job_st *previous_job= server_job;
393403

tests/libgearman-1.0/client_test.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2457,6 +2457,9 @@ test_st limit_tests[] ={
24572457
{0, 0, 0}
24582458
};
24592459

2460+
// Forward declaration for priority tests
2461+
test_st *test_gearman_worker_priority(void);
2462+
24602463
collection_st collection[] ={
24612464
{"gearman_return_t", 0, 0, gearman_return_t_TESTS},
24622465
{"init", 0, 0, gearman_client_st_init_TESTS},
@@ -2509,6 +2512,7 @@ collection_st collection[] ={
25092512
{"regression2", reset_SETUP, 0, regression2_TESTS },
25102513
{"gearman_worker_timeout()", default_v2_SETUP, 0, gearman_worker_timeout_TESTS },
25112514
{"gearman_client_set_identifier()", default_v2_SETUP, 0, gearman_client_set_identifier_TESTS },
2515+
{"gearman_worker_priority", 0, 0, test_gearman_worker_priority() },
25122516
{0, 0, 0, 0}
25132517
};
25142518

tests/libgearman-1.0/include.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ t_client_SOURCES+= tests/libgearman-1.0/protocol.cc
2828
t_client_SOURCES+= tests/libgearman-1.0/server_options.cc
2929
t_client_SOURCES+= tests/libgearman-1.0/task.cc
3030
t_client_SOURCES+= tests/libgearman-1.0/unique.cc
31+
t_client_SOURCES+= tests/libgearman-1.0/priority_test.cc
3132
t_client_SOURCES+= tests/workers/aggregator/cat.cc
3233
t_client_SOURCES+= tests/workers/v1/echo_or_react.cc
3334
t_client_SOURCES+= tests/workers/v1/echo_or_react_chunk.cc

0 commit comments

Comments
 (0)