2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Nordix Foundation
4 * Modifications Copyright (C) 2022 Bell Canada
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.ncmp.api.inventory.sync;
24 import java.util.Collection;
25 import java.util.HashSet;
26 import java.util.List;
28 import java.util.concurrent.BlockingQueue;
29 import java.util.concurrent.TimeUnit;
30 import lombok.RequiredArgsConstructor;
31 import lombok.extern.slf4j.Slf4j;
32 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
33 import org.onap.cps.spi.model.DataNode;
34 import org.springframework.scheduling.annotation.Scheduled;
35 import org.springframework.stereotype.Component;
38 @RequiredArgsConstructor
40 public class ModuleSyncWatchdog {
42 private final SyncUtils syncUtils;
43 private final BlockingQueue<DataNode> moduleSyncWorkQueue;
44 private final Map<String, Object> moduleSyncStartedOnCmHandles;
45 private final ModuleSyncTasks moduleSyncTasks;
47 private static final int MODULE_SYNC_BATCH_SIZE = 100;
48 private static final long PREVENT_CPU_BURN_WAIT_TIME_MILLIS = 10;
49 private static final String VALUE_FOR_HAZELCAST_IN_PROGRESS_MAP = "Started";
52 * Execute Cm Handle poll which changes the cm handle state from 'ADVISED' to 'READY'.
54 @Scheduled(fixedDelayString = "${timers.advised-modules-sync.sleep-time-ms:5000}")
55 public void moduleSyncAdvisedCmHandles() {
56 populateWorkQueueIfNeeded();
57 while (!moduleSyncWorkQueue.isEmpty()) {
58 final Collection<DataNode> nextBatch = prepareNextBatch();
59 moduleSyncTasks.performModuleSync(nextBatch);
65 * Find any failed (locked) cm handles and change state back to 'ADVISED'.
67 @Scheduled(fixedDelayString = "${timers.locked-modules-sync.sleep-time-ms:300000}")
68 public void resetPreviouslyFailedCmHandles() {
69 final List<YangModelCmHandle> failedCmHandles = syncUtils.getModuleSyncFailedCmHandles();
70 moduleSyncTasks.resetFailedCmHandles(failedCmHandles);
73 private void preventBusyWait() {
74 // This method isn't really needed until CPS-1200 Performance Improvement: Watchdog Parallel execution
75 // but leaving here to minimize impacts on this class for that Jira
77 TimeUnit.MILLISECONDS.sleep(PREVENT_CPU_BURN_WAIT_TIME_MILLIS);
78 } catch (final InterruptedException e) {
79 Thread.currentThread().interrupt();
83 private void populateWorkQueueIfNeeded() {
84 if (moduleSyncWorkQueue.isEmpty()) {
85 final List<DataNode> advisedCmHandles = syncUtils.getAdvisedCmHandles();
86 for (final DataNode advisedCmHandle : advisedCmHandles) {
87 if (!moduleSyncWorkQueue.offer(advisedCmHandle)) {
88 log.warn("Unable to add cm handle {} to the work queue", advisedCmHandle.getLeaves().get("id"));
94 private Collection<DataNode> prepareNextBatch() {
95 final Collection<DataNode> nextBatchCandidates = new HashSet<>(MODULE_SYNC_BATCH_SIZE);
96 final Collection<DataNode> nextBatch = new HashSet<>(MODULE_SYNC_BATCH_SIZE);
97 moduleSyncWorkQueue.drainTo(nextBatchCandidates, MODULE_SYNC_BATCH_SIZE);
98 log.debug("nextBatchCandidates size : {}", nextBatchCandidates.size());
99 for (final DataNode batchCandidate : nextBatchCandidates) {
100 final String cmHandleId = String.valueOf(batchCandidate.getLeaves().get("id"));
101 final boolean alreadyAddedToInProgressMap = VALUE_FOR_HAZELCAST_IN_PROGRESS_MAP
102 .equals(moduleSyncStartedOnCmHandles.putIfAbsent(cmHandleId, VALUE_FOR_HAZELCAST_IN_PROGRESS_MAP));
103 if (alreadyAddedToInProgressMap) {
104 log.debug("module sync for {} already in progress by other instance", cmHandleId);
106 nextBatch.add(batchCandidate);
109 log.debug("nextBatch size : {}", nextBatch.size());