BUG 2185 : Make the Custom Raft Policy externally configurable
[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.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;
19
20 /**
21  * Default implementation of the ConfigParams
22  *
23  * If no implementation is provided for ConfigParams, then this will be used.
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 snaphotChunkSize = SNAPSHOT_CHUNK_SIZE;
62
63     private long electionTimeoutFactor = 2;
64     private String customRaftPolicyImplementationClass;
65
66     Supplier<RaftPolicy> policySupplier = Suppliers.memoize(new PolicySupplier());
67
68     public void setHeartBeatInterval(FiniteDuration heartBeatInterval) {
69         this.heartBeatInterval = heartBeatInterval;
70         electionTimeOutInterval = null;
71     }
72
73     public void setSnapshotBatchCount(long snapshotBatchCount) {
74         this.snapshotBatchCount = snapshotBatchCount;
75     }
76
77     public void setSnapshotDataThresholdPercentage(int snapshotDataThresholdPercentage){
78         this.snapshotDataThresholdPercentage = snapshotDataThresholdPercentage;
79     }
80
81     public void setSnaphotChunkSize(int snaphotChunkSize) {
82         this.snaphotChunkSize = snaphotChunkSize;
83     }
84
85     public void setJournalRecoveryLogBatchSize(int journalRecoveryLogBatchSize) {
86         this.journalRecoveryLogBatchSize = journalRecoveryLogBatchSize;
87     }
88
89     public void setIsolatedLeaderCheckInterval(FiniteDuration isolatedLeaderCheckInterval) {
90         this.isolatedLeaderCheckInterval = isolatedLeaderCheckInterval.toMillis();
91     }
92
93     public void setElectionTimeoutFactor(long electionTimeoutFactor){
94         this.electionTimeoutFactor = electionTimeoutFactor;
95         electionTimeOutInterval = null;
96     }
97
98     public void setCustomRaftPolicyImplementationClass(String customRaftPolicyImplementationClass){
99         this.customRaftPolicyImplementationClass = customRaftPolicyImplementationClass;
100     }
101
102     @Override
103     public long getSnapshotBatchCount() {
104         return snapshotBatchCount;
105     }
106
107     @Override
108     public int getSnapshotDataThresholdPercentage() {
109         return snapshotDataThresholdPercentage;
110     }
111
112
113     @Override
114     public FiniteDuration getHeartBeatInterval() {
115         return heartBeatInterval;
116     }
117
118     @Override
119     public FiniteDuration getElectionTimeOutInterval() {
120         if(electionTimeOutInterval == null) {
121             electionTimeOutInterval = getHeartBeatInterval().$times(electionTimeoutFactor);
122         }
123
124         return electionTimeOutInterval;
125     }
126
127     @Override
128     public int getElectionTimeVariance() {
129         return ELECTION_TIME_MAX_VARIANCE;
130     }
131
132     @Override
133     public int getSnapshotChunkSize() {
134         return snaphotChunkSize;
135     }
136
137     @Override
138     public int getJournalRecoveryLogBatchSize() {
139         return journalRecoveryLogBatchSize;
140     }
141
142     @Override
143     public long getIsolatedCheckIntervalInMillis() {
144         return isolatedLeaderCheckInterval;
145     }
146
147     @Override
148     public long getElectionTimeoutFactor() {
149         return electionTimeoutFactor;
150     }
151
152     @Override
153     public RaftPolicy getRaftPolicy() {
154         return policySupplier.get();
155     }
156
157     private class PolicySupplier implements Supplier<RaftPolicy>{
158         @Override
159         public RaftPolicy get() {
160             if(Strings.isNullOrEmpty(DefaultConfigParamsImpl.this.customRaftPolicyImplementationClass)){
161                 return DefaultRaftPolicy.INSTANCE;
162             }
163             try {
164                 String className = DefaultConfigParamsImpl.this.customRaftPolicyImplementationClass;
165                 Class c = Class.forName(className);
166                 RaftPolicy obj = (RaftPolicy)c.newInstance();
167                 return obj;
168             } catch (Exception e) {
169                 if(LOG.isDebugEnabled()) {
170                     LOG.error("Could not create custom raft policy, will stick with default", e);
171                 } else {
172                     LOG.error("Could not create custom raft policy, will stick with default : cause = {}", e.getMessage());
173                 }
174             }
175             return DefaultRaftPolicy.INSTANCE;
176         }
177     }
178 }