a6e56ab22d3fbeb5397c8a89b0de445c8a408548
[cps.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2024 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the 'License');
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an 'AS IS' BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.integration.functional.ncmp
22
23 import io.micrometer.core.instrument.MeterRegistry
24 import org.onap.cps.integration.base.CpsIntegrationSpecBase
25 import org.onap.cps.ncmp.impl.inventory.sync.ModuleSyncWatchdog
26 import org.springframework.beans.factory.annotation.Autowired
27 import org.springframework.util.StopWatch
28 import spock.util.concurrent.PollingConditions
29
30 import java.util.concurrent.Executors
31 import java.util.concurrent.TimeUnit
32
33 class ModuleSyncWatchdogIntegrationSpec extends CpsIntegrationSpecBase {
34
35     ModuleSyncWatchdog objectUnderTest
36
37     @Autowired
38     MeterRegistry meterRegistry
39
40     def executorService = Executors.newFixedThreadPool(2)
41     def PARALLEL_SYNC_SAMPLE_SIZE = 100
42
43     def setup() {
44         objectUnderTest = moduleSyncWatchdog
45     }
46
47     def cleanup() {
48         try {
49             deregisterSequenceOfCmHandles(DMI1_URL, PARALLEL_SYNC_SAMPLE_SIZE, 1)
50             moduleSyncWorkQueue.clear()
51         } finally {
52             executorService.shutdownNow()
53         }
54     }
55
56     def 'Watchdog is disabled for test.'() {
57         given:
58             registerSequenceOfCmHandlesWithManyModuleReferencesButDoNotWaitForReady(DMI1_URL, NO_MODULE_SET_TAG, PARALLEL_SYNC_SAMPLE_SIZE, 1)
59         when: 'wait a while but less then the initial delay of 10 minutes'
60             Thread.sleep(3000)
61         then: 'the work queue remains empty'
62             assert moduleSyncWorkQueue.isEmpty()
63     }
64
65     def 'CPS-2478 Highlight (and improve) module sync inefficiencies.'() {
66         given: 'register 250 cm handles with module set tag cps-2478-A'
67             def numberOfTags = 2
68             def cmHandlesPerTag = 250
69             def totalCmHandles = numberOfTags * cmHandlesPerTag
70             def offset = 1
71             def minimumBatches = totalCmHandles / 100
72             registerSequenceOfCmHandlesWithManyModuleReferencesButDoNotWaitForReady(DMI1_URL, 'cps-2478-A', cmHandlesPerTag, offset)
73         and: 'register anther 250 cm handles with module set tag cps-2478-B'
74             offset += cmHandlesPerTag
75             registerSequenceOfCmHandlesWithManyModuleReferencesButDoNotWaitForReady(DMI1_URL, 'cps-2478-B', cmHandlesPerTag, offset)
76         and: 'clear any previous instrumentation'
77             meterRegistry.clear()
78         when: 'sync all advised cm handles'
79             objectUnderTest.moduleSyncAdvisedCmHandles()
80             Thread.sleep(100)
81         then: 'retry until all schema sets are stored in db (1 schema set  for each cm handle)'
82             def dbSchemaSetStorageTimer = meterRegistry.get('cps.module.persistence.schemaset.store').timer()
83             new PollingConditions().within(10, () -> {
84                 objectUnderTest.moduleSyncAdvisedCmHandles()
85                 Thread.sleep(100)
86                 assert dbSchemaSetStorageTimer.count() >= 500
87             })
88         then: 'wait till at least 5 batches of state updates are done (often more because of retries of locked cm handles)'
89             def dbStateUpdateTimer = meterRegistry.get('cps.ncmp.cmhandle.state.update.batch').timer()
90             new PollingConditions().within(10, () -> {
91                 assert dbStateUpdateTimer.count() >= minimumBatches
92             })
93         and: 'the db has been queried for tags exactly 2 times.'
94             def dbModuleQueriesTimer = meterRegistry.get('cps.module.service.module.reference.query.by.attribute').timer()
95             assert dbModuleQueriesTimer.count() == 2
96         and: 'exactly 2 calls to DMI to get module references'
97             def dmiModuleRetrievalTimer = meterRegistry.get('cps.ncmp.inventory.module.references.from.dmi').timer()
98             assert dmiModuleRetrievalTimer.count() == 2
99         and: 'log the relevant instrumentation'
100             logInstrumentation(dbModuleQueriesTimer,    'query module references')
101             logInstrumentation(dmiModuleRetrievalTimer, 'get modules from DMI   ')
102             logInstrumentation(dbSchemaSetStorageTimer, 'store schema sets      ')
103             logInstrumentation(dbStateUpdateTimer,      'batch state updates    ')
104         cleanup: 'remove all cm handles'
105             // To properly measure performance the sample-size should be increased to 20,000 cm handles or higher (10,000 per tag)
106             def stopWatch = new StopWatch()
107             stopWatch.start()
108             deregisterSequenceOfCmHandles(DMI1_URL, totalCmHandles, 1)
109             stopWatch.stop()
110             println "*** CPS-2478, Deletion of $totalCmHandles cm handles took ${stopWatch.getTotalTimeMillis()} milliseconds"
111     }
112
113     def 'Populate module sync work queue simultaneously on two parallel threads (CPS-2403).'() {
114         // This test failed before bug https://lf-onap.atlassian.net/browse/CPS-2403 was fixed
115         given: 'the queue is empty at the start'
116             registerSequenceOfCmHandlesWithManyModuleReferencesButDoNotWaitForReady(DMI1_URL, NO_MODULE_SET_TAG, PARALLEL_SYNC_SAMPLE_SIZE, 1)
117             assert moduleSyncWorkQueue.isEmpty()
118         when: 'attempt to populate the queue on the main (test) and another parallel thread at the same time'
119             objectUnderTest.populateWorkQueueIfNeeded()
120             executorService.execute(populateQueueWithoutDelay)
121         and: 'wait a little (to give all threads time to complete their task)'
122             Thread.sleep(50)
123         then: 'the queue size is exactly the sample size'
124             assert moduleSyncWorkQueue.size() == PARALLEL_SYNC_SAMPLE_SIZE
125     }
126
127     def 'Populate module sync work queue on two parallel threads with a slight difference in start time.'() {
128         // This test proved that the issue in CPS-2403 did not arise if the the queue was populated and given time to be distributed
129         given: 'the queue is empty at the start'
130             registerSequenceOfCmHandlesWithManyModuleReferencesButDoNotWaitForReady(DMI1_URL, NO_MODULE_SET_TAG, PARALLEL_SYNC_SAMPLE_SIZE, 1)
131             assert moduleSyncWorkQueue.isEmpty()
132         when: 'attempt to populate the queue on the main (test) and another parallel thread a little later'
133             objectUnderTest.populateWorkQueueIfNeeded()
134             executorService.execute(populateQueueWithDelay)
135         and: 'wait a little (to give all threads time to complete their task)'
136             Thread.sleep(50)
137         then: 'the queue size is exactly the sample size'
138             assert moduleSyncWorkQueue.size() == PARALLEL_SYNC_SAMPLE_SIZE
139     }
140
141     def logInstrumentation(timer, description) {
142         println "*** CPS-2478, $description : Invoked ${timer.count()} times, Total Time: ${timer.totalTime(TimeUnit.MILLISECONDS)} ms, Mean Time: ${timer.mean(TimeUnit.MILLISECONDS)} ms"
143         return true
144     }
145
146     def populateQueueWithoutDelay = () -> {
147         try {
148             objectUnderTest.populateWorkQueueIfNeeded()
149         } catch (InterruptedException e) {
150             e.printStackTrace()
151         }
152     }
153
154     def populateQueueWithoutDelayCallable = () -> {
155         try {
156             objectUnderTest.populateWorkQueueIfNeeded()
157             return 'task acquired the lock first'
158         } catch (InterruptedException e) {
159             e.printStackTrace()
160         }
161     }
162
163     def populateQueueWithDelay = () -> {
164         try {
165             Thread.sleep(10)
166             objectUnderTest.populateWorkQueueIfNeeded()
167         } catch (InterruptedException e) {
168             e.printStackTrace()
169         }
170     }
171
172 }