Java Code Examples for io.grpc.MethodDescriptor#getFullMethodName()

The following examples show how to use io.grpc.MethodDescriptor#getFullMethodName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CronetClientTransport.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Override
public CronetClientStream newStream(final MethodDescriptor<?, ?> method, final Metadata headers,
    final CallOptions callOptions) {
  Preconditions.checkNotNull(method, "method");
  Preconditions.checkNotNull(headers, "headers");

  final String defaultPath = "/" + method.getFullMethodName();
  final String url = "https://" + authority + defaultPath;

  final StatsTraceContext statsTraceCtx =
      StatsTraceContext.newClientContext(callOptions, headers);
  class StartCallback implements Runnable {
    final CronetClientStream clientStream = new CronetClientStream(
        url, userAgent, executor, headers, CronetClientTransport.this, this, lock, maxMessageSize,
        alwaysUsePut, method, statsTraceCtx, callOptions, transportTracer);

    @Override
    public void run() {
      synchronized (lock) {
        if (goAway) {
          clientStream.transportState().transportReportStatus(goAwayStatus, true, new Metadata());
        } else if (started) {
          startStream(clientStream);
        } else {
          throw new AssertionError("Transport is not started");
        }
      }
    }
  }

  return new StartCallback().clientStream;
}
 
Example 2
Source File: GrpcUtils.java    From grpc-spring-boot-starter with MIT License 5 votes vote down vote up
/**
 * Extracts the method name from the given method.
 *
 * @param method The method to get the method name from.
 * @return The extracted method name.
 * @see #extractServiceName(MethodDescriptor)
 */
public static String extractMethodName(final MethodDescriptor<?, ?> method) {
    // This method is the equivalent of MethodDescriptor.extractFullServiceName
    final String fullMethodName = method.getFullMethodName();
    final int index = fullMethodName.lastIndexOf('/');
    if (index == -1) {
        return fullMethodName;
    }
    return fullMethodName.substring(index + 1);
}
 
Example 3
Source File: GrpcUtils.java    From grpc-spring-boot-starter with MIT License 5 votes vote down vote up
/**
 * Extracts the method name from the given method.
 *
 * @param method The method to get the method name from.
 * @return The extracted method name.
 * @see #extractServiceName(MethodDescriptor)
 */
public static String extractMethodName(final MethodDescriptor<?, ?> method) {
    // This method is the equivalent of MethodDescriptor.extractFullServiceName
    final String fullMethodName = method.getFullMethodName();
    final int index = fullMethodName.lastIndexOf('/');
    if (index == -1) {
        return fullMethodName;
    }
    return fullMethodName.substring(index + 1);
}
 
Example 4
Source File: CronetClientTransport.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
public CronetClientStream newStream(final MethodDescriptor<?, ?> method, final Metadata headers,
    final CallOptions callOptions) {
  Preconditions.checkNotNull(method, "method");
  Preconditions.checkNotNull(headers, "headers");

  final String defaultPath = "/" + method.getFullMethodName();
  final String url = "https://" + authority + defaultPath;

  final StatsTraceContext statsTraceCtx =
      StatsTraceContext.newClientContext(callOptions, attrs, headers);
  class StartCallback implements Runnable {
    final CronetClientStream clientStream = new CronetClientStream(
        url, userAgent, executor, headers, CronetClientTransport.this, this, lock, maxMessageSize,
        alwaysUsePut, method, statsTraceCtx, callOptions, transportTracer, useGetForSafeMethods,
        usePutForIdempotentMethods);

    @Override
    public void run() {
      synchronized (lock) {
        if (goAway) {
          clientStream.transportState().transportReportStatus(goAwayStatus, true, new Metadata());
        } else if (started) {
          startStream(clientStream);
        } else {
          throw new AssertionError("Transport is not started");
        }
      }
    }
  }

  return new StartCallback().clientStream;
}
 
Example 5
Source File: NettyClientTransport.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
public ClientStream newStream(
    MethodDescriptor<?, ?> method, Metadata headers, CallOptions callOptions) {
  Preconditions.checkNotNull(method, "method");
  Preconditions.checkNotNull(headers, "headers");
  if (channel == null) {
    return new FailingClientStream(statusExplainingWhyTheChannelIsNull);
  }
  StatsTraceContext statsTraceCtx =
      StatsTraceContext.newClientContext(callOptions, getAttributes(), headers);
  return new NettyClientStream(
      new NettyClientStream.TransportState(
          handler,
          channel.eventLoop(),
          maxMessageSize,
          statsTraceCtx,
          transportTracer,
          method.getFullMethodName()) {
        @Override
        protected Status statusFromFailedFuture(ChannelFuture f) {
          return NettyClientTransport.this.statusFromFailedFuture(f);
        }
      },
      method,
      headers,
      channel,
      authority,
      negotiationScheme,
      userAgent,
      statsTraceCtx,
      transportTracer,
      callOptions,
      useGetForSafeMethods);
}
 
