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 private static final Map<String, CountDownLatch> blockReadMessagesLatches = new ConcurrentHashMap<>();
37 public static void addEntry(String persistenceId, long sequenceNr, Object data) {
38 Map<Long, Object> journal = journals.get(persistenceId);
40 journal = Maps.newLinkedHashMap();
41 journals.put(persistenceId, journal);
44 synchronized (journal) {
45 journal.put(sequenceNr, data);
49 public static void clear() {
53 public static Map<Long, Object> get(String persistenceId) {
54 Map<Long, Object> journal = journals.get(persistenceId);
55 return journal != null ? journal : Collections.<Long, Object>emptyMap();
58 public static void waitForDeleteMessagesComplete(String persistenceId) {
59 assertEquals("Recovery complete", true, Uninterruptibles.awaitUninterruptibly(
60 deleteMessagesCompleteLatches.get(persistenceId), 5, TimeUnit.SECONDS));
63 public static void addDeleteMessagesCompleteLatch(String persistenceId) {
64 deleteMessagesCompleteLatches.put(persistenceId, new CountDownLatch(1));
67 public static void addBlockReadMessagesLatch(String persistenceId, CountDownLatch latch) {
68 blockReadMessagesLatches.put(persistenceId, latch);
72 public Future<Void> doAsyncReplayMessages(final String persistenceId, long fromSequenceNr,
73 long toSequenceNr, long max, final Procedure<PersistentRepr> replayCallback) {
74 return Futures.future(new Callable<Void>() {
76 public Void call() throws Exception {
77 CountDownLatch blockLatch = blockReadMessagesLatches.remove(persistenceId);
78 if(blockLatch != null) {
79 Uninterruptibles.awaitUninterruptibly(blockLatch);
82 Map<Long, Object> journal = journals.get(persistenceId);
87 synchronized (journal) {
88 for (Map.Entry<Long,Object> entry : journal.entrySet()) {
89 PersistentRepr persistentMessage =
90 new PersistentImpl(entry.getValue(), entry.getKey(), persistenceId,
92 replayCallback.apply(persistentMessage);
98 }, context().dispatcher());
102 public Future<Long> doAsyncReadHighestSequenceNr(String persistenceId, long fromSequenceNr) {
103 return Futures.successful(-1L);
107 public Future<Void> doAsyncWriteMessages(final Iterable<PersistentRepr> messages) {
108 return Futures.future(new Callable<Void>() {
110 public Void call() throws Exception {
111 for (PersistentRepr repr : messages) {
112 Map<Long, Object> journal = journals.get(repr.persistenceId());
113 if(journal == null) {
114 journal = Maps.newLinkedHashMap();
115 journals.put(repr.persistenceId(), journal);
118 synchronized (journal) {
119 journal.put(repr.sequenceNr(), repr.payload());
124 }, context().dispatcher());
128 public Future<Void> doAsyncWriteConfirmations(Iterable<PersistentConfirmation> confirmations) {
129 return Futures.successful(null);
133 public Future<Void> doAsyncDeleteMessages(Iterable<PersistentId> messageIds, boolean permanent) {
134 return Futures.successful(null);
138 public Future<Void> doAsyncDeleteMessagesTo(String persistenceId, long toSequenceNr, boolean permanent) {
139 Map<Long, Object> journal = journals.get(persistenceId);
140 if(journal != null) {
141 synchronized (journal) {
142 Iterator<Long> iter = journal.keySet().iterator();
143 while(iter.hasNext()) {
144 Long n = iter.next();
145 if(n <= toSequenceNr) {
152 CountDownLatch latch = deleteMessagesCompleteLatches.get(persistenceId);
157 return Futures.successful(null);