Remove MockReplicatedLogEntry
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / RaftActorRecoverySupportTest.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. 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.cluster.raft;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.AdditionalMatchers.aryEq;
14 import static org.mockito.Matchers.any;
15 import static org.mockito.Matchers.anyInt;
16 import static org.mockito.Mockito.doNothing;
17 import static org.mockito.Mockito.doReturn;
18 import static org.mockito.Mockito.never;
19 import static org.mockito.Mockito.verify;
20 import static org.mockito.Mockito.verifyNoMoreInteractions;
21
22 import akka.persistence.RecoveryCompleted;
23 import akka.persistence.SnapshotMetadata;
24 import akka.persistence.SnapshotOffer;
25 import com.google.common.collect.Sets;
26 import java.util.Arrays;
27 import java.util.Collections;
28 import org.hamcrest.Description;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.ArgumentMatcher;
32 import org.mockito.InOrder;
33 import org.mockito.Matchers;
34 import org.mockito.Mock;
35 import org.mockito.Mockito;
36 import org.mockito.MockitoAnnotations;
37 import org.opendaylight.controller.cluster.DataPersistenceProvider;
38 import org.opendaylight.controller.cluster.PersistentDataProvider;
39 import org.opendaylight.controller.cluster.raft.persisted.ApplyJournalEntries;
40 import org.opendaylight.controller.cluster.raft.persisted.DeleteEntries;
41 import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload;
42 import org.opendaylight.controller.cluster.raft.persisted.ServerInfo;
43 import org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry;
44 import org.opendaylight.controller.cluster.raft.persisted.UpdateElectionTerm;
45 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 /**
50  * Unit tests for RaftActorRecoverySupport.
51  *
52  * @author Thomas Pantelis
53  */
54 public class RaftActorRecoverySupportTest {
55
56     private static final Logger LOG = LoggerFactory.getLogger(RaftActorRecoverySupportTest.class);
57
58     @Mock
59     private DataPersistenceProvider mockPersistence;
60
61
62     @Mock
63     private RaftActorRecoveryCohort mockCohort;
64
65     @Mock
66     PersistentDataProvider mockPersistentProvider;
67
68     private RaftActorRecoverySupport support;
69
70     private RaftActorContext context;
71     private final DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl();
72     private final String localId = "leader";
73
74
75     @Before
76     public void setup() {
77         MockitoAnnotations.initMocks(this);
78
79         context = new RaftActorContextImpl(null, null, localId, new ElectionTermImpl(mockPersistentProvider, "test",
80                 LOG), -1, -1, Collections.<String,String>emptyMap(), configParams, mockPersistence, LOG);
81
82         support = new RaftActorRecoverySupport(context, mockCohort);
83
84         doReturn(true).when(mockPersistence).isRecoveryApplicable();
85
86         context.setReplicatedLog(ReplicatedLogImpl.newInstance(context));
87     }
88
89     private void sendMessageToSupport(Object message) {
90         sendMessageToSupport(message, false);
91     }
92
93     private void sendMessageToSupport(Object message, boolean expComplete) {
94         boolean complete = support.handleRecoveryMessage(message, mockPersistentProvider);
95         assertEquals("complete", expComplete, complete);
96     }
97
98     @Test
99     public void testOnReplicatedLogEntry() {
100         ReplicatedLogEntry logEntry = new SimpleReplicatedLogEntry(1, 1, new MockRaftActorContext.MockPayload("1", 5));
101
102         sendMessageToSupport(logEntry);
103
104         assertEquals("Journal log size", 1, context.getReplicatedLog().size());
105         assertEquals("Journal data size", 5, context.getReplicatedLog().dataSize());
106         assertEquals("Last index", 1, context.getReplicatedLog().lastIndex());
107         assertEquals("Last applied", -1, context.getLastApplied());
108         assertEquals("Commit index", -1, context.getCommitIndex());
109         assertEquals("Snapshot term", -1, context.getReplicatedLog().getSnapshotTerm());
110         assertEquals("Snapshot index", -1, context.getReplicatedLog().getSnapshotIndex());
111     }
112
113     @Test
114     public void testOnApplyJournalEntries() {
115         configParams.setJournalRecoveryLogBatchSize(5);
116
117         ReplicatedLog replicatedLog = context.getReplicatedLog();
118         replicatedLog.append(new SimpleReplicatedLogEntry(0, 1, new MockRaftActorContext.MockPayload("0")));
119         replicatedLog.append(new SimpleReplicatedLogEntry(1, 1, new MockRaftActorContext.MockPayload("1")));
120         replicatedLog.append(new SimpleReplicatedLogEntry(2, 1, new MockRaftActorContext.MockPayload("2")));
121         replicatedLog.append(new SimpleReplicatedLogEntry(3, 1, new MockRaftActorContext.MockPayload("3")));
122         replicatedLog.append(new SimpleReplicatedLogEntry(4, 1, new MockRaftActorContext.MockPayload("4")));
123         replicatedLog.append(new SimpleReplicatedLogEntry(5, 1, new MockRaftActorContext.MockPayload("5")));
124
125         sendMessageToSupport(new ApplyJournalEntries(2));
126
127         assertEquals("Last applied", 2, context.getLastApplied());
128         assertEquals("Commit index", 2, context.getCommitIndex());
129
130         sendMessageToSupport(new ApplyJournalEntries(4));
131
132         assertEquals("Last applied", 4, context.getLastApplied());
133         assertEquals("Last applied", 4, context.getLastApplied());
134
135         sendMessageToSupport(new ApplyJournalEntries(5));
136
137         assertEquals("Last index", 5, context.getReplicatedLog().lastIndex());
138         assertEquals("Last applied", 5, context.getLastApplied());
139         assertEquals("Commit index", 5, context.getCommitIndex());
140         assertEquals("Snapshot term", -1, context.getReplicatedLog().getSnapshotTerm());
141         assertEquals("Snapshot index", -1, context.getReplicatedLog().getSnapshotIndex());
142
143         InOrder inOrder = Mockito.inOrder(mockCohort);
144         inOrder.verify(mockCohort).startLogRecoveryBatch(5);
145
146         for (int i = 0; i < replicatedLog.size() - 1; i++) {
147             inOrder.verify(mockCohort).appendRecoveredLogEntry(replicatedLog.get(i).getData());
148         }
149
150         inOrder.verify(mockCohort).applyCurrentLogRecoveryBatch();
151         inOrder.verify(mockCohort).startLogRecoveryBatch(5);
152         inOrder.verify(mockCohort).appendRecoveredLogEntry(replicatedLog.get(replicatedLog.size() - 1).getData());
153
154         inOrder.verifyNoMoreInteractions();
155     }
156
157     @Test
158     public void testOnSnapshotOffer() {
159
160         ReplicatedLog replicatedLog = context.getReplicatedLog();
161         replicatedLog.append(new SimpleReplicatedLogEntry(1, 1, new MockRaftActorContext.MockPayload("1")));
162         replicatedLog.append(new SimpleReplicatedLogEntry(2, 1, new MockRaftActorContext.MockPayload("2")));
163         replicatedLog.append(new SimpleReplicatedLogEntry(3, 1, new MockRaftActorContext.MockPayload("3")));
164
165         byte[] snapshotBytes = {1,2,3,4,5};
166
167         ReplicatedLogEntry unAppliedEntry1 = new SimpleReplicatedLogEntry(4, 1,
168                 new MockRaftActorContext.MockPayload("4", 4));
169
170         ReplicatedLogEntry unAppliedEntry2 = new SimpleReplicatedLogEntry(5, 1,
171                 new MockRaftActorContext.MockPayload("5", 5));
172
173         long lastAppliedDuringSnapshotCapture = 3;
174         long lastIndexDuringSnapshotCapture = 5;
175         long electionTerm = 2;
176         String electionVotedFor = "member-2";
177
178         Snapshot snapshot = Snapshot.create(snapshotBytes, Arrays.asList(unAppliedEntry1, unAppliedEntry2),
179                 lastIndexDuringSnapshotCapture, 1, lastAppliedDuringSnapshotCapture, 1, electionTerm, electionVotedFor);
180
181         SnapshotMetadata metadata = new SnapshotMetadata("test", 6, 12345);
182         SnapshotOffer snapshotOffer = new SnapshotOffer(metadata , snapshot);
183
184         sendMessageToSupport(snapshotOffer);
185
186         assertEquals("Journal log size", 2, context.getReplicatedLog().size());
187         assertEquals("Journal data size", 9, context.getReplicatedLog().dataSize());
188         assertEquals("Last index", lastIndexDuringSnapshotCapture, context.getReplicatedLog().lastIndex());
189         assertEquals("Last applied", lastAppliedDuringSnapshotCapture, context.getLastApplied());
190         assertEquals("Commit index", lastAppliedDuringSnapshotCapture, context.getCommitIndex());
191         assertEquals("Snapshot term", 1, context.getReplicatedLog().getSnapshotTerm());
192         assertEquals("Snapshot index", lastAppliedDuringSnapshotCapture, context.getReplicatedLog().getSnapshotIndex());
193         assertEquals("Election term", electionTerm, context.getTermInformation().getCurrentTerm());
194         assertEquals("Election votedFor", electionVotedFor, context.getTermInformation().getVotedFor());
195         assertFalse("Dynamic server configuration", context.isDynamicServerConfigurationInUse());
196
197         verify(mockCohort).applyRecoverySnapshot(snapshotBytes);
198     }
199
200     @Test
201     public void testOnRecoveryCompletedWithRemainingBatch() {
202         ReplicatedLog replicatedLog = context.getReplicatedLog();
203         replicatedLog.append(new SimpleReplicatedLogEntry(0, 1, new MockRaftActorContext.MockPayload("0")));
204         replicatedLog.append(new SimpleReplicatedLogEntry(1, 1, new MockRaftActorContext.MockPayload("1")));
205
206         sendMessageToSupport(new ApplyJournalEntries(1));
207
208         sendMessageToSupport(RecoveryCompleted.getInstance(), true);
209
210         assertEquals("Last applied", 1, context.getLastApplied());
211         assertEquals("Commit index", 1, context.getCommitIndex());
212
213         InOrder inOrder = Mockito.inOrder(mockCohort);
214         inOrder.verify(mockCohort).startLogRecoveryBatch(anyInt());
215
216         for (int i = 0; i < replicatedLog.size(); i++) {
217             inOrder.verify(mockCohort).appendRecoveredLogEntry(replicatedLog.get(i).getData());
218         }
219
220         inOrder.verify(mockCohort).applyCurrentLogRecoveryBatch();
221         inOrder.verify(mockCohort).getRestoreFromSnapshot();
222         inOrder.verifyNoMoreInteractions();
223     }
224
225     @Test
226     public void testOnRecoveryCompletedWithNoRemainingBatch() {
227         sendMessageToSupport(RecoveryCompleted.getInstance(), true);
228
229         verify(mockCohort).getRestoreFromSnapshot();
230         verifyNoMoreInteractions(mockCohort);
231     }
232
233     @Test
234     public void testOnDeleteEntries() {
235         ReplicatedLog replicatedLog = context.getReplicatedLog();
236         replicatedLog.append(new SimpleReplicatedLogEntry(0, 1, new MockRaftActorContext.MockPayload("0")));
237         replicatedLog.append(new SimpleReplicatedLogEntry(1, 1, new MockRaftActorContext.MockPayload("1")));
238         replicatedLog.append(new SimpleReplicatedLogEntry(2, 1, new MockRaftActorContext.MockPayload("2")));
239
240         sendMessageToSupport(new DeleteEntries(1));
241
242         assertEquals("Journal log size", 1, context.getReplicatedLog().size());
243         assertEquals("Last index", 0, context.getReplicatedLog().lastIndex());
244     }
245
246     @Test
247     public void testUpdateElectionTerm() {
248
249         sendMessageToSupport(new UpdateElectionTerm(5, "member2"));
250
251         assertEquals("Current term", 5, context.getTermInformation().getCurrentTerm());
252         assertEquals("Voted For", "member2", context.getTermInformation().getVotedFor());
253     }
254
255     @Test
256     public void testDataRecoveredWithPersistenceDisabled() {
257         doNothing().when(mockCohort).applyRecoverySnapshot(aryEq(new byte[0]));
258         doReturn(false).when(mockPersistence).isRecoveryApplicable();
259         doReturn(10L).when(mockPersistentProvider).getLastSequenceNumber();
260
261         Snapshot snapshot = Snapshot.create(new byte[]{1}, Collections.<ReplicatedLogEntry>emptyList(), 3, 1, 3, 1);
262         SnapshotOffer snapshotOffer = new SnapshotOffer(new SnapshotMetadata("test", 6, 12345), snapshot);
263
264         sendMessageToSupport(snapshotOffer);
265
266         sendMessageToSupport(new UpdateElectionTerm(5, "member2"));
267
268         sendMessageToSupport(new SimpleReplicatedLogEntry(4, 1, new MockRaftActorContext.MockPayload("4")));
269         sendMessageToSupport(new SimpleReplicatedLogEntry(5, 1, new MockRaftActorContext.MockPayload("5")));
270
271         sendMessageToSupport(new ApplyJournalEntries(4));
272
273         sendMessageToSupport(new DeleteEntries(5));
274
275         assertEquals("Journal log size", 0, context.getReplicatedLog().size());
276         assertEquals("Last index", -1, context.getReplicatedLog().lastIndex());
277         assertEquals("Last applied", -1, context.getLastApplied());
278         assertEquals("Commit index", -1, context.getCommitIndex());
279         assertEquals("Snapshot term", -1, context.getReplicatedLog().getSnapshotTerm());
280         assertEquals("Snapshot index", -1, context.getReplicatedLog().getSnapshotIndex());
281
282         assertEquals("Current term", 5, context.getTermInformation().getCurrentTerm());
283         assertEquals("Voted For", "member2", context.getTermInformation().getVotedFor());
284
285         sendMessageToSupport(RecoveryCompleted.getInstance(), true);
286
287         verify(mockCohort).applyRecoverySnapshot(aryEq(new byte[0]));
288         verify(mockCohort, never()).getRestoreFromSnapshot();
289         verifyNoMoreInteractions(mockCohort);
290
291         verify(mockPersistentProvider).deleteMessages(10L);
292     }
293
294     static UpdateElectionTerm updateElectionTerm(final long term, final String votedFor) {
295         return Matchers.argThat(new ArgumentMatcher<UpdateElectionTerm>() {
296             @Override
297             public boolean matches(Object argument) {
298                 UpdateElectionTerm other = (UpdateElectionTerm) argument;
299                 return term == other.getCurrentTerm() && votedFor.equals(other.getVotedFor());
300             }
301
302             @Override
303             public void describeTo(Description description) {
304                 description.appendValue(new UpdateElectionTerm(term, votedFor));
305             }
306         });
307     }
308
309     @Test
310     public void testNoDataRecoveredWithPersistenceDisabled() {
311         doReturn(false).when(mockPersistence).isRecoveryApplicable();
312
313         sendMessageToSupport(new UpdateElectionTerm(5, "member2"));
314
315         assertEquals("Current term", 5, context.getTermInformation().getCurrentTerm());
316         assertEquals("Voted For", "member2", context.getTermInformation().getVotedFor());
317
318         sendMessageToSupport(RecoveryCompleted.getInstance(), true);
319
320         verify(mockCohort).getRestoreFromSnapshot();
321         verifyNoMoreInteractions(mockCohort, mockPersistentProvider);
322     }
323
324     @Test
325     public void testServerConfigurationPayloadApplied() {
326         String follower1 = "follower1";
327         String follower2 = "follower2";
328         String follower3 = "follower3";
329
330         context.addToPeers(follower1, null, VotingState.VOTING);
331         context.addToPeers(follower2, null, VotingState.VOTING);
332
333         //add new Server
334         ServerConfigurationPayload obj = new ServerConfigurationPayload(Arrays.asList(
335                 new ServerInfo(localId, true),
336                 new ServerInfo(follower1, true),
337                 new ServerInfo(follower2, false),
338                 new ServerInfo(follower3, true)));
339
340         sendMessageToSupport(new SimpleReplicatedLogEntry(0, 1, obj));
341
342         //verify new peers
343         assertTrue("Dynamic server configuration", context.isDynamicServerConfigurationInUse());
344         assertEquals("New peer Ids", Sets.newHashSet(follower1, follower2, follower3),
345                 Sets.newHashSet(context.getPeerIds()));
346         assertEquals("follower1 isVoting", true, context.getPeerInfo(follower1).isVoting());
347         assertEquals("follower2 isVoting", false, context.getPeerInfo(follower2).isVoting());
348         assertEquals("follower3 isVoting", true, context.getPeerInfo(follower3).isVoting());
349
350         sendMessageToSupport(new ApplyJournalEntries(0));
351
352         verify(mockCohort, never()).startLogRecoveryBatch(anyInt());
353         verify(mockCohort, never()).appendRecoveredLogEntry(any(Payload.class));
354
355         //remove existing follower1
356         obj = new ServerConfigurationPayload(Arrays.asList(
357                 new ServerInfo(localId, true),
358                 new ServerInfo("follower2", true),
359                 new ServerInfo("follower3", true)));
360
361         sendMessageToSupport(new SimpleReplicatedLogEntry(1, 1, obj));
362
363         //verify new peers
364         assertTrue("Dynamic server configuration", context.isDynamicServerConfigurationInUse());
365         assertEquals("New peer Ids", Sets.newHashSet(follower2, follower3), Sets.newHashSet(context.getPeerIds()));
366     }
367
368     @Test
369     public void testServerConfigurationPayloadAppliedWithPersistenceDisabled() {
370         doReturn(false).when(mockPersistence).isRecoveryApplicable();
371
372         String follower = "follower";
373         ServerConfigurationPayload obj = new ServerConfigurationPayload(Arrays.asList(
374                 new ServerInfo(localId, true), new ServerInfo(follower, true)));
375
376         sendMessageToSupport(new SimpleReplicatedLogEntry(0, 1, obj));
377
378         //verify new peers
379         assertEquals("New peer Ids", Sets.newHashSet(follower), Sets.newHashSet(context.getPeerIds()));
380     }
381
382     @Test
383     public void testOnSnapshotOfferWithServerConfiguration() {
384         long electionTerm = 2;
385         String electionVotedFor = "member-2";
386         ServerConfigurationPayload serverPayload = new ServerConfigurationPayload(Arrays.asList(
387                                                         new ServerInfo(localId, true),
388                                                         new ServerInfo("follower1", true),
389                                                         new ServerInfo("follower2", true)));
390
391         Snapshot snapshot = Snapshot.create(new byte[]{1}, Collections.<ReplicatedLogEntry>emptyList(),
392                 -1, -1, -1, -1, electionTerm, electionVotedFor, serverPayload);
393
394         SnapshotMetadata metadata = new SnapshotMetadata("test", 6, 12345);
395         SnapshotOffer snapshotOffer = new SnapshotOffer(metadata , snapshot);
396
397         sendMessageToSupport(snapshotOffer);
398
399         assertEquals("Journal log size", 0, context.getReplicatedLog().size());
400         assertEquals("Election term", electionTerm, context.getTermInformation().getCurrentTerm());
401         assertEquals("Election votedFor", electionVotedFor, context.getTermInformation().getVotedFor());
402         assertTrue("Dynamic server configuration", context.isDynamicServerConfigurationInUse());
403         assertEquals("Peer List", Sets.newHashSet("follower1", "follower2"),
404             Sets.newHashSet(context.getPeerIds()));
405     }
406 }