summaryrefslogtreecommitdiffstats
path: root/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/ApplicationDataTypeCache.java
blob: 070ebf72e3e12f0ded4ed7f1d9726e238ae5d98b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.openecomp.sdc.be.model.cache;

import fj.data.Either;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import lombok.AccessLevel;
import lombok.Getter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.openecomp.sdc.be.config.BeEcompErrorManager;
import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity;
import org.openecomp.sdc.be.config.Configuration.ApplicationL1CacheInfo;
import org.openecomp.sdc.be.config.ConfigurationManager;
import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
import org.openecomp.sdc.be.model.DataTypeDefinition;
import org.openecomp.sdc.be.model.operations.impl.DataTypeOperation;
import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
import org.openecomp.sdc.be.resources.data.DataTypeData;
import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
import org.openecomp.sdc.common.log.wrappers.Logger;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

@Component("application-datatype-cache")
public class ApplicationDataTypeCache implements ApplicationCache<DataTypeDefinition>, Runnable {

    private static final String APPLICATION_DATA_TYPES_CACHE = "ApplicationDataTypesCache";
    private static final Logger log = Logger.getLogger(ApplicationDataTypeCache.class);

    private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
    private final PropertyOperation propertyOperation;
    private final ApplicationEventPublisher applicationEventPublisher;
    @Getter(AccessLevel.PACKAGE)
    private final ScheduledExecutorService scheduledPollingService;
    @Getter(AccessLevel.PACKAGE)
    private ScheduledFuture<?> scheduledFuture = null;
    private Map<String, Map<String, DataTypeDefinition>> dataTypesByModelCacheMap = new HashMap<>();
    private final DataTypeOperation dataTypeOperation;
    private int firstRunDelayInSec = 30;
    private int pollingIntervalInSec = 60;

    public ApplicationDataTypeCache(final PropertyOperation propertyOperation, final ApplicationEventPublisher applicationEventPublisher,
                                    final DataTypeOperation dataTypeOperation) {
        this.propertyOperation = propertyOperation;
        this.applicationEventPublisher = applicationEventPublisher;
        this.dataTypeOperation = dataTypeOperation;
        scheduledPollingService = Executors
            .newScheduledThreadPool(1, new BasicThreadFactory.Builder().namingPattern("ApplicationDataTypeCacheThread-%d").build());
    }

    @PostConstruct
    void init() {
        final Optional<ApplicationL1CacheInfo> dataTypeCacheConfigOptional = getDataTypeCacheConfig();
        if (dataTypeCacheConfigOptional.isEmpty()) {
            BeEcompErrorManager.getInstance()
                .logInternalFlowError(APPLICATION_DATA_TYPES_CACHE, "Data types cache is not configured and will be disabled", ErrorSeverity.INFO);
            return;
        }
        final ApplicationL1CacheInfo dataTypesCacheInfo = dataTypeCacheConfigOptional.get();
        if (!Boolean.TRUE.equals(dataTypesCacheInfo.getEnabled())) {
            BeEcompErrorManager.getInstance().logInternalFlowError(APPLICATION_DATA_TYPES_CACHE, "Data types cache is disabled", ErrorSeverity.INFO);
            return;
        }
        loadConfigurationValues(dataTypesCacheInfo);
        if (scheduledPollingService != null) {
            log.debug("Starting ApplicationDataTypeCache polling task. Initial delay {}s and polling interval {}s",
                firstRunDelayInSec, pollingIntervalInSec);
            scheduledFuture = scheduledPollingService
                .scheduleAtFixedRate(this, firstRunDelayInSec, pollingIntervalInSec, TimeUnit.SECONDS);
        }
    }

    private void loadConfigurationValues(final ApplicationL1CacheInfo dataTypesCacheInfo) {
        final Integer firstRunDelay = dataTypesCacheInfo.getFirstRunDelay();
        if (firstRunDelay != null) {
            firstRunDelayInSec = firstRunDelay;
        }
        log.trace("ApplicationDataTypesCache initial delay configured to {} seconds.", firstRunDelayInSec);

        final Integer intervalInSec = dataTypesCacheInfo.getPollIntervalInSec();
        if (intervalInSec != null) {
            pollingIntervalInSec = intervalInSec;
        }
        log.trace("ApplicationDataTypesCache polling interval configured to {} seconds.", pollingIntervalInSec);
    }

    private Optional<ApplicationL1CacheInfo> getDataTypeCacheConfig() {
        final var applicationL1CacheConfig = ConfigurationManager.getConfigurationManager().getConfiguration().getApplicationL1Cache();
        if (applicationL1CacheConfig == null || applicationL1CacheConfig.getDatatypes() == null) {
            return Optional.empty();
        }
        return Optional.ofNullable(applicationL1CacheConfig.getDatatypes());
    }

    @PreDestroy
    void destroy() {
        if (scheduledFuture != null) {
            boolean result = scheduledFuture.cancel(true);
            log.debug("Stop polling task. result = {}", result);
            scheduledFuture = null;
        }
        shutdownExecutor();
    }

