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 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;
23 * Contains contextual data for a data store.
25 * @author Thomas Pantelis
27 public class DatastoreContext {
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;
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 private boolean writeOnlyTransactionOptimizationsEnabled = false;
63 private DatastoreContext() {
64 setShardJournalRecoveryLogBatchSize(DEFAULT_JOURNAL_RECOVERY_BATCH_SIZE);
65 setSnapshotBatchCount(DEFAULT_SNAPSHOT_BATCH_COUNT);
66 setHeartbeatInterval(DEFAULT_HEARTBEAT_INTERVAL_IN_MILLIS);
67 setIsolatedLeaderCheckInterval(DEFAULT_ISOLATED_LEADER_CHECK_INTERVAL_IN_MILLIS);
68 setSnapshotDataThresholdPercentage(DEFAULT_SHARD_SNAPSHOT_DATA_THRESHOLD_PERCENTAGE);
69 setElectionTimeoutFactor(DEFAULT_SHARD_ELECTION_TIMEOUT_FACTOR);
72 private DatastoreContext(DatastoreContext other) {
73 this.dataStoreProperties = other.dataStoreProperties;
74 this.shardTransactionIdleTimeout = other.shardTransactionIdleTimeout;
75 this.operationTimeoutInSeconds = other.operationTimeoutInSeconds;
76 this.dataStoreMXBeanType = other.dataStoreMXBeanType;
77 this.shardTransactionCommitTimeoutInSeconds = other.shardTransactionCommitTimeoutInSeconds;
78 this.shardTransactionCommitQueueCapacity = other.shardTransactionCommitQueueCapacity;
79 this.shardInitializationTimeout = other.shardInitializationTimeout;
80 this.shardLeaderElectionTimeout = other.shardLeaderElectionTimeout;
81 this.persistent = other.persistent;
82 this.configurationReader = other.configurationReader;
83 this.transactionCreationInitialRateLimit = other.transactionCreationInitialRateLimit;
84 this.dataStoreType = other.dataStoreType;
85 this.shardBatchedModificationCount = other.shardBatchedModificationCount;
86 this.writeOnlyTransactionOptimizationsEnabled = other.writeOnlyTransactionOptimizationsEnabled;
88 setShardJournalRecoveryLogBatchSize(other.raftConfig.getJournalRecoveryLogBatchSize());
89 setSnapshotBatchCount(other.raftConfig.getSnapshotBatchCount());
90 setHeartbeatInterval(other.raftConfig.getHeartBeatInterval().toMillis());
91 setIsolatedLeaderCheckInterval(other.raftConfig.getIsolatedCheckIntervalInMillis());
92 setSnapshotDataThresholdPercentage(other.raftConfig.getSnapshotDataThresholdPercentage());
93 setElectionTimeoutFactor(other.raftConfig.getElectionTimeoutFactor());
96 public static Builder newBuilder() {
97 return new Builder(new DatastoreContext());
100 public static Builder newBuilderFrom(DatastoreContext context) {
101 return new Builder(new DatastoreContext(context));
104 public InMemoryDOMDataStoreConfigProperties getDataStoreProperties() {
105 return dataStoreProperties;
108 public Duration getShardTransactionIdleTimeout() {
109 return shardTransactionIdleTimeout;
112 public String getDataStoreMXBeanType() {
113 return dataStoreMXBeanType;
116 public int getOperationTimeoutInSeconds() {
117 return operationTimeoutInSeconds;
120 public ConfigParams getShardRaftConfig() {
124 public int getShardTransactionCommitTimeoutInSeconds() {
125 return shardTransactionCommitTimeoutInSeconds;
128 public int getShardTransactionCommitQueueCapacity() {
129 return shardTransactionCommitQueueCapacity;
132 public Timeout getShardInitializationTimeout() {
133 return shardInitializationTimeout;
136 public Timeout getShardLeaderElectionTimeout() {
137 return shardLeaderElectionTimeout;
140 public boolean isPersistent() {
144 public ConfigurationReader getConfigurationReader() {
145 return configurationReader;
148 public long getShardElectionTimeoutFactor(){
149 return raftConfig.getElectionTimeoutFactor();
152 public String getDataStoreType(){
153 return dataStoreType;
156 public long getTransactionCreationInitialRateLimit() {
157 return transactionCreationInitialRateLimit;
160 private void setHeartbeatInterval(long shardHeartbeatIntervalInMillis){
161 raftConfig.setHeartBeatInterval(new FiniteDuration(shardHeartbeatIntervalInMillis,
162 TimeUnit.MILLISECONDS));
165 private void setShardJournalRecoveryLogBatchSize(int shardJournalRecoveryLogBatchSize){
166 raftConfig.setJournalRecoveryLogBatchSize(shardJournalRecoveryLogBatchSize);
170 private void setIsolatedLeaderCheckInterval(long shardIsolatedLeaderCheckIntervalInMillis) {
171 raftConfig.setIsolatedLeaderCheckInterval(
172 new FiniteDuration(shardIsolatedLeaderCheckIntervalInMillis, TimeUnit.MILLISECONDS));
175 private void setElectionTimeoutFactor(long shardElectionTimeoutFactor) {
176 raftConfig.setElectionTimeoutFactor(shardElectionTimeoutFactor);
179 private void setSnapshotDataThresholdPercentage(int shardSnapshotDataThresholdPercentage) {
180 raftConfig.setSnapshotDataThresholdPercentage(shardSnapshotDataThresholdPercentage);
183 private void setSnapshotBatchCount(long shardSnapshotBatchCount) {
184 raftConfig.setSnapshotBatchCount(shardSnapshotBatchCount);
187 public int getShardBatchedModificationCount() {
188 return shardBatchedModificationCount;
191 public boolean isWriteOnlyTransactionOptimizationsEnabled() {
192 return writeOnlyTransactionOptimizationsEnabled;
195 public static class Builder {
196 private final DatastoreContext datastoreContext;
197 private int maxShardDataChangeExecutorPoolSize =
198 InMemoryDOMDataStoreConfigProperties.DEFAULT_MAX_DATA_CHANGE_EXECUTOR_POOL_SIZE;
199 private int maxShardDataChangeExecutorQueueSize =
200 InMemoryDOMDataStoreConfigProperties.DEFAULT_MAX_DATA_CHANGE_EXECUTOR_QUEUE_SIZE;
201 private int maxShardDataChangeListenerQueueSize =
202 InMemoryDOMDataStoreConfigProperties.DEFAULT_MAX_DATA_CHANGE_LISTENER_QUEUE_SIZE;
203 private int maxShardDataStoreExecutorQueueSize =
204 InMemoryDOMDataStoreConfigProperties.DEFAULT_MAX_DATA_STORE_EXECUTOR_QUEUE_SIZE;
206 private Builder(DatastoreContext datastoreContext) {
207 this.datastoreContext = datastoreContext;
209 if(datastoreContext.getDataStoreProperties() != null) {
210 maxShardDataChangeExecutorPoolSize =
211 datastoreContext.getDataStoreProperties().getMaxDataChangeExecutorPoolSize();
212 maxShardDataChangeExecutorQueueSize =
213 datastoreContext.getDataStoreProperties().getMaxDataChangeExecutorQueueSize();
214 maxShardDataChangeListenerQueueSize =
215 datastoreContext.getDataStoreProperties().getMaxDataChangeListenerQueueSize();
216 maxShardDataStoreExecutorQueueSize =
217 datastoreContext.getDataStoreProperties().getMaxDataStoreExecutorQueueSize();
221 public Builder boundedMailboxCapacity(int boundedMailboxCapacity) {
222 // TODO - this is defined in the yang DataStoreProperties but not currently used.
226 public Builder enableMetricCapture(boolean enableMetricCapture) {
227 // TODO - this is defined in the yang DataStoreProperties but not currently used.
232 public Builder shardTransactionIdleTimeout(long timeout, TimeUnit unit) {
233 datastoreContext.shardTransactionIdleTimeout = Duration.create(timeout, unit);
237 public Builder shardTransactionIdleTimeoutInMinutes(long timeout) {
238 return shardTransactionIdleTimeout(timeout, TimeUnit.MINUTES);
241 public Builder operationTimeoutInSeconds(int operationTimeoutInSeconds) {
242 datastoreContext.operationTimeoutInSeconds = operationTimeoutInSeconds;
246 public Builder dataStoreMXBeanType(String dataStoreMXBeanType) {
247 datastoreContext.dataStoreMXBeanType = dataStoreMXBeanType;
251 public Builder shardTransactionCommitTimeoutInSeconds(int shardTransactionCommitTimeoutInSeconds) {
252 datastoreContext.shardTransactionCommitTimeoutInSeconds = shardTransactionCommitTimeoutInSeconds;
256 public Builder shardJournalRecoveryLogBatchSize(int shardJournalRecoveryLogBatchSize) {
257 datastoreContext.setShardJournalRecoveryLogBatchSize(shardJournalRecoveryLogBatchSize);
261 public Builder shardSnapshotBatchCount(int shardSnapshotBatchCount) {
262 datastoreContext.setSnapshotBatchCount(shardSnapshotBatchCount);
266 public Builder shardSnapshotDataThresholdPercentage(int shardSnapshotDataThresholdPercentage) {
267 datastoreContext.setSnapshotDataThresholdPercentage(shardSnapshotDataThresholdPercentage);
271 public Builder shardHeartbeatIntervalInMillis(int shardHeartbeatIntervalInMillis) {
272 datastoreContext.setHeartbeatInterval(shardHeartbeatIntervalInMillis);
276 public Builder shardTransactionCommitQueueCapacity(int shardTransactionCommitQueueCapacity) {
277 datastoreContext.shardTransactionCommitQueueCapacity = shardTransactionCommitQueueCapacity;
281 public Builder shardInitializationTimeout(long timeout, TimeUnit unit) {
282 datastoreContext.shardInitializationTimeout = new Timeout(timeout, unit);
286 public Builder shardInitializationTimeoutInSeconds(long timeout) {
287 return shardInitializationTimeout(timeout, TimeUnit.SECONDS);
290 public Builder shardLeaderElectionTimeout(long timeout, TimeUnit unit) {
291 datastoreContext.shardLeaderElectionTimeout = new Timeout(timeout, unit);
295 public Builder shardLeaderElectionTimeoutInSeconds(long timeout) {
296 return shardLeaderElectionTimeout(timeout, TimeUnit.SECONDS);
299 public Builder configurationReader(ConfigurationReader configurationReader){
300 datastoreContext.configurationReader = configurationReader;
304 public Builder persistent(boolean persistent){
305 datastoreContext.persistent = persistent;
309 public Builder shardIsolatedLeaderCheckIntervalInMillis(int shardIsolatedLeaderCheckIntervalInMillis) {
310 datastoreContext.setIsolatedLeaderCheckInterval(shardIsolatedLeaderCheckIntervalInMillis);
314 public Builder shardElectionTimeoutFactor(long shardElectionTimeoutFactor){
315 datastoreContext.setElectionTimeoutFactor(shardElectionTimeoutFactor);
319 public Builder transactionCreationInitialRateLimit(long initialRateLimit){
320 datastoreContext.transactionCreationInitialRateLimit = initialRateLimit;
324 public Builder dataStoreType(String dataStoreType){
325 datastoreContext.dataStoreType = dataStoreType;
326 datastoreContext.dataStoreMXBeanType = "Distributed" + WordUtils.capitalize(dataStoreType) + "Datastore";
330 public Builder shardBatchedModificationCount(int shardBatchedModificationCount) {
331 datastoreContext.shardBatchedModificationCount = shardBatchedModificationCount;
335 public Builder writeOnlyTransactionOptimizationsEnabled(boolean value) {
336 datastoreContext.writeOnlyTransactionOptimizationsEnabled = value;
340 public Builder maxShardDataChangeExecutorPoolSize(int maxShardDataChangeExecutorPoolSize) {
341 this.maxShardDataChangeExecutorPoolSize = maxShardDataChangeExecutorPoolSize;
345 public Builder maxShardDataChangeExecutorQueueSize(int maxShardDataChangeExecutorQueueSize) {
346 this.maxShardDataChangeExecutorQueueSize = maxShardDataChangeExecutorQueueSize;
350 public Builder maxShardDataChangeListenerQueueSize(int maxShardDataChangeListenerQueueSize) {
351 this.maxShardDataChangeListenerQueueSize = maxShardDataChangeListenerQueueSize;
355 public Builder maxShardDataStoreExecutorQueueSize(int maxShardDataStoreExecutorQueueSize) {
356 this.maxShardDataStoreExecutorQueueSize = maxShardDataStoreExecutorQueueSize;
360 public DatastoreContext build() {
361 datastoreContext.dataStoreProperties = InMemoryDOMDataStoreConfigProperties.create(
362 maxShardDataChangeExecutorPoolSize, maxShardDataChangeExecutorQueueSize,
363 maxShardDataChangeListenerQueueSize, maxShardDataStoreExecutorQueueSize);
364 return datastoreContext;