BUG-8618: refactor SyncStatusTracker state
[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 com.google.common.base.Preconditions;
11 import com.google.common.base.Strings;
12 import com.google.common.base.Supplier;
13 import com.google.common.base.Suppliers;
14 import java.util.concurrent.TimeUnit;
15 import javax.annotation.Nonnull;
16 import org.opendaylight.controller.cluster.raft.policy.DefaultRaftPolicy;
17 import org.opendaylight.controller.cluster.raft.policy.RaftPolicy;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20 import scala.concurrent.duration.FiniteDuration;
21
22 /**
23  * Default implementation of the ConfigParams.
24  */
25 public class DefaultConfigParamsImpl implements ConfigParams {
26
27     private static final Logger LOG = LoggerFactory.getLogger(DefaultConfigParamsImpl.class);
28
29     private static final int SNAPSHOT_BATCH_COUNT = 20000;
30
31     private static final int JOURNAL_RECOVERY_LOG_BATCH_SIZE = 1000;
32
33     /**
34      * The maximum election time variance.
35      */
36     private static final int ELECTION_TIME_MAX_VARIANCE = 100;
37
38     private static final int SNAPSHOT_CHUNK_SIZE = 2048 * 1000; //2MB
39
40
41     /**
42      * The interval at which a heart beat message will be sent to the remote
43      * RaftActor.
44      *
45      * <p>
46      * Since this is set to 100 milliseconds the Election timeout should be
47      * at least 200 milliseconds
48      */
49     public static final FiniteDuration HEART_BEAT_INTERVAL =
50         new FiniteDuration(100, TimeUnit.MILLISECONDS);
51
52     private FiniteDuration heartBeatInterval = HEART_BEAT_INTERVAL;
53     private long snapshotBatchCount = SNAPSHOT_BATCH_COUNT;
54     private int journalRecoveryLogBatchSize = JOURNAL_RECOVERY_LOG_BATCH_SIZE;
55     private long isolatedLeaderCheckInterval = HEART_BEAT_INTERVAL.$times(1000).toMillis();
56     private FiniteDuration electionTimeOutInterval;
57
58     // 12 is just an arbitrary percentage. This is the amount of the total memory that a raft actor's
59     // in-memory journal can use before it needs to snapshot
60     private int snapshotDataThresholdPercentage = 12;
61
62     private int snapshotChunkSize = SNAPSHOT_CHUNK_SIZE;
63
64     private long electionTimeoutFactor = 2;
65     private String customRaftPolicyImplementationClass;
66
67     private final Supplier<RaftPolicy> policySupplier = Suppliers.memoize(new PolicySupplier());
68
69     private PeerAddressResolver peerAddressResolver = NoopPeerAddressResolver.INSTANCE;
70
71     private String tempFileDirectory = "";
72
73     private int fileBackedStreamingThreshold = 128 * MEGABYTE;
74
75     public void setHeartBeatInterval(FiniteDuration heartBeatInterval) {
76         this.heartBeatInterval = heartBeatInterval;
77         electionTimeOutInterval = null;
78     }
79
80     public void setSnapshotBatchCount(long snapshotBatchCount) {
81         this.snapshotBatchCount = snapshotBatchCount;
82     }
83
84     public void setSnapshotDataThresholdPercentage(int snapshotDataThresholdPercentage) {
85         this.snapshotDataThresholdPercentage = snapshotDataThresholdPercentage;
86     }
87
88     public void setSnapshotChunkSize(int snapshotChunkSize) {
89         this.snapshotChunkSize = snapshotChunkSize;
90     }
91
92     public void setJournalRecoveryLogBatchSize(int journalRecoveryLogBatchSize) {
93         this.journalRecoveryLogBatchSize = journalRecoveryLogBatchSize;
94     }
95
96     public void setIsolatedLeaderCheckInterval(FiniteDuration isolatedLeaderCheckInterval) {
97         this.isolatedLeaderCheckInterval = isolatedLeaderCheckInterval.toMillis();
98     }
99
100     public void setElectionTimeoutFactor(long electionTimeoutFactor) {
101         this.electionTimeoutFactor = electionTimeoutFactor;
102         electionTimeOutInterval = null;
103     }
104
105     public void setTempFileDirectory(String tempFileDirectory) {
106         this.tempFileDirectory = tempFileDirectory;
107     }
108
109     public void setFileBackedStreamingThreshold(int fileBackedStreamingThreshold) {
110         this.fileBackedStreamingThreshold = fileBackedStreamingThreshold;
111     }
112
113     public void setCustomRaftPolicyImplementationClass(String customRaftPolicyImplementationClass) {
114         this.customRaftPolicyImplementationClass = customRaftPolicyImplementationClass;
115     }
116
117     @Override
118     public String getCustomRaftPolicyImplementationClass() {
119         return customRaftPolicyImplementationClass;
120     }
121
122     @Override
123     public long getSnapshotBatchCount() {
124         return snapshotBatchCount;
125     }
126
127     @Override
128     public int getSnapshotDataThresholdPercentage() {
129         return snapshotDataThresholdPercentage;
130     }
131
132
133     @Override
134     public FiniteDuration getHeartBeatInterval() {
135         return heartBeatInterval;
136     }
137
138     @Override
139     public FiniteDuration getElectionTimeOutInterval() {
140         if (electionTimeOutInterval == null) {
141             electionTimeOutInterval = getHeartBeatInterval().$times(electionTimeoutFactor);
142         }
143
144         return electionTimeOutInterval;
145     }
146
147     @Override
148     public int getElectionTimeVariance() {
149         return ELECTION_TIME_MAX_VARIANCE;
150     }
151
152     @Override
153     public int getSnapshotChunkSize() {
154         return snapshotChunkSize;
155     }
156
157     @Override
158     public int getJournalRecoveryLogBatchSize() {
159         return journalRecoveryLogBatchSize;
160     }
161
162     @Override
163     public long getIsolatedCheckIntervalInMillis() {
164         return isolatedLeaderCheckInterval;
165     }
166
167     @Override
168     public long getElectionTimeoutFactor() {
169         return electionTimeoutFactor;
170     }
171
172     @Override
173     public RaftPolicy getRaftPolicy() {
174         return policySupplier.get();
175     }
176
177     @Override
178     public String getTempFileDirectory() {
179         return tempFileDirectory;
180     }
181
182     @Override
183     public int getFileBackedStreamingThreshold() {
184         return fileBackedStreamingThreshold;
185     }
186
187     private class PolicySupplier implements Supplier<RaftPolicy> {
188         @Override
189         @SuppressWarnings("checkstyle:IllegalCatch")
190         public RaftPolicy get() {
191             if (Strings.isNullOrEmpty(DefaultConfigParamsImpl.this.customRaftPolicyImplementationClass)) {
192                 LOG.debug("No custom RaftPolicy specified. Using DefaultRaftPolicy");
193                 return DefaultRaftPolicy.INSTANCE;
194             }
195
196             try {
197                 String className = DefaultConfigParamsImpl.this.customRaftPolicyImplementationClass;
198                 LOG.info("Trying to use custom RaftPolicy {}", className);
199                 return (RaftPolicy)Class.forName(className).newInstance();
200             } catch (Exception e) {
201                 if (LOG.isDebugEnabled()) {
202                     LOG.error("Could not create custom raft policy, will stick with default", e);
203                 } else {
204                     LOG.error("Could not create custom raft policy, will stick with default : cause = {}",
205                             e.getMessage());
206                 }
207             }
208             return DefaultRaftPolicy.INSTANCE;
209         }
210     }
211
212     @Override
213     public PeerAddressResolver getPeerAddressResolver() {
214         return peerAddressResolver;
215     }
216
217     public void setPeerAddressResolver(@Nonnull PeerAddressResolver peerAddressResolver) {
218         this.peerAddressResolver = Preconditions.checkNotNull(peerAddressResolver);
219     }
220 }