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;
62 private DatastoreContext() {
63 setShardJournalRecoveryLogBatchSize(DEFAULT_JOURNAL_RECOVERY_BATCH_SIZE);
64 setSnapshotBatchCount(DEFAULT_SNAPSHOT_BATCH_COUNT);
65 setHeartbeatInterval(DEFAULT_HEARTBEAT_INTERVAL_IN_MILLIS);
66 setIsolatedLeaderCheckInterval(DEFAULT_ISOLATED_LEADER_CHECK_INTERVAL_IN_MILLIS);
67 setSnapshotDataThresholdPercentage(DEFAULT_SHARD_SNAPSHOT_DATA_THRESHOLD_PERCENTAGE);
68 setElectionTimeoutFactor(DEFAULT_SHARD_ELECTION_TIMEOUT_FACTOR);
71 private DatastoreContext(DatastoreContext other) {
72 this.dataStoreProperties = other.dataStoreProperties;
73 this.shardTransactionIdleTimeout = other.shardTransactionIdleTimeout;
74 this.operationTimeoutInSeconds = other.operationTimeoutInSeconds;
75 this.dataStoreMXBeanType = other.dataStoreMXBeanType;
76 this.shardTransactionCommitTimeoutInSeconds = other.shardTransactionCommitTimeoutInSeconds;
77 this.shardTransactionCommitQueueCapacity = other.shardTransactionCommitQueueCapacity;
78 this.shardInitializationTimeout = other.shardInitializationTimeout;
79 this.shardLeaderElectionTimeout = other.shardLeaderElectionTimeout;
80 this.persistent = other.persistent;
81 this.configurationReader = other.configurationReader;
82 this.transactionCreationInitialRateLimit = other.transactionCreationInitialRateLimit;
83 this.dataStoreType = other.dataStoreType;
84 this.shardBatchedModificationCount = other.shardBatchedModificationCount;
86 setShardJournalRecoveryLogBatchSize(other.raftConfig.getJournalRecoveryLogBatchSize());
87 setSnapshotBatchCount(other.raftConfig.getSnapshotBatchCount());
88 setHeartbeatInterval(other.raftConfig.getHeartBeatInterval().toMillis());
89 setIsolatedLeaderCheckInterval(other.raftConfig.getIsolatedCheckIntervalInMillis());
90 setSnapshotDataThresholdPercentage(other.raftConfig.getSnapshotDataThresholdPercentage());
91 setElectionTimeoutFactor(other.raftConfig.getElectionTimeoutFactor());
94 public static Builder newBuilder() {
95 return new Builder(new DatastoreContext());
98 public static Builder newBuilderFrom(DatastoreContext context) {
99 return new Builder(new DatastoreContext(context));
102 public InMemoryDOMDataStoreConfigProperties getDataStoreProperties() {
103 return dataStoreProperties;
106 public Duration getShardTransactionIdleTimeout() {
107 return shardTransactionIdleTimeout;
110 public String getDataStoreMXBeanType() {
111 return dataStoreMXBeanType;
114 public int getOperationTimeoutInSeconds() {
115 return operationTimeoutInSeconds;
118 public ConfigParams getShardRaftConfig() {
122 public int getShardTransactionCommitTimeoutInSeconds() {
123 return shardTransactionCommitTimeoutInSeconds;
126 public int getShardTransactionCommitQueueCapacity() {
127 return shardTransactionCommitQueueCapacity;
130 public Timeout getShardInitializationTimeout() {
131 return shardInitializationTimeout;
134 public Timeout getShardLeaderElectionTimeout() {
135 return shardLeaderElectionTimeout;
138 public boolean isPersistent() {
142 public ConfigurationReader getConfigurationReader() {
143 return configurationReader;
146 public long getShardElectionTimeoutFactor(){
147 return raftConfig.getElectionTimeoutFactor();
150 public String getDataStoreType(){
151 return dataStoreType;
154 public long getTransactionCreationInitialRateLimit() {
155 return transactionCreationInitialRateLimit;
158 private void setHeartbeatInterval(long shardHeartbeatIntervalInMillis){
159 raftConfig.setHeartBeatInterval(new FiniteDuration(shardHeartbeatIntervalInMillis,
160 TimeUnit.MILLISECONDS));
163 private void setShardJournalRecoveryLogBatchSize(int shardJournalRecoveryLogBatchSize){
164 raftConfig.setJournalRecoveryLogBatchSize(shardJournalRecoveryLogBatchSize);
168 private void setIsolatedLeaderCheckInterval(long shardIsolatedLeaderCheckIntervalInMillis) {
169 raftConfig.setIsolatedLeaderCheckInterval(
170 new FiniteDuration(shardIsolatedLeaderCheckIntervalInMillis, TimeUnit.MILLISECONDS));
173 private void setElectionTimeoutFactor(long shardElectionTimeoutFactor) {
174 raftConfig.setElectionTimeoutFactor(shardElectionTimeoutFactor);
177 private void setSnapshotDataThresholdPercentage(int shardSnapshotDataThresholdPercentage) {
178 raftConfig.setSnapshotDataThresholdPercentage(shardSnapshotDataThresholdPercentage);
181 private void setSnapshotBatchCount(long shardSnapshotBatchCount) {
182 raftConfig.setSnapshotBatchCount(shardSnapshotBatchCount);
185 public int getShardBatchedModificationCount() {
186 return shardBatchedModificationCount;
189 public static class Builder {
190 private final DatastoreContext datastoreContext;
191 private int maxShardDataChangeExecutorPoolSize =
192 InMemoryDOMDataStoreConfigProperties.DEFAULT_MAX_DATA_CHANGE_EXECUTOR_POOL_SIZE;
193 private int maxShardDataChangeExecutorQueueSize =
194 InMemoryDOMDataStoreConfigProperties.DEFAULT_MAX_DATA_CHANGE_EXECUTOR_QUEUE_SIZE;
195 private int maxShardDataChangeListenerQueueSize =
196 InMemoryDOMDataStoreConfigProperties.DEFAULT_MAX_DATA_CHANGE_LISTENER_QUEUE_SIZE;
197 private int maxShardDataStoreExecutorQueueSize =
198 InMemoryDOMDataStoreConfigProperties.DEFAULT_MAX_DATA_STORE_EXECUTOR_QUEUE_SIZE;
200 private Builder(DatastoreContext datastoreContext) {
201 this.datastoreContext = datastoreContext;
203 if(datastoreContext.getDataStoreProperties() != null) {
204 maxShardDataChangeExecutorPoolSize =
205 datastoreContext.getDataStoreProperties().getMaxDataChangeExecutorPoolSize();
206 maxShardDataChangeExecutorQueueSize =
207 datastoreContext.getDataStoreProperties().getMaxDataChangeExecutorQueueSize();
208 maxShardDataChangeListenerQueueSize =
209 datastoreContext.getDataStoreProperties().getMaxDataChangeListenerQueueSize();
210 maxShardDataStoreExecutorQueueSize =
211 datastoreContext.getDataStoreProperties().getMaxDataStoreExecutorQueueSize();
215 public Builder boundedMailboxCapacity(int boundedMailboxCapacity) {
216 // TODO - this is defined in the yang DataStoreProperties but not currently used.
220 public Builder enableMetricCapture(boolean enableMetricCapture) {
221 // TODO - this is defined in the yang DataStoreProperties but not currently used.
226 public Builder shardTransactionIdleTimeout(long timeout, TimeUnit unit) {
227 datastoreContext.shardTransactionIdleTimeout = Duration.create(timeout, unit);
231 public Builder shardTransactionIdleTimeoutInMinutes(long timeout) {
232 return shardTransactionIdleTimeout(timeout, TimeUnit.MINUTES);
235 public Builder operationTimeoutInSeconds(int operationTimeoutInSeconds) {
236 datastoreContext.operationTimeoutInSeconds = operationTimeoutInSeconds;
240 public Builder dataStoreMXBeanType(String dataStoreMXBeanType) {
241 datastoreContext.dataStoreMXBeanType = dataStoreMXBeanType;
245 public Builder shardTransactionCommitTimeoutInSeconds(int shardTransactionCommitTimeoutInSeconds) {
246 datastoreContext.shardTransactionCommitTimeoutInSeconds = shardTransactionCommitTimeoutInSeconds;
250 public Builder shardJournalRecoveryLogBatchSize(int shardJournalRecoveryLogBatchSize) {
251 datastoreContext.setShardJournalRecoveryLogBatchSize(shardJournalRecoveryLogBatchSize);
255 public Builder shardSnapshotBatchCount(int shardSnapshotBatchCount) {
256 datastoreContext.setSnapshotBatchCount(shardSnapshotBatchCount);
260 public Builder shardSnapshotDataThresholdPercentage(int shardSnapshotDataThresholdPercentage) {
261 datastoreContext.setSnapshotDataThresholdPercentage(shardSnapshotDataThresholdPercentage);
265 public Builder shardHeartbeatIntervalInMillis(int shardHeartbeatIntervalInMillis) {
266 datastoreContext.setHeartbeatInterval(shardHeartbeatIntervalInMillis);
270 public Builder shardTransactionCommitQueueCapacity(int shardTransactionCommitQueueCapacity) {
271 datastoreContext.shardTransactionCommitQueueCapacity = shardTransactionCommitQueueCapacity;
275 public Builder shardInitializationTimeout(long timeout, TimeUnit unit) {
276 datastoreContext.shardInitializationTimeout = new Timeout(timeout, unit);
280 public Builder shardInitializationTimeoutInSeconds(long timeout) {
281 return shardInitializationTimeout(timeout, TimeUnit.SECONDS);
284 public Builder shardLeaderElectionTimeout(long timeout, TimeUnit unit) {
285 datastoreContext.shardLeaderElectionTimeout = new Timeout(timeout, unit);
289 public Builder shardLeaderElectionTimeoutInSeconds(long timeout) {
290 return shardLeaderElectionTimeout(timeout, TimeUnit.SECONDS);
293 public Builder configurationReader(ConfigurationReader configurationReader){
294 datastoreContext.configurationReader = configurationReader;
298 public Builder persistent(boolean persistent){
299 datastoreContext.persistent = persistent;
303 public Builder shardIsolatedLeaderCheckIntervalInMillis(int shardIsolatedLeaderCheckIntervalInMillis) {
304 datastoreContext.setIsolatedLeaderCheckInterval(shardIsolatedLeaderCheckIntervalInMillis);
308 public Builder shardElectionTimeoutFactor(long shardElectionTimeoutFactor){
309 datastoreContext.setElectionTimeoutFactor(shardElectionTimeoutFactor);
313 public Builder transactionCreationInitialRateLimit(long initialRateLimit){
314 datastoreContext.transactionCreationInitialRateLimit = initialRateLimit;
318 public Builder dataStoreType(String dataStoreType){
319 datastoreContext.dataStoreType = dataStoreType;
320 datastoreContext.dataStoreMXBeanType = "Distributed" + WordUtils.capitalize(dataStoreType) + "Datastore";
324 public Builder shardBatchedModificationCount(int shardBatchedModificationCount) {
325 datastoreContext.shardBatchedModificationCount = shardBatchedModificationCount;
329 public Builder maxShardDataChangeExecutorPoolSize(int maxShardDataChangeExecutorPoolSize) {
330 this.maxShardDataChangeExecutorPoolSize = maxShardDataChangeExecutorPoolSize;
334 public Builder maxShardDataChangeExecutorQueueSize(int maxShardDataChangeExecutorQueueSize) {
335 this.maxShardDataChangeExecutorQueueSize = maxShardDataChangeExecutorQueueSize;
339 public Builder maxShardDataChangeListenerQueueSize(int maxShardDataChangeListenerQueueSize) {
340 this.maxShardDataChangeListenerQueueSize = maxShardDataChangeListenerQueueSize;
344 public Builder maxShardDataStoreExecutorQueueSize(int maxShardDataStoreExecutorQueueSize) {
345 this.maxShardDataStoreExecutorQueueSize = maxShardDataStoreExecutorQueueSize;
349 public DatastoreContext build() {
350 datastoreContext.dataStoreProperties = InMemoryDOMDataStoreConfigProperties.create(
351 maxShardDataChangeExecutorPoolSize, maxShardDataChangeExecutorQueueSize,
352 maxShardDataChangeListenerQueueSize, maxShardDataStoreExecutorQueueSize);
353 return datastoreContext;