-
Notifications
You must be signed in to change notification settings - Fork 556
/
LegacyTypedResponseWriterImpl.java
176 lines (157 loc) · 4.74 KB
/
LegacyTypedResponseWriterImpl.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
/*
* 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.processing.streamprocessor.writers;
import io.camunda.zeebe.engine.api.TypedRecord;
import io.camunda.zeebe.engine.processing.streamprocessor.sideeffect.SideEffectProducer;
import io.camunda.zeebe.msgpack.UnpackedObject;
import io.camunda.zeebe.protocol.record.RecordType;
import io.camunda.zeebe.protocol.record.RejectionType;
import io.camunda.zeebe.protocol.record.ValueType;
import io.camunda.zeebe.protocol.record.intent.Intent;
import java.nio.charset.StandardCharsets;
import org.agrona.DirectBuffer;
import org.agrona.concurrent.UnsafeBuffer;
public final class LegacyTypedResponseWriterImpl
implements LegacyTypedResponseWriter, SideEffectProducer {
private final CommandResponseWriter writer;
private final int partitionId;
private final UnsafeBuffer stringWrapper = new UnsafeBuffer(0, 0);
private long requestId;
private int requestStreamId;
private boolean isResponseStaged;
public LegacyTypedResponseWriterImpl(final CommandResponseWriter writer, final int partitionId) {
this.writer = writer;
this.partitionId = partitionId;
}
@Override
public void writeRejectionOnCommand(
final TypedRecord<?> command, final RejectionType type, final String reason) {
final byte[] bytes = reason.getBytes(StandardCharsets.UTF_8);
stringWrapper.wrap(bytes);
stage(
RecordType.COMMAND_REJECTION,
command.getIntent(),
command.getKey(),
type,
stringWrapper,
command.getValueType(),
command.getRequestId(),
command.getRequestStreamId(),
command.getValue());
}
@Override
public void writeEvent(final TypedRecord<?> event) {
stringWrapper.wrap(0, 0);
stage(
RecordType.EVENT,
event.getIntent(),
event.getKey(),
RejectionType.NULL_VAL,
stringWrapper,
event.getValueType(),
event.getRequestId(),
event.getRequestStreamId(),
event.getValue());
}
@Override
public void writeEventOnCommand(
final long eventKey,
final Intent eventState,
final UnpackedObject eventValue,
final TypedRecord<?> command) {
stringWrapper.wrap(0, 0);
stage(
RecordType.EVENT,
eventState,
eventKey,
RejectionType.NULL_VAL,
stringWrapper,
command.getValueType(),
command.getRequestId(),
command.getRequestStreamId(),
eventValue);
}
@Override
public void writeResponse(
final long eventKey,
final Intent eventState,
final UnpackedObject eventValue,
final ValueType valueType,
final long requestId,
final int requestStreamId) {
stringWrapper.wrap(0, 0);
stage(
RecordType.EVENT,
eventState,
eventKey,
RejectionType.NULL_VAL,
stringWrapper,
valueType,
requestId,
requestStreamId,
eventValue);
}
@Override
public void writeResponse(
final RecordType recordType,
final long key,
final Intent intent,
final UnpackedObject value,
final ValueType valueType,
final RejectionType rejectionType,
final String rejectionReason,
final long requestId,
final int requestStreamId) {
final byte[] bytes = rejectionReason.getBytes(StandardCharsets.UTF_8);
stringWrapper.wrap(bytes);
stage(
recordType,
intent,
key,
rejectionType,
stringWrapper,
valueType,
requestId,
requestStreamId,
value);
}
@Override
public boolean flush() {
if (isResponseStaged) {
writer.tryWriteResponse(requestStreamId, requestId);
}
return true;
}
@Override
public void reset() {
isResponseStaged = false;
}
private void stage(
final RecordType type,
final Intent intent,
final long key,
final RejectionType rejectionType,
final DirectBuffer rejectionReason,
final ValueType valueType,
final long requestId,
final int requestStreamId,
final UnpackedObject value) {
writer
.partitionId(partitionId)
.key(key)
.intent(intent)
.recordType(type)
.valueType(valueType)
.rejectionType(rejectionType)
.rejectionReason(rejectionReason)
.valueWriter(value);
this.requestId = requestId;
this.requestStreamId = requestStreamId;
isResponseStaged = true;
}
}