Merge "Cleanup root pom "name"."
[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.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;
25
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;
33
34     public DummyShard(Configuration configuration, String followerId) {
35         this.configuration = configuration;
36         this.followerId = followerId;
37         LOG.info("Creating : {}", followerId);
38     }
39
40     @Override
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);
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()), self());
105             }
106         } else {
107             sender().tell(new AppendEntriesReply(followerId, req.getTerm(), true, lastIndex, req.getTerm()), self());
108         }
109     }
110
111     public static Props props(Configuration configuration, final String followerId) {
112
113         return Props.create(new DummyShardCreator(configuration, followerId));
114     }
115
116     private static class DummyShardCreator implements Creator<DummyShard> {
117
118         private static final long serialVersionUID = 1L;
119         private final Configuration configuration;
120         private final String followerId;
121
122         DummyShardCreator(Configuration configuration, String followerId) {
123             this.configuration = configuration;
124             this.followerId = followerId;
125         }
126
127         @Override
128         public DummyShard create() throws Exception {
129             return new DummyShard(configuration, followerId);
130         }
131     }
132
133 }