2d2f95bd767a566edfd0db331b3ece1d08c64d43
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / DefaultConfigParamsImpl.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.raft;
9
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;
21
22 /**
23  * Default implementation of the ConfigParams.
24  */
25 public class DefaultConfigParamsImpl implements ConfigParams {
26
27     private static final Logger LOG = LoggerFactory.getLogger(DefaultConfigParamsImpl.class);
28
29     private static final int SNAPSHOT_BATCH_COUNT = 20000;
30
31     private static final int JOURNAL_RECOVERY_LOG_BATCH_SIZE = 1000;
32
33     /**
34      * The maximum election time variance.
35      */
36     private static final int ELECTION_TIME_MAX_VARIANCE = 100;
37
38     private static final int SNAPSHOT_CHUNK_SIZE = 2048 * 1000; //2MB
39
40
41     /**
42      * The interval at which a heart beat message will be sent to the remote
43      * RaftActor.
44      * <p/>
45      * Since this is set to 100 milliseconds the Election timeout should be
46      * at least 200 milliseconds
47      */
48     public static final FiniteDuration HEART_BEAT_INTERVAL =
49         new FiniteDuration(100, TimeUnit.MILLISECONDS);
50
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;
56
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;
60
61     private int snapshotChunkSize = SNAPSHOT_CHUNK_SIZE;
62
63     private long electionTimeoutFactor = 2;
64     private String customRaftPolicyImplementationClass;
65
66     private final Supplier<RaftPolicy> policySupplier = Suppliers.memoize(new PolicySupplier());
67
68     private PeerAddressResolver peerAddressResolver = NoopPeerAddressResolver.INSTANCE;
69
70     public void setHeartBeatInterval(FiniteDuration heartBeatInterval) {
71         this.heartBeatInterval = heartBeatInterval;
72         electionTimeOutInterval = null;
73     }
74
75     public void setSnapshotBatchCount(long snapshotBatchCount) {
76         this.snapshotBatchCount = snapshotBatchCount;
77     }
78
79     public void setSnapshotDataThresholdPercentage(int snapshotDataThresholdPercentage) {
80         this.snapshotDataThresholdPercentage = snapshotDataThresholdPercentage;
81     }
82
83     public void setSnapshotChunkSize(int snapshotChunkSize) {
84         this.snapshotChunkSize = snapshotChunkSize;
85     }
86
87     public void setJournalRecoveryLogBatchSize(int journalRecoveryLogBatchSize) {
88         this.journalRecoveryLogBatchSize = journalRecoveryLogBatchSize;
89     }
90
91     public void setIsolatedLeaderCheckInterval(FiniteDuration isolatedLeaderCheckInterval) {
92         this.isolatedLeaderCheckInterval = isolatedLeaderCheckInterval.toMillis();
93     }
94
95     public void setElectionTimeoutFactor(long electionTimeoutFactor) {
96         this.electionTimeoutFactor = electionTimeoutFactor;
97         electionTimeOutInterval = null;
98     }
99
100     public void setCustomRaftPolicyImplementationClass(String customRaftPolicyImplementationClass) {
101         this.customRaftPolicyImplementationClass = customRaftPolicyImplementationClass;
102     }
103
104     @Override
105     public String getCustomRaftPolicyImplementationClass() {
106         return customRaftPolicyImplementationClass;
107     }
108
109     @Override
110     public long getSnapshotBatchCount() {
111         return snapshotBatchCount;
112     }
113
114     @Override
115     public int getSnapshotDataThresholdPercentage() {
116         return snapshotDataThresholdPercentage;
117     }
118
119
120     @Override
121     public FiniteDuration getHeartBeatInterval() {
122         return heartBeatInterval;
123     }
124
125     @Override
126     public FiniteDuration getElectionTimeOutInterval() {
127         if (electionTimeOutInterval == null) {
128             electionTimeOutInterval = getHeartBeatInterval().$times(electionTimeoutFactor);
129         }
130
131         return electionTimeOutInterval;
132     }
133
134     @Override
135     public int getElectionTimeVariance() {
136         return ELECTION_TIME_MAX_VARIANCE;
137     }
138
139     @Override
140     public int getSnapshotChunkSize() {
141         return snapshotChunkSize;
142     }
143
144     @Override
145     public int getJournalRecoveryLogBatchSize() {
146         return journalRecoveryLogBatchSize;
147     }
148
149     @Override
150     public long getIsolatedCheckIntervalInMillis() {
151         return isolatedLeaderCheckInterval;
152     }
153
154     @Override
155     public long getElectionTimeoutFactor() {
156         return electionTimeoutFactor;
157     }
158
159     @Override
160     public RaftPolicy getRaftPolicy() {
161         return policySupplier.get();
162     }
163
164     private class PolicySupplier implements Supplier<RaftPolicy> {
165         @Override
166         @SuppressWarnings("checkstyle:IllegalCatch")
167         public RaftPolicy get() {
168             if (Strings.isNullOrEmpty(DefaultConfigParamsImpl.this.customRaftPolicyImplementationClass)) {
169                 LOG.debug("No custom RaftPolicy specified. Using DefaultRaftPolicy");
170                 return DefaultRaftPolicy.INSTANCE;
171             }
172
173             try {
174                 String className = DefaultConfigParamsImpl.this.customRaftPolicyImplementationClass;
175                 LOG.info("Trying to use custom RaftPolicy {}", className);
176                 return (RaftPolicy)Class.forName(className).newInstance();
177             } catch (Exception e) {
178                 if (LOG.isDebugEnabled()) {
179                     LOG.error("Could not create custom raft policy, will stick with default", e);
180                 } else {
181                     LOG.error("Could not create custom raft policy, will stick with default : cause = {}",
182                             e.getMessage());
183                 }
184             }
185             return DefaultRaftPolicy.INSTANCE;
186         }
187     }
188
189     @Override
190     public PeerAddressResolver getPeerAddressResolver() {
191         return peerAddressResolver;
192     }
193
194     public void setPeerAddressResolver(@Nonnull PeerAddressResolver peerAddressResolver) {
195         this.peerAddressResolver = Preconditions.checkNotNull(peerAddressResolver);
196     }
197 }