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.protobuf.GeneratedMessage;
17 import java.io.Serializable;
18 import java.util.HashMap;
20 import org.opendaylight.controller.cluster.NonPersistentDataProvider;
21 import org.opendaylight.controller.cluster.raft.policy.RaftPolicy;
22 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
23 import org.opendaylight.controller.protobuff.messages.cluster.raft.AppendEntriesMessages;
24 import org.opendaylight.controller.protobuff.messages.cluster.raft.test.MockPayloadMessages;
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 currentTerm, String votedFor){
51 this.currentTerm = currentTerm;
52 this.votedFor = votedFor;
54 // TODO : Write to some persistent state
57 @Override public void updateAndPersist(long currentTerm,
59 update(currentTerm, votedFor);
64 public MockRaftActorContext(){
65 super(null, null, "test", newElectionTerm(), -1, -1, new HashMap<String, String>(),
66 new DefaultConfigParamsImpl(), new NonPersistentDataProvider(), LOG);
67 setReplicatedLog(new MockReplicatedLogBuilder().build());
70 public MockRaftActorContext(String id, ActorSystem system, ActorRef actor){
71 super(actor, null, id, newElectionTerm(), -1, -1, new HashMap<String, String>(),
72 new DefaultConfigParamsImpl(), new NonPersistentDataProvider(), LOG);
80 public void initReplicatedLog(){
81 SimpleReplicatedLog replicatedLog = new SimpleReplicatedLog();
82 long term = getTermInformation().getCurrentTerm();
83 replicatedLog.append(new MockReplicatedLogEntry(term, 0, new MockPayload("1")));
84 replicatedLog.append(new MockReplicatedLogEntry(term, 1, new MockPayload("2")));
85 setReplicatedLog(replicatedLog);
88 @Override public ActorRef actorOf(Props props) {
89 return system.actorOf(props);
92 @Override public ActorSelection actorSelection(String path) {
93 return system.actorSelection(path);
96 @Override public ActorSystem getActorSystem() {
100 @Override public ActorSelection getPeerActorSelection(String peerId) {
101 String peerAddress = getPeerAddress(peerId);
102 if(peerAddress != null){
103 return actorSelection(peerAddress);
108 public void setPeerAddresses(Map<String, String> peerAddresses) {
109 for(String id: getPeerIds()) {
113 for(Map.Entry<String, String> e: peerAddresses.entrySet()) {
114 addToPeers(e.getKey(), e.getValue(), VotingState.VOTING);
119 public SnapshotManager getSnapshotManager() {
120 SnapshotManager snapshotManager = super.getSnapshotManager();
121 snapshotManager.setCreateSnapshotCallable(NoopProcedure.<Void>instance());
122 return snapshotManager;
126 public RaftPolicy getRaftPolicy() {
127 return raftPolicy != null ? raftPolicy : super.getRaftPolicy();
130 public void setRaftPolicy(RaftPolicy raftPolicy) {
131 this.raftPolicy = raftPolicy;
134 public static class SimpleReplicatedLog extends AbstractReplicatedLogImpl {
136 public void appendAndPersist(
137 ReplicatedLogEntry replicatedLogEntry) {
138 append(replicatedLogEntry);
142 public int dataSize() {
147 public void captureSnapshotIfReady(ReplicatedLogEntry replicatedLogEntry) {
150 @Override public void removeFromAndPersist(long index) {
155 public void appendAndPersist(ReplicatedLogEntry replicatedLogEntry, Procedure<ReplicatedLogEntry> callback) {
156 append(replicatedLogEntry);
158 if(callback != null) {
160 callback.apply(replicatedLogEntry);
161 } catch (Exception 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 s) {
178 size = value.length();
181 public MockPayload(String s, int size) {
186 @Override public Map<GeneratedMessage.GeneratedExtension<?, ?>, String> encode() {
187 Map<GeneratedMessage.GeneratedExtension<?, ?>, String> map = new HashMap<>();
188 map.put(MockPayloadMessages.value, value);
192 @Override public Payload decode(
193 AppendEntriesMessages.AppendEntries.ReplicatedLogEntry.Payload payloadProtoBuff) {
194 String value = payloadProtoBuff.getExtension(MockPayloadMessages.value);
204 @Override public String getClientPayloadClassName() {
205 return MockPayload.class.getName();
209 public String toString() {
214 public int hashCode() {
215 final int prime = 31;
217 result = prime * result + ((value == null) ? 0 : value.hashCode());
222 public boolean equals(Object obj) {
229 if (getClass() != obj.getClass()) {
232 MockPayload other = (MockPayload) obj;
234 if (other.value != null) {
237 } else if (!value.equals(other.value)) {
244 public static class MockReplicatedLogEntry implements ReplicatedLogEntry, Serializable {
245 private static final long serialVersionUID = 1L;
247 private final long term;
248 private final long index;
249 private final Payload data;
251 public MockReplicatedLogEntry(long term, long index, Payload data){
258 @Override public Payload getData() {
262 @Override public long getTerm() {
266 @Override public long getIndex() {
272 return getData().size();
276 public int hashCode() {
277 final int prime = 31;
279 result = prime * result + ((data == null) ? 0 : data.hashCode());
280 result = prime * result + (int) (index ^ (index >>> 32));
281 result = prime * result + (int) (term ^ (term >>> 32));
286 public boolean equals(Object obj) {
293 if (getClass() != obj.getClass()) {
296 MockReplicatedLogEntry other = (MockReplicatedLogEntry) obj;
298 if (other.data != null) {
301 } else if (!data.equals(other.data)) {
304 if (index != other.index) {
307 if (term != other.term) {
314 public String toString() {
315 StringBuilder builder = new StringBuilder();
316 builder.append("MockReplicatedLogEntry [term=").append(term).append(", index=").append(index)
317 .append(", data=").append(data).append("]");
318 return builder.toString();
322 public static class MockReplicatedLogBuilder {
323 private final ReplicatedLog mockLog = new SimpleReplicatedLog();
325 public MockReplicatedLogBuilder createEntries(int start, int end, int term) {
326 for (int i=start; i<end; i++) {
327 this.mockLog.append(new ReplicatedLogImplEntry(i, term, new MockRaftActorContext.MockPayload(Integer.toString(i))));
332 public MockReplicatedLogBuilder addEntry(int index, int term, MockPayload payload) {
333 this.mockLog.append(new ReplicatedLogImplEntry(index, term, payload));
337 public ReplicatedLog build() {