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, services: make ProtoReflectionService interceptor compatible #6967

Merged
Merged
Show file tree
Hide file tree
Changes from 14 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
28 changes: 0 additions & 28 deletions api/src/main/java/io/grpc/InternalNotifyOnServerBuild.java

This file was deleted.

Expand Up @@ -29,7 +29,6 @@
import io.grpc.DecompressorRegistry;
import io.grpc.HandlerRegistry;
import io.grpc.InternalChannelz;
import io.grpc.InternalNotifyOnServerBuild;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.ServerInterceptor;
Expand Down Expand Up @@ -77,7 +76,6 @@ public static ServerBuilder<?> forPort(int port) {
new InternalHandlerRegistry.Builder();
final List<ServerTransportFilter> transportFilters = new ArrayList<>();
final List<ServerInterceptor> interceptors = new ArrayList<>();
private final List<InternalNotifyOnServerBuild> notifyOnBuildList = new ArrayList<>();
private final List<ServerStreamTracer.Factory> streamTracerFactories = new ArrayList<>();
HandlerRegistry fallbackRegistry = DEFAULT_FALLBACK_REGISTRY;
ObjectPool<? extends Executor> executorPool = DEFAULT_EXECUTOR_POOL;
Expand Down Expand Up @@ -114,9 +112,6 @@ public final T addService(ServerServiceDefinition service) {

@Override
public final T addService(BindableService bindableService) {
if (bindableService instanceof InternalNotifyOnServerBuild) {
notifyOnBuildList.add((InternalNotifyOnServerBuild) bindableService);
}
return addService(checkNotNull(bindableService, "bindableService").bindService());
}

Expand Down Expand Up @@ -222,14 +217,7 @@ protected void setDeadlineTicker(Deadline.Ticker ticker) {

@Override
public final Server build() {
ServerImpl server = new ServerImpl(
this,
buildTransportServers(getTracerFactories()),
Context.ROOT);
for (InternalNotifyOnServerBuild notifyTarget : notifyOnBuildList) {
notifyTarget.notifyOnBuild(server);
}
return server;
return new ServerImpl(this, buildTransportServers(getTracerFactories()), Context.ROOT);
}

@VisibleForTesting
Expand Down
36 changes: 36 additions & 0 deletions core/src/main/java/io/grpc/internal/InternalServerAccessor.java
@@ -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.internal;

import io.grpc.Context;
import io.grpc.Internal;
import io.grpc.Server;

/**
* Internal accessor for getting the {@link Server} instance inside server RPC {@link Context}.
* This is intended for usage internal to the gRPC team, as it's unclear to us what users would
* need. If you *really* think you need to use this, please file an issue for us to discuss a
Copy link
Member

Choose a reason for hiding this comment

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

Let's drop the "really". I don't see the need for the emphasis, and no real need to scare off users from filing an issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reworded. Users should refer to the documentation at where the Context key is created (io.grpc.Server). The accessor is just for our own usage.

* public API.
*/
@Internal
public class InternalServerAccessor {
public static final Context.Key<Server> SERVER_KEY = ServerImpl.SERVER_CONTEXT_KEY;
Copy link
Member

Choose a reason for hiding this comment

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

Since this instance is being used as API and not as convenience, the instance needs to be created in io.grpc (not just copied, like here). If we shaded io.grpc.internal this reference would no longer match between implementations.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved the creation to io.grpc.Server and accessor to io.grpc.


// Prevent instantiation.
private InternalServerAccessor() {
}
}
7 changes: 6 additions & 1 deletion core/src/main/java/io/grpc/internal/ServerImpl.java
Expand Up @@ -45,6 +45,7 @@
import io.grpc.InternalLogId;
import io.grpc.InternalServerInterceptors;
import io.grpc.Metadata;
import io.grpc.Server;
import io.grpc.ServerCall;
import io.grpc.ServerCallHandler;
import io.grpc.ServerInterceptor;
Expand Down Expand Up @@ -91,6 +92,7 @@ public final class ServerImpl extends io.grpc.Server implements InternalInstrume
private static final Logger log = Logger.getLogger(ServerImpl.class.getName());
private static final ServerStreamListener NOOP_LISTENER = new NoopListener();

static final Context.Key<Server> SERVER_CONTEXT_KEY = Context.key("io.grpc.ServerImpl");
private final InternalLogId logId;
private final ObjectPool<? extends Executor> executorPool;
/** Executor for application processing. Safe to read after {@link #start()}. */
Expand Down Expand Up @@ -593,7 +595,10 @@ private Context.CancellableContext createContext(
Metadata headers, StatsTraceContext statsTraceCtx) {
Long timeoutNanos = headers.get(TIMEOUT_KEY);

Context baseContext = statsTraceCtx.serverFilterContext(rootContext);
Context baseContext =
statsTraceCtx
.serverFilterContext(rootContext)
.withValue(SERVER_CONTEXT_KEY, ServerImpl.this);

if (timeoutNanos == null) {
return baseContext.withCancellation();
Expand Down
1 change: 1 addition & 0 deletions core/src/test/java/io/grpc/internal/ServerImplTest.java
Expand Up @@ -561,6 +561,7 @@ public ServerCall.Listener<String> startCall(
Context callContext = callContextReference.get();
assertNotNull(callContext);
assertEquals("context added by tracer", SERVER_TRACER_ADDED_KEY.get(callContext));
assertEquals(server, ServerImpl.SERVER_CONTEXT_KEY.get(callContext));

streamListener.messagesAvailable(new SingleMessageProducer(STRING_MARSHALLER.stream(request)));
assertEquals(1, executor.runDueTasks());
Expand Down