6d661e6aa22839d060974bd924bc6ed6ceff869d
[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.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.mock;
15 import static org.opendaylight.controller.cluster.datastore.DatastoreContext.DEFAULT_HEARTBEAT_INTERVAL_IN_MILLIS;
16 import static org.opendaylight.controller.cluster.datastore.DatastoreContext.DEFAULT_OPERATION_TIMEOUT_IN_MS;
17 import static org.opendaylight.controller.cluster.datastore.DatastoreContext.DEFAULT_SHARD_INITIALIZATION_TIMEOUT;
18 import static org.opendaylight.controller.cluster.datastore.DatastoreContext.DEFAULT_SHARD_SNAPSHOT_DATA_THRESHOLD_PERCENTAGE;
19 import static org.opendaylight.controller.cluster.datastore.DatastoreContext.DEFAULT_SHARD_TRANSACTION_IDLE_TIMEOUT;
20 import static org.opendaylight.controller.cluster.datastore.DatastoreContext.DEFAULT_SHARD_TX_COMMIT_TIMEOUT_IN_SECONDS;
21 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION;
22 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.OPERATIONAL;
23
24 import java.util.Arrays;
25 import java.util.HashMap;
26 import java.util.Map;
27 import org.junit.Test;
28 import org.opendaylight.mdsal.binding.generator.impl.GeneratedClassLoadingStrategy;
29 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
30 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
31 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
32 import org.opendaylight.mdsal.dom.store.inmemory.InMemoryDOMDataStoreConfigProperties;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.distributed.datastore.provider.rev140612.DataStorePropertiesContainer;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35
36 /**
37  * Unit tests for DatastoreContextIntrospector.
38  *
39  * @author Thomas Pantelis
40  */
41 @SuppressWarnings("checkstyle:IllegalCatch")
42 public class DatastoreContextIntrospectorTest {
43
44     static SchemaContext SCHEMA_CONTEXT;
45     static DatastoreContextIntrospectorFactory INTROSPECTOR_FACTORY;
46
47     static {
48         final ModuleInfoBackedContext moduleContext = ModuleInfoBackedContext.create();
49         try {
50             moduleContext.addModuleInfos(Arrays.asList(
51                     BindingReflections.getModuleInfo(DataStorePropertiesContainer.class)));
52         } catch (Exception e) {
53             throw new RuntimeException(e);
54         }
55         SCHEMA_CONTEXT = moduleContext.tryToCreateSchemaContext().get();
56
57         DOMSchemaService mockSchemaService = mock(DOMSchemaService.class);
58         doReturn(SCHEMA_CONTEXT).when(mockSchemaService).getGlobalContext();
59         INTROSPECTOR_FACTORY = new DatastoreContextIntrospectorFactory(mockSchemaService,
60                 GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy());
61     }
62
63     @Test
64     public void testYangDefaults() {
65         final DatastoreContextIntrospector introspector = INTROSPECTOR_FACTORY.newInstance(
66                 DatastoreContext.newBuilder().shardBatchedModificationCount(2)
67                 .transactionDebugContextEnabled(true).build());
68         DatastoreContext context = introspector.getContext();
69
70         assertEquals(1000, context.getShardBatchedModificationCount());
71         assertFalse(context.isTransactionDebugContextEnabled());
72     }
73
74     @Test
75     public void testUpdate() {
76         final DatastoreContextIntrospector introspector = INTROSPECTOR_FACTORY.newInstance(OPERATIONAL);
77
78         final Map<String, Object> properties = new HashMap<>();
79         properties.put("shard-transaction-idle-timeout-in-minutes", "31");
80         properties.put("operation-timeout-in-seconds", "26");
81         properties.put("shard-transaction-commit-timeout-in-seconds", "100");
82         properties.put("shard-journal-recovery-log-batch-size", "199");
83         properties.put("shard-snapshot-batch-count", "212");
84         properties.put("shard-heartbeat-interval-in-millis", "101");
85         properties.put("shard-transaction-commit-queue-capacity", "567");
86         properties.put("shard-initialization-timeout-in-seconds", "82");
87         properties.put("shard-leader-election-timeout-in-seconds", "66");
88         properties.put("shard-isolated-leader-check-interval-in-millis", "123");
89         properties.put("shard-snapshot-data-threshold-percentage", "100");
90         properties.put("shard-election-timeout-factor", "21");
91         properties.put("shard-batched-modification-count", "901");
92         properties.put("transactionCreationInitialRateLimit", "200");
93         properties.put("MaxShardDataChangeExecutorPoolSize", "41");
94         properties.put("Max-Shard-Data-Change Executor-Queue Size", "1111");
95         properties.put(" max shard data change listener queue size", "2222");
96         properties.put("mAx-shaRd-data-STORE-executor-quEUe-size", "3333");
97         properties.put("persistent", "false");
98         properties.put("initial-payload-serialized-buffer-capacity", "600");
99
100         boolean updated = introspector.update(properties);
101         assertTrue("updated", updated);
102         DatastoreContext context = introspector.getContext();
103
104         assertEquals(31, context.getShardTransactionIdleTimeout().toMinutes());
105         assertEquals(26000, context.getOperationTimeoutInMillis());
106         assertEquals(100, context.getShardTransactionCommitTimeoutInSeconds());
107         assertEquals(199, context.getShardRaftConfig().getJournalRecoveryLogBatchSize());
108         assertEquals(212, context.getShardRaftConfig().getSnapshotBatchCount());
109         assertEquals(101, context.getShardRaftConfig().getHeartBeatInterval().length());
110         assertEquals(567, context.getShardTransactionCommitQueueCapacity());
111         assertEquals(82, context.getShardInitializationTimeout().duration().toSeconds());
112         assertEquals(66, context.getShardLeaderElectionTimeout().duration().toSeconds());
113         assertEquals(123, context.getShardRaftConfig().getIsolatedCheckIntervalInMillis());
114         assertEquals(100, context.getShardRaftConfig().getSnapshotDataThresholdPercentage());
115         assertEquals(21, context.getShardRaftConfig().getElectionTimeoutFactor());
116         assertEquals(901, context.getShardBatchedModificationCount());
117         assertEquals(200, context.getTransactionCreationInitialRateLimit());
118         assertEquals(41, context.getDataStoreProperties().getMaxDataChangeExecutorPoolSize());
119         assertEquals(1111, context.getDataStoreProperties().getMaxDataChangeExecutorQueueSize());
120         assertEquals(2222, context.getDataStoreProperties().getMaxDataChangeListenerQueueSize());
121         assertEquals(3333, context.getDataStoreProperties().getMaxDataStoreExecutorQueueSize());
122         assertEquals(600, context.getInitialPayloadSerializedBufferCapacity());
123         assertFalse(context.isPersistent());
124
125         properties.put("shard-transaction-idle-timeout-in-minutes", "32");
126         properties.put("operation-timeout-in-seconds", "27");
127         properties.put("shard-heartbeat-interval-in-millis", "102");
128         properties.put("shard-election-timeout-factor", "22");
129         properties.put("max-shard-data-change-executor-pool-size", "42");
130         properties.put("max-shard-data-store-executor-queue-size", "4444");
131         properties.put("persistent", "true");
132
133         updated = introspector.update(properties);
134         assertTrue("updated", updated);
135         context = introspector.getContext();
136
137         assertEquals(32, context.getShardTransactionIdleTimeout().toMinutes());
138         assertEquals(27000, context.getOperationTimeoutInMillis());
139         assertEquals(100, context.getShardTransactionCommitTimeoutInSeconds());
140         assertEquals(199, context.getShardRaftConfig().getJournalRecoveryLogBatchSize());
141         assertEquals(212, context.getShardRaftConfig().getSnapshotBatchCount());
142         assertEquals(102, context.getShardRaftConfig().getHeartBeatInterval().length());
143         assertEquals(567, context.getShardTransactionCommitQueueCapacity());
144         assertEquals(82, context.getShardInitializationTimeout().duration().toSeconds());
145         assertEquals(66, context.getShardLeaderElectionTimeout().duration().toSeconds());
146         assertEquals(123, context.getShardRaftConfig().getIsolatedCheckIntervalInMillis());
147         assertEquals(100, context.getShardRaftConfig().getSnapshotDataThresholdPercentage());
148         assertEquals(22, context.getShardRaftConfig().getElectionTimeoutFactor());
149         assertEquals(200, context.getTransactionCreationInitialRateLimit());
150         assertEquals(42, context.getDataStoreProperties().getMaxDataChangeExecutorPoolSize());
151         assertEquals(1111, context.getDataStoreProperties().getMaxDataChangeExecutorQueueSize());
152         assertEquals(2222, context.getDataStoreProperties().getMaxDataChangeListenerQueueSize());
153         assertEquals(4444, context.getDataStoreProperties().getMaxDataStoreExecutorQueueSize());
154         assertTrue(context.isPersistent());
155
156         updated = introspector.update(null);
157         assertFalse("updated", updated);
158
159         updated = introspector.update(new HashMap<>());
160         assertFalse("updated", updated);
161     }
162
163
164     @Test
165     public void testUpdateWithInvalidValues() {
166         final DatastoreContextIntrospector introspector = INTROSPECTOR_FACTORY.newInstance(OPERATIONAL);
167
168         final Map<String, Object> properties = new HashMap<>();
169         properties.put("shard-transaction-idle-timeout-in-minutes", "0"); // bad - must be > 0
170         properties.put("shard-journal-recovery-log-batch-size", "199");
171         properties.put("shard-transaction-commit-timeout-in-seconds", "bogus"); // bad - NaN
172         properties.put("shard-snapshot-batch-count", "212"); // good
173         properties.put("operation-timeout-in-seconds", "4"); // bad - must be >= 5
174         properties.put("shard-heartbeat-interval-in-millis", "99"); // bad - must be >= 100
175         properties.put("shard-transaction-commit-queue-capacity", "567"); // good
176         properties.put("shard-snapshot-data-threshold-percentage", "101"); // bad - must be 0-100
177         properties.put("shard-initialization-timeout-in-seconds", "-1"); // bad - must be > 0
178         properties.put("max-shard-data-change-executor-pool-size", "bogus"); // bad - NaN
179         properties.put("unknownProperty", "1"); // bad - invalid property name
180
181         final boolean updated = introspector.update(properties);
182         assertTrue("updated", updated);
183         DatastoreContext context = introspector.getContext();
184
185         assertEquals(DEFAULT_SHARD_TRANSACTION_IDLE_TIMEOUT, context.getShardTransactionIdleTimeout());
186         assertEquals(199, context.getShardRaftConfig().getJournalRecoveryLogBatchSize());
187         assertEquals(DEFAULT_SHARD_TX_COMMIT_TIMEOUT_IN_SECONDS, context.getShardTransactionCommitTimeoutInSeconds());
188         assertEquals(212, context.getShardRaftConfig().getSnapshotBatchCount());
189         assertEquals(DEFAULT_OPERATION_TIMEOUT_IN_MS, context.getOperationTimeoutInMillis());
190         assertEquals(DEFAULT_HEARTBEAT_INTERVAL_IN_MILLIS,
191                 context.getShardRaftConfig().getHeartBeatInterval().length());
192         assertEquals(567, context.getShardTransactionCommitQueueCapacity());
193         assertEquals(DEFAULT_SHARD_SNAPSHOT_DATA_THRESHOLD_PERCENTAGE,
194                 context.getShardRaftConfig().getSnapshotDataThresholdPercentage());
195         assertEquals(DEFAULT_SHARD_INITIALIZATION_TIMEOUT, context.getShardInitializationTimeout());
196         assertEquals(InMemoryDOMDataStoreConfigProperties.DEFAULT_MAX_DATA_CHANGE_EXECUTOR_POOL_SIZE,
197                 context.getDataStoreProperties().getMaxDataChangeExecutorPoolSize());
198     }
199
200     @Test
201     public void testUpdateWithDatastoreTypeSpecificProperties() {
202         final Map<String, Object> properties = new HashMap<>();
203         properties.put("shard-transaction-idle-timeout-in-minutes", "22"); // global setting
204         properties.put("operational.shard-transaction-idle-timeout-in-minutes", "33"); // operational override
205         properties.put("config.shard-transaction-idle-timeout-in-minutes", "44"); // config override
206
207         properties.put("max-shard-data-change-executor-pool-size", "222"); // global setting
208         properties.put("operational.max-shard-data-change-executor-pool-size", "333"); // operational override
209         properties.put("config.max-shard-data-change-executor-pool-size", "444"); // config override
210
211         properties.put("persistent", "false"); // global setting
212         properties.put("operational.Persistent", "true"); // operational override
213
214         final DatastoreContextIntrospector operIntrospector = INTROSPECTOR_FACTORY.newInstance(OPERATIONAL);
215         boolean updated = operIntrospector.update(properties);
216         assertTrue("updated", updated);
217         DatastoreContext operContext = operIntrospector.getContext();
218
219         assertEquals(33, operContext.getShardTransactionIdleTimeout().toMinutes());
220         assertTrue(operContext.isPersistent());
221         assertEquals(333, operContext.getDataStoreProperties().getMaxDataChangeExecutorPoolSize());
222
223         final DatastoreContextIntrospector configIntrospector = INTROSPECTOR_FACTORY.newInstance(CONFIGURATION);
224         updated = configIntrospector.update(properties);
225         assertTrue("updated", updated);
226         DatastoreContext configContext = configIntrospector.getContext();
227
228         assertEquals(44, configContext.getShardTransactionIdleTimeout().toMinutes());
229         assertFalse(configContext.isPersistent());
230         assertEquals(444, configContext.getDataStoreProperties().getMaxDataChangeExecutorPoolSize());
231     }
232
233     @Test
234     public void testGetDatastoreContextForShard() {
235         final Map<String, Object> properties = new HashMap<>();
236         properties.put("shard-transaction-idle-timeout-in-minutes", "22"); // global setting
237         properties.put("operational.shard-transaction-idle-timeout-in-minutes", "33"); // operational override
238         properties.put("config.shard-transaction-idle-timeout-in-minutes", "44"); // config override
239         properties.put("topology.shard-transaction-idle-timeout-in-minutes", "55"); // global shard override
240
241         final DatastoreContextIntrospector operIntrospector = INTROSPECTOR_FACTORY.newInstance(OPERATIONAL);
242
243         DatastoreContext shardContext = operIntrospector.newContextFactory().getShardDatastoreContext("topology");
244         assertEquals(10, shardContext.getShardTransactionIdleTimeout().toMinutes());
245
246         operIntrospector.update(properties);
247         DatastoreContext operContext = operIntrospector.getContext();
248         assertEquals(33, operContext.getShardTransactionIdleTimeout().toMinutes());
249
250         shardContext = operIntrospector.newContextFactory().getShardDatastoreContext("topology");
251         assertEquals(55, shardContext.getShardTransactionIdleTimeout().toMinutes());
252
253         final DatastoreContextIntrospector configIntrospector = INTROSPECTOR_FACTORY.newInstance(CONFIGURATION);
254         configIntrospector.update(properties);
255         DatastoreContext configContext = configIntrospector.getContext();
256         assertEquals(44, configContext.getShardTransactionIdleTimeout().toMinutes());
257
258         shardContext = configIntrospector.newContextFactory().getShardDatastoreContext("topology");
259         assertEquals(55, shardContext.getShardTransactionIdleTimeout().toMinutes());
260
261         // operational shard override
262         properties.put("operational.topology.shard-transaction-idle-timeout-in-minutes", "66");
263         // config shard override
264         properties.put("config.topology.shard-transaction-idle-timeout-in-minutes", "77");
265
266         operIntrospector.update(properties);
267         shardContext = operIntrospector.newContextFactory().getShardDatastoreContext("topology");
268         assertEquals(66, shardContext.getShardTransactionIdleTimeout().toMinutes());
269
270         configIntrospector.update(properties);
271         shardContext = configIntrospector.newContextFactory().getShardDatastoreContext("topology");
272         assertEquals(77, shardContext.getShardTransactionIdleTimeout().toMinutes());
273
274         shardContext = configIntrospector.newContextFactory().getShardDatastoreContext("default");
275         assertEquals(44, shardContext.getShardTransactionIdleTimeout().toMinutes());
276     }
277 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.