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

[core,api,auth: Choose executor based on need for thread affinity #9504

Merged
merged 4 commits into from Sep 3, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
22 changes: 22 additions & 0 deletions api/src/main/java/io/grpc/InternalMayRequireSpecificExecutor.java
@@ -0,0 +1,22 @@
/*
* Copyright 2022 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
public interface InternalMayRequireSpecificExecutor {
boolean isSpecificExecutorRequired();
}
Expand Up @@ -22,6 +22,7 @@
import com.google.auth.RequestMetadataCallback;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.io.BaseEncoding;
import io.grpc.InternalMayRequireSpecificExecutor;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
import io.grpc.SecurityLevel;
Expand All @@ -44,13 +45,16 @@
/**
* Wraps {@link Credentials} as a {@link io.grpc.CallCredentials}.
*/
final class GoogleAuthLibraryCallCredentials extends io.grpc.CallCredentials {
final class GoogleAuthLibraryCallCredentials extends io.grpc.CallCredentials
implements InternalMayRequireSpecificExecutor {
private static final Logger log
= Logger.getLogger(GoogleAuthLibraryCallCredentials.class.getName());
private static final JwtHelper jwtHelper
= createJwtHelperOrNull(GoogleAuthLibraryCallCredentials.class.getClassLoader());
private static final Class<? extends Credentials> googleCredentialsClass
= loadGoogleCredentialsClass();
private static final Class<?> appEngineCredentialsClass
= loadAppEngineCredentials();

private final boolean requirePrivacy;
@VisibleForTesting
Expand All @@ -59,6 +63,8 @@ final class GoogleAuthLibraryCallCredentials extends io.grpc.CallCredentials {
private Metadata lastHeaders;
private Map<String, List<String>> lastMetadata;

private Boolean requiresSpecificExecutor;

public GoogleAuthLibraryCallCredentials(Credentials creds) {
this(creds, jwtHelper);
}
Expand Down Expand Up @@ -242,6 +248,16 @@ private static Class<? extends Credentials> loadGoogleCredentialsClass() {
return rawGoogleCredentialsClass.asSubclass(Credentials.class);
}

@Nullable
private static Class<?> loadAppEngineCredentials() {
try {
return Class.forName("com.google.auth.appengine.AppEngineCredentials");
} catch (ClassNotFoundException ex) {
log.log(Level.FINE, "AppEngineCredentials not available in classloader", ex);
return null;
}
}

private static class MethodPair {
private final Method getter;
private final Method builderSetter;
Expand Down Expand Up @@ -353,4 +369,24 @@ public Credentials tryServiceAccountToJwt(Credentials creds) {
return creds;
}
}

/**
* This method is to support the hack for AppEngineCredentials which need to run on a
* specific thread.
* @return Whether a specific executor is needed or if any executor can be used
*/
@Override
public boolean isSpecificExecutorRequired() {
// Cache the value so we only need to try to load the class once
if (requiresSpecificExecutor == null) {
if (creds == null || appEngineCredentialsClass == null) {
larry-safran marked this conversation as resolved.
Show resolved Hide resolved
requiresSpecificExecutor = Boolean.FALSE;
} else {
requiresSpecificExecutor = appEngineCredentialsClass.isInstance(creds);
}
}

return requiresSpecificExecutor;
}

}
@@ -1,5 +1,5 @@
/*
* Copyright 2016 The gRPC Authors
* Copyright 2016,2022 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.
Expand Down Expand Up @@ -27,6 +27,7 @@
import io.grpc.ChannelLogger;
import io.grpc.ClientStreamTracer;
import io.grpc.CompositeCallCredentials;
import io.grpc.InternalMayRequireSpecificExecutor;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
import io.grpc.SecurityLevel;
Expand Down Expand Up @@ -144,7 +145,21 @@ public Attributes getTransportAttrs() {
}
};
try {
creds.applyRequestMetadata(requestInfo, appExecutor, applier);
// Hack to allow appengine to work when using AppEngineCredentials (b/244209681)
// since processing must happen on a specific thread.
//
// Ideally would always use appExecutor and we could eliminate the interface
// InternalMayRequireSpecificExecutor
Executor executor;
if (creds instanceof InternalMayRequireSpecificExecutor
&& ((InternalMayRequireSpecificExecutor)creds).isSpecificExecutorRequired()
&& callOptions.getExecutor() != null) {
executor = callOptions.getExecutor();
} else {
executor = appExecutor;
}

creds.applyRequestMetadata(requestInfo, executor, applier);
} catch (Throwable t) {
applier.fail(Status.UNAUTHENTICATED
.withDescription("Credentials should use fail() instead of throwing exceptions")
Expand Down