Fix followerDistributedDataStore tear down
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / utils / InMemoryJournal.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. 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.cluster.raft.utils;
9
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.LinkedHashMap;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Optional;
23 import java.util.concurrent.ConcurrentHashMap;
24 import java.util.concurrent.CountDownLatch;
25 import java.util.concurrent.TimeUnit;
26 import java.util.function.Consumer;
27 import org.apache.commons.lang3.SerializationUtils;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import scala.Option;
31 import scala.concurrent.Future;
32 import scala.jdk.javaapi.CollectionConverters;
33
34 /**
35  * An akka AsyncWriteJournal implementation that stores data in memory. This is intended for testing.
36  *
37  * @author Thomas Pantelis
38  */
39 public class InMemoryJournal extends AsyncWriteJournal {
40
41     private static class WriteMessagesComplete {
42         final CountDownLatch latch;
43         final Class<?> ofType;
44
45         WriteMessagesComplete(final int count, final Class<?> ofType) {
46             latch = new CountDownLatch(count);
47             this.ofType = ofType;
48         }
49     }
50
51     static final Logger LOG = LoggerFactory.getLogger(InMemoryJournal.class);
52
53     private static final Map<String, Map<Long, Object>> JOURNALS = new ConcurrentHashMap<>();
54
55     private static final Map<String, CountDownLatch> DELETE_MESSAGES_COMPLETE_LATCHES = new ConcurrentHashMap<>();
56
57     private static final Map<String, WriteMessagesComplete> WRITE_MESSAGES_COMPLETE = new ConcurrentHashMap<>();
58
59     private static final Map<String, CountDownLatch> BLOCK_READ_MESSAGES_LATCHES = new ConcurrentHashMap<>();
60
61     private static Object deserialize(final Object data) {
62         return data instanceof byte[] ? SerializationUtils.deserialize((byte[])data) : data;
63     }
64
65     public static void addEntry(final String persistenceId, final long sequenceNr, final Object data) {
66         Map<Long, Object> journal = JOURNALS.computeIfAbsent(persistenceId, k -> new LinkedHashMap<>());
67
68         synchronized (journal) {
69             journal.put(sequenceNr, data instanceof Serializable
70                     ? SerializationUtils.serialize((Serializable) data) : data);
71         }
72     }
73
74     public static void clear() {
75         JOURNALS.clear();
76         DELETE_MESSAGES_COMPLETE_LATCHES.clear();
77         WRITE_MESSAGES_COMPLETE.clear();
78         BLOCK_READ_MESSAGES_LATCHES.clear();
79     }
80
81     @SuppressWarnings("unchecked")
82     public static <T> List<T> get(final String persistenceId, final Class<T> type) {
83         Map<Long, Object> journalMap = JOURNALS.get(persistenceId);
84         if (journalMap == null) {
85             return Collections.<T>emptyList();
86         }
87
88         synchronized (journalMap) {
89             List<T> journal = new ArrayList<>(journalMap.size());
90             for (Object entry: journalMap.values()) {
91                 Object data = deserialize(entry);
92                 if (type.isInstance(data)) {
93                     journal.add((T) data);
94                 }
95             }
96
97             return journal;
98         }
99     }
100
101     public static Map<Long, Object> get(final String persistenceId) {
102         Map<Long, Object> journalMap = JOURNALS.get(persistenceId);
103         return journalMap != null ? journalMap : Collections.<Long, Object>emptyMap();
104     }
105
106     public static void dumpJournal(final String persistenceId) {
107         StringBuilder builder = new StringBuilder(String.format("Journal log for %s:", persistenceId));
108         Map<Long, Object> journalMap = JOURNALS.get(persistenceId);
109         if (journalMap != null) {
110             synchronized (journalMap) {
111                 for (Map.Entry<Long, Object> e: journalMap.entrySet()) {
112                     builder.append("\n    ").append(e.getKey()).append(" = ").append(deserialize(e.getValue()));
113                 }
114             }
115         }
116
117         LOG.info(builder.toString());
118     }
119
120     public static void waitForDeleteMessagesComplete(final String persistenceId) {
121         if (!Uninterruptibles.awaitUninterruptibly(DELETE_MESSAGES_COMPLETE_LATCHES.get(persistenceId),
122                 5, TimeUnit.SECONDS)) {
123             throw new AssertionError("Delete messages did not complete");
124         }
125     }
126
127     public static void waitForWriteMessagesComplete(final String persistenceId) {
128         if (!Uninterruptibles.awaitUninterruptibly(WRITE_MESSAGES_COMPLETE.get(persistenceId).latch,
129                 5, TimeUnit.SECONDS)) {
130             throw new AssertionError("Journal write messages did not complete");
131         }
132     }
133
134     public static void addDeleteMessagesCompleteLatch(final String persistenceId) {
135         DELETE_MESSAGES_COMPLETE_LATCHES.put(persistenceId, new CountDownLatch(1));
136     }
137
138     public static void addWriteMessagesCompleteLatch(final String persistenceId, final int count) {
139         WRITE_MESSAGES_COMPLETE.put(persistenceId, new WriteMessagesComplete(count, null));
140     }
141
142     public static void addWriteMessagesCompleteLatch(final String persistenceId, final int count,
143             final Class<?> ofType) {
144         WRITE_MESSAGES_COMPLETE.put(persistenceId, new WriteMessagesComplete(count, ofType));
145     }
146
147     public static void addBlockReadMessagesLatch(final String persistenceId, final CountDownLatch latch) {
148         BLOCK_READ_MESSAGES_LATCHES.put(persistenceId, latch);
149     }
150
151     @Override
152     public Future<Void> doAsyncReplayMessages(final String persistenceId, final long fromSequenceNr,
153             final long toSequenceNr, final long max, final Consumer<PersistentRepr> replayCallback) {
154         LOG.trace("doAsyncReplayMessages for {}: fromSequenceNr: {}, toSequenceNr: {}", persistenceId,
155                 fromSequenceNr,toSequenceNr);
156         return Futures.future(() -> {
157             CountDownLatch blockLatch = BLOCK_READ_MESSAGES_LATCHES.remove(persistenceId);
158             if (blockLatch != null) {
159                 Uninterruptibles.awaitUninterruptibly(blockLatch);
160             }
161
162             Map<Long, Object> journal = JOURNALS.get(persistenceId);
163             if (journal == null) {
164                 return null;
165             }
166
167             synchronized (journal) {
168                 int count = 0;
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, 0, Option.empty());
174                         replayCallback.accept(persistentMessage);
175                     }
176                 }
177             }
178
179             return null;
180         }, context().dispatcher());
181     }
182
183     @Override
184     public Future<Long> doAsyncReadHighestSequenceNr(final String persistenceId, final long fromSequenceNr) {
185         LOG.trace("doAsyncReadHighestSequenceNr for {}: fromSequenceNr: {}", persistenceId, fromSequenceNr);
186
187         // Akka calls this during recovery.
188         Map<Long, Object> journal = JOURNALS.get(persistenceId);
189         if (journal == null) {
190             return Futures.successful(fromSequenceNr);
191         }
192
193         synchronized (journal) {
194             long highest = -1;
195             for (Long seqNr : journal.keySet()) {
196                 if (seqNr.longValue() >= fromSequenceNr && seqNr.longValue() > highest) {
197                     highest = seqNr.longValue();
198                 }
199             }
200
201             return Futures.successful(highest);
202         }
203     }
204
205     @Override
206     public Future<Iterable<Optional<Exception>>> doAsyncWriteMessages(final Iterable<AtomicWrite> messages) {
207         return Futures.future(() -> {
208             for (AtomicWrite write : messages) {
209                 for (PersistentRepr repr : CollectionConverters.asJava(write.payload())) {
210                     LOG.trace("doAsyncWriteMessages: id: {}: seqNr: {}, payload: {}", repr.persistenceId(),
211                         repr.sequenceNr(), repr.payload());
212
213                     addEntry(repr.persistenceId(), repr.sequenceNr(), repr.payload());
214
215                     WriteMessagesComplete complete = WRITE_MESSAGES_COMPLETE.get(repr.persistenceId());
216                     if (complete != null) {
217                         if (complete.ofType == null || complete.ofType.equals(repr.payload().getClass())) {
218                             complete.latch.countDown();
219                         }
220                     }
221                 }
222             }
223
224             return Collections.emptyList();
225         }, context().dispatcher());
226     }
227
228     @Override
229     public Future<Void> doAsyncDeleteMessagesTo(final String persistenceId, final long toSequenceNr) {
230         LOG.trace("doAsyncDeleteMessagesTo: {}", toSequenceNr);
231         Map<Long, Object> journal = JOURNALS.get(persistenceId);
232         if (journal != null) {
233             synchronized (journal) {
234                 journal.keySet().removeIf(num -> num <= toSequenceNr);
235             }
236         }
237
238         CountDownLatch latch = DELETE_MESSAGES_COMPLETE_LATCHES.get(persistenceId);
239         if (latch != null) {
240             latch.countDown();
241         }
242
243         return Futures.successful(null);
244     }
245 }