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 com.google.common.base.Preconditions;
16 import com.google.protobuf.GeneratedMessage;
17 import java.io.Serializable;
18 import java.util.HashMap;
20 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
21 import org.opendaylight.controller.protobuff.messages.cluster.raft.AppendEntriesMessages;
22 import org.opendaylight.controller.protobuff.messages.cluster.raft.test.MockPayloadMessages;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 public class MockRaftActorContext implements RaftActorContext {
29 private ActorSystem system;
30 private ActorRef actor;
31 private long index = 0;
32 private long lastApplied = 0;
33 private final ElectionTerm electionTerm;
34 private ReplicatedLog replicatedLog;
35 private Map<String, String> peerAddresses = new HashMap<>();
36 private ConfigParams configParams;
37 private boolean snapshotCaptureInitiated;
39 public MockRaftActorContext(){
45 public MockRaftActorContext(String id, ActorSystem system, ActorRef actor){
50 final String id1 = id;
51 electionTerm = new ElectionTerm() {
53 * Identifier of the actor whose election term information this is
55 private final String id = id1;
56 private long currentTerm = 1;
57 private String votedFor = "";
60 public long getCurrentTerm() {
65 public String getVotedFor() {
70 public void update(long currentTerm, String votedFor){
71 this.currentTerm = currentTerm;
72 this.votedFor = votedFor;
74 // TODO : Write to some persistent state
77 @Override public void updateAndPersist(long currentTerm,
79 update(currentTerm, votedFor);
83 configParams = new DefaultConfigParamsImpl();
89 public void initReplicatedLog(){
90 this.replicatedLog = new SimpleReplicatedLog();
91 long term = getTermInformation().getCurrentTerm();
92 this.replicatedLog.append(new MockReplicatedLogEntry(term, 0, new MockPayload("1")));
93 this.replicatedLog.append(new MockReplicatedLogEntry(term, 1, new MockPayload("2")));
96 @Override public ActorRef actorOf(Props props) {
97 return system.actorOf(props);
100 @Override public ActorSelection actorSelection(String path) {
101 return system.actorSelection(path);
104 @Override public String getId() {
108 @Override public ActorRef getActor() {
112 @Override public ElectionTerm getTermInformation() {
116 public void setIndex(long index){
120 @Override public long getCommitIndex() {
124 @Override public void setCommitIndex(long commitIndex) {
125 this.index = commitIndex;
128 @Override public void setLastApplied(long lastApplied){
129 this.lastApplied = lastApplied;
132 @Override public long getLastApplied() {
137 // FIXME : A lot of tests try to manipulate the replicated log by setting it using this method
138 // This is OK to do if the underlyingActor is not RafActor or a derived class. If not then you should not
139 // used this way to manipulate the log because the RaftActor actually has a field replicatedLog
140 // which it creates internally and sets on the RaftActorContext
141 // The only right way to manipulate the replicated log therefore is to get it from either the RaftActor
142 // or the RaftActorContext and modify the entries in there instead of trying to replace it by using this setter
143 // Simple assertion that will fail if you do so
144 // ReplicatedLog log = new ReplicatedLogImpl();
145 // raftActor.underlyingActor().getRaftActorContext().setReplicatedLog(log);
146 // assertEquals(log, raftActor.underlyingActor().getReplicatedLog())
147 public void setReplicatedLog(ReplicatedLog replicatedLog) {
148 this.replicatedLog = replicatedLog;
151 @Override public ReplicatedLog getReplicatedLog() {
152 return replicatedLog;
155 @Override public ActorSystem getActorSystem() {
159 @Override public Logger getLogger() {
160 return LoggerFactory.getLogger(getClass());
163 @Override public Map<String, String> getPeerAddresses() {
164 return peerAddresses;
167 @Override public String getPeerAddress(String peerId) {
168 return peerAddresses.get(peerId);
171 @Override public void addToPeers(String name, String address) {
172 peerAddresses.put(name, address);
175 @Override public void removePeer(String name) {
176 peerAddresses.remove(name);
179 @Override public ActorSelection getPeerActorSelection(String peerId) {
180 String peerAddress = getPeerAddress(peerId);
181 if(peerAddress != null){
182 return actorSelection(peerAddress);
187 @Override public void setPeerAddress(String peerId, String peerAddress) {
188 Preconditions.checkState(peerAddresses.containsKey(peerId));
189 peerAddresses.put(peerId, peerAddress);
192 public void setPeerAddresses(Map<String, String> peerAddresses) {
193 this.peerAddresses = peerAddresses;
197 public ConfigParams getConfigParams() {
202 public void setSnapshotCaptureInitiated(boolean snapshotCaptureInitiated) {
203 this.snapshotCaptureInitiated = snapshotCaptureInitiated;
207 public boolean isSnapshotCaptureInitiated() {
208 return snapshotCaptureInitiated;
211 public void setConfigParams(ConfigParams configParams) {
212 this.configParams = configParams;
215 public static class SimpleReplicatedLog extends AbstractReplicatedLogImpl {
216 @Override public void appendAndPersist(
217 ReplicatedLogEntry replicatedLogEntry) {
218 append(replicatedLogEntry);
222 public int dataSize() {
226 @Override public void removeFromAndPersist(long index) {
231 public static class MockPayload extends Payload implements Serializable {
232 private static final long serialVersionUID = 3121380393130864247L;
233 private String value = "";
235 public MockPayload(){
239 public MockPayload(String s) {
243 @Override public Map<GeneratedMessage.GeneratedExtension, String> encode() {
244 Map<GeneratedMessage.GeneratedExtension, String> map = new HashMap<GeneratedMessage.GeneratedExtension, String>();
245 map.put(MockPayloadMessages.value, value);
249 @Override public Payload decode(
250 AppendEntriesMessages.AppendEntries.ReplicatedLogEntry.Payload payloadProtoBuff) {
251 String value = payloadProtoBuff.getExtension(MockPayloadMessages.value);
258 return value.length();
261 @Override public String getClientPayloadClassName() {
262 return MockPayload.class.getName();
266 public String toString() {
271 public int hashCode() {
272 final int prime = 31;
274 result = prime * result + ((value == null) ? 0 : value.hashCode());
279 public boolean equals(Object obj) {
286 if (getClass() != obj.getClass()) {
289 MockPayload other = (MockPayload) obj;
291 if (other.value != null) {
294 } else if (!value.equals(other.value)) {
301 public static class MockReplicatedLogEntry implements ReplicatedLogEntry, Serializable {
302 private static final long serialVersionUID = 1L;
304 private final long term;
305 private final long index;
306 private final Payload data;
308 public MockReplicatedLogEntry(long term, long index, Payload data){
315 @Override public Payload getData() {
319 @Override public long getTerm() {
323 @Override public long getIndex() {
329 return getData().size();
333 public int hashCode() {
334 final int prime = 31;
336 result = prime * result + ((data == null) ? 0 : data.hashCode());
337 result = prime * result + (int) (index ^ (index >>> 32));
338 result = prime * result + (int) (term ^ (term >>> 32));
343 public boolean equals(Object obj) {
350 if (getClass() != obj.getClass()) {
353 MockReplicatedLogEntry other = (MockReplicatedLogEntry) obj;
355 if (other.data != null) {
358 } else if (!data.equals(other.data)) {
361 if (index != other.index) {
364 if (term != other.term) {
371 public String toString() {
372 StringBuilder builder = new StringBuilder();
373 builder.append("MockReplicatedLogEntry [term=").append(term).append(", index=").append(index)
374 .append(", data=").append(data).append("]");
375 return builder.toString();
379 public static class MockReplicatedLogBuilder {
380 private final ReplicatedLog mockLog = new SimpleReplicatedLog();
382 public MockReplicatedLogBuilder createEntries(int start, int end, int term) {
383 for (int i=start; i<end; i++) {
384 this.mockLog.append(new ReplicatedLogImplEntry(i, term, new MockRaftActorContext.MockPayload("foo" + i)));
389 public MockReplicatedLogBuilder addEntry(int index, int term, MockPayload payload) {
390 this.mockLog.append(new ReplicatedLogImplEntry(index, term, payload));
394 public ReplicatedLog build() {