2 * Copyright (c) 2014 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.datastore.utils;
10 import static org.junit.Assert.assertEquals;
11 import java.util.Collections;
12 import java.util.Iterator;
14 import java.util.concurrent.Callable;
15 import java.util.concurrent.ConcurrentHashMap;
16 import java.util.concurrent.CountDownLatch;
17 import java.util.concurrent.TimeUnit;
18 import com.google.common.collect.Maps;
19 import com.google.common.util.concurrent.Uninterruptibles;
20 import scala.concurrent.Future;
21 import akka.dispatch.Futures;
22 import akka.japi.Procedure;
23 import akka.persistence.PersistentConfirmation;
24 import akka.persistence.PersistentId;
25 import akka.persistence.PersistentImpl;
26 import akka.persistence.PersistentRepr;
27 import akka.persistence.journal.japi.AsyncWriteJournal;
29 public class InMemoryJournal extends AsyncWriteJournal {
31 private static final Map<String, Map<Long, Object>> journals = new ConcurrentHashMap<>();
33 private static final Map<String, CountDownLatch> deleteMessagesCompleteLatches = new ConcurrentHashMap<>();
35 public static void addEntry(String persistenceId, long sequenceNr, Object data) {
36 Map<Long, Object> journal = journals.get(persistenceId);
38 journal = Maps.newLinkedHashMap();
39 journals.put(persistenceId, journal);
42 synchronized (journal) {
43 journal.put(sequenceNr, data);
47 public static void clear() {
51 public static Map<Long, Object> get(String persistenceId) {
52 Map<Long, Object> journal = journals.get(persistenceId);
53 return journal != null ? journal : Collections.<Long, Object>emptyMap();
56 public static void waitForDeleteMessagesComplete(String persistenceId) {
57 assertEquals("Recovery complete", true, Uninterruptibles.awaitUninterruptibly(
58 deleteMessagesCompleteLatches.get(persistenceId), 5, TimeUnit.SECONDS));
61 public static void addDeleteMessagesCompleteLatch(String persistenceId) {
62 deleteMessagesCompleteLatches.put(persistenceId, new CountDownLatch(1));
66 public Future<Void> doAsyncReplayMessages(final String persistenceId, long fromSequenceNr,
67 long toSequenceNr, long max, final Procedure<PersistentRepr> replayCallback) {
68 return Futures.future(new Callable<Void>() {
70 public Void call() throws Exception {
71 Map<Long, Object> journal = journals.get(persistenceId);
76 synchronized (journal) {
77 for (Map.Entry<Long,Object> entry : journal.entrySet()) {
78 PersistentRepr persistentMessage =
79 new PersistentImpl(entry.getValue(), entry.getKey(), persistenceId,
81 replayCallback.apply(persistentMessage);
87 }, context().dispatcher());
91 public Future<Long> doAsyncReadHighestSequenceNr(String persistenceId, long fromSequenceNr) {
92 return Futures.successful(-1L);
96 public Future<Void> doAsyncWriteMessages(final Iterable<PersistentRepr> messages) {
97 return Futures.future(new Callable<Void>() {
99 public Void call() throws Exception {
100 for (PersistentRepr repr : messages) {
101 Map<Long, Object> journal = journals.get(repr.persistenceId());
102 if(journal == null) {
103 journal = Maps.newLinkedHashMap();
104 journals.put(repr.persistenceId(), journal);
107 synchronized (journal) {
108 journal.put(repr.sequenceNr(), repr.payload());
113 }, context().dispatcher());
117 public Future<Void> doAsyncWriteConfirmations(Iterable<PersistentConfirmation> confirmations) {
118 return Futures.successful(null);
122 public Future<Void> doAsyncDeleteMessages(Iterable<PersistentId> messageIds, boolean permanent) {
123 return Futures.successful(null);
127 public Future<Void> doAsyncDeleteMessagesTo(String persistenceId, long toSequenceNr, boolean permanent) {
128 Map<Long, Object> journal = journals.get(persistenceId);
129 if(journal != null) {
130 synchronized (journal) {
131 Iterator<Long> iter = journal.keySet().iterator();
132 while(iter.hasNext()) {
133 Long n = iter.next();
134 if(n <= toSequenceNr) {
141 CountDownLatch latch = deleteMessagesCompleteLatches.get(persistenceId);
146 return Futures.successful(null);