
org.glowroot.agent.shaded.grpc.netty.Utils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of glowroot-agent-it-harness Show documentation
Show all versions of glowroot-agent-it-harness Show documentation
Glowroot Agent Integration Test Harness
/*
* Copyright 2014, Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.glowroot.agent.shaded.grpc.netty;
import static org.glowroot.agent.shaded.grpc.internal.GrpcUtil.CONTENT_TYPE_KEY;
import static org.glowroot.agent.shaded.grpc.internal.GrpcUtil.USER_AGENT_KEY;
import static org.glowroot.agent.shaded.netty.util.CharsetUtil.UTF_8;
import org.glowroot.agent.shaded.google.common.base.Preconditions;
import org.glowroot.agent.shaded.google.common.util.concurrent.ThreadFactoryBuilder;
import org.glowroot.agent.shaded.grpc.Metadata;
import org.glowroot.agent.shaded.grpc.Status;
import org.glowroot.agent.shaded.grpc.internal.GrpcUtil;
import org.glowroot.agent.shaded.grpc.internal.SharedResourceHolder.Resource;
import org.glowroot.agent.shaded.grpc.internal.TransportFrameUtil;
import org.glowroot.agent.shaded.netty.channel.EventLoopGroup;
import org.glowroot.agent.shaded.netty.channel.nio.NioEventLoopGroup;
import org.glowroot.agent.shaded.netty.handler.codec.http2.DefaultHttp2Headers;
import org.glowroot.agent.shaded.netty.handler.codec.http2.Http2Exception;
import org.glowroot.agent.shaded.netty.handler.codec.http2.Http2Headers;
import org.glowroot.agent.shaded.netty.util.AsciiString;
import org.glowroot.agent.shaded.netty.util.concurrent.Future;
import org.glowroot.agent.shaded.netty.util.concurrent.GenericFutureListener;
import java.io.IOException;
import java.nio.channels.ClosedChannelException;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
/**
* Common utility methods.
*/
class Utils {
public static final AsciiString STATUS_OK = AsciiString.of("200");
public static final AsciiString HTTP_METHOD = AsciiString.of(GrpcUtil.HTTP_METHOD);
public static final AsciiString HTTPS = AsciiString.of("https");
public static final AsciiString HTTP = AsciiString.of("http");
public static final AsciiString CONTENT_TYPE_HEADER = AsciiString.of(CONTENT_TYPE_KEY.name());
public static final AsciiString CONTENT_TYPE_GRPC = AsciiString.of(GrpcUtil.CONTENT_TYPE_GRPC);
public static final AsciiString TE_HEADER = AsciiString.of("te");
public static final AsciiString TE_TRAILERS = AsciiString.of(GrpcUtil.TE_TRAILERS);
public static final AsciiString USER_AGENT = AsciiString.of(USER_AGENT_KEY.name());
public static final Resource DEFAULT_BOSS_EVENT_LOOP_GROUP =
new DefaultEventLoopGroupResource(1, "grpc-default-boss-ELG");
public static final Resource DEFAULT_WORKER_EVENT_LOOP_GROUP =
new DefaultEventLoopGroupResource(0, "grpc-default-worker-ELG");
public static Metadata convertHeaders(Http2Headers http2Headers) {
return new Metadata(convertHeadersToArray(http2Headers));
}
private static byte[][] convertHeadersToArray(Http2Headers http2Headers) {
// The Netty AsciiString class is really just a wrapper around a byte[] and supports
// arbitrary binary data, not just ASCII.
byte[][] headerValues = new byte[http2Headers.size() * 2][];
int i = 0;
for (Map.Entry entry : http2Headers) {
headerValues[i++] = bytes(entry.getKey());
headerValues[i++] = bytes(entry.getValue());
}
return TransportFrameUtil.toRawSerializedHeaders(headerValues);
}
private static byte[] bytes(CharSequence seq) {
if (seq instanceof AsciiString) {
// Fast path - no copy.
return ((AsciiString) seq).array();
}
// Slow path - copy.
return seq.toString().getBytes(UTF_8);
}
public static Http2Headers convertClientHeaders(Metadata headers,
AsciiString scheme,
AsciiString defaultPath,
AsciiString authority) {
Preconditions.checkNotNull(defaultPath, "defaultPath");
Preconditions.checkNotNull(authority, "authority");
// Add any application-provided headers first.
Http2Headers http2Headers = convertMetadata(headers);
// Now set GRPC-specific default headers.
http2Headers.authority(authority)
.path(defaultPath)
.method(HTTP_METHOD)
.scheme(scheme)
.set(CONTENT_TYPE_HEADER, CONTENT_TYPE_GRPC)
.set(TE_HEADER, TE_TRAILERS);
// Set the User-Agent header.
String userAgent = GrpcUtil.getGrpcUserAgent("netty", headers.get(USER_AGENT_KEY));
http2Headers.set(USER_AGENT, new AsciiString(userAgent.getBytes(UTF_8)));
return http2Headers;
}
public static Http2Headers convertServerHeaders(Metadata headers) {
Http2Headers http2Headers = convertMetadata(headers);
http2Headers.set(CONTENT_TYPE_HEADER, CONTENT_TYPE_GRPC);
http2Headers.status(STATUS_OK);
return http2Headers;
}
public static Metadata convertTrailers(Http2Headers http2Headers) {
return new Metadata(convertHeadersToArray(http2Headers));
}
public static Http2Headers convertTrailers(Metadata trailers, boolean headersSent) {
Http2Headers http2Trailers = convertMetadata(trailers);
if (!headersSent) {
http2Trailers.set(Utils.CONTENT_TYPE_HEADER, Utils.CONTENT_TYPE_GRPC);
http2Trailers.status(STATUS_OK);
}
return http2Trailers;
}
private static Http2Headers convertMetadata(Metadata headers) {
Preconditions.checkNotNull(headers, "headers");
Http2Headers http2Headers = new DefaultHttp2Headers();
byte[][] serializedHeaders = TransportFrameUtil.toHttp2Headers(headers);
for (int i = 0; i < serializedHeaders.length; i += 2) {
AsciiString name = new AsciiString(serializedHeaders[i], false);
AsciiString value = new AsciiString(serializedHeaders[i + 1], false);
http2Headers.add(name, value);
}
return http2Headers;
}
public static Status statusFromThrowable(Throwable t) {
Status s = Status.fromThrowable(t);
if (s.getCode() != Status.Code.UNKNOWN) {
return s;
}
// TODO(ejona): reenable once startup races are resolved; ClosedChannelException is being seen
// still. Some tests are asserting UNAVAILABLE and were "working" previously but are now
// detecting that our behavior is flaky. See #1330
if (false && t instanceof ClosedChannelException) {
// ClosedChannelException is used any time the Netty channel is closed. Proper error
// processing requires remembering the error that occurred before this one and using it
// instead.
return Status.UNKNOWN.withCause(t);
}
if (t instanceof IOException) {
return Status.UNAVAILABLE.withCause(t);
}
if (t instanceof Http2Exception) {
return Status.INTERNAL.withCause(t);
}
return s;
}
private static class DefaultEventLoopGroupResource implements Resource {
private final String name;
private final int numEventLoops;
DefaultEventLoopGroupResource(int numEventLoops, String name) {
this.name = name;
this.numEventLoops = numEventLoops;
}
@Override
public EventLoopGroup create() {
// Use the executor based constructor so we can work with both Netty4 & Netty5.
ThreadFactory threadFactory = new ThreadFactoryBuilder()
.setDaemon(true)
.setNameFormat(name + "-%d")
.build();
int parallelism = numEventLoops == 0
? Runtime.getRuntime().availableProcessors() * 2 : numEventLoops;
final ExecutorService executor = Executors.newFixedThreadPool(parallelism, threadFactory);
NioEventLoopGroup nioEventLoopGroup = new NioEventLoopGroup(parallelism, executor);
nioEventLoopGroup.terminationFuture().addListener(
new GenericFutureListener>() {
@Override
public void operationComplete(Future
© 2015 - 2025 Weber Informatics LLC | Privacy Policy