-
Notifications
You must be signed in to change notification settings - Fork 215
/
ThingPersistenceActor.java
executable file
·242 lines (207 loc) · 9.3 KB
/
ThingPersistenceActor.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
/*
* 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.things.service.persistence.actors;
import org.eclipse.ditto.base.model.acks.DittoAcknowledgementLabel;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeExceptionBuilder;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.LiveChannelTimeoutStrategy;
import org.eclipse.ditto.base.model.headers.WithDittoHeaders;
import org.eclipse.ditto.base.model.json.JsonSchemaVersion;
import org.eclipse.ditto.base.model.signals.commands.Command;
import org.eclipse.ditto.internal.utils.config.DefaultScopedConfig;
import org.eclipse.ditto.internal.utils.config.ScopedConfig;
import org.eclipse.ditto.internal.utils.persistence.SnapshotAdapter;
import org.eclipse.ditto.internal.utils.persistence.mongo.config.ActivityCheckConfig;
import org.eclipse.ditto.internal.utils.persistence.mongo.config.SnapshotConfig;
import org.eclipse.ditto.internal.utils.persistentactors.AbstractPersistenceActor;
import org.eclipse.ditto.internal.utils.persistentactors.commands.CommandStrategy;
import org.eclipse.ditto.internal.utils.persistentactors.commands.DefaultContext;
import org.eclipse.ditto.internal.utils.persistentactors.events.EventStrategy;
import org.eclipse.ditto.internal.utils.pubsub.DistributedPub;
import org.eclipse.ditto.internal.utils.pubsub.extractors.AckExtractor;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.things.api.commands.sudo.SudoRetrieveThing;
import org.eclipse.ditto.things.model.Thing;
import org.eclipse.ditto.things.model.ThingBuilder;
import org.eclipse.ditto.things.model.ThingConstants;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.ThingLifecycle;
import org.eclipse.ditto.things.model.ThingsModelFactory;
import org.eclipse.ditto.things.model.signals.commands.exceptions.ThingNotAccessibleException;
import org.eclipse.ditto.things.model.signals.commands.modify.CreateThing;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveThing;
import org.eclipse.ditto.things.model.signals.commands.query.ThingQueryCommandResponse;
import org.eclipse.ditto.things.model.signals.events.ThingEvent;
import org.eclipse.ditto.things.service.common.config.DittoThingsConfig;
import org.eclipse.ditto.things.service.common.config.ThingConfig;
import org.eclipse.ditto.things.service.persistence.actors.strategies.commands.ThingCommandStrategies;
import org.eclipse.ditto.things.service.persistence.actors.strategies.events.ThingEventStrategies;
import com.typesafe.config.Config;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.japi.pf.ReceiveBuilder;
import akka.persistence.RecoveryCompleted;
/**
* PersistentActor which "knows" the state of a single {@link Thing}.
*/
public final class ThingPersistenceActor
extends AbstractPersistenceActor<Command<?>, Thing, ThingId, ThingId, ThingEvent<?>> {
/**
* The prefix of the persistenceId for Things.
*/
static final String PERSISTENCE_ID_PREFIX = ThingConstants.ENTITY_TYPE + ":";
/**
* The ID of the journal plugin this persistence actor uses.
*/
static final String JOURNAL_PLUGIN_ID = "akka-contrib-mongodb-persistence-things-journal";
/**
* The ID of the snapshot plugin this persistence actor uses.
*/
static final String SNAPSHOT_PLUGIN_ID = "akka-contrib-mongodb-persistence-things-snapshots";
private static final AckExtractor<ThingEvent<?>> ACK_EXTRACTOR =
AckExtractor.of(ThingEvent::getEntityId, ThingEvent::getDittoHeaders);
private final ThingConfig thingConfig;
private final DistributedPub<ThingEvent<?>> distributedPub;
@SuppressWarnings("unused")
private ThingPersistenceActor(final ThingId thingId,
final DistributedPub<ThingEvent<?>> distributedPub,
final ActorSystem actorSystem) {
super(thingId, SnapshotAdapter.get(actorSystem, getThingsRawConfig(actorSystem)));
final DittoThingsConfig thingsConfig = DittoThingsConfig.of(
DefaultScopedConfig.dittoScoped(getContext().getSystem().settings().config())
);
thingConfig = thingsConfig.getThingConfig();
this.distributedPub = distributedPub;
}
private static Config getThingsRawConfig(final ActorSystem actorSystem) {
return ScopedConfig.getOrEmpty(actorSystem.settings().config(), "ditto.things");
}
/**
* Creates Akka configuration object {@link Props} for this ThingPersistenceActor.
*
* @param thingId the Thing ID this Actor manages.
* @param distributedPub the distributed-pub access to publish thing events.
* @param actorSystem the actor-system.
* @return the Akka configuration Props object
*/
public static Props props(final ThingId thingId,
final DistributedPub<ThingEvent<?>> distributedPub,
final ActorSystem actorSystem) {
return Props.create(ThingPersistenceActor.class, () -> new ThingPersistenceActor(thingId, distributedPub,
actorSystem));
}
@Override
public void onQuery(final Command<?> command, final WithDittoHeaders response) {
if (response.getDittoHeaders().didLiveChannelConditionMatch()) {
final var liveChannelTimeoutStrategy = response.getDittoHeaders()
.getLiveChannelTimeoutStrategy()
.orElse(LiveChannelTimeoutStrategy.FAIL);
if (liveChannelTimeoutStrategy != LiveChannelTimeoutStrategy.USE_TWIN &&
response instanceof ThingQueryCommandResponse<?> queryResponse) {
super.onQuery(command, queryResponse.setEntity(JsonFactory.nullLiteral()));
return;
}
}
super.onQuery(command, response);
}
@Override
public String persistenceId() {
return entityId.getEntityType() + ":" + entityId;
}
@Override
public String journalPluginId() {
return JOURNAL_PLUGIN_ID;
}
@Override
public String snapshotPluginId() {
return SNAPSHOT_PLUGIN_ID;
}
@Override
protected Class<?> getEventClass() {
return ThingEvent.class;
}
@Override
protected CommandStrategy.Context<ThingId> getStrategyContext() {
return DefaultContext.getInstance(entityId, log);
}
@Override
protected ThingCommandStrategies getCreatedStrategy() {
return ThingCommandStrategies.getInstance(getContext().getSystem());
}
@Override
protected CommandStrategy<CreateThing, Thing, ThingId, ThingEvent<?>> getDeletedStrategy() {
return ThingCommandStrategies.getCreateThingStrategy(getContext().getSystem());
}
@Override
protected EventStrategy<ThingEvent<?>, Thing> getEventStrategy() {
return ThingEventStrategies.getInstance();
}
@Override
protected ActivityCheckConfig getActivityCheckConfig() {
return thingConfig.getActivityCheckConfig();
}
@Override
protected SnapshotConfig getSnapshotConfig() {
return thingConfig.getSnapshotConfig();
}
@Override
protected boolean entityExistsAsDeleted() {
return null != entity && entity.hasLifecycle(ThingLifecycle.DELETED);
}
@Override
protected Receive matchAnyWhenDeleted() {
return ReceiveBuilder.create()
.match(RetrieveThing.class, this::handleByCommandStrategy)
.match(SudoRetrieveThing.class, this::handleByCommandStrategy)
.build()
.orElse(super.matchAnyWhenDeleted());
}
@Override
protected DittoRuntimeExceptionBuilder<?> newNotAccessibleExceptionBuilder() {
return ThingNotAccessibleException.newBuilder(entityId);
}
@Override
protected void recoveryCompleted(final RecoveryCompleted event) {
if (entity != null) {
entity = enhanceThingWithLifecycle(entity);
log.info("Thing <{}> was recovered.", entityId);
}
super.recoveryCompleted(event);
}
@Override
protected void publishEvent(final ThingEvent<?> event) {
distributedPub.publishWithAcks(event, ACK_EXTRACTOR, getSelf());
}
@Override
protected boolean shouldSendResponse(final DittoHeaders dittoHeaders) {
return dittoHeaders.isResponseRequired() ||
dittoHeaders.getAcknowledgementRequests()
.stream()
.anyMatch(ar -> DittoAcknowledgementLabel.TWIN_PERSISTED.equals(ar.getLabel()));
}
@Override
protected boolean isEntityAlwaysAlive() {
return false;
}
@Override
protected JsonSchemaVersion getEntitySchemaVersion(final Thing entity) {
return entity.getImplementedSchemaVersion();
}
private static Thing enhanceThingWithLifecycle(final Thing thing) {
final ThingBuilder.FromCopy thingBuilder = ThingsModelFactory.newThingBuilder(thing);
if (thing.getLifecycle().isEmpty()) {
thingBuilder.setLifecycle(ThingLifecycle.ACTIVE);
}
return thingBuilder.build();
}
}