ad7836347d27578f6801dfec643834cab4a76f7a
[controller.git] / opendaylight / md-sal / sal-dummy-distributed-datastore / src / main / java / org / opendaylight / controller / dummy / datastore / DummyShard.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.dummy.datastore;
10
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.datastore.DataStoreVersions;
17 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
18 import org.opendaylight.controller.cluster.raft.messages.AppendEntries;
19 import org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply;
20 import org.opendaylight.controller.cluster.raft.messages.InstallSnapshot;
21 import org.opendaylight.controller.cluster.raft.messages.InstallSnapshotReply;
22 import org.opendaylight.controller.cluster.raft.messages.RequestVote;
23 import org.opendaylight.controller.cluster.raft.messages.RequestVoteReply;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public class DummyShard extends UntypedActor{
28     private final Configuration configuration;
29     private final String followerId;
30     private final Logger LOG = LoggerFactory.getLogger(DummyShard.class);
31     private long lastMessageIndex  = -1;
32     private long lastMessageSize = 0;
33     private Stopwatch appendEntriesWatch;
34
35     public DummyShard(Configuration configuration, String followerId) {
36         this.configuration = configuration;
37         this.followerId = followerId;
38         LOG.info("Creating : {}", followerId);
39     }
40
41     @Override
42     public void onReceive(Object o) throws Exception {
43         if(o instanceof RequestVote){
44             RequestVote req = (RequestVote) o;
45             sender().tell(new RequestVoteReply(req.getTerm(), true), self());
46         } else if(o instanceof AppendEntries) {
47             handleAppendEntries((AppendEntries)o);
48         } else if(InstallSnapshot.isSerializedType(o)) {
49             InstallSnapshot req = InstallSnapshot.fromSerializable(o);
50             handleInstallSnapshot(req);
51         } else if(o instanceof InstallSnapshot){
52             handleInstallSnapshot((InstallSnapshot) o);
53         } else {
54             LOG.error("Unknown message : {}", o.getClass());
55         }
56     }
57
58     private void handleInstallSnapshot(InstallSnapshot req) {
59         sender().tell(new InstallSnapshotReply(req.getTerm(), followerId, req.getChunkIndex(), true), self());
60     }
61
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());
65
66         if(appendEntriesWatch != null){
67             long elapsed = appendEntriesWatch.elapsed(TimeUnit.SECONDS);
68             if(elapsed >= 5){
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());
72             }
73             appendEntriesWatch.reset().start();
74         } else {
75             appendEntriesWatch = Stopwatch.createStarted();
76         }
77
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());
80         }
81
82         lastMessageIndex = req.getLeaderCommit();
83         lastMessageSize = req.getEntries().size();
84
85         long lastIndex = req.getLeaderCommit();
86         if (req.getEntries().size() > 0) {
87             for(ReplicatedLogEntry entry : req.getEntries()) {
88                 lastIndex = entry.getIndex();
89             }
90         }
91
92         if (configuration.shouldCauseTrouble() && req.getEntries().size() > 0) {
93             boolean ignore = false;
94
95             if (configuration.shouldDropReplies()) {
96                 ignore = Math.random() > 0.5;
97             }
98
99             long delay = (long) (Math.random() * configuration.getMaxDelayInMillis());
100
101             if (!ignore) {
102                 LOG.info("{} - Randomizing delay : {}", followerId, delay);
103                 Thread.sleep(delay);
104                 sender().tell(new AppendEntriesReply(followerId, req.getTerm(), true, lastIndex, req.getTerm(),
105                         DataStoreVersions.CURRENT_VERSION), self());
106             }
107         } else {
108             sender().tell(new AppendEntriesReply(followerId, req.getTerm(), true, lastIndex, req.getTerm(),
109                     DataStoreVersions.CURRENT_VERSION), self());
110         }
111     }
112
113     public static Props props(Configuration configuration, final String followerId) {
114
115         return Props.create(new DummyShardCreator(configuration, followerId));
116     }
117
118     private static class DummyShardCreator implements Creator<DummyShard> {
119
120         private static final long serialVersionUID = 1L;
121         private final Configuration configuration;
122         private final String followerId;
123
124         DummyShardCreator(Configuration configuration, String followerId) {
125             this.configuration = configuration;
126             this.followerId = followerId;
127         }
128
129         @Override
130         public DummyShard create() throws Exception {
131             return new DummyShard(configuration, followerId);
132         }
133     }
134
135 }