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

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