2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.raft;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
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;
25 * Default implementation of the ConfigParams.
27 public class DefaultConfigParamsImpl implements ConfigParams {
29 private static final Logger LOG = LoggerFactory.getLogger(DefaultConfigParamsImpl.class);
31 private static final int SNAPSHOT_BATCH_COUNT = 20000;
33 * Interval after which a snapshot should be taken during the recovery process. 0 if never.
35 private static final int RECOVERY_SNAPSHOT_INTERVAL_SECONDS = 0;
37 private static final int JOURNAL_RECOVERY_LOG_BATCH_SIZE = 1000;
40 * The maximum election time variance.
42 private static final int ELECTION_TIME_MAX_VARIANCE = 100;
44 private static final int SNAPSHOT_CHUNK_SIZE = 2048 * 1000; //2MB
48 * The interval at which a heart beat message will be sent to the remote
52 * Since this is set to 100 milliseconds the Election timeout should be
53 * at least 200 milliseconds
55 public static final FiniteDuration HEART_BEAT_INTERVAL =
56 new FiniteDuration(100, TimeUnit.MILLISECONDS);
58 private final Supplier<RaftPolicy> policySupplier = Suppliers.memoize(this::getPolicy);
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;
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;
71 private int snapshotChunkSize = SNAPSHOT_CHUNK_SIZE;
73 private long electionTimeoutFactor = 2;
74 private long candidateElectionTimeoutDivisor = 1;
75 private String customRaftPolicyImplementationClass;
77 private PeerAddressResolver peerAddressResolver = NoopPeerAddressResolver.INSTANCE;
79 private String tempFileDirectory = "";
81 private int fileBackedStreamingThreshold = 128 * MEGABYTE;
83 private long syncIndexThreshold = 10;
85 public void setHeartBeatInterval(final FiniteDuration heartBeatInterval) {
86 this.heartBeatInterval = heartBeatInterval;
87 electionTimeOutInterval = null;
90 public void setSnapshotBatchCount(final long snapshotBatchCount) {
91 this.snapshotBatchCount = snapshotBatchCount;
94 public void setRecoverySnapshotIntervalSeconds(int recoverySnapshotInterval) {
95 checkArgument(recoverySnapshotInterval >= 0);
96 this.recoverySnapshotIntervalSeconds = recoverySnapshotInterval;
99 public void setSnapshotDataThresholdPercentage(final int snapshotDataThresholdPercentage) {
100 this.snapshotDataThresholdPercentage = snapshotDataThresholdPercentage;
103 public void setSnapshotChunkSize(final int snapshotChunkSize) {
104 this.snapshotChunkSize = snapshotChunkSize;
107 public void setJournalRecoveryLogBatchSize(final int journalRecoveryLogBatchSize) {
108 this.journalRecoveryLogBatchSize = journalRecoveryLogBatchSize;
111 public void setIsolatedLeaderCheckInterval(final FiniteDuration isolatedLeaderCheckInterval) {
112 this.isolatedLeaderCheckInterval = isolatedLeaderCheckInterval.toMillis();
115 public void setElectionTimeoutFactor(final long electionTimeoutFactor) {
116 this.electionTimeoutFactor = electionTimeoutFactor;
117 electionTimeOutInterval = null;
120 public void setCandidateElectionTimeoutDivisor(final long candidateElectionTimeoutDivisor) {
121 this.candidateElectionTimeoutDivisor = candidateElectionTimeoutDivisor;
124 public void setTempFileDirectory(final String tempFileDirectory) {
125 this.tempFileDirectory = tempFileDirectory;
128 public void setFileBackedStreamingThreshold(final int fileBackedStreamingThreshold) {
129 this.fileBackedStreamingThreshold = fileBackedStreamingThreshold;
132 public void setCustomRaftPolicyImplementationClass(final String customRaftPolicyImplementationClass) {
133 this.customRaftPolicyImplementationClass = customRaftPolicyImplementationClass;
137 public String getCustomRaftPolicyImplementationClass() {
138 return customRaftPolicyImplementationClass;
142 public long getSnapshotBatchCount() {
143 return snapshotBatchCount;
147 public int getSnapshotDataThresholdPercentage() {
148 return snapshotDataThresholdPercentage;
152 public int getRecoverySnapshotIntervalSeconds() {
153 return this.recoverySnapshotIntervalSeconds;
157 public FiniteDuration getHeartBeatInterval() {
158 return heartBeatInterval;
162 public FiniteDuration getElectionTimeOutInterval() {
163 if (electionTimeOutInterval == null) {
164 electionTimeOutInterval = getHeartBeatInterval().$times(electionTimeoutFactor);
167 return electionTimeOutInterval;
171 public long getCandidateElectionTimeoutDivisor() {
172 return candidateElectionTimeoutDivisor;
176 public int getElectionTimeVariance() {
177 return ELECTION_TIME_MAX_VARIANCE;
181 public int getSnapshotChunkSize() {
182 return snapshotChunkSize;
186 public int getJournalRecoveryLogBatchSize() {
187 return journalRecoveryLogBatchSize;
191 public long getIsolatedCheckIntervalInMillis() {
192 return isolatedLeaderCheckInterval;
196 public long getElectionTimeoutFactor() {
197 return electionTimeoutFactor;
201 public RaftPolicy getRaftPolicy() {
202 return policySupplier.get();
206 public String getTempFileDirectory() {
207 return tempFileDirectory;
211 public int getFileBackedStreamingThreshold() {
212 return fileBackedStreamingThreshold;
217 public PeerAddressResolver getPeerAddressResolver() {
218 return peerAddressResolver;
221 public void setPeerAddressResolver(final @NonNull PeerAddressResolver peerAddressResolver) {
222 this.peerAddressResolver = requireNonNull(peerAddressResolver);
226 public long getSyncIndexThreshold() {
227 return syncIndexThreshold;
230 public void setSyncIndexThreshold(final long syncIndexThreshold) {
231 checkArgument(syncIndexThreshold >= 0);
232 this.syncIndexThreshold = syncIndexThreshold;
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;
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);
250 LOG.error("Could not create custom raft policy, will stick with default : cause = {}",
254 return DefaultRaftPolicy.INSTANCE;