Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api,core: Add ServerInterceptor2 interface #7746

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions api/src/main/java/io/grpc/ForwardingServerBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ public T intercept(ServerInterceptor interceptor) {
return thisT();
}

@Override
public T intercept(ServerInterceptor2 interceptor) {
delegate().intercept(interceptor);
return thisT();
}

@Override
public T addTransportFilter(ServerTransportFilter filter) {
delegate().addTransportFilter(filter);
Expand Down
25 changes: 21 additions & 4 deletions api/src/main/java/io/grpc/ServerBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,11 @@ public static ServerBuilder<?> forPort(int port) {
public abstract T addService(BindableService bindableService);

/**
* Adds a {@link ServerInterceptor} that is run for all services on the server. Interceptors
* added through this method always run before per-service interceptors added through {@link
* ServerInterceptors}. Interceptors run in the reverse order in which they are added, just as
* with consecutive calls to {@code ServerInterceptors.intercept()}.
* Adds a {@link ServerInterceptor} that is run for all services on the server. Interceptors added
* through this method always run before per-service interceptors added through {@link
* ServerInterceptors}. Interceptors run in the reverse order in which they are added (either
* through this method or {@link #intercept(ServerInterceptor2)}), just as with consecutive calls
* to {@code ServerInterceptors.intercept()}.
*
* @param interceptor the all-service interceptor
* @return this
Expand All @@ -104,6 +105,22 @@ public T intercept(ServerInterceptor interceptor) {
throw new UnsupportedOperationException();
}

/**
* Adds a {@link ServerInterceptor2} that is run for all services on the server. Interceptors
* added through this method always run before per-service interceptors added through {@link
* ServerInterceptors}. Interceptors run in the reverse order in which they are added (either
* through this method or {@link #intercept(ServerInterceptor)}), just as with consecutive calls
* to {@code ServerInterceptors.intercept()}.
*
* @param interceptor the all-service interceptor
* @return this
* @since 1.36.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/????")
public T intercept(ServerInterceptor2 interceptor) {
throw new UnsupportedOperationException();
}

/**
* Adds a {@link ServerTransportFilter}. The order of filters being added is the order they will
* be executed.
Expand Down
36 changes: 36 additions & 0 deletions api/src/main/java/io/grpc/ServerInterceptor2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2020 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 javax.annotation.concurrent.ThreadSafe;

/**
* Interface for intercepting server calls by modifying the {@link ServerMethodDefinition} after
* method lookup and before call dispatch. This provides a superset of the functionality offered by
* {@link ServerInterceptor}.
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/????")
@ThreadSafe
public interface ServerInterceptor2 {
/**
* Intercept and modify the {@link ServerMethodDefinition}.
*
* <p>The returned ServerMethodDefinition cannot be null and implementations must not throw.
*/
<ReqT, RespT> ServerMethodDefinition<ReqT, RespT> interceptMethodDefinition(
ServerMethodDefinition<ReqT, RespT> method);
}
27 changes: 27 additions & 0 deletions api/src/main/java/io/grpc/ServerInterceptors.java
Original file line number Diff line number Diff line change
Expand Up @@ -303,4 +303,31 @@ public void onMessage(WReqT message) {
}
};
}

/** Wrapper to convert ServerInterceptor to ServerInterceptor2. */
private static final class ServerInterceptorConverter implements ServerInterceptor2 {
private final ServerInterceptor wrappedInterceptor;

ServerInterceptorConverter(ServerInterceptor interceptor) {
wrappedInterceptor = interceptor;
}

@Override
public <ReqT, RespT> ServerMethodDefinition<ReqT, RespT> interceptMethodDefinition(
ServerMethodDefinition<ReqT, RespT> method) {
return method.withServerCallHandler(
InternalServerInterceptors.interceptCallHandler(
wrappedInterceptor, method.getServerCallHandler()));
}
}

/**
* Returns a {@link ServerInterceptor2} instance that wraps the given {@code ServerInterceptor}.
*
* @since 1.36.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/????")
public static ServerInterceptor2 serverInterceptorConverter(ServerInterceptor interceptor) {
return new ServerInterceptorConverter(interceptor);
}
}
42 changes: 36 additions & 6 deletions api/src/main/java/io/grpc/ServerMethodDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package io.grpc;

import java.util.Collections;
import java.util.List;

/**
* Definition of a method exposed by a {@link Server}.
*
Expand All @@ -24,11 +27,15 @@
public final class ServerMethodDefinition<ReqT, RespT> {
private final MethodDescriptor<ReqT, RespT> method;
private final ServerCallHandler<ReqT, RespT> handler;
private final List<ServerStreamTracer.Factory> tracerFactories;

private ServerMethodDefinition(MethodDescriptor<ReqT, RespT> method,
ServerCallHandler<ReqT, RespT> handler) {
private ServerMethodDefinition(
MethodDescriptor<ReqT, RespT> method,
ServerCallHandler<ReqT, RespT> handler,
List<ServerStreamTracer.Factory> tracerFactories) {
this.method = method;
this.handler = handler;
this.tracerFactories = Collections.unmodifiableList(tracerFactories);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not immutable. You need to make a copy.

List<ServerStreamTracer.Factory> l = new ArrayList<>();
def = def.withStreamTracerFactories(l);
l.add(foo);

}

/**
Expand All @@ -39,9 +46,9 @@ private ServerMethodDefinition(MethodDescriptor<ReqT, RespT> method,
* @return a new instance.
*/
public static <ReqT, RespT> ServerMethodDefinition<ReqT, RespT> create(
MethodDescriptor<ReqT, RespT> method,
ServerCallHandler<ReqT, RespT> handler) {
return new ServerMethodDefinition<>(method, handler);
MethodDescriptor<ReqT, RespT> method, ServerCallHandler<ReqT, RespT> handler) {
return new ServerMethodDefinition<>(
method, handler, Collections.<ServerStreamTracer.Factory>emptyList());
}

