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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.