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 String customRaftPolicyImplementationClass;
71 private PeerAddressResolver peerAddressResolver = NoopPeerAddressResolver.INSTANCE;
73 private String tempFileDirectory = "";
75 private int fileBackedStreamingThreshold = 128 * MEGABYTE;
77 private long syncIndexThreshold = 10;
79 public void setHeartBeatInterval(final FiniteDuration heartBeatInterval) {
80 this.heartBeatInterval = heartBeatInterval;
81 electionTimeOutInterval = null;
84 public void setSnapshotBatchCount(final long snapshotBatchCount) {
85 this.snapshotBatchCount = snapshotBatchCount;
88 public void setSnapshotDataThresholdPercentage(final int snapshotDataThresholdPercentage) {
89 this.snapshotDataThresholdPercentage = snapshotDataThresholdPercentage;
92 public void setSnapshotChunkSize(final int snapshotChunkSize) {
93 this.snapshotChunkSize = snapshotChunkSize;
96 public void setJournalRecoveryLogBatchSize(final int journalRecoveryLogBatchSize) {
97 this.journalRecoveryLogBatchSize = journalRecoveryLogBatchSize;
100 public void setIsolatedLeaderCheckInterval(final FiniteDuration isolatedLeaderCheckInterval) {
101 this.isolatedLeaderCheckInterval = isolatedLeaderCheckInterval.toMillis();
104 public void setElectionTimeoutFactor(final long electionTimeoutFactor) {
105 this.electionTimeoutFactor = electionTimeoutFactor;
106 electionTimeOutInterval = null;
109 public void setTempFileDirectory(final String tempFileDirectory) {
110 this.tempFileDirectory = tempFileDirectory;
113 public void setFileBackedStreamingThreshold(final int fileBackedStreamingThreshold) {
114 this.fileBackedStreamingThreshold = fileBackedStreamingThreshold;
117 public void setCustomRaftPolicyImplementationClass(final String customRaftPolicyImplementationClass) {
118 this.customRaftPolicyImplementationClass = customRaftPolicyImplementationClass;
122 public String getCustomRaftPolicyImplementationClass() {
123 return customRaftPolicyImplementationClass;
127 public long getSnapshotBatchCount() {
128 return snapshotBatchCount;
132 public int getSnapshotDataThresholdPercentage() {
133 return snapshotDataThresholdPercentage;
138 public FiniteDuration getHeartBeatInterval() {
139 return heartBeatInterval;
143 public FiniteDuration getElectionTimeOutInterval() {
144 if (electionTimeOutInterval == null) {
145 electionTimeOutInterval = getHeartBeatInterval().$times(electionTimeoutFactor);
148 return electionTimeOutInterval;
152 public int getElectionTimeVariance() {
153 return ELECTION_TIME_MAX_VARIANCE;
157 public int getSnapshotChunkSize() {
158 return snapshotChunkSize;
162 public int getJournalRecoveryLogBatchSize() {
163 return journalRecoveryLogBatchSize;
167 public long getIsolatedCheckIntervalInMillis() {
168 return isolatedLeaderCheckInterval;
172 public long getElectionTimeoutFactor() {
173 return electionTimeoutFactor;
177 public RaftPolicy getRaftPolicy() {
178 return policySupplier.get();
182 public String getTempFileDirectory() {
183 return tempFileDirectory;
187 public int getFileBackedStreamingThreshold() {
188 return fileBackedStreamingThreshold;
193 public PeerAddressResolver getPeerAddressResolver() {
194 return peerAddressResolver;
197 public void setPeerAddressResolver(final @NonNull PeerAddressResolver peerAddressResolver) {
198 this.peerAddressResolver = requireNonNull(peerAddressResolver);
202 public long getSyncIndexThreshold() {
203 return syncIndexThreshold;
206 public void setSyncIndexThreshold(final long syncIndexThreshold) {
207 checkArgument(syncIndexThreshold >= 0);
208 this.syncIndexThreshold = syncIndexThreshold;
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;
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);
226 LOG.error("Could not create custom raft policy, will stick with default : cause = {}",
230 return DefaultRaftPolicy.INSTANCE;