Fix javadocs and enable doclint
[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      *
45      * <p>
46      * Since this is set to 100 milliseconds the Election timeout should be
47      * at least 200 milliseconds
48      */
49     public static final FiniteDuration HEART_BEAT_INTERVAL =
50         new FiniteDuration(100, TimeUnit.MILLISECONDS);
51
52     private FiniteDuration heartBeatInterval = HEART_BEAT_INTERVAL;
53     private long snapshotBatchCount = SNAPSHOT_BATCH_COUNT;
54     private int journalRecoveryLogBatchSize = JOURNAL_RECOVERY_LOG_BATCH_SIZE;
55     private long isolatedLeaderCheckInterval = HEART_BEAT_INTERVAL.$times(1000).toMillis();
56     private FiniteDuration electionTimeOutInterval;
57
58     // 12 is just an arbitrary percentage. This is the amount of the total memory that a raft actor's
59     // in-memory journal can use before it needs to snapshot
60     private int snapshotDataThresholdPercentage = 12;
61
62     private int snapshotChunkSize = SNAPSHOT_CHUNK_SIZE;
63
64     private long electionTimeoutFactor = 2;
65     private String customRaftPolicyImplementationClass;
66
67     private final Supplier<RaftPolicy> policySupplier = Suppliers.memoize(new PolicySupplier());
68
69     private PeerAddressResolver peerAddressResolver = NoopPeerAddressResolver.INSTANCE;
70
71     public void setHeartBeatInterval(FiniteDuration heartBeatInterval) {
72         this.heartBeatInterval = heartBeatInterval;
73         electionTimeOutInterval = null;
74     }
75
76     public void setSnapshotBatchCount(long snapshotBatchCount) {
77         this.snapshotBatchCount = snapshotBatchCount;
78     }
79
80     public void setSnapshotDataThresholdPercentage(int snapshotDataThresholdPercentage) {
81         this.snapshotDataThresholdPercentage = snapshotDataThresholdPercentage;
82     }
83
84     public void setSnapshotChunkSize(int snapshotChunkSize) {
85         this.snapshotChunkSize = snapshotChunkSize;
86     }
87
88     public void setJournalRecoveryLogBatchSize(int journalRecoveryLogBatchSize) {
89         this.journalRecoveryLogBatchSize = journalRecoveryLogBatchSize;
90     }
91
92     public void setIsolatedLeaderCheckInterval(FiniteDuration isolatedLeaderCheckInterval) {
93         this.isolatedLeaderCheckInterval = isolatedLeaderCheckInterval.toMillis();
94     }
95
96     public void setElectionTimeoutFactor(long electionTimeoutFactor) {
97         this.electionTimeoutFactor = electionTimeoutFactor;
98         electionTimeOutInterval = null;
99     }
100
101     public void setCustomRaftPolicyImplementationClass(String customRaftPolicyImplementationClass) {
102         this.customRaftPolicyImplementationClass = customRaftPolicyImplementationClass;
103     }
104
105     @Override
106     public String getCustomRaftPolicyImplementationClass() {
107         return customRaftPolicyImplementationClass;
108     }
109
110     @Override
111     public long getSnapshotBatchCount() {
112         return snapshotBatchCount;
113     }
114
115     @Override
116     public int getSnapshotDataThresholdPercentage() {
117         return snapshotDataThresholdPercentage;
118     }
119
120
121     @Override
122     public FiniteDuration getHeartBeatInterval() {
123         return heartBeatInterval;
124     }
125
126     @Override
127     public FiniteDuration getElectionTimeOutInterval() {
128         if (electionTimeOutInterval == null) {
129             electionTimeOutInterval = getHeartBeatInterval().$times(electionTimeoutFactor);
130         }
131
132         return electionTimeOutInterval;
133     }
134
135     @Override
136     public int getElectionTimeVariance() {
137         return ELECTION_TIME_MAX_VARIANCE;
138     }
139
140     @Override
141     public int getSnapshotChunkSize() {
142         return snapshotChunkSize;
143     }
144
145     @Override
146     public int getJournalRecoveryLogBatchSize() {
147         return journalRecoveryLogBatchSize;
148     }
149
150     @Override
151     public long getIsolatedCheckIntervalInMillis() {
152         return isolatedLeaderCheckInterval;
153     }
154
155     @Override
156     public long getElectionTimeoutFactor() {
157         return electionTimeoutFactor;
158     }
159
160     @Override
161     public RaftPolicy getRaftPolicy() {
162         return policySupplier.get();
163     }
164
165     private class PolicySupplier implements Supplier<RaftPolicy> {
166         @Override
167         @SuppressWarnings("checkstyle:IllegalCatch")
168         public RaftPolicy get() {
169             if (Strings.isNullOrEmpty(DefaultConfigParamsImpl.this.customRaftPolicyImplementationClass)) {
170                 LOG.debug("No custom RaftPolicy specified. Using DefaultRaftPolicy");
171                 return DefaultRaftPolicy.INSTANCE;
172             }
173
174             try {
175                 String className = DefaultConfigParamsImpl.this.customRaftPolicyImplementationClass;
176                 LOG.info("Trying to use custom RaftPolicy {}", className);
177                 return (RaftPolicy)Class.forName(className).newInstance();
178             } catch (Exception e) {
179                 if (LOG.isDebugEnabled()) {
180                     LOG.error("Could not create custom raft policy, will stick with default", e);
181                 } else {
182                     LOG.error("Could not create custom raft policy, will stick with default : cause = {}",
183                             e.getMessage());
184                 }
185             }
186             return DefaultRaftPolicy.INSTANCE;
187         }
188     }
189
190     @Override
191     public PeerAddressResolver getPeerAddressResolver() {
192         return peerAddressResolver;
193     }
194
195     public void setPeerAddressResolver(@Nonnull PeerAddressResolver peerAddressResolver) {
196         this.peerAddressResolver = Preconditions.checkNotNull(peerAddressResolver);
197     }
198 }