H2AggregationQueryDAO.java 5.7 KB
Newer Older
wu-sheng's avatar
wu-sheng 已提交
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
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 *
 */

package org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.skywalking.oap.server.core.analysis.indicator.Indicator;
import org.apache.skywalking.oap.server.core.query.entity.Order;
import org.apache.skywalking.oap.server.core.query.entity.Step;
import org.apache.skywalking.oap.server.core.query.entity.TopNEntity;
import org.apache.skywalking.oap.server.core.register.EndpointInventory;
import org.apache.skywalking.oap.server.core.register.ServiceInstanceInventory;
import org.apache.skywalking.oap.server.core.storage.DownSamplingModelNameBuilder;
import org.apache.skywalking.oap.server.core.storage.query.IAggregationQueryDAO;
import org.apache.skywalking.oap.server.library.client.jdbc.hikaricp.JDBCHikariCPClient;

/**
 * @author wusheng
 */
public class H2AggregationQueryDAO implements IAggregationQueryDAO {
    private JDBCHikariCPClient h2Client;

    public H2AggregationQueryDAO(JDBCHikariCPClient h2Client) {
        this.h2Client = h2Client;
    }

    @Override
    public List<TopNEntity> getServiceTopN(String indName, String valueCName, int topN, Step step,
        long startTB, long endTB, Order order) throws IOException {
        return topNQuery(indName, valueCName, topN, step, startTB, endTB, order, null);
    }

    @Override public List<TopNEntity> getAllServiceInstanceTopN(String indName, String valueCName, int topN,
        Step step, long startTB, long endTB, Order order) throws IOException {
        return topNQuery(indName, valueCName, topN, step, startTB, endTB, order, null);
    }

    @Override
    public List<TopNEntity> getServiceInstanceTopN(int serviceId, String indName, String valueCName,
        int topN, Step step, long startTB, long endTB, Order order) throws IOException {
        return topNQuery(indName, valueCName, topN, step, startTB, endTB, order, (sql, conditions) -> {
            sql.append(" and ").append(ServiceInstanceInventory.SERVICE_ID).append("=?");
            conditions.add(serviceId);
        });
    }

    @Override
    public List<TopNEntity> getAllEndpointTopN(String indName, String valueCName, int topN, Step step,
        long startTB, long endTB, Order order) throws IOException {
        return topNQuery(indName, valueCName, topN, step, startTB, endTB, order, null);
    }

    @Override public List<TopNEntity> getEndpointTopN(int serviceId, String indName, String valueCName,
        int topN, Step step, long startTB, long endTB, Order order) throws IOException {
        return topNQuery(indName, valueCName, topN, step, startTB, endTB, order, (sql, conditions) -> {
            sql.append(" and ").append(EndpointInventory.SERVICE_ID).append("=?");
            conditions.add(serviceId);
        });
    }

    public List<TopNEntity> topNQuery(String indName, String valueCName, int topN, Step step,
        long startTB, long endTB, Order order, AppendCondition appender) throws IOException {
        String tableName = DownSamplingModelNameBuilder.build(step, indName);
        StringBuilder sql = new StringBuilder();
        List<Object> conditions = new ArrayList<>(10);
        sql.append("select * from (select avg(").append(valueCName).append(") value,").append(Indicator.ENTITY_ID).append(" from ")
            .append(tableName).append(" where ");
        this.setTimeRangeCondition(sql, conditions, startTB, endTB);
        if (appender != null) {
            appender.append(sql, conditions);
        }
        sql.append(" group by ").append(Indicator.ENTITY_ID);
        sql.append(") order by value ").append(order.equals(Order.ASC) ? "asc" : "desc").append(" limit ").append(topN);

        Connection connection = null;
        List<TopNEntity> topNEntities = new ArrayList<>();
        try {
            connection = h2Client.getConnection();
            ResultSet resultSet = h2Client.executeQuery(connection, sql.toString(), conditions.toArray(new Object[0]));

            try {
                while (resultSet.next()) {
                    TopNEntity topNEntity = new TopNEntity();
                    topNEntity.setId(resultSet.getString(Indicator.ENTITY_ID));
105
                    topNEntity.setValue(resultSet.getLong("value"));
wu-sheng's avatar
wu-sheng 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
                    topNEntities.add(topNEntity);
                }
            } catch (SQLException e) {
                throw new IOException(e);
            }
        } finally {
            h2Client.close(connection);
        }
        return topNEntities;
    }

    private void setTimeRangeCondition(StringBuilder sql, List<Object> conditions, long startTimestamp,
        long endTimestamp) {
        sql.append(Indicator.TIME_BUCKET).append(" >= ? and ").append(Indicator.TIME_BUCKET).append(" <= ?");
        conditions.add(startTimestamp);
        conditions.add(endTimestamp);
    }

    private interface AppendCondition {
        void append(StringBuilder sql, List<Object> conditions);
    }
}