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 * If no implementation is provided for ConfigParams, then this will be used.
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
47 * Since this is set to 100 milliseconds the Election timeout should be
48 * at least 200 milliseconds
50 public static final FiniteDuration HEART_BEAT_INTERVAL =
51 new FiniteDuration(100, TimeUnit.MILLISECONDS);
53 private FiniteDuration heartBeatInterval = HEART_BEAT_INTERVAL;
54 private long snapshotBatchCount = SNAPSHOT_BATCH_COUNT;
55 private int journalRecoveryLogBatchSize = JOURNAL_RECOVERY_LOG_BATCH_SIZE;
56 private long isolatedLeaderCheckInterval = HEART_BEAT_INTERVAL.$times(1000).toMillis();
57 private FiniteDuration electionTimeOutInterval;
59 // 12 is just an arbitrary percentage. This is the amount of the total memory that a raft actor's
60 // in-memory journal can use before it needs to snapshot
61 private int snapshotDataThresholdPercentage = 12;
63 private int snapshotChunkSize = SNAPSHOT_CHUNK_SIZE;
65 private long electionTimeoutFactor = 2;
66 private String customRaftPolicyImplementationClass;
68 private final Supplier<RaftPolicy> policySupplier = Suppliers.memoize(new PolicySupplier());
70 private PeerAddressResolver peerAddressResolver = NoopPeerAddressResolver.INSTANCE;
72 public void setHeartBeatInterval(FiniteDuration heartBeatInterval) {
73 this.heartBeatInterval = heartBeatInterval;
74 electionTimeOutInterval = null;
77 public void setSnapshotBatchCount(long snapshotBatchCount) {
78 this.snapshotBatchCount = snapshotBatchCount;
81 public void setSnapshotDataThresholdPercentage(int snapshotDataThresholdPercentage){
82 this.snapshotDataThresholdPercentage = snapshotDataThresholdPercentage;
85 public void setSnapshotChunkSize(int snapshotChunkSize) {
86 this.snapshotChunkSize = snapshotChunkSize;
89 public void setJournalRecoveryLogBatchSize(int journalRecoveryLogBatchSize) {
90 this.journalRecoveryLogBatchSize = journalRecoveryLogBatchSize;
93 public void setIsolatedLeaderCheckInterval(FiniteDuration isolatedLeaderCheckInterval) {
94 this.isolatedLeaderCheckInterval = isolatedLeaderCheckInterval.toMillis();
97 public void setElectionTimeoutFactor(long electionTimeoutFactor){
98 this.electionTimeoutFactor = electionTimeoutFactor;
99 electionTimeOutInterval = null;
102 public void setCustomRaftPolicyImplementationClass(String customRaftPolicyImplementationClass){
103 this.customRaftPolicyImplementationClass = customRaftPolicyImplementationClass;
106 public String getCustomRaftPolicyImplementationClass() {
107 return customRaftPolicyImplementationClass;
111 public long getSnapshotBatchCount() {
112 return snapshotBatchCount;
116 public int getSnapshotDataThresholdPercentage() {
117 return snapshotDataThresholdPercentage;
122 public FiniteDuration getHeartBeatInterval() {
123 return heartBeatInterval;
127 public FiniteDuration getElectionTimeOutInterval() {
128 if(electionTimeOutInterval == null) {
129 electionTimeOutInterval = getHeartBeatInterval().$times(electionTimeoutFactor);
132 return electionTimeOutInterval;
136 public int getElectionTimeVariance() {
137 return ELECTION_TIME_MAX_VARIANCE;
141 public int getSnapshotChunkSize() {
142 return snapshotChunkSize;
146 public int getJournalRecoveryLogBatchSize() {
147 return journalRecoveryLogBatchSize;
151 public long getIsolatedCheckIntervalInMillis() {
152 return isolatedLeaderCheckInterval;
156 public long getElectionTimeoutFactor() {
157 return electionTimeoutFactor;
161 public RaftPolicy getRaftPolicy() {
162 return policySupplier.get();
165 private class PolicySupplier implements Supplier<RaftPolicy>{
167 public RaftPolicy get() {
168 if(Strings.isNullOrEmpty(DefaultConfigParamsImpl.this.customRaftPolicyImplementationClass)){
169 LOG.debug("No custom RaftPolicy specified. Using DefaultRaftPolicy");
170 return DefaultRaftPolicy.INSTANCE;
173 String className = DefaultConfigParamsImpl.this.customRaftPolicyImplementationClass;
174 LOG.info("Trying to use custom RaftPolicy {}", className);
175 Class c = Class.forName(className);
176 RaftPolicy obj = (RaftPolicy)c.newInstance();
178 } catch (Exception e) {
179 if(LOG.isDebugEnabled()) {
180 LOG.error("Could not create custom raft policy, will stick with default", e);
182 LOG.error("Could not create custom raft policy, will stick with default : cause = {}", e.getMessage());
185 return DefaultRaftPolicy.INSTANCE;
190 public PeerAddressResolver getPeerAddressResolver() {
191 return peerAddressResolver;
194 public void setPeerAddressResolver(@Nonnull PeerAddressResolver peerAddressResolver) {
195 this.peerAddressResolver = Preconditions.checkNotNull(peerAddressResolver);