Skip to content

Commit 5a6464c

Browse files
authored
Add HTTP request info to leaking buffers (#116130)
1 parent 219372e commit 5a6464c

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpServerTransport.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import io.netty.handler.timeout.ReadTimeoutException;
3434
import io.netty.handler.timeout.ReadTimeoutHandler;
3535
import io.netty.util.AttributeKey;
36+
import io.netty.util.ResourceLeakDetector;
3637

3738
import org.apache.logging.log4j.LogManager;
3839
import org.apache.logging.log4j.Logger;
@@ -410,6 +411,9 @@ protected Result beginEncode(HttpResponse httpResponse, String acceptEncoding) t
410411
}
411412
});
412413
}
414+
if (ResourceLeakDetector.isEnabled()) {
415+
ch.pipeline().addLast(new Netty4LeakDetectionHandler());
416+
}
413417
ch.pipeline()
414418
.addLast(
415419
"pipelining",
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.http.netty4;
11+
12+
import io.netty.channel.ChannelHandlerContext;
13+
import io.netty.channel.ChannelInboundHandlerAdapter;
14+
import io.netty.handler.codec.http.HttpContent;
15+
import io.netty.handler.codec.http.HttpRequest;
16+
17+
import org.elasticsearch.tasks.Task;
18+
19+
/**
20+
* Inbound channel handler that enrich leaking buffers information from HTTP request.
21+
* It helps to detect which handler is leaking buffers. Especially integration tests that run with
22+
* paranoid leak detector that samples all buffers for leaking. Supplying informative opaque-id in
23+
* integ test helps to narrow down problem (for example test name).
24+
*/
25+
public class Netty4LeakDetectionHandler extends ChannelInboundHandlerAdapter {
26+
27+
private String info;
28+
29+
@Override
30+
public void channelRead(ChannelHandlerContext ctx, Object msg) {
31+
if (msg instanceof HttpRequest request) {
32+
var opaqueId = request.headers().get(Task.X_OPAQUE_ID_HTTP_HEADER);
33+
info = "method: " + request.method() + "; uri: " + request.uri() + "; x-opaque-id: " + opaqueId;
34+
}
35+
if (msg instanceof HttpContent content) {
36+
content.touch(info);
37+
}
38+
ctx.fireChannelRead(msg);
39+
}
40+
}

0 commit comments

Comments
 (0)