-
Notifications
You must be signed in to change notification settings - Fork 215
/
PolicyPersistenceActor.java
executable file
·289 lines (242 loc) · 10.7 KB
/
PolicyPersistenceActor.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
/*
* 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.policies.service.persistence.actors;
import java.util.Set;
import java.util.stream.StreamSupport;
import javax.annotation.Nullable;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeExceptionBuilder;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
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.cluster.DistPubSubAccess;
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.policies.api.PolicyTag;
import org.eclipse.ditto.policies.model.Policy;
import org.eclipse.ditto.policies.model.PolicyEntry;
import org.eclipse.ditto.policies.model.PolicyId;
import org.eclipse.ditto.policies.model.PolicyLifecycle;
import org.eclipse.ditto.policies.model.Subjects;
import org.eclipse.ditto.policies.model.signals.commands.exceptions.PolicyNotAccessibleException;
import org.eclipse.ditto.policies.model.signals.events.PolicyEvent;
import org.eclipse.ditto.policies.service.common.config.DittoPoliciesConfig;
import org.eclipse.ditto.policies.service.common.config.PolicyConfig;
import org.eclipse.ditto.policies.service.persistence.actors.strategies.commands.PolicyCommandStrategies;
import org.eclipse.ditto.policies.service.persistence.actors.strategies.events.PolicyEventStrategies;
import com.typesafe.config.Config;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.persistence.RecoveryCompleted;
/**
* PersistentActor which "knows" the state of a single {@link Policy}.
*/
public final class PolicyPersistenceActor
extends AbstractPersistenceActor<Command<?>, Policy, PolicyId, PolicyId, PolicyEvent<?>> {
/**
* The prefix of the persistenceId for Policies.
*/
public static final String PERSISTENCE_ID_PREFIX = "policy:";
/**
* The ID of the journal plugin this persistence actor uses.
*/
static final String JOURNAL_PLUGIN_ID = "akka-contrib-mongodb-persistence-policies-journal";
/**
* The ID of the snapshot plugin this persistence actor uses.
*/
static final String SNAPSHOT_PLUGIN_ID = "akka-contrib-mongodb-persistence-policies-snapshots";
private final ActorRef pubSubMediator;
private final PolicyConfig policyConfig;
private final ActorRef announcementManager;
@SuppressWarnings("unused")
private PolicyPersistenceActor(final PolicyId policyId,
final ActorRef pubSubMediator,
final ActorRef announcementManager,
final PolicyConfig policyConfig,
final ActorSystem actorSystem) {
super(policyId, SnapshotAdapter.get(actorSystem, getPoliciesRawConfig(actorSystem)));
this.pubSubMediator = pubSubMediator;
this.announcementManager = announcementManager;
this.policyConfig = policyConfig;
}
private static Config getPoliciesRawConfig(final ActorSystem actorSystem) {
return ScopedConfig.getOrEmpty(actorSystem.settings().config(), "ditto.policies");
}
private PolicyPersistenceActor(final PolicyId policyId,
final ActorRef pubSubMediator,
final ActorRef announcementManager,
final ActorSystem actorSystem) {
// not possible to call other constructor because "getContext()" is not available as argument of "this()"
super(policyId, SnapshotAdapter.get(actorSystem, getPoliciesRawConfig(actorSystem)));
this.pubSubMediator = pubSubMediator;
this.announcementManager = announcementManager;
final DittoPoliciesConfig policiesConfig = DittoPoliciesConfig.of(
DefaultScopedConfig.dittoScoped(getContext().getSystem().settings().config())
);
this.policyConfig = policiesConfig.getPolicyConfig();
}
/**
* Creates Akka configuration object {@link Props} for this PolicyPersistenceActor.
*
* @param policyId the ID of the Policy this Actor manages.
* @param pubSubMediator the PubSub mediator actor.
* @param announcementManager manager of policy announcements.
* @param policyConfig the policy config.
* @param actorSystem the actor-system.
* @return the Akka configuration Props object
*/
public static Props props(final PolicyId policyId,
final ActorRef pubSubMediator,
final ActorRef announcementManager,
final PolicyConfig policyConfig,
final ActorSystem actorSystem) {
return Props.create(PolicyPersistenceActor.class, () -> new PolicyPersistenceActor(policyId, pubSubMediator,
announcementManager, policyConfig, actorSystem));
}
static Props propsForTests(final PolicyId policyId,
final ActorRef pubSubMediator,
final ActorRef announcementManager,
final ActorSystem actorSystem) {
return Props.create(PolicyPersistenceActor.class, () -> new PolicyPersistenceActor(policyId, pubSubMediator,
announcementManager, actorSystem));
}
@Override
public String persistenceId() {
return PERSISTENCE_ID_PREFIX + entityId;
}
@Override
public String journalPluginId() {
return JOURNAL_PLUGIN_ID;
}
@Override
public String snapshotPluginId() {
return SNAPSHOT_PLUGIN_ID;
}
@Override
protected Class<?> getEventClass() {
return PolicyEvent.class;
}
@Override
protected CommandStrategy.Context<PolicyId> getStrategyContext() {
return DefaultContext.getInstance(entityId, log);
}
@Override
protected PolicyCommandStrategies getCreatedStrategy() {
return PolicyCommandStrategies.getInstance(policyConfig, getContext().getSystem());
}
@Override
protected CommandStrategy<? extends Command<?>, Policy, PolicyId, PolicyEvent<?>> getDeletedStrategy() {
return PolicyCommandStrategies.getCreatePolicyStrategy(policyConfig);
}
@Override
protected EventStrategy<PolicyEvent<?>, Policy> getEventStrategy() {
return PolicyEventStrategies.getInstance();
}
@Override
protected ActivityCheckConfig getActivityCheckConfig() {
return policyConfig.getActivityCheckConfig();
}
@Override
protected SnapshotConfig getSnapshotConfig() {
return policyConfig.getSnapshotConfig();
}
@Override
protected boolean entityExistsAsDeleted() {
return null != entity && entity.hasLifecycle(PolicyLifecycle.DELETED);
}
@Override
protected DittoRuntimeExceptionBuilder<?> newNotAccessibleExceptionBuilder() {
return PolicyNotAccessibleException.newBuilder(entityId);
}
@Override
protected void publishEvent(final PolicyEvent<?> event) {
pubSubMediator.tell(DistPubSubAccess.publishViaGroup(PolicyEvent.TYPE_PREFIX, event), getSelf());
final PolicyTag policyTag = PolicyTag.of(entityId, event.getRevision());
pubSubMediator.tell(DistPubSubAccess.publishViaGroup(PolicyTag.PUB_SUB_TOPIC_MODIFIED, policyTag), getSelf());
pubSubMediator.tell(DistPubSubAccess.publish(PolicyTag.PUB_SUB_TOPIC_INVALIDATE_ENFORCERS, policyTag),
getSelf());
}
@Override
public void onMutation(final Command<?> command, final PolicyEvent<?> event, final WithDittoHeaders response,
final boolean becomeCreated, final boolean becomeDeleted) {
persistAndApplyEvent(event, (persistedEvent, resultingEntity) -> {
if (shouldSendResponse(command.getDittoHeaders())) {
notifySender(getSender(), response);
}
if (becomeDeleted) {
becomeDeletedHandler();
}
if (becomeCreated) {
becomeCreatedHandler();
}
});
}
@Override
protected JsonSchemaVersion getEntitySchemaVersion(final Policy entity) {
return entity.getImplementedSchemaVersion();
}
@Override
protected boolean shouldSendResponse(final DittoHeaders dittoHeaders) {
return dittoHeaders.isResponseRequired();
}
@Override
protected boolean isEntityAlwaysAlive() {
return isAlwaysAlive(entity);
}
@Override
protected void recoveryCompleted(final RecoveryCompleted event) {
if (entity != null) {
announcementManager.tell(entity, ActorRef.noSender());
}
super.recoveryCompleted(event);
}
@Override
protected PolicyEvent<?> modifyEventBeforePersist(final PolicyEvent<?> event) {
final PolicyEvent<?> superEvent = super.modifyEventBeforePersist(event);
if (willEntityBeAlwaysAlive(event)) {
final DittoHeaders headersWithJournalTags = superEvent.getDittoHeaders()
.toBuilder()
.journalTags(Set.of(JOURNAL_TAG_ALWAYS_ALIVE))
.build();
return superEvent.setDittoHeaders(headersWithJournalTags);
}
return superEvent;
}
@Override
protected void onEntityModified() {
if (entity != null) {
announcementManager.tell(entity, ActorRef.noSender());
}
}
private boolean willEntityBeAlwaysAlive(final PolicyEvent<?> policyEvent) {
return isAlwaysAlive(getEventStrategy().handle(policyEvent, entity, getRevisionNumber()));
}
private boolean isAlwaysAlive(@Nullable final Policy policy) {
if (policy == null) {
return false;
} else {
return StreamSupport.stream(policy.spliterator(), false)
.map(PolicyEntry::getSubjects)
.flatMap(Subjects::stream)
.anyMatch(subject -> subject.getExpiry().isPresent());
}
}
}