-
Notifications
You must be signed in to change notification settings - Fork 214
/
PolicyAnnouncementAcknowledgementAggregatorActor.java
145 lines (128 loc) · 6.96 KB
/
PolicyAnnouncementAcknowledgementAggregatorActor.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
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.policies.service.persistence.actors.announcements;
import java.time.Duration;
import java.util.function.Consumer;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.base.model.headers.DittoHeaderDefinition;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.translator.HeaderTranslator;
import org.eclipse.ditto.base.model.signals.acks.Acknowledgement;
import org.eclipse.ditto.base.model.signals.acks.Acknowledgements;
import org.eclipse.ditto.base.model.signals.commands.exceptions.CommandTimeoutException;
import org.eclipse.ditto.base.service.acknowledgements.AcknowledgementAggregator;
import org.eclipse.ditto.internal.utils.pekko.logging.DittoDiagnosticLoggingAdapter;
import org.eclipse.ditto.internal.utils.pekko.logging.DittoLoggerFactory;
import org.eclipse.ditto.policies.model.PolicyId;
import org.eclipse.ditto.policies.model.signals.announcements.PolicyAnnouncement;
import org.apache.pekko.actor.AbstractActorWithTimers;
import org.apache.pekko.actor.Props;
/**
* Aggregates acknowledgements for a published {@link PolicyAnnouncement}
* When all acknowledgements are completed the responseConsumer will be called with an {@link Acknowledgements} in case
* there are multiple acknowledgements requested or with an {@link Acknowledgement} in case just a single acknowledgement
* was requested.
* If an error happens the responseConsumer will be called with a {@link DittoRuntimeException}.
*
* @since 3.0.0
*/
final class PolicyAnnouncementAcknowledgementAggregatorActor extends AbstractActorWithTimers {
private final DittoDiagnosticLoggingAdapter log = DittoLoggerFactory.getDiagnosticLoggingAdapter(this);
private final String correlationId;
private final Duration timeout;
private final PolicyAnnouncement<?> originatingSignal;
private final AcknowledgementAggregator aggregator;
private final Consumer<Object> responseSignalConsumer;
private PolicyAnnouncementAcknowledgementAggregatorActor(final PolicyAnnouncement<?> policyAnnouncement,
final Duration timeout,
final Consumer<Object> responseSignalConsumer) {
this.originatingSignal = policyAnnouncement;
this.timeout = timeout;
this.responseSignalConsumer = responseSignalConsumer;
final var dittoHeaders = originatingSignal.getDittoHeaders();
this.correlationId = dittoHeaders.getCorrelationId()
.orElseGet(() -> self().path().name());
timers().startSingleTimer(Control.WAITING_FOR_ACKS_TIMED_OUT, Control.WAITING_FOR_ACKS_TIMED_OUT, timeout);
final var acknowledgementRequests = dittoHeaders.getAcknowledgementRequests();
aggregator = AcknowledgementAggregator.getInstance(policyAnnouncement.getEntityId(), correlationId, timeout,
HeaderTranslator.empty());
aggregator.addAcknowledgementRequests(acknowledgementRequests);
log.withCorrelationId(correlationId)
.info("Starting to wait for all requested acknowledgements <{}> for a maximum duration of <{}>.",
acknowledgementRequests, timeout);
}
static Props props(final PolicyAnnouncement<?> policyAnnouncement,
final Duration timeout,
final Consumer<Object> responseSignalConsumer) {
return Props.create(PolicyAnnouncementAcknowledgementAggregatorActor.class, policyAnnouncement, timeout,
responseSignalConsumer);
}
@Override
public Receive createReceive() {
return receiveBuilder()
.match(Acknowledgement.class, this::handleAcknowledgement)
.match(Acknowledgements.class, this::handleAcknowledgements)
.match(DittoRuntimeException.class, this::handleDittoRuntimeException)
.matchEquals(Control.WAITING_FOR_ACKS_TIMED_OUT, this::handleReceiveTimeout)
.matchAny(m -> log.warning("Received unexpected message: <{}>", m))
.build();
}
private void handleReceiveTimeout(final Control receiveTimeout) {
log.withCorrelationId(correlationId).info("Timed out waiting for all requested acknowledgements, " +
"completing Acknowledgements with timeouts...");
final var aggregatedAcknowledgements =
aggregator.getAggregatedAcknowledgements(originatingSignal.getDittoHeaders());
final PolicyId entityId = originatingSignal.getEntityId();
final DittoHeaders headersWithEntityId = aggregatedAcknowledgements.getDittoHeaders()
.toBuilder()
.putHeader(DittoHeaderDefinition.ENTITY_ID.getKey(), entityId.getEntityType() + ":" + entityId)
.build();
responseSignalConsumer.accept(CommandTimeoutException.newBuilder(timeout)
.dittoHeaders(headersWithEntityId)
.build());
getContext().stop(getSelf());
}
private void handleAcknowledgement(final Acknowledgement acknowledgement) {
log.withCorrelationId(correlationId).debug("Received acknowledgement <{}>.", acknowledgement);
aggregator.addReceivedAcknowledgment(acknowledgement);
potentiallyCompleteAcknowledgements();
}
private void handleAcknowledgements(final Acknowledgements acknowledgements) {
log.withCorrelationId(correlationId).debug("Received acknowledgements <{}>.", acknowledgements);
acknowledgements.stream().forEach(aggregator::addReceivedAcknowledgment);
potentiallyCompleteAcknowledgements();
}
private void handleDittoRuntimeException(final DittoRuntimeException dittoRuntimeException) {
log.withCorrelationId(correlationId)
.info("Stopped waiting for acknowledgements because of ditto runtime exception <{}>.",
dittoRuntimeException);
responseSignalConsumer.accept(dittoRuntimeException);
getContext().stop(getSelf());
}
private void potentiallyCompleteAcknowledgements() {
if (aggregator.receivedAllRequestedAcknowledgements()) {
completeAcknowledgements();
}
}
private void completeAcknowledgements() {
final var aggregatedAcknowledgements =
aggregator.getAggregatedAcknowledgements(originatingSignal.getDittoHeaders());
log.withCorrelationId(originatingSignal)
.debug("Completing with collected acknowledgements: {}", aggregatedAcknowledgements);
responseSignalConsumer.accept(aggregatedAcknowledgements);
getContext().stop(getSelf());
}
private enum Control {
WAITING_FOR_ACKS_TIMED_OUT
}
}