public void trimOldMetricsDBFiles()

in src/main/java/org/opensearch/performanceanalyzer/reader/ReaderMetricsProcessor.java [296:346]


    public void trimOldMetricsDBFiles() throws Exception {
        boolean deleteDBFiles = PluginSettings.instance().shouldCleanupMetricsDBFiles();
        // Cleanup all but the 2 most recent metricsDB files from metricsDBMap. The most recent
        // metricsDB files needs to be
        // retained for future metrics query handling, the second most recent metricsDB file needs
        // to be retained in case
        // any metrics query handler just got access to it right before the most recent metricsDB
        // file was available.
        while (metricsDBMap.size() > MAX_DATABASES) {
            Map.Entry<Long, MetricsDB> oldestEntry = metricsDBMap.pollFirstEntry();
            if (oldestEntry != null) {
                Long key = oldestEntry.getKey();
                MetricsDB value = oldestEntry.getValue();
                value.remove();
                if (deleteDBFiles && !batchMetricsDBSet.contains(key)) {
                    value.deleteOnDiskFile();
                }
            }
        }
        // Flush any tracking batch metrics if batch metrics is disabled. Note, in order to ensure
        // that batch metrics
        // consumers have had at least one cycle to use any metrics they may be holding, this flush
        // is done before
        // re-reading the config file to update the state of the batch metrics feature.
        if (!batchMetricsEnabled && !batchMetricsDBSet.isEmpty()) {
            if (deleteDBFiles) {
                for (Long timestamp : batchMetricsDBSet) {
                    if (!metricsDBMap.containsKey(timestamp)) {
                        MetricsDB.deleteOnDiskFile(timestamp);
                    }
                }
            }
            batchMetricsDBSet.clear();
        }
        readBatchMetricsEnabledFromConf();
        // The (retentionPeriod * 12 + 2)'th database can be safely removed, since getBatchMetrics
        // never returns more than
        // the (retentionPeriod * 12) freshest metrics files. The (retentionPeriod * 12 + 1)'th file
        // is also retained in
        // case getBatchMetrics was called at the start of this cycle, right before the newest
        // metrics file was added to
        // the batchMetricsDBSet.
        long maxNumBatchMetricsDBFiles =
                PluginSettings.instance().getBatchMetricsRetentionPeriodMinutes() * 12 + 1;
        while (batchMetricsDBSet.size() > maxNumBatchMetricsDBFiles) {
            Long timestamp = batchMetricsDBSet.pollFirst();
            if (deleteDBFiles && !metricsDBMap.containsKey(timestamp)) {
                MetricsDB.deleteOnDiskFile(timestamp);
            }
        }
    }