HornMetric.java

// 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.doris.metric;

import org.apache.doris.metric.Metric.MetricUnit;

/**
 * Horn CBO ������������������������������ kernel ��� HornMetrics.java������
 * ��������������������� HORN_PREFIX ������������������������ MetricRepo.DORIS_METRIC_REGISTER���
 * init() ��� MetricRepo.init() ���������������
 */
public class HornMetric {
    // Horn ��������������������������������������������������� doris_fe_������
    // ��������������������������������������� horn_cbo_ ��������������� "horn_cbo_"���
    private static final String HORN_PREFIX = "horn_";

    // ���������������������
    public static LongCounterMetric COUNTER_HORN_QUERY;           // doris_fe_horn_query_total
    public static LongCounterMetric COUNTER_HORN_QUERY_SUPPORT;   // ..._query_support_total
    public static LongCounterMetric COUNTER_HORN_QUERY_SUCCESS;   // ..._query_success_total
    public static LongCounterMetric COUNTER_HORN_QUERY_ERROR;     // ..._query_error_total
    public static LongCounterMetric COUNTER_HORN_QUERY_FALLBACK;  // ..._query_fallback_total������ ratio ������
    // Doris ���������������forward/backward ������������������������������������ kernel ��������� error/fallback���
    public static LongCounterMetric COUNTER_HORN_QUERY_TRANSLATE_PLAN_ERROR; // ..._query_translate_plan_error_total

    // ��������������������������������������������������������� porting-plan ��2���
    public static GaugeMetricImpl<Double> GAUGE_HORN_FALLBACK_RATIO;
    public static GaugeMetricImpl<Long>   GAUGE_HORN_OPTIMIZE_LATENCY_NS;
    public static GaugeMetricImpl<Long>   GAUGE_HORN_CASCADE_GROUP_COUNT;
    public static GaugeMetricImpl<Long>   GAUGE_HORN_CASCADE_GROUP_EXPR_COUNT;
    public static GaugeMetricImpl<Double> GAUGE_HORN_CASCADE_SAFE_TO_PRUNE_RATIO;
    public static GaugeMetricImpl<Long>   GAUGE_HORN_SCHEDULER_JOB_COUNT;
    public static GaugeMetricImpl<Long>   GAUGE_HORN_DPHYPER_CSG_CMP_COUNT;
    public static GaugeMetricImpl<Double> GAUGE_HORN_DPHYPER_ENUM_SUCCESS_RATIO;

    /** ��������������������������������������������������������� horn_��� */
    private static String name(String suffix) {
        return HORN_PREFIX + suffix;
    }

    public static void init() {
        COUNTER_HORN_QUERY = register(new LongCounterMetric(name("query_total"),
                MetricUnit.REQUESTS, "total queries entering horn optimize path"));
        COUNTER_HORN_QUERY_SUPPORT = register(new LongCounterMetric(name("query_support_total"),
                MetricUnit.REQUESTS, "queries that attempted horn optimization"));
        COUNTER_HORN_QUERY_SUCCESS = register(new LongCounterMetric(name("query_success_total"),
                MetricUnit.REQUESTS, "queries optimized by horn successfully"));
        COUNTER_HORN_QUERY_ERROR = register(new LongCounterMetric(name("query_error_total"),
                MetricUnit.REQUESTS, "queries where horn threw an error"));
        COUNTER_HORN_QUERY_FALLBACK = register(new LongCounterMetric(name("query_fallback_total"),
                MetricUnit.REQUESTS, "queries that fell back from horn to nereids"));
        COUNTER_HORN_QUERY_TRANSLATE_PLAN_ERROR = register(new LongCounterMetric(
                name("query_translate_plan_error_total"),
                MetricUnit.REQUESTS,
                "queries where doris<->horn plan translation (forward or backward) failed"));

        // ���������GaugeMetricImpl ��������������� 4 ������ (name, unit, description, defaultValue)���
        GAUGE_HORN_FALLBACK_RATIO = register(new GaugeMetricImpl<>(name("fallback_ratio"),
                MetricUnit.PERCENT, "fraction of supported queries that fell back to nereids", 0.0));
        GAUGE_HORN_OPTIMIZE_LATENCY_NS = register(new GaugeMetricImpl<>(name("optimize_latency_ns"),
                MetricUnit.NANOSECONDS, "horn optimize wall time of last query (ns)", 0L));
        GAUGE_HORN_CASCADE_GROUP_COUNT = register(new GaugeMetricImpl<>(name("cascade_group_count"),
                MetricUnit.NOUNIT, "cascades memo group count of last query", 0L));
        GAUGE_HORN_CASCADE_GROUP_EXPR_COUNT = register(new GaugeMetricImpl<>(name("cascade_group_expression_count"),
                MetricUnit.NOUNIT, "cascades memo group expression count of last query", 0L));
        GAUGE_HORN_CASCADE_SAFE_TO_PRUNE_RATIO = register(new GaugeMetricImpl<>(name("cascade_safe_to_prune_ratio"),
                MetricUnit.PERCENT, "safe-to-prune ratio of last query", 0.0));
        GAUGE_HORN_SCHEDULER_JOB_COUNT = register(new GaugeMetricImpl<>(name("scheduler_job_count"),
                MetricUnit.NOUNIT, "optimizer scheduler job count of last query", 0L));
        GAUGE_HORN_DPHYPER_CSG_CMP_COUNT = register(new GaugeMetricImpl<>(name("dphyper_csg_cmp_count"),
                MetricUnit.NOUNIT, "dphyper csg-cmp emit count of last query", 0L));
        GAUGE_HORN_DPHYPER_ENUM_SUCCESS_RATIO = register(new GaugeMetricImpl<>(
                name("dphyper_enumeration_successful_ratio"),
                MetricUnit.PERCENT, "dphyper enumeration successful ratio of last query", 0.0));
    }

    /** ��������������������� registry ��������������������������������������� */
    private static <M extends Metric> M register(M metric) {
        MetricRepo.DORIS_METRIC_REGISTER.addMetrics(metric);
        return metric;
    }

    /** fallback ratio = fallback / support������������������������ MetricCalculator ��������������� */
    public static void updateFallbackRatio() {
        long support = COUNTER_HORN_QUERY_SUPPORT.getValue();
        long fallback = COUNTER_HORN_QUERY_FALLBACK.getValue();
        GAUGE_HORN_FALLBACK_RATIO.setValue(support == 0 ? 0.0 : (double) fallback / (double) support);
    }
}