JournalReader is not an Iterator
[controller.git] / opendaylight / md-sal / sal-akka-segmented-journal / src / main / java / org / opendaylight / controller / akka / segjournal / DataJournalV0.java
1 /*
2  * Copyright (c) 2019, 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.akka.segjournal;
9
10 import akka.actor.ActorSystem;
11 import akka.persistence.PersistentRepr;
12 import com.codahale.metrics.Histogram;
13 import com.google.common.base.VerifyException;
14 import io.atomix.storage.journal.JournalReader;
15 import io.atomix.storage.journal.JournalSerdes;
16 import io.atomix.storage.journal.JournalWriter;
17 import io.atomix.storage.journal.SegmentedJournal;
18 import io.atomix.storage.journal.StorageLevel;
19 import java.io.File;
20 import java.io.Serializable;
21 import java.util.List;
22 import org.opendaylight.controller.akka.segjournal.DataJournalEntry.FromPersistence;
23 import org.opendaylight.controller.akka.segjournal.DataJournalEntry.ToPersistence;
24 import org.opendaylight.controller.akka.segjournal.SegmentedJournalActor.ReplayMessages;
25 import org.opendaylight.controller.akka.segjournal.SegmentedJournalActor.WriteMessages;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import scala.jdk.javaapi.CollectionConverters;
29
30 /**
31  * Version 0 data journal, where every journal entry maps to exactly one segmented file entry.
32  */
33 final class DataJournalV0 extends DataJournal {
34     private static final Logger LOG = LoggerFactory.getLogger(DataJournalV0.class);
35
36     private final SegmentedJournal<DataJournalEntry> entries;
37
38     DataJournalV0(final String persistenceId, final Histogram messageSize, final ActorSystem system,
39             final StorageLevel storage, final File directory, final int maxEntrySize, final int maxSegmentSize) {
40         super(persistenceId, messageSize);
41         entries = SegmentedJournal.<DataJournalEntry>builder()
42                 .withStorageLevel(storage).withDirectory(directory).withName("data")
43                 .withNamespace(JournalSerdes.builder()
44                     .register(new DataJournalEntrySerdes(system), FromPersistence.class, ToPersistence.class)
45                     .build())
46                 .withMaxEntrySize(maxEntrySize).withMaxSegmentSize(maxSegmentSize)
47                 .build();
48     }
49
50     @Override
51     long lastWrittenSequenceNr() {
52         return entries.writer().getLastIndex();
53     }
54
55     @Override
56     void deleteTo(final long sequenceNr) {
57         entries.writer().commit(sequenceNr);
58     }
59
60     @Override
61     void compactTo(final long sequenceNr) {
62         entries.compact(sequenceNr + 1);
63     }
64
65     @Override
66     void close() {
67         entries.close();
68     }
69
70     @Override
71     @SuppressWarnings("checkstyle:illegalCatch")
72     void handleReplayMessages(final ReplayMessages message, final long fromSequenceNr) {
73         try (var reader = entries.openReader(fromSequenceNr)) {
74             handleReplayMessages(reader, message);
75         } catch (Exception e) {
76             LOG.warn("{}: failed to replay messages for {}", persistenceId, message, e);
77             message.promise.failure(e);
78         } finally {
79             message.promise.success(null);
80         }
81     }
82
83     private void handleReplayMessages(final JournalReader<DataJournalEntry> reader, final ReplayMessages message) {
84         int count = 0;
85         while (count < message.max) {
86             final var next = reader.tryNext();
87             if (next == null || next.index() > message.toSequenceNr) {
88                 break;
89             }
90
91             LOG.trace("{}: replay {}", persistenceId, next);
92             updateLargestSize(next.size());
93             final var entry = next.entry();
94             if (entry instanceof FromPersistence fromPersistence) {
95                 final var repr = fromPersistence.toRepr(persistenceId, next.index());
96                 LOG.debug("{}: replaying {}", persistenceId, repr);
97                 message.replayCallback.accept(repr);
98                 count++;
99             } else {
100                 throw new VerifyException("Unexpected entry " + entry);
101             }
102         }
103         LOG.debug("{}: successfully replayed {} entries", persistenceId, count);
104     }
105
106     @Override
107     @SuppressWarnings("checkstyle:illegalCatch")
108     long handleWriteMessages(final WriteMessages message) {
109         final int count = message.size();
110         final var writer = entries.writer();
111         long bytes = 0;
112
113         for (int i = 0; i < count; ++i) {
114             final long mark = writer.getLastIndex();
115             final var request = message.getRequest(i);
116
117             final var reprs = CollectionConverters.asJava(request.payload());
118             LOG.trace("{}: append {}/{}: {} items at mark {}", persistenceId, i, count, reprs.size(), mark);
119             try {
120                 bytes += writePayload(writer, reprs);
121             } catch (Exception e) {
122                 LOG.warn("{}: failed to write out request {}/{} reverting to {}", persistenceId, i, count, mark, e);
123                 message.setFailure(i, e);
124                 writer.truncate(mark);
125                 continue;
126             }
127
128             message.setSuccess(i);
129         }
130         writer.flush();
131         return bytes;
132     }
133
134     private long writePayload(final JournalWriter<DataJournalEntry> writer, final List<PersistentRepr> reprs) {
135         long bytes = 0;
136         for (var repr : reprs) {
137             final Object payload = repr.payload();
138             if (!(payload instanceof Serializable)) {
139                 throw new UnsupportedOperationException("Non-serializable payload encountered "
140                         + payload.getClass());
141             }
142
143             LOG.trace("{}: starting append of {}", persistenceId, payload);
144             final var entry = writer.append(new ToPersistence(repr));
145             final int size = entry.size();
146             LOG.trace("{}: finished append of {} with {} bytes at {}", persistenceId, payload, size, entry.index());
147             recordMessageSize(size);
148             bytes += size;
149         }
150         return bytes;
151     }
152 }