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