NiFi处理器链下HandleHttpResponse报找不到对应标识HTTP响应对象的问题排查
NiFi处理链路配置
- 全链路流向:
HandleHttpRequest->RouteOnAttribute->HandleHttpResponse RouteOnAttribute自定义匹配规则:- 规则属性名:
test - 匹配逻辑:
${http.headers.type:contains('test')}
- 规则属性名:
- 对应流程的NiFi模板、配置参考截图均已提前归档留存
Python客户端请求脚本
import requests import json import traceback nifi_url = "http://localhost:8009/report" def uploadLogs(fileName): f = open(fileName, 'rb') payload = { "this": "that" } files = {'info': (None, json.dumps(payload), 'application/json; charset=utf-8'), 'file': ('current', f, "text/plain; charset=us-ascii")} headers = {'type': 'test', "Keep-Alive": "timeout=100", "Content-Transfer-Encoding": "8bit", "Connection": "Keep-Alive"} url = '' try: url = nifi_url response = requests.post(url, files=files, headers=headers) print(response.status_code) if (response != None and (response.status_code == 200 or response.status_code == 201)): return True except: traceback.print_exc() return False filename = "C:\\sample.txt" print(uploadLogs(filename))
实际运行表现
- 客户端可正常收到与配置一致的200状态码,链路基础连通性正常
- NiFi后台持续抛出错误日志:
nifi_1 | 2020-11-13 19:14:09,360 ERROR [Timer-Driven Process Thread-7] o.a.n.p.standard.HandleHttpResponse
HandleHttpResponse[id=39d755ed-f825-3013-ce47-5218721d27d4] Failed to respond to HTTP request for StandardFlowFileRecord[uuid=1a422f48-41e3-4eb9-9390-73b12762c5dc,claim=StandardContentClaim [resourceClaim=StandardResourceClaim[id=1605269814303-549, container=default, section=549], offset=39642, length=16],offset=0,name=1a422f48-41e3-4eb9-9390-73b12762c5dc,size=16] because FlowFile had an 'http.context.identifier' attribute of 5e7dc338-8684-491d-a554-6392e2a0e3b9 but could not find an HTTP Response Object for this identifier
该报错不属于预期运行行为。
报错触发的核心原因
HandleHttpRequest 与 HandleHttpResponse 采用HTTP上下文绑定缓存机制实现请求-响应的配对:
- 当
HandleHttpRequest接收到外部请求时,会为对应的FlowFile生成唯一的http.context.identifier,同时在本地临时缓存中存储该标识符对应的HTTP请求句柄,等待HandleHttpResponse消费后完成响应,缓存会在响应完成后立即清除,或超过默认60秒最大生命周期后自动清理。 - 当前流程存在**多路径FlowFile重复流向
HandleHttpResponse**的问题:第一次携带该http.context.identifier的FlowFile会正常被HandleHttpResponse消费,成功返回200响应给客户端,并清除本地缓存的HTTP上下文。剩余重复流向HandleHttpResponse的FlowFile依然带着已经失效的http.context.identifier,此时缓存中已经没有对应的HTTP句柄,就会抛出上述找不到Response Object的错误。
常见的重复流向场景包括:
HandleHttpResponse除了连接RouteOnAttribute的test规则成功分支外,还额外连接了RouteOnAttribute的unmatched分支、其他处理器的流向,接收了不应该触发响应逻辑的FlowFileRouteOnAttribute配置的多个匹配规则同时命中同一个入站FlowFile,导致同一个FlowFile被同时输出到多个出向关系,其中多个关系最终都流向HandleHttpResponse- 流程中存在其他处理器转发、复制FlowFile的逻辑,把携带
http.context.identifier的FlowFile意外路由到了HandleHttpResponse的入站连接
具体修复步骤
- 梳理所有流入
HandleHttpResponse的连接,确保只有RouteOnAttribute的test规则匹配成功分支,是唯一可以触发响应逻辑的路径,删除其他所有无关的入站连接 - 检查
RouteOnAttribute的规则配置,确保入站请求只会命中test这一个规则,避免同一个FlowFile被多规则同时匹配、输出到多个出向关系 - 对所有不触发响应逻辑的分支,增加临时
UpdateAttribute处理器,删除FlowFile上的http.context.identifier属性,避免这些FlowFile误触发HandleHttpResponse的响应逻辑 - 如果流程处理耗时较长、存在超过60秒的延迟,可调整
HandleHttpRequest的Incoming Request Maximum Lifespan配置,延长HTTP上下文的缓存保留时间,避免上下文提前过期被清理
内容的提问来源于stack exchange,提问作者Sahil Saxena

