BUG 2817 - Basic implementation of RemoveServer in the Raft code
[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  * If no implementation is provided for ConfigParams, then this will be used.
26  */
27 public class DefaultConfigParamsImpl implements ConfigParams {
28
29     private static final Logger LOG = LoggerFactory.getLogger(DefaultConfigParamsImpl.class);
30
31     private static final int SNAPSHOT_BATCH_COUNT = 20000;
32
33     private static final int JOURNAL_RECOVERY_LOG_BATCH_SIZE = 1000;
34
35     /**
36      * The maximum election time variance
37      */
38     private static final int ELECTION_TIME_MAX_VARIANCE = 100;
39
40     private static final int SNAPSHOT_CHUNK_SIZE = 2048 * 1000; //2MB
41
42
43     /**
44      * The interval at which a heart beat message will be sent to the remote
45      * RaftActor
46      * <p/>
47      * Since this is set to 100 milliseconds the Election timeout should be
48      * at least 200 milliseconds
49      */
50     public static final FiniteDuration HEART_BEAT_INTERVAL =
51         new FiniteDuration(100, TimeUnit.MILLISECONDS);
52
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;
58
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;
62
63     private int snapshotChunkSize = SNAPSHOT_CHUNK_SIZE;
64
65     private long electionTimeoutFactor = 2;
66     private String customRaftPolicyImplementationClass;
67
68     private final Supplier<RaftPolicy> policySupplier = Suppliers.memoize(new PolicySupplier());
69
70     private PeerAddressResolver peerAddressResolver = NoopPeerAddressResolver.INSTANCE;
71
72     public void setHeartBeatInterval(FiniteDuration heartBeatInterval) {
73         this.heartBeatInterval = heartBeatInterval;
74         electionTimeOutInterval = null;
75     }
76
77     public void setSnapshotBatchCount(long snapshotBatchCount) {
78         this.snapshotBatchCount = snapshotBatchCount;
79     }
80
81     public void setSnapshotDataThresholdPercentage(int snapshotDataThresholdPercentage){
82         this.snapshotDataThresholdPercentage = snapshotDataThresholdPercentage;
83     }
84
85     public void setSnapshotChunkSize(int snapshotChunkSize) {
86         this.snapshotChunkSize = snapshotChunkSize;
87     }
88
89     public void setJournalRecoveryLogBatchSize(int journalRecoveryLogBatchSize) {
90         this.journalRecoveryLogBatchSize = journalRecoveryLogBatchSize;
91     }
92
93     public void setIsolatedLeaderCheckInterval(FiniteDuration isolatedLeaderCheckInterval) {
94         this.isolatedLeaderCheckInterval = isolatedLeaderCheckInterval.toMillis();
95     }
96
97     public void setElectionTimeoutFactor(long electionTimeoutFactor){
98         this.electionTimeoutFactor = electionTimeoutFactor;
99         electionTimeOutInterval = null;
100     }
101
102     public void setCustomRaftPolicyImplementationClass(String customRaftPolicyImplementationClass){
103         this.customRaftPolicyImplementationClass = customRaftPolicyImplementationClass;
104     }
105
106     @Override
107     public String getCustomRaftPolicyImplementationClass() {
108         return customRaftPolicyImplementationClass;
109     }
110
111     @Override
112     public long getSnapshotBatchCount() {
113         return snapshotBatchCount;
114     }
115
116     @Override
117     public int getSnapshotDataThresholdPercentage() {
118         return snapshotDataThresholdPercentage;
119     }
120
121
122     @Override
123     public FiniteDuration getHeartBeatInterval() {
124         return heartBeatInterval;
125     }
126
127     @Override
128     public FiniteDuration getElectionTimeOutInterval() {
129         if(electionTimeOutInterval == null) {
130             electionTimeOutInterval = getHeartBeatInterval().$times(electionTimeoutFactor);
131         }
132
133         return electionTimeOutInterval;
134     }
135
136     @Override
137     public int getElectionTimeVariance() {
138         return ELECTION_TIME_MAX_VARIANCE;
139     }
140
141     @Override
142     public int getSnapshotChunkSize() {
143         return snapshotChunkSize;
144     }
145
146     @Override
147     public int getJournalRecoveryLogBatchSize() {
148         return journalRecoveryLogBatchSize;
149     }
150
151     @Override
152     public long getIsolatedCheckIntervalInMillis() {
153         return isolatedLeaderCheckInterval;
154     }
155
156     @Override
157     public long getElectionTimeoutFactor() {
158         return electionTimeoutFactor;
159     }
160
161     @Override
162     public RaftPolicy getRaftPolicy() {
163         return policySupplier.get();
164     }
165
166     private class PolicySupplier implements Supplier<RaftPolicy>{
167         @Override
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             try {
174                 String className = DefaultConfigParamsImpl.this.customRaftPolicyImplementationClass;
175                 LOG.info("Trying to use custom RaftPolicy {}", className);
176                 Class c = Class.forName(className);
177                 RaftPolicy obj = (RaftPolicy)c.newInstance();
178                 return obj;
179             } catch (Exception e) {
180                 if(LOG.isDebugEnabled()) {
181                     LOG.error("Could not create custom raft policy, will stick with default", e);
182                 } else {
183                     LOG.error("Could not create custom raft policy, will stick with default : cause = {}", 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 }