Split out TransactionContext classes
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DatastoreContext.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.cluster.datastore;
10
11 import akka.util.Timeout;
12 import java.util.concurrent.TimeUnit;
13 import org.opendaylight.controller.cluster.datastore.config.ConfigurationReader;
14 import org.opendaylight.controller.cluster.datastore.config.FileConfigurationReader;
15 import org.opendaylight.controller.cluster.raft.ConfigParams;
16 import org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl;
17 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStoreConfigProperties;
18 import scala.concurrent.duration.Duration;
19 import scala.concurrent.duration.FiniteDuration;
20
21 /**
22  * Contains contextual data for a data store.
23  *
24  * @author Thomas Pantelis
25  */
26 public class DatastoreContext {
27
28     private final InMemoryDOMDataStoreConfigProperties dataStoreProperties;
29     private final Duration shardTransactionIdleTimeout;
30     private final int operationTimeoutInSeconds;
31     private final String dataStoreMXBeanType;
32     private final ConfigParams shardRaftConfig;
33     private final int shardTransactionCommitTimeoutInSeconds;
34     private final int shardTransactionCommitQueueCapacity;
35     private final Timeout shardInitializationTimeout;
36     private final Timeout shardLeaderElectionTimeout;
37     private final boolean persistent;
38     private final ConfigurationReader configurationReader;
39     private final long shardElectionTimeoutFactor;
40
41     private DatastoreContext(InMemoryDOMDataStoreConfigProperties dataStoreProperties,
42             ConfigParams shardRaftConfig, String dataStoreMXBeanType, int operationTimeoutInSeconds,
43             Duration shardTransactionIdleTimeout, int shardTransactionCommitTimeoutInSeconds,
44             int shardTransactionCommitQueueCapacity, Timeout shardInitializationTimeout,
45             Timeout shardLeaderElectionTimeout,
46             boolean persistent, ConfigurationReader configurationReader, long shardElectionTimeoutFactor) {
47         this.dataStoreProperties = dataStoreProperties;
48         this.shardRaftConfig = shardRaftConfig;
49         this.dataStoreMXBeanType = dataStoreMXBeanType;
50         this.operationTimeoutInSeconds = operationTimeoutInSeconds;
51         this.shardTransactionIdleTimeout = shardTransactionIdleTimeout;
52         this.shardTransactionCommitTimeoutInSeconds = shardTransactionCommitTimeoutInSeconds;
53         this.shardTransactionCommitQueueCapacity = shardTransactionCommitQueueCapacity;
54         this.shardInitializationTimeout = shardInitializationTimeout;
55         this.shardLeaderElectionTimeout = shardLeaderElectionTimeout;
56         this.persistent = persistent;
57         this.configurationReader = configurationReader;
58         this.shardElectionTimeoutFactor = shardElectionTimeoutFactor;
59     }
60
61     public static Builder newBuilder() {
62         return new Builder();
63     }
64
65     public InMemoryDOMDataStoreConfigProperties getDataStoreProperties() {
66         return dataStoreProperties;
67     }
68
69     public Duration getShardTransactionIdleTimeout() {
70         return shardTransactionIdleTimeout;
71     }
72
73     public String getDataStoreMXBeanType() {
74         return dataStoreMXBeanType;
75     }
76
77     public int getOperationTimeoutInSeconds() {
78         return operationTimeoutInSeconds;
79     }
80
81     public ConfigParams getShardRaftConfig() {
82         return shardRaftConfig;
83     }
84
85     public int getShardTransactionCommitTimeoutInSeconds() {
86         return shardTransactionCommitTimeoutInSeconds;
87     }
88
89     public int getShardTransactionCommitQueueCapacity() {
90         return shardTransactionCommitQueueCapacity;
91     }
92
93     public Timeout getShardInitializationTimeout() {
94         return shardInitializationTimeout;
95     }
96
97     public Timeout getShardLeaderElectionTimeout() {
98         return shardLeaderElectionTimeout;
99     }
100
101     public boolean isPersistent() {
102         return persistent;
103     }
104
105     public ConfigurationReader getConfigurationReader() {
106         return configurationReader;
107     }
108
109     public long getShardElectionTimeoutFactor(){
110         return this.shardElectionTimeoutFactor;
111     }
112
113     public static class Builder {
114         private InMemoryDOMDataStoreConfigProperties dataStoreProperties;
115         private Duration shardTransactionIdleTimeout = Duration.create(10, TimeUnit.MINUTES);
116         private int operationTimeoutInSeconds = 5;
117         private String dataStoreMXBeanType;
118         private int shardTransactionCommitTimeoutInSeconds = 30;
119         private int shardJournalRecoveryLogBatchSize = 1000;
120         private int shardSnapshotBatchCount = 20000;
121         private int shardHeartbeatIntervalInMillis = 500;
122         private int shardTransactionCommitQueueCapacity = 20000;
123         private Timeout shardInitializationTimeout = new Timeout(5, TimeUnit.MINUTES);
124         private Timeout shardLeaderElectionTimeout = new Timeout(30, TimeUnit.SECONDS);
125         private boolean persistent = true;
126         private ConfigurationReader configurationReader = new FileConfigurationReader();
127         private int shardIsolatedLeaderCheckIntervalInMillis = shardHeartbeatIntervalInMillis * 10;
128         private int shardSnapshotDataThresholdPercentage = 12;
129         private long shardElectionTimeoutFactor = 2;
130
131         public Builder shardTransactionIdleTimeout(Duration shardTransactionIdleTimeout) {
132             this.shardTransactionIdleTimeout = shardTransactionIdleTimeout;
133             return this;
134         }
135
136         public Builder operationTimeoutInSeconds(int operationTimeoutInSeconds) {
137             this.operationTimeoutInSeconds = operationTimeoutInSeconds;
138             return this;
139         }
140
141         public Builder dataStoreMXBeanType(String dataStoreMXBeanType) {
142             this.dataStoreMXBeanType = dataStoreMXBeanType;
143             return this;
144         }
145
146         public Builder dataStoreProperties(InMemoryDOMDataStoreConfigProperties dataStoreProperties) {
147             this.dataStoreProperties = dataStoreProperties;
148             return this;
149         }
150
151         public Builder shardTransactionCommitTimeoutInSeconds(int shardTransactionCommitTimeoutInSeconds) {
152             this.shardTransactionCommitTimeoutInSeconds = shardTransactionCommitTimeoutInSeconds;
153             return this;
154         }
155
156         public Builder shardJournalRecoveryLogBatchSize(int shardJournalRecoveryLogBatchSize) {
157             this.shardJournalRecoveryLogBatchSize = shardJournalRecoveryLogBatchSize;
158             return this;
159         }
160
161         public Builder shardSnapshotBatchCount(int shardSnapshotBatchCount) {
162             this.shardSnapshotBatchCount = shardSnapshotBatchCount;
163             return this;
164         }
165
166         public Builder shardSnapshotDataThresholdPercentage(int shardSnapshotDataThresholdPercentage) {
167             this.shardSnapshotDataThresholdPercentage = shardSnapshotDataThresholdPercentage;
168             return this;
169         }
170
171
172         public Builder shardHeartbeatIntervalInMillis(int shardHeartbeatIntervalInMillis) {
173             this.shardHeartbeatIntervalInMillis = shardHeartbeatIntervalInMillis;
174             return this;
175         }
176
177         public Builder shardTransactionCommitQueueCapacity(int shardTransactionCommitQueueCapacity) {
178             this.shardTransactionCommitQueueCapacity = shardTransactionCommitQueueCapacity;
179             return this;
180         }
181
182         public Builder shardInitializationTimeout(long timeout, TimeUnit unit) {
183             this.shardInitializationTimeout = new Timeout(timeout, unit);
184             return this;
185         }
186
187         public Builder shardLeaderElectionTimeout(long timeout, TimeUnit unit) {
188             this.shardLeaderElectionTimeout = new Timeout(timeout, unit);
189             return this;
190         }
191
192         public Builder configurationReader(ConfigurationReader configurationReader){
193             this.configurationReader = configurationReader;
194             return this;
195         }
196
197         public Builder persistent(boolean persistent){
198             this.persistent = persistent;
199             return this;
200         }
201
202         public Builder shardIsolatedLeaderCheckIntervalInMillis(int shardIsolatedLeaderCheckIntervalInMillis) {
203             this.shardIsolatedLeaderCheckIntervalInMillis = shardIsolatedLeaderCheckIntervalInMillis;
204             return this;
205         }
206
207         public Builder shardElectionTimeoutFactor(long shardElectionTimeoutFactor){
208             this.shardElectionTimeoutFactor = shardElectionTimeoutFactor;
209             return this;
210         }
211
212
213         public DatastoreContext build() {
214             DefaultConfigParamsImpl raftConfig = new DefaultConfigParamsImpl();
215             raftConfig.setHeartBeatInterval(new FiniteDuration(shardHeartbeatIntervalInMillis,
216                     TimeUnit.MILLISECONDS));
217             raftConfig.setJournalRecoveryLogBatchSize(shardJournalRecoveryLogBatchSize);
218             raftConfig.setSnapshotBatchCount(shardSnapshotBatchCount);
219             raftConfig.setSnapshotDataThresholdPercentage(shardSnapshotDataThresholdPercentage);
220             raftConfig.setElectionTimeoutFactor(shardElectionTimeoutFactor);
221             raftConfig.setIsolatedLeaderCheckInterval(
222                 new FiniteDuration(shardIsolatedLeaderCheckIntervalInMillis, TimeUnit.MILLISECONDS));
223
224             return new DatastoreContext(dataStoreProperties, raftConfig, dataStoreMXBeanType,
225                     operationTimeoutInSeconds, shardTransactionIdleTimeout,
226                     shardTransactionCommitTimeoutInSeconds, shardTransactionCommitQueueCapacity,
227                     shardInitializationTimeout, shardLeaderElectionTimeout,
228                     persistent, configurationReader, shardElectionTimeoutFactor);
229         }
230     }
231 }