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行打印是它真正的调用位置。

相关推荐
在未来等你2 小时前
Elasticsearch面试精讲 Day 18:内存管理与JVM调优
大数据·分布式·elasticsearch·搜索引擎·面试
智海观潮2 小时前
Spark SQL | 目前Spark社区最活跃的组件之一
大数据·spark
Lx3522 小时前
Hadoop数据一致性保障:处理分布式系统常见问题
大数据·hadoop
婲落ヽ紅顏誶3 小时前
测试es向量检索
大数据·elasticsearch·搜索引擎
IT学长编程3 小时前
计算机毕业设计 基于Hadoop豆瓣电影数据可视化分析设计与实现 Python 大数据毕业设计 Hadoop毕业设计选题【附源码+文档报告+安装调试
大数据·hadoop·python·django·毕业设计·毕业论文·豆瓣电影数据可视化分析
semantist@语校4 小时前
第二十篇|SAMU教育学院的教育数据剖析:制度阈值、能力矩阵与升学网络
大数据·数据库·人工智能·百度·语言模型·矩阵·prompt
Dobby_054 小时前
【Hadoop】Yarn:Hadoop 生态的资源操作系统
大数据·hadoop·分布式·yarn
数智顾问4 小时前
基于Hadoop进程的分布式计算任务调度与优化实践——深入理解分布式计算引擎的核心机制
大数据
笨蛋少年派5 小时前
安装Hadoop中遇到的一些问题和解决
大数据·hadoop·分布式
在未来等你5 小时前
Kafka面试精讲 Day 18:磁盘IO与网络优化
大数据·分布式·面试·kafka·消息队列