org.elasticsearch.transport.InboundPipeline Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of elasticsearch Show documentation
Show all versions of elasticsearch Show documentation
Elasticsearch subproject :server
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
package org.elasticsearch.transport;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.Version;
import org.elasticsearch.common.breaker.CircuitBreaker;
import org.elasticsearch.common.bytes.CompositeBytesReference;
import org.elasticsearch.common.bytes.ReleasableBytesReference;
import org.elasticsearch.common.recycler.Recycler;
import org.elasticsearch.core.Releasable;
import org.elasticsearch.core.Releasables;
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.LongSupplier;
import java.util.function.Supplier;
public class InboundPipeline implements Releasable {
private static final ThreadLocal> fragmentList = ThreadLocal.withInitial(ArrayList::new);
private static final InboundMessage PING_MESSAGE = new InboundMessage(null, true);
private final LongSupplier relativeTimeInMillis;
private final StatsTracker statsTracker;
private final InboundDecoder decoder;
private final InboundAggregator aggregator;
private final BiConsumer messageHandler;
private Exception uncaughtException;
private final ArrayDeque pending = new ArrayDeque<>(2);
private boolean isClosed = false;
public InboundPipeline(
Version version,
StatsTracker statsTracker,
Recycler recycler,
LongSupplier relativeTimeInMillis,
Supplier circuitBreaker,
Function> registryFunction,
BiConsumer messageHandler,
boolean ignoreDeserializationErrors
) {
this(
statsTracker,
relativeTimeInMillis,
new InboundDecoder(version, recycler),
new InboundAggregator(circuitBreaker, registryFunction, ignoreDeserializationErrors),
messageHandler
);
}
public InboundPipeline(
StatsTracker statsTracker,
LongSupplier relativeTimeInMillis,
InboundDecoder decoder,
InboundAggregator aggregator,
BiConsumer messageHandler
) {
this.relativeTimeInMillis = relativeTimeInMillis;
this.statsTracker = statsTracker;
this.decoder = decoder;
this.aggregator = aggregator;
this.messageHandler = messageHandler;
}
@Override
public void close() {
isClosed = true;
Releasables.closeExpectNoException(decoder, aggregator, () -> Releasables.close(pending), pending::clear);
}
public void handleBytes(TcpChannel channel, ReleasableBytesReference reference) throws IOException {
if (uncaughtException != null) {
throw new IllegalStateException("Pipeline state corrupted by uncaught exception", uncaughtException);
}
try {
doHandleBytes(channel, reference);
} catch (Exception e) {
uncaughtException = e;
throw e;
}
}
public void doHandleBytes(TcpChannel channel, ReleasableBytesReference reference) throws IOException {
channel.getChannelStats().markAccessed(relativeTimeInMillis.getAsLong());
statsTracker.markBytesRead(reference.length());
pending.add(reference.retain());
final ArrayList