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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.