Merge "Avoid IllegalArgument on missing source"
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DatastoreContext.java
1 /*
2  * Copyright (c) 2014 Brocade Communications 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
9 package org.opendaylight.controller.cluster.datastore;
10
11 import akka.util.Timeout;
12 import java.util.concurrent.TimeUnit;
13 import org.apache.commons.lang3.text.WordUtils;
14 import org.opendaylight.controller.cluster.datastore.config.ConfigurationReader;
15 import org.opendaylight.controller.cluster.datastore.config.FileConfigurationReader;
16 import org.opendaylight.controller.cluster.raft.ConfigParams;
17 import org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl;
18 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStoreConfigProperties;
19 import scala.concurrent.duration.Duration;
20 import scala.concurrent.duration.FiniteDuration;
21
22 /**
23  * Contains contextual data for a data store.
24  *
25  * @author Thomas Pantelis
26  */
27 public class DatastoreContext {
28
29     public static final Duration DEFAULT_SHARD_TRANSACTION_IDLE_TIMEOUT = Duration.create(10, TimeUnit.MINUTES);
30     public static final int DEFAULT_OPERATION_TIMEOUT_IN_SECONDS = 5;
31     public static final int DEFAULT_SHARD_TX_COMMIT_TIMEOUT_IN_SECONDS = 30;
32     public static final int DEFAULT_JOURNAL_RECOVERY_BATCH_SIZE = 1000;
33     public static final int DEFAULT_SNAPSHOT_BATCH_COUNT = 20000;
34     public static final int DEFAULT_HEARTBEAT_INTERVAL_IN_MILLIS = 500;
35     public static final int DEFAULT_ISOLATED_LEADER_CHECK_INTERVAL_IN_MILLIS = DEFAULT_HEARTBEAT_INTERVAL_IN_MILLIS * 10;
36     public static final int DEFAULT_SHARD_TX_COMMIT_QUEUE_CAPACITY = 20000;
37     public static final Timeout DEFAULT_SHARD_INITIALIZATION_TIMEOUT = new Timeout(5, TimeUnit.MINUTES);
38     public static final Timeout DEFAULT_SHARD_LEADER_ELECTION_TIMEOUT = new Timeout(30, TimeUnit.SECONDS);
39     public static final boolean DEFAULT_PERSISTENT = true;
40     public static final FileConfigurationReader DEFAULT_CONFIGURATION_READER = new FileConfigurationReader();
41     public static final int DEFAULT_SHARD_SNAPSHOT_DATA_THRESHOLD_PERCENTAGE = 12;
42     public static final int DEFAULT_SHARD_ELECTION_TIMEOUT_FACTOR = 2;
43     public static final int DEFAULT_TX_CREATION_INITIAL_RATE_LIMIT = 100;
44     public static final String UNKNOWN_DATA_STORE_TYPE = "unknown";
45     public static final int DEFAULT_SHARD_BATCHED_MODIFICATION_COUNT= 100;
46
47     private InMemoryDOMDataStoreConfigProperties dataStoreProperties;
48     private Duration shardTransactionIdleTimeout = DatastoreContext.DEFAULT_SHARD_TRANSACTION_IDLE_TIMEOUT;
49     private int operationTimeoutInSeconds = DEFAULT_OPERATION_TIMEOUT_IN_SECONDS;
50     private String dataStoreMXBeanType;
51     private int shardTransactionCommitTimeoutInSeconds = DEFAULT_SHARD_TX_COMMIT_TIMEOUT_IN_SECONDS;
52     private int shardTransactionCommitQueueCapacity = DEFAULT_SHARD_TX_COMMIT_QUEUE_CAPACITY;
53     private Timeout shardInitializationTimeout = DEFAULT_SHARD_INITIALIZATION_TIMEOUT;
54     private Timeout shardLeaderElectionTimeout = DEFAULT_SHARD_LEADER_ELECTION_TIMEOUT;
55     private boolean persistent = DEFAULT_PERSISTENT;
56     private ConfigurationReader configurationReader = DEFAULT_CONFIGURATION_READER;
57     private long transactionCreationInitialRateLimit = DEFAULT_TX_CREATION_INITIAL_RATE_LIMIT;
58     private final DefaultConfigParamsImpl raftConfig = new DefaultConfigParamsImpl();
59     private String dataStoreType = UNKNOWN_DATA_STORE_TYPE;
60     private int shardBatchedModificationCount = DEFAULT_SHARD_BATCHED_MODIFICATION_COUNT;
61     private boolean writeOnlyTransactionOptimizationsEnabled = false;
62
63     private DatastoreContext() {
64         setShardJournalRecoveryLogBatchSize(DEFAULT_JOURNAL_RECOVERY_BATCH_SIZE);
65         setSnapshotBatchCount(DEFAULT_SNAPSHOT_BATCH_COUNT);
66         setHeartbeatInterval(DEFAULT_HEARTBEAT_INTERVAL_IN_MILLIS);
67         setIsolatedLeaderCheckInterval(DEFAULT_ISOLATED_LEADER_CHECK_INTERVAL_IN_MILLIS);
68         setSnapshotDataThresholdPercentage(DEFAULT_SHARD_SNAPSHOT_DATA_THRESHOLD_PERCENTAGE);
69         setElectionTimeoutFactor(DEFAULT_SHARD_ELECTION_TIMEOUT_FACTOR);
70     }
71
72     private DatastoreContext(DatastoreContext other) {
73         this.dataStoreProperties = other.dataStoreProperties;
74         this.shardTransactionIdleTimeout = other.shardTransactionIdleTimeout;
75         this.operationTimeoutInSeconds = other.operationTimeoutInSeconds;
76         this.dataStoreMXBeanType = other.dataStoreMXBeanType;
77         this.shardTransactionCommitTimeoutInSeconds = other.shardTransactionCommitTimeoutInSeconds;
78         this.shardTransactionCommitQueueCapacity = other.shardTransactionCommitQueueCapacity;
79         this.shardInitializationTimeout = other.shardInitializationTimeout;
80         this.shardLeaderElectionTimeout = other.shardLeaderElectionTimeout;
81         this.persistent = other.persistent;
82         this.configurationReader = other.configurationReader;
83         this.transactionCreationInitialRateLimit = other.transactionCreationInitialRateLimit;
84         this.dataStoreType = other.dataStoreType;
85         this.shardBatchedModificationCount = other.shardBatchedModificationCount;
86         this.writeOnlyTransactionOptimizationsEnabled = other.writeOnlyTransactionOptimizationsEnabled;
87
88         setShardJournalRecoveryLogBatchSize(other.raftConfig.getJournalRecoveryLogBatchSize());
89         setSnapshotBatchCount(other.raftConfig.getSnapshotBatchCount());
90         setHeartbeatInterval(other.raftConfig.getHeartBeatInterval().toMillis());
91         setIsolatedLeaderCheckInterval(other.raftConfig.getIsolatedCheckIntervalInMillis());
92         setSnapshotDataThresholdPercentage(other.raftConfig.getSnapshotDataThresholdPercentage());
93         setElectionTimeoutFactor(other.raftConfig.getElectionTimeoutFactor());
94     }
95
96     public static Builder newBuilder() {
97         return new Builder(new DatastoreContext());
98     }
99
100     public static Builder newBuilderFrom(DatastoreContext context) {
101         return new Builder(new DatastoreContext(context));
102     }
103
104     public InMemoryDOMDataStoreConfigProperties getDataStoreProperties() {
105         return dataStoreProperties;
106     }
107
108     public Duration getShardTransactionIdleTimeout() {
109         return shardTransactionIdleTimeout;
110     }
111
112     public String getDataStoreMXBeanType() {
113         return dataStoreMXBeanType;
114     }
115
116     public int getOperationTimeoutInSeconds() {
117         return operationTimeoutInSeconds;
118     }
119
120     public ConfigParams getShardRaftConfig() {
121         return raftConfig;
122     }
123
124     public int getShardTransactionCommitTimeoutInSeconds() {
125         return shardTransactionCommitTimeoutInSeconds;
126     }
127
128     public int getShardTransactionCommitQueueCapacity() {
129         return shardTransactionCommitQueueCapacity;
130     }
131
132     public Timeout getShardInitializationTimeout() {
133         return shardInitializationTimeout;
134     }
135
136     public Timeout getShardLeaderElectionTimeout() {
137         return shardLeaderElectionTimeout;
138     }
139
140     public boolean isPersistent() {
141         return persistent;
142     }
143
144     public ConfigurationReader getConfigurationReader() {
145         return configurationReader;
146     }
147
148     public long getShardElectionTimeoutFactor(){
149         return raftConfig.getElectionTimeoutFactor();
150     }
151
152     public String getDataStoreType(){
153         return dataStoreType;
154     }
155
156     public long getTransactionCreationInitialRateLimit() {
157         return transactionCreationInitialRateLimit;
158     }
159
160     private void setHeartbeatInterval(long shardHeartbeatIntervalInMillis){
161         raftConfig.setHeartBeatInterval(new FiniteDuration(shardHeartbeatIntervalInMillis,
162                 TimeUnit.MILLISECONDS));
163     }
164
165     private void setShardJournalRecoveryLogBatchSize(int shardJournalRecoveryLogBatchSize){
166         raftConfig.setJournalRecoveryLogBatchSize(shardJournalRecoveryLogBatchSize);
167     }
168
169
170     private void setIsolatedLeaderCheckInterval(long shardIsolatedLeaderCheckIntervalInMillis) {
171         raftConfig.setIsolatedLeaderCheckInterval(
172                 new FiniteDuration(shardIsolatedLeaderCheckIntervalInMillis, TimeUnit.MILLISECONDS));
173     }
174
175     private void setElectionTimeoutFactor(long shardElectionTimeoutFactor) {
176         raftConfig.setElectionTimeoutFactor(shardElectionTimeoutFactor);
177     }
178
179     private void setSnapshotDataThresholdPercentage(int shardSnapshotDataThresholdPercentage) {
180         raftConfig.setSnapshotDataThresholdPercentage(shardSnapshotDataThresholdPercentage);
181     }
182
183     private void setSnapshotBatchCount(long shardSnapshotBatchCount) {
184         raftConfig.setSnapshotBatchCount(shardSnapshotBatchCount);
185     }
186
187     public int getShardBatchedModificationCount() {
188         return shardBatchedModificationCount;
189     }
190
191     public boolean isWriteOnlyTransactionOptimizationsEnabled() {
192         return writeOnlyTransactionOptimizationsEnabled;
193     }
194
195     public static class Builder {
196         private final DatastoreContext datastoreContext;
197         private int maxShardDataChangeExecutorPoolSize =
198                 InMemoryDOMDataStoreConfigProperties.DEFAULT_MAX_DATA_CHANGE_EXECUTOR_POOL_SIZE;
199         private int maxShardDataChangeExecutorQueueSize =
200                 InMemoryDOMDataStoreConfigProperties.DEFAULT_MAX_DATA_CHANGE_EXECUTOR_QUEUE_SIZE;
201         private int maxShardDataChangeListenerQueueSize =
202                 InMemoryDOMDataStoreConfigProperties.DEFAULT_MAX_DATA_CHANGE_LISTENER_QUEUE_SIZE;
203         private int maxShardDataStoreExecutorQueueSize =
204                 InMemoryDOMDataStoreConfigProperties.DEFAULT_MAX_DATA_STORE_EXECUTOR_QUEUE_SIZE;
205
206         private Builder(DatastoreContext datastoreContext) {
207             this.datastoreContext = datastoreContext;
208
209             if(datastoreContext.getDataStoreProperties() != null) {
210                 maxShardDataChangeExecutorPoolSize =
211                         datastoreContext.getDataStoreProperties().getMaxDataChangeExecutorPoolSize();
212                 maxShardDataChangeExecutorQueueSize =
213                         datastoreContext.getDataStoreProperties().getMaxDataChangeExecutorQueueSize();
214                 maxShardDataChangeListenerQueueSize =
215                         datastoreContext.getDataStoreProperties().getMaxDataChangeListenerQueueSize();
216                 maxShardDataStoreExecutorQueueSize =
217                         datastoreContext.getDataStoreProperties().getMaxDataStoreExecutorQueueSize();
218             }
219         }
220
221         public Builder boundedMailboxCapacity(int boundedMailboxCapacity) {
222             // TODO - this is defined in the yang DataStoreProperties but not currently used.
223             return this;
224         }
225
226         public Builder enableMetricCapture(boolean enableMetricCapture) {
227             // TODO - this is defined in the yang DataStoreProperties but not currently used.
228             return this;
229         }
230
231
232         public Builder shardTransactionIdleTimeout(long timeout, TimeUnit unit) {
233             datastoreContext.shardTransactionIdleTimeout = Duration.create(timeout, unit);
234             return this;
235         }
236
237         public Builder shardTransactionIdleTimeoutInMinutes(long timeout) {
238             return shardTransactionIdleTimeout(timeout, TimeUnit.MINUTES);
239         }
240
241         public Builder operationTimeoutInSeconds(int operationTimeoutInSeconds) {
242             datastoreContext.operationTimeoutInSeconds = operationTimeoutInSeconds;
243             return this;
244         }
245
246         public Builder dataStoreMXBeanType(String dataStoreMXBeanType) {
247             datastoreContext.dataStoreMXBeanType = dataStoreMXBeanType;
248             return this;
249         }
250
251         public Builder shardTransactionCommitTimeoutInSeconds(int shardTransactionCommitTimeoutInSeconds) {
252             datastoreContext.shardTransactionCommitTimeoutInSeconds = shardTransactionCommitTimeoutInSeconds;
253             return this;
254         }
255
256         public Builder shardJournalRecoveryLogBatchSize(int shardJournalRecoveryLogBatchSize) {
257             datastoreContext.setShardJournalRecoveryLogBatchSize(shardJournalRecoveryLogBatchSize);
258             return this;
259         }
260
261         public Builder shardSnapshotBatchCount(int shardSnapshotBatchCount) {
262             datastoreContext.setSnapshotBatchCount(shardSnapshotBatchCount);
263             return this;
264         }
265
266         public Builder shardSnapshotDataThresholdPercentage(int shardSnapshotDataThresholdPercentage) {
267             datastoreContext.setSnapshotDataThresholdPercentage(shardSnapshotDataThresholdPercentage);
268             return this;
269         }
270
271         public Builder shardHeartbeatIntervalInMillis(int shardHeartbeatIntervalInMillis) {
272             datastoreContext.setHeartbeatInterval(shardHeartbeatIntervalInMillis);
273             return this;
274         }
275
276         public Builder shardTransactionCommitQueueCapacity(int shardTransactionCommitQueueCapacity) {
277             datastoreContext.shardTransactionCommitQueueCapacity = shardTransactionCommitQueueCapacity;
278             return this;
279         }
280
281         public Builder shardInitializationTimeout(long timeout, TimeUnit unit) {
282             datastoreContext.shardInitializationTimeout = new Timeout(timeout, unit);
283             return this;
284         }
285
286         public Builder shardInitializationTimeoutInSeconds(long timeout) {
287             return shardInitializationTimeout(timeout, TimeUnit.SECONDS);
288         }
289
290         public Builder shardLeaderElectionTimeout(long timeout, TimeUnit unit) {
291             datastoreContext.shardLeaderElectionTimeout = new Timeout(timeout, unit);
292             return this;
293         }
294
295         public Builder shardLeaderElectionTimeoutInSeconds(long timeout) {
296             return shardLeaderElectionTimeout(timeout, TimeUnit.SECONDS);
297         }
298
299         public Builder configurationReader(ConfigurationReader configurationReader){
300             datastoreContext.configurationReader = configurationReader;
301             return this;
302         }
303
304         public Builder persistent(boolean persistent){
305             datastoreContext.persistent = persistent;
306             return this;
307         }
308
309         public Builder shardIsolatedLeaderCheckIntervalInMillis(int shardIsolatedLeaderCheckIntervalInMillis) {
310             datastoreContext.setIsolatedLeaderCheckInterval(shardIsolatedLeaderCheckIntervalInMillis);
311             return this;
312         }
313
314         public Builder shardElectionTimeoutFactor(long shardElectionTimeoutFactor){
315             datastoreContext.setElectionTimeoutFactor(shardElectionTimeoutFactor);
316             return this;
317         }
318
319         public Builder transactionCreationInitialRateLimit(long initialRateLimit){
320             datastoreContext.transactionCreationInitialRateLimit = initialRateLimit;
321             return this;
322         }
323
324         public Builder dataStoreType(String dataStoreType){
325             datastoreContext.dataStoreType = dataStoreType;
326             datastoreContext.dataStoreMXBeanType = "Distributed" + WordUtils.capitalize(dataStoreType) + "Datastore";
327             return this;
328         }
329
330         public Builder shardBatchedModificationCount(int shardBatchedModificationCount) {
331             datastoreContext.shardBatchedModificationCount = shardBatchedModificationCount;
332             return this;
333         }
334
335         public Builder writeOnlyTransactionOptimizationsEnabled(boolean value) {
336             datastoreContext.writeOnlyTransactionOptimizationsEnabled = value;
337             return this;
338         }
339
340         public Builder maxShardDataChangeExecutorPoolSize(int maxShardDataChangeExecutorPoolSize) {
341             this.maxShardDataChangeExecutorPoolSize = maxShardDataChangeExecutorPoolSize;
342             return this;
343         }
344
345         public Builder maxShardDataChangeExecutorQueueSize(int maxShardDataChangeExecutorQueueSize) {
346             this.maxShardDataChangeExecutorQueueSize = maxShardDataChangeExecutorQueueSize;
347             return this;
348         }
349
350         public Builder maxShardDataChangeListenerQueueSize(int maxShardDataChangeListenerQueueSize) {
351             this.maxShardDataChangeListenerQueueSize = maxShardDataChangeListenerQueueSize;
352             return this;
353         }
354
355         public Builder maxShardDataStoreExecutorQueueSize(int maxShardDataStoreExecutorQueueSize) {
356             this.maxShardDataStoreExecutorQueueSize = maxShardDataStoreExecutorQueueSize;
357             return this;
358         }
359
360         public DatastoreContext build() {
361             datastoreContext.dataStoreProperties = InMemoryDOMDataStoreConfigProperties.create(
362                     maxShardDataChangeExecutorPoolSize, maxShardDataChangeExecutorQueueSize,
363                     maxShardDataChangeListenerQueueSize, maxShardDataStoreExecutorQueueSize);
364             return datastoreContext;
365         }
366     }
367 }