b470018c5f4a224a02203468acfe840584764ce8
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / DatastoreContextIntrospectorTest.java
1 /*
2  * Copyright (c) 2015 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 package org.opendaylight.controller.cluster.datastore;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.opendaylight.controller.cluster.datastore.DatastoreContext.DEFAULT_HEARTBEAT_INTERVAL_IN_MILLIS;
12 import static org.opendaylight.controller.cluster.datastore.DatastoreContext.DEFAULT_OPERATION_TIMEOUT_IN_MS;
13 import static org.opendaylight.controller.cluster.datastore.DatastoreContext.DEFAULT_SHARD_INITIALIZATION_TIMEOUT;
14 import static org.opendaylight.controller.cluster.datastore.DatastoreContext.DEFAULT_SHARD_SNAPSHOT_DATA_THRESHOLD_PERCENTAGE;
15 import static org.opendaylight.controller.cluster.datastore.DatastoreContext.DEFAULT_SHARD_TRANSACTION_IDLE_TIMEOUT;
16 import static org.opendaylight.controller.cluster.datastore.DatastoreContext.DEFAULT_SHARD_TX_COMMIT_TIMEOUT_IN_SECONDS;
17
18 import java.util.Dictionary;
19 import java.util.Hashtable;
20 import org.junit.Test;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStoreConfigProperties;
23
24 /**
25  * Unit tests for DatastoreContextIntrospector.
26  *
27  * @author Thomas Pantelis
28  */
29 public class DatastoreContextIntrospectorTest {
30
31     @Test
32     public void testUpdate() {
33         DatastoreContext context = DatastoreContext.newBuilder()
34                 .logicalStoreType(LogicalDatastoreType.OPERATIONAL).build();
35         DatastoreContextIntrospector introspector = new DatastoreContextIntrospector(context );
36
37         Dictionary<String, Object> properties = new Hashtable<>();
38         properties.put("shard-transaction-idle-timeout-in-minutes", "31");
39         properties.put("operation-timeout-in-seconds", "26");
40         properties.put("shard-transaction-commit-timeout-in-seconds", "100");
41         properties.put("shard-journal-recovery-log-batch-size", "199");
42         properties.put("shard-snapshot-batch-count", "212");
43         properties.put("shard-heartbeat-interval-in-millis", "101");
44         properties.put("shard-transaction-commit-queue-capacity", "567");
45         properties.put("shard-initialization-timeout-in-seconds", "82");
46         properties.put("shard-leader-election-timeout-in-seconds", "66");
47         properties.put("shard-isolated-leader-check-interval-in-millis", "123");
48         properties.put("shard-snapshot-data-threshold-percentage", "100");
49         properties.put("shard-election-timeout-factor", "21");
50         properties.put("shard-batched-modification-count", "901");
51         properties.put("transactionCreationInitialRateLimit", "200");
52         properties.put("MaxShardDataChangeExecutorPoolSize", "41");
53         properties.put("Max-Shard-Data-Change Executor-Queue Size", "1111");
54         properties.put(" max shard data change listener queue size", "2222");
55         properties.put("mAx-shaRd-data-STORE-executor-quEUe-size", "3333");
56         properties.put("persistent", "false");
57
58         boolean updated = introspector.update(properties);
59         assertEquals("updated", true, updated);
60         context = introspector.getContext();
61
62         assertEquals(31, context.getShardTransactionIdleTimeout().toMinutes());
63         assertEquals(26000, context.getOperationTimeoutInMillis());
64         assertEquals(100, context.getShardTransactionCommitTimeoutInSeconds());
65         assertEquals(199, context.getShardRaftConfig().getJournalRecoveryLogBatchSize());
66         assertEquals(212, context.getShardRaftConfig().getSnapshotBatchCount());
67         assertEquals(101, context.getShardRaftConfig().getHeartBeatInterval().length());
68         assertEquals(567, context.getShardTransactionCommitQueueCapacity());
69         assertEquals(82, context.getShardInitializationTimeout().duration().toSeconds());
70         assertEquals(66, context.getShardLeaderElectionTimeout().duration().toSeconds());
71         assertEquals(123, context.getShardRaftConfig().getIsolatedCheckIntervalInMillis());
72         assertEquals(100, context.getShardRaftConfig().getSnapshotDataThresholdPercentage());
73         assertEquals(21, context.getShardRaftConfig().getElectionTimeoutFactor());
74         assertEquals(901, context.getShardBatchedModificationCount());
75         assertEquals(200, context.getTransactionCreationInitialRateLimit());
76         assertEquals(41, context.getDataStoreProperties().getMaxDataChangeExecutorPoolSize());
77         assertEquals(1111, context.getDataStoreProperties().getMaxDataChangeExecutorQueueSize());
78         assertEquals(2222, context.getDataStoreProperties().getMaxDataChangeListenerQueueSize());
79         assertEquals(3333, context.getDataStoreProperties().getMaxDataStoreExecutorQueueSize());
80         assertEquals(false, context.isPersistent());
81
82         properties.put("shard-transaction-idle-timeout-in-minutes", "32");
83         properties.put("operation-timeout-in-seconds", "27");
84         properties.put("shard-heartbeat-interval-in-millis", "102");
85         properties.put("shard-election-timeout-factor", "22");
86         properties.put("max-shard-data-change-executor-pool-size", "42");
87         properties.put("max-shard-data-store-executor-queue-size", "4444");
88         properties.put("persistent", "true");
89
90         updated = introspector.update(properties);
91         assertEquals("updated", true, updated);
92         context = introspector.getContext();
93
94         assertEquals(32, context.getShardTransactionIdleTimeout().toMinutes());
95         assertEquals(27000, context.getOperationTimeoutInMillis());
96         assertEquals(100, context.getShardTransactionCommitTimeoutInSeconds());
97         assertEquals(199, context.getShardRaftConfig().getJournalRecoveryLogBatchSize());
98         assertEquals(212, context.getShardRaftConfig().getSnapshotBatchCount());
99         assertEquals(102, context.getShardRaftConfig().getHeartBeatInterval().length());
100         assertEquals(567, context.getShardTransactionCommitQueueCapacity());
101         assertEquals(82, context.getShardInitializationTimeout().duration().toSeconds());
102         assertEquals(66, context.getShardLeaderElectionTimeout().duration().toSeconds());
103         assertEquals(123, context.getShardRaftConfig().getIsolatedCheckIntervalInMillis());
104         assertEquals(100, context.getShardRaftConfig().getSnapshotDataThresholdPercentage());
105         assertEquals(22, context.getShardRaftConfig().getElectionTimeoutFactor());
106         assertEquals(200, context.getTransactionCreationInitialRateLimit());
107         assertEquals(42, context.getDataStoreProperties().getMaxDataChangeExecutorPoolSize());
108         assertEquals(1111, context.getDataStoreProperties().getMaxDataChangeExecutorQueueSize());
109         assertEquals(2222, context.getDataStoreProperties().getMaxDataChangeListenerQueueSize());
110         assertEquals(4444, context.getDataStoreProperties().getMaxDataStoreExecutorQueueSize());
111         assertEquals(true, context.isPersistent());
112
113         updated = introspector.update(null);
114         assertEquals("updated", false, updated);
115
116         updated = introspector.update(new Hashtable<>());
117         assertEquals("updated", false, updated);
118     }
119
120
121     @Test
122     public void testUpdateWithInvalidValues() {
123         DatastoreContext context = DatastoreContext.newBuilder()
124                 .logicalStoreType(LogicalDatastoreType.OPERATIONAL).build();
125         DatastoreContextIntrospector introspector = new DatastoreContextIntrospector(context );
126
127         Dictionary<String, Object> properties = new Hashtable<>();
128         properties.put("shard-transaction-idle-timeout-in-minutes", "0"); // bad - must be > 0
129         properties.put("shard-journal-recovery-log-batch-size", "199");
130         properties.put("shard-transaction-commit-timeout-in-seconds", "bogus"); // bad - NaN
131         properties.put("shard-snapshot-batch-count", "212"); // good
132         properties.put("operation-timeout-in-seconds", "4"); // bad - must be >= 5
133         properties.put("shard-heartbeat-interval-in-millis", "99"); // bad - must be >= 100
134         properties.put("shard-transaction-commit-queue-capacity", "567"); // good
135         properties.put("shard-snapshot-data-threshold-percentage", "101"); // bad - must be 0-100
136         properties.put("shard-initialization-timeout-in-seconds", "-1"); // bad - must be > 0
137         properties.put("max-shard-data-change-executor-pool-size", "bogus"); // bad - NaN
138         properties.put("unknownProperty", "1"); // bad - invalid property name
139
140         boolean updated = introspector.update(properties);
141         assertEquals("updated", true, updated);
142         context = introspector.getContext();
143
144         assertEquals(DEFAULT_SHARD_TRANSACTION_IDLE_TIMEOUT, context.getShardTransactionIdleTimeout());
145         assertEquals(199, context.getShardRaftConfig().getJournalRecoveryLogBatchSize());
146         assertEquals(DEFAULT_SHARD_TX_COMMIT_TIMEOUT_IN_SECONDS, context.getShardTransactionCommitTimeoutInSeconds());
147         assertEquals(212, context.getShardRaftConfig().getSnapshotBatchCount());
148         assertEquals(DEFAULT_OPERATION_TIMEOUT_IN_MS, context.getOperationTimeoutInMillis());
149         assertEquals(DEFAULT_HEARTBEAT_INTERVAL_IN_MILLIS,
150                 context.getShardRaftConfig().getHeartBeatInterval().length());
151         assertEquals(567, context.getShardTransactionCommitQueueCapacity());
152         assertEquals(DEFAULT_SHARD_SNAPSHOT_DATA_THRESHOLD_PERCENTAGE,
153                 context.getShardRaftConfig().getSnapshotDataThresholdPercentage());
154         assertEquals(DEFAULT_SHARD_INITIALIZATION_TIMEOUT, context.getShardInitializationTimeout());
155         assertEquals(InMemoryDOMDataStoreConfigProperties.DEFAULT_MAX_DATA_CHANGE_EXECUTOR_POOL_SIZE,
156                 context.getDataStoreProperties().getMaxDataChangeExecutorPoolSize());
157     }
158
159     @Test
160     public void testUpdateWithDatastoreTypeSpecificProperties() {
161         Dictionary<String, Object> properties = new Hashtable<>();
162         properties.put("shard-transaction-idle-timeout-in-minutes", "22"); // global setting
163         properties.put("operational.shard-transaction-idle-timeout-in-minutes", "33"); // operational override
164         properties.put("config.shard-transaction-idle-timeout-in-minutes", "44"); // config override
165
166         properties.put("max-shard-data-change-executor-pool-size", "222"); // global setting
167         properties.put("operational.max-shard-data-change-executor-pool-size", "333"); // operational override
168         properties.put("config.max-shard-data-change-executor-pool-size", "444"); // config override
169
170         properties.put("persistent", "false"); // global setting
171         properties.put("operational.Persistent", "true"); // operational override
172
173         DatastoreContext operContext = DatastoreContext.newBuilder()
174                 .logicalStoreType(LogicalDatastoreType.OPERATIONAL).build();
175         DatastoreContextIntrospector operIntrospector = new DatastoreContextIntrospector(operContext);
176         boolean updated = operIntrospector.update(properties);
177         assertEquals("updated", true, updated);
178         operContext = operIntrospector.getContext();
179
180         assertEquals(33, operContext.getShardTransactionIdleTimeout().toMinutes());
181         assertEquals(true, operContext.isPersistent());
182         assertEquals(333, operContext.getDataStoreProperties().getMaxDataChangeExecutorPoolSize());
183
184         DatastoreContext configContext = DatastoreContext.newBuilder()
185                 .logicalStoreType(LogicalDatastoreType.CONFIGURATION).build();
186         DatastoreContextIntrospector configIntrospector = new DatastoreContextIntrospector(configContext);
187         updated = configIntrospector.update(properties);
188         assertEquals("updated", true, updated);
189         configContext = configIntrospector.getContext();
190
191         assertEquals(44, configContext.getShardTransactionIdleTimeout().toMinutes());
192         assertEquals(false, configContext.isPersistent());
193         assertEquals(444, configContext.getDataStoreProperties().getMaxDataChangeExecutorPoolSize());
194     }
195
196     @Test
197     public void testGetDatastoreContextForShard() {
198         Dictionary<String, Object> properties = new Hashtable<>();
199         properties.put("shard-transaction-idle-timeout-in-minutes", "22"); // global setting
200         properties.put("operational.shard-transaction-idle-timeout-in-minutes", "33"); // operational override
201         properties.put("config.shard-transaction-idle-timeout-in-minutes", "44"); // config override
202         properties.put("topology.shard-transaction-idle-timeout-in-minutes", "55"); // global shard override
203
204         DatastoreContext operContext = DatastoreContext.newBuilder()
205                 .logicalStoreType(LogicalDatastoreType.OPERATIONAL).build();
206         DatastoreContextIntrospector operIntrospector = new DatastoreContextIntrospector(operContext);
207
208         DatastoreContext shardContext = operIntrospector.newContextFactory().getShardDatastoreContext("topology");
209         assertEquals(10, shardContext.getShardTransactionIdleTimeout().toMinutes());
210
211         operIntrospector.update(properties);
212         operContext = operIntrospector.getContext();
213         assertEquals(33, operContext.getShardTransactionIdleTimeout().toMinutes());
214
215         shardContext = operIntrospector.newContextFactory().getShardDatastoreContext("topology");
216         assertEquals(55, shardContext.getShardTransactionIdleTimeout().toMinutes());
217
218         DatastoreContext configContext = DatastoreContext.newBuilder()
219                 .logicalStoreType(LogicalDatastoreType.CONFIGURATION).build();
220         DatastoreContextIntrospector configIntrospector = new DatastoreContextIntrospector(configContext);
221         configIntrospector.update(properties);
222         configContext = configIntrospector.getContext();
223         assertEquals(44, configContext.getShardTransactionIdleTimeout().toMinutes());
224
225         shardContext = configIntrospector.newContextFactory().getShardDatastoreContext("topology");
226         assertEquals(55, shardContext.getShardTransactionIdleTimeout().toMinutes());
227
228         // operational shard override
229         properties.put("operational.topology.shard-transaction-idle-timeout-in-minutes", "66");
230         // config shard override
231         properties.put("config.topology.shard-transaction-idle-timeout-in-minutes", "77");
232
233         operIntrospector.update(properties);
234         shardContext = operIntrospector.newContextFactory().getShardDatastoreContext("topology");
235         assertEquals(66, shardContext.getShardTransactionIdleTimeout().toMinutes());
236
237         configIntrospector.update(properties);
238         shardContext = configIntrospector.newContextFactory().getShardDatastoreContext("topology");
239         assertEquals(77, shardContext.getShardTransactionIdleTimeout().toMinutes());
240
241         shardContext = configIntrospector.newContextFactory().getShardDatastoreContext("default");
242         assertEquals(44, shardContext.getShardTransactionIdleTimeout().toMinutes());
243     }
244 }