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.dummy.datastore;
11 import akka.actor.Props;
12 import akka.actor.UntypedActor;
13 import akka.japi.Creator;
14 import com.google.common.base.Stopwatch;
15 import java.util.concurrent.TimeUnit;
16 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
17 import org.opendaylight.controller.cluster.raft.messages.AppendEntries;
18 import org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply;
19 import org.opendaylight.controller.cluster.raft.messages.InstallSnapshot;
20 import org.opendaylight.controller.cluster.raft.messages.InstallSnapshotReply;
21 import org.opendaylight.controller.cluster.raft.messages.RequestVote;
22 import org.opendaylight.controller.cluster.raft.messages.RequestVoteReply;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 public class DummyShard extends UntypedActor{
27 private final Configuration configuration;
28 private final String followerId;
29 private final Logger LOG = LoggerFactory.getLogger(DummyShard.class);
30 private long lastMessageIndex = -1;
31 private long lastMessageSize = 0;
32 private Stopwatch appendEntriesWatch;
34 public DummyShard(Configuration configuration, String followerId) {
35 this.configuration = configuration;
36 this.followerId = followerId;
37 LOG.info("Creating : {}", followerId);
41 public void onReceive(Object o) throws Exception {
42 if(o instanceof RequestVote){
43 RequestVote req = (RequestVote) o;
44 sender().tell(new RequestVoteReply(req.getTerm(), true), self());
45 } else if(AppendEntries.LEGACY_SERIALIZABLE_CLASS.equals(o.getClass()) || o instanceof AppendEntries) {
46 AppendEntries req = AppendEntries.fromSerializable(o);
47 handleAppendEntries(req);
48 } else if(InstallSnapshot.SERIALIZABLE_CLASS.equals(o.getClass())) {
49 InstallSnapshot req = InstallSnapshot.fromSerializable(o);
50 handleInstallSnapshot(req);
51 } else if(o instanceof InstallSnapshot){
52 handleInstallSnapshot((InstallSnapshot) o);
54 LOG.error("Unknown message : {}", o.getClass());
58 private void handleInstallSnapshot(InstallSnapshot req) {
59 sender().tell(new InstallSnapshotReply(req.getTerm(), followerId, req.getChunkIndex(), true), self());
62 protected void handleAppendEntries(AppendEntries req) throws InterruptedException {
63 LOG.info("{} - Received AppendEntries message : leader term = {}, index = {}, prevLogIndex = {}, size = {}",
64 followerId, req.getTerm(),req.getLeaderCommit(), req.getPrevLogIndex(), req.getEntries().size());
66 if(appendEntriesWatch != null){
67 long elapsed = appendEntriesWatch.elapsed(TimeUnit.SECONDS);
69 LOG.error("More than 5 seconds since last append entry, elapsed Time = {} seconds" +
70 ", leaderCommit = {}, prevLogIndex = {}, size = {}",
71 elapsed, req.getLeaderCommit(), req.getPrevLogIndex(), req.getEntries().size());
73 appendEntriesWatch.reset().start();
75 appendEntriesWatch = Stopwatch.createStarted();
78 if(lastMessageIndex == req.getLeaderCommit() && req.getEntries().size() > 0 && lastMessageSize > 0){
79 LOG.error("{} - Duplicate message with leaderCommit = {} prevLogIndex = {} received", followerId, req.getLeaderCommit(), req.getPrevLogIndex());
82 lastMessageIndex = req.getLeaderCommit();
83 lastMessageSize = req.getEntries().size();
85 long lastIndex = req.getLeaderCommit();
86 if (req.getEntries().size() > 0) {
87 for(ReplicatedLogEntry entry : req.getEntries()) {
88 lastIndex = entry.getIndex();
92 if (configuration.shouldCauseTrouble() && req.getEntries().size() > 0) {
93 boolean ignore = false;
95 if (configuration.shouldDropReplies()) {
96 ignore = Math.random() > 0.5;
99 long delay = (long) (Math.random() * configuration.getMaxDelayInMillis());
102 LOG.info("{} - Randomizing delay : {}", followerId, delay);
104 sender().tell(new AppendEntriesReply(followerId, req.getTerm(), true, lastIndex, req.getTerm()), self());
107 sender().tell(new AppendEntriesReply(followerId, req.getTerm(), true, lastIndex, req.getTerm()), self());
111 public static Props props(Configuration configuration, final String followerId) {
113 return Props.create(new DummyShardCreator(configuration, followerId));
116 private static class DummyShardCreator implements Creator<DummyShard> {
118 private static final long serialVersionUID = 1L;
119 private final Configuration configuration;
120 private final String followerId;
122 DummyShardCreator(Configuration configuration, String followerId) {
123 this.configuration = configuration;
124 this.followerId = followerId;
128 public DummyShard create() throws Exception {
129 return new DummyShard(configuration, followerId);