Example 6
Source File: ChannelLoggingInterceptor.java    From genie with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
    final MethodDescriptor<ReqT, RespT> method,
    final CallOptions callOptions,
    final Channel next
) {
    final String methodType = method.getType().toString();
    final String methodName = method.getFullMethodName();
    final int channelId = next.hashCode();
    log.info("gRPC {} call: {} (channel: {})", methodType, methodName, channelId);
    return next.newCall(method, callOptions);
}
 
Example 7
Source File: LoggingInterceptor.java    From google-ads-java with Apache License 2.0 4 votes vote down vote up
private static <ReqT, RespT> String getMethodName(MethodDescriptor<ReqT, RespT> method) {
  return method == null ? null : method.getFullMethodName();
}
 
Example 8
Source File: OperationNameConstructor.java    From java-grpc with Apache License 2.0 4 votes vote down vote up
@Override
public <ReqT, RespT> String constructOperationName(MethodDescriptor<ReqT, RespT> method) {
  return method.getFullMethodName();
}
 
Example 9
Source File: MethodHandlersBuilder.java    From titus-control-plane with Apache License 2.0 4 votes vote down vote up
private GrpcToReactorMethodBinding findReactorMethod(MethodDescriptor<?, ?> methodDescriptor) {
    String methodName = toMethodNameFromFullName(methodDescriptor.getFullMethodName());
    Method reactorMethod = reactorMethodMap.get(methodName);
    Preconditions.checkNotNull(reactorMethod, "Cannot find corresponding Reactor method for: {}", methodDescriptor);

    ProtoMethodDescriptorSupplier methodDescriptorSupplier = (ProtoMethodDescriptorSupplier) methodDescriptor.getSchemaDescriptor();
    Descriptors.MethodDescriptor md = methodDescriptorSupplier.getMethodDescriptor();
    String inputTypeName = md.getInputType().getName();
    String outputTypeName = md.getOutputType().getName();

    Class<?> reactorReturnType = reactorMethod.getReturnType();
    boolean isMono = reactorReturnType.isAssignableFrom(Mono.class);
    boolean isFlux = !isMono && reactorReturnType.isAssignableFrom(Flux.class);
    Preconditions.checkArgument(isMono || isFlux, "Mono or Flux return types allowed only");
    Type[] returnTypeParameters = ((ParameterizedType) reactorMethod.getGenericReturnType()).getActualTypeArguments();
    Preconditions.checkArgument(
            returnTypeParameters != null && returnTypeParameters.length == 1,
            "Expected one type parameter in the return type: %s", methodDescriptor.getFullMethodName()
    );
    Class returnTypeParameter = (Class) returnTypeParameters[0];

    // Check return types
    if (returnTypeParameter == Void.class) {
        Preconditions.checkArgument(
                outputTypeName.equals("Empty"),
                "Reactor Mono<Void>/Flux<Void> can be mapped to GRPC/Empty only: %s", methodDescriptor.getFullMethodName()
        );
    } else {
        Preconditions.checkArgument(
                returnTypeParameter.getSimpleName().equals(outputTypeName),
                "Different GRPC and Reactor API return types: %s", methodDescriptor.getFullMethodName()
        );
    }

    // Check method arguments
    if (reactorMethod.getParameterCount() == 0) {
        Preconditions.checkArgument(
                inputTypeName.equals(Empty.class.getSimpleName()),
                "Only Empty request argument allowed for Reactor methods with no parameters: %s", methodDescriptor.getFullMethodName()
        );
        return new GrpcToReactorMethodBinding<>(methodDescriptor, reactorMethod, -1, isMono, returnTypeParameter);
    }
    if (reactorMethod.getParameterCount() == 1) {
        if (reactorMethod.getParameterTypes()[0] == contextType) {
            Preconditions.checkArgument(
                    inputTypeName.equals(Empty.class.getSimpleName()),
                    "Only Empty request argument allowed for Reactor methods with no parameters: %s", methodDescriptor.getFullMethodName()
            );
            return new GrpcToReactorMethodBinding<>(methodDescriptor, reactorMethod, 0, isMono, returnTypeParameter);
        }
        Preconditions.checkArgument(
                inputTypeName.equals(reactorMethod.getParameterTypes()[0].getSimpleName()),
                "Reactor and GRPC parameter types do not match: %s", methodDescriptor.getFullMethodName()
        );
        return new GrpcToReactorMethodBinding<>(methodDescriptor, reactorMethod, -1, isMono, returnTypeParameter);
    }
    if (reactorMethod.getParameterCount() == 2) {
        Preconditions.checkArgument(
                reactorMethod.getParameterTypes()[0] == contextType || reactorMethod.getParameterTypes()[1] == contextType,
                "Expected one GRPC method argument, and one CallMetadata value in Reactor method mapped to: %s", methodDescriptor.getFullMethodName()
        );

        int callMetadataPos = reactorMethod.getParameterTypes()[0] == contextType ? 0 : 1;
        int grpcArgumentPos = callMetadataPos == 0 ? 1 : 0;
        Preconditions.checkArgument(
                inputTypeName.equals(reactorMethod.getParameterTypes()[grpcArgumentPos].getSimpleName()),
                "Reactor and GRPC parameter types do not match: %s", methodDescriptor.getFullMethodName()
        );
        return new GrpcToReactorMethodBinding<>(methodDescriptor, reactorMethod, callMetadataPos, isMono, returnTypeParameter);
    }

    throw new IllegalArgumentException("Cannot map GRPC method to any reactor method: " + methodDescriptor.getFullMethodName());
}
 
