-
Notifications
You must be signed in to change notification settings - Fork 556
/
StreamProcessingComposite.java
316 lines (270 loc) · 11.1 KB
/
StreamProcessingComposite.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
/*
* 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.Records.processInstance;
import io.camunda.zeebe.db.ZeebeDbFactory;
import io.camunda.zeebe.engine.processing.streamprocessor.TypedRecordProcessorContext;
import io.camunda.zeebe.engine.processing.streamprocessor.TypedRecordProcessorFactory;
import io.camunda.zeebe.engine.processing.streamprocessor.TypedRecordProcessors;
import io.camunda.zeebe.engine.state.mutable.MutableZeebeState;
import io.camunda.zeebe.logstreams.log.LogStreamRecordWriter;
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.Actor;
import io.camunda.zeebe.scheduler.ActorScheduler;
import io.camunda.zeebe.scheduler.future.ActorFuture;
import io.camunda.zeebe.streamprocessor.StreamProcessor;
import io.camunda.zeebe.streamprocessor.StreamProcessorListener;
import io.camunda.zeebe.streamprocessor.state.MutableLastProcessedPositionState;
import java.util.Optional;
import java.util.concurrent.Callable;
public class StreamProcessingComposite {
private static final String STREAM_NAME = "stream-";
private final TestStreams streams;
private final int partitionId;
private final ZeebeDbFactory<?> zeebeDbFactory;
private MutableZeebeState zeebeState;
private MutableLastProcessedPositionState lastProcessedPositionState = null;
private final WriteActor writeActor = new WriteActor();
public StreamProcessingComposite(
final TestStreams streams,
final int partitionId,
final ZeebeDbFactory<?> zeebeDbFactory,
final ActorScheduler actorScheduler) {
this.streams = streams;
this.partitionId = partitionId;
this.zeebeDbFactory = zeebeDbFactory;
actorScheduler.submitActor(writeActor);
}
public LogStreamRecordWriter getLogStreamRecordWriter(final int partitionId) {
final String logName = getLogName(partitionId);
return streams.getLogStreamRecordWriter(logName);
}
public SynchronousLogStream getLogStream(final int partitionId) {
return streams.getLogStream(getLogName(partitionId));
}
public LogStreamRecordWriter newLogStreamRecordWriter(final int partitionId) {
final String logName = getLogName(partitionId);
return streams.newLogStreamRecordWriter(logName);
}
public StreamProcessor startTypedStreamProcessor(
final StreamProcessorTestFactory factory,
final Optional<StreamProcessorListener> streamProcessorListenerOpt) {
return startTypedStreamProcessor(
(processingContext) -> createTypedRecordProcessors(factory, processingContext),
streamProcessorListenerOpt);
}
private TypedRecordProcessors createTypedRecordProcessors(
final StreamProcessorTestFactory factory,
final TypedRecordProcessorContext typedRecordProcessorContext) {
zeebeState = typedRecordProcessorContext.getZeebeState();
return factory.build(
TypedRecordProcessors.processors(
zeebeState.getKeyGenerator(), typedRecordProcessorContext.getWriters()),
typedRecordProcessorContext);
}
public StreamProcessor startTypedStreamProcessor(
final TypedRecordProcessorFactory factory,
final Optional<StreamProcessorListener> streamProcessorListenerOpt) {
return startTypedStreamProcessor(partitionId, factory, streamProcessorListenerOpt);
}
public StreamProcessor startTypedStreamProcessor(
final int partitionId,
final TypedRecordProcessorFactory factory,
final Optional<StreamProcessorListener> streamProcessorListenerOpt) {
final var result =
streams.startStreamProcessor(
getLogName(partitionId),
zeebeDbFactory,
(processingContext -> {
zeebeState = processingContext.getZeebeState();
return factory.createProcessors(processingContext);
}),
streamProcessorListenerOpt);
lastProcessedPositionState = result.getStreamProcessorDbState().getLastProcessedPositionState();
return result;
}
public StreamProcessor startTypedStreamProcessorNotAwaitOpening(
final StreamProcessorTestFactory factory) {
return startTypedStreamProcessorNotAwaitOpening(
(processingContext) -> createTypedRecordProcessors(factory, processingContext));
}
public StreamProcessor startTypedStreamProcessorNotAwaitOpening(
final TypedRecordProcessorFactory factory) {
return startTypedStreamProcessorNotAwaitOpening(partitionId, factory);
}
public StreamProcessor startTypedStreamProcessorNotAwaitOpening(
final int partitionId, final TypedRecordProcessorFactory factory) {
final var result =
streams.startStreamProcessorNotAwaitOpening(
getLogName(partitionId),
zeebeDbFactory,
(processingContext -> {
zeebeState = processingContext.getZeebeState();
return factory.createProcessors(processingContext);
}),
Optional.empty());
lastProcessedPositionState = result.getStreamProcessorDbState().getLastProcessedPositionState();
return result;
}
public void pauseProcessing(final int partitionId) {
streams.pauseProcessing(getLogName(partitionId));
}
public void resumeProcessing(final int partitionId) {
streams.resumeProcessing(getLogName(partitionId));
}
public void snapshot(final int partitionId) {
streams.snapshot(getLogName(partitionId));
}
public void closeStreamProcessor(final int partitionId) {
try {
streams.closeProcessor(getLogName(partitionId));
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
public StreamProcessor getStreamProcessor(final int partitionId) {
return streams.getStreamProcessor(getLogName(partitionId));
}
public MutableZeebeState getZeebeState() {
return zeebeState;
}
public long getLastSuccessfulProcessedRecordPosition() {
return lastProcessedPositionState.getLastSuccessfulProcessedRecordPosition();
}
public RecordStream events() {
return new RecordStream(streams.events(getLogName(partitionId)));
}
public long writeProcessInstanceEventWithSource(
final ProcessInstanceIntent intent, final int instanceKey, final long sourceEventPosition) {
final var writer =
streams
.newRecord(getLogName(partitionId))
.event(processInstance(instanceKey))
.recordType(RecordType.EVENT)
.sourceRecordPosition(sourceEventPosition)
.intent(intent);
return writeActor.submit(writer::write).join();
}
public long writeProcessInstanceEvent(final ProcessInstanceIntent intent, final int instanceKey) {
final var writer =
streams
.newRecord(getLogName(partitionId))
.event(processInstance(instanceKey))
.recordType(RecordType.EVENT)
.intent(intent);
return writeActor.submit(writer::write).join();
}
public long writeEvent(final long key, final Intent intent, final UnpackedObject value) {
final var writer =
streams
.newRecord(getLogName(partitionId))
.recordType(RecordType.EVENT)
.key(key)
.intent(intent)
.event(value);
return writeActor.submit(writer::write).join();
}
public long writeEvent(final Intent intent, final UnpackedObject value) {
final var writer =
streams
.newRecord(getLogName(partitionId))
.recordType(RecordType.EVENT)
.intent(intent)
.event(value);
return writeActor.submit(writer::write).join();
}
public long writeBatch(final RecordToWrite... recordsToWrite) {
final var batchWriter = streams.setupBatchWriter(getLogName(partitionId), recordsToWrite);
return writeActor.submit(batchWriter::tryWrite).join();
}
public long writeCommandOnPartition(
final int partition, final Intent intent, final UnpackedObject value) {
final var writer =
streams
.newRecord(getLogName(partition))
.recordType(RecordType.COMMAND)
.intent(intent)
.event(value);
return writeActor.submit(writer::write).join();
}
public long writeCommandOnPartition(
final int partition, final long key, final Intent intent, final UnpackedObject value) {
final var writer =
streams
.newRecord(getLogName(partition))
.key(key)
.recordType(RecordType.COMMAND)
.intent(intent)
.event(value);
return writeActor.submit(writer::write).join();
}
public long writeCommand(final long key, final Intent intent, final UnpackedObject value) {
final var writer =
streams
.newRecord(getLogName(partitionId))
.recordType(RecordType.COMMAND)
.key(key)
.intent(intent)
.event(value);
return writeActor.submit(writer::write).join();
}
public long writeCommand(final Intent intent, final UnpackedObject value) {
final var writer =
streams
.newRecord(getLogName(partitionId))
.recordType(RecordType.COMMAND)
.intent(intent)
.event(value);
return writeActor.submit(writer::write).join();
}
public long writeCommand(
final int requestStreamId,
final long requestId,
final Intent intent,
final UnpackedObject value) {
final var writer =
streams
.newRecord(getLogName(partitionId))
.recordType(RecordType.COMMAND)
.requestId(requestId)
.requestStreamId(requestStreamId)
.intent(intent)
.event(value);
return writeActor.submit(writer::write).join();
}
public long writeCommandRejection(final Intent intent, final UnpackedObject value) {
final var writer =
streams
.newRecord(getLogName(partitionId))
.recordType(RecordType.COMMAND_REJECTION)
.intent(intent)
.event(value);
return writeActor.submit(writer::write).join();
}
public static String getLogName(final int partitionId) {
return STREAM_NAME + partitionId;
}
public MutableLastProcessedPositionState getLastProcessedPositionState() {
return lastProcessedPositionState;
}
/** Used to run writes within an actor thread. */
private static final class WriteActor extends Actor {
public ActorFuture<Long> submit(final Callable<Long> write) {
return actor.call(write);
}
}
@FunctionalInterface
public interface StreamProcessorTestFactory {
TypedRecordProcessors build(
TypedRecordProcessors builder, TypedRecordProcessorContext typedRecordProcessorContext);
}
}