2 * Bacula job queue routines.
4 * This code consists of three queues, the waiting_jobs
5 * queue, where jobs are initially queued, the ready_jobs
6 * queue, where jobs are placed when all the resources are
7 * allocated and they can immediately be run, and the
8 * running queue where jobs are placed when they are
11 * Kern Sibbald, July MMIII
15 * This code was adapted from the Bacula workq, which was
16 * adapted from "Programming with POSIX Threads", by
21 Copyright (C) 2000-2003 Kern Sibbald and John Walker
23 This program is free software; you can redistribute it and/or
24 modify it under the terms of the GNU General Public License as
25 published by the Free Software Foundation; either version 2 of
26 the License, or (at your option) any later version.
28 This program is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
31 General Public License for more details.
33 You should have received a copy of the GNU General Public
34 License along with this program; if not, write to the Free
35 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
45 /* Forward referenced functions */
46 static void *jobq_server(void *arg);
47 static int start_server(jobq_t *jq);
50 * Initialize a job queue
52 * Returns: 0 on success
55 int jobq_init(jobq_t *jq, int threads, void *(*engine)(void *arg))
58 jobq_item_t *item = NULL;
60 if ((stat = pthread_attr_init(&jq->attr)) != 0) {
63 if ((stat = pthread_attr_setdetachstate(&jq->attr, PTHREAD_CREATE_DETACHED)) != 0) {
64 pthread_attr_destroy(&jq->attr);
67 if ((stat = pthread_mutex_init(&jq->mutex, NULL)) != 0) {
68 pthread_attr_destroy(&jq->attr);
71 if ((stat = pthread_cond_init(&jq->work, NULL)) != 0) {
72 pthread_mutex_destroy(&jq->mutex);
73 pthread_attr_destroy(&jq->attr);
77 jq->max_workers = threads; /* max threads to create */
78 jq->num_workers = 0; /* no threads yet */
79 jq->idle_workers = 0; /* no idle threads */
80 jq->engine = engine; /* routine to run */
81 jq->valid = JOBQ_VALID;
82 /* Initialize the job queues */
83 jq->waiting_jobs = new dlist(item, &item->link);
84 jq->running_jobs = new dlist(item, &item->link);
85 jq->ready_jobs = new dlist(item, &item->link);
90 * Destroy the job queue
92 * Returns: 0 on success
95 int jobq_destroy(jobq_t *jq)
97 int stat, stat1, stat2;
99 if (jq->valid != JOBQ_VALID) {
102 if ((stat = pthread_mutex_lock(&jq->mutex)) != 0) {
105 jq->valid = 0; /* prevent any more operations */
108 * If any threads are active, wake them
110 if (jq->num_workers > 0) {
112 if (jq->idle_workers) {
113 if ((stat = pthread_cond_broadcast(&jq->work)) != 0) {
114 pthread_mutex_unlock(&jq->mutex);
118 while (jq->num_workers > 0) {
119 if ((stat = pthread_cond_wait(&jq->work, &jq->mutex)) != 0) {
120 pthread_mutex_unlock(&jq->mutex);
125 if ((stat = pthread_mutex_unlock(&jq->mutex)) != 0) {
128 stat = pthread_mutex_destroy(&jq->mutex);
129 stat1 = pthread_cond_destroy(&jq->work);
130 stat2 = pthread_attr_destroy(&jq->attr);
131 delete jq->waiting_jobs;
132 delete jq->running_jobs;
133 delete jq->ready_jobs;
134 return (stat != 0 ? stat : (stat1 != 0 ? stat1 : stat2));
143 * Wait until schedule time arrives before starting
145 static void *sched_wait(void *arg)
147 JCR *jcr = ((wait_pkt *)arg)->jcr;
148 jobq_t *jq = ((wait_pkt *)arg)->jq;
150 Dmsg0(100, "Enter sched_wait.\n");
152 time_t wtime = jcr->sched_time - time(NULL);
153 /* Wait until scheduled time arrives */
154 if (wtime > 0 && verbose) {
155 Jmsg(jcr, M_INFO, 0, _("Job %s waiting %d seconds for scheduled start time.\n"),
157 set_jcr_job_status(jcr, JS_WaitStartTime);
159 /* Check every 30 seconds if canceled */
161 Dmsg2(100, "Waiting on sched time, jobid=%d secs=%d\n", jcr->JobId, wtime);
165 bmicrosleep(wtime, 0);
166 if (job_canceled(jcr)) {
169 wtime = jcr->sched_time - time(NULL);
172 Dmsg0(100, "Exit sched_wait\n");
178 * Add a job to the queue
179 * jq is a queue that was created with jobq_init
182 int jobq_add(jobq_t *jq, JCR *jcr)
185 jobq_item_t *item, *li;
186 bool inserted = false;
187 time_t wtime = jcr->sched_time - time(NULL);
192 Dmsg1(100, "jobq_add jobid=%d\n", jcr->JobId);
193 if (jq->valid != JOBQ_VALID) {
197 if (!job_canceled(jcr) && wtime > 0) {
198 set_thread_concurrency(jq->max_workers + 2);
199 sched_pkt = (wait_pkt *)malloc(sizeof(wait_pkt));
200 sched_pkt->jcr = jcr;
202 stat = pthread_create(&id, &jq->attr, sched_wait, (void *)sched_pkt);
206 if ((stat = pthread_mutex_lock(&jq->mutex)) != 0) {
210 if ((item = (jobq_item_t *)malloc(sizeof(jobq_item_t))) == NULL) {
215 if (job_canceled(jcr)) {
216 /* Add job to ready queue so that it is canceled quickly */
217 jq->ready_jobs->prepend(item);
218 Dmsg1(100, "Prepended job=%d to ready queue\n", jcr->JobId);
220 /* Add this job to the wait queue in priority sorted order */
221 for (li=NULL; (li=(jobq_item_t *)jq->waiting_jobs->next(li)); ) {
222 Dmsg2(100, "waiting item jobid=%d priority=%d\n",
223 li->jcr->JobId, li->jcr->JobPriority);
224 if (li->jcr->JobPriority > jcr->JobPriority) {
225 jq->waiting_jobs->insert_before(item, li);
226 Dmsg2(100, "insert_before jobid=%d before %d\n",
227 li->jcr->JobId, jcr->JobId);
232 /* If not jobs in wait queue, append it */
234 jq->waiting_jobs->append(item);
235 Dmsg1(100, "Appended item jobid=%d\n", jcr->JobId);
239 stat = start_server(jq);
242 pthread_mutex_unlock(&jq->mutex);
244 Dmsg0(100, "Return jobq_add\n");
249 * Remove a job from the job queue
250 * jq is a queue that was created with jobq_init
251 * work_item is an element of work
253 * Note, it is "removed" by immediately calling a processing routine.
254 * if you want to cancel it, you need to provide some external means
257 int jobq_remove(jobq_t *jq, JCR *jcr)
263 Dmsg1(100, "jobq_remove jobid=%d\n", jcr->JobId);
264 if (jq->valid != JOBQ_VALID) {
268 if ((stat = pthread_mutex_lock(&jq->mutex)) != 0) {
272 for (item=NULL; (item=(jobq_item_t *)jq->waiting_jobs->next(item)); ) {
273 if (jcr == item->jcr) {
282 /* Move item to be the first on the list */
283 jq->waiting_jobs->remove(item);
284 jq->ready_jobs->prepend(item);
286 stat = start_server(jq);
290 pthread_mutex_unlock(&jq->mutex);
291 Dmsg0(100, "Return jobq_remove\n");
297 * Start the server thread
299 static int start_server(jobq_t *jq)
304 /* if any threads are idle, wake one */
305 if (jq->idle_workers > 0) {
306 Dmsg0(100, "Signal worker to wake up\n");
307 if ((stat = pthread_cond_signal(&jq->work)) != 0) {
308 pthread_mutex_unlock(&jq->mutex);
311 } else if (jq->num_workers < jq->max_workers) {
312 Dmsg0(100, "Create worker thread\n");
313 /* No idle threads so create a new one */
314 set_thread_concurrency(jq->max_workers + 1);
315 if ((stat = pthread_create(&id, &jq->attr, jobq_server, (void *)jq)) != 0) {
316 pthread_mutex_unlock(&jq->mutex);
326 * This is the worker thread that serves the job queue.
327 * When all the resources are acquired for the job,
328 * it will call the user's engine.
330 static void *jobq_server(void *arg)
332 struct timespec timeout;
333 jobq_t *jq = (jobq_t *)arg;
334 jobq_item_t *je; /* job entry in queue */
339 Dmsg0(100, "Start jobq_server\n");
340 if ((stat = pthread_mutex_lock(&jq->mutex)) != 0) {
348 Dmsg0(100, "Top of for loop\n");
350 Dmsg0(100, "gettimeofday()\n");
351 gettimeofday(&tv, &tz);
353 timeout.tv_sec = tv.tv_sec + 4;
355 while (!work && !jq->quit) {
357 * Wait 4 seconds, then if no more work, exit
359 Dmsg0(200, "pthread_cond_timedwait()\n");
360 stat = pthread_cond_timedwait(&jq->work, &jq->mutex, &timeout);
361 Dmsg1(100, "timedwait=%d\n", stat);
362 if (stat == ETIMEDOUT) {
365 } else if (stat != 0) {
366 /* This shouldn't happen */
367 Dmsg0(100, "This shouldn't happen\n");
369 pthread_mutex_unlock(&jq->mutex);
374 * If anything is in the ready queue, run it
376 Dmsg0(100, "Checking ready queue.\n");
377 while (!jq->ready_jobs->empty() && !jq->quit) {
379 je = (jobq_item_t *)jq->ready_jobs->first();
381 jq->ready_jobs->remove(je);
382 if (!jq->ready_jobs->empty()) {
383 Dmsg0(100, "ready queue not empty start server\n");
384 if (start_server(jq) != 0) {
388 jq->running_jobs->append(je);
389 Dmsg1(100, "Took jobid=%d from ready and appended to run\n", jcr->JobId);
390 if ((stat = pthread_mutex_unlock(&jq->mutex)) != 0) {
393 /* Call user's routine here */
394 Dmsg1(100, "Calling user engine for jobid=%d\n", jcr->JobId);
396 Dmsg1(100, "Back from user engine jobid=%d.\n", jcr->JobId);
397 if ((stat = pthread_mutex_lock(&jq->mutex)) != 0) {
398 free(je); /* release job entry */
401 Dmsg0(200, "Done lock mutex\n");
402 jq->running_jobs->remove(je);
404 * Release locks if acquired. Note, they will not have
405 * been acquired for jobs canceled before they were
406 * put into the ready queue.
408 if (jcr->acquired_resource_locks) {
409 jcr->store->NumConcurrentJobs--;
410 jcr->client->NumConcurrentJobs--;
411 jcr->job->NumConcurrentJobs--;
414 if (jcr->job->RescheduleOnError &&
415 jcr->JobStatus != JS_Terminated &&
416 jcr->JobStatus != JS_Canceled &&
417 jcr->job->RescheduleTimes > 0 &&
418 jcr->reschedule_count < jcr->job->RescheduleTimes) {
421 * Reschedule this job by cleaning it up, but
422 * reuse the same JobId if possible.
424 jcr->reschedule_count++;
425 jcr->sched_time = time(NULL) + jcr->job->RescheduleInterval;
426 Dmsg2(100, "Rescheduled Job %s to re-run in %d seconds.\n", jcr->Job,
427 (int)jcr->job->RescheduleInterval);
428 jcr->JobStatus = JS_Created; /* force new status */
429 dird_free_jcr(jcr); /* partial cleanup old stuff */
430 if (jcr->JobBytes == 0) {
431 Dmsg1(100, "Requeue job=%d\n", jcr->JobId);
433 jobq_add(jq, jcr); /* queue the job to run again */
435 free(je); /* free the job entry */
439 * Something was actually backed up, so we cannot reuse
440 * the old JobId or there will be database record
441 * conflicts. We now create a new job, copying the
442 * appropriate fields.
444 JCR *njcr = new_jcr(sizeof(JCR), dird_free_jcr);
445 set_jcr_defaults(njcr, jcr->job);
446 njcr->reschedule_count = jcr->reschedule_count;
447 njcr->JobLevel = jcr->JobLevel;
448 njcr->JobStatus = jcr->JobStatus;
449 njcr->pool = jcr->pool;
450 njcr->store = jcr->store;
451 njcr->messages = jcr->messages;
452 Dmsg0(100, "Call to run new job\n");
456 Dmsg0(100, "Back from running new job.\n");
458 /* Clean up and release old jcr */
460 Dmsg0(100, "Close DB\n");
461 db_close_database(jcr, jcr->db);
464 Dmsg1(100, "====== Termination job=%d\n", jcr->JobId);
466 free(je); /* release job entry */
469 * If any job in the wait queue can be run,
470 * move it to the ready queue
472 Dmsg0(100, "Done check ready, now check wait queue.\n");
473 while (!jq->waiting_jobs->empty() && !jq->quit) {
475 je = (jobq_item_t *)jq->waiting_jobs->first();
476 jobq_item_t *re = (jobq_item_t *)jq->running_jobs->first();
478 Priority = re->jcr->JobPriority;
479 Dmsg1(100, "Set Run pri=%d\n", Priority);
481 Priority = je->jcr->JobPriority;
482 Dmsg1(100, "Set Job pri=%d\n", Priority);
489 jobq_item_t *jn = (jobq_item_t *)jq->waiting_jobs->next(je);
490 Dmsg3(100, "Examining Job=%d JobPri=%d want Pri=%d\n",
491 jcr->JobId, jcr->JobPriority, Priority);
492 /* Take only jobs of correct Priority */
493 if (jcr->JobPriority != Priority) {
494 set_jcr_job_status(jcr, JS_WaitPriority);
497 if (jcr->store->NumConcurrentJobs < jcr->store->MaxConcurrentJobs) {
498 jcr->store->NumConcurrentJobs++;
500 set_jcr_job_status(jcr, JS_WaitStoreRes);
504 if (jcr->client->NumConcurrentJobs < jcr->client->MaxConcurrentJobs) {
505 jcr->client->NumConcurrentJobs++;
507 jcr->store->NumConcurrentJobs--;
508 set_jcr_job_status(jcr, JS_WaitClientRes);
512 if (jcr->job->NumConcurrentJobs < jcr->job->MaxConcurrentJobs) {
513 jcr->job->NumConcurrentJobs++;
515 jcr->store->NumConcurrentJobs--;
516 jcr->client->NumConcurrentJobs--;
517 set_jcr_job_status(jcr, JS_WaitJobRes);
521 jcr->acquired_resource_locks = true;
522 jq->waiting_jobs->remove(je);
523 jq->ready_jobs->append(je);
524 Dmsg1(100, "moved JobId=%d from wait to ready queue\n", je->jcr->JobId);
528 } /* end while loop */
529 Dmsg0(100, "Done checking wait queue.\n");
531 * If no more ready work and we are asked to quit, then do it
533 if (jq->ready_jobs->empty() && jq->quit) {
535 if (jq->num_workers == 0) {
536 Dmsg0(100, "Wake up destroy routine\n");
537 /* Wake up destroy routine if he is waiting */
538 pthread_cond_broadcast(&jq->work);
542 Dmsg0(100, "Check for work request\n");
544 * If no more work requests, and we waited long enough, quit
546 Dmsg2(100, "timedout=%d read empty=%d\n", timedout,
547 jq->ready_jobs->empty());
548 if (jq->ready_jobs->empty() && timedout) {
549 Dmsg0(100, "break big loop\n");
553 Dmsg0(100, "Loop again\n");
555 } /* end of big for loop */
557 Dmsg0(200, "unlock mutex\n");
558 pthread_mutex_unlock(&jq->mutex);
559 Dmsg0(100, "End jobq_server\n");
563 #endif /* JOB_QUEUE */