public void deleteHistory()

in oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/base/HistoryDeleteEsDAO.java [44:106]


    public void deleteHistory(Model model, String timeBucketColumnName, int ttl) {
        ElasticSearchClient client = getClient();

        if (!model.isRecord()) {
            if (!DownSampling.Minute.equals(model.getDownsampling())) {
                /*
                 * In ElasticSearch storage, the TTL triggers the index deletion directly.
                 * As all metrics data in different down sampling rule of one day are in the same index, the deletion operation
                 * is only required to run once.
                 */
                return;
            }
        }
        long deadline = Long.parseLong(new DateTime().plusDays(-ttl).toString("yyyyMMdd"));
        String tableName = IndexController.INSTANCE.getTableName(model);
        Long latestSuccessDeadline = this.indexLatestSuccess.get(model.getName());
        if (latestSuccessDeadline != null && deadline <= latestSuccessDeadline) {
            if (log.isDebugEnabled()) {
                log.debug("Index = {} already deleted, skip, deadline = {}, ttl = {}", tableName, deadline, ttl);
            }
            return;
        }

        String latestIndex = TimeSeriesUtils.latestWriteIndexName(model);
        if (!client.isExistsIndex(latestIndex)) {
            try {
                client.createIndex(latestIndex);
                if (log.isDebugEnabled()) {
                    log.debug("Latest index = {} is not exist, create.", latestIndex);
                }
            } catch (ResponseException e) {
                if (e.getStatusCode() == 400 && client.isExistsIndex(latestIndex)) {
                    if (log.isDebugEnabled()) {
                        log.debug(
                            "Failed to create index {}, index is already created.", latestIndex);
                    }
                } else {
                    throw e;
                }
            }
        }

        Collection<String> indices = client.retrievalIndexByAliases(tableName);

        if (log.isDebugEnabled()) {
            log.debug("Deadline = {}, indices = {}, ttl = {}", deadline, indices, ttl);
        }

        List<String> prepareDeleteIndexes = new ArrayList<>();
        for (String index : indices) {
            long timeSeries = TimeSeriesUtils.isolateTimeFromIndexName(index);
            if (deadline >= timeSeries) {
                prepareDeleteIndexes.add(index);
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("Indices to be deleted: {}", prepareDeleteIndexes);
        }
        for (String prepareDeleteIndex : prepareDeleteIndexes) {
            client.deleteByIndexName(prepareDeleteIndex);
        }
        this.indexLatestSuccess.put(tableName, deadline);
    }