Search in sources :

Example 71 with CallOptions

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

the class BinaryLogProviderTest method wrapChannel_handler.

@Test
public void wrapChannel_handler() throws Exception {
    final List<byte[]> serializedReq = new ArrayList<>();
    final AtomicReference<ClientCall.Listener<?>> listener = new AtomicReference<>();
    Channel channel = new Channel() {

        @Override
        public <RequestT, ResponseT> ClientCall<RequestT, ResponseT> newCall(MethodDescriptor<RequestT, ResponseT> methodDescriptor, CallOptions callOptions) {
            return new NoopClientCall<RequestT, ResponseT>() {

                @Override
                public void start(ClientCall.Listener<ResponseT> responseListener, Metadata headers) {
                    listener.set(responseListener);
                }

                @Override
                public void sendMessage(RequestT message) {
                    serializedReq.add((byte[]) message);
                }
            };
        }

        @Override
        public String authority() {
            throw new UnsupportedOperationException();
        }
    };
    Channel wChannel = binlogProvider.wrapChannel(channel);
    ClientCall<String, Integer> clientCall = wChannel.newCall(method, CallOptions.DEFAULT);
    final List<Integer> observedResponse = new ArrayList<>();
    clientCall.start(new NoopClientCall.NoopClientCallListener<Integer>() {

        @Override
        public void onMessage(Integer message) {
            observedResponse.add(message);
        }
    }, new Metadata());
    String expectedRequest = "hello world";
    assertThat(binlogReq).isEmpty();
    assertThat(serializedReq).isEmpty();
    assertEquals(0, reqMarshaller.streamInvocations);
    clientCall.sendMessage(expectedRequest);
    // it is unacceptably expensive for the binlog to double parse every logged message
    assertEquals(1, reqMarshaller.streamInvocations);
    assertEquals(0, reqMarshaller.parseInvocations);
    assertThat(binlogReq).hasSize(1);
    assertThat(serializedReq).hasSize(1);
    assertEquals(expectedRequest, StringMarshaller.INSTANCE.parse(new ByteArrayInputStream(binlogReq.get(0))));
    assertEquals(expectedRequest, StringMarshaller.INSTANCE.parse(new ByteArrayInputStream(serializedReq.get(0))));
    int expectedResponse = 12345;
    assertThat(binlogResp).isEmpty();
    assertThat(observedResponse).isEmpty();
    assertEquals(0, respMarshaller.parseInvocations);
    onClientMessageHelper(listener.get(), IntegerMarshaller.INSTANCE.stream(expectedResponse));
    // it is unacceptably expensive for the binlog to double parse every logged message
    assertEquals(1, respMarshaller.parseInvocations);
    assertEquals(0, respMarshaller.streamInvocations);
    assertThat(binlogResp).hasSize(1);
    assertThat(observedResponse).hasSize(1);
    assertEquals(expectedResponse, (int) IntegerMarshaller.INSTANCE.parse(new ByteArrayInputStream(binlogResp.get(0))));
    assertEquals(expectedResponse, (int) observedResponse.get(0));
}
Also used : SimpleForwardingServerCallListener(io.grpc.ForwardingServerCallListener.SimpleForwardingServerCallListener) SimpleForwardingClientCallListener(io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener) Channel(io.grpc.Channel) ArrayList(java.util.ArrayList) Metadata(io.grpc.Metadata) AtomicReference(java.util.concurrent.atomic.AtomicReference) CallOptions(io.grpc.CallOptions) MethodDescriptor(io.grpc.MethodDescriptor) NoopClientCall(io.grpc.internal.NoopClientCall) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 72 with CallOptions

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

the class BinaryLogProviderTest method wrapChannel_methodDescriptor.

@Test
public void wrapChannel_methodDescriptor() throws Exception {
    final AtomicReference<MethodDescriptor<?, ?>> methodRef = new AtomicReference<>();
    Channel channel = new Channel() {

        @Override
        public <RequestT, ResponseT> ClientCall<RequestT, ResponseT> newCall(MethodDescriptor<RequestT, ResponseT> method, CallOptions callOptions) {
            methodRef.set(method);
            return new NoopClientCall<>();
        }

        @Override
        public String authority() {
            throw new UnsupportedOperationException();
        }
    };
    Channel wChannel = binlogProvider.wrapChannel(channel);
    ClientCall<String, Integer> unusedClientCall = wChannel.newCall(method, CallOptions.DEFAULT);
    validateWrappedMethod(methodRef.get());
}
Also used : NoopClientCall(io.grpc.internal.NoopClientCall) Channel(io.grpc.Channel) AtomicReference(java.util.concurrent.atomic.AtomicReference) CallOptions(io.grpc.CallOptions) MethodDescriptor(io.grpc.MethodDescriptor) Test(org.junit.Test)

Example 73 with CallOptions

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

the class DelayedClientTransport method reprocess.

/**
 * Use the picker to try picking a transport for every pending stream, proceed the stream if the
 * pick is successful, otherwise keep it pending.
 *
 * <p>This method may be called concurrently with {@code newStream()}, and it's safe.  All pending
 * streams will be served by the latest picker (if a same picker is given more than once, they are
 * considered different pickers) as soon as possible.
 *
 * <p>This method <strong>must not</strong> be called concurrently with itself.
 */
