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.annotations.VisibleForTesting;
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.Sets;
16 import java.util.concurrent.TimeUnit;
17 import org.apache.commons.lang3.text.WordUtils;
18 import org.opendaylight.controller.cluster.common.actor.AkkaConfigurationReader;
19 import org.opendaylight.controller.cluster.common.actor.FileAkkaConfigurationReader;
20 import org.opendaylight.controller.cluster.raft.ConfigParams;
21 import org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl;
22 import org.opendaylight.controller.cluster.raft.PeerAddressResolver;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStoreConfigProperties;
25 import scala.concurrent.duration.Duration;
26 import scala.concurrent.duration.FiniteDuration;
29 * Contains contextual data for a data store.
31 * @author Thomas Pantelis
33 public class DatastoreContext {
34 public static final String METRICS_DOMAIN = "org.opendaylight.controller.cluster.datastore";
36 public static final Duration DEFAULT_SHARD_TRANSACTION_IDLE_TIMEOUT = Duration.create(10, TimeUnit.MINUTES);
37 public static final int DEFAULT_OPERATION_TIMEOUT_IN_MS = 5000;
38 public static final int DEFAULT_SHARD_TX_COMMIT_TIMEOUT_IN_SECONDS = 30;
39 public static final int DEFAULT_JOURNAL_RECOVERY_BATCH_SIZE = 1;
40 public static final int DEFAULT_SNAPSHOT_BATCH_COUNT = 20000;
41 public static final int DEFAULT_HEARTBEAT_INTERVAL_IN_MILLIS = 500;
42 public static final int DEFAULT_ISOLATED_LEADER_CHECK_INTERVAL_IN_MILLIS = DEFAULT_HEARTBEAT_INTERVAL_IN_MILLIS * 10;
43 public static final int DEFAULT_SHARD_TX_COMMIT_QUEUE_CAPACITY = 50000;
44 public static final Timeout DEFAULT_SHARD_INITIALIZATION_TIMEOUT = new Timeout(5, TimeUnit.MINUTES);
45 public static final Timeout DEFAULT_SHARD_LEADER_ELECTION_TIMEOUT = new Timeout(30, TimeUnit.SECONDS);
46 public static final boolean DEFAULT_PERSISTENT = true;
47 public static final FileAkkaConfigurationReader DEFAULT_CONFIGURATION_READER = new FileAkkaConfigurationReader();
48 public static final int DEFAULT_SHARD_SNAPSHOT_DATA_THRESHOLD_PERCENTAGE = 12;
49 public static final int DEFAULT_SHARD_ELECTION_TIMEOUT_FACTOR = 2;
50 public static final int DEFAULT_TX_CREATION_INITIAL_RATE_LIMIT = 100;
51 public static final String UNKNOWN_DATA_STORE_TYPE = "unknown";
52 public static final int DEFAULT_SHARD_BATCHED_MODIFICATION_COUNT = 1000;
53 public static final long DEFAULT_SHARD_COMMIT_QUEUE_EXPIRY_TIMEOUT_IN_MS = TimeUnit.MILLISECONDS.convert(2, TimeUnit.MINUTES);
54 public static final int DEFAULT_SHARD_SNAPSHOT_CHUNK_SIZE = 2048000;
56 private static final Set<String> globalDatastoreNames = Sets.newConcurrentHashSet();
58 private InMemoryDOMDataStoreConfigProperties dataStoreProperties;
59 private Duration shardTransactionIdleTimeout = DatastoreContext.DEFAULT_SHARD_TRANSACTION_IDLE_TIMEOUT;
60 private long operationTimeoutInMillis = DEFAULT_OPERATION_TIMEOUT_IN_MS;
61 private String dataStoreMXBeanType;
62 private int shardTransactionCommitTimeoutInSeconds = DEFAULT_SHARD_TX_COMMIT_TIMEOUT_IN_SECONDS;
63 private int shardTransactionCommitQueueCapacity = DEFAULT_SHARD_TX_COMMIT_QUEUE_CAPACITY;
64 private Timeout shardInitializationTimeout = DEFAULT_SHARD_INITIALIZATION_TIMEOUT;
65 private Timeout shardLeaderElectionTimeout = DEFAULT_SHARD_LEADER_ELECTION_TIMEOUT;
66 private boolean persistent = DEFAULT_PERSISTENT;
67 private AkkaConfigurationReader configurationReader = DEFAULT_CONFIGURATION_READER;
68 private long transactionCreationInitialRateLimit = DEFAULT_TX_CREATION_INITIAL_RATE_LIMIT;
69 private final DefaultConfigParamsImpl raftConfig = new DefaultConfigParamsImpl();
70 private String dataStoreName = UNKNOWN_DATA_STORE_TYPE;
71 private LogicalDatastoreType logicalStoreType = LogicalDatastoreType.OPERATIONAL;
72 private int shardBatchedModificationCount = DEFAULT_SHARD_BATCHED_MODIFICATION_COUNT;
73 private boolean writeOnlyTransactionOptimizationsEnabled = true;
74 private long shardCommitQueueExpiryTimeoutInMillis = DEFAULT_SHARD_COMMIT_QUEUE_EXPIRY_TIMEOUT_IN_MS;
75 private boolean transactionDebugContextEnabled = false;
76 private String shardManagerPersistenceId;
78 public static Set<String> getGlobalDatastoreNames() {
79 return globalDatastoreNames;
82 private DatastoreContext() {
83 setShardJournalRecoveryLogBatchSize(DEFAULT_JOURNAL_RECOVERY_BATCH_SIZE);
84 setSnapshotBatchCount(DEFAULT_SNAPSHOT_BATCH_COUNT);
85 setHeartbeatInterval(DEFAULT_HEARTBEAT_INTERVAL_IN_MILLIS);
86 setIsolatedLeaderCheckInterval(DEFAULT_ISOLATED_LEADER_CHECK_INTERVAL_IN_MILLIS);
87 setSnapshotDataThresholdPercentage(DEFAULT_SHARD_SNAPSHOT_DATA_THRESHOLD_PERCENTAGE);
88 setElectionTimeoutFactor(DEFAULT_SHARD_ELECTION_TIMEOUT_FACTOR);
89 setShardSnapshotChunkSize(DEFAULT_SHARD_SNAPSHOT_CHUNK_SIZE);
92 private DatastoreContext(DatastoreContext other) {
93 this.dataStoreProperties = other.dataStoreProperties;
94 this.shardTransactionIdleTimeout = other.shardTransactionIdleTimeout;
95 this.operationTimeoutInMillis = other.operationTimeoutInMillis;
96 this.dataStoreMXBeanType = other.dataStoreMXBeanType;
97 this.shardTransactionCommitTimeoutInSeconds = other.shardTransactionCommitTimeoutInSeconds;
98 this.shardTransactionCommitQueueCapacity = other.shardTransactionCommitQueueCapacity;
99 this.shardInitializationTimeout = other.shardInitializationTimeout;
100 this.shardLeaderElectionTimeout = other.shardLeaderElectionTimeout;
101 this.persistent = other.persistent;
102 this.configurationReader = other.configurationReader;
103 this.transactionCreationInitialRateLimit = other.transactionCreationInitialRateLimit;
104 this.dataStoreName = other.dataStoreName;
105 this.logicalStoreType = other.logicalStoreType;
106 this.shardBatchedModificationCount = other.shardBatchedModificationCount;
107 this.writeOnlyTransactionOptimizationsEnabled = other.writeOnlyTransactionOptimizationsEnabled;
108 this.shardCommitQueueExpiryTimeoutInMillis = other.shardCommitQueueExpiryTimeoutInMillis;
109 this.transactionDebugContextEnabled = other.transactionDebugContextEnabled;
110 this.shardManagerPersistenceId = other.shardManagerPersistenceId;
112 setShardJournalRecoveryLogBatchSize(other.raftConfig.getJournalRecoveryLogBatchSize());
113 setSnapshotBatchCount(other.raftConfig.getSnapshotBatchCount());
114 setHeartbeatInterval(other.raftConfig.getHeartBeatInterval().toMillis());
115 setIsolatedLeaderCheckInterval(other.raftConfig.getIsolatedCheckIntervalInMillis());
116 setSnapshotDataThresholdPercentage(other.raftConfig.getSnapshotDataThresholdPercentage());
117 setElectionTimeoutFactor(other.raftConfig.getElectionTimeoutFactor());
118 setCustomRaftPolicyImplementation(other.raftConfig.getCustomRaftPolicyImplementationClass());
119 setShardSnapshotChunkSize(other.raftConfig.getSnapshotChunkSize());
120 setPeerAddressResolver(other.raftConfig.getPeerAddressResolver());
123 public static Builder newBuilder() {
124 return new Builder(new DatastoreContext());
127 public static Builder newBuilderFrom(DatastoreContext context) {
128 return new Builder(new DatastoreContext(context));
131 public InMemoryDOMDataStoreConfigProperties getDataStoreProperties() {
132 return dataStoreProperties;
135 public Duration getShardTransactionIdleTimeout() {
136 return shardTransactionIdleTimeout;
139 public String getDataStoreMXBeanType() {
140 return dataStoreMXBeanType;
143 public long getOperationTimeoutInMillis() {
144 return operationTimeoutInMillis;
147 public ConfigParams getShardRaftConfig() {
151 public int getShardTransactionCommitTimeoutInSeconds() {
152 return shardTransactionCommitTimeoutInSeconds;
155 public int getShardTransactionCommitQueueCapacity() {
156 return shardTransactionCommitQueueCapacity;
159 public Timeout getShardInitializationTimeout() {
160 return shardInitializationTimeout;
163 public Timeout getShardLeaderElectionTimeout() {
164 return shardLeaderElectionTimeout;
167 public boolean isPersistent() {
171 public AkkaConfigurationReader getConfigurationReader() {
172 return configurationReader;
175 public long getShardElectionTimeoutFactor(){
176 return raftConfig.getElectionTimeoutFactor();
179 public String getDataStoreName(){
180 return dataStoreName;
183 public LogicalDatastoreType getLogicalStoreType() {
184 return logicalStoreType;
187 public long getTransactionCreationInitialRateLimit() {
188 return transactionCreationInitialRateLimit;
191 public String getShardManagerPersistenceId() {
192 return shardManagerPersistenceId;
195 private void setPeerAddressResolver(PeerAddressResolver resolver) {
196 raftConfig.setPeerAddressResolver(resolver);
199 private void setHeartbeatInterval(long shardHeartbeatIntervalInMillis){
200 raftConfig.setHeartBeatInterval(new FiniteDuration(shardHeartbeatIntervalInMillis,
201 TimeUnit.MILLISECONDS));
204 private void setShardJournalRecoveryLogBatchSize(int shardJournalRecoveryLogBatchSize){
205 raftConfig.setJournalRecoveryLogBatchSize(shardJournalRecoveryLogBatchSize);
209 private void setIsolatedLeaderCheckInterval(long shardIsolatedLeaderCheckIntervalInMillis) {
210 raftConfig.setIsolatedLeaderCheckInterval(
211 new FiniteDuration(shardIsolatedLeaderCheckIntervalInMillis, TimeUnit.MILLISECONDS));
214 private void setElectionTimeoutFactor(long shardElectionTimeoutFactor) {
215 raftConfig.setElectionTimeoutFactor(shardElectionTimeoutFactor);
218 private void setCustomRaftPolicyImplementation(String customRaftPolicyImplementation) {
219 raftConfig.setCustomRaftPolicyImplementationClass(customRaftPolicyImplementation);
222 private void setSnapshotDataThresholdPercentage(int shardSnapshotDataThresholdPercentage) {
223 Preconditions.checkArgument(shardSnapshotDataThresholdPercentage >= 0
224 && shardSnapshotDataThresholdPercentage <= 100);
225 raftConfig.setSnapshotDataThresholdPercentage(shardSnapshotDataThresholdPercentage);
228 private void setSnapshotBatchCount(long shardSnapshotBatchCount) {
229 raftConfig.setSnapshotBatchCount(shardSnapshotBatchCount);
232 private void setShardSnapshotChunkSize(int shardSnapshotChunkSize) {
233 raftConfig.setSnapshotChunkSize(shardSnapshotChunkSize);
236 public int getShardBatchedModificationCount() {
237 return shardBatchedModificationCount;
240 public boolean isWriteOnlyTransactionOptimizationsEnabled() {
241 return writeOnlyTransactionOptimizationsEnabled;
244 public long getShardCommitQueueExpiryTimeoutInMillis() {
245 return shardCommitQueueExpiryTimeoutInMillis;
248 public boolean isTransactionDebugContextEnabled() {
249 return transactionDebugContextEnabled;
252 public int getShardSnapshotChunkSize() {
253 return raftConfig.getSnapshotChunkSize();
256 public static class Builder {
257 private final DatastoreContext datastoreContext;
258 private int maxShardDataChangeExecutorPoolSize =
259 InMemoryDOMDataStoreConfigProperties.DEFAULT_MAX_DATA_CHANGE_EXECUTOR_POOL_SIZE;
260 private int maxShardDataChangeExecutorQueueSize =
261 InMemoryDOMDataStoreConfigProperties.DEFAULT_MAX_DATA_CHANGE_EXECUTOR_QUEUE_SIZE;
262 private int maxShardDataChangeListenerQueueSize =
263 InMemoryDOMDataStoreConfigProperties.DEFAULT_MAX_DATA_CHANGE_LISTENER_QUEUE_SIZE;
264 private int maxShardDataStoreExecutorQueueSize =
265 InMemoryDOMDataStoreConfigProperties.DEFAULT_MAX_DATA_STORE_EXECUTOR_QUEUE_SIZE;
267 private Builder(DatastoreContext datastoreContext) {
268 this.datastoreContext = datastoreContext;
270 if(datastoreContext.getDataStoreProperties() != null) {
271 maxShardDataChangeExecutorPoolSize =
272 datastoreContext.getDataStoreProperties().getMaxDataChangeExecutorPoolSize();
273 maxShardDataChangeExecutorQueueSize =
274 datastoreContext.getDataStoreProperties().getMaxDataChangeExecutorQueueSize();
275 maxShardDataChangeListenerQueueSize =
276 datastoreContext.getDataStoreProperties().getMaxDataChangeListenerQueueSize();
277 maxShardDataStoreExecutorQueueSize =
278 datastoreContext.getDataStoreProperties().getMaxDataStoreExecutorQueueSize();
282 public Builder boundedMailboxCapacity(int boundedMailboxCapacity) {
283 // TODO - this is defined in the yang DataStoreProperties but not currently used.
287 public Builder enableMetricCapture(boolean enableMetricCapture) {
288 // TODO - this is defined in the yang DataStoreProperties but not currently used.
293 public Builder shardTransactionIdleTimeout(long timeout, TimeUnit unit) {
294 datastoreContext.shardTransactionIdleTimeout = Duration.create(timeout, unit);
298 public Builder shardTransactionIdleTimeoutInMinutes(long timeout) {
299 return shardTransactionIdleTimeout(timeout, TimeUnit.MINUTES);
302 public Builder operationTimeoutInSeconds(int operationTimeoutInSeconds) {
303 datastoreContext.operationTimeoutInMillis = TimeUnit.SECONDS.toMillis(operationTimeoutInSeconds);
307 public Builder operationTimeoutInMillis(long operationTimeoutInMillis) {
308 datastoreContext.operationTimeoutInMillis = operationTimeoutInMillis;
312 public Builder dataStoreMXBeanType(String dataStoreMXBeanType) {
313 datastoreContext.dataStoreMXBeanType = dataStoreMXBeanType;
317 public Builder shardTransactionCommitTimeoutInSeconds(int shardTransactionCommitTimeoutInSeconds) {
318 datastoreContext.shardTransactionCommitTimeoutInSeconds = shardTransactionCommitTimeoutInSeconds;
322 public Builder shardJournalRecoveryLogBatchSize(int shardJournalRecoveryLogBatchSize) {
323 datastoreContext.setShardJournalRecoveryLogBatchSize(shardJournalRecoveryLogBatchSize);
327 public Builder shardSnapshotBatchCount(int shardSnapshotBatchCount) {
328 datastoreContext.setSnapshotBatchCount(shardSnapshotBatchCount);
332 public Builder shardSnapshotDataThresholdPercentage(int shardSnapshotDataThresholdPercentage) {
333 datastoreContext.setSnapshotDataThresholdPercentage(shardSnapshotDataThresholdPercentage);
337 public Builder shardHeartbeatIntervalInMillis(int shardHeartbeatIntervalInMillis) {
338 datastoreContext.setHeartbeatInterval(shardHeartbeatIntervalInMillis);
342 public Builder shardTransactionCommitQueueCapacity(int shardTransactionCommitQueueCapacity) {
343 datastoreContext.shardTransactionCommitQueueCapacity = shardTransactionCommitQueueCapacity;
347 public Builder shardInitializationTimeout(long timeout, TimeUnit unit) {
348 datastoreContext.shardInitializationTimeout = new Timeout(timeout, unit);
352 public Builder shardInitializationTimeoutInSeconds(long timeout) {
353 return shardInitializationTimeout(timeout, TimeUnit.SECONDS);
356 public Builder shardLeaderElectionTimeout(long timeout, TimeUnit unit) {
357 datastoreContext.shardLeaderElectionTimeout = new Timeout(timeout, unit);
361 public Builder shardLeaderElectionTimeoutInSeconds(long timeout) {
362 return shardLeaderElectionTimeout(timeout, TimeUnit.SECONDS);
365 public Builder configurationReader(AkkaConfigurationReader configurationReader){
366 datastoreContext.configurationReader = configurationReader;
370 public Builder persistent(boolean persistent){
371 datastoreContext.persistent = persistent;
375 public Builder shardIsolatedLeaderCheckIntervalInMillis(int shardIsolatedLeaderCheckIntervalInMillis) {
376 datastoreContext.setIsolatedLeaderCheckInterval(shardIsolatedLeaderCheckIntervalInMillis);
380 public Builder shardElectionTimeoutFactor(long shardElectionTimeoutFactor){
381 datastoreContext.setElectionTimeoutFactor(shardElectionTimeoutFactor);
385 public Builder transactionCreationInitialRateLimit(long initialRateLimit){
386 datastoreContext.transactionCreationInitialRateLimit = initialRateLimit;
390 public Builder logicalStoreType(LogicalDatastoreType logicalStoreType){
391 datastoreContext.logicalStoreType = Preconditions.checkNotNull(logicalStoreType);
393 // Retain compatible naming
394 switch (logicalStoreType) {
396 dataStoreName("config");
399 dataStoreName("operational");
402 dataStoreName(logicalStoreType.name());
408 public Builder dataStoreName(String dataStoreName){
409 datastoreContext.dataStoreName = Preconditions.checkNotNull(dataStoreName);
410 datastoreContext.dataStoreMXBeanType = "Distributed" + WordUtils.capitalize(dataStoreName) + "Datastore";
414 public Builder shardBatchedModificationCount(int shardBatchedModificationCount) {
415 datastoreContext.shardBatchedModificationCount = shardBatchedModificationCount;
419 public Builder writeOnlyTransactionOptimizationsEnabled(boolean value) {
420 datastoreContext.writeOnlyTransactionOptimizationsEnabled = value;
424 public Builder shardCommitQueueExpiryTimeoutInMillis(long value) {
425 datastoreContext.shardCommitQueueExpiryTimeoutInMillis = value;
429 public Builder shardCommitQueueExpiryTimeoutInSeconds(long value) {
430 datastoreContext.shardCommitQueueExpiryTimeoutInMillis = TimeUnit.MILLISECONDS.convert(
431 value, TimeUnit.SECONDS);
435 public Builder transactionDebugContextEnabled(boolean value) {
436 datastoreContext.transactionDebugContextEnabled = value;
440 public Builder maxShardDataChangeExecutorPoolSize(int maxShardDataChangeExecutorPoolSize) {
441 this.maxShardDataChangeExecutorPoolSize = maxShardDataChangeExecutorPoolSize;
445 public Builder maxShardDataChangeExecutorQueueSize(int maxShardDataChangeExecutorQueueSize) {
446 this.maxShardDataChangeExecutorQueueSize = maxShardDataChangeExecutorQueueSize;
450 public Builder maxShardDataChangeListenerQueueSize(int maxShardDataChangeListenerQueueSize) {
451 this.maxShardDataChangeListenerQueueSize = maxShardDataChangeListenerQueueSize;
455 public Builder maxShardDataStoreExecutorQueueSize(int maxShardDataStoreExecutorQueueSize) {
456 this.maxShardDataStoreExecutorQueueSize = maxShardDataStoreExecutorQueueSize;
461 * For unit tests only.
464 public Builder shardManagerPersistenceId(String id) {
465 datastoreContext.shardManagerPersistenceId = id;
469 public DatastoreContext build() {
470 datastoreContext.dataStoreProperties = InMemoryDOMDataStoreConfigProperties.create(
471 maxShardDataChangeExecutorPoolSize, maxShardDataChangeExecutorQueueSize,
472 maxShardDataChangeListenerQueueSize, maxShardDataStoreExecutorQueueSize);
474 if(datastoreContext.dataStoreName != null) {
475 globalDatastoreNames.add(datastoreContext.dataStoreName);
478 return datastoreContext;
481 public Builder customRaftPolicyImplementation(String customRaftPolicyImplementation) {
482 datastoreContext.setCustomRaftPolicyImplementation(customRaftPolicyImplementation);
486 public Builder shardSnapshotChunkSize(int shardSnapshotChunkSize) {
487 datastoreContext.setShardSnapshotChunkSize(shardSnapshotChunkSize);
491 public Builder shardPeerAddressResolver(PeerAddressResolver resolver) {
492 datastoreContext.setPeerAddressResolver(resolver);