2 * Copyright (c) 2014 Cisco Systems, Inc. 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
9 package org.opendaylight.controller.cluster.raft;
11 import akka.actor.ActorRef;
12 import akka.actor.ActorSelection;
13 import akka.actor.ActorSystem;
14 import akka.actor.Props;
15 import akka.japi.Procedure;
16 import com.google.common.base.Throwables;
17 import java.io.Serializable;
18 import java.util.HashMap;
20 import org.opendaylight.controller.cluster.NonPersistentDataProvider;
21 import org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior;
22 import org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry;
23 import org.opendaylight.controller.cluster.raft.policy.RaftPolicy;
24 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 public class MockRaftActorContext extends RaftActorContextImpl {
29 private static final Logger LOG = LoggerFactory.getLogger(MockRaftActorContext.class);
31 private ActorSystem system;
32 private RaftPolicy raftPolicy;
34 private static ElectionTerm newElectionTerm() {
35 return new ElectionTerm() {
36 private long currentTerm = 1;
37 private String votedFor = "";
40 public long getCurrentTerm() {
45 public String getVotedFor() {
50 public void update(long newTerm, String newVotedFor) {
51 this.currentTerm = newTerm;
52 this.votedFor = newVotedFor;
54 // TODO : Write to some persistent state
57 @Override public void updateAndPersist(long newTerm, String newVotedFor) {
58 update(newTerm, newVotedFor);
63 public MockRaftActorContext() {
64 super(null, null, "test", newElectionTerm(), -1, -1, new HashMap<>(),
65 new DefaultConfigParamsImpl(), new NonPersistentDataProvider(), applyState -> { }, LOG);
66 setReplicatedLog(new MockReplicatedLogBuilder().build());
69 public MockRaftActorContext(String id, ActorSystem system, ActorRef actor) {
70 super(actor, null, id, newElectionTerm(), -1, -1, new HashMap<>(),
71 new DefaultConfigParamsImpl(), new NonPersistentDataProvider(),
72 applyState -> actor.tell(applyState, actor), LOG);
80 public void initReplicatedLog() {
81 SimpleReplicatedLog replicatedLog = new SimpleReplicatedLog();
82 long term = getTermInformation().getCurrentTerm();
83 replicatedLog.append(new SimpleReplicatedLogEntry(0, term, new MockPayload("1")));
84 replicatedLog.append(new SimpleReplicatedLogEntry(1, term, new MockPayload("2")));
85 setReplicatedLog(replicatedLog);
86 setCommitIndex(replicatedLog.lastIndex());
87 setLastApplied(replicatedLog.lastIndex());
90 @Override public ActorRef actorOf(Props props) {
91 return system.actorOf(props);
94 @Override public ActorSelection actorSelection(String path) {
95 return system.actorSelection(path);
98 @Override public ActorSystem getActorSystem() {
102 @Override public ActorSelection getPeerActorSelection(String peerId) {
103 String peerAddress = getPeerAddress(peerId);
104 if (peerAddress != null) {
105 return actorSelection(peerAddress);
110 public void setPeerAddresses(Map<String, String> peerAddresses) {
111 for (String id: getPeerIds()) {
115 for (Map.Entry<String, String> e: peerAddresses.entrySet()) {
116 addToPeers(e.getKey(), e.getValue(), VotingState.VOTING);
121 public SnapshotManager getSnapshotManager() {
122 SnapshotManager snapshotManager = super.getSnapshotManager();
123 snapshotManager.setCreateSnapshotRunnable(() -> { });
124 return snapshotManager;
128 public RaftPolicy getRaftPolicy() {
129 return raftPolicy != null ? raftPolicy : super.getRaftPolicy();
132 public void setRaftPolicy(RaftPolicy raftPolicy) {
133 this.raftPolicy = raftPolicy;
136 public static class SimpleReplicatedLog extends AbstractReplicatedLogImpl {
138 public int dataSize() {
143 public void captureSnapshotIfReady(ReplicatedLogEntry replicatedLogEntry) {
147 public boolean shouldCaptureSnapshot(long logIndex) {
152 public boolean removeFromAndPersist(long index) {
153 return removeFrom(index) >= 0;
157 @SuppressWarnings("checkstyle:IllegalCatch")
158 public boolean appendAndPersist(ReplicatedLogEntry replicatedLogEntry, Procedure<ReplicatedLogEntry> callback,
160 append(replicatedLogEntry);
162 if (callback != null) {
164 callback.apply(replicatedLogEntry);
165 } catch (Exception e) {
166 Throwables.propagate(e);
174 public static class MockPayload extends Payload implements Serializable {
175 private static final long serialVersionUID = 3121380393130864247L;
176 private String value = "";
179 public MockPayload() {
182 public MockPayload(String data) {
184 size = value.length();
187 public MockPayload(String data, int size) {
198 public String toString() {
203 public int hashCode() {
204 final int prime = 31;
206 result = prime * result + (value == null ? 0 : value.hashCode());
211 public boolean equals(Object obj) {
218 if (getClass() != obj.getClass()) {
221 MockPayload other = (MockPayload) obj;
223 if (other.value != null) {
226 } else if (!value.equals(other.value)) {
233 public static class MockReplicatedLogBuilder {
234 private final ReplicatedLog mockLog = new SimpleReplicatedLog();
236 public MockReplicatedLogBuilder createEntries(int start, int end, int term) {
237 for (int i = start; i < end; i++) {
238 this.mockLog.append(new SimpleReplicatedLogEntry(i, term,
239 new MockRaftActorContext.MockPayload(Integer.toString(i))));
244 public MockReplicatedLogBuilder addEntry(int index, int term, MockPayload payload) {
245 this.mockLog.append(new SimpleReplicatedLogEntry(index, term, payload));
249 public ReplicatedLog build() {
255 public void setCurrentBehavior(final RaftActorBehavior behavior) {
256 super.setCurrentBehavior(behavior);