2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
3 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.cluster.raft;
11 import static org.junit.Assert.assertEquals;
12 import static org.mockito.Mockito.mock;
13 import akka.actor.ActorRef;
14 import akka.actor.Props;
15 import akka.japi.Creator;
16 import com.google.common.base.Optional;
17 import com.google.common.util.concurrent.Uninterruptibles;
18 import java.io.ByteArrayInputStream;
19 import java.io.IOException;
20 import java.io.ObjectInputStream;
21 import java.util.ArrayList;
22 import java.util.List;
24 import java.util.concurrent.CountDownLatch;
25 import java.util.concurrent.TimeUnit;
26 import javax.annotation.Nonnull;
27 import org.opendaylight.controller.cluster.DataPersistenceProvider;
28 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
30 public class MockRaftActor extends RaftActor implements RaftActorRecoveryCohort, RaftActorSnapshotCohort {
32 public static final short PAYLOAD_VERSION = 5;
34 final RaftActor actorDelegate;
35 final RaftActorRecoveryCohort recoveryCohortDelegate;
36 final RaftActorSnapshotCohort snapshotCohortDelegate;
37 private final CountDownLatch recoveryComplete = new CountDownLatch(1);
38 private final List<Object> state;
39 private ActorRef roleChangeNotifier;
40 private final CountDownLatch initializeBehaviorComplete = new CountDownLatch(1);
41 private RaftActorRecoverySupport raftActorRecoverySupport;
42 private RaftActorSnapshotMessageSupport snapshotMessageSupport;
44 public static final class MockRaftActorCreator implements Creator<MockRaftActor> {
45 private static final long serialVersionUID = 1L;
46 private final Map<String, String> peerAddresses;
47 private final String id;
48 private final Optional<ConfigParams> config;
49 private final DataPersistenceProvider dataPersistenceProvider;
50 private final ActorRef roleChangeNotifier;
51 private RaftActorSnapshotMessageSupport snapshotMessageSupport;
53 private MockRaftActorCreator(Map<String, String> peerAddresses, String id,
54 Optional<ConfigParams> config, DataPersistenceProvider dataPersistenceProvider,
55 ActorRef roleChangeNotifier) {
56 this.peerAddresses = peerAddresses;
59 this.dataPersistenceProvider = dataPersistenceProvider;
60 this.roleChangeNotifier = roleChangeNotifier;
64 public MockRaftActor create() throws Exception {
65 MockRaftActor mockRaftActor = new MockRaftActor(id, peerAddresses, config,
66 dataPersistenceProvider);
67 mockRaftActor.roleChangeNotifier = this.roleChangeNotifier;
68 mockRaftActor.snapshotMessageSupport = snapshotMessageSupport;
73 public MockRaftActor(String id, Map<String, String> peerAddresses, Optional<ConfigParams> config,
74 DataPersistenceProvider dataPersistenceProvider) {
75 super(id, peerAddresses, config, PAYLOAD_VERSION);
76 state = new ArrayList<>();
77 this.actorDelegate = mock(RaftActor.class);
78 this.recoveryCohortDelegate = mock(RaftActorRecoveryCohort.class);
79 this.snapshotCohortDelegate = mock(RaftActorSnapshotCohort.class);
80 if(dataPersistenceProvider == null){
83 setPersistence(dataPersistenceProvider);
87 public void setRaftActorRecoverySupport(RaftActorRecoverySupport support) {
88 raftActorRecoverySupport = support;
92 public RaftActorRecoverySupport newRaftActorRecoverySupport() {
93 return raftActorRecoverySupport != null ? raftActorRecoverySupport : super.newRaftActorRecoverySupport();
97 protected RaftActorSnapshotMessageSupport newRaftActorSnapshotMessageSupport() {
98 return snapshotMessageSupport != null ? snapshotMessageSupport : super.newRaftActorSnapshotMessageSupport();
101 public void waitForRecoveryComplete() {
103 assertEquals("Recovery complete", true, recoveryComplete.await(5, TimeUnit.SECONDS));
104 } catch (InterruptedException e) {
109 public void waitForInitializeBehaviorComplete() {
111 assertEquals("Behavior initialized", true, initializeBehaviorComplete.await(5, TimeUnit.SECONDS));
112 } catch (InterruptedException e) {
118 public void waitUntilLeader(){
119 for(int i = 0;i < 10; i++){
123 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
127 public List<Object> getState() {
131 public static Props props(final String id, final Map<String, String> peerAddresses,
132 Optional<ConfigParams> config){
133 return Props.create(new MockRaftActorCreator(peerAddresses, id, config, null, null));
136 public static Props props(final String id, final Map<String, String> peerAddresses,
137 Optional<ConfigParams> config, RaftActorSnapshotMessageSupport snapshotMessageSupport){
138 MockRaftActorCreator creator = new MockRaftActorCreator(peerAddresses, id, config, null, null);
139 creator.snapshotMessageSupport = snapshotMessageSupport;
140 return Props.create(creator);
143 public static Props props(final String id, final Map<String, String> peerAddresses,
144 Optional<ConfigParams> config, DataPersistenceProvider dataPersistenceProvider){
145 return Props.create(new MockRaftActorCreator(peerAddresses, id, config, dataPersistenceProvider, null));
148 public static Props props(final String id, final Map<String, String> peerAddresses,
149 Optional<ConfigParams> config, ActorRef roleChangeNotifier){
150 return Props.create(new MockRaftActorCreator(peerAddresses, id, config, null, roleChangeNotifier));
153 public static Props props(final String id, final Map<String, String> peerAddresses,
154 Optional<ConfigParams> config, ActorRef roleChangeNotifier,
155 DataPersistenceProvider dataPersistenceProvider){
156 return Props.create(new MockRaftActorCreator(peerAddresses, id, config, dataPersistenceProvider, roleChangeNotifier));
159 @Override protected void applyState(ActorRef clientActor, String identifier, Object data) {
160 actorDelegate.applyState(clientActor, identifier, data);
161 LOG.info("{}: applyState called: {}", persistenceId(), data);
168 protected RaftActorRecoveryCohort getRaftActorRecoveryCohort() {
173 protected RaftActorSnapshotCohort getRaftActorSnapshotCohort() {
178 public void startLogRecoveryBatch(int maxBatchSize) {
182 public void appendRecoveredLogEntry(Payload data) {
187 public void applyCurrentLogRecoveryBatch() {
191 protected void onRecoveryComplete() {
192 actorDelegate.onRecoveryComplete();
193 recoveryComplete.countDown();
197 protected void initializeBehavior() {
198 super.initializeBehavior();
199 initializeBehaviorComplete.countDown();
203 public void applyRecoverySnapshot(byte[] bytes) {
204 recoveryCohortDelegate.applyRecoverySnapshot(bytes);
205 applySnapshotBytes(bytes);
208 private void applySnapshotBytes(byte[] bytes) {
210 Object data = toObject(bytes);
211 if (data instanceof List) {
212 state.addAll((List<?>) data);
214 } catch (Exception e) {
220 public void createSnapshot(ActorRef actorRef) {
221 LOG.info("{}: createSnapshot called", persistenceId());
222 snapshotCohortDelegate.createSnapshot(actorRef);
226 public void applySnapshot(byte [] snapshot) {
227 LOG.info("{}: applySnapshot called", persistenceId());
228 snapshotCohortDelegate.applySnapshot(snapshot);
229 applySnapshotBytes(snapshot);
233 protected void onStateChanged() {
234 actorDelegate.onStateChanged();
238 protected Optional<ActorRef> getRoleChangeNotifier() {
239 return Optional.fromNullable(roleChangeNotifier);
242 @Override public String persistenceId() {
246 public static Object toObject(byte[] bs) throws ClassNotFoundException, IOException {
248 ByteArrayInputStream bis = null;
249 ObjectInputStream ois = null;
251 bis = new ByteArrayInputStream(bs);
252 ois = new ObjectInputStream(bis);
253 obj = ois.readObject();
265 public ReplicatedLog getReplicatedLog(){
266 return this.getRaftActorContext().getReplicatedLog();