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.reset;
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.junit.runner.RunWith;
45 import org.mockito.Mock;
46 import org.mockito.junit.MockitoJUnitRunner;
47 import org.opendaylight.controller.akka.segjournal.SegmentedJournalActor.AsyncMessage;
48 import org.opendaylight.controller.akka.segjournal.SegmentedJournalActor.WriteMessages;
49 import scala.concurrent.Future;
51 @RunWith(MockitoJUnitRunner.StrictStubs.class)
52 public class SegmentedFileJournalTest {
53 private static final File DIRECTORY = new File("target/sfj-test");
54 private static final int SEGMENT_SIZE = 1024 * 1024;
55 private static final int MESSAGE_SIZE = 512 * 1024;
57 private static ActorSystem SYSTEM;
60 private Consumer<PersistentRepr> firstCallback;
63 private ActorRef actor;
66 public static void beforeClass() {
67 SYSTEM = ActorSystem.create("test");
71 public static void afterClass() {
72 TestKit.shutdownActorSystem(SYSTEM);
77 public void before() {
78 kit = new TestKit(SYSTEM);
79 FileUtils.deleteQuietly(DIRECTORY);
85 actor.tell(PoisonPill.getInstance(), ActorRef.noSender());
89 public void testDeleteAfterStop() {
91 final WriteMessages write = new WriteMessages();
92 final Future<Optional<Exception>> first = write.add(AtomicWrite.apply(PersistentRepr.apply("first", 1, "foo",
93 null, false, kit.getRef(), "uuid")));
94 final Future<Optional<Exception>> second = write.add(AtomicWrite.apply(PersistentRepr.apply("second", 2, "foo",
95 null, false, kit.getRef(), "uuid")));
96 actor.tell(write, ActorRef.noSender());
97 assertFalse(getFuture(first).isPresent());
98 assertFalse(getFuture(second).isPresent());
100 assertHighestSequenceNr(2);
101 assertReplayCount(2);
105 assertHighestSequenceNr(2);
106 assertReplayCount(1);
109 actor.tell(PoisonPill.getInstance(), ActorRef.noSender());
112 // Check if state is retained
113 assertHighestSequenceNr(2);
114 assertReplayCount(1);
118 public void testSegmentation() throws IOException {
119 // We want to have roughly three segments
120 final LargePayload payload = new LargePayload();
122 final WriteMessages write = new WriteMessages();
123 final List<Future<Optional<Exception>>> requests = new ArrayList<>();
125 // Each payload is half of segment size, plus some overhead, should result in two segments being present
126 for (int i = 1; i <= SEGMENT_SIZE * 3 / MESSAGE_SIZE; ++i) {
127 requests.add(write.add(AtomicWrite.apply(PersistentRepr.apply(payload, i, "foo", null, false, kit.getRef(),
131 actor.tell(write, ActorRef.noSender());
132 requests.forEach(future -> assertFalse(getFuture(future).isPresent()));
134 assertFileCount(2, 1);
136 // Delete all but the last entry
137 deleteEntries(requests.size());
139 assertFileCount(1, 1);
143 public void testComplexDeletesAndPartialReplays() throws Exception {
144 for (int i = 0; i <= 4; i++) {
148 assertFileCount(10, 1);
150 // delete including index 3, so get rid of the first segment
152 assertFileCount(9, 1);
154 // get rid of segments 2(index 4-6) and 3(index 7-9)
156 assertFileCount(7, 1);
158 // get rid of all segments except the last one
160 assertFileCount(1, 1);
164 // Check if state is retained
165 assertHighestSequenceNr(30);
167 assertReplayCount(3);
173 assertHighestSequenceNr(30);
175 assertReplayCount(2);
181 assertReplayCount(1);
187 assertReplayCount(0);
190 private void restartActor() {
191 actor.tell(PoisonPill.getInstance(), ActorRef.noSender());
195 private void writeBigPaylod() {
196 final LargePayload payload = new LargePayload();
198 final WriteMessages write = new WriteMessages();
199 final List<Future<Optional<Exception>>> requests = new ArrayList<>();
201 // Each payload is half of segment size, plus some overhead, should result in two segments being present
202 for (int i = 1; i <= SEGMENT_SIZE * 3 / MESSAGE_SIZE; ++i) {
203 requests.add(write.add(AtomicWrite.apply(PersistentRepr.apply(payload, i, "foo", null, false, kit.getRef(),
207 actor.tell(write, ActorRef.noSender());
208 requests.forEach(future -> assertFalse(getFuture(future).isPresent()));
211 private ActorRef actor() {
212 return kit.childActorOf(SegmentedJournalActor.props("foo", DIRECTORY, StorageLevel.DISK, MESSAGE_SIZE,
213 SEGMENT_SIZE).withDispatcher(CallingThreadDispatcher.Id()));
216 private void deleteEntries(final long deleteTo) {
217 final AsyncMessage<Void> delete = SegmentedJournalActor.deleteMessagesTo(deleteTo);
218 actor.tell(delete, ActorRef.noSender());
219 assertNull(get(delete));
222 private void assertHighestSequenceNr(final long expected) {
223 AsyncMessage<Long> highest = SegmentedJournalActor.readHighestSequenceNr(0);
224 actor.tell(highest, ActorRef.noSender());
225 assertEquals(expected, (long) get(highest));
228 private void assertReplayCount(final int expected) {
229 // Cast fixes an Eclipse warning 'generic array created'
230 reset((Object) firstCallback);
231 doNothing().when(firstCallback).accept(any(PersistentRepr.class));
232 AsyncMessage<Void> replay = SegmentedJournalActor.replayMessages(0, Long.MAX_VALUE, Long.MAX_VALUE,
234 actor.tell(replay, ActorRef.noSender());
235 assertNull(get(replay));
236 verify(firstCallback, times(expected)).accept(any(PersistentRepr.class));
239 private static void assertFileCount(final long dataFiles, final long deleteFiles) throws IOException {
240 List<File> contents = Files.list(DIRECTORY.toPath()).map(Path::toFile).collect(Collectors.toList());
241 assertEquals(dataFiles, contents.stream().filter(file -> file.getName().startsWith("data-")).count());
242 assertEquals(deleteFiles, contents.stream().filter(file -> file.getName().startsWith("delete-")).count());
245 private static <T> T get(final AsyncMessage<T> message) {
246 return getFuture(message.promise.future());
249 private static <T> T getFuture(final Future<T> future) {
250 assertTrue(future.isCompleted());
251 return future.value().get().get();
254 private static final class LargePayload implements Serializable {
255 private static final long serialVersionUID = 1L;
257 final byte[] bytes = new byte[MESSAGE_SIZE / 2];