2 * Copyright (c) 2015 Brocade Communications Systems, Inc. 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.cluster.raft.utils;
10 import akka.dispatch.Futures;
11 import akka.persistence.AtomicWrite;
12 import akka.persistence.PersistentImpl;
13 import akka.persistence.PersistentRepr;
14 import akka.persistence.journal.japi.AsyncWriteJournal;
15 import com.google.common.util.concurrent.Uninterruptibles;
16 import java.io.Serializable;
17 import java.util.ArrayList;
18 import java.util.Collections;
19 import java.util.Iterator;
20 import java.util.LinkedHashMap;
21 import java.util.List;
23 import java.util.Optional;
24 import java.util.concurrent.ConcurrentHashMap;
25 import java.util.concurrent.CountDownLatch;
26 import java.util.concurrent.TimeUnit;
27 import java.util.function.Consumer;
28 import org.apache.commons.lang.SerializationUtils;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import scala.concurrent.Future;
34 * An akka AsyncWriteJournal implementation that stores data in memory. This is intended for testing.
36 * @author Thomas Pantelis
38 public class InMemoryJournal extends AsyncWriteJournal {
40 private static class WriteMessagesComplete {
41 final CountDownLatch latch;
42 final Class<?> ofType;
44 WriteMessagesComplete(int count, Class<?> ofType) {
45 this.latch = new CountDownLatch(count);
50 static final Logger LOG = LoggerFactory.getLogger(InMemoryJournal.class);
52 private static final Map<String, Map<Long, Object>> JOURNALS = new ConcurrentHashMap<>();
54 private static final Map<String, CountDownLatch> DELETE_MESSAGES_COMPLETE_LATCHES = new ConcurrentHashMap<>();
56 private static final Map<String, WriteMessagesComplete> WRITE_MESSAGES_COMPLETE = new ConcurrentHashMap<>();
58 private static final Map<String, CountDownLatch> BLOCK_READ_MESSAGES_LATCHES = new ConcurrentHashMap<>();
60 private static Object deserialize(Object data) {
61 return data instanceof byte[] ? SerializationUtils.deserialize((byte[])data) : data;
64 public static void addEntry(String persistenceId, long sequenceNr, Object data) {
65 Map<Long, Object> journal = JOURNALS.computeIfAbsent(persistenceId, k -> new LinkedHashMap<>());
67 synchronized (journal) {
68 journal.put(sequenceNr, data instanceof Serializable
69 ? SerializationUtils.serialize((Serializable) data) : data);
73 public static void clear() {
75 DELETE_MESSAGES_COMPLETE_LATCHES.clear();
76 WRITE_MESSAGES_COMPLETE.clear();
77 BLOCK_READ_MESSAGES_LATCHES.clear();
80 @SuppressWarnings("unchecked")
81 public static <T> List<T> get(String persistenceId, Class<T> type) {
82 Map<Long, Object> journalMap = JOURNALS.get(persistenceId);
83 if (journalMap == null) {
84 return Collections.<T>emptyList();
87 synchronized (journalMap) {
88 List<T> journal = new ArrayList<>(journalMap.size());
89 for (Object entry: journalMap.values()) {
90 Object data = deserialize(entry);
91 if (type.isInstance(data)) {
92 journal.add((T) data);
100 public static Map<Long, Object> get(String persistenceId) {
101 Map<Long, Object> journalMap = JOURNALS.get(persistenceId);
102 return journalMap != null ? journalMap : Collections.<Long, Object>emptyMap();
105 public static void dumpJournal(String persistenceId) {
106 StringBuilder builder = new StringBuilder(String.format("Journal log for %s:", persistenceId));
107 Map<Long, Object> journalMap = JOURNALS.get(persistenceId);
108 if (journalMap != null) {
109 synchronized (journalMap) {
110 for (Map.Entry<Long, Object> e: journalMap.entrySet()) {
111 builder.append("\n ").append(e.getKey()).append(" = ").append(deserialize(e.getValue()));
116 LOG.info(builder.toString());
119 public static void waitForDeleteMessagesComplete(String persistenceId) {
120 if (!Uninterruptibles.awaitUninterruptibly(DELETE_MESSAGES_COMPLETE_LATCHES.get(persistenceId),
121 5, TimeUnit.SECONDS)) {
122 throw new AssertionError("Delete messages did not complete");
126 public static void waitForWriteMessagesComplete(String persistenceId) {
127 if (!Uninterruptibles.awaitUninterruptibly(WRITE_MESSAGES_COMPLETE.get(persistenceId).latch,
128 5, TimeUnit.SECONDS)) {
129 throw new AssertionError("Journal write messages did not complete");
133 public static void addDeleteMessagesCompleteLatch(String persistenceId) {
134 DELETE_MESSAGES_COMPLETE_LATCHES.put(persistenceId, new CountDownLatch(1));
137 public static void addWriteMessagesCompleteLatch(String persistenceId, int count) {
138 WRITE_MESSAGES_COMPLETE.put(persistenceId, new WriteMessagesComplete(count, null));
141 public static void addWriteMessagesCompleteLatch(String persistenceId, int count, Class<?> ofType) {
142 WRITE_MESSAGES_COMPLETE.put(persistenceId, new WriteMessagesComplete(count, ofType));
145 public static void addBlockReadMessagesLatch(String persistenceId, CountDownLatch latch) {
146 BLOCK_READ_MESSAGES_LATCHES.put(persistenceId, latch);
150 public Future<Void> doAsyncReplayMessages(final String persistenceId, final long fromSequenceNr,
151 final long toSequenceNr, final long max, final Consumer<PersistentRepr> replayCallback) {
152 LOG.trace("doAsyncReplayMessages for {}: fromSequenceNr: {}, toSequenceNr: {}", persistenceId,
153 fromSequenceNr,toSequenceNr);
154 return Futures.future(() -> {
155 CountDownLatch blockLatch = BLOCK_READ_MESSAGES_LATCHES.remove(persistenceId);
156 if (blockLatch != null) {
157 Uninterruptibles.awaitUninterruptibly(blockLatch);
160 Map<Long, Object> journal = JOURNALS.get(persistenceId);
161 if (journal == null) {
165 synchronized (journal) {
167 for (Map.Entry<Long,Object> entry : journal.entrySet()) {
168 if (++count <= max && entry.getKey() >= fromSequenceNr && entry.getKey() <= toSequenceNr) {
169 PersistentRepr persistentMessage =
170 new PersistentImpl(deserialize(entry.getValue()), entry.getKey(), persistenceId,
171 null, false, null, null);
172 replayCallback.accept(persistentMessage);
178 }, context().dispatcher());
182 public Future<Long> doAsyncReadHighestSequenceNr(String persistenceId, long fromSequenceNr) {
183 LOG.trace("doAsyncReadHighestSequenceNr for {}: fromSequenceNr: {}", persistenceId, fromSequenceNr);
185 // Akka calls this during recovery.
186 Map<Long, Object> journal = JOURNALS.get(persistenceId);
187 if (journal == null) {
188 return Futures.successful(fromSequenceNr);
191 synchronized (journal) {
193 for (Long seqNr : journal.keySet()) {
194 if (seqNr.longValue() >= fromSequenceNr && seqNr.longValue() > highest) {
195 highest = seqNr.longValue();
199 return Futures.successful(highest);
204 public Future<Iterable<Optional<Exception>>> doAsyncWriteMessages(final Iterable<AtomicWrite> messages) {
205 return Futures.future(() -> {
206 for (AtomicWrite write : messages) {
207 // Copy to array - workaround for eclipse "ambiguous method" errors for toIterator, toIterable etc
208 PersistentRepr[] array = new PersistentRepr[write.payload().size()];
209 write.payload().copyToArray(array);
210 for (PersistentRepr repr: array) {
211 LOG.trace("doAsyncWriteMessages: id: {}: seqNr: {}, payload: {}", repr.persistenceId(),
212 repr.sequenceNr(), repr.payload());
214 addEntry(repr.persistenceId(), repr.sequenceNr(), repr.payload());
216 WriteMessagesComplete complete = WRITE_MESSAGES_COMPLETE.get(repr.persistenceId());
217 if (complete != null) {
218 if (complete.ofType == null || complete.ofType.equals(repr.payload().getClass())) {
219 complete.latch.countDown();
225 return Collections.emptyList();
226 }, context().dispatcher());
230 public Future<Void> doAsyncDeleteMessagesTo(String persistenceId, long toSequenceNr) {
231 LOG.trace("doAsyncDeleteMessagesTo: {}", toSequenceNr);
232 Map<Long, Object> journal = JOURNALS.get(persistenceId);
233 if (journal != null) {
234 synchronized (journal) {
235 Iterator<Long> iter = journal.keySet().iterator();
236 while (iter.hasNext()) {
237 Long num = iter.next();
238 if (num <= toSequenceNr) {
245 CountDownLatch latch = DELETE_MESSAGES_COMPLETE_LATCHES.get(persistenceId);
250 return Futures.successful(null);