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

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