org.graylog2.inputs.transports.netty.EnvelopeMessageAggregationHandler Maven / Gradle / Ivy
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* .
*/
package org.graylog2.inputs.transports.netty;
import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import io.netty.buffer.ByteBuf;
import io.netty.channel.AddressedEnvelope;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.graylog2.plugin.inputs.codecs.CodecAggregator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.InetSocketAddress;
public class EnvelopeMessageAggregationHandler extends SimpleChannelInboundHandler> {
private static final Logger LOG = LoggerFactory.getLogger(EnvelopeMessageAggregationHandler.class);
private final CodecAggregator aggregator;
private final Timer aggregationTimer;
private final Meter invalidChunksMeter;
public EnvelopeMessageAggregationHandler(CodecAggregator aggregator, MetricRegistry metricRegistry) {
this.aggregator = aggregator;
aggregationTimer = metricRegistry.timer("aggregationTime");
invalidChunksMeter = metricRegistry.meter("invalidMessages");
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, AddressedEnvelope envelope) throws Exception {
final CodecAggregator.Result result;
try (Timer.Context ignored = aggregationTimer.time()) {
result = aggregator.addChunk(envelope.content());
}
final ByteBuf completeMessage = result.getMessage();
if (completeMessage != null) {
LOG.debug("Message aggregation completion, forwarding {}", completeMessage);
ctx.fireChannelRead(SenderEnvelope.of(completeMessage, envelope.sender()));
} else if (result.isValid()) {
LOG.debug("More chunks necessary to complete this message");
} else {
invalidChunksMeter.mark();
LOG.debug("Message chunk was not valid and discarded.");
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable e) throws Exception {
LOG.error("Caught exception while decoding type of GELF packet: {}", e.getMessage());
invalidChunksMeter.mark();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy