2 * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.cluster.datastore;
11 import akka.util.Timeout;
12 import com.google.common.collect.Sets;
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;
25 * Contains contextual data for a data store.
27 * @author Thomas Pantelis
29 public class DatastoreContext {
30 public static final String METRICS_DOMAIN = "org.opendaylight.controller.cluster.datastore";
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;
50 private static Set<String> globalDatastoreTypes = Sets.newConcurrentHashSet();
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 = false;
68 public static Set<String> getGlobalDatastoreTypes() {
69 return globalDatastoreTypes;
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);
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;
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());
105 public static Builder newBuilder() {
106 return new Builder(new DatastoreContext());
109 public static Builder newBuilderFrom(DatastoreContext context) {
110 return new Builder(new DatastoreContext(context));
113 public InMemoryDOMDataStoreConfigProperties getDataStoreProperties() {
114 return dataStoreProperties;
117 public Duration getShardTransactionIdleTimeout() {
118 return shardTransactionIdleTimeout;
121 public String getDataStoreMXBeanType() {
122 return dataStoreMXBeanType;
125 public int getOperationTimeoutInSeconds() {
126 return operationTimeoutInSeconds;
129 public ConfigParams getShardRaftConfig() {
133 public int getShardTransactionCommitTimeoutInSeconds() {
134 return shardTransactionCommitTimeoutInSeconds;
137 public int getShardTransactionCommitQueueCapacity() {
138 return shardTransactionCommitQueueCapacity;
141 public Timeout getShardInitializationTimeout() {
142 return shardInitializationTimeout;
145 public Timeout getShardLeaderElectionTimeout() {
146 return shardLeaderElectionTimeout;
149 public boolean isPersistent() {
153 public ConfigurationReader getConfigurationReader() {
154 return configurationReader;
157 public long getShardElectionTimeoutFactor(){
158 return raftConfig.getElectionTimeoutFactor();
161 public String getDataStoreType(){
162 return dataStoreType;
165 public long getTransactionCreationInitialRateLimit() {
166 return transactionCreationInitialRateLimit;
169 private void setHeartbeatInterval(long shardHeartbeatIntervalInMillis){
170 raftConfig.setHeartBeatInterval(new FiniteDuration(shardHeartbeatIntervalInMillis,
171 TimeUnit.MILLISECONDS));
174 private void setShardJournalRecoveryLogBatchSize(int shardJournalRecoveryLogBatchSize){
175 raftConfig.setJournalRecoveryLogBatchSize(shardJournalRecoveryLogBatchSize);
179 private void setIsolatedLeaderCheckInterval(long shardIsolatedLeaderCheckIntervalInMillis) {
180 raftConfig.setIsolatedLeaderCheckInterval(
181 new FiniteDuration(shardIsolatedLeaderCheckIntervalInMillis, TimeUnit.MILLISECONDS));
184 private void setElectionTimeoutFactor(long shardElectionTimeoutFactor) {
185 raftConfig.setElectionTimeoutFactor(shardElectionTimeoutFactor);
188 private void setSnapshotDataThresholdPercentage(int shardSnapshotDataThresholdPercentage) {
189 raftConfig.setSnapshotDataThresholdPercentage(shardSnapshotDataThresholdPercentage);
192 private void setSnapshotBatchCount(long shardSnapshotBatchCount) {
193 raftConfig.setSnapshotBatchCount(shardSnapshotBatchCount);
196 public int getShardBatchedModificationCount() {
197 return shardBatchedModificationCount;
200 public boolean isWriteOnlyTransactionOptimizationsEnabled() {
201 return writeOnlyTransactionOptimizationsEnabled;
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;
215 private Builder(DatastoreContext datastoreContext) {
216 this.datastoreContext = datastoreContext;
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();
230 public Builder boundedMailboxCapacity(int boundedMailboxCapacity) {
231 // TODO - this is defined in the yang DataStoreProperties but not currently used.
235 public Builder enableMetricCapture(boolean enableMetricCapture) {
236 // TODO - this is defined in the yang DataStoreProperties but not currently used.
241 public Builder shardTransactionIdleTimeout(long timeout, TimeUnit unit) {
242 datastoreContext.shardTransactionIdleTimeout = Duration.create(timeout, unit);
246 public Builder shardTransactionIdleTimeoutInMinutes(long timeout) {
247 return shardTransactionIdleTimeout(timeout, TimeUnit.MINUTES);
250 public Builder operationTimeoutInSeconds(int operationTimeoutInSeconds) {
251 datastoreContext.operationTimeoutInSeconds = operationTimeoutInSeconds;
255 public Builder dataStoreMXBeanType(String dataStoreMXBeanType) {
256 datastoreContext.dataStoreMXBeanType = dataStoreMXBeanType;
260 public Builder shardTransactionCommitTimeoutInSeconds(int shardTransactionCommitTimeoutInSeconds) {
261 datastoreContext.shardTransactionCommitTimeoutInSeconds = shardTransactionCommitTimeoutInSeconds;
265 public Builder shardJournalRecoveryLogBatchSize(int shardJournalRecoveryLogBatchSize) {
266 datastoreContext.setShardJournalRecoveryLogBatchSize(shardJournalRecoveryLogBatchSize);
270 public Builder shardSnapshotBatchCount(int shardSnapshotBatchCount) {
271 datastoreContext.setSnapshotBatchCount(shardSnapshotBatchCount);
275 public Builder shardSnapshotDataThresholdPercentage(int shardSnapshotDataThresholdPercentage) {
276 datastoreContext.setSnapshotDataThresholdPercentage(shardSnapshotDataThresholdPercentage);
280 public Builder shardHeartbeatIntervalInMillis(int shardHeartbeatIntervalInMillis) {
281 datastoreContext.setHeartbeatInterval(shardHeartbeatIntervalInMillis);
285 public Builder shardTransactionCommitQueueCapacity(int shardTransactionCommitQueueCapacity) {
286 datastoreContext.shardTransactionCommitQueueCapacity = shardTransactionCommitQueueCapacity;
290 public Builder shardInitializationTimeout(long timeout, TimeUnit unit) {
291 datastoreContext.shardInitializationTimeout = new Timeout(timeout, unit);
295 public Builder shardInitializationTimeoutInSeconds(long timeout) {
296 return shardInitializationTimeout(timeout, TimeUnit.SECONDS);
299 public Builder shardLeaderElectionTimeout(long timeout, TimeUnit unit) {
300 datastoreContext.shardLeaderElectionTimeout = new Timeout(timeout, unit);
304 public Builder shardLeaderElectionTimeoutInSeconds(long timeout) {
305 return shardLeaderElectionTimeout(timeout, TimeUnit.SECONDS);
308 public Builder configurationReader(ConfigurationReader configurationReader){
309 datastoreContext.configurationReader = configurationReader;
313 public Builder persistent(boolean persistent){
314 datastoreContext.persistent = persistent;
318 public Builder shardIsolatedLeaderCheckIntervalInMillis(int shardIsolatedLeaderCheckIntervalInMillis) {
319 datastoreContext.setIsolatedLeaderCheckInterval(shardIsolatedLeaderCheckIntervalInMillis);
323 public Builder shardElectionTimeoutFactor(long shardElectionTimeoutFactor){
324 datastoreContext.setElectionTimeoutFactor(shardElectionTimeoutFactor);
328 public Builder transactionCreationInitialRateLimit(long initialRateLimit){
329 datastoreContext.transactionCreationInitialRateLimit = initialRateLimit;
333 public Builder dataStoreType(String dataStoreType){
334 datastoreContext.dataStoreType = dataStoreType;
335 datastoreContext.dataStoreMXBeanType = "Distributed" + WordUtils.capitalize(dataStoreType) + "Datastore";
339 public Builder shardBatchedModificationCount(int shardBatchedModificationCount) {
340 datastoreContext.shardBatchedModificationCount = shardBatchedModificationCount;
344 public Builder writeOnlyTransactionOptimizationsEnabled(boolean value) {
345 datastoreContext.writeOnlyTransactionOptimizationsEnabled = value;
349 public Builder maxShardDataChangeExecutorPoolSize(int maxShardDataChangeExecutorPoolSize) {
350 this.maxShardDataChangeExecutorPoolSize = maxShardDataChangeExecutorPoolSize;
354 public Builder maxShardDataChangeExecutorQueueSize(int maxShardDataChangeExecutorQueueSize) {
355 this.maxShardDataChangeExecutorQueueSize = maxShardDataChangeExecutorQueueSize;
359 public Builder maxShardDataChangeListenerQueueSize(int maxShardDataChangeListenerQueueSize) {
360 this.maxShardDataChangeListenerQueueSize = maxShardDataChangeListenerQueueSize;
364 public Builder maxShardDataStoreExecutorQueueSize(int maxShardDataStoreExecutorQueueSize) {
365 this.maxShardDataStoreExecutorQueueSize = maxShardDataStoreExecutorQueueSize;
369 public DatastoreContext build() {
370 datastoreContext.dataStoreProperties = InMemoryDOMDataStoreConfigProperties.create(
371 maxShardDataChangeExecutorPoolSize, maxShardDataChangeExecutorQueueSize,
372 maxShardDataChangeListenerQueueSize, maxShardDataStoreExecutorQueueSize);
374 if(datastoreContext.dataStoreType != null) {
375 globalDatastoreTypes.add(datastoreContext.dataStoreType);
378 return datastoreContext;