2cd5b97ceb45c3b9c2755337ce09f06932fc93ca
[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(o instanceof InstallSnapshot){
49             handleInstallSnapshot((InstallSnapshot) o);
50         } else {
51             LOG.error("Unknown message : {}", o.getClass());
52         }
53     }
54
55     private void handleInstallSnapshot(InstallSnapshot req) {
56         sender().tell(new InstallSnapshotReply(req.getTerm(), followerId, req.getChunkIndex(), true), self());
57     }
58
59     protected void handleAppendEntries(AppendEntries req) throws InterruptedException {
60         LOG.info("{} - Received AppendEntries message : leader term = {}, index = {}, prevLogIndex = {}, size = {}",
61                 followerId, req.getTerm(),req.getLeaderCommit(), req.getPrevLogIndex(), req.getEntries().size());
62
63         if(appendEntriesWatch != null){
64             long elapsed = appendEntriesWatch.elapsed(TimeUnit.SECONDS);
65             if(elapsed >= 5){
66                 LOG.error("More than 5 seconds since last append entry, elapsed Time = {} seconds" +
67                                 ", leaderCommit = {}, prevLogIndex = {}, size = {}",
68                         elapsed, req.getLeaderCommit(), req.getPrevLogIndex(), req.getEntries().size());
69             }
70             appendEntriesWatch.reset().start();
71         } else {
72             appendEntriesWatch = Stopwatch.createStarted();
73         }
74
75         if(lastMessageIndex == req.getLeaderCommit() && req.getEntries().size() > 0 && lastMessageSize > 0){
76             LOG.error("{} - Duplicate message with leaderCommit = {} prevLogIndex = {} received", followerId, req.getLeaderCommit(), req.getPrevLogIndex());
77         }
78
79         lastMessageIndex = req.getLeaderCommit();
80         lastMessageSize = req.getEntries().size();
81
82         long lastIndex = req.getLeaderCommit();
83         if (req.getEntries().size() > 0) {
84             for(ReplicatedLogEntry entry : req.getEntries()) {
85                 lastIndex = entry.getIndex();
86             }
87         }
88
89         if (configuration.shouldCauseTrouble() && req.getEntries().size() > 0) {
90             boolean ignore = false;
91
92             if (configuration.shouldDropReplies()) {
93                 ignore = Math.random() > 0.5;
94             }
95
96             long delay = (long) (Math.random() * configuration.getMaxDelayInMillis());
97
98             if (!ignore) {
99                 LOG.info("{} - Randomizing delay : {}", followerId, delay);
100                 Thread.sleep(delay);
101                 sender().tell(new AppendEntriesReply(followerId, req.getTerm(), true, lastIndex, req.getTerm(),
102                         DataStoreVersions.CURRENT_VERSION), self());
103             }
104         } else {
105             sender().tell(new AppendEntriesReply(followerId, req.getTerm(), true, lastIndex, req.getTerm(),
106                     DataStoreVersions.CURRENT_VERSION), self());
107         }
108     }
109
110     public static Props props(Configuration configuration, final String followerId) {
111
112         return Props.create(new DummyShardCreator(configuration, followerId));
113     }
114
115     private static class DummyShardCreator implements Creator<DummyShard> {
116
117         private static final long serialVersionUID = 1L;
118         private final Configuration configuration;
119         private final String followerId;
120
121         DummyShardCreator(Configuration configuration, String followerId) {
122             this.configuration = configuration;
123             this.followerId = followerId;
124         }
125
126         @Override
127         public DummyShard create() throws Exception {
128             return new DummyShard(configuration, followerId);
129         }
130     }
131
132 }