Functions Debug 终极指南

栏目: Java · 发布时间: 6年前

内容简介:作者:冉小龙审校:Jennifer编辑:Anonymitaet

作者:冉小龙

审校:Jennifer

编辑:Anonymitaet

阅读本文需要约 10 分钟。

问:什么比七个工作日更让人不爽?

答:第八个(debug)。

Functions Debug 终极指南

行话说得好,开发 5 分钟,调试 2 小时。

大兄弟,你咋调试 Pulsar Function 阿?

Functions Debug 终极指南

重启?

Functions Debug 终极指南

撸鸭?

Functions Debug 终极指南

意念?

Functions Debug 终极指南

这也太随缘了吧

Functions Debug 终极指南

今天, Apache 龙 就来教教大家,debug 是一件多么舒坦的事! :dragon:

Functions Debug 终极指南

--------正经分界线--------

在之前的文章 Java Function Python Function Go Function 中,向大家详细介绍了如何基于自己的场景快速编写并部署 Pulsar  Functions。这篇文章将介绍如何利用 Pulsar 提供的 CLI 对 Functions 进行 Debug。

使用 LogTopic

Pulsar Functions 在设计之初,就考虑到如何对 Pulsar Functions 进行 Debug。

如下图所示,Pulsar Functions 允许用户将自己在 Functions 中定义好的日志信息输出到指定的 LogTopic 中,如果有遇到相关的问题需要从日志信息中获取,用户可以编写 consumer 从指定的 LogTopic 中消费消息,即可查看相关日志信息。

Functions Debug 终极指南

具体使用如下:

import org.apache.pulsar.functions.api.Context;
import org.apache.pulsar.functions.api.Function;
import org.slf4j.Logger;
public class LoggingFunction implements Function {
@Override
public void apply(String input, Context context) {
Logger LOG = context.getLogger();

String messageId = new String(context.getMessageId());


if (input.contains("danger")) {
LOG.warn("A warning was received in message {}", messageId);
} else {
LOG.info("Message {} received\nContent: {}", messageId, input);
}
return null;
}
}

如上述代码所示:

用户可以通过 context.getLogger() 获取 logger 对象并将其赋值给  slf4j 的  LOG  字段,这样用户就可以使用  LOG  字段在 Pulsar Functions 中定义自己想要的日志信息。

当你有日志信息需要输出时,在启动过程中,需要告诉 Pulsar Functions 你期望将日志信息输出到哪个 topic 中,具体使用如下:

bin/pulsar-admin functions create \

--log-topic persistent://public/default/logging-function-logs \
# Other function configs

Functions CLI

除了使用 LogTopic 通过自定义日志信息的方式来追踪 Functions 的问题外,用户还可以通过使用 Functions CLI 对 Pulsar Functions 进行调试。

Pulsar 将 Pulsar Functions 相关的 CLI其集成到 pulsar-admin  中;

使用 ./bin/pulsar-admin 和  Functions ,可以执行和 Pulsar Functions 相关的操作。

为了方便用户定位问题,Pulsar Functions 提供了如下几种 CLI:

  • get

  • stats

  • trigger

  • list

  • status

01

get

使用 get  命令可以获取当前 Pulsar Functions 的详细信息,它提供了如下参数:

  • FQFN

  • name

  • tenant

  • namespace

需要说明的是,

FQFN(Fully Qualified Functions Name)

是由 tenantnamespace 和  name 这三个字段组成,它可以唯一标识一个 Pulsar Functions。

用户使用时,可以使用 FQFN 或者同时指定 tenantnamespace 和  name 这三个字段。示例如下:

./bin/pulsar-admin functions get \

--tenant public \
--namespace default \
--name ExclamationFunctio6

执行如上命令之后,输出如下:

{

"tenant": "public",
"namespace": "default",
"name": "ExclamationFunctio6",
"className": "org.example.test.ExclamationFunction",
"inputSpecs": {
"persistent://public/default/my-topic-1": {
"isRegexPattern": false
}
},
"output": "persistent://public/default/test-1",
"processingGuarantees": "ATLEAST_ONCE",
"retainOrdering": false,
"userConfig": {},
"runtime": "JAVA",
"autoAck": true,
"parallelism": 1
}

通过 get 命令,可以获取到当前 Pulsar Functions 具体的输入、输出,运行的 runtime 是什么,当前 Functions 中有多少个 instance 在运行等信息。

02

list

list 命令可以获取当前 namespace 下有哪些 Functions 在运行,会返回运行的 Functions 的 name 列表,它提供了如下参数:

  • tenant

  • namespace

参考示例如下:

./bin/pulsar-admin functions list \

--tenant public \
--namespace default

执行如上命令后,输出如下:

ExclamationFunctio1

ExclamationFunctio2
ExclamationFunctio3

上述输出说明,在 default 这个 namespace 下,运行了三个 Pulsar Functions。

03

trigger