final void reprocess(@Nullable SubchannelPicker picker) {
    ArrayList<PendingStream> toProcess;
    synchronized (lock) {
        lastPicker = picker;
        lastPickerVersion++;
        if (picker == null || !hasPendingStreams()) {
            return;
        }
        toProcess = new ArrayList<>(pendingStreams);
    }
    ArrayList<PendingStream> toRemove = new ArrayList<>();
    for (final PendingStream stream : toProcess) {
        PickResult pickResult = picker.pickSubchannel(stream.args);
        CallOptions callOptions = stream.args.getCallOptions();
        final ClientTransport transport = GrpcUtil.getTransportFromPickResult(pickResult, callOptions.isWaitForReady());
        if (transport != null) {
            Executor executor = defaultAppExecutor;
            // we are now on transport thread, we need to offload the work to an executor.
            if (callOptions.getExecutor() != null) {
                executor = callOptions.getExecutor();
            }
            Runnable runnable = stream.createRealStream(transport);
            if (runnable != null) {
                executor.execute(runnable);
            }
            toRemove.add(stream);
        }
    // else: stay pending
    }
    synchronized (lock) {
        // - shutdown() may be called, which may turn pendingStreams into null.
        if (!hasPendingStreams()) {
            return;
        }
        pendingStreams.removeAll(toRemove);
        // hashmap.
        if (pendingStreams.isEmpty()) {
            pendingStreams = new LinkedHashSet<>();
        }
        if (!hasPendingStreams()) {
            // There may be a brief gap between delayed transport clearing in-use state, and first real
            // transport starting streams and setting in-use state.  During the gap the whole channel's
            // in-use state may be false. However, it shouldn't cause spurious switching to idleness
            // (which would shutdown the transports and LoadBalancer) because the gap should be shorter
            // than IDLE_MODE_DEFAULT_TIMEOUT_MILLIS (1 second).
            syncContext.executeLater(reportTransportNotInUse);
            if (shutdownStatus != null && reportTransportTerminated != null) {
                syncContext.executeLater(reportTransportTerminated);
                reportTransportTerminated = null;
            }
        }
    }
    syncContext.drain();
}
Also used : Executor(java.util.concurrent.Executor) ArrayList(java.util.ArrayList) PickResult(io.grpc.LoadBalancer.PickResult) CallOptions(io.grpc.CallOptions)

Example 74 with CallOptions

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

the class AbstractFutureStubTest method defaultCallOptions.

@Test
public void defaultCallOptions() {
    NoopFutureStub stub = NoopFutureStub.newStub(new StubFactory<NoopFutureStub>() {

        @Override
        public NoopFutureStub newStub(Channel channel, CallOptions callOptions) {
            return create(channel, callOptions);
        }
    }, channel, CallOptions.DEFAULT);
    assertThat(stub.getCallOptions().getOption(ClientCalls.STUB_TYPE_OPTION)).isEqualTo(StubType.FUTURE);
}
Also used : NoopFutureStub(io.grpc.stub.AbstractFutureStubTest.NoopFutureStub) Channel(io.grpc.Channel) CallOptions(io.grpc.CallOptions) Test(org.junit.Test)

Example 75 with CallOptions

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

the class BaseAbstractStubTest method withExecutor.

@Test
public void withExecutor() {
    T stub = create(channel);
    CallOptions callOptions = stub.getCallOptions();
    assertNull(callOptions.getExecutor());
    Executor executor = mock(Executor.class);
    stub = stub.withExecutor(executor);
    callOptions = stub.getCallOptions();
    assertEquals(callOptions.getExecutor(), executor);
}
Also used : Executor(java.util.concurrent.Executor) CallOptions(io.grpc.CallOptions) Test(org.junit.Test)

Aggregations

CallOptions (io.grpc.CallOptions)81 Test (org.junit.Test)61 Metadata (io.grpc.Metadata)50 Channel (io.grpc.Channel)36 MethodDescriptor (io.grpc.MethodDescriptor)28 ClientInterceptor (io.grpc.ClientInterceptor)23 ManagedChannel (io.grpc.ManagedChannel)18 ClientStreamTracer (io.grpc.ClientStreamTracer)17 ClientCall (io.grpc.ClientCall)15 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)11 Status (io.grpc.Status)11 Context (io.grpc.Context)10 Subchannel (io.grpc.LoadBalancer.Subchannel)10 MockClientTransportInfo (io.grpc.internal.TestUtils.MockClientTransportInfo)10 ForwardingSubchannel (io.grpc.util.ForwardingSubchannel)9 SimpleForwardingClientCall (io.grpc.ForwardingClientCall.SimpleForwardingClientCall)8 NoopClientCall (io.grpc.internal.NoopClientCall)8 SocketAddress (java.net.SocketAddress)8 SimpleForwardingClientCallListener (io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener)7 ByteString (com.google.protobuf.ByteString)6