-
Notifications
You must be signed in to change notification settings - Fork 558
/
DbJobState.java
386 lines (317 loc) · 12.4 KB
/
DbJobState.java
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH under
* one or more contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright ownership.
* Licensed under the Zeebe Community License 1.1. You may not use this file
* except in compliance with the Zeebe Community License 1.1.
*/
package io.camunda.zeebe.engine.state.instance;
import io.camunda.zeebe.db.ColumnFamily;
import io.camunda.zeebe.db.TransactionContext;
import io.camunda.zeebe.db.ZeebeDb;
import io.camunda.zeebe.db.impl.DbCompositeKey;
import io.camunda.zeebe.db.impl.DbForeignKey;
import io.camunda.zeebe.db.impl.DbLong;
import io.camunda.zeebe.db.impl.DbNil;
import io.camunda.zeebe.db.impl.DbString;
import io.camunda.zeebe.engine.Loggers;
import io.camunda.zeebe.engine.state.immutable.JobState;
import io.camunda.zeebe.engine.state.mutable.MutableJobState;
import io.camunda.zeebe.protocol.ZbColumnFamilies;
import io.camunda.zeebe.protocol.impl.record.value.job.JobRecord;
import io.camunda.zeebe.util.EnsureUtil;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import org.agrona.DirectBuffer;
import org.slf4j.Logger;
public final class DbJobState implements JobState, MutableJobState {
private static final Logger LOG = Loggers.PROCESS_PROCESSOR_LOGGER;
// key => job record value
// we need two separate wrapper to not interfere with get and put
// see https://github.com/zeebe-io/zeebe/issues/1914
private final JobRecordValue jobRecordToRead = new JobRecordValue();
private final JobRecordValue jobRecordToWrite = new JobRecordValue();
private final DbLong jobKey;
private final DbForeignKey<DbLong> fkJob;
private final ColumnFamily<DbLong, JobRecordValue> jobsColumnFamily;
// key => job state
private final JobStateValue jobState = new JobStateValue();
private final ColumnFamily<DbForeignKey<DbLong>, JobStateValue> statesJobColumnFamily;
// type => [key]
private final DbString jobTypeKey;
private final DbCompositeKey<DbString, DbForeignKey<DbLong>> typeJobKey;
private final ColumnFamily<DbCompositeKey<DbString, DbForeignKey<DbLong>>, DbNil>
activatableColumnFamily;
// timeout => key
private final DbLong deadlineKey;
private final DbCompositeKey<DbLong, DbForeignKey<DbLong>> deadlineJobKey;
private final ColumnFamily<DbCompositeKey<DbLong, DbForeignKey<DbLong>>, DbNil>
deadlinesColumnFamily;
private final DbLong backoffKey;
private final DbCompositeKey<DbLong, DbForeignKey<DbLong>> backoffJobKey;
private final ColumnFamily<DbCompositeKey<DbLong, DbForeignKey<DbLong>>, DbNil>
backoffColumnFamily;
private long nextBackOffDueDate;
public DbJobState(
final ZeebeDb<ZbColumnFamilies> zeebeDb,
final TransactionContext transactionContext,
final int partitionId) {
jobKey = new DbLong();
fkJob = new DbForeignKey<>(jobKey, ZbColumnFamilies.JOBS);
jobsColumnFamily =
zeebeDb.createColumnFamily(
ZbColumnFamilies.JOBS, transactionContext, jobKey, jobRecordToRead);
statesJobColumnFamily =
zeebeDb.createColumnFamily(
ZbColumnFamilies.JOB_STATES, transactionContext, fkJob, jobState);
jobTypeKey = new DbString();
typeJobKey = new DbCompositeKey<>(jobTypeKey, fkJob);
activatableColumnFamily =
zeebeDb.createColumnFamily(
ZbColumnFamilies.JOB_ACTIVATABLE, transactionContext, typeJobKey, DbNil.INSTANCE);
deadlineKey = new DbLong();
deadlineJobKey = new DbCompositeKey<>(deadlineKey, fkJob);
deadlinesColumnFamily =
zeebeDb.createColumnFamily(
ZbColumnFamilies.JOB_DEADLINES, transactionContext, deadlineJobKey, DbNil.INSTANCE);
backoffKey = new DbLong();
backoffJobKey = new DbCompositeKey<>(backoffKey, fkJob);
backoffColumnFamily =
zeebeDb.createColumnFamily(
ZbColumnFamilies.JOB_BACKOFF, transactionContext, backoffJobKey, DbNil.INSTANCE);
}
@Override
public void create(final long key, final JobRecord record) {
final DirectBuffer type = record.getTypeBuffer();
createJob(key, record, type);
}
@Override
public void activate(final long key, final JobRecord record) {
final DirectBuffer type = record.getTypeBuffer();
final long deadline = record.getDeadline();
validateParameters(type);
EnsureUtil.ensureGreaterThan("deadline", deadline, 0);
updateJobRecord(key, record);
updateJobState(State.ACTIVATED);
makeJobNotActivatable(type);
deadlineKey.wrapLong(deadline);
deadlinesColumnFamily.insert(deadlineJobKey, DbNil.INSTANCE);
}
@Override
public void recurAfterBackoff(final long key, final JobRecord record) {
updateJob(key, record, State.ACTIVATABLE);
jobKey.wrapLong(key);
backoffKey.wrapLong(record.getRecurringTime());
backoffColumnFamily.deleteExisting(backoffJobKey);
}
@Override
public void timeout(final long key, final JobRecord record) {
final DirectBuffer type = record.getTypeBuffer();
final long deadline = record.getDeadline();
validateParameters(type);
EnsureUtil.ensureGreaterThan("deadline", deadline, 0);
updateJob(key, record, State.ACTIVATABLE);
removeJobDeadline(deadline);
}
@Override
public void complete(final long key, final JobRecord record) {
delete(key, record);
}
@Override
public void cancel(final long key, final JobRecord record) {
delete(key, record);
}
@Override
public void disable(final long key, final JobRecord record) {
updateJob(key, record, State.FAILED);
makeJobNotActivatable(record.getTypeBuffer());
}
@Override
public void throwError(final long key, final JobRecord updatedValue) {
updateJob(key, updatedValue, State.ERROR_THROWN);
makeJobNotActivatable(updatedValue.getTypeBuffer());
}
@Override
public void delete(final long key, final JobRecord record) {
final DirectBuffer type = record.getTypeBuffer();
final long deadline = record.getDeadline();
jobKey.wrapLong(key);
jobsColumnFamily.deleteExisting(jobKey);
statesJobColumnFamily.deleteExisting(fkJob);
makeJobNotActivatable(type);
removeJobDeadline(deadline);
}
@Override
public void fail(final long key, final JobRecord updatedValue) {
if (updatedValue.getRetries() > 0) {
if (updatedValue.getRetryBackoff() > 0) {
jobKey.wrapLong(key);
backoffKey.wrapLong(updatedValue.getRecurringTime());
backoffColumnFamily.insert(backoffJobKey, DbNil.INSTANCE);
updateJob(key, updatedValue, State.FAILED);
} else {
updateJob(key, updatedValue, State.ACTIVATABLE);
}
} else {
updateJob(key, updatedValue, State.FAILED);
makeJobNotActivatable(updatedValue.getTypeBuffer());
}
}
@Override
public void yield(final long key, final JobRecord updatedValue) {
updateJob(key, updatedValue, State.ACTIVATABLE);
}
@Override
public void resolve(final long key, final JobRecord updatedValue) {
updateJob(key, updatedValue, State.ACTIVATABLE);
}
@Override
public JobRecord updateJobRetries(final long jobKey, final int retries) {
final JobRecord job = getJob(jobKey);
if (job != null) {
job.setRetries(retries);
updateJobRecord(jobKey, job);
}
return job;
}
private void createJob(final long key, final JobRecord record, final DirectBuffer type) {
createJobRecord(key, record);
initializeJobState();
makeJobActivatable(type, key);
}
private void updateJob(final long key, final JobRecord updatedValue, final State newState) {
final DirectBuffer type = updatedValue.getTypeBuffer();
final long deadline = updatedValue.getDeadline();
validateParameters(type);
updateJobRecord(key, updatedValue);
updateJobState(newState);
if (newState == State.ACTIVATABLE) {
makeJobActivatable(type, key);
}
if (deadline > 0) {
removeJobDeadline(deadline);
}
}
private void validateParameters(final DirectBuffer type) {
EnsureUtil.ensureNotNullOrEmpty("type", type);
}
@Override
public void forEachTimedOutEntry(
final long upperBound, final BiFunction<Long, JobRecord, Boolean> callback) {
deadlinesColumnFamily.whileTrue(
(key, value) -> {
final long deadline = key.first().getValue();
final boolean isDue = deadline < upperBound;
if (isDue) {
final long jobKey1 = key.second().inner().getValue();
return visitJob(
jobKey1, callback::apply, () -> deadlinesColumnFamily.deleteExisting(key));
}
return false;
});
}
@Override
public boolean exists(final long jobKey) {
this.jobKey.wrapLong(jobKey);
return jobsColumnFamily.exists(this.jobKey);
}
@Override
public State getState(final long key) {
jobKey.wrapLong(key);
final JobStateValue storedState = statesJobColumnFamily.get(fkJob);
if (storedState == null) {
return State.NOT_FOUND;
}
return storedState.getState();
}
@Override
public boolean isInState(final long key, final State state) {
return getState(key) == state;
}
@Override
public void forEachActivatableJobs(
final DirectBuffer type, final BiFunction<Long, JobRecord, Boolean> callback) {
jobTypeKey.wrapBuffer(type);
activatableColumnFamily.whileEqualPrefix(
jobTypeKey,
((compositeKey, zbNil) -> {
final long jobKey = compositeKey.second().inner().getValue();
// TODO #6521 reconsider race condition and whether or not the cleanup task is needed
return visitJob(jobKey, callback::apply, () -> {});
}));
}
@Override
public JobRecord getJob(final long key) {
jobKey.wrapLong(key);
final JobRecordValue jobState = jobsColumnFamily.get(jobKey);
return jobState == null ? null : jobState.getRecord();
}
@Override
public long findBackedOffJobs(final long timestamp, final BiPredicate<Long, JobRecord> callback) {
nextBackOffDueDate = -1L;
backoffColumnFamily.whileTrue(
(key, value) -> {
final long deadline = key.first().getValue();
boolean consumed = false;
if (deadline <= timestamp) {
final long jobKey = key.second().inner().getValue();
consumed = visitJob(jobKey, callback, () -> backoffColumnFamily.deleteExisting(key));
}
if (!consumed) {
nextBackOffDueDate = deadline;
}
return consumed;
});
return nextBackOffDueDate;
}
boolean visitJob(
final long jobKey,
final BiPredicate<Long, JobRecord> callback,
final Runnable cleanupRunnable) {
final JobRecord job = getJob(jobKey);
if (job == null) {
LOG.error("Expected to find job with key {}, but no job found", jobKey);
cleanupRunnable.run();
return true; // we want to continue with the iteration
}
return callback.test(jobKey, job);
}
private void createJobRecord(final long key, final JobRecord record) {
jobKey.wrapLong(key);
// do not persist variables in job state
jobRecordToWrite.setRecordWithoutVariables(record);
jobsColumnFamily.insert(jobKey, jobRecordToWrite);
}
/** Updates the job record without updating variables */
private void updateJobRecord(final long key, final JobRecord updatedValue) {
jobKey.wrapLong(key);
// do not persist variables in job state
jobRecordToWrite.setRecordWithoutVariables(updatedValue);
jobsColumnFamily.update(jobKey, jobRecordToWrite);
}
private void initializeJobState() {
jobState.setState(State.ACTIVATABLE);
statesJobColumnFamily.insert(fkJob, jobState);
}
private void updateJobState(final State newState) {
jobState.setState(newState);
statesJobColumnFamily.update(fkJob, jobState);
}
private void makeJobActivatable(final DirectBuffer type, final long key) {
EnsureUtil.ensureNotNullOrEmpty("type", type);
jobTypeKey.wrapBuffer(type);
jobKey.wrapLong(key);
// Need to upsert here because jobs can be marked as failed (and thus made activatable)
// without activating them first
activatableColumnFamily.upsert(typeJobKey, DbNil.INSTANCE);
}
private void makeJobNotActivatable(final DirectBuffer type) {
EnsureUtil.ensureNotNullOrEmpty("type", type);
jobTypeKey.wrapBuffer(type);
activatableColumnFamily.deleteIfExists(typeJobKey);
}
private void removeJobDeadline(final long deadline) {
deadlineKey.wrapLong(deadline);
deadlinesColumnFamily.deleteIfExists(deadlineJobKey);
}
}