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 com.google.common.base.Function;
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.Collections;
23 import java.util.List;
25 import java.util.concurrent.CountDownLatch;
26 import java.util.concurrent.TimeUnit;
27 import javax.annotation.Nonnull;
28 import org.opendaylight.controller.cluster.DataPersistenceProvider;
29 import org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior;
30 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
31 import org.opendaylight.yangtools.concepts.Identifier;
33 public class MockRaftActor extends RaftActor implements RaftActorRecoveryCohort, RaftActorSnapshotCohort {
34 public static final short PAYLOAD_VERSION = 5;
36 final RaftActor actorDelegate;
37 final RaftActorRecoveryCohort recoveryCohortDelegate;
38 volatile RaftActorSnapshotCohort snapshotCohortDelegate;
39 private final CountDownLatch recoveryComplete = new CountDownLatch(1);
40 private final List<Object> state;
41 private final ActorRef roleChangeNotifier;
42 protected final CountDownLatch initializeBehaviorComplete = new CountDownLatch(1);
43 private RaftActorRecoverySupport raftActorRecoverySupport;
44 private RaftActorSnapshotMessageSupport snapshotMessageSupport;
45 private final byte[] restoreFromSnapshot;
46 final CountDownLatch snapshotCommitted = new CountDownLatch(1);
47 private final Function<Runnable, Void> pauseLeaderFunction;
49 protected MockRaftActor(AbstractBuilder<?, ?> builder) {
50 super(builder.id, builder.peerAddresses != null ? builder.peerAddresses :
51 Collections.<String, String>emptyMap(), Optional.fromNullable(builder.config), PAYLOAD_VERSION);
52 state = new ArrayList<>();
53 this.actorDelegate = mock(RaftActor.class);
54 this.recoveryCohortDelegate = mock(RaftActorRecoveryCohort.class);
55 this.snapshotCohortDelegate = mock(RaftActorSnapshotCohort.class);
57 if(builder.dataPersistenceProvider == null){
58 setPersistence(builder.persistent.isPresent() ? builder.persistent.get() : true);
60 setPersistence(builder.dataPersistenceProvider);
63 roleChangeNotifier = builder.roleChangeNotifier;
64 snapshotMessageSupport = builder.snapshotMessageSupport;
65 restoreFromSnapshot = builder.restoreFromSnapshot;
66 pauseLeaderFunction = builder.pauseLeaderFunction;
69 public void setRaftActorRecoverySupport(RaftActorRecoverySupport support) {
70 raftActorRecoverySupport = support;
74 public RaftActorRecoverySupport newRaftActorRecoverySupport() {
75 return raftActorRecoverySupport != null ? raftActorRecoverySupport : super.newRaftActorRecoverySupport();
79 protected RaftActorSnapshotMessageSupport newRaftActorSnapshotMessageSupport() {
80 return snapshotMessageSupport != null ? snapshotMessageSupport :
81 (snapshotMessageSupport = super.newRaftActorSnapshotMessageSupport());
84 public RaftActorSnapshotMessageSupport getSnapshotMessageSupport() {
85 return snapshotMessageSupport;
88 public void waitForRecoveryComplete() {
90 assertEquals("Recovery complete", true, recoveryComplete.await(5, TimeUnit.SECONDS));
91 } catch (InterruptedException e) {
96 public void waitForInitializeBehaviorComplete() {
98 assertEquals("Behavior initialized", true, initializeBehaviorComplete.await(5, TimeUnit.SECONDS));
99 } catch (InterruptedException e) {
105 public void waitUntilLeader(){
106 for(int i = 0;i < 10; i++){
110 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
114 public List<Object> getState() {
119 protected void applyState(ActorRef clientActor, Identifier identifier, Object data) {
120 actorDelegate.applyState(clientActor, identifier, data);
121 LOG.info("{}: applyState called: {}", persistenceId(), data);
128 protected RaftActorRecoveryCohort getRaftActorRecoveryCohort() {
133 protected RaftActorSnapshotCohort getRaftActorSnapshotCohort() {
138 public void startLogRecoveryBatch(int maxBatchSize) {
142 public void appendRecoveredLogEntry(Payload data) {
147 public void applyCurrentLogRecoveryBatch() {
151 protected void onRecoveryComplete() {
152 actorDelegate.onRecoveryComplete();
153 recoveryComplete.countDown();
157 protected void initializeBehavior() {
158 super.initializeBehavior();
159 initializeBehaviorComplete.countDown();
163 public void applyRecoverySnapshot(byte[] bytes) {
164 recoveryCohortDelegate.applyRecoverySnapshot(bytes);
165 applySnapshotBytes(bytes);
168 private void applySnapshotBytes(byte[] bytes) {
170 Object data = toObject(bytes);
171 if (data instanceof List) {
173 state.addAll((List<?>) data);
175 } catch (Exception e) {
181 public void createSnapshot(ActorRef actorRef) {
182 LOG.info("{}: createSnapshot called", persistenceId());
183 snapshotCohortDelegate.createSnapshot(actorRef);
187 public void applySnapshot(byte [] snapshot) {
188 LOG.info("{}: applySnapshot called", persistenceId());
189 applySnapshotBytes(snapshot);
190 snapshotCohortDelegate.applySnapshot(snapshot);
194 protected void onStateChanged() {
195 actorDelegate.onStateChanged();
199 protected Optional<ActorRef> getRoleChangeNotifier() {
200 return Optional.fromNullable(roleChangeNotifier);
203 @Override public String persistenceId() {
207 protected void newBehavior(RaftActorBehavior newBehavior) {
208 self().tell(newBehavior, ActorRef.noSender());
212 protected void handleCommand(final Object message) {
213 if(message instanceof RaftActorBehavior) {
214 super.changeCurrentBehavior((RaftActorBehavior)message);
216 super.handleCommand(message);
218 if(RaftActorSnapshotMessageSupport.COMMIT_SNAPSHOT.equals(message)) {
219 snapshotCommitted.countDown();
225 protected void pauseLeader(Runnable operation) {
226 if(pauseLeaderFunction != null) {
227 pauseLeaderFunction.apply(operation);
229 super.pauseLeader(operation);
233 public static Object toObject(byte[] bs) throws ClassNotFoundException, IOException {
235 ByteArrayInputStream bis = null;
236 ObjectInputStream ois = null;
238 bis = new ByteArrayInputStream(bs);
239 ois = new ObjectInputStream(bis);
240 obj = ois.readObject();
252 public ReplicatedLog getReplicatedLog(){
253 return this.getRaftActorContext().getReplicatedLog();
257 public byte[] getRestoreFromSnapshot() {
258 return restoreFromSnapshot;
261 public static Props props(final String id, final Map<String, String> peerAddresses,
262 ConfigParams config){
263 return builder().id(id).peerAddresses(peerAddresses).config(config).props();
266 public static Props props(final String id, final Map<String, String> peerAddresses,
267 ConfigParams config, DataPersistenceProvider dataPersistenceProvider){
268 return builder().id(id).peerAddresses(peerAddresses).config(config).
269 dataPersistenceProvider(dataPersistenceProvider).props();
272 public static Builder builder() {
273 return new Builder();
276 public static class AbstractBuilder<T extends AbstractBuilder<T, A>, A extends MockRaftActor> {
277 private Map<String, String> peerAddresses = Collections.emptyMap();
279 private ConfigParams config;
280 private DataPersistenceProvider dataPersistenceProvider;
281 private ActorRef roleChangeNotifier;
282 private RaftActorSnapshotMessageSupport snapshotMessageSupport;
283 private byte[] restoreFromSnapshot;
284 private Optional<Boolean> persistent = Optional.absent();
285 private final Class<A> actorClass;
286 private Function<Runnable, Void> pauseLeaderFunction;
288 protected AbstractBuilder(Class<A> actorClass) {
289 this.actorClass = actorClass;
292 @SuppressWarnings("unchecked")
297 public T id(String id) {
302 public T peerAddresses(Map<String, String> peerAddresses) {
303 this.peerAddresses = peerAddresses;
307 public T config(ConfigParams config) {
308 this.config = config;
312 public T dataPersistenceProvider(DataPersistenceProvider dataPersistenceProvider) {
313 this.dataPersistenceProvider = dataPersistenceProvider;
317 public T roleChangeNotifier(ActorRef roleChangeNotifier) {
318 this.roleChangeNotifier = roleChangeNotifier;
322 public T snapshotMessageSupport(RaftActorSnapshotMessageSupport snapshotMessageSupport) {
323 this.snapshotMessageSupport = snapshotMessageSupport;
327 public T restoreFromSnapshot(byte[] restoreFromSnapshot) {
328 this.restoreFromSnapshot = restoreFromSnapshot;
332 public T persistent(Optional<Boolean> persistent) {
333 this.persistent = persistent;
337 public T pauseLeaderFunction(Function<Runnable, Void> pauseLeaderFunction) {
338 this.pauseLeaderFunction = pauseLeaderFunction;
342 public Props props() {
343 return Props.create(actorClass, this);
347 public static class Builder extends AbstractBuilder<Builder, MockRaftActor> {
349 super(MockRaftActor.class);