More descriptive error message.
[beanstalkd.git] / job.c
blob008a13539889554a91f22e26796ae474591b5f14
1 /* job.c - a job in the queue */
3 /* Copyright (C) 2007 Keith Rarick and Philotic Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <stdlib.h>
20 #include <string.h>
22 #include "tube.h"
23 #include "job.h"
24 #include "primes.h"
25 #include "util.h"
27 static uint64_t next_id = 1;
29 static int cur_prime = 0;
31 static job *all_jobs = NULL;
32 static size_t all_jobs_cap = 12289; /* == primes[0] */
33 static size_t all_jobs_used = 0;
35 static int hash_table_was_oom = 0;
37 static void rehash();
39 static int
40 _get_job_hash_index(uint64_t job_id)
42 return job_id % all_jobs_cap;
45 static void
46 store_job(job j)
48 int index = 0;
50 index = _get_job_hash_index(j->id);
52 j->ht_next = all_jobs[index];
53 all_jobs[index] = j;
54 all_jobs_used++;
56 /* accept a load factor of 4 */
57 if (all_jobs_used > (all_jobs_cap << 2)) rehash();
60 static void
61 rehash()
63 job *old = all_jobs;
64 size_t old_cap = all_jobs_cap, old_used = all_jobs_used, i;
66 if (cur_prime >= NUM_PRIMES) return;
67 if (hash_table_was_oom) return;
69 all_jobs_cap = primes[++cur_prime];
70 all_jobs = calloc(all_jobs_cap, sizeof(job));
71 if (!all_jobs) {
72 twarnx("Failed to allocate %d new hash buckets", all_jobs_cap);
73 hash_table_was_oom = 1;
74 --cur_prime;
75 all_jobs = old;
76 all_jobs_cap = old_cap;
77 all_jobs_used = old_used;
78 return;
80 all_jobs_used = 0;
82 for (i = 0; i < old_cap; i++) {
83 while (old[i]) {
84 job j = old[i];
85 old[i] = j->ht_next;
86 j->ht_next = NULL;
87 store_job(j);
90 free(old);
93 job
94 job_find(uint64_t job_id)
96 job jh = NULL;
97 int index = _get_job_hash_index(job_id);
99 for (jh = all_jobs[index]; jh && jh->id != job_id; jh = jh->ht_next);
101 return jh;
105 allocate_job(int body_size)
107 job j;
109 j = malloc(sizeof(struct job) + body_size);
110 if (!j) return twarnx("OOM"), (job) 0;
112 j->id = 0;
113 j->state = JOB_STATE_INVALID;
114 j->created_at = now_usec();
115 j->reserve_ct = j->timeout_ct = j->release_ct = j->bury_ct = j->kick_ct = 0;
116 j->body_size = body_size;
117 j->next = j->prev = j; /* not in a linked list */
118 j->ht_next = NULL;
119 j->tube = NULL;
120 j->binlog = NULL;
121 j->heap_index = 0;
122 j->reserved_binlog_space = 0;
124 return j;
128 make_job_with_id(unsigned int pri, usec delay, usec ttr,
129 int body_size, tube tube, uint64_t id)
131 job j;
133 j = allocate_job(body_size);
134 if (!j) return twarnx("OOM"), (job) 0;
136 if (id) {
137 j->id = id;
138 if (id >= next_id) next_id = id + 1;
139 } else {
140 j->id = next_id++;
142 j->pri = pri;
143 j->delay = delay;
144 j->ttr = ttr;
146 store_job(j);
148 TUBE_ASSIGN(j->tube, tube);
150 return j;
153 static void
154 job_hash_free(job j)
156 job *slot;
158 slot = &all_jobs[_get_job_hash_index(j->id)];
159 while (*slot && *slot != j) slot = &(*slot)->ht_next;
160 if (*slot) {
161 *slot = (*slot)->ht_next;
162 --all_jobs_used;
166 void
167 job_free(job j)
169 if (j) {
170 TUBE_ASSIGN(j->tube, NULL);
171 if (j->state != JOB_STATE_COPY) job_hash_free(j);
174 free(j);
177 /* We can't substrct any of these values because there are too many bits */
179 job_pri_cmp(job a, job b)
181 if (a->pri > b->pri) return 1;
182 if (a->pri < b->pri) return -1;
183 if (a->id > b->id) return 1;
184 if (a->id < b->id) return -1;
185 return 0;
188 /* We can't substrct any of these values because there are too many bits */
190 job_delay_cmp(job a, job b)
192 if (a->deadline_at > b->deadline_at) return 1;
193 if (a->deadline_at < b->deadline_at) return -1;
194 if (a->id > b->id) return 1;
195 if (a->id < b->id) return -1;
196 return 0;
200 job_copy(job j)
202 job n;
204 if (!j) return NULL;
206 n = malloc(sizeof(struct job) + j->body_size);
207 if (!n) return twarnx("OOM"), (job) 0;
209 memcpy(n, j, sizeof(struct job) + j->body_size);
210 n->next = n->prev = n; /* not in a linked list */
212 n->binlog = NULL; /* copies do not have refcnt on the binlog */
214 n->tube = 0; /* Don't use memcpy for the tube, which we must refcount. */
215 TUBE_ASSIGN(n->tube, j->tube);
217 /* Mark this job as a copy so it can be appropriately freed later on */
218 n->state = JOB_STATE_COPY;
220 return n;
223 const char *
224 job_state(job j)
226 if (j->state == JOB_STATE_READY) return "ready";
227 if (j->state == JOB_STATE_RESERVED) return "reserved";
228 if (j->state == JOB_STATE_BURIED) return "buried";
229 if (j->state == JOB_STATE_DELAYED) return "delayed";
230 return "invalid";
234 job_list_any_p(job head)
236 return head->next != head || head->prev != head;
240 job_remove(job j)
242 if (!j) return NULL;
243 if (!job_list_any_p(j)) return NULL; /* not in a doubly-linked list */
245 j->next->prev = j->prev;
246 j->prev->next = j->next;
248 j->prev = j->next = j;
250 return j;
253 void
254 job_insert(job head, job j)
256 if (job_list_any_p(j)) return; /* already in a linked list */
258 j->prev = head->prev;
259 j->next = head;
260 head->prev->next = j;
261 head->prev = j;
264 uint64_t
265 total_jobs()
267 return next_id - 1;
270 /* for unit tests */
271 size_t
272 get_all_jobs_used()
274 return all_jobs_used;
277 void
278 job_init()
280 all_jobs = calloc(all_jobs_cap, sizeof(job));
281 if (!all_jobs) {
282 twarnx("Failed to allocate %d hash buckets", all_jobs_cap);