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.collect.Maps;
16 import com.google.common.util.concurrent.Uninterruptibles;
17 import java.io.Serializable;
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.Iterator;
21 import java.util.List;
23 import java.util.Optional;
24 import java.util.concurrent.Callable;
25 import java.util.concurrent.ConcurrentHashMap;
26 import java.util.concurrent.CountDownLatch;
27 import java.util.concurrent.TimeUnit;
28 import java.util.function.Consumer;
29 import org.apache.commons.lang.SerializationUtils;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import scala.concurrent.Future;
35 * An akka AsyncWriteJournal implementation that stores data in memory. This is intended for testing.
37 * @author Thomas Pantelis
39 public class InMemoryJournal extends AsyncWriteJournal {
41 private static class WriteMessagesComplete {
42 final CountDownLatch latch;
43 final Class<?> ofType;
45 public WriteMessagesComplete(int count, Class<?> ofType) {
46 this.latch = new CountDownLatch(count);
51 static final Logger LOG = LoggerFactory.getLogger(InMemoryJournal.class);
53 private static final Map<String, Map<Long, Object>> journals = new ConcurrentHashMap<>();
55 private static final Map<String, CountDownLatch> deleteMessagesCompleteLatches = new ConcurrentHashMap<>();
57 private static final Map<String, WriteMessagesComplete> writeMessagesComplete = new ConcurrentHashMap<>();
59 private static final Map<String, CountDownLatch> blockReadMessagesLatches = new ConcurrentHashMap<>();
61 private static Object deserialize(Object data) {
62 return data instanceof byte[] ? SerializationUtils.deserialize((byte[])data) : data;
65 public static void addEntry(String persistenceId, long sequenceNr, Object data) {
66 Map<Long, Object> journal = journals.get(persistenceId);
68 journal = Maps.newLinkedHashMap();
69 journals.put(persistenceId, journal);
72 synchronized (journal) {
73 journal.put(sequenceNr, data instanceof Serializable ?
74 SerializationUtils.serialize((Serializable) data) : data);
78 public static void clear() {
82 @SuppressWarnings("unchecked")
83 public static <T> List<T> get(String persistenceId, Class<T> type) {
84 Map<Long, Object> journalMap = journals.get(persistenceId);
85 if(journalMap == null) {
86 return Collections.<T>emptyList();
89 synchronized (journalMap) {
90 List<T> journal = new ArrayList<>(journalMap.size());
91 for(Object entry: journalMap.values()) {
92 Object data = deserialize(entry);
93 if(type.isInstance(data)) {
94 journal.add((T) data);
102 public static Map<Long, Object> get(String persistenceId) {
103 Map<Long, Object> journalMap = journals.get(persistenceId);
104 return journalMap != null ? journalMap : Collections.<Long, Object>emptyMap();
107 public static void dumpJournal(String persistenceId) {
108 StringBuilder builder = new StringBuilder(String.format("Journal log for %s:", persistenceId));
109 Map<Long, Object> journalMap = journals.get(persistenceId);
110 if(journalMap != null) {
111 synchronized (journalMap) {
112 for(Map.Entry<Long, Object> e: journalMap.entrySet()) {
113 builder.append("\n ").append(e.getKey()).append(" = ").append(e.getValue());
118 LOG.info(builder.toString());
121 public static void waitForDeleteMessagesComplete(String persistenceId) {
122 if(!Uninterruptibles.awaitUninterruptibly(deleteMessagesCompleteLatches.get(persistenceId), 5, TimeUnit.SECONDS)) {
123 throw new AssertionError("Delete messages did not complete");
127 public static void waitForWriteMessagesComplete(String persistenceId) {
128 if(!Uninterruptibles.awaitUninterruptibly(writeMessagesComplete.get(persistenceId).latch, 5, TimeUnit.SECONDS)) {
129 throw new AssertionError("Journal write messages did not complete");
133 public static void addDeleteMessagesCompleteLatch(String persistenceId) {
134 deleteMessagesCompleteLatches.put(persistenceId, new CountDownLatch(1));
137 public static void addWriteMessagesCompleteLatch(String persistenceId, int count) {
138 writeMessagesComplete.put(persistenceId, new WriteMessagesComplete(count, null));
141 public static void addWriteMessagesCompleteLatch(String persistenceId, int count, Class<?> ofType) {
142 writeMessagesComplete.put(persistenceId, new WriteMessagesComplete(count, ofType));
145 public static void addBlockReadMessagesLatch(String persistenceId, CountDownLatch latch) {
146 blockReadMessagesLatches.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(new Callable<Void>() {
156 public Void call() throws Exception {
157 CountDownLatch blockLatch = blockReadMessagesLatches.remove(persistenceId);
158 if(blockLatch != null) {
159 Uninterruptibles.awaitUninterruptibly(blockLatch);
162 Map<Long, Object> journal = journals.get(persistenceId);
163 if (journal == null) {
167 synchronized (journal) {
169 for (Map.Entry<Long,Object> entry : journal.entrySet()) {
170 if (++count <= max && entry.getKey() >= fromSequenceNr && entry.getKey() <= toSequenceNr) {
171 PersistentRepr persistentMessage =
172 new PersistentImpl(deserialize(entry.getValue()), entry.getKey(), persistenceId,
173 null, false, null, null);
174 replayCallback.accept(persistentMessage);
181 }, context().dispatcher());
185 public Future<Long> doAsyncReadHighestSequenceNr(String persistenceId, long fromSequenceNr) {
186 LOG.trace("doAsyncReadHighestSequenceNr for {}: fromSequenceNr: {}", persistenceId, fromSequenceNr);
188 // Akka calls this during recovery.
189 Map<Long, Object> journal = journals.get(persistenceId);
190 if(journal == null) {
191 return Futures.successful(fromSequenceNr);
194 synchronized (journal) {
196 for (Long seqNr : journal.keySet()) {
197 if(seqNr.longValue() >= fromSequenceNr && seqNr.longValue() > highest) {
198 highest = seqNr.longValue();
202 return Futures.successful(highest);
207 public Future<Iterable<Optional<Exception>>> doAsyncWriteMessages(final Iterable<AtomicWrite> messages) {
208 return Futures.future(new Callable<Iterable<Optional<Exception>>>() {
210 public Iterable<Optional<Exception>> call() throws Exception {
211 for (AtomicWrite write : messages) {
212 // Copy to array - workaround for eclipse "ambiguous method" errors for toIterator, toIterable etc
213 PersistentRepr[] array = new PersistentRepr[write.payload().size()];
214 write.payload().copyToArray(array);
215 for(PersistentRepr repr: array) {
216 LOG.trace("doAsyncWriteMessages: id: {}: seqNr: {}, payload: {}", repr.persistenceId(),
217 repr.sequenceNr(), repr.payload());
219 addEntry(repr.persistenceId(), repr.sequenceNr(), repr.payload());
221 WriteMessagesComplete complete = writeMessagesComplete.get(repr.persistenceId());
222 if(complete != null) {
223 if(complete.ofType == null || complete.ofType.equals(repr.payload().getClass())) {
224 complete.latch.countDown();
230 return Collections.emptyList();
232 }, context().dispatcher());
236 public Future<Void> doAsyncDeleteMessagesTo(String persistenceId, long toSequenceNr) {
237 LOG.trace("doAsyncDeleteMessagesTo: {}", toSequenceNr);
238 Map<Long, Object> journal = journals.get(persistenceId);
239 if(journal != null) {
240 synchronized (journal) {
241 Iterator<Long> iter = journal.keySet().iterator();
242 while(iter.hasNext()) {
243 Long n = iter.next();
244 if(n <= toSequenceNr) {
251 CountDownLatch latch = deleteMessagesCompleteLatches.get(persistenceId);
256 return Futures.successful(null);