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