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.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;
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 private static final int JOURNAL_RECOVERY_LOG_BATCH_SIZE = 1000;
36 * The maximum election time variance.
38 private static final int ELECTION_TIME_MAX_VARIANCE = 100;
40 private static final int SNAPSHOT_CHUNK_SIZE = 2048 * 1000; //2MB
44 * The interval at which a heart beat message will be sent to the remote
48 * Since this is set to 100 milliseconds the Election timeout should be
49 * at least 200 milliseconds
51 public static final FiniteDuration HEART_BEAT_INTERVAL =
52 new FiniteDuration(100, TimeUnit.MILLISECONDS);
54 private final Supplier<RaftPolicy> policySupplier = Suppliers.memoize(this::getPolicy);
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;
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;
66 private int snapshotChunkSize = SNAPSHOT_CHUNK_SIZE;
68 private long electionTimeoutFactor = 2;
69 private long candidateElectionTimeoutDivisor = 1;
70 private String customRaftPolicyImplementationClass;
72 private PeerAddressResolver peerAddressResolver = NoopPeerAddressResolver.INSTANCE;
74 private String tempFileDirectory = "";
76 private int fileBackedStreamingThreshold = 128 * MEGABYTE;
78 private long syncIndexThreshold = 10;
80 public void setHeartBeatInterval(final FiniteDuration heartBeatInterval) {
81 this.heartBeatInterval = heartBeatInterval;
82 electionTimeOutInterval = null;
85 public void setSnapshotBatchCount(final long snapshotBatchCount) {
86 this.snapshotBatchCount = snapshotBatchCount;
89 public void setSnapshotDataThresholdPercentage(final int snapshotDataThresholdPercentage) {
90 this.snapshotDataThresholdPercentage = snapshotDataThresholdPercentage;
93 public void setSnapshotChunkSize(final int snapshotChunkSize) {
94 this.snapshotChunkSize = snapshotChunkSize;
97 public void setJournalRecoveryLogBatchSize(final int journalRecoveryLogBatchSize) {
98 this.journalRecoveryLogBatchSize = journalRecoveryLogBatchSize;
101 public void setIsolatedLeaderCheckInterval(final FiniteDuration isolatedLeaderCheckInterval) {
102 this.isolatedLeaderCheckInterval = isolatedLeaderCheckInterval.toMillis();
105 public void setElectionTimeoutFactor(final long electionTimeoutFactor) {
106 this.electionTimeoutFactor = electionTimeoutFactor;
107 electionTimeOutInterval = null;
110 public void setCandidateElectionTimeoutDivisor(final long candidateElectionTimeoutDivisor) {
111 this.candidateElectionTimeoutDivisor = candidateElectionTimeoutDivisor;
114 public void setTempFileDirectory(final String tempFileDirectory) {
115 this.tempFileDirectory = tempFileDirectory;
118 public void setFileBackedStreamingThreshold(final int fileBackedStreamingThreshold) {
119 this.fileBackedStreamingThreshold = fileBackedStreamingThreshold;
122 public void setCustomRaftPolicyImplementationClass(final String customRaftPolicyImplementationClass) {
123 this.customRaftPolicyImplementationClass = customRaftPolicyImplementationClass;
127 public String getCustomRaftPolicyImplementationClass() {
128 return customRaftPolicyImplementationClass;
132 public long getSnapshotBatchCount() {
133 return snapshotBatchCount;
137 public int getSnapshotDataThresholdPercentage() {
138 return snapshotDataThresholdPercentage;
142 public FiniteDuration getHeartBeatInterval() {
143 return heartBeatInterval;
147 public FiniteDuration getElectionTimeOutInterval() {
148 if (electionTimeOutInterval == null) {
149 electionTimeOutInterval = getHeartBeatInterval().$times(electionTimeoutFactor);
152 return electionTimeOutInterval;
156 public long getCandidateElectionTimeoutDivisor() {
157 return candidateElectionTimeoutDivisor;
161 public int getElectionTimeVariance() {
162 return ELECTION_TIME_MAX_VARIANCE;
166 public int getSnapshotChunkSize() {
167 return snapshotChunkSize;
171 public int getJournalRecoveryLogBatchSize() {
172 return journalRecoveryLogBatchSize;
176 public long getIsolatedCheckIntervalInMillis() {
177 return isolatedLeaderCheckInterval;
181 public long getElectionTimeoutFactor() {
182 return electionTimeoutFactor;
186 public RaftPolicy getRaftPolicy() {
187 return policySupplier.get();
191 public String getTempFileDirectory() {
192 return tempFileDirectory;
196 public int getFileBackedStreamingThreshold() {
197 return fileBackedStreamingThreshold;
202 public PeerAddressResolver getPeerAddressResolver() {
203 return peerAddressResolver;
206 public void setPeerAddressResolver(final @NonNull PeerAddressResolver peerAddressResolver) {
207 this.peerAddressResolver = requireNonNull(peerAddressResolver);
211 public long getSyncIndexThreshold() {
212 return syncIndexThreshold;
215 public void setSyncIndexThreshold(final long syncIndexThreshold) {
216 checkArgument(syncIndexThreshold >= 0);
217 this.syncIndexThreshold = syncIndexThreshold;
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;
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);
235 LOG.error("Could not create custom raft policy, will stick with default : cause = {}",
239 return DefaultRaftPolicy.INSTANCE;