-
Notifications
You must be signed in to change notification settings - Fork 215
/
ThingUpdater.java
578 lines (509 loc) · 25.6 KB
/
ThingUpdater.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
/*
* 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.thingsearch.service.updater.actors;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.text.MessageFormat;
import java.time.Duration;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import javax.annotation.Nullable;
import org.bson.BsonDocument;
import org.eclipse.ditto.base.api.common.ShutdownReasonType;
import org.eclipse.ditto.base.model.acks.AcknowledgementRequest;
import org.eclipse.ditto.base.model.acks.DittoAcknowledgementLabel;
import org.eclipse.ditto.base.service.actors.ShutdownBehaviour;
import org.eclipse.ditto.base.service.config.supervision.ExponentialBackOff;
import org.eclipse.ditto.internal.models.streaming.IdentifiableStreamingMessage;
import org.eclipse.ditto.internal.utils.akka.logging.DittoDiagnosticLoggingAdapter;
import org.eclipse.ditto.internal.utils.akka.logging.DittoLogger;
import org.eclipse.ditto.internal.utils.akka.logging.DittoLoggerFactory;
import org.eclipse.ditto.internal.utils.akka.streaming.StreamAck;
import org.eclipse.ditto.internal.utils.metrics.DittoMetrics;
import org.eclipse.ditto.internal.utils.metrics.instruments.counter.Counter;
import org.eclipse.ditto.internal.utils.metrics.instruments.timer.StartedTimer;
import org.eclipse.ditto.internal.utils.tracing.DittoTracing;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.policies.api.PolicyReferenceTag;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.signals.events.ThingDeleted;
import org.eclipse.ditto.things.model.signals.events.ThingEvent;
import org.eclipse.ditto.thingsearch.api.UpdateReason;
import org.eclipse.ditto.thingsearch.api.commands.sudo.UpdateThing;
import org.eclipse.ditto.thingsearch.service.common.config.SearchConfig;
import org.eclipse.ditto.thingsearch.service.persistence.write.model.AbstractWriteModel;
import org.eclipse.ditto.thingsearch.service.persistence.write.model.Metadata;
import org.eclipse.ditto.thingsearch.service.persistence.write.model.ThingDeleteModel;
import org.eclipse.ditto.thingsearch.service.persistence.write.model.WriteResultAndErrors;
import org.eclipse.ditto.thingsearch.service.persistence.write.streaming.BulkWriteResultAckFlow;
import org.eclipse.ditto.thingsearch.service.persistence.write.streaming.ConsistencyLag;
import com.mongodb.client.model.DeleteOneModel;
import akka.Done;
import akka.NotUsed;
import akka.actor.AbstractFSMWithStash;
import akka.actor.ActorRef;
import akka.actor.FSM;
import akka.actor.Props;
import akka.cluster.pubsub.DistributedPubSubMediator;
import akka.japi.pf.FSMStateFunctionBuilder;
import akka.japi.pf.PFBuilder;
import akka.pattern.Patterns;
import akka.stream.KillSwitches;
import akka.stream.Materializer;
import akka.stream.UniqueKillSwitch;
import akka.stream.javadsl.Flow;
import akka.stream.javadsl.Keep;
import akka.stream.javadsl.Sink;
import akka.stream.javadsl.Source;
/**
* This Actor initiates persistence updates related to 1 thing.
*/
public final class ThingUpdater extends AbstractFSMWithStash<ThingUpdater.State, ThingUpdater.Data> {
private static final Counter INCORRECT_PATCH_UPDATE_COUNT =
DittoMetrics.counter("wildcard_search_incorrect_patch_updates");
private static final Counter UPDATE_FAILURE_COUNT = DittoMetrics.counter("wildcard_search_update_failures");
private static final Duration BLOCK_NAMESPACE_SHUTDOWN_DELAY = Duration.ofMinutes(2);
// alias Ditto Shutdown class because FSM shadows it
private static final Class<org.eclipse.ditto.base.api.common.Shutdown> SHUTDOWN_CLASS =
org.eclipse.ditto.base.api.common.Shutdown.class;
// logger for "trace" statements
private static final DittoLogger LOGGER = DittoLoggerFactory.getLogger(ThingUpdater.class);
private static final AcknowledgementRequest SEARCH_PERSISTED_REQUEST =
AcknowledgementRequest.of(DittoAcknowledgementLabel.SEARCH_PERSISTED);
private static final Duration THING_DELETION_TIMEOUT = Duration.ofMinutes(5);
private static final String FORCE_UPDATE = "force-update";
private final DittoDiagnosticLoggingAdapter log;
private final ThingId thingId;
private final Flow<Data, Result, NotUsed> flow;
private final Materializer materializer;
private final Duration writeInterval;
private ExponentialBackOff backOff;
private boolean shuttingDown = false;
@Nullable private UniqueKillSwitch killSwitch;
/**
* Data of the thing-updater.
*
* @param metadata The most up-to-date metadata known to this actor.
* @param lastWriteModel The last write model confirmed to be written to the persistence.
*/
public record Data(Metadata metadata, AbstractWriteModel lastWriteModel) {}
/**
* The result of a persistence operation.
*
* @param mongoWriteModel The write model describing the persistence operation.
* @param resultAndErrors The result of the persistence operation.
*/
public record Result(MongoWriteModel mongoWriteModel, WriteResultAndErrors resultAndErrors) {
public static Result fromError(final Metadata metadata, final Throwable error) {
final var mockWriteModel = MongoWriteModel.of(
ThingDeleteModel.of(metadata),
new DeleteOneModel<>(new BsonDocument()),
false);
return new Result(mockWriteModel, WriteResultAndErrors.failure(error));
}
}
enum State {
RECOVERING,
READY,
PERSISTING,
RETRYING
}
enum Control {
TICK
}
enum ShutdownTrigger {
DELETE,
IDLE,
NAMESPACE_BLOCKED
}
private ThingUpdater(final Flow<Data, Result, NotUsed> flow,
final Function<ThingId, Source<AbstractWriteModel, NotUsed>> recoveryFunction,
final SearchConfig config,
final ActorRef pubSubMediator) {
log = DittoLoggerFactory.getDiagnosticLoggingAdapter(this);
thingId = tryToGetThingId();
this.flow = flow;
materializer = Materializer.createMaterializer(getContext());
writeInterval = config.getUpdaterConfig().getStreamConfig().getWriteInterval();
backOff = ExponentialBackOff.initial(
config.getUpdaterConfig().getStreamConfig().getPersistenceConfig().getExponentialBackOffConfig());
startSingleTimer(ShutdownTrigger.IDLE.name(), ShutdownTrigger.IDLE, config.getUpdaterConfig().getMaxIdleTime());
startWith(State.RECOVERING, getInitialData(thingId));
when(State.RECOVERING, recovering());
when(State.READY, ready());
when(State.PERSISTING, persisting());
when(State.RETRYING, retrying());
onTransition(this::handleTransition);
whenUnhandled(unhandled());
initialize();
killSwitch = recoverLastWriteModel(thingId, recoveryFunction);
// subscribe for Shutdown commands
ShutdownBehaviour.fromId(thingId, pubSubMediator, getSelf());
}
/**
* Create props of this actor.
*
* @param flow Flow to perform persistence operations.
* @param recoveryFunction The function to recover the previous write model on start up.
* @param config Configuration of search service.
* @param pubSubMediator The pubsub mediator.
* @return The Props object.
*/
public static Props props(final Flow<Data, Result, NotUsed> flow,
final Function<ThingId, Source<AbstractWriteModel, NotUsed>> recoveryFunction,
final SearchConfig config,
final ActorRef pubSubMediator) {
return Props.create(ThingUpdater.class, flow, recoveryFunction, config, pubSubMediator);
}
@Override
public void postStop() {
if (killSwitch != null) {
killSwitch.shutdown();
}
switch (stateName()) {
case PERSISTING, RETRYING -> log.warning("Shut down during <{}>", stateName());
}
try {
super.postStop();
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
private FSMStateFunctionBuilder<State, Data> unhandled() {
return matchAnyEvent((message, data) -> {
log.warning("Unknown message in <{}>: <{}>", stateName(), message);
return stay();
});
}
private FSMStateFunctionBuilder<State, Data> recovering() {
return matchEvent(AbstractWriteModel.class, this::recoveryComplete)
.event(Throwable.class, this::recoveryFailed)
.event(ShutdownTrigger.class, this::shutdown)
.event(SHUTDOWN_CLASS, this::shutdownNow)
.event(DistributedPubSubMediator.SubscribeAck.class, this::subscribeAck)
.anyEvent(this::stashAndStay);
}
private FSMStateFunctionBuilder<State, Data> retrying() {
return ready();
}
private FSMStateFunctionBuilder<State, Data> ready() {
return matchEvent(ThingEvent.class, this::onThingEvent)
.event(Metadata.class, this::onEventMetadata)
.event(PolicyReferenceTag.class, this::onPolicyReferenceTag)
.event(UpdateThing.class, this::updateThing)
.eventEquals(Control.TICK, this::tick)
.event(ShutdownTrigger.class, this::shutdown)
.event(SHUTDOWN_CLASS, this::shutdownNow)
.event(DistributedPubSubMediator.SubscribeAck.class, this::subscribeAck);
}
private FSMStateFunctionBuilder<State, Data> persisting() {
return matchEvent(Result.class, this::onResult)
.event(Done.class, this::onDone)
.event(ThingEvent.class, this::onEventWhenPersisting)
.event(ShutdownTrigger.class, this::shutdown)
.event(SHUTDOWN_CLASS, this::shutdownNow)
.event(DistributedPubSubMediator.SubscribeAck.class, this::subscribeAck)
.anyEvent(this::stashAndStay);
}
private FSM.State<State, Data> subscribeAck(final DistributedPubSubMediator.SubscribeAck trigger, final Data data) {
log.debug("Received <{}> in state <{}>", trigger, stateName());
return stay();
}
private FSM.State<State, Data> shutdownNow(final org.eclipse.ditto.base.api.common.Shutdown shutdown,
final Data data) {
log.info("Shutting down now due to <{}> during <{}>", shutdown, stateName());
data.metadata().sendWeakAck(getDescription(shutdown));
return stop();
}
private FSM.State<State, Data> shutdown(final Object trigger, final Data data) {
log.info("Shutting down due to <{}> during <{}>", trigger, stateName());
shuttingDown = true;
return switch (stateName()) {
case RECOVERING -> stop();
case READY, RETRYING -> {
getSelf().tell(Control.TICK, ActorRef.noSender());
yield stay();
}
default -> stay();
};
}
private void handleTransition(final State previousState, final State nextState) {
if (previousState != nextState) {
switch (nextState) {
case READY, RETRYING -> {
final Duration delay;
if (nextState == State.READY) {
delay = writeInterval;
} else {
backOff = backOff.calculateNextBackOff();
delay = backOff.getRestartDelay();
}
startTimerWithFixedDelay(Control.TICK.name(), Control.TICK, delay);
unstashAll();
}
default -> cancelTimer(Control.TICK.name());
}
}
}
private FSM.State<State, Data> onEventWhenPersisting(final ThingEvent<?> event, final Data data) {
computeEventMetadata(event, data).ifPresent(eventMetadata -> getSelf().tell(eventMetadata, getSelf()));
return stay();
}
private FSM.State<State, Data> onResult(final Result result, final Data data) {
killSwitch = null;
final var writeResultAndErrors = result.resultAndErrors();
final var pair = BulkWriteResultAckFlow.checkBulkWriteResult(writeResultAndErrors);
pair.second().forEach(log::info);
if (shuttingDown) {
log.info("Shutting down after completing persistence operation");
return stop();
} else if (result.resultAndErrors().isNamespaceBlockedException()) {
log.info("Disabling actor because namespace is blocked");
startSingleTimer(ShutdownTrigger.NAMESPACE_BLOCKED.name(), ShutdownTrigger.NAMESPACE_BLOCKED, BLOCK_NAMESPACE_SHUTDOWN_DELAY);
return goTo(State.RECOVERING).using(getInitialData(thingId));
}
// TODO: change to debug log
log.info("Got Result=<{}>", pair.first());
switch (pair.first()) {
case INCORRECT_PATCH -> INCORRECT_PATCH_UPDATE_COUNT.increment();
case UNACKNOWLEDGED, CONSISTENCY_ERROR, WRITE_ERROR -> UPDATE_FAILURE_COUNT.increment();
}
return switch (pair.first()) {
case UNACKNOWLEDGED, CONSISTENCY_ERROR, INCORRECT_PATCH, WRITE_ERROR -> {
final var metadata = data.metadata().export();
yield goTo(State.RETRYING).using(new Data(metadata, ThingDeleteModel.of(Metadata.ofDeleted(thingId))));
}
case OK -> {
final var writeModel = result.mongoWriteModel().getDitto();
final var nextMetadata = writeModel.getMetadata().export();
yield goTo(State.READY).using(new Data(nextMetadata, writeModel));
}
};
}
private FSM.State<State, Data> onDone(final Done done, final Data data) {
killSwitch = null;
// TODO: convert to debug
final var nextMetadata = data.metadata().export();
log.info("Update skipped: <{}>", nextMetadata);
return goTo(State.READY).using(new Data(nextMetadata, data.lastWriteModel()));
}
private FSM.State<State, Data> tick(final Control tick, final Data data) {
if (shouldPersist(data.metadata(), data.lastWriteModel().getMetadata())) {
// TODO: convert to debug log with condition
log.info("Persisting <{}>", data.metadata().export());
ConsistencyLag.startS2WaitForDemand(data.metadata());
final var pair = Source.single(data)
.viaMat(KillSwitches.single(), Keep.right())
.via(flow)
.<Object>map(result -> result)
.orElse(Source.single(Done.done()))
.toMat(Sink.head(), Keep.both())
.run(materializer);
killSwitch = pair.first();
final var resultFuture = pair.second().handle((result, error) -> {
if (error != null || result == null) {
final var errorToReport = error != null
? error
: new IllegalStateException("Got no persistence result");
return Result.fromError(data.metadata(), errorToReport);
} else {
return result;
}
});
Patterns.pipe(resultFuture, getContext().getDispatcher()).to(getSelf());
return goTo(State.PERSISTING);
} else if (shuttingDown) {
// shutting down during READY without pending updates
// TODO: convert to debug log with condition
log.info("Shutting down when requested to persist <{}>", data.metadata().export());
return stop();
} else {
// TODO: convert to debug log with condition
log.info("Decided not to persist <{}>", data.metadata().export());
return stay();
}
}
private boolean shouldPersist(final Metadata metadata, final Metadata lastMetadata) {
return !metadata.equals(lastMetadata.export()) || lastMetadata.getThingRevision() <= 0;
}
private FSM.State<State, Data> updateThing(final UpdateThing updateThing, final Data data) {
log.withCorrelationId(updateThing)
.info("Requested to update search index <{}> by <{}>", updateThing, getSender());
final AbstractWriteModel lastWriteModel;
if (updateThing.getDittoHeaders().containsKey(FORCE_UPDATE)) {
lastWriteModel = ThingDeleteModel.of(data.metadata());
} else {
lastWriteModel = data.lastWriteModel();
}
final Metadata metadata = data.metadata()
.invalidateCaches(updateThing.shouldInvalidateThing(), updateThing.shouldInvalidatePolicy())
.withUpdateReason(updateThing.getUpdateReason());
final Metadata nextMetadata =
updateThing.getDittoHeaders().getAcknowledgementRequests().contains(SEARCH_PERSISTED_REQUEST)
? metadata.withSender(getSender())
: metadata;
return stay().using(new Data(nextMetadata, lastWriteModel));
}
private FSM.State<State, Data> onPolicyReferenceTag(final PolicyReferenceTag policyReferenceTag, final Data data) {
final var thingRevision = data.metadata().getThingRevision();
final var policyId = data.metadata().getPolicyId().orElse(null);
final var policyRevision = data.metadata().getPolicyRevision().orElse(-1L);
if (log.isDebugEnabled()) {
log.debug("Received new Policy-Reference-Tag for thing <{}> with revision <{}>, policy-id <{}> and " +
"policy-revision <{}>: <{}>.",
thingId, thingRevision, policyId, policyRevision, policyReferenceTag.asIdentifierString());
} else {
log.info("Got policy update <{}> at revision <{}>. Previous known policy is <{}> at <{}>.",
policyReferenceTag.getPolicyTag().getEntityId(), policyReferenceTag.getPolicyTag().getRevision(),
policyId, policyRevision);
}
acknowledge(policyReferenceTag);
final var policyTag = policyReferenceTag.getPolicyTag();
final var policyIdOfTag = policyTag.getEntityId();
if (!Objects.equals(policyId, policyIdOfTag) || policyRevision < policyTag.getRevision()) {
final var newMetadata = Metadata.of(thingId, thingRevision, policyIdOfTag, policyTag.getRevision(), null)
.withUpdateReason(UpdateReason.POLICY_UPDATE)
.invalidateCaches(false, true);
return enqueue(newMetadata, data);
} else {
log.debug("Dropping <{}> because my policyId=<{}> and policyRevision=<{}>",
policyReferenceTag, policyId, policyRevision);
return stay();
}
}
private FSM.State<State, Data> onEventMetadata(final Metadata eventMetadata, final Data data) {
return enqueue(eventMetadata, data);
}
private FSM.State<State, Data> onThingEvent(final ThingEvent<?> thingEvent, final Data data) {
return computeEventMetadata(thingEvent, data).map(eventMetadata -> onEventMetadata(eventMetadata, data))
.orElseGet(this::stay);
}
private Optional<Metadata> computeEventMetadata(final ThingEvent<?> thingEvent, final Data data) {
final DittoDiagnosticLoggingAdapter l = log.withCorrelationId(thingEvent);
l.debug("Received new thing event for thing id <{}> with revision <{}>.", thingId, thingEvent.getRevision());
final boolean shouldAcknowledge =
thingEvent.getDittoHeaders().getAcknowledgementRequests().contains(SEARCH_PERSISTED_REQUEST);
// check if the revision is valid (thingEvent.revision = 1 + sequenceNumber)
if (thingEvent.getRevision() <= data.metadata().getThingRevision() && !shouldAcknowledge) {
l.debug("Dropped thing event for thing id <{}> with revision <{}> because it was older than or "
+ "equal to the current sequence number <{}> of the update actor.", thingId,
thingEvent.getRevision(), data.metadata().getThingRevision());
return Optional.empty();
}
l.debug("Applying thing event <{}>.", thingEvent);
if (thingEvent instanceof ThingDeleted) {
// will stop this actor after 5 minutes (finishing up updating the index):
// this time should be longer than the consistency lag, otherwise the actor will be stopped before the
// actual "delete" is applied to the search index:
getContext().setReceiveTimeout(THING_DELETION_TIMEOUT);
}
if (shouldAcknowledge) {
tickNow();
}
final StartedTimer timer = DittoMetrics.timer(ConsistencyLag.TIMER_NAME)
.tag(ConsistencyLag.TAG_SHOULD_ACK, Boolean.toString(shouldAcknowledge))
.onExpiration(startedTimer ->
l.warning("Timer measuring consistency lag timed out for event <{}>", thingEvent))
.start();
DittoTracing.wrapTimer(DittoTracing.extractTraceContext(thingEvent), timer);
ConsistencyLag.startS1InUpdater(timer);
final var metadata = exportMetadataWithSender(shouldAcknowledge, thingEvent, getSender(), timer, data)
.withUpdateReason(UpdateReason.THING_UPDATE);
return Optional.of(metadata);
}
private FSM.State<State, Data> enqueue(final Metadata newMetadata, final Data data) {
return stay().using(new Data(data.metadata().append(newMetadata), data.lastWriteModel()));
}
private FSM.State<State, Data> recoveryComplete(final AbstractWriteModel lastWriteModel,
final Data initialData) {
log.debug("Recovered: <{}>", lastWriteModel.getClass().getSimpleName());
LOGGER.trace("Recovered: <{}>", lastWriteModel);
killSwitch = null;
return goTo(State.READY).using(new Data(lastWriteModel.getMetadata(), lastWriteModel));
}
private FSM.State<State, Data> recoveryFailed(final Throwable error, final Data initialData) {
log.error(error, "Recovery failed");
return stop();
}
private FSM.State<State, Data> stashAndStay(final Object message, final Data initialData) {
stash();
return stay();
}
private ThingId tryToGetThingId() {
final Charset utf8 = StandardCharsets.UTF_8;
try {
final String actorName = self().path().name();
return ThingId.of(URLDecoder.decode(actorName, utf8.name()));
} catch (final UnsupportedEncodingException e) {
throw new IllegalStateException(MessageFormat.format("Charset <{0}> is unsupported!", utf8.name()), e);
}
}
private UniqueKillSwitch recoverLastWriteModel(final ThingId thingId,
final Function<ThingId, Source<AbstractWriteModel, NotUsed>> recoveryFunction) {
final var pair = recoveryFunction.apply(thingId)
.<Object>map(writeModel -> writeModel)
.recover(new PFBuilder<Throwable, Object>().matchAny(x -> x).build())
.viaMat(KillSwitches.single(), Keep.right())
.toMat(Sink.head(), Keep.both())
.run(materializer);
Patterns.pipe(pair.second(), getContext().getDispatcher()).to(getSelf());
return pair.first();
}
private void tickNow() {
getSelf().tell(Control.TICK, ActorRef.noSender());
}
private Metadata exportMetadataWithSender(final boolean shouldAcknowledge,
final ThingEvent<?> event,
final ActorRef sender,
final StartedTimer consistencyLagTimer,
final Data data) {
final long thingRevision = event.getRevision();
if (shouldAcknowledge) {
return Metadata.of(thingId, thingRevision, data.metadata().getPolicyId().orElse(null),
data.metadata().getPolicyRevision().orElse(null), List.of(event), consistencyLagTimer, sender);
} else {
return exportMetadata(event, thingRevision, consistencyLagTimer, data);
}
}
private Metadata exportMetadata(@Nullable final ThingEvent<?> event, final long thingRevision,
@Nullable final StartedTimer timer, final Data data) {
return Metadata.of(thingId, thingRevision, data.metadata().getPolicyId().orElse(null),
data.metadata().getPolicyRevision().orElse(null),
event == null ? List.of() : List.of(event), timer, null);
}
private void acknowledge(final IdentifiableStreamingMessage message) {
final ActorRef sender = getSender();
if (!getContext().system().deadLetters().equals(sender)) {
getSender().tell(StreamAck.success(message.asIdentifierString()), getSelf());
}
}
private static Data getInitialData(final ThingId thingId) {
final var deletedMetadata = Metadata.ofDeleted(thingId);
return new Data(deletedMetadata, ThingDeleteModel.of(deletedMetadata));
}
private static JsonValue getDescription(final org.eclipse.ditto.base.api.common.Shutdown shutdown) {
final var type = shutdown.getReason().getType();
if (type instanceof ShutdownReasonType.Known knownType) {
return JsonValue.of(switch (knownType) {
case PURGE_NAMESPACE -> "The namespace is being purged.";
case PURGE_ENTITIES -> "The entities are being purged.";
});
} else {
return JsonValue.of(type.toString());
}
}
}