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;
69 private boolean transactionDebugContextEnabled = false;
71 public static Set<String> getGlobalDatastoreTypes() {
72 return globalDatastoreTypes;
75 private DatastoreContext() {
76 setShardJournalRecoveryLogBatchSize(DEFAULT_JOURNAL_RECOVERY_BATCH_SIZE);
77 setSnapshotBatchCount(DEFAULT_SNAPSHOT_BATCH_COUNT);
78 setHeartbeatInterval(DEFAULT_HEARTBEAT_INTERVAL_IN_MILLIS);
79 setIsolatedLeaderCheckInterval(DEFAULT_ISOLATED_LEADER_CHECK_INTERVAL_IN_MILLIS);
80 setSnapshotDataThresholdPercentage(DEFAULT_SHARD_SNAPSHOT_DATA_THRESHOLD_PERCENTAGE);
81 setElectionTimeoutFactor(DEFAULT_SHARD_ELECTION_TIMEOUT_FACTOR);
84 private DatastoreContext(DatastoreContext other) {
85 this.dataStoreProperties = other.dataStoreProperties;
86 this.shardTransactionIdleTimeout = other.shardTransactionIdleTimeout;
87 this.operationTimeoutInSeconds = other.operationTimeoutInSeconds;
88 this.dataStoreMXBeanType = other.dataStoreMXBeanType;
89 this.shardTransactionCommitTimeoutInSeconds = other.shardTransactionCommitTimeoutInSeconds;
90 this.shardTransactionCommitQueueCapacity = other.shardTransactionCommitQueueCapacity;
91 this.shardInitializationTimeout = other.shardInitializationTimeout;
92 this.shardLeaderElectionTimeout = other.shardLeaderElectionTimeout;
93 this.persistent = other.persistent;
94 this.configurationReader = other.configurationReader;
95 this.transactionCreationInitialRateLimit = other.transactionCreationInitialRateLimit;
96 this.dataStoreType = other.dataStoreType;
97 this.shardBatchedModificationCount = other.shardBatchedModificationCount;
98 this.writeOnlyTransactionOptimizationsEnabled = other.writeOnlyTransactionOptimizationsEnabled;
99 this.shardCommitQueueExpiryTimeoutInMillis = other.shardCommitQueueExpiryTimeoutInMillis;
100 this.transactionDebugContextEnabled = other.transactionDebugContextEnabled;
102 setShardJournalRecoveryLogBatchSize(other.raftConfig.getJournalRecoveryLogBatchSize());
103 setSnapshotBatchCount(other.raftConfig.getSnapshotBatchCount());
104 setHeartbeatInterval(other.raftConfig.getHeartBeatInterval().toMillis());
105 setIsolatedLeaderCheckInterval(other.raftConfig.getIsolatedCheckIntervalInMillis());
106 setSnapshotDataThresholdPercentage(other.raftConfig.getSnapshotDataThresholdPercentage());
107 setElectionTimeoutFactor(other.raftConfig.getElectionTimeoutFactor());
110 public static Builder newBuilder() {
111 return new Builder(new DatastoreContext());
114 public static Builder newBuilderFrom(DatastoreContext context) {
115 return new Builder(new DatastoreContext(context));
118 public InMemoryDOMDataStoreConfigProperties getDataStoreProperties() {
119 return dataStoreProperties;
122 public Duration getShardTransactionIdleTimeout() {
123 return shardTransactionIdleTimeout;
126 public String getDataStoreMXBeanType() {
127 return dataStoreMXBeanType;
130 public int getOperationTimeoutInSeconds() {
131 return operationTimeoutInSeconds;
134 public ConfigParams getShardRaftConfig() {
138 public int getShardTransactionCommitTimeoutInSeconds() {
139 return shardTransactionCommitTimeoutInSeconds;
142 public int getShardTransactionCommitQueueCapacity() {
143 return shardTransactionCommitQueueCapacity;
146 public Timeout getShardInitializationTimeout() {
147 return shardInitializationTimeout;
150 public Timeout getShardLeaderElectionTimeout() {
151 return shardLeaderElectionTimeout;
154 public boolean isPersistent() {
158 public ConfigurationReader getConfigurationReader() {
159 return configurationReader;
162 public long getShardElectionTimeoutFactor(){
163 return raftConfig.getElectionTimeoutFactor();
166 public String getDataStoreType(){
167 return dataStoreType;
170 public long getTransactionCreationInitialRateLimit() {
171 return transactionCreationInitialRateLimit;
174 private void setHeartbeatInterval(long shardHeartbeatIntervalInMillis){
175 raftConfig.setHeartBeatInterval(new FiniteDuration(shardHeartbeatIntervalInMillis,
176 TimeUnit.MILLISECONDS));
179 private void setShardJournalRecoveryLogBatchSize(int shardJournalRecoveryLogBatchSize){
180 raftConfig.setJournalRecoveryLogBatchSize(shardJournalRecoveryLogBatchSize);
184 private void setIsolatedLeaderCheckInterval(long shardIsolatedLeaderCheckIntervalInMillis) {
185 raftConfig.setIsolatedLeaderCheckInterval(
186 new FiniteDuration(shardIsolatedLeaderCheckIntervalInMillis, TimeUnit.MILLISECONDS));
189 private void setElectionTimeoutFactor(long shardElectionTimeoutFactor) {
190 raftConfig.setElectionTimeoutFactor(shardElectionTimeoutFactor);
193 private void setSnapshotDataThresholdPercentage(int shardSnapshotDataThresholdPercentage) {
194 raftConfig.setSnapshotDataThresholdPercentage(shardSnapshotDataThresholdPercentage);
197 private void setSnapshotBatchCount(long shardSnapshotBatchCount) {
198 raftConfig.setSnapshotBatchCount(shardSnapshotBatchCount);
201 public int getShardBatchedModificationCount() {
202 return shardBatchedModificationCount;
205 public boolean isWriteOnlyTransactionOptimizationsEnabled() {
206 return writeOnlyTransactionOptimizationsEnabled;
209 public long getShardCommitQueueExpiryTimeoutInMillis() {
210 return shardCommitQueueExpiryTimeoutInMillis;
213 public boolean isTransactionDebugContextEnabled() {
214 return transactionDebugContextEnabled;
217 public static class Builder {
218 private final DatastoreContext datastoreContext;
219 private int maxShardDataChangeExecutorPoolSize =
220 InMemoryDOMDataStoreConfigProperties.DEFAULT_MAX_DATA_CHANGE_EXECUTOR_POOL_SIZE;
221 private int maxShardDataChangeExecutorQueueSize =
222 InMemoryDOMDataStoreConfigProperties.DEFAULT_MAX_DATA_CHANGE_EXECUTOR_QUEUE_SIZE;
223 private int maxShardDataChangeListenerQueueSize =
224 InMemoryDOMDataStoreConfigProperties.DEFAULT_MAX_DATA_CHANGE_LISTENER_QUEUE_SIZE;
225 private int maxShardDataStoreExecutorQueueSize =
226 InMemoryDOMDataStoreConfigProperties.DEFAULT_MAX_DATA_STORE_EXECUTOR_QUEUE_SIZE;
228 private Builder(DatastoreContext datastoreContext) {
229 this.datastoreContext = datastoreContext;
231 if(datastoreContext.getDataStoreProperties() != null) {
232 maxShardDataChangeExecutorPoolSize =
233 datastoreContext.getDataStoreProperties().getMaxDataChangeExecutorPoolSize();
234 maxShardDataChangeExecutorQueueSize =
235 datastoreContext.getDataStoreProperties().getMaxDataChangeExecutorQueueSize();
236 maxShardDataChangeListenerQueueSize =
237 datastoreContext.getDataStoreProperties().getMaxDataChangeListenerQueueSize();
238 maxShardDataStoreExecutorQueueSize =
239 datastoreContext.getDataStoreProperties().getMaxDataStoreExecutorQueueSize();
243 public Builder boundedMailboxCapacity(int boundedMailboxCapacity) {
244 // TODO - this is defined in the yang DataStoreProperties but not currently used.
248 public Builder enableMetricCapture(boolean enableMetricCapture) {
249 // TODO - this is defined in the yang DataStoreProperties but not currently used.
254 public Builder shardTransactionIdleTimeout(long timeout, TimeUnit unit) {
255 datastoreContext.shardTransactionIdleTimeout = Duration.create(timeout, unit);
259 public Builder shardTransactionIdleTimeoutInMinutes(long timeout) {
260 return shardTransactionIdleTimeout(timeout, TimeUnit.MINUTES);
263 public Builder operationTimeoutInSeconds(int operationTimeoutInSeconds) {
264 datastoreContext.operationTimeoutInSeconds = operationTimeoutInSeconds;
268 public Builder dataStoreMXBeanType(String dataStoreMXBeanType) {
269 datastoreContext.dataStoreMXBeanType = dataStoreMXBeanType;
273 public Builder shardTransactionCommitTimeoutInSeconds(int shardTransactionCommitTimeoutInSeconds) {
274 datastoreContext.shardTransactionCommitTimeoutInSeconds = shardTransactionCommitTimeoutInSeconds;
278 public Builder shardJournalRecoveryLogBatchSize(int shardJournalRecoveryLogBatchSize) {
279 datastoreContext.setShardJournalRecoveryLogBatchSize(shardJournalRecoveryLogBatchSize);
283 public Builder shardSnapshotBatchCount(int shardSnapshotBatchCount) {
284 datastoreContext.setSnapshotBatchCount(shardSnapshotBatchCount);
288 public Builder shardSnapshotDataThresholdPercentage(int shardSnapshotDataThresholdPercentage) {
289 datastoreContext.setSnapshotDataThresholdPercentage(shardSnapshotDataThresholdPercentage);
293 public Builder shardHeartbeatIntervalInMillis(int shardHeartbeatIntervalInMillis) {
294 datastoreContext.setHeartbeatInterval(shardHeartbeatIntervalInMillis);
298 public Builder shardTransactionCommitQueueCapacity(int shardTransactionCommitQueueCapacity) {
299 datastoreContext.shardTransactionCommitQueueCapacity = shardTransactionCommitQueueCapacity;
303 public Builder shardInitializationTimeout(long timeout, TimeUnit unit) {
304 datastoreContext.shardInitializationTimeout = new Timeout(timeout, unit);
308 public Builder shardInitializationTimeoutInSeconds(long timeout) {
309 return shardInitializationTimeout(timeout, TimeUnit.SECONDS);
312 public Builder shardLeaderElectionTimeout(long timeout, TimeUnit unit) {
313 datastoreContext.shardLeaderElectionTimeout = new Timeout(timeout, unit);
317 public Builder shardLeaderElectionTimeoutInSeconds(long timeout) {
318 return shardLeaderElectionTimeout(timeout, TimeUnit.SECONDS);
321 public Builder configurationReader(ConfigurationReader configurationReader){
322 datastoreContext.configurationReader = configurationReader;
326 public Builder persistent(boolean persistent){
327 datastoreContext.persistent = persistent;
331 public Builder shardIsolatedLeaderCheckIntervalInMillis(int shardIsolatedLeaderCheckIntervalInMillis) {
332 datastoreContext.setIsolatedLeaderCheckInterval(shardIsolatedLeaderCheckIntervalInMillis);
336 public Builder shardElectionTimeoutFactor(long shardElectionTimeoutFactor){
337 datastoreContext.setElectionTimeoutFactor(shardElectionTimeoutFactor);
341 public Builder transactionCreationInitialRateLimit(long initialRateLimit){
342 datastoreContext.transactionCreationInitialRateLimit = initialRateLimit;
346 public Builder dataStoreType(String dataStoreType){
347 datastoreContext.dataStoreType = dataStoreType;
348 datastoreContext.dataStoreMXBeanType = "Distributed" + WordUtils.capitalize(dataStoreType) + "Datastore";
352 public Builder shardBatchedModificationCount(int shardBatchedModificationCount) {
353 datastoreContext.shardBatchedModificationCount = shardBatchedModificationCount;
357 public Builder writeOnlyTransactionOptimizationsEnabled(boolean value) {
358 datastoreContext.writeOnlyTransactionOptimizationsEnabled = value;
362 public Builder shardCommitQueueExpiryTimeoutInMillis(long value) {
363 datastoreContext.shardCommitQueueExpiryTimeoutInMillis = value;
367 public Builder shardCommitQueueExpiryTimeoutInSeconds(long value) {
368 datastoreContext.shardCommitQueueExpiryTimeoutInMillis = TimeUnit.MILLISECONDS.convert(
369 value, TimeUnit.SECONDS);
373 public Builder transactionDebugContextEnabled(boolean value) {
374 datastoreContext.transactionDebugContextEnabled = value;
378 public Builder maxShardDataChangeExecutorPoolSize(int maxShardDataChangeExecutorPoolSize) {
379 this.maxShardDataChangeExecutorPoolSize = maxShardDataChangeExecutorPoolSize;
383 public Builder maxShardDataChangeExecutorQueueSize(int maxShardDataChangeExecutorQueueSize) {
384 this.maxShardDataChangeExecutorQueueSize = maxShardDataChangeExecutorQueueSize;
388 public Builder maxShardDataChangeListenerQueueSize(int maxShardDataChangeListenerQueueSize) {
389 this.maxShardDataChangeListenerQueueSize = maxShardDataChangeListenerQueueSize;
393 public Builder maxShardDataStoreExecutorQueueSize(int maxShardDataStoreExecutorQueueSize) {
394 this.maxShardDataStoreExecutorQueueSize = maxShardDataStoreExecutorQueueSize;
398 public DatastoreContext build() {
399 datastoreContext.dataStoreProperties = InMemoryDOMDataStoreConfigProperties.create(
400 maxShardDataChangeExecutorPoolSize, maxShardDataChangeExecutorQueueSize,
401 maxShardDataChangeListenerQueueSize, maxShardDataStoreExecutorQueueSize);
403 if(datastoreContext.dataStoreType != null) {
404 globalDatastoreTypes.add(datastoreContext.dataStoreType);
407 return datastoreContext;