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.Strings;
11 import com.google.common.base.Supplier;
12 import com.google.common.base.Suppliers;
13 import java.util.concurrent.TimeUnit;
14 import org.opendaylight.controller.cluster.raft.policy.DefaultRaftPolicy;
15 import org.opendaylight.controller.cluster.raft.policy.RaftPolicy;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18 import scala.concurrent.duration.FiniteDuration;
21 * Default implementation of the ConfigParams
23 * If no implementation is provided for ConfigParams, then this will be used.
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
45 * Since this is set to 100 milliseconds the Election timeout should be
46 * at least 200 milliseconds
48 public static final FiniteDuration HEART_BEAT_INTERVAL =
49 new FiniteDuration(100, TimeUnit.MILLISECONDS);
51 private FiniteDuration heartBeatInterval = HEART_BEAT_INTERVAL;
52 private long snapshotBatchCount = SNAPSHOT_BATCH_COUNT;
53 private int journalRecoveryLogBatchSize = JOURNAL_RECOVERY_LOG_BATCH_SIZE;
54 private long isolatedLeaderCheckInterval = HEART_BEAT_INTERVAL.$times(1000).toMillis();
55 private FiniteDuration electionTimeOutInterval;
57 // 12 is just an arbitrary percentage. This is the amount of the total memory that a raft actor's
58 // in-memory journal can use before it needs to snapshot
59 private int snapshotDataThresholdPercentage = 12;
61 private int snaphotChunkSize = SNAPSHOT_CHUNK_SIZE;
63 private long electionTimeoutFactor = 2;
64 private String customRaftPolicyImplementationClass;
66 Supplier<RaftPolicy> policySupplier = Suppliers.memoize(new PolicySupplier());
68 public void setHeartBeatInterval(FiniteDuration heartBeatInterval) {
69 this.heartBeatInterval = heartBeatInterval;
70 electionTimeOutInterval = null;
73 public void setSnapshotBatchCount(long snapshotBatchCount) {
74 this.snapshotBatchCount = snapshotBatchCount;
77 public void setSnapshotDataThresholdPercentage(int snapshotDataThresholdPercentage){
78 this.snapshotDataThresholdPercentage = snapshotDataThresholdPercentage;
81 public void setSnaphotChunkSize(int snaphotChunkSize) {
82 this.snaphotChunkSize = snaphotChunkSize;
85 public void setJournalRecoveryLogBatchSize(int journalRecoveryLogBatchSize) {
86 this.journalRecoveryLogBatchSize = journalRecoveryLogBatchSize;
89 public void setIsolatedLeaderCheckInterval(FiniteDuration isolatedLeaderCheckInterval) {
90 this.isolatedLeaderCheckInterval = isolatedLeaderCheckInterval.toMillis();
93 public void setElectionTimeoutFactor(long electionTimeoutFactor){
94 this.electionTimeoutFactor = electionTimeoutFactor;
95 electionTimeOutInterval = null;
98 public void setCustomRaftPolicyImplementationClass(String customRaftPolicyImplementationClass){
99 this.customRaftPolicyImplementationClass = customRaftPolicyImplementationClass;
103 public long getSnapshotBatchCount() {
104 return snapshotBatchCount;
108 public int getSnapshotDataThresholdPercentage() {
109 return snapshotDataThresholdPercentage;
114 public FiniteDuration getHeartBeatInterval() {
115 return heartBeatInterval;
119 public FiniteDuration getElectionTimeOutInterval() {
120 if(electionTimeOutInterval == null) {
121 electionTimeOutInterval = getHeartBeatInterval().$times(electionTimeoutFactor);
124 return electionTimeOutInterval;
128 public int getElectionTimeVariance() {
129 return ELECTION_TIME_MAX_VARIANCE;
133 public int getSnapshotChunkSize() {
134 return snaphotChunkSize;
138 public int getJournalRecoveryLogBatchSize() {
139 return journalRecoveryLogBatchSize;
143 public long getIsolatedCheckIntervalInMillis() {
144 return isolatedLeaderCheckInterval;
148 public long getElectionTimeoutFactor() {
149 return electionTimeoutFactor;
153 public RaftPolicy getRaftPolicy() {
154 return policySupplier.get();
157 private class PolicySupplier implements Supplier<RaftPolicy>{
159 public RaftPolicy get() {
160 if(Strings.isNullOrEmpty(DefaultConfigParamsImpl.this.customRaftPolicyImplementationClass)){
161 LOG.debug("No custom RaftPolicy specified. Using DefaultRaftPolicy");
162 return DefaultRaftPolicy.INSTANCE;
165 String className = DefaultConfigParamsImpl.this.customRaftPolicyImplementationClass;
166 LOG.info("Trying to use custom RaftPolicy {}", className);
167 Class c = Class.forName(className);
168 RaftPolicy obj = (RaftPolicy)c.newInstance();
170 } catch (Exception e) {
171 if(LOG.isDebugEnabled()) {
172 LOG.error("Could not create custom raft policy, will stick with default", e);
174 LOG.error("Could not create custom raft policy, will stick with default : cause = {}", e.getMessage());
177 return DefaultRaftPolicy.INSTANCE;