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);
136 public void testComplexDeletesAndPartialReplays() throws Exception {
137 for (int i = 0; i <= 4; i++) {
141 assertFileCount(10, 1);
143 // delete including index 3, so get rid of the first segment
145 assertFileCount(9, 1);
147 // get rid of segments 2(index 4-6) and 3(index 7-9)
149 assertFileCount(7, 1);
151 // get rid of all segments except the last one
153 assertFileCount(1, 1);
157 // Check if state is retained
158 assertHighestSequenceNr(30);
160 assertReplayCount(3);
166 assertHighestSequenceNr(30);
168 assertReplayCount(2);
174 assertReplayCount(1);
180 assertReplayCount(0);
183 private void restartActor() {
184 actor.tell(PoisonPill.getInstance(), ActorRef.noSender());
188 private void writeBigPaylod() {
189 final LargePayload payload = new LargePayload();
191 final WriteMessages write = new WriteMessages();
192 final List<Future<Optional<Exception>>> requests = new ArrayList<>();
194 // Each payload is half of segment size, plus some overhead, should result in two segments being present
195 for (int i = 1; i <= SEGMENT_SIZE * 3 / MESSAGE_SIZE; ++i) {
196 requests.add(write.add(AtomicWrite.apply(PersistentRepr.apply(payload, i, "foo", null, false, kit.getRef(),
200 actor.tell(write, ActorRef.noSender());
201 requests.forEach(future -> assertFalse(getFuture(future).isPresent()));
204 private ActorRef actor() {
205 return kit.childActorOf(SegmentedJournalActor.props("foo", DIRECTORY, StorageLevel.DISK, MESSAGE_SIZE,
206 SEGMENT_SIZE).withDispatcher(CallingThreadDispatcher.Id()));
209 private void deleteEntries(final long deleteTo) {
210 final AsyncMessage<Void> delete = SegmentedJournalActor.deleteMessagesTo(deleteTo);
211 actor.tell(delete, ActorRef.noSender());
212 assertNull(get(delete));
215 private void assertHighestSequenceNr(final long expected) {
216 AsyncMessage<Long> highest = SegmentedJournalActor.readHighestSequenceNr(0);
217 actor.tell(highest, ActorRef.noSender());
218 assertEquals(expected, (long) get(highest));
221 private void assertReplayCount(final int expected) {
222 Consumer<PersistentRepr> firstCallback = mock(Consumer.class);
223 doNothing().when(firstCallback).accept(any(PersistentRepr.class));
224 AsyncMessage<Void> replay = SegmentedJournalActor.replayMessages(0, Long.MAX_VALUE, Long.MAX_VALUE,
226 actor.tell(replay, ActorRef.noSender());
227 assertNull(get(replay));
228 verify(firstCallback, times(expected)).accept(any(PersistentRepr.class));
231 private static void assertFileCount(final long dataFiles, final long deleteFiles) throws IOException {
232 List<File> contents = Files.list(DIRECTORY.toPath()).map(Path::toFile).collect(Collectors.toList());
233 assertEquals(dataFiles, contents.stream().filter(file -> file.getName().startsWith("data-")).count());
234 assertEquals(deleteFiles, contents.stream().filter(file -> file.getName().startsWith("delete-")).count());
237 private static <T> T get(final AsyncMessage<T> message) {
238 return getFuture(message.promise.future());
241 private static <T> T getFuture(final Future<T> future) {
242 assertTrue(future.isCompleted());
243 return future.value().get().get();
246 private static final class LargePayload implements Serializable {
247 private static final long serialVersionUID = 1L;
249 final byte[] bytes = new byte[MESSAGE_SIZE / 2];