/** The {@code MethodDescriptor} for this method. */
Expand All @@ -62,6 +69,29 @@ public ServerCallHandler<ReqT, RespT> getServerCallHandler() {
*/
public ServerMethodDefinition<ReqT, RespT> withServerCallHandler(
ServerCallHandler<ReqT, RespT> handler) {
return new ServerMethodDefinition<>(method, handler);
return new ServerMethodDefinition<>(method, handler, tracerFactories);
}

/**
* Create a new method definition with a different list of tracer factories.
*
* @param tracerFactories to bind to a cloned instance of this
* @return a cloned instance of this with an unmodifiable view of the new tracer factories
* @since 1.36.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/????")
public ServerMethodDefinition<ReqT, RespT> withStreamTracerFactories(
List<ServerStreamTracer.Factory> tracerFactories) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an annoying API to use, as it requires getting the current list, copying it, and adding your own factory to the list, all before calling this API. How about a withAdditionalStreamTracerFactory(ServerStreamTracer.Factory) which adds to the list for you. (withStreamTracerFactory may be an okay name, but that's more for an API review meeting discussion.)

return new ServerMethodDefinition<>(method, handler, tracerFactories);
}

/**
* Returns the stream tracer factories bound to this definition.
*
* @since 1.36.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/????")
public List<ServerStreamTracer.Factory> getStreamTracerFactories() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There may be value in this being a Collection. List is fine for now, but it should be discussed in the API review meeting.

return tracerFactories;
}
}
59 changes: 53 additions & 6 deletions core/src/main/java/io/grpc/inprocess/InProcessTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -417,13 +417,47 @@ private class InProcessServerStream implements ServerStream {
@GuardedBy("this")
private int outboundSeqNo;

private final class CallStartedListener
implements StatsTraceContext.ServerCallStartedListener {
private final ArrayList<Runnable> queuedStatsEvents = new ArrayList<Runnable>();
private boolean isReady;

@Override
public void serverCallStarted() {
synchronized (this) {
for (Runnable queuedEvent : queuedStatsEvents) {
queuedEvent.run();
}
queuedStatsEvents.clear();
isReady = true;
}
}

private void queueEvent(Runnable event) {
synchronized (this) {
if (isReady) {
event.run();
} else {
queuedStatsEvents.add(event);
}
}
}
}

private final CallStartedListener serverCallStartedListener = new CallStartedListener();

InProcessServerStream(MethodDescriptor<?, ?> method, Metadata headers) {
statsTraceCtx = StatsTraceContext.newServerContext(
serverStreamTracerFactories, method.getFullMethodName(), headers);
statsTraceCtx =
StatsTraceContext.newServerContext(
serverStreamTracerFactories,
method.getFullMethodName(),
headers,
serverCallStartedListener);
}

private synchronized void setListener(ClientStreamListener listener) {
clientStreamListener = listener;
private synchronized void setListener(
ClientStreamListener statsTraceCtxServerCallStartedListener) {
clientStreamListener = statsTraceCtxServerCallStartedListener;
}

@Override
Expand Down Expand Up @@ -725,8 +759,21 @@ public synchronized void writeMessage(InputStream message) {
}
statsTraceCtx.outboundMessage(outboundSeqNo);
statsTraceCtx.outboundMessageSent(outboundSeqNo, -1, -1);
serverStream.statsTraceCtx.inboundMessage(outboundSeqNo);
serverStream.statsTraceCtx.inboundMessageRead(outboundSeqNo, -1, -1);
final int outboundSeqNumCopy = outboundSeqNo;
serverStream.serverCallStartedListener.queueEvent(
new Runnable() {
@Override
public void run() {
serverStream.statsTraceCtx.inboundMessage(outboundSeqNumCopy);
}
});
serverStream.serverCallStartedListener.queueEvent(
new Runnable() {
@Override
public void run() {
serverStream.statsTraceCtx.inboundMessageRead(outboundSeqNumCopy, -1, -1);
}
});
outboundSeqNo++;
StreamListener.MessageProducer producer = new SingleMessageProducer(message);
if (serverRequested > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.ServerInterceptor;
import io.grpc.ServerInterceptor2;
import io.grpc.ServerServiceDefinition;
import io.grpc.ServerStreamTracer;
import io.grpc.ServerTransportFilter;
Expand Down Expand Up @@ -91,6 +92,12 @@ public T intercept(ServerInterceptor interceptor) {
return thisT();
}

@Override
public T intercept(ServerInterceptor2 interceptor) {
delegate().intercept(interceptor);
return thisT();
}

@Override
public T addTransportFilter(ServerTransportFilter filter) {
delegate().addTransportFilter(filter);
Expand Down