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,
44 /* Forward referenced functions */
45 static void *jobq_server(void *arg);
46 static int start_server(jobq_t *jq);
49 * Initialize a job queue
51 * Returns: 0 on success
54 int jobq_init(jobq_t *jq, int threads, void *(*engine)(void *arg))
57 jobq_item_t *item = NULL;
59 if ((stat = pthread_attr_init(&jq->attr)) != 0) {
62 if ((stat = pthread_attr_setdetachstate(&jq->attr, PTHREAD_CREATE_DETACHED)) != 0) {
63 pthread_attr_destroy(&jq->attr);
66 if ((stat = pthread_mutex_init(&jq->mutex, NULL)) != 0) {
67 pthread_attr_destroy(&jq->attr);
70 if ((stat = pthread_cond_init(&jq->work, NULL)) != 0) {
71 pthread_mutex_destroy(&jq->mutex);
72 pthread_attr_destroy(&jq->attr);
76 jq->max_workers = threads; /* max threads to create */
77 jq->num_workers = 0; /* no threads yet */
78 jq->idle_workers = 0; /* no idle threads */
79 jq->engine = engine; /* routine to run */
80 jq->valid = JOBQ_VALID;
81 /* Initialize the job queues */
82 jq->waiting_jobs = new dlist(item, &item->link);
83 jq->running_jobs = new dlist(item, &item->link);
84 jq->ready_jobs = new dlist(item, &item->link);
89 * Destroy the job queue
91 * Returns: 0 on success
94 int jobq_destroy(jobq_t *jq)
96 int stat, stat1, stat2;
98 if (jq->valid != JOBQ_VALID) {
101 if ((stat = pthread_mutex_lock(&jq->mutex)) != 0) {
104 jq->valid = 0; /* prevent any more operations */
107 * If any threads are active, wake them
109 if (jq->num_workers > 0) {
111 if (jq->idle_workers) {
112 if ((stat = pthread_cond_broadcast(&jq->work)) != 0) {
113 pthread_mutex_unlock(&jq->mutex);
117 while (jq->num_workers > 0) {
118 if ((stat = pthread_cond_wait(&jq->work, &jq->mutex)) != 0) {
119 pthread_mutex_unlock(&jq->mutex);
124 if ((stat = pthread_mutex_unlock(&jq->mutex)) != 0) {
127 stat = pthread_mutex_destroy(&jq->mutex);
128 stat1 = pthread_cond_destroy(&jq->work);
129 stat2 = pthread_attr_destroy(&jq->attr);
130 delete jq->waiting_jobs;
131 delete jq->running_jobs;
132 delete jq->ready_jobs;
133 return (stat != 0 ? stat : (stat1 != 0 ? stat1 : stat2));
142 * Wait until schedule time arrives before starting. Normally
143 * this routine is only used for jobs started from the console
144 * for which the user explicitly specified a start time. Otherwise
145 * most jobs are put into the job queue only when their
146 * scheduled time arives.
148 static void *sched_wait(void *arg)
150 JCR *jcr = ((wait_pkt *)arg)->jcr;
151 jobq_t *jq = ((wait_pkt *)arg)->jq;
153 Dmsg0(100, "Enter sched_wait.\n");
155 time_t wtime = jcr->sched_time - time(NULL);
156 /* Wait until scheduled time arrives */
157 if (wtime > 0 && verbose) {
158 Jmsg(jcr, M_INFO, 0, _("Job %s waiting %d seconds for scheduled start time.\n"),
160 set_jcr_job_status(jcr, JS_WaitStartTime);
162 /* Check every 30 seconds if canceled */
164 Dmsg2(100, "Waiting on sched time, jobid=%d secs=%d\n", jcr->JobId, wtime);
168 bmicrosleep(wtime, 0);
169 if (job_canceled(jcr)) {
172 wtime = jcr->sched_time - time(NULL);
175 Dmsg0(100, "Exit sched_wait\n");
181 * Add a job to the queue
182 * jq is a queue that was created with jobq_init
185 int jobq_add(jobq_t *jq, JCR *jcr)
188 jobq_item_t *item, *li;
189 bool inserted = false;
190 time_t wtime = jcr->sched_time - time(NULL);
195 Dmsg1(100, "jobq_add jobid=%d\n", jcr->JobId);
196 if (jq->valid != JOBQ_VALID) {
200 if (!job_canceled(jcr) && wtime > 0) {
201 set_thread_concurrency(jq->max_workers + 2);
202 sched_pkt = (wait_pkt *)malloc(sizeof(wait_pkt));
203 sched_pkt->jcr = jcr;
205 stat = pthread_create(&id, &jq->attr, sched_wait, (void *)sched_pkt);
209 if ((stat = pthread_mutex_lock(&jq->mutex)) != 0) {
213 if ((item = (jobq_item_t *)malloc(sizeof(jobq_item_t))) == NULL) {
218 if (job_canceled(jcr)) {
219 /* Add job to ready queue so that it is canceled quickly */
220 jq->ready_jobs->prepend(item);
221 Dmsg1(100, "Prepended job=%d to ready queue\n", jcr->JobId);
223 /* Add this job to the wait queue in priority sorted order */
224 for (li=NULL; (li=(jobq_item_t *)jq->waiting_jobs->next(li)); ) {
225 Dmsg2(100, "waiting item jobid=%d priority=%d\n",
226 li->jcr->JobId, li->jcr->JobPriority);
227 if (li->jcr->JobPriority > jcr->JobPriority) {
228 jq->waiting_jobs->insert_before(item, li);
229 Dmsg2(100, "insert_before jobid=%d before %d\n",
230 li->jcr->JobId, jcr->JobId);
235 /* If not jobs in wait queue, append it */
237 jq->waiting_jobs->append(item);
238 Dmsg1(100, "Appended item jobid=%d\n", jcr->JobId);
242 /* Ensure that at least one server looks at the queue. */
243 stat = start_server(jq);
246 pthread_mutex_unlock(&jq->mutex);
248 Dmsg0(100, "Return jobq_add\n");
253 * Remove a job from the job queue. Used only by cancel Console command.
254 * jq is a queue that was created with jobq_init
255 * work_item is an element of work
257 * Note, it is "removed" by immediately calling a processing routine.
258 * if you want to cancel it, you need to provide some external means
261 int jobq_remove(jobq_t *jq, JCR *jcr)
267 Dmsg1(100, "jobq_remove jobid=%d\n", jcr->JobId);
268 if (jq->valid != JOBQ_VALID) {
272 if ((stat = pthread_mutex_lock(&jq->mutex)) != 0) {
276 for (item=NULL; (item=(jobq_item_t *)jq->waiting_jobs->next(item)); ) {
277 if (jcr == item->jcr) {
286 /* Move item to be the first on the list */
287 jq->waiting_jobs->remove(item);
288 jq->ready_jobs->prepend(item);
290 stat = start_server(jq);
294 pthread_mutex_unlock(&jq->mutex);
295 Dmsg0(100, "Return jobq_remove\n");
301 * Start the server thread if it isn't already running
303 static int start_server(jobq_t *jq)
308 /* if any threads are idle, wake one */
309 if (jq->idle_workers > 0) {
310 Dmsg0(100, "Signal worker to wake up\n");
311 if ((stat = pthread_cond_signal(&jq->work)) != 0) {
312 pthread_mutex_unlock(&jq->mutex);
315 } else if (jq->num_workers < jq->max_workers) {
316 Dmsg0(100, "Create worker thread\n");
317 /* No idle threads so create a new one */
318 set_thread_concurrency(jq->max_workers + 1);
319 if ((stat = pthread_create(&id, &jq->attr, jobq_server, (void *)jq)) != 0) {
320 pthread_mutex_unlock(&jq->mutex);
330 * This is the worker thread that serves the job queue.
331 * When all the resources are acquired for the job,
332 * it will call the user's engine.
334 static void *jobq_server(void *arg)
336 struct timespec timeout;
337 jobq_t *jq = (jobq_t *)arg;
338 jobq_item_t *je; /* job entry in queue */
343 Dmsg0(100, "Start jobq_server\n");
344 if ((stat = pthread_mutex_lock(&jq->mutex)) != 0) {
352 Dmsg0(100, "Top of for loop\n");
354 Dmsg0(100, "gettimeofday()\n");
355 gettimeofday(&tv, &tz);
357 timeout.tv_sec = tv.tv_sec + 4;
359 while (!work && !jq->quit) {
361 * Wait 4 seconds, then if no more work, exit
363 Dmsg0(200, "pthread_cond_timedwait()\n");
364 stat = pthread_cond_timedwait(&jq->work, &jq->mutex, &timeout);
365 Dmsg1(100, "timedwait=%d\n", stat);
366 if (stat == ETIMEDOUT) {
369 } else if (stat != 0) {
370 /* This shouldn't happen */
371 Dmsg0(100, "This shouldn't happen\n");
373 pthread_mutex_unlock(&jq->mutex);
378 * If anything is in the ready queue, run it
380 Dmsg0(100, "Checking ready queue.\n");
381 while (!jq->ready_jobs->empty() && !jq->quit) {
383 je = (jobq_item_t *)jq->ready_jobs->first();
385 jq->ready_jobs->remove(je);
386 if (!jq->ready_jobs->empty()) {
387 Dmsg0(100, "ready queue not empty start server\n");
388 if (start_server(jq) != 0) {
392 jq->running_jobs->append(je);
393 Dmsg1(100, "Took jobid=%d from ready and appended to run\n", jcr->JobId);
394 if ((stat = pthread_mutex_unlock(&jq->mutex)) != 0) {
397 /* Call user's routine here */
398 Dmsg1(100, "Calling user engine for jobid=%d\n", jcr->JobId);
400 Dmsg1(100, "Back from user engine jobid=%d.\n", jcr->JobId);
401 if ((stat = pthread_mutex_lock(&jq->mutex)) != 0) {
402 free(je); /* release job entry */
405 Dmsg0(200, "Done lock mutex\n");
406 jq->running_jobs->remove(je);
408 * Release locks if acquired. Note, they will not have
409 * been acquired for jobs canceled before they were
410 * put into the ready queue.
412 if (jcr->acquired_resource_locks) {
413 jcr->store->NumConcurrentJobs--;
414 jcr->client->NumConcurrentJobs--;
415 jcr->job->NumConcurrentJobs--;
418 if (jcr->job->RescheduleOnError &&
419 jcr->JobStatus != JS_Terminated &&
420 jcr->JobStatus != JS_Canceled &&
421 jcr->job->RescheduleTimes > 0 &&
422 jcr->reschedule_count < jcr->job->RescheduleTimes) {
425 * Reschedule this job by cleaning it up, but
426 * reuse the same JobId if possible.
428 jcr->reschedule_count++;
429 jcr->sched_time = time(NULL) + jcr->job->RescheduleInterval;
430 Dmsg2(100, "Rescheduled Job %s to re-run in %d seconds.\n", jcr->Job,
431 (int)jcr->job->RescheduleInterval);
432 jcr->JobStatus = JS_Created; /* force new status */
433 dird_free_jcr(jcr); /* partial cleanup old stuff */
434 if (jcr->JobBytes == 0) {
435 Dmsg1(100, "Requeue job=%d\n", jcr->JobId);
437 jobq_add(jq, jcr); /* queue the job to run again */
439 free(je); /* free the job entry */
440 continue; /* look for another job to run */
443 * Something was actually backed up, so we cannot reuse
444 * the old JobId or there will be database record
445 * conflicts. We now create a new job, copying the
446 * appropriate fields.
448 JCR *njcr = new_jcr(sizeof(JCR), dird_free_jcr);
449 set_jcr_defaults(njcr, jcr->job);
450 njcr->reschedule_count = jcr->reschedule_count;
451 njcr->JobLevel = jcr->JobLevel;
452 njcr->JobStatus = jcr->JobStatus;
453 njcr->pool = jcr->pool;
454 njcr->store = jcr->store;
455 njcr->messages = jcr->messages;
456 Dmsg0(100, "Call to run new job\n");
458 run_job(njcr); /* This creates a "new" job */
460 Dmsg0(100, "Back from running new job.\n");
462 /* Clean up and release old jcr */
464 Dmsg0(100, "Close DB\n");
465 db_close_database(jcr, jcr->db);
468 Dmsg1(100, "====== Termination job=%d\n", jcr->JobId);
470 free(je); /* release job entry */
473 * If any job in the wait queue can be run,
474 * move it to the ready queue
476 Dmsg0(100, "Done check ready, now check wait queue.\n");
477 while (!jq->waiting_jobs->empty() && !jq->quit) {
479 je = (jobq_item_t *)jq->waiting_jobs->first();
480 jobq_item_t *re = (jobq_item_t *)jq->running_jobs->first();
482 Priority = re->jcr->JobPriority;
483 Dmsg1(100, "Set Run pri=%d\n", Priority);
485 Priority = je->jcr->JobPriority;
486 Dmsg1(100, "Set Job pri=%d\n", Priority);
489 * Walk down the list of waiting jobs and attempt
490 * to acquire the resources it needs.
493 /* je is current job item on the queue, jn is the next one */
495 jobq_item_t *jn = (jobq_item_t *)jq->waiting_jobs->next(je);
496 Dmsg3(100, "Examining Job=%d JobPri=%d want Pri=%d\n",
497 jcr->JobId, jcr->JobPriority, Priority);
498 /* Take only jobs of correct Priority */
499 if (jcr->JobPriority != Priority) {
500 set_jcr_job_status(jcr, JS_WaitPriority);
503 if (jcr->store->NumConcurrentJobs < jcr->store->MaxConcurrentJobs) {
504 jcr->store->NumConcurrentJobs++;
506 set_jcr_job_status(jcr, JS_WaitStoreRes);
510 if (jcr->client->NumConcurrentJobs < jcr->client->MaxConcurrentJobs) {
511 jcr->client->NumConcurrentJobs++;
513 jcr->store->NumConcurrentJobs--;
514 set_jcr_job_status(jcr, JS_WaitClientRes);
518 if (jcr->job->NumConcurrentJobs < jcr->job->MaxConcurrentJobs) {
519 jcr->job->NumConcurrentJobs++;
521 jcr->store->NumConcurrentJobs--;
522 jcr->client->NumConcurrentJobs--;
523 set_jcr_job_status(jcr, JS_WaitJobRes);
527 /* Got all locks, now remove it from wait queue and append it
530 jcr->acquired_resource_locks = true;
531 jq->waiting_jobs->remove(je);
532 jq->ready_jobs->append(je);
533 Dmsg1(100, "moved JobId=%d from wait to ready queue\n", je->jcr->JobId);
537 } /* end while loop */
538 Dmsg0(100, "Done checking wait queue.\n");
540 * If no more ready work and we are asked to quit, then do it
542 if (jq->ready_jobs->empty() && jq->quit) {
544 if (jq->num_workers == 0) {
545 Dmsg0(100, "Wake up destroy routine\n");
546 /* Wake up destroy routine if he is waiting */
547 pthread_cond_broadcast(&jq->work);
551 Dmsg0(100, "Check for work request\n");
553 * If no more work requests, and we waited long enough, quit
555 Dmsg2(100, "timedout=%d read empty=%d\n", timedout,
556 jq->ready_jobs->empty());
557 if (jq->ready_jobs->empty() && timedout) {
558 Dmsg0(100, "break big loop\n");
562 Dmsg0(100, "Loop again\n");
564 } /* end of big for loop */
566 Dmsg0(200, "unlock mutex\n");
567 pthread_mutex_unlock(&jq->mutex);
568 Dmsg0(100, "End jobq_server\n");