Search in sources :

Example 1 with Metadata

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.Metadata in project core-java by SpineEventEngine.

the class MetadataConverter method toMetadata.

/**
     * Returns the {@link Metadata}, containing the {@link Error} as a byte array.
     *
     * @param error the error to convert
     * @return the metadata containing error
     */
public static Metadata toMetadata(Error error) {
    checkNotNull(error);
    final Metadata metadata = new Metadata();
    metadata.put(KEY, error.toByteArray());
    return metadata;
}
Also used : Metadata(io.grpc.Metadata)

Example 2 with Metadata

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.Metadata in project grpc-java by grpc.

the class GoogleAuthLibraryCallCredentials method applyRequestMetadata.

@Override
public void applyRequestMetadata(MethodDescriptor<?, ?> method, Attributes attrs, Executor appExecutor, final MetadataApplier applier) {
    String authority = checkNotNull(attrs.get(ATTR_AUTHORITY), "authority");
    final URI uri;
    try {
        uri = serviceUri(authority, method);
    } catch (StatusException e) {
        applier.fail(e.getStatus());
        return;
    }
    appExecutor.execute(new Runnable() {

        @Override
        public void run() {
            try {
                // Credentials is expected to manage caching internally if the metadata is fetched over
                // the network.
                //
                // TODO(zhangkun83): we don't know whether there is valid cache data. If there is, we
                // would waste a context switch by always scheduling in executor. However, we have to
                // do so because we can't risk blocking the network thread. This can be resolved after
                // https://github.com/google/google-auth-library-java/issues/3 is resolved.
                //
                // Some implementations may return null here.
                Map<String, List<String>> metadata = creds.getRequestMetadata(uri);
                // Re-use the headers if getRequestMetadata() returns the same map. It may return a
                // different map based on the provided URI, i.e., for JWT. However, today it does not
                // cache JWT and so we won't bother tring to save its return value based on the URI.
                Metadata headers;
                synchronized (GoogleAuthLibraryCallCredentials.this) {
                    if (lastMetadata == null || lastMetadata != metadata) {
                        lastMetadata = metadata;
                        lastHeaders = toHeaders(metadata);
                    }
                    headers = lastHeaders;
                }
                applier.apply(headers);
            } catch (Throwable e) {
                applier.fail(Status.UNAUTHENTICATED.withCause(e));
            }
        }
    });
}
Also used : StatusException(io.grpc.StatusException) Metadata(io.grpc.Metadata) URI(java.net.URI) Map(java.util.Map)

Example 3 with Metadata

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.Metadata in project grpc-java by grpc.

the class StatsTraceContext method newServerContext.

/**
   * Creates a {@code StatsTraceContext} for an incoming RPC, using the StatsContext deserialized
   * from the headers.
   *
   * <p>The current time is used as the start time of the RPC.
   */
public static StatsTraceContext newServerContext(String methodName, StatsContextFactory statsFactory, Metadata headers, Supplier<Stopwatch> stopwatchSupplier) {
    Metadata.Key<StatsContext> statsHeader = createStatsHeader(statsFactory);
    StatsContext parentCtx = headers.get(statsHeader);
    if (parentCtx == null) {
        parentCtx = statsFactory.getDefault();
    }
    return new StatsTraceContext(Side.SERVER, methodName, parentCtx, stopwatchSupplier, statsHeader);
}
Also used : StatsContext(com.google.instrumentation.stats.StatsContext) Metadata(io.grpc.Metadata)

Example 4 with Metadata

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.Metadata in project grpc-java by grpc.

the class AbstractClientStream method inboundDataReceived.

/**
   * Processes the contents of a received data frame from the server.
   *
   * @param frame the received data frame. Its ownership is transferred to this method.
   */
protected void inboundDataReceived(ReadableBuffer frame) {
    Preconditions.checkNotNull(frame, "frame");
    boolean needToCloseFrame = true;
    try {
        if (inboundPhase() == Phase.STATUS) {
            return;
        }
        if (inboundPhase() == Phase.HEADERS) {
            // Have not received headers yet so error
            inboundTransportError(Status.INTERNAL.withDescription("headers not received before payload"), new Metadata());
            return;
        }
        inboundPhase(Phase.MESSAGE);
        needToCloseFrame = false;
        deframe(frame, false);
    } finally {
        if (needToCloseFrame) {
            frame.close();
        }
    }
}
Also used : Metadata(io.grpc.Metadata)

Example 5 with Metadata

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.Metadata in project grpc-java by grpc.

the class ClientCallImplTest method contextDeadlineShouldBePropagatedInMetadata.

@Test
public void contextDeadlineShouldBePropagatedInMetadata() {
    long deadlineNanos = TimeUnit.SECONDS.toNanos(1);
    Context context = Context.current().withDeadlineAfter(deadlineNanos, TimeUnit.NANOSECONDS, deadlineCancellationExecutor);
    context.attach();
    ClientCallImpl<Void, Void> call = new ClientCallImpl<Void, Void>(method, MoreExecutors.directExecutor(), CallOptions.DEFAULT, statsTraceCtx, provider, deadlineCancellationExecutor);
    Metadata headers = new Metadata();
    call.start(callListener, headers);
    assertTrue(headers.containsKey(GrpcUtil.TIMEOUT_KEY));
    Long timeout = headers.get(GrpcUtil.TIMEOUT_KEY);
    assertNotNull(timeout);
    long deltaNanos = TimeUnit.MILLISECONDS.toNanos(400);
    assertTimeoutBetween(timeout, deadlineNanos - deltaNanos, deadlineNanos);
}
Also used : Context(io.grpc.Context) StatsContext(com.google.instrumentation.stats.StatsContext) Metadata(io.grpc.Metadata) Test(org.junit.Test)

Aggregations

Metadata (io.grpc.Metadata)701 Test (org.junit.Test)559 Status (io.grpc.Status)190 CallOptions (io.grpc.CallOptions)56 ClientStreamTracer (io.grpc.ClientStreamTracer)51 ServerCall (io.grpc.ServerCall)48 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)44 InOrder (org.mockito.InOrder)41 Subchannel (io.grpc.LoadBalancer.Subchannel)40 ByteArrayInputStream (java.io.ByteArrayInputStream)40 InputStream (java.io.InputStream)38 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)33 IOException (java.io.IOException)32 AtomicReference (java.util.concurrent.atomic.AtomicReference)32 Context (io.grpc.Context)31 MockClientTransportInfo (io.grpc.internal.TestUtils.MockClientTransportInfo)31 InternalMetadata (io.grpc.InternalMetadata)30 MethodDescriptor (io.grpc.MethodDescriptor)30 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)29 ManagedChannel (io.grpc.ManagedChannel)27