6939dbcba78e2cfba07b7db2017cf3be37180d01
[controller.git] / opendaylight / md-sal / sal-akka-segmented-journal / src / test / java / org / opendaylight / controller / akka / segjournal / SegmentedFileJournalTest.java
1 /*
2  * Copyright (c) 2019 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.akka.segjournal;
9
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;
19
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;
28 import java.io.File;
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;
47
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;
52
53     private static ActorSystem SYSTEM;
54
55     private TestKit kit;
56     private ActorRef actor;
57
58     @BeforeClass
59     public static void beforeClass() {
60         SYSTEM = ActorSystem.create("test");
61     }
62
63     @AfterClass
64     public static void afterClass() {
65         TestKit.shutdownActorSystem(SYSTEM);
66         SYSTEM = null;
67     }
68
69     @Before
70     public void before() {
71         kit = new TestKit(SYSTEM);
72         FileUtils.deleteQuietly(DIRECTORY);
73         actor = actor();
74     }
75
76     @After
77     public void after() {
78         actor.tell(PoisonPill.getInstance(), ActorRef.noSender());
79     }
80
81     @Test
82     public void testDeleteAfterStop() {
83         // Preliminary setup
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());
92
93         assertHighestSequenceNr(2);
94         assertReplayCount(2);
95
96         deleteEntries(1);
97
98         assertHighestSequenceNr(2);
99         assertReplayCount(1);
100
101         // Restart actor
102         actor.tell(PoisonPill.getInstance(), ActorRef.noSender());
103         actor = actor();
104
105         // Check if state is retained
106         assertHighestSequenceNr(2);
107         assertReplayCount(1);
108     }
109
110     @Test
111     public void testSegmentation() throws IOException {
112         // We want to have roughly three segments
113         final LargePayload payload = new LargePayload();
114
115         final WriteMessages write = new WriteMessages();
116         final List<Future<Optional<Exception>>> requests = new ArrayList<>();
117
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(),
121                 "uuid"))));
122         }
123
124         actor.tell(write, ActorRef.noSender());
125         requests.forEach(future -> assertFalse(getFuture(future).isPresent()));
126
127         assertFileCount(2, 1);
128
129         // Delete all but the last entry
130         deleteEntries(requests.size());
131
132         assertFileCount(1, 1);
133     }
134
135     private ActorRef actor() {
136         return kit.childActorOf(SegmentedJournalActor.props("foo", DIRECTORY, StorageLevel.DISK, MESSAGE_SIZE,
137             SEGMENT_SIZE).withDispatcher(CallingThreadDispatcher.Id()));
138     }
139
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));
144     }
145
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));
150     }
151
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,
156             firstCallback);
157         actor.tell(replay, ActorRef.noSender());
158         assertNull(get(replay));
159         verify(firstCallback, times(expected)).accept(any(PersistentRepr.class));
160     }
161
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());
166     }
167
168     private static <T> T get(final AsyncMessage<T> message) {
169         return getFuture(message.promise.future());
170     }
171
172     private static <T> T getFuture(final Future<T> future) {
173         assertTrue(future.isCompleted());
174         return future.value().get().get();
175     }
176
177     private static final class LargePayload implements Serializable {
178         private static final long serialVersionUID = 1L;
179
180         final byte[] bytes = new byte[MESSAGE_SIZE / 2];
181
182     }
183 }