/
EventAndResponsePublisher.java
executable file
·182 lines (167 loc) · 8.23 KB
/
EventAndResponsePublisher.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
/*
* Copyright (c) 2017 Bosch Software Innovations GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/index.php
*
* Contributors:
* Bosch Software Innovations GmbH - initial contribution
*/
package org.eclipse.ditto.services.gateway.streaming.actors;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.eclipse.ditto.json.JsonField;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.model.base.exceptions.DittoRuntimeException;
import org.eclipse.ditto.model.base.json.Jsonifiable;
import org.eclipse.ditto.services.gateway.streaming.Connect;
import org.eclipse.ditto.services.utils.akka.LogUtil;
import org.eclipse.ditto.signals.base.Signal;
import org.eclipse.ditto.signals.commands.base.CommandResponse;
import org.eclipse.ditto.signals.events.base.Event;
import akka.actor.Props;
import akka.event.DiagnosticLoggingAdapter;
import akka.japi.Creator;
import akka.japi.pf.ReceiveBuilder;
import akka.stream.actor.AbstractActorPublisher;
import akka.stream.actor.ActorPublisherMessage;
import scala.concurrent.duration.FiniteDuration;
/**
* Actor publishing {@link Event}s and {@link CommandResponse}s which were sent to him applying backpressure if
* necessary.
*/
public final class EventAndResponsePublisher
extends AbstractActorPublisher<Jsonifiable.WithPredicate<JsonObject, JsonField>> {
private static final int MESSAGE_CONSUMPTION_CHECK_SECONDS = 2;
private final DiagnosticLoggingAdapter logger = LogUtil.obtain(this);
private final int backpressureBufferSize;
private final List<Jsonifiable.WithPredicate<JsonObject, JsonField>> buffer = new ArrayList<>();
private final AtomicBoolean currentlyInMessageConsumedCheck = new AtomicBoolean(false);
private String connectionCorrelationId;
private EventAndResponsePublisher(final int backpressureBufferSize) {
this.backpressureBufferSize = backpressureBufferSize;
}
/**
* Creates Akka configuration object Props for this EventAndResponsePublisher.
*
* @param backpressureBufferSize the max buffer size of how many outstanding CommandResponses and Events a single
* consumer may have - additionally incoming CommandResponses and Events are dropped if this size is reached.
* @return the Akka configuration Props object.
*/
public static Props props(final int backpressureBufferSize) {
return Props.create(EventAndResponsePublisher.class, new Creator<EventAndResponsePublisher>() {
private static final long serialVersionUID = 1L;
@Override
public EventAndResponsePublisher create() {
return new EventAndResponsePublisher(backpressureBufferSize);
}
});
}
@Override
public Receive createReceive() {
// Initially, this Actor can only receive the Connect message:
return ReceiveBuilder.create()
.match(Connect.class, connect -> {
final String connectionCorrelationId = connect.getConnectionCorrelationId();
LogUtil.enhanceLogWithCorrelationId(logger, connectionCorrelationId);
logger.debug("Established new connection: {}", connectionCorrelationId);
getContext().become(connected(connectionCorrelationId));
})
.matchAny(any -> logger.warning("Got unknown message during init phase '{}'", any)).build();
}
private Receive connected(final String connectionCorrelationId) {
this.connectionCorrelationId = connectionCorrelationId;
return ReceiveBuilder.create()
.match(Signal.class, signal -> buffer.size() >= backpressureBufferSize, signal -> {
LogUtil.enhanceLogWithCorrelationId(logger, signal);
handleBackpressureFor((Signal<?>) signal);
})
.match(Signal.class, signal -> {
if (buffer.isEmpty() && totalDemand() > 0) {
onNext((Signal<?>) signal);
} else {
buffer.add((Signal<?>) signal);
deliverBuf();
}
})
.match(DittoRuntimeException.class, cre -> buffer.size() >= backpressureBufferSize, cre -> {
LogUtil.enhanceLogWithCorrelationId(logger, cre.getDittoHeaders().getCorrelationId());
handleBackpressureFor(cre);
})
.match(DittoRuntimeException.class, cre -> {
if (buffer.isEmpty() && totalDemand() > 0) {
onNext(cre);
} else {
buffer.add(cre);
deliverBuf();
}
})
.match(Jsonifiable.WithPredicate.class, signal -> buffer.size() >= backpressureBufferSize,
this::handleBackpressureFor)
.match(Jsonifiable.WithPredicate.class, jsonifiable -> {
if (buffer.isEmpty() && totalDemand() > 0) {
onNext(jsonifiable);
} else {
buffer.add(jsonifiable);
deliverBuf();
}
})
.match(ActorPublisherMessage.Request.class, request -> {
LogUtil.enhanceLogWithCorrelationId(logger, connectionCorrelationId);
logger.debug("Got new demand: {}", request);
deliverBuf();
})
.match(ActorPublisherMessage.Cancel.class, cancel -> getContext().stop(getSelf()))
.matchAny(any -> {
LogUtil.enhanceLogWithCorrelationId(logger, connectionCorrelationId);
logger.warning("Got unknown message during connected phase: '{}'", any);
})
.build();
}
private void handleBackpressureFor(final Jsonifiable.WithPredicate<JsonObject, JsonField> jsonifiable) {
if (currentlyInMessageConsumedCheck.compareAndSet(false, true)) {
logger.warning("Backpressure - buffer of '{}' outstanding Events/CommandResponses is full, dropping '{}'",
backpressureBufferSize, jsonifiable);
final long bufSize = buffer.size();
final ActorContext context = getContext();
context.system().scheduler().scheduleOnce(FiniteDuration.apply(MESSAGE_CONSUMPTION_CHECK_SECONDS,
TimeUnit.SECONDS), () -> {
if (bufSize == buffer.size()) {
logger.warning(
"Terminating Publisher - did not to consume anything in the last '{}' seconds, buffer "
+ "is still at '{}' outstanding messages", MESSAGE_CONSUMPTION_CHECK_SECONDS,
bufSize);
context.stop(getSelf());
} else {
currentlyInMessageConsumedCheck.set(false);
logger.info("Outstanding messages were consumed, Publisher is not terminated");
}
}, context.system().dispatcher());
}
}
private void deliverBuf() {
LogUtil.enhanceLogWithCorrelationId(logger, connectionCorrelationId);
while (totalDemand() > 0) {
/*
* totalDemand is a Long and could be larger than
* what buffer.splitAt can accept
*/
if (totalDemand() <= Integer.MAX_VALUE) {
final List<Jsonifiable.WithPredicate<JsonObject, JsonField>> took =
buffer.subList(0, Math.min(buffer.size(), (int) totalDemand()));
took.forEach(this::onNext);
buffer.removeAll(took);
break;
} else {
final List<Jsonifiable.WithPredicate<JsonObject, JsonField>> took =
buffer.subList(0, Math.min(buffer.size(), Integer.MAX_VALUE));
took.forEach(this::onNext);
buffer.removeAll(took);
}
}
}
}