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.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;
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;
35 public DummyShard(Configuration configuration, String followerId) {
36 this.configuration = configuration;
37 this.followerId = followerId;
38 LOG.info("Creating : {}", followerId);
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);
51 LOG.error("Unknown message : {}", o.getClass());
55 private void handleInstallSnapshot(InstallSnapshot req) {
56 sender().tell(new InstallSnapshotReply(req.getTerm(), followerId, req.getChunkIndex(), true), self());
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());
63 if(appendEntriesWatch != null){
64 long elapsed = appendEntriesWatch.elapsed(TimeUnit.SECONDS);
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());
70 appendEntriesWatch.reset().start();
72 appendEntriesWatch = Stopwatch.createStarted();
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());
79 lastMessageIndex = req.getLeaderCommit();
80 lastMessageSize = req.getEntries().size();
82 long lastIndex = req.getLeaderCommit();
83 if (req.getEntries().size() > 0) {
84 for(ReplicatedLogEntry entry : req.getEntries()) {
85 lastIndex = entry.getIndex();
89 if (configuration.shouldCauseTrouble() && req.getEntries().size() > 0) {
90 boolean ignore = false;
92 if (configuration.shouldDropReplies()) {
93 ignore = Math.random() > 0.5;
96 long delay = (long) (Math.random() * configuration.getMaxDelayInMillis());
99 LOG.info("{} - Randomizing delay : {}", followerId, delay);
101 sender().tell(new AppendEntriesReply(followerId, req.getTerm(), true, lastIndex, req.getTerm(),
102 DataStoreVersions.CURRENT_VERSION), self());
105 sender().tell(new AppendEntriesReply(followerId, req.getTerm(), true, lastIndex, req.getTerm(),
106 DataStoreVersions.CURRENT_VERSION), self());
110 public static Props props(Configuration configuration, final String followerId) {
112 return Props.create(new DummyShardCreator(configuration, followerId));
115 private static class DummyShardCreator implements Creator<DummyShard> {
117 private static final long serialVersionUID = 1L;
118 private final Configuration configuration;
119 private final String followerId;
121 DummyShardCreator(Configuration configuration, String followerId) {
122 this.configuration = configuration;
123 this.followerId = followerId;
127 public DummyShard create() throws Exception {
128 return new DummyShard(configuration, followerId);