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