2 * Copyright (c) 2019 Pantheon Technologies, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.akka.segjournal;
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
13 import akka.actor.AbstractActor;
14 import akka.actor.Props;
15 import akka.persistence.AtomicWrite;
16 import akka.persistence.PersistentRepr;
17 import com.codahale.metrics.Histogram;
18 import com.codahale.metrics.Meter;
19 import com.codahale.metrics.MetricRegistry;
20 import com.codahale.metrics.Timer;
21 import com.google.common.base.MoreObjects;
22 import io.atomix.storage.StorageLevel;
23 import io.atomix.storage.journal.Indexed;
24 import io.atomix.storage.journal.SegmentedJournal;
25 import io.atomix.storage.journal.SegmentedJournalWriter;
26 import io.atomix.utils.serializer.Namespace;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Optional;
31 import java.util.concurrent.TimeUnit;
32 import java.util.function.Consumer;
33 import org.opendaylight.controller.cluster.common.actor.MeteringBehavior;
34 import org.opendaylight.controller.cluster.reporting.MetricsReporter;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import scala.concurrent.Future;
38 import scala.concurrent.Promise;
41 * This actor handles a single PersistentActor's journal. The journal is split into two {@link SegmentedJournal}s:
43 * <li>A memory-mapped data journal, containing actual data entries</li>
44 * <li>A simple file journal, containing sequence numbers of last deleted entry</li>
48 * This is a conscious design decision to minimize the amount of data that is being stored in the data journal while
49 * speeding up normal operations. Since the SegmentedJournal is an append-only linear log and Akka requires the ability
50 * to delete persistence entries, we need ability to mark a subset of a SegmentedJournal as deleted. While we could
51 * treat such delete requests as normal events, this leads to a mismatch between SegmentedJournal indices (as exposed by
52 * {@link Indexed}) and Akka sequence numbers -- requiring us to potentially perform costly deserialization to find the
53 * index corresponding to a particular sequence number, or maintain moderately-complex logic and data structures to
54 * perform that mapping in sub-linear time complexity.
57 * Split-file approach allows us to treat sequence numbers and indices as equivalent, without maintaining any explicit
58 * mapping information. The only additional information we need to maintain is the last deleted sequence number.
60 * @author Robert Varga
62 final class SegmentedJournalActor extends AbstractActor {
63 abstract static class AsyncMessage<T> {
64 final Promise<T> promise = Promise.apply();
67 private static final class ReadHighestSequenceNr extends AsyncMessage<Long> {
68 private final long fromSequenceNr;
70 ReadHighestSequenceNr(final long fromSequenceNr) {
71 this.fromSequenceNr = fromSequenceNr;
75 public String toString() {
76 return MoreObjects.toStringHelper(this).add("fromSequenceNr", fromSequenceNr).toString();
80 static final class ReplayMessages extends AsyncMessage<Void> {
81 private final long fromSequenceNr;
82 final long toSequenceNr;
84 final Consumer<PersistentRepr> replayCallback;
86 ReplayMessages(final long fromSequenceNr,
87 final long toSequenceNr, final long max, final Consumer<PersistentRepr> replayCallback) {
88 this.fromSequenceNr = fromSequenceNr;
89 this.toSequenceNr = toSequenceNr;
91 this.replayCallback = requireNonNull(replayCallback);
95 public String toString() {
96 return MoreObjects.toStringHelper(this).add("fromSequenceNr", fromSequenceNr)
97 .add("toSequenceNr", toSequenceNr).add("max", max).toString();
101 static final class WriteMessages {
102 private final List<AtomicWrite> requests = new ArrayList<>();
103 private final List<Promise<Optional<Exception>>> results = new ArrayList<>();
105 Future<Optional<Exception>> add(final AtomicWrite write) {
106 final Promise<Optional<Exception>> promise = Promise.apply();
108 results.add(promise);
109 return promise.future();
113 return requests.size();
116 AtomicWrite getRequest(final int index) {
117 return requests.get(index);
120 void setFailure(final int index, final Exception cause) {
121 results.get(index).success(Optional.of(cause));
125 void setSuccess(final int index) {
126 results.get(index).success(Optional.empty());
130 public String toString() {
131 return MoreObjects.toStringHelper(this).add("requests", requests).toString();
135 private static final class DeleteMessagesTo extends AsyncMessage<Void> {
136 final long toSequenceNr;
138 DeleteMessagesTo(final long toSequenceNr) {
139 this.toSequenceNr = toSequenceNr;
143 public String toString() {
144 return MoreObjects.toStringHelper(this).add("toSequenceNr", toSequenceNr).toString();
148 private static final Logger LOG = LoggerFactory.getLogger(SegmentedJournalActor.class);
149 private static final Namespace DELETE_NAMESPACE = Namespace.builder().register(Long.class).build();
150 private static final int DELETE_SEGMENT_SIZE = 64 * 1024;
152 private final String persistenceId;
153 private final StorageLevel storage;
154 private final int maxSegmentSize;
155 private final int maxEntrySize;
156 private final File directory;
158 // Tracks the time it took us to write a batch of messages
159 private Timer batchWriteTime;
160 // Tracks the number of individual messages written
161 private Meter messageWriteCount;
162 // Tracks the size distribution of messages
163 private Histogram messageSize;
165 private DataJournal dataJournal;
166 private SegmentedJournal<Long> deleteJournal;
167 private long lastDelete;
169 SegmentedJournalActor(final String persistenceId, final File directory, final StorageLevel storage,
170 final int maxEntrySize, final int maxSegmentSize) {
171 this.persistenceId = requireNonNull(persistenceId);
172 this.directory = requireNonNull(directory);
173 this.storage = requireNonNull(storage);
174 this.maxEntrySize = maxEntrySize;
175 this.maxSegmentSize = maxSegmentSize;
178 static Props props(final String persistenceId, final File directory, final StorageLevel storage,
179 final int maxEntrySize, final int maxSegmentSize) {
180 return Props.create(SegmentedJournalActor.class, requireNonNull(persistenceId), directory, storage,
181 maxEntrySize, maxSegmentSize);
185 public Receive createReceive() {
186 return receiveBuilder()
187 .match(DeleteMessagesTo.class, this::handleDeleteMessagesTo)
188 .match(ReadHighestSequenceNr.class, this::handleReadHighestSequenceNr)
189 .match(ReplayMessages.class, this::handleReplayMessages)
190 .match(WriteMessages.class, this::handleWriteMessages)
191 .matchAny(this::handleUnknown)
196 public void preStart() throws Exception {
197 LOG.debug("{}: actor starting", persistenceId);
200 final MetricRegistry registry = MetricsReporter.getInstance(MeteringBehavior.DOMAIN).getMetricsRegistry();
201 final String actorName = self().path().parent().toStringWithoutAddress() + '/' + directory.getName();
203 batchWriteTime = registry.timer(MetricRegistry.name(actorName, "batchWriteTime"));
204 messageWriteCount = registry.meter(MetricRegistry.name(actorName, "messageWriteCount"));
205 messageSize = registry.histogram(MetricRegistry.name(actorName, "messageSize"));
209 public void postStop() throws Exception {
210 LOG.debug("{}: actor stopping", persistenceId);
211 if (dataJournal != null) {
213 LOG.debug("{}: data journal closed", persistenceId);
216 if (deleteJournal != null) {
217 deleteJournal.close();
218 LOG.debug("{}: delete journal closed", persistenceId);
219 deleteJournal = null;
221 LOG.debug("{}: actor stopped", persistenceId);
225 static AsyncMessage<Void> deleteMessagesTo(final long toSequenceNr) {
226 return new DeleteMessagesTo(toSequenceNr);
229 static AsyncMessage<Long> readHighestSequenceNr(final long fromSequenceNr) {
230 return new ReadHighestSequenceNr(fromSequenceNr);
233 static AsyncMessage<Void> replayMessages(final long fromSequenceNr, final long toSequenceNr, final long max,
234 final Consumer<PersistentRepr> replayCallback) {
235 return new ReplayMessages(fromSequenceNr, toSequenceNr, max, replayCallback);
238 private void handleDeleteMessagesTo(final DeleteMessagesTo message) {
241 LOG.debug("{}: delete messages {}", persistenceId, message);
242 final long to = Long.min(dataJournal.lastWrittenSequenceNr(), message.toSequenceNr);
243 LOG.debug("{}: adjusted delete to {}", persistenceId, to);
245 if (lastDelete < to) {
246 LOG.debug("{}: deleting entries up to {}", persistenceId, to);
249 final SegmentedJournalWriter<Long> deleteWriter = deleteJournal.writer();
250 final Indexed<Long> entry = deleteWriter.append(lastDelete);
251 deleteWriter.commit(entry.index());
252 dataJournal.deleteTo(lastDelete);
254 LOG.debug("{}: compaction started", persistenceId);
255 dataJournal.compactTo(lastDelete);
256 deleteJournal.compact(entry.index());
257 LOG.debug("{}: compaction finished", persistenceId);
259 LOG.debug("{}: entries up to {} already deleted", persistenceId, lastDelete);
262 message.promise.success(null);
265 private void handleReadHighestSequenceNr(final ReadHighestSequenceNr message) {
266 LOG.debug("{}: looking for highest sequence on {}", persistenceId, message);
268 if (directory.isDirectory()) {
270 sequence = dataJournal.lastWrittenSequenceNr();
275 LOG.debug("{}: highest sequence is {}", message, sequence);
276 message.promise.success(sequence);
279 private void handleReplayMessages(final ReplayMessages message) {
280 LOG.debug("{}: replaying messages {}", persistenceId, message);
283 final long from = Long.max(lastDelete + 1, message.fromSequenceNr);
284 LOG.debug("{}: adjusted fromSequenceNr to {}", persistenceId, from);
286 dataJournal.handleReplayMessages(message, from);
289 private void handleWriteMessages(final WriteMessages message) {
292 final long startTicks = System.nanoTime();
293 final long start = dataJournal.lastWrittenSequenceNr();
295 dataJournal.handleWriteMessages(message);
297 batchWriteTime.update(System.nanoTime() - startTicks, TimeUnit.NANOSECONDS);
298 messageWriteCount.mark(dataJournal.lastWrittenSequenceNr() - start);
301 private void handleUnknown(final Object message) {
302 LOG.error("{}: Received unknown message {}", persistenceId, message);
305 private void ensureOpen() {
306 if (dataJournal != null) {
307 verifyNotNull(deleteJournal);
311 deleteJournal = SegmentedJournal.<Long>builder().withDirectory(directory).withName("delete")
312 .withNamespace(DELETE_NAMESPACE).withMaxSegmentSize(DELETE_SEGMENT_SIZE).build();
313 final Indexed<Long> lastEntry = deleteJournal.writer().getLastEntry();
314 lastDelete = lastEntry == null ? 0 : lastEntry.entry();
316 dataJournal = new DataJournalV0(persistenceId, messageSize, context().system(), storage, directory,
317 maxEntrySize, maxSegmentSize);
318 dataJournal.deleteTo(lastDelete);
319 LOG.debug("{}: journal open with last index {}, deleted to {}", persistenceId,
320 dataJournal.lastWrittenSequenceNr(), lastDelete);