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