move io.grpc.BinaryLogProvider to io.grpc.services (#4501)

Add internal accessors for ServerInterceptors and ClientInterceptors because some helpers were pkg private
Fix tests that were once creating BinaryLogProvider instances, they should now only create io.grpc.BinaryLog instances
This commit is contained in:
zpencer 2018-05-23 17:04:48 -07:00 committed by GitHub
parent c60580c669
commit 1f99fcdc82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 169 additions and 56 deletions

View File

@ -0,0 +1,32 @@
/*
* Copyright 2018 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.grpc;
import io.grpc.MethodDescriptor.Marshaller;
/**
* Internal accessor. Do not use.
*/
@Internal
public final class InternalClientInterceptors {
public static <WReqT, WRespT> ClientInterceptor wrapClientInterceptor(
final ClientInterceptor interceptor,
final Marshaller<WReqT> reqMarshaller,
final Marshaller<WRespT> respMarshaller) {
return ClientInterceptors.wrapClientInterceptor(interceptor, reqMarshaller, respMarshaller);
}
}

View File

@ -26,6 +26,17 @@ public final class InternalServerInterceptors {
return ServerInterceptors.InterceptCallHandler.create(interceptor, callHandler);
}
public static <OReqT, ORespT, WReqT, WRespT> ServerMethodDefinition<WReqT, WRespT> wrapMethod(
final ServerMethodDefinition<OReqT, ORespT> definition,
final MethodDescriptor<WReqT, WRespT> wrappedMethod) {
return ServerInterceptors.wrapMethod(definition, wrappedMethod);
}
public static <ReqT, RespT> ServerCallHandler<ReqT, RespT> interceptCallHandlerCreate(
ServerInterceptor interceptor, ServerCallHandler<ReqT, RespT> callHandler) {
return ServerInterceptors.InterceptCallHandler.create(interceptor, callHandler);
}
private InternalServerInterceptors() {
}
}

View File

