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