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(), 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(), LOG);
79 public void initReplicatedLog() {
80 SimpleReplicatedLog replicatedLog = new SimpleReplicatedLog();
81 long term = getTermInformation().getCurrentTerm();
82 replicatedLog.append(new SimpleReplicatedLogEntry(0, term, new MockPayload("1")));
83 replicatedLog.append(new SimpleReplicatedLogEntry(1, term, new MockPayload("2")));
84 setReplicatedLog(replicatedLog);
85 setCommitIndex(replicatedLog.lastIndex());
86 setLastApplied(replicatedLog.lastIndex());
89 @Override public ActorRef actorOf(Props props) {
90 return system.actorOf(props);
93 @Override public ActorSelection actorSelection(String path) {
94 return system.actorSelection(path);
97 @Override public ActorSystem getActorSystem() {
101 @Override public ActorSelection getPeerActorSelection(String peerId) {
102 String peerAddress = getPeerAddress(peerId);
103 if (peerAddress != null) {
104 return actorSelection(peerAddress);
109 public void setPeerAddresses(Map<String, String> peerAddresses) {
110 for (String id: getPeerIds()) {
114 for (Map.Entry<String, String> e: peerAddresses.entrySet()) {
115 addToPeers(e.getKey(), e.getValue(), VotingState.VOTING);
120 public SnapshotManager getSnapshotManager() {
121 SnapshotManager snapshotManager = super.getSnapshotManager();
122 snapshotManager.setCreateSnapshotRunnable(() -> { });
123 return snapshotManager;
127 public RaftPolicy getRaftPolicy() {
128 return raftPolicy != null ? raftPolicy : super.getRaftPolicy();
131 public void setRaftPolicy(RaftPolicy raftPolicy) {
132 this.raftPolicy = raftPolicy;
135 public static class SimpleReplicatedLog extends AbstractReplicatedLogImpl {
137 public int dataSize() {
142 public void captureSnapshotIfReady(ReplicatedLogEntry replicatedLogEntry) {
146 public boolean removeFromAndPersist(long index) {
147 return removeFrom(index) >= 0;
151 @SuppressWarnings("checkstyle:IllegalCatch")
152 public boolean appendAndPersist(ReplicatedLogEntry replicatedLogEntry, Procedure<ReplicatedLogEntry> callback,
154 append(replicatedLogEntry);
156 if (callback != null) {
158 callback.apply(replicatedLogEntry);
159 } catch (Exception e) {
160 Throwables.propagate(e);
168 public static class MockPayload extends Payload implements Serializable {
169 private static final long serialVersionUID = 3121380393130864247L;
170 private String value = "";
173 public MockPayload() {
176 public MockPayload(String data) {
178 size = value.length();
181 public MockPayload(String data, int size) {
192 public String toString() {
197 public int hashCode() {
198 final int prime = 31;
200 result = prime * result + (value == null ? 0 : value.hashCode());
205 public boolean equals(Object obj) {
212 if (getClass() != obj.getClass()) {
215 MockPayload other = (MockPayload) obj;
217 if (other.value != null) {
220 } else if (!value.equals(other.value)) {
227 public static class MockReplicatedLogBuilder {
228 private final ReplicatedLog mockLog = new SimpleReplicatedLog();
230 public MockReplicatedLogBuilder createEntries(int start, int end, int term) {
231 for (int i = start; i < end; i++) {
232 this.mockLog.append(new SimpleReplicatedLogEntry(i, term,
233 new MockRaftActorContext.MockPayload(Integer.toString(i))));
238 public MockReplicatedLogBuilder addEntry(int index, int term, MockPayload payload) {
239 this.mockLog.append(new SimpleReplicatedLogEntry(index, term, payload));
243 public ReplicatedLog build() {
249 public void setCurrentBehavior(final RaftActorBehavior behavior) {
250 super.setCurrentBehavior(behavior);