flink源码分析 - 获取调用位置信息

flink版本: flink-1.11.2

调用位置: org.apache.flink.streaming.api.datastream.DataStream#flatMap(org.apache.flink.api.common.functions.FlatMapFunction<T,R>)

代码核心位置: org.apache.flink.api.java.Utils#getCallLocationName()

flink算子flatmap中调用了一个 Utils.getCallLocationName() 方法:

具体源码实现如下(为方便查看,该源码类中其他内容未加入):

复制代码
/*
 * 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.flink.api.java;

import org.apache.flink.annotation.Internal;
import org.apache.flink.api.common.accumulators.Accumulator;
import org.apache.flink.api.common.accumulators.SerializedListAccumulator;
import org.apache.flink.api.common.accumulators.SimpleAccumulator;
import org.apache.flink.api.common.io.RichOutputFormat;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.api.common.typeutils.CompositeType;
import org.apache.flink.api.common.typeutils.TypeSerializer;
import org.apache.flink.api.java.typeutils.GenericTypeInfo;
import org.apache.flink.configuration.Configuration;

import org.apache.commons.lang3.StringUtils;

import javax.annotation.Nullable;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Optional;
import java.util.Random;

/** Utility class that contains helper methods to work with Java APIs. */
@Internal
public final class Utils {

    public static final Random RNG = new Random();

    public static String getCallLocationName() {
        return getCallLocationName(4);
    }

    public static String getCallLocationName(int depth) {
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();

        if (stackTrace.length <= depth) {
            return "<unknown>";
        }

        StackTraceElement elem = stackTrace[depth];

        return String.format(
                "%s(%s:%d)", elem.getMethodName(), elem.getFileName(), elem.getLineNumber());
    }

  

    /** Private constructor to prevent instantiation. */
    private Utils() {
        throw new RuntimeException();
    }
}

从中可以看出,该方法主要使用Thread.currentThread().getStackTrace();获取到调用栈信息,然后从调用栈信息中获取指定层级的调用栈元素作为调用位置信息。

个人做了一个简单测试,将Utils.getCallLocationName()的关键代码拷贝出来执行:

可以看到,这段代码运行在main方法中,它的调用栈信息是代码21行日志打印的结果,代码22行打印是它真正的调用位置。

相关推荐
Elastic 中国社区官方博客3 分钟前
Elasticsearch 向量数据库:几分钟内完成部署,以经济高效的方式扩展至数千亿规模
大数据·运维·数据库·elasticsearch·搜索引擎·ai·全文检索
A hao8 分钟前
LED视频处理器中帧率与刷新率的区别
大数据·图像处理·人工智能·音视频
AgentMaster8 分钟前
数据治理工具选型指南:一套可复用的四阶段决策框架
大数据·数据结构·人工智能·算法
数融智域12 分钟前
2026年获客智能体排行榜top前五
大数据·人工智能
Zen200336 分钟前
建文AI·新能源项目管理软件解决方案:从立项→设计→采购→施工→并网,一条线管到底,专为光伏、风电、储能、充电桩工程打造;让新能源项目,交付快一步
大数据·工程项目管理软件·新能源项目管理·光伏项目管理·风电项目管理·充电桩项目管理·储能项目管理软件
haon11221 小时前
树模型在信贷风控怎么用——从决策树到 XGBoost
大数据·人工智能·算法·决策树·机器学习·数据挖掘
长谷深风1111 小时前
Agent 跑了 30 分钟宕机,如何从断点继续?
java·大数据·人工智能·ai·大模型·memory·aiagent
TDengine (老段)1 小时前
TDgpt 使用 — 部署、SQL、算法
大数据·数据库·sql·算法·时序数据库·tdengine·涛思数据
starzy19901 小时前
Flink基础之Transformation API详解及代码实现:从map到connect
大数据·flink
字节跳动数据平台1 小时前
PDF 文档解析算子全新升级!不仅“读得懂”,更让解析结果直接可用
大数据