@ -20,8 +20,8 @@ import static com.google.common.base.Preconditions.checkNotNull;
import static io.opencensus.trace.unsafe.ContextUtils.CONTEXT_SPAN_KEY;
import com.google.common.annotations.VisibleForTesting;
import io.grpc.BinaryLog;
import io.grpc.BinaryLog.CallId;
import io.grpc.BinaryLogProvider;
import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ClientCall;
@ -386,7 +386,7 @@ final class CensusTracingModule {
method,
callOptions.withStreamTracerFactory(tracerFactory)
.withOption(
BinaryLogProvider.CLIENT_CALL_ID_CALLOPTION_KEY,
BinaryLog.CLIENT_CALL_ID_CALLOPTION_KEY,
new CallId(
0,
ByteBuffer.wrap(

View File

@ -31,7 +31,6 @@ import com.google.common.base.Supplier;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.SettableFuture;
import io.grpc.Attributes;
import io.grpc.BinaryLogProvider;
import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ClientCall;
@ -140,7 +139,7 @@ final class ManagedChannelImpl extends ManagedChannel implements Instrumented<Ch
/**
* We delegate to this channel, so that we can have interceptors as necessary. If there aren't
* any interceptors and the {@link BinaryLogProvider} is {@code null} then this will just be a
* any interceptors and the {@link io.grpc.BinaryLog} is {@code null} then this will just be a
* {@link RealChannel}.
*/
private final Channel interceptorChannel;

View File

@ -44,8 +44,8 @@ import static org.mockito.Mockito.when;
import com.google.common.util.concurrent.SettableFuture;
import io.grpc.Attributes;
import io.grpc.BinaryLog;
import io.grpc.BinaryLog.CallId;
import io.grpc.BinaryLogProvider;
import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ClientCall;
@ -1000,7 +1000,7 @@ public class CensusModulesTest {
new CensusTracingModule(tracer, mock(BinaryFormat.class))
.getClientInterceptor()
.interceptCall(TestMethodDescriptors.voidMethod(), CallOptions.DEFAULT, c);
CallId callId = options.get().getOption(BinaryLogProvider.CLIENT_CALL_ID_CALLOPTION_KEY);
CallId callId = options.get().getOption(BinaryLog.CLIENT_CALL_ID_CALLOPTION_KEY);
assertThat(callId.hi).isEqualTo(0);
assertThat(callId.lo)
.isEqualTo(ByteBuffer.wrap(mockableSpan.getContext().getSpanId().getBytes()).getLong());

View File

@ -52,13 +52,14 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.util.concurrent.SettableFuture;
import io.grpc.Attributes;
import io.grpc.BinaryLogProvider;
import io.grpc.BinaryLog;
import io.grpc.CallCredentials;
import io.grpc.CallCredentials.MetadataApplier;
import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ClientCall;
import io.grpc.ClientInterceptor;
import io.grpc.ClientInterceptors;
import io.grpc.ClientStreamTracer;
import io.grpc.ConnectivityState;
import io.grpc.ConnectivityStateInfo;
@ -77,11 +78,12 @@ import io.grpc.MethodDescriptor;
import io.grpc.MethodDescriptor.MethodType;
import io.grpc.NameResolver;
import io.grpc.SecurityLevel;
import io.grpc.ServerInterceptor;
import io.grpc.ServerMethodDefinition;
import io.grpc.Status;
import io.grpc.StringMarshaller;
import io.grpc.internal.Channelz.ChannelStats;
import io.grpc.internal.TestUtils.MockClientTransportInfo;
import java.io.IOException;
import java.net.SocketAddress;
import java.net.URI;
import java.util.ArrayList;
@ -94,7 +96,6 @@ import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import javax.annotation.Nullable;
import org.junit.After;
import org.junit.Assume;
import org.junit.Before;
@ -2282,26 +2283,31 @@ public class ManagedChannelImplTest {
@Test
public void binaryLogInstalled() throws Exception {
final SettableFuture<Boolean> intercepted = SettableFuture.create();
channelBuilder.binlog = new BinaryLogProvider() {
@Nullable
channelBuilder.binlog = new BinaryLog() {
@Override
public ServerInterceptor getServerInterceptor(String fullMethodName) {
return null;
public void close() throws IOException {
// noop
}
@Override
public ClientInterceptor getClientInterceptor(
String fullMethodName, CallOptions callOptions) {
return new ClientInterceptor() {
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
MethodDescriptor<ReqT, RespT> method,
CallOptions callOptions,
Channel next) {
intercepted.set(true);
return next.newCall(method, callOptions);
}
};
public <ReqT, RespT> ServerMethodDefinition<?, ?> wrapMethodDefinition(
ServerMethodDefinition<ReqT, RespT> oMethodDef) {
return oMethodDef;
}
@Override
public Channel wrapChannel(Channel channel) {
return ClientInterceptors.intercept(channel,
new ClientInterceptor() {
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
MethodDescriptor<ReqT, RespT> method,
CallOptions callOptions,
Channel next) {
intercepted.set(true);
return next.newCall(method, callOptions);
}
});
}
};

View File

@ -48,14 +48,14 @@ import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.SettableFuture;
import io.grpc.Attributes;
import io.grpc.BinaryLogProvider;
import io.grpc.CallOptions;
import io.grpc.ClientInterceptor;
import io.grpc.BinaryLog;
import io.grpc.Channel;
import io.grpc.Compressor;
import io.grpc.Context;
import io.grpc.Grpc;
import io.grpc.HandlerRegistry;
import io.grpc.IntegerMarshaller;
import io.grpc.InternalServerInterceptors;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
import io.grpc.ServerCall;
@ -1237,26 +1237,35 @@ public class ServerImplTest {
@Test
public void binaryLogInstalled() throws Exception {
final SettableFuture<Boolean> intercepted = SettableFuture.create();
builder.binlog = new BinaryLogProvider() {
@Nullable
final ServerInterceptor interceptor = new ServerInterceptor() {
@Override
public ServerInterceptor getServerInterceptor(String fullMethodName) {
return new ServerInterceptor() {
@Override
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call,
Metadata headers,
ServerCallHandler<ReqT, RespT> next) {
intercepted.set(true);
return next.startCall(call, headers);
}
};
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call,
Metadata headers,
ServerCallHandler<ReqT, RespT> next) {
intercepted.set(true);
return next.startCall(call, headers);
}
};
builder.binlog = new BinaryLog() {
@Override
public void close() throws IOException {
// noop
}
@Nullable
@Override
public ClientInterceptor getClientInterceptor(
String fullMethodName, CallOptions callOptions) {
return null;
public <ReqT, RespT> ServerMethodDefinition<?, ?> wrapMethodDefinition(
ServerMethodDefinition<ReqT, RespT> oMethodDef) {
return ServerMethodDefinition.create(
oMethodDef.getMethodDescriptor(),
InternalServerInterceptors.interceptCallHandlerCreate(
interceptor,
oMethodDef.getServerCallHandler()));
}
@Override
public Channel wrapChannel(Channel channel) {
return channel;
}
};
createAndStartServer();

View File

@ -14,11 +14,25 @@
* limitations under the License.
*/
package io.grpc;
package io.grpc.services;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import io.grpc.BinaryLog;
import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ClientCall;
import io.grpc.ClientInterceptor;
import io.grpc.ClientInterceptors;
import io.grpc.Internal;
import io.grpc.InternalClientInterceptors;
import io.grpc.InternalServerInterceptors;
import io.grpc.ManagedChannel;
import io.grpc.MethodDescriptor;
import io.grpc.MethodDescriptor.Marshaller;
import io.grpc.ServerCallHandler;
import io.grpc.ServerInterceptor;
import io.grpc.ServerMethodDefinition;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@ -61,9 +75,9 @@ public abstract class BinaryLogProvider extends BinaryLog {
MethodDescriptor<byte[], byte[]> binMethod =
BinaryLogProvider.toByteBufferMethod(oMethodDef.getMethodDescriptor());
ServerMethodDefinition<byte[], byte[]> binDef =
ServerInterceptors.wrapMethod(oMethodDef, binMethod);
InternalServerInterceptors.wrapMethod(oMethodDef, binMethod);
ServerCallHandler<byte[], byte[]> binlogHandler =
ServerInterceptors.InterceptCallHandler.create(
InternalServerInterceptors.interceptCallHandlerCreate(
binlogInterceptor, binDef.getServerCallHandler());
return ServerMethodDefinition.create(binMethod, binlogHandler);
}
@ -137,7 +151,7 @@ public abstract class BinaryLogProvider extends BinaryLog {
if (binlogInterceptor == null) {
return next.newCall(method, callOptions);
} else {
return ClientInterceptors
return InternalClientInterceptors
.wrapClientInterceptor(
binlogInterceptor,
BYTEARRAY_MARSHALLER,

View File

@ -17,7 +17,6 @@
package io.grpc.services;
import com.google.common.base.Preconditions;
import io.grpc.BinaryLogProvider;
import io.grpc.CallOptions;
import io.grpc.ClientInterceptor;
import io.grpc.ServerInterceptor;

View File

@ -16,7 +16,7 @@
package io.grpc.services;
import static io.grpc.BinaryLogProvider.BYTEARRAY_MARSHALLER;
import static io.grpc.services.BinaryLogProvider.BYTEARRAY_MARSHALLER;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;

View File

@ -16,7 +16,6 @@
package io.grpc.services;
import io.grpc.BinaryLogProvider;
import io.grpc.CallOptions;
import io.opencensus.trace.Span;
import io.opencensus.trace.Tracing;

View File

@ -14,19 +14,32 @@
* limitations under the License.
*/
package io.grpc;
package io.grpc.services;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import com.google.common.io.ByteStreams;
import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ClientCall;
import io.grpc.ClientInterceptor;
import io.grpc.ForwardingClientCall.SimpleForwardingClientCall;
import io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener;
import io.grpc.ForwardingServerCall.SimpleForwardingServerCall;
import io.grpc.ForwardingServerCallListener.SimpleForwardingServerCallListener;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
import io.grpc.MethodDescriptor.Marshaller;
import io.grpc.MethodDescriptor.MethodType;
import io.grpc.ServerCall;
import io.grpc.ServerCall.Listener;
import io.grpc.ServerCallHandler;
import io.grpc.ServerInterceptor;
import io.grpc.ServerMethodDefinition;
import io.grpc.internal.IoUtils;
import io.grpc.internal.NoopClientCall;
import io.grpc.internal.NoopServerCall;
@ -199,7 +212,7 @@ public class BinaryLogProviderTest {
method,
new ServerCallHandler<String, Integer>() {
@Override
public ServerCall.Listener<String> startCall(
public Listener<String> startCall(
ServerCall<String, Integer> call, Metadata headers) {
throw new UnsupportedOperationException();
}
@ -390,4 +403,37 @@ public class BinaryLogProviderTest {
return delegate().parse(stream);
}
}
private static class StringMarshaller implements MethodDescriptor.Marshaller<String> {
public static StringMarshaller INSTANCE = new StringMarshaller();
@Override
public InputStream stream(String value) {
return new ByteArrayInputStream(value.getBytes(UTF_8));
}
@Override
public String parse(InputStream stream) {
try {
return new String(ByteStreams.toByteArray(stream), UTF_8);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}
private static class IntegerMarshaller implements MethodDescriptor.Marshaller<Integer> {
public static final IntegerMarshaller INSTANCE = new IntegerMarshaller();
@Override
public InputStream stream(Integer value) {
return StringMarshaller.INSTANCE.stream(value.toString());
}
@Override
public Integer parse(InputStream stream) {
return Integer.valueOf(StringMarshaller.INSTANCE.parse(stream));
}
}
}

View File

@ -16,7 +16,7 @@
package io.grpc.services;
import static io.grpc.BinaryLogProvider.BYTEARRAY_MARSHALLER;
import static io.grpc.services.BinaryLogProvider.BYTEARRAY_MARSHALLER;
import static io.grpc.services.BinlogHelper.DUMMY_SOCKET;
import static io.grpc.services.BinlogHelper.getPeerSocket;
import static org.junit.Assert.assertEquals;
@ -31,7 +31,6 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
import com.google.protobuf.ByteString;
import io.grpc.Attributes;
import io.grpc.BinaryLog.CallId;
import io.grpc.BinaryLogProvider;
import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ClientCall;

View File

@ -20,7 +20,6 @@ import static com.google.common.truth.Truth.assertThat;
import static io.opencensus.trace.unsafe.ContextUtils.CONTEXT_SPAN_KEY;
import io.grpc.BinaryLog.CallId;
import io.grpc.BinaryLogProvider;
import io.grpc.CallOptions;
import io.grpc.Context;
import io.grpc.internal.testing.StatsTestUtils.MockableSpan;