All files / backend/cluster datasets-warmup.mjs

82.14% Statements 23/28
91.66% Branches 11/12
44.44% Functions 4/9
100% Lines 21/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                            2x 2x 2x     8x 8x 8x 8x 5x     8x 8x       8x 8x           8x 7x 237x     7x 1x   1x   8x     8x   8x    
/*
  Copyright (C) 2023 Sber
 
  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.
*/
 
import datasets from '../helpers/datasets.mjs';
import recalculateDatasets from '@back/helpers/recalculate-datasets.mjs';
import {isRolesMode} from '@back/utils/roles.mjs';
import {getLoggerWithTag} from '@global/logger/v2/logger.mjs';
import {getCachePrefixWithDomain} from '@back/helpers/cachePrefixByDomain.mjs';
 
const LOG_TAG = 'datasets-warmup';
const UPDATER_PREFIX = 'tmp-dataset-updater-prefix';
const logger = getLoggerWithTag(LOG_TAG);
 
export default async function warmup(storage, cache, isCluster, oldManifestMeta, processedDatasets) {
    logger.info(() => 'Datasets cache warm up started');
    let oldManifestHash = oldManifestMeta?.manifestHash;
    let cachePrefixTmp = UPDATER_PREFIX;
    if (storage.permission) {
        cachePrefixTmp = `${UPDATER_PREFIX}.${storage.permission}`;
    }
 
    const datasetUpdater = datasets(storage, null, cachePrefixTmp);
    const newManifestCachePrefix = getCachePrefixWithDomain(storage);
    // Посчитанное держим в памяти до конца прогрева: датасеты ссылаются друг на друга, и без этого
    // каждый читал бы свои источники из БД. Префикс манифеста нужен, чтобы не писать в БД то, что под
    // него не перенести: его ключи длиннее ключей временного префикса
    const problems = await cache.withWarmupMemory(cachePrefixTmp, async() => {
        const problems = await recalculateDatasets(
            isRolesMode() ? storage.manifests.origin : storage.manifest,
            datasetUpdater,
            processedDatasets
        );
 
        if (!isCluster || oldManifestHash !== storage.hash) {
            oldManifestHash && logger.debug(() => 'Зафиксировано изменение в манифесте или датасетах.');
            const cacheKeys = Object.keys(processedDatasets).map((key) => `{"path":"/datasets/${key}"}`);
            // Переноса дожидаемся: прогрев меню и валидаторы идут следом и читают датасеты уже из под нового
            // префикса. Сбой переноса загрузку не роняет - непрогретый датасет досчитается при первом обращении
            await cache.moveKeysInDataCache(cachePrefixTmp, newManifestCachePrefix, cacheKeys)
                .catch((error) => logger.error(() => `Moving ${cacheKeys.length} warmed up dataset(s) to manifest cache prefix failed`, error));
        } else {
            logger.debug(() => 'Не зафиксировано изменений в манифесте или датасетах.');
        }
        return problems;
    }, newManifestCachePrefix);
 
    if (problems.items.length > 0) storage.problems.push(problems);
 
    logger.info(() => 'Datasets cache warm up finished');
}