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