Example 10
Source File: ArmeriaChannel.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Override
public <I, O> ClientCall<I, O> newCall(
        MethodDescriptor<I, O> method, CallOptions callOptions) {
    final HttpRequestWriter req = HttpRequest.streaming(
            RequestHeaders.of(HttpMethod.POST, uri().getPath() + method.getFullMethodName(),
                              HttpHeaderNames.CONTENT_TYPE, serializationFormat.mediaType(),
                              HttpHeaderNames.TE, HttpHeaderValues.TRAILERS));
    final DefaultClientRequestContext ctx = newContext(HttpMethod.POST, req);

    final String fullMethodName = method.getFullMethodName();
    final int methodIndex = fullMethodName.lastIndexOf('/') + 1;
    ctx.logBuilder().name(method.getServiceName(), fullMethodName.substring(methodIndex));
    ctx.logBuilder().serializationFormat(serializationFormat);
    ctx.logBuilder().defer(RequestLogProperty.REQUEST_CONTENT,
                           RequestLogProperty.RESPONSE_CONTENT);

    final ClientOptions options = options();
    final int maxOutboundMessageSizeBytes = options.get(GrpcClientOptions.MAX_OUTBOUND_MESSAGE_SIZE_BYTES);
    final int maxInboundMessageSizeBytes = options.get(GrpcClientOptions.MAX_INBOUND_MESSAGE_SIZE_BYTES);
    final boolean unsafeWrapResponseBuffers = options.get(GrpcClientOptions.UNSAFE_WRAP_RESPONSE_BUFFERS);

    final PooledHttpClient client;

    final CallCredentials credentials = callOptions.getCredentials();
    if (credentials != null) {
        client = new CallCredentialsDecoratingClient(httpClient, credentials, method, authority());
    } else {
        client = httpClient;
    }

    return new ArmeriaClientCall<>(
            ctx,
            params.endpointGroup(),
            client,
            req,
            method,
            maxOutboundMessageSizeBytes,
            maxInboundMessageSizeBytes > 0 ? maxInboundMessageSizeBytes
                                           : Ints.saturatedCast(options.maxResponseLength()),
            callOptions,
            CompressorRegistry.getDefaultInstance(),
            DecompressorRegistry.getDefaultInstance(),
            serializationFormat,
            jsonMarshaller,
            unsafeWrapResponseBuffers,
            advertisedEncodingsHeader);
}
 
Example 11
Source File: OperationNameFormatUtil.java    From skywalking with Apache License 2.0 4 votes vote down vote up
public static String formatOperationName(MethodDescriptor<?, ?> methodDescriptor) {
    String fullMethodName = methodDescriptor.getFullMethodName();
    return formatServiceName(fullMethodName) + "." + formatMethodName(fullMethodName);
}
 
Example 12
Source File: OkHttpClientStream.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
OkHttpClientStream(
    MethodDescriptor<?, ?> method,
    Metadata headers,
    ExceptionHandlingFrameWriter frameWriter,
    OkHttpClientTransport transport,
    OutboundFlowController outboundFlow,
    Object lock,
    int maxMessageSize,
    int initialWindowSize,
    String authority,
    String userAgent,
    StatsTraceContext statsTraceCtx,
    TransportTracer transportTracer,
    CallOptions callOptions,
    boolean useGetForSafeMethods) {
  super(
      new OkHttpWritableBufferAllocator(),
      statsTraceCtx,
      transportTracer,
      headers,
      callOptions,
      useGetForSafeMethods && method.isSafe());
  this.statsTraceCtx = checkNotNull(statsTraceCtx, "statsTraceCtx");
  this.method = method;
  this.authority = authority;
  this.userAgent = userAgent;
  // OkHttpClientStream is only created after the transport has finished connecting,
  // so it is safe to read the transport attributes.
  // We make a copy here for convenience, even though we can ask the transport.
  this.attributes = transport.getAttributes();
  this.state =
      new TransportState(
          maxMessageSize,
          statsTraceCtx,
          lock,
          frameWriter,
          outboundFlow,
          transport,
          initialWindowSize,
          method.getFullMethodName());

  // TODO(#7168): fix a potential data race using MigratingThreadDeframer in OkHttp.
  // disabling MigratingThreadDeframer in OkHttp due to the race
  optimizeForDirectExecutor();
}
 
Example 13
Source File: GrpcParser.java    From brave with Apache License 2.0 4 votes vote down vote up
/** Returns the span name of the request. Defaults to the full grpc method name. */
protected <ReqT, RespT> String spanName(MethodDescriptor<ReqT, RespT> methodDescriptor) {
  return methodDescriptor.getFullMethodName();
}