-
Notifications
You must be signed in to change notification settings - Fork 215
/
DevOpsCommandsActor.java
630 lines (552 loc) · 30.1 KB
/
DevOpsCommandsActor.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
/*
* Copyright (c) 2017 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.base.service.devops;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.StreamSupport;
import javax.annotation.Nullable;
import org.eclipse.ditto.base.api.devops.LoggerConfig;
import org.eclipse.ditto.base.api.devops.LoggingFacade;
import org.eclipse.ditto.base.api.devops.signals.commands.AggregatedDevOpsCommandResponse;
import org.eclipse.ditto.base.api.devops.signals.commands.ChangeLogLevel;
import org.eclipse.ditto.base.api.devops.signals.commands.ChangeLogLevelResponse;
import org.eclipse.ditto.base.api.devops.signals.commands.DevOpsCommand;
import org.eclipse.ditto.base.api.devops.signals.commands.DevOpsCommandResponse;
import org.eclipse.ditto.base.api.devops.signals.commands.DevOpsErrorResponse;
import org.eclipse.ditto.base.api.devops.signals.commands.ExecutePiggybackCommand;
import org.eclipse.ditto.base.api.devops.signals.commands.ExecutePiggybackCommandResponse;
import org.eclipse.ditto.base.api.devops.signals.commands.RetrieveLoggerConfig;
import org.eclipse.ditto.base.api.devops.signals.commands.RetrieveLoggerConfigResponse;
import org.eclipse.ditto.base.model.common.HttpStatus;
import org.eclipse.ditto.base.model.exceptions.AskException;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.WithDittoHeaders;
import org.eclipse.ditto.base.model.json.Jsonifiable;
import org.eclipse.ditto.base.model.signals.JsonParsable;
import org.eclipse.ditto.base.model.signals.JsonTypeNotParsableException;
import org.eclipse.ditto.base.model.signals.commands.Command;
import org.eclipse.ditto.base.model.signals.commands.CommandResponse;
import org.eclipse.ditto.base.model.signals.commands.exceptions.GatewayInternalErrorException;
import org.eclipse.ditto.internal.utils.akka.actors.RetrieveConfigBehavior;
import org.eclipse.ditto.internal.utils.akka.logging.DittoDiagnosticLoggingAdapter;
import org.eclipse.ditto.internal.utils.akka.logging.DittoLoggerFactory;
import org.eclipse.ditto.internal.utils.akka.logging.ThreadSafeDittoLoggingAdapter;
import org.eclipse.ditto.internal.utils.cluster.DistPubSubAccess;
import org.eclipse.ditto.internal.utils.cluster.JsonValueSourceRef;
import org.eclipse.ditto.internal.utils.cluster.MappingStrategies;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonValue;
import com.typesafe.config.Config;
import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.PoisonPill;
import akka.actor.Props;
import akka.actor.ReceiveTimeout;
import akka.cluster.Cluster;
import akka.cluster.pubsub.DistributedPubSub;
import akka.cluster.pubsub.DistributedPubSubMediator;
import akka.event.DiagnosticLoggingAdapter;
import akka.japi.pf.ReceiveBuilder;
import akka.pattern.Patterns;
/**
* An actor to consume {@link org.eclipse.ditto.base.api.devops.signals.commands.DevOpsCommand}s and reply appropriately.
*/
public final class DevOpsCommandsActor extends AbstractActor implements RetrieveConfigBehavior {
/**
* The name of this Actor in the ActorSystem.
*/
public static final String ACTOR_NAME = "devOpsCommandsActor";
/**
* Ditto header to turn aggregation on and off.
*/
public static final String AGGREGATE_HEADER = "aggregate";
/**
* Name of the poison pill "command".
*/
private static final String POISON_PILL_NAME = "poison-pill";
private static final Duration DEFAULT_RECEIVE_TIMEOUT = Duration.ofMillis(10_000);
private static final String UNKNOWN_MESSAGE_TEMPLATE = "Unknown message: {}";
private static final String TOPIC_HEADER = "topic";
private static final String IS_GROUP_TOPIC_HEADER = "is-group-topic";
private final ThreadSafeDittoLoggingAdapter logger = DittoLoggerFactory.getThreadSafeDittoLoggingAdapter(this);
private final LoggingFacade loggingFacade;
private final String serviceName;
private final String instance;
private final ActorRef pubSubMediator;
private final MappingStrategies serviceMappingStrategy;
private final Cluster cluster;
@SuppressWarnings("unused")
private DevOpsCommandsActor(final LoggingFacade loggingFacade, final String serviceName, final String instance) {
this.loggingFacade = loggingFacade;
this.serviceName = serviceName;
this.instance = instance;
final var context = getContext();
pubSubMediator = DistributedPubSub.get(context.getSystem()).mediator();
serviceMappingStrategy = MappingStrategies.loadMappingStrategies(context.getSystem());
cluster = Cluster.get(context.getSystem());
context.actorOf(PubSubSubscriberActor.props(pubSubMediator, serviceName, instance,
RetrieveLoggerConfig.TYPE,
ChangeLogLevel.TYPE,
ExecutePiggybackCommand.TYPE),
"pubSubSubscriber");
}
/**
* Creates Akka configuration object Props for this Actor.
*
* @param loggingFacade a facade providing logging functionality.
* @param serviceName name of the microservice.
* @param instance instance number of the microservice instance.
* @return the Akka configuration Props object.
*/
public static Props props(final LoggingFacade loggingFacade, final String serviceName, final String instance) {
return Props.create(DevOpsCommandsActor.class, loggingFacade, serviceName, instance);
}
@Override
public Receive createReceive() {
return ReceiveBuilder.create()
.match(DevOpsCommand.class, this::handleInitialDevOpsCommand)
.match(DevOpsCommandViaPubSub.class, this::handleDevOpsCommandViaPubSub)
.build()
.orElse(retrieveConfigBehavior())
.orElse(matchAnyUnhandled());
}
private Receive matchAnyUnhandled() {
return ReceiveBuilder.create()
.matchAny(m -> {
logger.warning(UNKNOWN_MESSAGE_TEMPLATE, m);
unhandled(m);
}).build();
}
@Override
public Config getConfig() {
return getContext().getSystem().settings().config();
}
/**
* DevOps commands issued via the HTTP DevopsRoute are handled here on the (gateway) cluster node which got the HTTP
* request. The job now is to:
* <ul>
* <li>publish the command in the cluster (so that all services which should react on that command get it)</li>
* <li>start aggregation of responses</li>
* </ul>
*
* @param command the initial DevOpsCommand to handle
*/
private void handleInitialDevOpsCommand(final DevOpsCommand<?> command) {
final var responseCorrelationActorSupplier = getResponseCorrelationActorSupplier(command);
if (isExecutePiggybackCommandToPubSubMediator(command)) {
executeAsPiggybackCommandToPubSubMediator(command, responseCorrelationActorSupplier);
} else {
executeAsIndirectPiggybackCommandToPubSubMediator(command, responseCorrelationActorSupplier);
}
}
private int getExpectedResponses(final DevOpsCommand<?> command) {
if (command.getServiceName().isPresent() && command.getInstance().isPresent()) {
// response wanted from 1 instance only
return 1;
} else if (command.getServiceName().isEmpty()) {
// response wanted from every cluster member
return (int) StreamSupport.stream(cluster.state().getMembers().spliterator(), false).count();
} else {
// response wanted from all instances of a service
final var theServiceName = command.getServiceName().orElseThrow();
return (int) StreamSupport.stream(cluster.state().getMembers().spliterator(), false)
.filter(member -> member.getRoles().contains(theServiceName))
.count();
}
}
private Supplier<ActorRef> getResponseCorrelationActorSupplier(final DevOpsCommand<?> command) {
final int expectedResponses = getExpectedResponses(command);
final var context = getContext();
return () -> context.actorOf(
DevOpsCommandResponseCorrelationActor.props(getSender(), command, expectedResponses),
getCorrelationIdOrThrow(command));
}
private static String getCorrelationIdOrThrow(final WithDittoHeaders command) {
final var dittoHeaders = command.getDittoHeaders();
return dittoHeaders.getCorrelationId()
.orElseThrow(() -> new IllegalArgumentException("Missing correlation-id for DevOpsCommand!"));
}
private boolean isExecutePiggybackCommandToPubSubMediator(final DevOpsCommand<?> command) {
final boolean result;
if (ExecutePiggybackCommand.TYPE.equals(command.getType())) {
final var executePiggyback = (ExecutePiggybackCommand) command;
final var pubSubMediatorPath = pubSubMediator.path();
result = Objects.equals(executePiggyback.getTargetActorSelection(),
pubSubMediatorPath.toStringWithoutAddress());
} else {
result = false;
}
return result;
}
private void executeAsPiggybackCommandToPubSubMediator(final DevOpsCommand<?> command,
final Supplier<ActorRef> responseCorrelationActor) {
tryInterpretAsDirectPublication(command, publish -> {
logger.withCorrelationId(command)
.info("Publishing <{}> into cluster on topic <{}> with sendOneMessageToEachGroup=<{}>",
publish.msg().getClass().getCanonicalName(),
publish.topic(),
publish.sendOneMessageToEachGroup());
pubSubMediator.tell(publish, responseCorrelationActor.get());
}, devOpsErrorResponse -> {
logger.withCorrelationId(command).warning("Dropping publishing command <{}>. Reason: <{}>", command,
devOpsErrorResponse.getDittoRuntimeException());
getSender().tell(devOpsErrorResponse, getSelf());
});
}
private void executeAsIndirectPiggybackCommandToPubSubMediator(final DevOpsCommand<?> command,
final Supplier<ActorRef> responseCorrelationActor) {
final String topic;
final Optional<String> commandServiceNameOpt = command.getServiceName();
if (commandServiceNameOpt.isPresent()) {
final String commandServiceName = commandServiceNameOpt.get();
final String commandInstance = command.getInstance().orElse(null);
if (commandInstance != null) {
topic = command.getType() + ":" + commandServiceName + ":" + commandInstance;
} else {
topic = command.getType() + ":" + commandServiceName;
}
} else {
topic = command.getType();
}
final DistributedPubSubMediator.Publish msg;
if (isGroupTopic(command.getDittoHeaders())) {
msg = DistPubSubAccess.publishViaGroup(topic, command);
} else {
msg = DistPubSubAccess.publish(topic, command);
}
logger.withCorrelationId(command)
.info("Publishing DevOpsCommand <{}> into cluster on topic <{}> with " +
"sendOneMessageToEachGroup=<{}>", command.getType(), msg.topic(),
msg.sendOneMessageToEachGroup());
pubSubMediator.tell(msg, responseCorrelationActor.get());
}
private void tryInterpretAsDirectPublication(final DevOpsCommand<?> command,
final Consumer<DistributedPubSubMediator.Publish> onSuccess,
final Consumer<DevOpsErrorResponse> onError) {
if (command instanceof ExecutePiggybackCommand executePiggyback) {
final DittoHeaders dittoHeaders = executePiggyback.getDittoHeaders();
deserializePiggybackCommand(executePiggyback,
jsonifiable -> {
final Optional<String> topic =
Optional.ofNullable(dittoHeaders.get(TOPIC_HEADER))
.or(() -> executePiggyback.getPiggybackCommand()
.getValue(Command.JsonFields.TYPE));
if (topic.isPresent()) {
if (isGroupTopic(dittoHeaders)) {
onSuccess.accept(DistPubSubAccess.publishViaGroup(topic.get(), jsonifiable));
} else {
onSuccess.accept(DistPubSubAccess.publish(topic.get(), jsonifiable));
}
} else {
onError.accept(getErrorResponse(command));
}
},
dittoRuntimeException -> onError.accept(getErrorResponse(command, dittoRuntimeException.toJson())));
} else {
// this should not happen
final JsonObject error =
GatewayInternalErrorException.newBuilder().dittoHeaders(command.getDittoHeaders()).build().toJson();
onError.accept(getErrorResponse(command, error));
}
}
private static boolean isGroupTopic(final DittoHeaders dittoHeaders) {
final String isGroupTopicValue = dittoHeaders.get(IS_GROUP_TOPIC_HEADER);
return isGroupTopicValue != null && !"false".equalsIgnoreCase(isGroupTopicValue);
}
private void handleDevOpsCommandViaPubSub(final DevOpsCommandViaPubSub devOpsCommandViaPubSub) {
final DevOpsCommand<?> wrappedCommand = devOpsCommandViaPubSub.wrappedCommand;
if (wrappedCommand instanceof ChangeLogLevel changeLogLevel) {
handleChangeLogLevel(changeLogLevel);
} else if (wrappedCommand instanceof RetrieveLoggerConfig retrieveLoggerConfig) {
handleRetrieveLoggerConfig(retrieveLoggerConfig);
} else if (wrappedCommand instanceof ExecutePiggybackCommand executePiggybackCommand) {
handleExecutePiggyBack(executePiggybackCommand);
}
}
private void handleChangeLogLevel(final ChangeLogLevel command) {
final boolean isApplied = loggingFacade.setLogLevel(command.getLoggerConfig());
final ChangeLogLevelResponse changeLogLevelResponse =
ChangeLogLevelResponse.of(serviceName, instance, isApplied, command.getDittoHeaders());
getSender().tell(changeLogLevelResponse, getSelf());
}
private void handleRetrieveLoggerConfig(final RetrieveLoggerConfig command) {
final List<LoggerConfig> loggerConfigs;
if (command.isAllKnownLoggers()) {
loggerConfigs = loggingFacade.getLoggerConfig();
} else {
loggerConfigs = loggingFacade.getLoggerConfig(command.getSpecificLoggers());
}
final RetrieveLoggerConfigResponse retrieveLoggerConfigResponse =
RetrieveLoggerConfigResponse.of(serviceName, instance, loggerConfigs, command.getDittoHeaders());
getSender().tell(retrieveLoggerConfigResponse, getSelf());
}
private void handleExecutePiggyBack(final ExecutePiggybackCommand command) {
final ActorRef sender = getSender();
deserializePiggybackCommand(command,
jsonifiable -> {
logger.withCorrelationId(command)
.info("Received PiggybackCommand: <{}> - telling to: <{}>", jsonifiable,
command.getTargetActorSelection());
final Duration timeout = command.getDittoHeaders().getTimeout().orElse(DEFAULT_RECEIVE_TIMEOUT);
final var actorSelection = getContext().actorSelection(command.getTargetActorSelection());
Patterns.ask(actorSelection, jsonifiable, timeout).whenComplete((result, error) -> {
final DevOpsCommandResponse<?> devOpsCommandResponse;
if (result instanceof JsonValueSourceRef) {
// to be streamed to sender
sender.tell(result, getSelf());
return;
} else if (result instanceof CommandResponse<?> response) {
devOpsCommandResponse =
ExecutePiggybackCommandResponse.of(serviceName, instance, response.getHttpStatus(),
response.toJson(), response.getDittoHeaders());
} else if (result instanceof DittoRuntimeException exception) {
devOpsCommandResponse =
ExecutePiggybackCommandResponse.of(serviceName, instance, exception.getHttpStatus(),
exception.toJson(), exception.getDittoHeaders());
} else if (result instanceof Jsonifiable<?> response) {
devOpsCommandResponse =
ExecutePiggybackCommandResponse.of(serviceName, instance, HttpStatus.CONFLICT,
response.toJson(), DittoHeaders.empty());
} else if (result != null) {
devOpsCommandResponse =
ExecutePiggybackCommandResponse.of(serviceName, instance, HttpStatus.CONFLICT,
JsonValue.of(Objects.toString(result)), DittoHeaders.empty());
} else {
devOpsCommandResponse = getErrorResponse(command,
AskException.fromMessage(error != null ? error.getMessage() : "Unknown error occurred.",
DittoHeaders.empty()).toJson());
}
sender.tell(devOpsCommandResponse, getSelf());
});
},
dittoRuntimeException -> sender.tell(getErrorResponse(command, dittoRuntimeException.toJson()),
getSelf()));
}
private void deserializePiggybackCommand(final ExecutePiggybackCommand command,
final Consumer<Object> onSuccess, final Consumer<DittoRuntimeException> onError) {
final JsonObject piggybackCommandJson = command.getPiggybackCommand();
@Nullable final String piggybackCommandType = piggybackCommandJson.getValue(Command.JsonFields.TYPE)
.orElse(null);
if (POISON_PILL_NAME.equals(piggybackCommandType)) {
onSuccess.accept(PoisonPill.getInstance());
} else {
final Consumer<JsonParsable<Jsonifiable<?>>> action = mappingStrategy -> {
try {
onSuccess.accept(mappingStrategy.parse(piggybackCommandJson, command.getDittoHeaders()));
} catch (final DittoRuntimeException e) {
logger.withCorrelationId(command)
.warning("Got DittoRuntimeException while parsing PiggybackCommand <{}>: {}!",
piggybackCommandType, e);
onError.accept(e);
}
};
final Runnable emptyAction = () -> {
final String msgPattern =
"ExecutePiggybackCommand with PiggybackCommand <%s> cannot be executed by this"
+ " service as there is no mapping strategy for it!";
final String message = String.format(msgPattern, piggybackCommandType);
logger.withCorrelationId(command).warning(message);
onError.accept(JsonTypeNotParsableException.fromMessage(message, command.getDittoHeaders()));
};
serviceMappingStrategy.getMappingStrategy(piggybackCommandType).ifPresentOrElse(action, emptyAction);
}
}
private DevOpsErrorResponse getErrorResponse(final DevOpsCommand<?> command, final JsonObject error) {
final String responseServiceName = command.getServiceName().orElse(this.serviceName);
final String responseInstance = command.getInstance().map(String::valueOf).orElse(this.instance);
return DevOpsErrorResponse.of(responseServiceName, responseInstance, error, command.getDittoHeaders());
}
private DevOpsErrorResponse getErrorResponse(final DevOpsCommand<?> command) {
final JsonObject error = JsonFactory.newObjectBuilder()
.set(DittoRuntimeException.JsonFields.STATUS, HttpStatus.BAD_REQUEST.getCode())
.set(DittoRuntimeException.JsonFields.MESSAGE,
"No topic found for publishing. Did you set the <topic> header?")
.build();
return getErrorResponse(command, error);
}
/**
* Child actor handling the distributed pub/sub subscriptions of DevOpsCommands in the cluster.
*/
private static final class PubSubSubscriberActor extends AbstractActor {
private final DiagnosticLoggingAdapter log = DittoLoggerFactory.getDiagnosticLoggingAdapter(this);
@SuppressWarnings("unused")
private PubSubSubscriberActor(final ActorRef pubSubMediator, final String serviceName, final String instance,
final String... pubSubTopicsToSubscribeTo) {
Arrays.stream(pubSubTopicsToSubscribeTo).forEach(topic ->
subscribeToDevOpsTopic(pubSubMediator, topic, serviceName, instance));
}
/**
* @return the Akka configuration Props object.
*/
static Props props(final ActorRef pubSubMediator,
final String serviceName,
final String instance,
final String... pubSubTopicsToSubscribeTo) {
return Props.create(PubSubSubscriberActor.class, pubSubMediator, serviceName, instance,
pubSubTopicsToSubscribeTo);
}
private void subscribeToDevOpsTopic(final ActorRef pubSubMediator,
final String topic,
final String serviceName,
final String instance) {
pubSubMediator.tell(DistPubSubAccess.subscribe(topic, getSelf()), getSelf());
pubSubMediator.tell(DistPubSubAccess.subscribe(String.join(":", topic, serviceName), getSelf()), getSelf());
pubSubMediator.tell(
DistPubSubAccess.subscribeViaGroup(String.join(":", topic, serviceName), serviceName, getSelf()),
getSelf());
pubSubMediator.tell(DistPubSubAccess.subscribe(String.join(":", topic, serviceName, instance), getSelf()),
getSelf());
}
@Override
public Receive createReceive() {
return ReceiveBuilder.create()
.match(DevOpsCommand.class, command -> getContext().getParent().forward(
new DevOpsCommandViaPubSub(command), getContext()
))
.match(DistributedPubSubMediator.SubscribeAck.class, this::handleSubscribeAck)
.matchAny(m -> {
log.warning(UNKNOWN_MESSAGE_TEMPLATE, m);
unhandled(m);
}).build();
}
private void handleSubscribeAck(final DistributedPubSubMediator.SubscribeAck subscribeAck) {
log.info("Successfully subscribed to distributed pub/sub on topic <{}>.", subscribeAck.subscribe().topic());
}
}
private static final class DevOpsCommandViaPubSub {
private final DevOpsCommand<?> wrappedCommand;
private DevOpsCommandViaPubSub(final DevOpsCommand<?> wrappedCommand) {
this.wrappedCommand = wrappedCommand;
}
}
/**
* Child actor correlating the {@link org.eclipse.ditto.base.api.devops.signals.commands.DevOpsCommandResponse}s.
* Waits for and collects responses until time runs out, then forwards all collected responses to the sender of the
* original DevOps command.
*/
private static final class DevOpsCommandResponseCorrelationActor extends AbstractActor {
private static final boolean DEFAULT_AGGREGATE = true;
private final ActorRef devOpsCommandSender;
private final DevOpsCommand<?> devOpsCommand;
private final List<CommandResponse<?>> commandResponses = new ArrayList<>();
private final boolean aggregateResults;
private final int expectedResponses;
private final DittoDiagnosticLoggingAdapter logger;
@SuppressWarnings("unused")
private DevOpsCommandResponseCorrelationActor(final ActorRef devOpsCommandSender,
final DevOpsCommand<?> devOpsCommand,
final int expectedResponses) {
this.devOpsCommandSender = devOpsCommandSender;
this.devOpsCommand = devOpsCommand;
final var dittoHeaders = devOpsCommand.getDittoHeaders();
aggregateResults = isAggregateResults(dittoHeaders);
this.expectedResponses = expectedResponses;
logger = DittoLoggerFactory.getDiagnosticLoggingAdapter(this);
logger.setCorrelationId(dittoHeaders);
}
private static boolean isAggregateResults(final DittoHeaders dittoHeaders) {
var result = DEFAULT_AGGREGATE;
final var aggregateHeaderValue = dittoHeaders.get(AGGREGATE_HEADER);
if (null != aggregateHeaderValue) {
result = Boolean.parseBoolean(aggregateHeaderValue);
}
return result;
}
/**
* @return the Akka configuration Props object.
*/
static Props props(final ActorRef devOpsCommandSender, final DevOpsCommand<?> devOpsCommand,
final int expectedResponses) {
return Props.create(DevOpsCommandResponseCorrelationActor.class, devOpsCommandSender, devOpsCommand,
expectedResponses);
}
@Override
public Receive createReceive() {
return ReceiveBuilder.create()
.match(CommandResponse.class, this::handleCommandResponse)
.match(JsonValueSourceRef.class, this::handleJsonValueSourceRef)
.match(DittoRuntimeException.class, this::handleDittoRuntimeException)
.match(ReceiveTimeout.class, receiveTimeout -> {
logger.withCorrelationId(getSelf().path().name())
.info("Got ReceiveTimeout, answering with all aggregated DevOpsCommandResponses and " +
"stopping ourselves ...");
sendCommandResponsesAndStop();
})
.matchAny(m -> {
logger.warning(UNKNOWN_MESSAGE_TEMPLATE, m);
unhandled(m);
}).build();
}
private void handleCommandResponse(final CommandResponse<?> commandResponse) {
if (commandResponse instanceof DevOpsCommandResponse) {
logger.debug("Received DevOpsCommandResponse from service/instance <{}/{}>: {}",
((DevOpsCommandResponse<?>) commandResponse).getServiceName().orElse("?"),
((DevOpsCommandResponse<?>) commandResponse).getInstance().orElse("?"),
commandResponse.getType());
} else {
logger.debug("Received DevOpsCommandResponse from service/instance <?/?>: {}",
commandResponse.getType());
}
addCommandResponse(commandResponse);
}
private void addCommandResponse(final CommandResponse<?> commandResponse) {
commandResponses.add(commandResponse);
if (!aggregateResults || (expectedResponses >= 0 && commandResponses.size() >= expectedResponses)) {
logger.info("All expected responses arrived. Sent response immediately.");
sendCommandResponsesAndStop();
}
}
private void sendCommandResponsesAndStop() {
final AggregatedDevOpsCommandResponse response = AggregatedDevOpsCommandResponse.of(commandResponses,
DevOpsCommandResponse.TYPE_PREFIX + devOpsCommand.getName(),
commandResponses.isEmpty() ? HttpStatus.REQUEST_TIMEOUT : HttpStatus.OK,
devOpsCommand.getDittoHeaders(), aggregateResults);
devOpsCommandSender.tell(response, getSelf());
stopSelf();
}
private void stopSelf() {
final var context = getContext();
context.cancelReceiveTimeout();
context.stop(getSelf());
}
private void handleJsonValueSourceRef(final JsonValueSourceRef jsonValueSourceRef) {
logger.debug("Received {} from: <{}>", jsonValueSourceRef.getClass().getSimpleName(), getSender());
devOpsCommandSender.tell(jsonValueSourceRef, getSelf());
stopSelf();
}
private void handleDittoRuntimeException(final DittoRuntimeException dittoRuntimeException) {
logger.warning("Received DittoRuntimeException from <{}>: <{}>!", getSender(), dittoRuntimeException);
addCommandResponse(DevOpsErrorResponse.of(null, null, dittoRuntimeException.toJson(),
dittoRuntimeException.getDittoHeaders()));
}
@Override
public void preStart() throws Exception {
super.preStart();
final var context = getContext();
context.setReceiveTimeout(getReceiveTimeout());
}
private Duration getReceiveTimeout() {
final var dittoHeaders = devOpsCommand.getDittoHeaders();
final var timeout = dittoHeaders.getTimeout().orElse(DEFAULT_RECEIVE_TIMEOUT);
return timeout.compareTo(DEFAULT_RECEIVE_TIMEOUT) > 0 ? timeout : DEFAULT_RECEIVE_TIMEOUT;
}
}
}