Reduce JSR305 proliferation
[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.Supplier;
15 import com.google.common.base.Suppliers;
16 import java.util.concurrent.TimeUnit;
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 String customRaftPolicyImplementationClass;
70
71     private PeerAddressResolver peerAddressResolver = NoopPeerAddressResolver.INSTANCE;
72
73     private String tempFileDirectory = "";
74
75     private int fileBackedStreamingThreshold = 128 * MEGABYTE;
76
77     private long syncIndexThreshold = 10;
78
79     public void setHeartBeatInterval(final FiniteDuration heartBeatInterval) {
80         this.heartBeatInterval = heartBeatInterval;
81         electionTimeOutInterval = null;
82     }
83
84     public void setSnapshotBatchCount(final long snapshotBatchCount) {
85         this.snapshotBatchCount = snapshotBatchCount;
86     }
87
88     public void setSnapshotDataThresholdPercentage(final int snapshotDataThresholdPercentage) {
89         this.snapshotDataThresholdPercentage = snapshotDataThresholdPercentage;
90     }
91
92     public void setSnapshotChunkSize(final int snapshotChunkSize) {
93         this.snapshotChunkSize = snapshotChunkSize;
94     }
95
96     public void setJournalRecoveryLogBatchSize(final int journalRecoveryLogBatchSize) {
97         this.journalRecoveryLogBatchSize = journalRecoveryLogBatchSize;
98     }
99
100     public void setIsolatedLeaderCheckInterval(final FiniteDuration isolatedLeaderCheckInterval) {
101         this.isolatedLeaderCheckInterval = isolatedLeaderCheckInterval.toMillis();
102     }
103
104     public void setElectionTimeoutFactor(final long electionTimeoutFactor) {
105         this.electionTimeoutFactor = electionTimeoutFactor;
106         electionTimeOutInterval = null;
107     }
108
109     public void setTempFileDirectory(final String tempFileDirectory) {
110         this.tempFileDirectory = tempFileDirectory;
111     }
112
113     public void setFileBackedStreamingThreshold(final int fileBackedStreamingThreshold) {
114         this.fileBackedStreamingThreshold = fileBackedStreamingThreshold;
115     }
116
117     public void setCustomRaftPolicyImplementationClass(final String customRaftPolicyImplementationClass) {
118         this.customRaftPolicyImplementationClass = customRaftPolicyImplementationClass;
119     }
120
121     @Override
122     public String getCustomRaftPolicyImplementationClass() {
123         return customRaftPolicyImplementationClass;
124     }
125
126     @Override
127     public long getSnapshotBatchCount() {
128         return snapshotBatchCount;
129     }
130
131     @Override
132     public int getSnapshotDataThresholdPercentage() {
133         return snapshotDataThresholdPercentage;
134     }
135
136
137     @Override
138     public FiniteDuration getHeartBeatInterval() {
139         return heartBeatInterval;
140     }
141
142     @Override
143     public FiniteDuration getElectionTimeOutInterval() {
144         if (electionTimeOutInterval == null) {
145             electionTimeOutInterval = getHeartBeatInterval().$times(electionTimeoutFactor);
146         }
147
148         return electionTimeOutInterval;
149     }
150
151     @Override
152     public int getElectionTimeVariance() {
153         return ELECTION_TIME_MAX_VARIANCE;
154     }
155
156     @Override
157     public int getSnapshotChunkSize() {
158         return snapshotChunkSize;
159     }
160
161     @Override
162     public int getJournalRecoveryLogBatchSize() {
163         return journalRecoveryLogBatchSize;
164     }
165
166     @Override
167     public long getIsolatedCheckIntervalInMillis() {
168         return isolatedLeaderCheckInterval;
169     }
170
171     @Override
172     public long getElectionTimeoutFactor() {
173         return electionTimeoutFactor;
174     }
175
176     @Override
177     public RaftPolicy getRaftPolicy() {
178         return policySupplier.get();
179     }
180
181     @Override
182     public String getTempFileDirectory() {
183         return tempFileDirectory;
184     }
185
186     @Override
187     public int getFileBackedStreamingThreshold() {
188         return fileBackedStreamingThreshold;
189     }
190
191
192     @Override
193     public PeerAddressResolver getPeerAddressResolver() {
194         return peerAddressResolver;
195     }
196
197     public void setPeerAddressResolver(final @NonNull PeerAddressResolver peerAddressResolver) {
198         this.peerAddressResolver = requireNonNull(peerAddressResolver);
199     }
200
201     @Override
202     public long getSyncIndexThreshold() {
203         return syncIndexThreshold;
204     }
205
206     public void setSyncIndexThreshold(final long syncIndexThreshold) {
207         checkArgument(syncIndexThreshold >= 0);
208         this.syncIndexThreshold = syncIndexThreshold;
209     }
210
211     @SuppressWarnings("checkstyle:IllegalCatch")
212     private RaftPolicy getPolicy() {
213         if (Strings.isNullOrEmpty(DefaultConfigParamsImpl.this.customRaftPolicyImplementationClass)) {
214             LOG.debug("No custom RaftPolicy specified. Using DefaultRaftPolicy");
215             return DefaultRaftPolicy.INSTANCE;
216         }
217
218         try {
219             String className = DefaultConfigParamsImpl.this.customRaftPolicyImplementationClass;
220             LOG.info("Trying to use custom RaftPolicy {}", className);
221             return (RaftPolicy)Class.forName(className).getDeclaredConstructor().newInstance();
222         } catch (ClassCastException | ReflectiveOperationException e) {
223             if (LOG.isDebugEnabled()) {
224                 LOG.error("Could not create custom raft policy, will stick with default", e);
225             } else {
226                 LOG.error("Could not create custom raft policy, will stick with default : cause = {}",
227                     e.getMessage());
228             }
229         }
230         return DefaultRaftPolicy.INSTANCE;
231     }
232 }