|
| 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.ChannelDuplexHandler; |
| 13 | +import io.netty.channel.ChannelHandlerContext; |
| 14 | +import io.netty.util.concurrent.ScheduledFuture; |
| 15 | + |
| 16 | +import org.apache.logging.log4j.LogManager; |
| 17 | +import org.apache.logging.log4j.Logger; |
| 18 | +import org.elasticsearch.common.time.TimeProvider; |
| 19 | +import org.elasticsearch.common.util.concurrent.FutureUtils; |
| 20 | + |
| 21 | +import java.util.concurrent.TimeUnit; |
| 22 | + |
| 23 | +/** |
| 24 | + * When channel auto-read is disabled handlers are responsible to read from channel. |
| 25 | + * But it's hard to detect when read is missing. This helper class print warnings |
| 26 | + * when no reads where detected in given time interval. Normally, in tests, 10 seconds is enough |
| 27 | + * to avoid test hang for too long, but can be increased if needed. |
| 28 | + */ |
| 29 | +class MissingReadDetector extends ChannelDuplexHandler { |
| 30 | + |
| 31 | + private static final Logger logger = LogManager.getLogger(MissingReadDetector.class); |
| 32 | + |
| 33 | + private final long interval; |
| 34 | + private final TimeProvider timer; |
| 35 | + private boolean pendingRead; |
| 36 | + private long lastRead; |
| 37 | + private ScheduledFuture<?> checker; |
| 38 | + |
| 39 | + MissingReadDetector(TimeProvider timer, long missingReadIntervalMillis) { |
| 40 | + this.interval = missingReadIntervalMillis; |
| 41 | + this.timer = timer; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public void handlerAdded(ChannelHandlerContext ctx) throws Exception { |
| 46 | + checker = ctx.channel().eventLoop().scheduleAtFixedRate(() -> { |
| 47 | + if (pendingRead == false) { |
| 48 | + long now = timer.absoluteTimeInMillis(); |
| 49 | + if (now >= lastRead + interval) { |
| 50 | + logger.warn("chan-id={} haven't read from channel for [{}ms]", ctx.channel().id(), (now - lastRead)); |
| 51 | + } |
| 52 | + } |
| 53 | + }, interval, interval, TimeUnit.MILLISECONDS); |
| 54 | + super.handlerAdded(ctx); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { |
| 59 | + if (checker != null) { |
| 60 | + FutureUtils.cancel(checker); |
| 61 | + } |
| 62 | + super.handlerRemoved(ctx); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void read(ChannelHandlerContext ctx) throws Exception { |
| 67 | + pendingRead = true; |
| 68 | + ctx.read(); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { |
| 73 | + assert ctx.channel().config().isAutoRead() == false : "auto-read must be always disabled"; |
| 74 | + pendingRead = false; |
| 75 | + lastRead = timer.absoluteTimeInMillis(); |
| 76 | + ctx.fireChannelRead(msg); |
| 77 | + } |
| 78 | +} |
0 commit comments