2 * Copyright (c) 2019 Pantheon Technologies, s.r.o. 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.akka.segjournal;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.mockito.ArgumentMatchers.any;
15 import static org.mockito.Mockito.doNothing;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.times;
18 import static org.mockito.Mockito.verify;
20 import akka.actor.ActorRef;
21 import akka.actor.ActorSystem;
22 import akka.actor.PoisonPill;
23 import akka.persistence.AtomicWrite;
24 import akka.persistence.PersistentRepr;
25 import akka.testkit.CallingThreadDispatcher;
26 import akka.testkit.javadsl.TestKit;
27 import io.atomix.storage.StorageLevel;
29 import java.io.IOException;
30 import java.io.Serializable;
31 import java.nio.file.Files;
32 import java.nio.file.Path;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Optional;
36 import java.util.function.Consumer;
37 import java.util.stream.Collectors;
38 import org.apache.commons.io.FileUtils;
39 import org.junit.After;
40 import org.junit.AfterClass;
41 import org.junit.Before;
42 import org.junit.BeforeClass;
43 import org.junit.Test;
44 import org.opendaylight.controller.akka.segjournal.SegmentedJournalActor.AsyncMessage;
45 import org.opendaylight.controller.akka.segjournal.SegmentedJournalActor.WriteMessages;
46 import scala.concurrent.Future;
48 public class SegmentedFileJournalTest {
49 private static final File DIRECTORY = new File("target/sfj-test");
50 private static final int SEGMENT_SIZE = 1024 * 1024;
51 private static final int MESSAGE_SIZE = 512 * 1024;
53 private static ActorSystem SYSTEM;
56 private ActorRef actor;
59 public static void beforeClass() {
60 SYSTEM = ActorSystem.create("test");
64 public static void afterClass() {
65 TestKit.shutdownActorSystem(SYSTEM);
70 public void before() {
71 kit = new TestKit(SYSTEM);
72 FileUtils.deleteQuietly(DIRECTORY);
78 actor.tell(PoisonPill.getInstance(), ActorRef.noSender());
82 public void testDeleteAfterStop() {
84 final WriteMessages write = new WriteMessages();
85 final Future<Optional<Exception>> first = write.add(AtomicWrite.apply(PersistentRepr.apply("first", 1, "foo",
86 null, false, kit.getRef(), "uuid")));
87 final Future<Optional<Exception>> second = write.add(AtomicWrite.apply(PersistentRepr.apply("second", 2, "foo",
88 null, false, kit.getRef(), "uuid")));
89 actor.tell(write, ActorRef.noSender());
90 assertFalse(getFuture(first).isPresent());
91 assertFalse(getFuture(second).isPresent());
93 assertHighestSequenceNr(2);
98 assertHighestSequenceNr(2);
102 actor.tell(PoisonPill.getInstance(), ActorRef.noSender());
105 // Check if state is retained
106 assertHighestSequenceNr(2);
107 assertReplayCount(1);
111 public void testSegmentation() throws IOException {
112 // We want to have roughly three segments
113 final LargePayload payload = new LargePayload();
115 final WriteMessages write = new WriteMessages();
116 final List<Future<Optional<Exception>>> requests = new ArrayList<>();
118 // Each payload is half of segment size, plus some overhead, should result in two segments being present
119 for (int i = 1; i <= SEGMENT_SIZE * 3 / MESSAGE_SIZE; ++i) {
120 requests.add(write.add(AtomicWrite.apply(PersistentRepr.apply(payload, i, "foo", null, false, kit.getRef(),
124 actor.tell(write, ActorRef.noSender());
125 requests.forEach(future -> assertFalse(getFuture(future).isPresent()));
127 assertFileCount(2, 1);
129 // Delete all but the last entry
130 deleteEntries(requests.size());
132 assertFileCount(1, 1);
135 private ActorRef actor() {
136 return kit.childActorOf(SegmentedJournalActor.props("foo", DIRECTORY, StorageLevel.DISK, MESSAGE_SIZE,
137 SEGMENT_SIZE).withDispatcher(CallingThreadDispatcher.Id()));
140 private void deleteEntries(final long deleteTo) {
141 final AsyncMessage<Void> delete = SegmentedJournalActor.deleteMessagesTo(deleteTo);
142 actor.tell(delete, ActorRef.noSender());
143 assertNull(get(delete));
146 private void assertHighestSequenceNr(final long expected) {
147 AsyncMessage<Long> highest = SegmentedJournalActor.readHighestSequenceNr(0);
148 actor.tell(highest, ActorRef.noSender());
149 assertEquals(expected, (long) get(highest));
152 private void assertReplayCount(final int expected) {
153 Consumer<PersistentRepr> firstCallback = mock(Consumer.class);
154 doNothing().when(firstCallback).accept(any(PersistentRepr.class));
155 AsyncMessage<Void> replay = SegmentedJournalActor.replayMessages(0, Long.MAX_VALUE, Long.MAX_VALUE,
157 actor.tell(replay, ActorRef.noSender());
158 assertNull(get(replay));
159 verify(firstCallback, times(expected)).accept(any(PersistentRepr.class));
162 private static void assertFileCount(final long dataFiles, final long deleteFiles) throws IOException {
163 List<File> contents = Files.list(DIRECTORY.toPath()).map(Path::toFile).collect(Collectors.toList());
164 assertEquals(dataFiles, contents.stream().filter(file -> file.getName().startsWith("data-")).count());
165 assertEquals(deleteFiles, contents.stream().filter(file -> file.getName().startsWith("delete-")).count());
168 private static <T> T get(final AsyncMessage<T> message) {
169 return getFuture(message.promise.future());
172 private static <T> T getFuture(final Future<T> future) {
173 assertTrue(future.isCompleted());
174 return future.value().get().get();
177 private static final class LargePayload implements Serializable {
178 private static final long serialVersionUID = 1L;
180 final byte[] bytes = new byte[MESSAGE_SIZE / 2];