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 com.google.common.base.Preconditions;
11 import com.google.common.base.Strings;
12 import com.google.common.base.Supplier;
13 import com.google.common.base.Suppliers;
14 import java.util.concurrent.TimeUnit;
15 import javax.annotation.Nonnull;
16 import org.opendaylight.controller.cluster.raft.policy.DefaultRaftPolicy;
17 import org.opendaylight.controller.cluster.raft.policy.RaftPolicy;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20 import scala.concurrent.duration.FiniteDuration;
23 * Default implementation of the ConfigParams.
25 public class DefaultConfigParamsImpl implements ConfigParams {
27 private static final Logger LOG = LoggerFactory.getLogger(DefaultConfigParamsImpl.class);
29 private static final int SNAPSHOT_BATCH_COUNT = 20000;
31 private static final int JOURNAL_RECOVERY_LOG_BATCH_SIZE = 1000;
34 * The maximum election time variance.
36 private static final int ELECTION_TIME_MAX_VARIANCE = 100;
38 private static final int SNAPSHOT_CHUNK_SIZE = 2048 * 1000; //2MB
42 * The interval at which a heart beat message will be sent to the remote
46 * Since this is set to 100 milliseconds the Election timeout should be
47 * at least 200 milliseconds
49 public static final FiniteDuration HEART_BEAT_INTERVAL =
50 new FiniteDuration(100, TimeUnit.MILLISECONDS);
52 private final Supplier<RaftPolicy> policySupplier = Suppliers.memoize(this::getPolicy);
54 private FiniteDuration heartBeatInterval = HEART_BEAT_INTERVAL;
55 private long snapshotBatchCount = SNAPSHOT_BATCH_COUNT;
56 private int journalRecoveryLogBatchSize = JOURNAL_RECOVERY_LOG_BATCH_SIZE;
57 private long isolatedLeaderCheckInterval = HEART_BEAT_INTERVAL.$times(1000).toMillis();
58 private FiniteDuration electionTimeOutInterval;
60 // 12 is just an arbitrary percentage. This is the amount of the total memory that a raft actor's
61 // in-memory journal can use before it needs to snapshot
62 private int snapshotDataThresholdPercentage = 12;
64 private int snapshotChunkSize = SNAPSHOT_CHUNK_SIZE;
66 private long electionTimeoutFactor = 2;
67 private String customRaftPolicyImplementationClass;
69 private PeerAddressResolver peerAddressResolver = NoopPeerAddressResolver.INSTANCE;
71 private String tempFileDirectory = "";
73 private int fileBackedStreamingThreshold = 128 * MEGABYTE;
75 private long syncIndexThreshold = 10;
77 public void setHeartBeatInterval(final FiniteDuration heartBeatInterval) {
78 this.heartBeatInterval = heartBeatInterval;
79 electionTimeOutInterval = null;
82 public void setSnapshotBatchCount(final long snapshotBatchCount) {
83 this.snapshotBatchCount = snapshotBatchCount;
86 public void setSnapshotDataThresholdPercentage(final int snapshotDataThresholdPercentage) {
87 this.snapshotDataThresholdPercentage = snapshotDataThresholdPercentage;
90 public void setSnapshotChunkSize(final int snapshotChunkSize) {
91 this.snapshotChunkSize = snapshotChunkSize;
94 public void setJournalRecoveryLogBatchSize(final int journalRecoveryLogBatchSize) {
95 this.journalRecoveryLogBatchSize = journalRecoveryLogBatchSize;
98 public void setIsolatedLeaderCheckInterval(final FiniteDuration isolatedLeaderCheckInterval) {
99 this.isolatedLeaderCheckInterval = isolatedLeaderCheckInterval.toMillis();
102 public void setElectionTimeoutFactor(final long electionTimeoutFactor) {
103 this.electionTimeoutFactor = electionTimeoutFactor;
104 electionTimeOutInterval = null;
107 public void setTempFileDirectory(final String tempFileDirectory) {
108 this.tempFileDirectory = tempFileDirectory;
111 public void setFileBackedStreamingThreshold(final int fileBackedStreamingThreshold) {
112 this.fileBackedStreamingThreshold = fileBackedStreamingThreshold;
115 public void setCustomRaftPolicyImplementationClass(final String customRaftPolicyImplementationClass) {
116 this.customRaftPolicyImplementationClass = customRaftPolicyImplementationClass;
120 public String getCustomRaftPolicyImplementationClass() {
121 return customRaftPolicyImplementationClass;
125 public long getSnapshotBatchCount() {
126 return snapshotBatchCount;
130 public int getSnapshotDataThresholdPercentage() {
131 return snapshotDataThresholdPercentage;
136 public FiniteDuration getHeartBeatInterval() {
137 return heartBeatInterval;
141 public FiniteDuration getElectionTimeOutInterval() {
142 if (electionTimeOutInterval == null) {
143 electionTimeOutInterval = getHeartBeatInterval().$times(electionTimeoutFactor);
146 return electionTimeOutInterval;
150 public int getElectionTimeVariance() {
151 return ELECTION_TIME_MAX_VARIANCE;
155 public int getSnapshotChunkSize() {
156 return snapshotChunkSize;
160 public int getJournalRecoveryLogBatchSize() {
161 return journalRecoveryLogBatchSize;
165 public long getIsolatedCheckIntervalInMillis() {
166 return isolatedLeaderCheckInterval;
170 public long getElectionTimeoutFactor() {
171 return electionTimeoutFactor;
175 public RaftPolicy getRaftPolicy() {
176 return policySupplier.get();
180 public String getTempFileDirectory() {
181 return tempFileDirectory;
185 public int getFileBackedStreamingThreshold() {
186 return fileBackedStreamingThreshold;
191 public PeerAddressResolver getPeerAddressResolver() {
192 return peerAddressResolver;
195 public void setPeerAddressResolver(@Nonnull final PeerAddressResolver peerAddressResolver) {
196 this.peerAddressResolver = Preconditions.checkNotNull(peerAddressResolver);
200 public long getSyncIndexThreshold() {
201 return syncIndexThreshold;
204 public void setSyncIndexThreshold(final long syncIndexThreshold) {
205 Preconditions.checkArgument(syncIndexThreshold >= 0);
206 this.syncIndexThreshold = syncIndexThreshold;
209 @SuppressWarnings("checkstyle:IllegalCatch")
210 private RaftPolicy getPolicy() {
211 if (Strings.isNullOrEmpty(DefaultConfigParamsImpl.this.customRaftPolicyImplementationClass)) {
212 LOG.debug("No custom RaftPolicy specified. Using DefaultRaftPolicy");
213 return DefaultRaftPolicy.INSTANCE;
217 String className = DefaultConfigParamsImpl.this.customRaftPolicyImplementationClass;
218 LOG.info("Trying to use custom RaftPolicy {}", className);
219 return (RaftPolicy)Class.forName(className).newInstance();
220 } catch (Exception e) {
221 if (LOG.isDebugEnabled()) {
222 LOG.error("Could not create custom raft policy, will stick with default", e);
224 LOG.error("Could not create custom raft policy, will stick with default : cause = {}",
228 return DefaultRaftPolicy.INSTANCE;