trigger  命令会模拟 Functions 执行的过程,可以用来验证当前 Functions 运行的整个链路是否正常,它提供了如下参数:

  • FQFN

  • tenant

  • namespace

  • name

  • topic

  • trigger-file

  • trigger-value

参考示例如下:

./bin/pulsar-admin functions trigger \

--tenant public \
--namespace default \
--name ExclamationFunctio6 \
--topic persistent://public/default/my-topic-1 \
--trigger-value "hello pulsar functions"

执行成功后会输出 This is my function!

注意

使用 --topic 指定具体的 topic 时,需要使用 topic 的 全称 ,否则会出现如下错误:

Function in trigger function has unidentified topic


Reason: Function in trigger function has unidentified topic

04

status

使用  status 命令可以查看当前 Functions 运行的状态信息,它提供了如下参数:

  • FQFN

  • tenant

  • namespace

  • name

  • instance-id

参考示例如下:

./bin/pulsar-admin functions status \

--tenant public \
--namespace default \
--name ExclamationFunctio6 \

执行上述命令后,输出如下:

{

"numInstances" : 1,
"numRunning" : 1,
"instances" : [ {
"instanceId" : 0,
"status" : {
"running" : true,
"error" : "",
"numRestarts" : 0,
"numReceived" : 1,
"numSuccessfullyProcessed" : 1,
"numUserExceptions" : 0,
"latestUserExceptions" : [ ],
"numSystemExceptions" : 0,
"latestSystemExceptions" : [ ],
"averageLatency" : 0.8385,
"lastInvocationTime" : 1557734137987,
"workerId" : "c-standalone-fw-23ccc88ef29b-8080"
}
} ]
}

status 命令中,可以查看到当前 Functions 是否处于运行状态,总共接收了多少条消息,成功处理了多少条消息,系统发生过多少次错误,平均延迟为多少等。由于一个 function 可以运行多个 instance,所以可以通过 --instance-id 来具体查看某个 instance 的详细信息。上述示例中, ExclamationFunctio6 的 function 下只运行了一个 instance。

05

stats

stats 命令主要用来获取当前 Functions 的统计信息,它提供了如下参数:

  • FQFN

  • tenant

  • namespace

  • name

  • instance-id

参数列表的含义与 status 命令一致, 参考示例如下:

/bin/pulsar-admin functions stats \

--tenant public \
--namespace default \
--name ExclamationFunctio6 \

执行上述命令后,输出如下:

{

"receivedTotal" : 1,
"processedSuccessfullyTotal" : 1,
"systemExceptionsTotal" : 0,
"userExceptionsTotal" : 0,
"avgProcessLatency" : 0.8385,
"1min" : {
"receivedTotal" : 0,
"processedSuccessfullyTotal" : 0,
"systemExceptionsTotal" : 0,
"userExceptionsTotal" : 0,
"avgProcessLatency" : null
},
"lastInvocation" : 1557734137987,
"instances" : [ {
"instanceId" : 0,
"metrics" : {
"receivedTotal" : 1,
"processedSuccessfullyTotal" : 1,
"systemExceptionsTotal" : 0,
"userExceptionsTotal" : 0,
"avgProcessLatency" : 0.8385,
"1min" : {
"receivedTotal" : 0,
"processedSuccessfullyTotal" : 0,
"systemExceptionsTotal" : 0,
"userExceptionsTotal" : 0,
"avgProcessLatency" : null
},
"lastInvocation" : 1557734137987,
"userMetrics" : { }
}
} ]
}

运行 Pulsar Functions 出现问题时,你可以结合上述 Debug 命令和自定义的日志信息,对 Pulsar Functions 的问题进行很好的定位和分析。

更多 Pulsar Functions 系列文章,戳 :point_down|type_1_2:

--------歪楼分界线--------

学成后我心情爽朗, 想吟诗一首 :sunglasses:

耳闻 码农 鄙视链

三等码农搞架构, 高并低延能吹牛

二等码农搞算法, 吃香喝辣调调参

一等码农参 会议手拿三分屁屁踢

特等码农有对象, 搞啥我都看不上

Functions Debug 终极指南

会议现场 一睹我 一等 码农  Apache 龙 的火力吗? :fire:

Functions Debug 终极指南

还有私信问我  Apache 龙 是不是 特等 码农的妹纸们  :heart_eyes:

带胆来 Pulsar 深圳 Meetup 现场验货阿 :point_down|type_1_2:

Functions Debug 终极指南

Functions Debug 终极指南

Functions Debug 终极指南

点击 阅读原文 ,报名  Pulsar 深圳 Meetup

Functions Debug 终极指南


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Flexible Rails

Flexible Rails

Peter Armstrong / Manning Publications / 2008-01-23 / USD 44.99

Rails is a fantastic tool for web application development, but its Ajax-driven interfaces stop short of the richness you gain with a tool like Adobe Flex. Simply put, Flex is the most productive way t......一起来看看 《Flexible Rails》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具