forked from beanstalkd/beanstalkd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.c
269 lines (215 loc) · 5.13 KB
/
job.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "dat.h"
#include "snowflake.h"
static snowflake_st st= {
.last_timestamp = 0,
.datacenter = 0,
.machine = 0,
.seq = 0
};
static uint64 next_id = 0;
static int cur_prime = 0;
static job all_jobs_init[12289] = {0};
static job *all_jobs = all_jobs_init;
static size_t all_jobs_cap = 12289; /* == primes[0] */
static size_t all_jobs_used = 0;
static int hash_table_was_oom = 0;
static void rehash();
static int
_get_job_hash_index(uint64 job_id)
{
return job_id % all_jobs_cap;
}
static void
store_job(job j)
{
int index = 0;
index = _get_job_hash_index(j->r.id);
j->ht_next = all_jobs[index];
all_jobs[index] = j;
all_jobs_used++;
/* accept a load factor of 4 */
if (all_jobs_used > (all_jobs_cap << 2)) rehash(1);
}
static void
rehash(int is_upscaling)
{
job *old = all_jobs;
size_t old_cap = all_jobs_cap, old_used = all_jobs_used, i;
int old_prime = cur_prime;
int d = is_upscaling ? 1 : -1;
if (cur_prime + d >= NUM_PRIMES) return;
if (cur_prime + d < 0) return;
if (is_upscaling && hash_table_was_oom) return;
cur_prime += d;
all_jobs_cap = primes[cur_prime];
all_jobs = calloc(all_jobs_cap, sizeof(job));
if (!all_jobs) {
twarnx("Failed to allocate %zu new hash buckets", all_jobs_cap);
hash_table_was_oom = 1;
cur_prime = old_prime;
all_jobs = old;
all_jobs_cap = old_cap;
all_jobs_used = old_used;
return;
}
all_jobs_used = 0;
hash_table_was_oom = 0;
for (i = 0; i < old_cap; i++) {
while (old[i]) {
job j = old[i];
old[i] = j->ht_next;
j->ht_next = NULL;
store_job(j);
}
}
if (old != all_jobs_init) {
free(old);
}
}
job
job_find(uint64 job_id)
{
job jh = NULL;
int index = _get_job_hash_index(job_id);
for (jh = all_jobs[index]; jh && jh->r.id != job_id; jh = jh->ht_next);
return jh;
}
job
allocate_job(int body_size)
{
job j;
j = malloc(sizeof(struct job) + body_size);
if (!j) return twarnx("OOM"), (job) 0;
memset(j, 0, sizeof(struct job));
j->r.created_at = nanoseconds();
j->r.body_size = body_size;
j->next = j->prev = j; /* not in a linked list */
return j;
}
job
make_job_with_id(uint pri, int64 delay, int64 ttr,
int body_size, tube tube, uint64 id)
{
job j;
j = allocate_job(body_size);
if (!j) return twarnx("OOM"), (job) 0;
if (id) {
j->r.id = id;
// if (id >= next_id) next_id = id + 1;
} else {
j->r.id = snowflake_id(st);
next_id++;
}
j->r.pri = pri;
j->r.delay = delay;
j->r.ttr = ttr;
store_job(j);
TUBE_ASSIGN(j->tube, tube);
return j;
}
static void
job_hash_free(job j)
{
job *slot;
slot = &all_jobs[_get_job_hash_index(j->r.id)];
while (*slot && *slot != j) slot = &(*slot)->ht_next;
if (*slot) {
*slot = (*slot)->ht_next;
--all_jobs_used;
}
// Downscale when the hashmap is too sparse
if (all_jobs_used < (all_jobs_cap >> 4)) rehash(0);
}
void
job_free(job j)
{
if (j) {
TUBE_ASSIGN(j->tube, NULL);
if (j->r.state != Copy) job_hash_free(j);
}
free(j);
}
void
job_setheappos(void *j, int pos)
{
((job)j)->heap_index = pos;
}
int
job_pri_less(void *ax, void *bx)
{
job a = ax, b = bx;
if (a->r.pri < b->r.pri) return 1;
if (a->r.pri > b->r.pri) return 0;
return a->r.id < b->r.id;
}
int
job_delay_less(void *ax, void *bx)
{
job a = ax, b = bx;
if (a->r.deadline_at < b->r.deadline_at) return 1;
if (a->r.deadline_at > b->r.deadline_at) return 0;
return a->r.id < b->r.id;
}
job
job_copy(job j)
{
job n;
if (!j) return NULL;
n = malloc(sizeof(struct job) + j->r.body_size);
if (!n) return twarnx("OOM"), (job) 0;
memcpy(n, j, sizeof(struct job) + j->r.body_size);
n->next = n->prev = n; /* not in a linked list */
n->file = NULL; /* copies do not have refcnt on the wal */
n->tube = 0; /* Don't use memcpy for the tube, which we must refcount. */
TUBE_ASSIGN(n->tube, j->tube);
/* Mark this job as a copy so it can be appropriately freed later on */
n->r.state = Copy;
return n;
}
const char *
job_state(job j)
{
if (j->r.state == Ready) return "ready";
if (j->r.state == Reserved) return "reserved";
if (j->r.state == Buried) return "buried";
if (j->r.state == Delayed) return "delayed";
return "invalid";
}
int
job_list_any_p(job head)
{
return head->next != head || head->prev != head;
}
job
job_remove(job j)
{
if (!j) return NULL;
if (!job_list_any_p(j)) return NULL; /* not in a doubly-linked list */
j->next->prev = j->prev;
j->prev->next = j->next;
j->prev = j->next = j;
return j;
}
void
job_insert(job head, job j)
{
if (job_list_any_p(j)) return; /* already in a linked list */
j->prev = head->prev;
j->next = head;
head->prev->next = j;
head->prev = j;
}
uint64
total_jobs()
{
return next_id;
}
/* for unit tests */
size_t
get_all_jobs_used()
{
return all_jobs_used;
}