Allow shard settle timeout to be tuned
[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("initial-settle-timeout-multiplier", "5");
89         properties.put("shard-isolated-leader-check-interval-in-millis", "123");
90         properties.put("shard-snapshot-data-threshold-percentage", "100");
91         properties.put("shard-election-timeout-factor", "21");
92         properties.put("shard-batched-modification-count", "901");
93         properties.put("transactionCreationInitialRateLimit", "200");
94         properties.put("MaxShardDataChangeExecutorPoolSize", "41");
95         properties.put("Max-Shard-Data-Change Executor-Queue Size", "1111");
96         properties.put(" max shard data change listener queue size", "2222");
97         properties.put("mAx-shaRd-data-STORE-executor-quEUe-size", "3333");
98         properties.put("persistent", "false");
99         properties.put("initial-payload-serialized-buffer-capacity", "600");
100
101         boolean updated = introspector.update(properties);
102         assertTrue("updated", updated);
103         DatastoreContext context = introspector.getContext();
104
105         assertEquals(31, context.getShardTransactionIdleTimeout().toMinutes());
106         assertEquals(26000, context.getOperationTimeoutInMillis());
107         assertEquals(100, context.getShardTransactionCommitTimeoutInSeconds());
108         assertEquals(199, context.getShardRaftConfig().getJournalRecoveryLogBatchSize());
109         assertEquals(212, context.getShardRaftConfig().getSnapshotBatchCount());
110         assertEquals(101, context.getShardRaftConfig().getHeartBeatInterval().length());
111         assertEquals(567, context.getShardTransactionCommitQueueCapacity());
112         assertEquals(82, context.getShardInitializationTimeout().duration().toSeconds());
113         assertEquals(66, context.getShardLeaderElectionTimeout().duration().toSeconds());
114         assertEquals(5, context.getInitialSettleTimeoutMultiplier());
115         assertEquals(123, context.getShardRaftConfig().getIsolatedCheckIntervalInMillis());
116         assertEquals(100, context.getShardRaftConfig().getSnapshotDataThresholdPercentage());
117         assertEquals(21, context.getShardRaftConfig().getElectionTimeoutFactor());
118         assertEquals(901, context.getShardBatchedModificationCount());
119         assertEquals(200, context.getTransactionCreationInitialRateLimit());
120         assertEquals(41, context.getDataStoreProperties().getMaxDataChangeExecutorPoolSize());
121         assertEquals(1111, context.getDataStoreProperties().getMaxDataChangeExecutorQueueSize());
122         assertEquals(2222, context.getDataStoreProperties().getMaxDataChangeListenerQueueSize());
123         assertEquals(3333, context.getDataStoreProperties().getMaxDataStoreExecutorQueueSize());
124         assertEquals(600, context.getInitialPayloadSerializedBufferCapacity());
125         assertFalse(context.isPersistent());
126
127         properties.put("shard-transaction-idle-timeout-in-minutes", "32");
128         properties.put("operation-timeout-in-seconds", "27");
129         properties.put("shard-heartbeat-interval-in-millis", "102");
130         properties.put("shard-election-timeout-factor", "22");
131         properties.put("initial-settle-timeout-multiplier", "6");
132         properties.put("max-shard-data-change-executor-pool-size", "42");
133         properties.put("max-shard-data-store-executor-queue-size", "4444");
134         properties.put("persistent", "true");
135
136         updated = introspector.update(properties);
137         assertTrue("updated", updated);
138         context = introspector.getContext();
139
140         assertEquals(32, context.getShardTransactionIdleTimeout().toMinutes());
141         assertEquals(27000, context.getOperationTimeoutInMillis());
142         assertEquals(100, context.getShardTransactionCommitTimeoutInSeconds());
143         assertEquals(199, context.getShardRaftConfig().getJournalRecoveryLogBatchSize());
144         assertEquals(212, context.getShardRaftConfig().getSnapshotBatchCount());
145         assertEquals(102, context.getShardRaftConfig().getHeartBeatInterval().length());
146         assertEquals(567, context.getShardTransactionCommitQueueCapacity());
147         assertEquals(82, context.getShardInitializationTimeout().duration().toSeconds());
148         assertEquals(66, context.getShardLeaderElectionTimeout().duration().toSeconds());
149         assertEquals(6, context.getInitialSettleTimeoutMultiplier());
150         assertEquals(123, context.getShardRaftConfig().getIsolatedCheckIntervalInMillis());
151         assertEquals(100, context.getShardRaftConfig().getSnapshotDataThresholdPercentage());
152         assertEquals(22, context.getShardRaftConfig().getElectionTimeoutFactor());
153         assertEquals(200, context.getTransactionCreationInitialRateLimit());
154         assertEquals(42, context.getDataStoreProperties().getMaxDataChangeExecutorPoolSize());
155         assertEquals(1111, context.getDataStoreProperties().getMaxDataChangeExecutorQueueSize());
156         assertEquals(2222, context.getDataStoreProperties().getMaxDataChangeListenerQueueSize());
157         assertEquals(4444, context.getDataStoreProperties().getMaxDataStoreExecutorQueueSize());
158         assertTrue(context.isPersistent());
159
160         updated = introspector.update(null);
161         assertFalse("updated", updated);
162
163         updated = introspector.update(new HashMap<>());
164         assertFalse("updated", updated);
165     }
166
167
168     @Test
169     public void testUpdateWithInvalidValues() {
170         final DatastoreContextIntrospector introspector = INTROSPECTOR_FACTORY.newInstance(OPERATIONAL);
171
172         final Map<String, Object> properties = new HashMap<>();
173         properties.put("shard-transaction-idle-timeout-in-minutes", "0"); // bad - must be > 0
174         properties.put("shard-journal-recovery-log-batch-size", "199");
175         properties.put("shard-transaction-commit-timeout-in-seconds", "bogus"); // bad - NaN
176         properties.put("shard-snapshot-batch-count", "212"); // good
177         properties.put("operation-timeout-in-seconds", "4"); // bad - must be >= 5
178         properties.put("shard-heartbeat-interval-in-millis", "99"); // bad - must be >= 100
179         properties.put("shard-transaction-commit-queue-capacity", "567"); // good
180         properties.put("shard-snapshot-data-threshold-percentage", "101"); // bad - must be 0-100
181         properties.put("shard-initialization-timeout-in-seconds", "-1"); // bad - must be > 0
182         properties.put("max-shard-data-change-executor-pool-size", "bogus"); // bad - NaN
183         properties.put("unknownProperty", "1"); // bad - invalid property name
184
185         final boolean updated = introspector.update(properties);
186         assertTrue("updated", updated);
187         DatastoreContext context = introspector.getContext();
188
189         assertEquals(DEFAULT_SHARD_TRANSACTION_IDLE_TIMEOUT, context.getShardTransactionIdleTimeout());
190         assertEquals(199, context.getShardRaftConfig().getJournalRecoveryLogBatchSize());
191         assertEquals(DEFAULT_SHARD_TX_COMMIT_TIMEOUT_IN_SECONDS, context.getShardTransactionCommitTimeoutInSeconds());
192         assertEquals(212, context.getShardRaftConfig().getSnapshotBatchCount());
193         assertEquals(DEFAULT_OPERATION_TIMEOUT_IN_MS, context.getOperationTimeoutInMillis());
194         assertEquals(DEFAULT_HEARTBEAT_INTERVAL_IN_MILLIS,
195                 context.getShardRaftConfig().getHeartBeatInterval().length());
196         assertEquals(567, context.getShardTransactionCommitQueueCapacity());
197         assertEquals(DEFAULT_SHARD_SNAPSHOT_DATA_THRESHOLD_PERCENTAGE,
198                 context.getShardRaftConfig().getSnapshotDataThresholdPercentage());
199         assertEquals(DEFAULT_SHARD_INITIALIZATION_TIMEOUT, context.getShardInitializationTimeout());
200         assertEquals(InMemoryDOMDataStoreConfigProperties.DEFAULT_MAX_DATA_CHANGE_EXECUTOR_POOL_SIZE,
201                 context.getDataStoreProperties().getMaxDataChangeExecutorPoolSize());
202     }
203
204     @Test
205     public void testUpdateWithDatastoreTypeSpecificProperties() {
206         final Map<String, Object> properties = new HashMap<>();
207         properties.put("shard-transaction-idle-timeout-in-minutes", "22"); // global setting
208         properties.put("operational.shard-transaction-idle-timeout-in-minutes", "33"); // operational override
209         properties.put("config.shard-transaction-idle-timeout-in-minutes", "44"); // config override
210
211         properties.put("max-shard-data-change-executor-pool-size", "222"); // global setting
212         properties.put("operational.max-shard-data-change-executor-pool-size", "333"); // operational override
213         properties.put("config.max-shard-data-change-executor-pool-size", "444"); // config override
214
215         properties.put("persistent", "false"); // global setting
216         properties.put("operational.Persistent", "true"); // operational override
217
218         final DatastoreContextIntrospector operIntrospector = INTROSPECTOR_FACTORY.newInstance(OPERATIONAL);
219         boolean updated = operIntrospector.update(properties);
220         assertTrue("updated", updated);
221         DatastoreContext operContext = operIntrospector.getContext();
222
223         assertEquals(33, operContext.getShardTransactionIdleTimeout().toMinutes());
224         assertTrue(operContext.isPersistent());
225         assertEquals(333, operContext.getDataStoreProperties().getMaxDataChangeExecutorPoolSize());
226
227         final DatastoreContextIntrospector configIntrospector = INTROSPECTOR_FACTORY.newInstance(CONFIGURATION);
228         updated = configIntrospector.update(properties);
229         assertTrue("updated", updated);
230         DatastoreContext configContext = configIntrospector.getContext();
231
232         assertEquals(44, configContext.getShardTransactionIdleTimeout().toMinutes());
233         assertFalse(configContext.isPersistent());
234         assertEquals(444, configContext.getDataStoreProperties().getMaxDataChangeExecutorPoolSize());
235     }
236
237     @Test
238     public void testGetDatastoreContextForShard() {
239         final Map<String, Object> properties = new HashMap<>();
240         properties.put("shard-transaction-idle-timeout-in-minutes", "22"); // global setting
241         properties.put("operational.shard-transaction-idle-timeout-in-minutes", "33"); // operational override
242         properties.put("config.shard-transaction-idle-timeout-in-minutes", "44"); // config override
243         properties.put("topology.shard-transaction-idle-timeout-in-minutes", "55"); // global shard override
244
245         final DatastoreContextIntrospector operIntrospector = INTROSPECTOR_FACTORY.newInstance(OPERATIONAL);
246
247         DatastoreContext shardContext = operIntrospector.newContextFactory().getShardDatastoreContext("topology");
248         assertEquals(10, shardContext.getShardTransactionIdleTimeout().toMinutes());
249
250         operIntrospector.update(properties);
251         DatastoreContext operContext = operIntrospector.getContext();
252         assertEquals(33, operContext.getShardTransactionIdleTimeout().toMinutes());
253
254         shardContext = operIntrospector.newContextFactory().getShardDatastoreContext("topology");
255         assertEquals(55, shardContext.getShardTransactionIdleTimeout().toMinutes());
256
257         final DatastoreContextIntrospector configIntrospector = INTROSPECTOR_FACTORY.newInstance(CONFIGURATION);
258         configIntrospector.update(properties);
259         DatastoreContext configContext = configIntrospector.getContext();
260         assertEquals(44, configContext.getShardTransactionIdleTimeout().toMinutes());
261
262         shardContext = configIntrospector.newContextFactory().getShardDatastoreContext("topology");
263         assertEquals(55, shardContext.getShardTransactionIdleTimeout().toMinutes());
264
265         // operational shard override
266         properties.put("operational.topology.shard-transaction-idle-timeout-in-minutes", "66");
267         // config shard override
268         properties.put("config.topology.shard-transaction-idle-timeout-in-minutes", "77");
269
270         operIntrospector.update(properties);
271         shardContext = operIntrospector.newContextFactory().getShardDatastoreContext("topology");
272         assertEquals(66, shardContext.getShardTransactionIdleTimeout().toMinutes());
273
274         configIntrospector.update(properties);
275         shardContext = configIntrospector.newContextFactory().getShardDatastoreContext("topology");
276         assertEquals(77, shardContext.getShardTransactionIdleTimeout().toMinutes());
277
278         shardContext = configIntrospector.newContextFactory().getShardDatastoreContext("default");
279         assertEquals(44, shardContext.getShardTransactionIdleTimeout().toMinutes());
280     }
281 }

©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.