-
Notifications
You must be signed in to change notification settings - Fork 555
/
StreamProcessorRule.java
executable file
·395 lines (326 loc) · 13.8 KB
/
StreamProcessorRule.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
387
388
389
390
391
392
393
394
395
/*
* 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.util;
import static io.camunda.zeebe.engine.util.StreamProcessingComposite.getLogName;
import io.camunda.zeebe.db.ZeebeDbFactory;
import io.camunda.zeebe.engine.api.CommandResponseWriter;
import io.camunda.zeebe.engine.processing.streamprocessor.TypedRecordProcessorFactory;
import io.camunda.zeebe.engine.state.DefaultZeebeDbFactory;
import io.camunda.zeebe.engine.state.EventApplier;
import io.camunda.zeebe.engine.state.mutable.MutableZeebeState;
import io.camunda.zeebe.engine.util.StreamProcessingComposite.StreamProcessorTestFactory;
import io.camunda.zeebe.engine.util.TestStreams.FluentLogWriter;
import io.camunda.zeebe.logstreams.log.LogStreamRecordWriter;
import io.camunda.zeebe.logstreams.util.ListLogStorage;
import io.camunda.zeebe.logstreams.util.SynchronousLogStream;
import io.camunda.zeebe.msgpack.UnpackedObject;
import io.camunda.zeebe.protocol.record.RecordType;
import io.camunda.zeebe.protocol.record.intent.Intent;
import io.camunda.zeebe.protocol.record.intent.ProcessInstanceIntent;
import io.camunda.zeebe.scheduler.clock.ControlledActorClock;
import io.camunda.zeebe.scheduler.testing.ActorSchedulerRule;
import io.camunda.zeebe.streamprocessor.StreamProcessor;
import io.camunda.zeebe.streamprocessor.StreamProcessorListener;
import io.camunda.zeebe.streamprocessor.StreamProcessorMode;
import io.camunda.zeebe.streamprocessor.state.MutableLastProcessedPositionState;
import io.camunda.zeebe.test.util.AutoCloseableRule;
import io.camunda.zeebe.util.FileUtil;
import io.camunda.zeebe.util.allocation.DirectBufferAllocator;
import java.io.File;
import java.io.IOException;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import org.junit.rules.ExternalResource;
import org.junit.rules.RuleChain;
import org.junit.rules.TemporaryFolder;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class StreamProcessorRule implements TestRule {
private static final Logger LOG = LoggerFactory.getLogger("io.camunda.zeebe.broker.test");
private static final int PARTITION_ID = 0;
// environment
private final TemporaryFolder tempFolder;
private final AutoCloseableRule closeables = new AutoCloseableRule();
private final ControlledActorClock clock = new ControlledActorClock();
private final ActorSchedulerRule actorSchedulerRule = new ActorSchedulerRule(clock);
private final ZeebeDbFactory zeebeDbFactory;
private final SetupRule rule;
private final int startPartitionId;
private final int partitionCount;
private final RuleChain chain;
private TestStreams streams;
private StreamProcessingComposite streamProcessingComposite;
private ListLogStorage sharedStorage = null;
private StreamProcessorMode streamProcessorMode = StreamProcessorMode.PROCESSING;
public StreamProcessorRule() {
this(new TemporaryFolder());
}
public StreamProcessorRule(final TemporaryFolder temporaryFolder) {
this(PARTITION_ID, temporaryFolder);
}
public StreamProcessorRule(final int partitionId) {
this(partitionId, 1, DefaultZeebeDbFactory.defaultFactory(), new TemporaryFolder());
}
public StreamProcessorRule(final int partitionId, final TemporaryFolder temporaryFolder) {
this(partitionId, 1, DefaultZeebeDbFactory.defaultFactory(), temporaryFolder);
}
public StreamProcessorRule(
final int startPartitionId,
final int partitionCount,
final ZeebeDbFactory dbFactory,
final ListLogStorage sharedStorage) {
this(startPartitionId, partitionCount, dbFactory, new TemporaryFolder());
this.sharedStorage = sharedStorage;
}
public StreamProcessorRule(
final int startPartitionId,
final int partitionCount,
final ZeebeDbFactory dbFactory,
final TemporaryFolder temporaryFolder) {
this.startPartitionId = startPartitionId;
this.partitionCount = partitionCount;
rule = new SetupRule(startPartitionId, partitionCount);
tempFolder = temporaryFolder;
zeebeDbFactory = dbFactory;
chain =
RuleChain.outerRule(tempFolder)
.around(actorSchedulerRule)
.around(new CleanUpRule(tempFolder::getRoot))
.around(closeables)
.around(rule);
}
public ActorSchedulerRule getActorSchedulerRule() {
return actorSchedulerRule;
}
@Override
public Statement apply(final Statement base, final Description description) {
return chain.apply(base, description);
}
public StreamProcessorRule withEventApplierFactory(
final Function<MutableZeebeState, EventApplier> eventApplierFactory) {
streams.withEventApplierFactory(eventApplierFactory);
return this;
}
public StreamProcessorRule withStreamProcessorMode(
final StreamProcessorMode streamProcessorMode) {
this.streamProcessorMode = streamProcessorMode;
return this;
}
public LogStreamRecordWriter getLogStreamRecordWriter(final int partitionId) {
return streamProcessingComposite.getLogStreamRecordWriter(partitionId);
}
public LogStreamRecordWriter newLogStreamRecordWriter(final int partitionId) {
return streamProcessingComposite.newLogStreamRecordWriter(partitionId);
}
public StreamProcessor startTypedStreamProcessor(final StreamProcessorTestFactory factory) {
return startTypedStreamProcessor(factory, Optional.empty());
}
public StreamProcessor startTypedStreamProcessor(
final StreamProcessorTestFactory factory,
final Optional<StreamProcessorListener> streamProcessorListenerOpt) {
return streamProcessingComposite.startTypedStreamProcessor(factory, streamProcessorListenerOpt);
}
public StreamProcessor startTypedStreamProcessorNotAwaitOpening(
final StreamProcessorTestFactory factory) {
return streamProcessingComposite.startTypedStreamProcessorNotAwaitOpening(factory);
}
public StreamProcessor startTypedStreamProcessorNotAwaitOpening(
final TypedRecordProcessorFactory factory) {
return streamProcessingComposite.startTypedStreamProcessorNotAwaitOpening(
startPartitionId, factory);
}
public StreamProcessor startTypedStreamProcessor(
final int partitionId,
final TypedRecordProcessorFactory factory,
final Optional<StreamProcessorListener> streamProcessorListenerOpt) {
return streamProcessingComposite.startTypedStreamProcessor(
partitionId, factory, streamProcessorListenerOpt);
}
public void pauseProcessing(final int partitionId) {
streamProcessingComposite.pauseProcessing(partitionId);
}
public void resumeProcessing(final int partitionId) {
streamProcessingComposite.resumeProcessing(partitionId);
}
public void closeStreamProcessor(final int partitionId) {
streamProcessingComposite.closeStreamProcessor(partitionId);
}
public void closeStreamProcessor() {
closeStreamProcessor(startPartitionId);
}
public StreamProcessor getStreamProcessor(final int partitionId) {
return streamProcessingComposite.getStreamProcessor(partitionId);
}
public SynchronousLogStream getLogStream(final int partitionId) {
return streamProcessingComposite.getLogStream(partitionId);
}
public CommandResponseWriter getCommandResponseWriter() {
return streams.getMockedResponseWriter();
}
public StreamProcessorListener getMockStreamProcessorListener() {
return streams.getMockStreamProcessorListener();
}
public ControlledActorClock getClock() {
return clock;
}
public MutableZeebeState getZeebeState() {
return streamProcessingComposite.getZeebeState();
}
public long getLastSuccessfulProcessedRecordPosition() {
return streamProcessingComposite.getLastSuccessfulProcessedRecordPosition();
}
public long getLastWrittenPosition(final int partitionId) {
return streams.getLastWrittenPosition(getLogName(partitionId));
}
public RecordStream events() {
return new RecordStream(streams.events(getLogName(startPartitionId)));
}
public void printAllRecords() {
int partitionId = startPartitionId;
for (int i = 0; i < partitionCount; i++) {
final SynchronousLogStream logStream = streams.getLogStream(getLogName(partitionId++));
LogStreamPrinter.printRecords(logStream);
}
}
public long writeProcessInstanceEvent(final ProcessInstanceIntent intent) {
return writeProcessInstanceEvent(intent, 1);
}
public long writeProcessInstanceEventWithSource(
final ProcessInstanceIntent intent, final int instanceKey, final long sourceEventPosition) {
return streamProcessingComposite.writeProcessInstanceEventWithSource(
intent, instanceKey, sourceEventPosition);
}
public long writeProcessInstanceEvent(final ProcessInstanceIntent intent, final int instanceKey) {
return streamProcessingComposite.writeProcessInstanceEvent(intent, instanceKey);
}
public long writeEvent(final long key, final Intent intent, final UnpackedObject value) {
return streamProcessingComposite.writeEvent(key, intent, value);
}
public long writeEvent(final Intent intent, final UnpackedObject value) {
return streamProcessingComposite.writeEvent(intent, value);
}
public long writeBatch(final RecordToWrite... recordToWrites) {
return streamProcessingComposite.writeBatch(recordToWrites);
}
public long writeCommandOnPartition(
final int partition, final Intent intent, final UnpackedObject value) {
return streamProcessingComposite.writeCommandOnPartition(partition, intent, value);
}
public long writeCommandOnPartition(
final int partition, final long key, final Intent intent, final UnpackedObject value) {
return streamProcessingComposite.writeCommandOnPartition(partition, key, intent, value);
}
public long writeCommand(final long key, final Intent intent, final UnpackedObject value) {
return streamProcessingComposite.writeCommand(key, intent, value);
}
public long writeCommand(final Intent intent, final UnpackedObject value) {
return streamProcessingComposite.writeCommand(intent, value);
}
public long writeCommand(
final int requestStreamId,
final long requestId,
final Intent intent,
final UnpackedObject value) {
return streamProcessingComposite.writeCommand(requestStreamId, requestId, intent, value);
}
public long writeCommandRejection(final Intent intent, final UnpackedObject value) {
return streamProcessingComposite.writeCommandRejection(intent, value);
}
public long writeEvent(
final Intent intent,
final UnpackedObject value,
final UnaryOperator<FluentLogWriter> writer) {
return writeRecord(intent, value, w -> writer.apply(w.recordType(RecordType.EVENT)));
}
public long writeCommandRejection(
final Intent intent,
final UnpackedObject value,
final UnaryOperator<FluentLogWriter> writer) {
return writeRecord(
intent, value, w -> writer.apply(w.recordType(RecordType.COMMAND_REJECTION)));
}
private long writeRecord(
final Intent intent,
final UnpackedObject value,
final UnaryOperator<FluentLogWriter> writer) {
final var recordWriter =
streams
.newRecord(getLogName(startPartitionId))
.recordType(RecordType.EVENT)
.intent(intent)
.event(value);
return writer.apply(recordWriter).write();
}
public void snapshot() {
final var partitionId = startPartitionId;
streamProcessingComposite.snapshot(partitionId);
}
public MutableLastProcessedPositionState getLastProcessedPositionState() {
return streamProcessingComposite.getLastProcessedPositionState();
}
private class SetupRule extends ExternalResource {
private final int startPartitionId;
private final int partitionCount;
SetupRule(final int startPartitionId, final int partitionCount) {
this.startPartitionId = startPartitionId;
this.partitionCount = partitionCount;
}
@Override
protected void before() {
streams = new TestStreams(tempFolder, closeables, actorSchedulerRule.get());
streams.withStreamProcessorMode(streamProcessorMode);
int partitionId = startPartitionId;
for (int i = 0; i < partitionCount; i++) {
if (sharedStorage != null) {
streams.createLogStream(getLogName(partitionId), partitionId++, sharedStorage);
} else {
streams.createLogStream(getLogName(partitionId), partitionId++);
}
}
streamProcessingComposite =
new StreamProcessingComposite(
streams, startPartitionId, zeebeDbFactory, actorSchedulerRule.get());
}
@Override
protected void after() {
streams = null;
streamProcessingComposite = null;
}
}
private class CleanUpRule extends ExternalResource {
private File root;
private final Supplier<File> rootSupplier;
CleanUpRule(final Supplier<File> rootSupplier) {
this.rootSupplier = rootSupplier;
}
@Override
protected void before() {
root = rootSupplier.get();
}
@Override
protected void after() {
try {
LOG.debug("Clean up test files on path {}", root);
FileUtil.deleteFolder(root.toPath());
final long allocatedMemoryInKb = DirectBufferAllocator.getAllocatedMemoryInKb();
if (allocatedMemoryInKb > 0) {
LOG.warn(
"There are still allocated direct buffers of a total size of {}kB.",
allocatedMemoryInKb);
}
} catch (final IOException e) {
LOG.error("Error on deleting root test folder", e);
}
}
}
}