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