Bug 2787: Batch AppendEntries to speed up follower sync
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / DefaultConfigParamsImpl.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 package org.opendaylight.controller.cluster.raft;
9
10 import java.util.concurrent.TimeUnit;
11 import scala.concurrent.duration.FiniteDuration;
12
13 /**
14  * Default implementation of the ConfigParams
15  *
16  * If no implementation is provided for ConfigParams, then this will be used.
17  */
18 public class DefaultConfigParamsImpl implements ConfigParams {
19
20     private static final int SNAPSHOT_BATCH_COUNT = 20000;
21
22     private static final int JOURNAL_RECOVERY_LOG_BATCH_SIZE = 1000;
23
24     /**
25      * The maximum election time variance
26      */
27     private static final int ELECTION_TIME_MAX_VARIANCE = 100;
28
29     private static final int SNAPSHOT_CHUNK_SIZE = 2048 * 1000; //2MB
30
31
32     /**
33      * The interval at which a heart beat message will be sent to the remote
34      * RaftActor
35      * <p/>
36      * Since this is set to 100 milliseconds the Election timeout should be
37      * at least 200 milliseconds
38      */
39     public static final FiniteDuration HEART_BEAT_INTERVAL =
40         new FiniteDuration(100, TimeUnit.MILLISECONDS);
41
42     private FiniteDuration heartBeatInterval = HEART_BEAT_INTERVAL;
43     private long snapshotBatchCount = SNAPSHOT_BATCH_COUNT;
44     private int journalRecoveryLogBatchSize = JOURNAL_RECOVERY_LOG_BATCH_SIZE;
45     private long isolatedLeaderCheckInterval = HEART_BEAT_INTERVAL.$times(1000).toMillis();
46     private FiniteDuration electionTimeOutInterval;
47
48     // 12 is just an arbitrary percentage. This is the amount of the total memory that a raft actor's
49     // in-memory journal can use before it needs to snapshot
50     private int snapshotDataThresholdPercentage = 12;
51
52     private int snaphotChunkSize = SNAPSHOT_CHUNK_SIZE;
53
54     private long electionTimeoutFactor = 2;
55
56     public void setHeartBeatInterval(FiniteDuration heartBeatInterval) {
57         this.heartBeatInterval = heartBeatInterval;
58         electionTimeOutInterval = null;
59     }
60
61     public void setSnapshotBatchCount(long snapshotBatchCount) {
62         this.snapshotBatchCount = snapshotBatchCount;
63     }
64
65     public void setSnapshotDataThresholdPercentage(int snapshotDataThresholdPercentage){
66         this.snapshotDataThresholdPercentage = snapshotDataThresholdPercentage;
67     }
68
69     public void setSnaphotChunkSize(int snaphotChunkSize) {
70         this.snaphotChunkSize = snaphotChunkSize;
71     }
72
73     public void setJournalRecoveryLogBatchSize(int journalRecoveryLogBatchSize) {
74         this.journalRecoveryLogBatchSize = journalRecoveryLogBatchSize;
75     }
76
77     public void setIsolatedLeaderCheckInterval(FiniteDuration isolatedLeaderCheckInterval) {
78         this.isolatedLeaderCheckInterval = isolatedLeaderCheckInterval.toMillis();
79     }
80
81     public void setElectionTimeoutFactor(long electionTimeoutFactor){
82         this.electionTimeoutFactor = electionTimeoutFactor;
83         electionTimeOutInterval = null;
84     }
85
86     @Override
87     public long getSnapshotBatchCount() {
88         return snapshotBatchCount;
89     }
90
91     @Override
92     public int getSnapshotDataThresholdPercentage() {
93         return snapshotDataThresholdPercentage;
94     }
95
96
97     @Override
98     public FiniteDuration getHeartBeatInterval() {
99         return heartBeatInterval;
100     }
101
102     @Override
103     public FiniteDuration getElectionTimeOutInterval() {
104         if(electionTimeOutInterval == null) {
105             electionTimeOutInterval = getHeartBeatInterval().$times(electionTimeoutFactor);
106         }
107
108         return electionTimeOutInterval;
109     }
110
111     @Override
112     public int getElectionTimeVariance() {
113         return ELECTION_TIME_MAX_VARIANCE;
114     }
115
116     @Override
117     public int getSnapshotChunkSize() {
118         return snaphotChunkSize;
119     }
120
121     @Override
122     public int getJournalRecoveryLogBatchSize() {
123         return journalRecoveryLogBatchSize;
124     }
125
126     @Override
127     public long getIsolatedCheckIntervalInMillis() {
128         return isolatedLeaderCheckInterval;
129     }
130
131     @Override
132     public long getElectionTimeoutFactor() {
133         return electionTimeoutFactor;
134     }
135 }