    private void shutdownExecutor() {
        if (scheduledPollingService == null) {
            return;
        }
        scheduledPollingService.shutdown(); // Disable new tasks from being

        // submitted
        try {
            // Wait a while for existing tasks to terminate
            if (!scheduledPollingService.awaitTermination(60, TimeUnit.SECONDS)) {
                scheduledPollingService.shutdownNow(); // Cancel currently

                // executing tasks

                // Wait a while for tasks to respond to being cancelled
                if (!scheduledPollingService.awaitTermination(60, TimeUnit.SECONDS)) {
                    log.debug("Pool did not terminate");
                }
            }
        } catch (InterruptedException ie) {
            // (Re-)Cancel if current thread also interrupted
            scheduledPollingService.shutdownNow();
            // Preserve interrupt status
            Thread.currentThread().interrupt();
        }
    }

    private Either<Map<String, Map<String, DataTypeDefinition>>, JanusGraphOperationStatus> getAllDataTypesFromGraph() {
        return propertyOperation.getAllDataTypes();
    }

    public Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> getAll(final String model) {
        try {
            readWriteLock.readLock().lock();
            if (MapUtils.isEmpty(dataTypesByModelCacheMap)) {
                final var dataTypesFound = getAllDataTypesFromGraph();
                if (dataTypesFound.isRight()) {
                    return Either.right(dataTypesFound.right().value());
                }
                dataTypesByModelCacheMap = dataTypesFound.left().value();
            }
            return Either.left(getDataTypeDefinitionMapByModel(model));
        } finally {
            readWriteLock.readLock().unlock();
        }
    }

    @Override
    public Either<DataTypeDefinition, JanusGraphOperationStatus> get(final String model, final String uniqueId) {
        try {
            readWriteLock.readLock().lock();
            if (MapUtils.isEmpty(dataTypesByModelCacheMap)) {
                return propertyOperation.getDataTypeByUid(uniqueId);
            }
            final Optional<DataTypeDefinition> dataTypeDefinition = getDataTypeDefinitionMapByModel(model).values().stream()
                .filter(p -> p.getUniqueId().equals(uniqueId)).findFirst();
            if (dataTypeDefinition.isEmpty()) {
                return propertyOperation.getDataTypeByUid(uniqueId);
            }
            return Either.left(new DataTypeDefinition(dataTypeDefinition.get()));
        } finally {
            readWriteLock.readLock().unlock();
        }
    }

    private Map<String, DataTypeDefinition> getDataTypeDefinitionMapByModel(final String model) {
        return dataTypesByModelCacheMap.containsKey(model) ? dataTypesByModelCacheMap.get(model) : new HashMap<>();
    }

    @Override
    public void run() {
        try {
            final long startTime = System.currentTimeMillis();
            log.trace("Starting refresh data types cache job");
            if (hasDataTypesChanged()) {
                log.info("Detected changes in the data types, updating the data type cache.");
                refreshDataTypesCache();
            }
            log.trace("Finished refresh data types cache job. Finished in {}ms", (System.currentTimeMillis() - startTime));
        } catch (final Exception e) {
            var errorMsg = "Failed to run refresh data types cache job";
            log.error(EcompLoggerErrorCode.UNKNOWN_ERROR, ApplicationDataTypeCache.class.getName(), errorMsg, e);
            BeEcompErrorManager.getInstance().logInternalUnexpectedError(APPLICATION_DATA_TYPES_CACHE, errorMsg, ErrorSeverity.INFO);
        } finally {
            try {
                propertyOperation.getJanusGraphGenericDao().commit();
            } catch (final Exception e) {
                log.error(EcompLoggerErrorCode.UNKNOWN_ERROR, ApplicationDataTypeCache.class.getName(),
                    "Failed to commit ApplicationDataTypeCache", e);
            }
        }
    }

    private boolean hasDataTypesChanged() {
        final List<DataTypeData> dataTypeListFromDatabase = findAllDataTypesLazy();
        final int dataTypesCacheCopyMap = dataTypesCacheMapSize();
        if (dataTypeListFromDatabase.size() != dataTypesCacheCopyMap) {
            log.debug("Total of cached data types '{}' differs from the actual '{}'", dataTypeListFromDatabase.size(),  dataTypesCacheCopyMap);
            return true;
        }

        if (CollectionUtils.isEmpty(dataTypeListFromDatabase)) {
            log.debug("Both data type cache and database are empty");
            return false;
        }

        return hasDataTypesChanged(dataTypeListFromDatabase, copyDataTypeCache());
    }

    private int dataTypesCacheMapSize() {
        var count = 0;
        for (var i = 0; i < copyDataTypeCache().size(); i++) {
            count += new ArrayList<>(copyDataTypeCache().values()).get(i).size();

        }
        return count;
    }

