-
Notifications
You must be signed in to change notification settings - Fork 3.9k
netty: implement UdsNameResolver and UdsNettyChannelProvider #9113
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
bce07d9
netty: implement UdsNameResolver and UdsNettyChannelProvider
sanjaypujare e8835c6
fix UdsNameResolver to return DomainSocketAddess
sanjaypujare 449a7fc
fix build issue
sanjaypujare ed055c3
address 2nd set of review comments
sanjaypujare c53ee5b
fix tests for Mac by checking isEpollAvailable()
sanjaypujare ffc58d0
address next set of review comments
sanjaypujare dbaa1a9
fix more tests for Mac by checking isEpollAvailable()
sanjaypujare 339d067
define netty_unix_common in libraries and fix UdsNettyChannelProvider…
sanjaypujare 0b5b71e
define netty_epoll_common in libraries and use it from grpc-netty
sanjaypujare f87fd56
change the test to make it dependent on provider being available
sanjaypujare a835b31
fix bazel build
sanjaypujare File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* 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.netty; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
import com.google.common.base.Preconditions; | ||
import io.grpc.EquivalentAddressGroup; | ||
import io.grpc.NameResolver; | ||
import io.netty.channel.unix.DomainSocketAddress; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
final class UdsNameResolver extends NameResolver { | ||
private NameResolver.Listener2 listener; | ||
private final String authority; | ||
|
||
UdsNameResolver(String authority, String targetPath) { | ||
checkArgument(authority == null, "non-null authority not supported"); | ||
this.authority = targetPath; | ||
} | ||
|
||
@Override | ||
public String getServiceAuthority() { | ||
return this.authority; | ||
} | ||
|
||
@Override | ||
public void start(Listener2 listener) { | ||
Preconditions.checkState(this.listener == null, "already started"); | ||
this.listener = checkNotNull(listener, "listener"); | ||
resolve(); | ||
} | ||
|
||
@Override | ||
public void refresh() { | ||
resolve(); | ||
} | ||
|
||
private void resolve() { | ||
ResolutionResult.Builder resolutionResultBuilder = ResolutionResult.newBuilder(); | ||
List<EquivalentAddressGroup> servers = new ArrayList<>(1); | ||
servers.add(new EquivalentAddressGroup(new DomainSocketAddress(authority))); | ||
resolutionResultBuilder.setAddresses(Collections.unmodifiableList(servers)); | ||
listener.onResult(resolutionResultBuilder.build()); | ||
} | ||
|
||
@Override | ||
public void shutdown() {} | ||
} |
71 changes: 71 additions & 0 deletions
71
netty/src/main/java/io/grpc/netty/UdsNameResolverProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* 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.netty; | ||
|
||
import com.google.common.base.Preconditions; | ||
import io.grpc.Internal; | ||
import io.grpc.NameResolver; | ||
import io.grpc.NameResolverProvider; | ||
import io.netty.channel.unix.DomainSocketAddress; | ||
import java.net.SocketAddress; | ||
import java.net.URI; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
|
||
@Internal | ||
public final class UdsNameResolverProvider extends NameResolverProvider { | ||
|
||
private static final String SCHEME = "unix"; | ||
|
||
@Override | ||
public UdsNameResolver newNameResolver(URI targetUri, NameResolver.Args args) { | ||
if (SCHEME.equals(targetUri.getScheme())) { | ||
return new UdsNameResolver(targetUri.getAuthority(), getTargetPathFromUri(targetUri)); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
static String getTargetPathFromUri(URI targetUri) { | ||
Preconditions.checkArgument(SCHEME.equals(targetUri.getScheme()), "scheme must be " + SCHEME); | ||
String targetPath = targetUri.getPath(); | ||
if (targetPath == null) { | ||
targetPath = Preconditions.checkNotNull(targetUri.getSchemeSpecificPart(), "targetPath"); | ||
} | ||
return targetPath; | ||
} | ||
|
||
@Override | ||
public String getDefaultScheme() { | ||
return SCHEME; | ||
} | ||
|
||
@Override | ||
protected boolean isAvailable() { | ||
return true; | ||
} | ||
|
||
@Override | ||
protected int priority() { | ||
return 3; | ||
} | ||
|
||
@Override | ||
protected Collection<Class<? extends SocketAddress>> getProducedSocketAddressTypes() { | ||
return Collections.singleton(DomainSocketAddress.class); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
netty/src/main/java/io/grpc/netty/UdsNettyChannelProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright 2015 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.netty; | ||
|
||
import io.grpc.CallCredentials; | ||
import io.grpc.ChannelCredentials; | ||
import io.grpc.InsecureChannelCredentials; | ||
import io.grpc.Internal; | ||
import io.grpc.ManagedChannelProvider; | ||
import io.grpc.internal.SharedResourcePool; | ||
import io.netty.channel.unix.DomainSocketAddress; | ||
import java.net.SocketAddress; | ||
import java.net.URI; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
|
||
/** Provider for {@link NettyChannelBuilder} instances for UDS channels. */ | ||
@Internal | ||
public final class UdsNettyChannelProvider extends ManagedChannelProvider { | ||
|
||
@Override | ||
public boolean isAvailable() { | ||
return (Utils.EPOLL_DOMAIN_CLIENT_CHANNEL_TYPE != null); | ||
} | ||
|
||
@Override | ||
public int priority() { | ||
return 3; | ||
} | ||
|
||
@Override | ||
public NettyChannelBuilder builderForAddress(String name, int port) { | ||
throw new UnsupportedOperationException("host:port not supported"); | ||
} | ||
|
||
@Override | ||
public NettyChannelBuilder builderForTarget(String target) { | ||
ChannelCredentials creds = InsecureChannelCredentials.create(); | ||
ProtocolNegotiators.FromChannelCredentialsResult result = ProtocolNegotiators.from(creds); | ||
if (result.error != null) { | ||
throw new RuntimeException(result.error); | ||
} | ||
return getNettyChannelBuilder(target, creds, null, result.negotiator); | ||
} | ||
|
||
@Override | ||
public NewChannelBuilderResult newChannelBuilder(String target, ChannelCredentials creds) { | ||
ProtocolNegotiators.FromChannelCredentialsResult result = ProtocolNegotiators.from(creds); | ||
if (result.error != null) { | ||
return NewChannelBuilderResult.error(result.error); | ||
} | ||
return NewChannelBuilderResult.channelBuilder( | ||
getNettyChannelBuilder(target, creds, result.callCredentials, result.negotiator)); | ||
} | ||
|
||
private static NettyChannelBuilder getNettyChannelBuilder( | ||
String target, | ||
ChannelCredentials creds, | ||
CallCredentials callCredentials, | ||
ProtocolNegotiator.ClientFactory negotiator) { | ||
if (Utils.EPOLL_DOMAIN_CLIENT_CHANNEL_TYPE == null) { | ||
throw new IllegalStateException("Epoll is not available"); | ||
} | ||
String targetPath = UdsNameResolverProvider.getTargetPathFromUri(URI.create(target)); | ||
NettyChannelBuilder builder = | ||
new NettyChannelBuilder( | ||
new DomainSocketAddress(targetPath), creds, callCredentials, negotiator); | ||
builder = | ||
builder | ||
.eventLoopGroupPool( | ||
SharedResourcePool.forResource(Utils.DEFAULT_WORKER_EVENT_LOOP_GROUP)) | ||
.channelType(Utils.EPOLL_DOMAIN_CLIENT_CHANNEL_TYPE); | ||
return builder; | ||
} | ||
|
||
@Override | ||
protected Collection<Class<? extends SocketAddress>> getSupportedSocketAddressTypes() { | ||
return Collections.singleton(DomainSocketAddress.class); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
netty/src/main/resources/META-INF/services/io.grpc.ManagedChannelProvider
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
io.grpc.netty.NettyChannelProvider | ||
io.grpc.netty.UdsNettyChannelProvider |
1 change: 1 addition & 0 deletions
1
netty/src/main/resources/META-INF/services/io.grpc.NameResolverProvider
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
io.grpc.netty.UdsNameResolverProvider |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.