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 = 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);
51 private static Set<String> globalDatastoreTypes = Sets.newConcurrentHashSet();
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;
70 public static Set<String> getGlobalDatastoreTypes() {
71 return globalDatastoreTypes;
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);
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;
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());
108 public static Builder newBuilder() {
109 return new Builder(new DatastoreContext());
112 public static Builder newBuilderFrom(DatastoreContext context) {
113 return new Builder(new DatastoreContext(context));
116 public InMemoryDOMDataStoreConfigProperties getDataStoreProperties() {
117 return dataStoreProperties;
120 public Duration getShardTransactionIdleTimeout() {
121 return shardTransactionIdleTimeout;
124 public String getDataStoreMXBeanType() {
125 return dataStoreMXBeanType;
128 public int getOperationTimeoutInSeconds() {
129 return operationTimeoutInSeconds;
132 public ConfigParams getShardRaftConfig() {
136 public int getShardTransactionCommitTimeoutInSeconds() {
137 return shardTransactionCommitTimeoutInSeconds;
140 public int getShardTransactionCommitQueueCapacity() {
141 return shardTransactionCommitQueueCapacity;
144 public Timeout getShardInitializationTimeout() {
145 return shardInitializationTimeout;
148 public Timeout getShardLeaderElectionTimeout() {
149 return shardLeaderElectionTimeout;
152 public boolean isPersistent() {
156 public ConfigurationReader getConfigurationReader() {
157 return configurationReader;
160 public long getShardElectionTimeoutFactor(){
161 return raftConfig.getElectionTimeoutFactor();
164 public String getDataStoreType(){
165 return dataStoreType;
168 public long getTransactionCreationInitialRateLimit() {
169 return transactionCreationInitialRateLimit;
172 private void setHeartbeatInterval(long shardHeartbeatIntervalInMillis){
173 raftConfig.setHeartBeatInterval(new FiniteDuration(shardHeartbeatIntervalInMillis,
174 TimeUnit.MILLISECONDS));
177 private void setShardJournalRecoveryLogBatchSize(int shardJournalRecoveryLogBatchSize){
178 raftConfig.setJournalRecoveryLogBatchSize(shardJournalRecoveryLogBatchSize);
182 private void setIsolatedLeaderCheckInterval(long shardIsolatedLeaderCheckIntervalInMillis) {
183 raftConfig.setIsolatedLeaderCheckInterval(
184 new FiniteDuration(shardIsolatedLeaderCheckIntervalInMillis, TimeUnit.MILLISECONDS));
187 private void setElectionTimeoutFactor(long shardElectionTimeoutFactor) {
188 raftConfig.setElectionTimeoutFactor(shardElectionTimeoutFactor);
191 private void setSnapshotDataThresholdPercentage(int shardSnapshotDataThresholdPercentage) {
192 raftConfig.setSnapshotDataThresholdPercentage(shardSnapshotDataThresholdPercentage);
195 private void setSnapshotBatchCount(long shardSnapshotBatchCount) {
196 raftConfig.setSnapshotBatchCount(shardSnapshotBatchCount);
199 public int getShardBatchedModificationCount() {
200 return shardBatchedModificationCount;
203 public boolean isWriteOnlyTransactionOptimizationsEnabled() {
204 return writeOnlyTransactionOptimizationsEnabled;
207 public long getShardCommitQueueExpiryTimeoutInMillis() {
208 return shardCommitQueueExpiryTimeoutInMillis;
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;
222 private Builder(DatastoreContext datastoreContext) {
223 this.datastoreContext = datastoreContext;
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();
237 public Builder boundedMailboxCapacity(int boundedMailboxCapacity) {
238 // TODO - this is defined in the yang DataStoreProperties but not currently used.
242 public Builder enableMetricCapture(boolean enableMetricCapture) {
243 // TODO - this is defined in the yang DataStoreProperties but not currently used.
248 public Builder shardTransactionIdleTimeout(long timeout, TimeUnit unit) {
249 datastoreContext.shardTransactionIdleTimeout = Duration.create(timeout, unit);
253 public Builder shardTransactionIdleTimeoutInMinutes(long timeout) {
254 return shardTransactionIdleTimeout(timeout, TimeUnit.MINUTES);
257 public Builder operationTimeoutInSeconds(int operationTimeoutInSeconds) {
258 datastoreContext.operationTimeoutInSeconds = operationTimeoutInSeconds;
262 public Builder dataStoreMXBeanType(String dataStoreMXBeanType) {
263 datastoreContext.dataStoreMXBeanType = dataStoreMXBeanType;
267 public Builder shardTransactionCommitTimeoutInSeconds(int shardTransactionCommitTimeoutInSeconds) {
268 datastoreContext.shardTransactionCommitTimeoutInSeconds = shardTransactionCommitTimeoutInSeconds;
272 public Builder shardJournalRecoveryLogBatchSize(int shardJournalRecoveryLogBatchSize) {
273 datastoreContext.setShardJournalRecoveryLogBatchSize(shardJournalRecoveryLogBatchSize);
277 public Builder shardSnapshotBatchCount(int shardSnapshotBatchCount) {
278 datastoreContext.setSnapshotBatchCount(shardSnapshotBatchCount);
282 public Builder shardSnapshotDataThresholdPercentage(int shardSnapshotDataThresholdPercentage) {
283 datastoreContext.setSnapshotDataThresholdPercentage(shardSnapshotDataThresholdPercentage);
287 public Builder shardHeartbeatIntervalInMillis(int shardHeartbeatIntervalInMillis) {
288 datastoreContext.setHeartbeatInterval(shardHeartbeatIntervalInMillis);
292 public Builder shardTransactionCommitQueueCapacity(int shardTransactionCommitQueueCapacity) {
293 datastoreContext.shardTransactionCommitQueueCapacity = shardTransactionCommitQueueCapacity;
297 public Builder shardInitializationTimeout(long timeout, TimeUnit unit) {
298 datastoreContext.shardInitializationTimeout = new Timeout(timeout, unit);
302 public Builder shardInitializationTimeoutInSeconds(long timeout) {
303 return shardInitializationTimeout(timeout, TimeUnit.SECONDS);
306 public Builder shardLeaderElectionTimeout(long timeout, TimeUnit unit) {
307 datastoreContext.shardLeaderElectionTimeout = new Timeout(timeout, unit);
311 public Builder shardLeaderElectionTimeoutInSeconds(long timeout) {
312 return shardLeaderElectionTimeout(timeout, TimeUnit.SECONDS);
315 public Builder configurationReader(ConfigurationReader configurationReader){
316 datastoreContext.configurationReader = configurationReader;
320 public Builder persistent(boolean persistent){
321 datastoreContext.persistent = persistent;
325 public Builder shardIsolatedLeaderCheckIntervalInMillis(int shardIsolatedLeaderCheckIntervalInMillis) {
326 datastoreContext.setIsolatedLeaderCheckInterval(shardIsolatedLeaderCheckIntervalInMillis);
330 public Builder shardElectionTimeoutFactor(long shardElectionTimeoutFactor){
331 datastoreContext.setElectionTimeoutFactor(shardElectionTimeoutFactor);
335 public Builder transactionCreationInitialRateLimit(long initialRateLimit){
336 datastoreContext.transactionCreationInitialRateLimit = initialRateLimit;
340 public Builder dataStoreType(String dataStoreType){
341 datastoreContext.dataStoreType = dataStoreType;
342 datastoreContext.dataStoreMXBeanType = "Distributed" + WordUtils.capitalize(dataStoreType) + "Datastore";
346 public Builder shardBatchedModificationCount(int shardBatchedModificationCount) {
347 datastoreContext.shardBatchedModificationCount = shardBatchedModificationCount;
351 public Builder writeOnlyTransactionOptimizationsEnabled(boolean value) {
352 datastoreContext.writeOnlyTransactionOptimizationsEnabled = value;
356 public Builder shardCommitQueueExpiryTimeoutInMillis(long value) {
357 datastoreContext.shardCommitQueueExpiryTimeoutInMillis = value;
361 public Builder shardCommitQueueExpiryTimeoutInSeconds(long value) {
362 datastoreContext.shardCommitQueueExpiryTimeoutInMillis = TimeUnit.MILLISECONDS.convert(
363 value, TimeUnit.SECONDS);
367 public Builder maxShardDataChangeExecutorPoolSize(int maxShardDataChangeExecutorPoolSize) {
368 this.maxShardDataChangeExecutorPoolSize = maxShardDataChangeExecutorPoolSize;
372 public Builder maxShardDataChangeExecutorQueueSize(int maxShardDataChangeExecutorQueueSize) {
373 this.maxShardDataChangeExecutorQueueSize = maxShardDataChangeExecutorQueueSize;
377 public Builder maxShardDataChangeListenerQueueSize(int maxShardDataChangeListenerQueueSize) {
378 this.maxShardDataChangeListenerQueueSize = maxShardDataChangeListenerQueueSize;
382 public Builder maxShardDataStoreExecutorQueueSize(int maxShardDataStoreExecutorQueueSize) {
383 this.maxShardDataStoreExecutorQueueSize = maxShardDataStoreExecutorQueueSize;
387 public DatastoreContext build() {
388 datastoreContext.dataStoreProperties = InMemoryDOMDataStoreConfigProperties.create(
389 maxShardDataChangeExecutorPoolSize, maxShardDataChangeExecutorQueueSize,
390 maxShardDataChangeListenerQueueSize, maxShardDataStoreExecutorQueueSize);
392 if(datastoreContext.dataStoreType != null) {
393 globalDatastoreTypes.add(datastoreContext.dataStoreType);
396 return datastoreContext;