/
AbstractPersistentActorWithTimersAndCleanup.java
173 lines (152 loc) · 7.7 KB
/
AbstractPersistentActorWithTimersAndCleanup.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
/*
* Copyright (c) 2019 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.services.utils.persistentactors;
import java.util.Optional;
import javax.annotation.Nullable;
import org.eclipse.ditto.model.base.entity.id.DefaultEntityId;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.services.utils.akka.LogUtil;
import org.eclipse.ditto.signals.commands.cleanup.CleanupPersistence;
import org.eclipse.ditto.signals.commands.cleanup.CleanupPersistenceResponse;
import akka.actor.ActorRef;
import akka.event.DiagnosticLoggingAdapter;
import akka.persistence.AbstractPersistentActorWithTimers;
import akka.persistence.DeleteMessagesFailure;
import akka.persistence.DeleteMessagesSuccess;
import akka.persistence.DeleteSnapshotsFailure;
import akka.persistence.DeleteSnapshotsSuccess;
import akka.persistence.JournalProtocol;
import akka.persistence.Protocol;
import akka.persistence.SnapshotProtocol;
import akka.persistence.SnapshotSelectionCriteria;
/**
* Extends {@code AbstractPersistentActorWithTimers} to provide functionality to handle the {@link CleanupPersistence} command by
* deleting all persisted snapshots and events up to the latest snapshot sequence number.
*/
public abstract class AbstractPersistentActorWithTimersAndCleanup extends AbstractPersistentActorWithTimers {
private static final int STALE_EVENTS_KEPT_AFTER_CLEANUP = 0;
protected final DiagnosticLoggingAdapter log;
@Nullable private ActorRef origin = null;
@Nullable private SnapshotProtocol.Response deleteSnapshotsResponse = null;
@Nullable private JournalProtocol.Response deleteMessagesResponse = null;
private long lastCleanupExecutedAtSequenceNumber = 0;
protected AbstractPersistentActorWithTimersAndCleanup() {
this.log = LogUtil.obtain(this);
}
@Override
public Receive createReceive() {
return receiveBuilder()
.match(CleanupPersistence.class, this::handleCleanupCommand)
.match(DeleteSnapshotsSuccess.class, this::handleDeleteSnapshotsResponse)
.match(DeleteSnapshotsFailure.class, this::handleDeleteSnapshotsResponse)
.match(DeleteMessagesSuccess.class, this::handleDeleteMessagesResponse)
.match(DeleteMessagesFailure.class, this::handleDeleteMessagesResponse)
.build();
}
/**
* Return the number of events to keep after a cleanup action.
* If this number is positive, the PID will not be removed from
* the set of current PIDs known to the persistence plugin.
*
* @return number of stale events to keep.
*/
protected long staleEventsKeptAfterCleanup() {
return STALE_EVENTS_KEPT_AFTER_CLEANUP;
}
private void handleCleanupCommand(final CleanupPersistence cleanupPersistence) {
log.debug("Received Cleanup command: {}", cleanupPersistence);
if (origin == null) {
// check if there is at least one snapshot to delete
final long latestSnapshotSequenceNumber = getLatestSnapshotSequenceNumber();
if (latestSnapshotSequenceNumber > lastCleanupExecutedAtSequenceNumber) {
startCleanup(latestSnapshotSequenceNumber);
} else {
log.debug("Snapshot revision did not change since last cleanup, nothing to delete.");
getSender().tell(
CleanupPersistenceResponse.success(DefaultEntityId.of(persistenceId()), DittoHeaders.empty()),
getSelf());
}
} else {
log.info("Another cleanup is already running, rejecting the new cleanup request.");
origin.tell(CleanupPersistenceResponse.failure(DefaultEntityId.of(persistenceId()), DittoHeaders.empty()),
getSelf());
}
}
private void handleDeleteSnapshotsResponse(final SnapshotProtocol.Response deleteSnapshotsResponse) {
log.debug("Received response for DeleteSnapshots command: {}",
deleteSnapshotsResponse.getClass().getSimpleName());
this.deleteSnapshotsResponse = deleteSnapshotsResponse;
checkCleanupCompleted();
}
private void handleDeleteMessagesResponse(final JournalProtocol.Response deleteMessagesResponse) {
log.debug("Received response for DeleteMessages command: {}",
deleteMessagesResponse.getClass().getSimpleName());
this.deleteMessagesResponse = deleteMessagesResponse;
checkCleanupCompleted();
}
private void checkCleanupCompleted() {
if (deleteSnapshotsResponse != null && deleteMessagesResponse != null) {
if (isCleanupCompletedSuccessfully()) {
log.info("Cleanup for '{}' completed.", persistenceId());
Optional.ofNullable(origin)
.ifPresent(o -> o.tell(CleanupPersistenceResponse.success(DefaultEntityId.of(persistenceId()),
DittoHeaders.empty()), getSelf()));
} else {
log.info("Cleanup for '{}' failed. Snapshots: {}. Messages: {}.", persistenceId(),
getResponseStatus(deleteSnapshotsResponse), getResponseStatus(deleteMessagesResponse));
Optional.ofNullable(origin)
.ifPresent(o -> o.tell(CleanupPersistenceResponse.failure(DefaultEntityId.of(persistenceId()),
DittoHeaders.empty()), getSelf()));
}
finishCleanup();
}
}
private boolean isCleanupCompletedSuccessfully() {
return deleteSnapshotsResponse instanceof DeleteSnapshotsSuccess &&
deleteMessagesResponse instanceof DeleteMessagesSuccess;
}
private String getResponseStatus(@Nullable final Protocol.Message message) {
if (message == null) {
return "no response received";
} else if (message instanceof DeleteSnapshotsFailure) {
return String.format("%s (%s)", message.getClass().getSimpleName(),
((DeleteSnapshotsFailure) message).cause().getMessage());
} else if (message instanceof DeleteMessagesFailure) {
return String.format("%s (%s)", message.getClass().getSimpleName(),
((DeleteMessagesFailure) message).cause().getMessage());
} else {
return message.getClass().getSimpleName();
}
}
private void startCleanup(final long latestSnapshotSequenceNumber) {
origin = getSender();
final long maxSnapSeqNoToDelete = latestSnapshotSequenceNumber - 1;
final long maxEventSeqNoToDelete = latestSnapshotSequenceNumber - staleEventsKeptAfterCleanup();
log.info("Starting cleanup for '{}', deleting snapshots to sequence number {} and events to {}.",
persistenceId(), maxSnapSeqNoToDelete, maxEventSeqNoToDelete);
final SnapshotSelectionCriteria deletionCriteria =
SnapshotSelectionCriteria.create(maxSnapSeqNoToDelete, Long.MAX_VALUE);
deleteMessages(maxEventSeqNoToDelete);
deleteSnapshots(deletionCriteria);
lastCleanupExecutedAtSequenceNumber = latestSnapshotSequenceNumber;
}
private void finishCleanup() {
origin = null;
deleteSnapshotsResponse = null;
deleteMessagesResponse = null;
}
/**
* @return the latest and greatest sequence number of a snapshot that was confirmed to be persisted
*/
protected abstract long getLatestSnapshotSequenceNumber();
}