    private boolean hasDataTypesChanged(final List<DataTypeData> dataTypeListFromDatabase, final Map<String, Map<String, DataTypeDefinition>> dataTypesCacheCopyMap) {
        return dataTypeListFromDatabase.stream().map(DataTypeData::getDataTypeDataDefinition).anyMatch(actualDataTypeDefinition -> {
            final String dataTypeName = actualDataTypeDefinition.getName();
            final String model = actualDataTypeDefinition.getModel();
            final DataTypeDefinition cachedDataTypeDefinition = dataTypesCacheCopyMap.get(model).get(dataTypeName);
            if (cachedDataTypeDefinition == null) {
                log.debug("Datatype '{}' is not present in the cache. ", dataTypeName);
                return true;
            }

            final long cachedCreationTime = cachedDataTypeDefinition.getCreationTime() == null ? 0 : cachedDataTypeDefinition.getCreationTime();
            final long actualCreationTime = actualDataTypeDefinition.getCreationTime() == null ? 0 : actualDataTypeDefinition.getCreationTime();
            if (cachedCreationTime != actualCreationTime) {
                log.debug("Datatype '{}' with model '{}' was updated. Cache/database creation time '{}'/'{}'.",
                    dataTypeName, model, cachedCreationTime, actualCreationTime);
                return true;
            }
            final long cachedModificationTime =
                cachedDataTypeDefinition.getModificationTime() == null ? 0 : cachedDataTypeDefinition.getModificationTime();
            final long actualModificationTime =
                actualDataTypeDefinition.getModificationTime() == null ? 0 : actualDataTypeDefinition.getModificationTime();
            if (cachedModificationTime != actualModificationTime) {
                log.debug("Datatype '{}' was updated. Cache/database modification time '{}'/'{}'.",
                    dataTypeName, cachedModificationTime, actualModificationTime);
                return true;
            }

            return false;
        });
    }

    private Map<String, Map<String, DataTypeDefinition>> copyDataTypeCache() {
        try {
            readWriteLock.readLock().lock();
            return new HashMap<>(this.dataTypesByModelCacheMap);
        } finally {
            readWriteLock.readLock().unlock();
        }
    }

    private void refreshDataTypesCache() {
        final Map<String, Map<String, DataTypeDefinition>> dataTypesDefinitionMap = findAllDataTypesEager();
        if (dataTypesDefinitionMap.isEmpty()) {
            return;
        }
        try {
            readWriteLock.writeLock().lock();
            dataTypesByModelCacheMap = dataTypesDefinitionMap;
            onDataChangeEventEmit();
            BeEcompErrorManager.getInstance()
                .logInternalFlowError("ReplaceDataTypesCache", "Succeed to replace the data types cache", ErrorSeverity.INFO);
        } finally {
            readWriteLock.writeLock().unlock();
        }
    }

    private Map<String, Map<String, DataTypeDefinition>> findAllDataTypesEager() {
        log.trace("Fetching data types from database, eager mode");
        final long startTime = System.currentTimeMillis();
        final Either<Map<String, Map<String, DataTypeDefinition>>, JanusGraphOperationStatus> allDataTypes = propertyOperation.getAllDataTypes();
        log.trace("Finish fetching data types from database. Took {}ms", (System.currentTimeMillis() - startTime));
        if (allDataTypes.isRight()) {
            final JanusGraphOperationStatus status = allDataTypes.right().value();
            var errorMsg= String.format("Failed to fetch data types from database. Status is %s", status);
            log.error(EcompLoggerErrorCode.UNKNOWN_ERROR, ApplicationDataTypeCache.class.getName(), errorMsg);
            BeEcompErrorManager.getInstance().logInternalConnectionError(APPLICATION_DATA_TYPES_CACHE, errorMsg, ErrorSeverity.ERROR);
            return Collections.emptyMap();
        }
        return allDataTypes.left().value();
    }

    private List<DataTypeData> findAllDataTypesLazy() {
        log.trace("Fetching data types from database, lazy mode");
        final long startTime = System.currentTimeMillis();
        final List<DataTypeData> allDataTypes = dataTypeOperation.getAllDataTypeNodes();
        log.trace("Finish fetching data types from database. Took {}ms", (System.currentTimeMillis() - startTime));
        return allDataTypes;
    }

    private void onDataChangeEventEmit() {
        log.trace("Data type cache has changed, sending DataTypesCacheChangedEvent.");
        applicationEventPublisher.publishEvent(new DataTypesCacheChangedEvent(this, copyDataTypeCache()));
    }

    /**
     * Custom event to notify all interested in cached data changes
     */
    public static class DataTypesCacheChangedEvent extends ApplicationEvent {

        @Getter
        private final Map<String,  Map<String, DataTypeDefinition>> newData;

        public DataTypesCacheChangedEvent(final Object source, final Map<String,  Map<String, DataTypeDefinition>> newData) {
            super(source);
            this.newData = newData;
        }
    }

}