Skip to content

Commit

Permalink
Don't use reflection for epoll/unix domain sockets
Browse files Browse the repository at this point in the history
The classes are available, even on Windows. Trying to use them though
won't work. You'll get an error like:
java.lang.UnsatisfiedLinkError: no netty-transport-native-epoll in java.library.path
  • Loading branch information
ejona86 committed May 21, 2015
1 parent 3af3d1b commit 65d73c0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 54 deletions.
9 changes: 3 additions & 6 deletions benchmarks/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,9 @@ dependencies {
project(':grpc-interop-testing'),
libraries.junit,
libraries.mockito,
libraries.hdrhistogram
if (osdetector.os == "linux") {
// These are only available on linux.
compile libraries.netty_tcnative,
libraries.netty_transport_native_epoll
}
libraries.hdrhistogram,
libraries.netty_tcnative,
libraries.netty_transport_native_epoll

alpnboot alpnboot_package_name
}
Expand Down
75 changes: 29 additions & 46 deletions benchmarks/src/main/java/io/grpc/benchmarks/qps/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@
import io.grpc.transport.netty.NettyChannelBuilder;
import io.grpc.transport.okhttp.OkHttpChannelBuilder;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.epoll.EpollDomainSocketChannel;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.epoll.EpollSocketChannel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.channel.unix.DomainSocketAddress;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslProvider;

Expand Down Expand Up @@ -79,24 +83,22 @@ static boolean parseBoolean(String value) {
static SocketAddress parseSocketAddress(String value) {
if (value.startsWith(UNIX_DOMAIN_SOCKET_PREFIX)) {
// Unix Domain Socket address.
// Create the underlying file for the Unix Domain Socket.
String filePath = value.substring(UNIX_DOMAIN_SOCKET_PREFIX.length());
File file = new File(filePath);
if (!file.isAbsolute()) {
throw new IllegalArgumentException("File path must be absolute: " + filePath);
}
try {
// Create the underlying file for the Unix Domain Socket.
String filePath = value.substring(UNIX_DOMAIN_SOCKET_PREFIX.length());
File file = new File(filePath);
if (!file.isAbsolute()) {
throw new IllegalArgumentException("File path must be absolute: " + filePath);
}
if (file.createNewFile()) {
// If this application created the file, delete it when the application exits.
file.deleteOnExit();
}
// Create the SocketAddress referencing the file.
Class<?> addressClass = Class.forName("io.netty.channel.unix.DomainSocketAddress");
return (SocketAddress) addressClass.getDeclaredConstructor(File.class)
.newInstance(file);
} catch (Exception e) {
throw new RuntimeException(e);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
// Create the SocketAddress referencing the file.
return new DomainSocketAddress(file);
} else {
// Standard TCP/IP address.
String[] parts = value.split(":", 2);
Expand Down Expand Up @@ -146,45 +148,26 @@ static Channel newClientChannel(ClientConfiguration config) throws IOException {
final EventLoopGroup group;
final Class<? extends io.netty.channel.Channel> channelType;
switch (config.transport) {
case NETTY_NIO: {
case NETTY_NIO:
group = new NioEventLoopGroup();
channelType = NioSocketChannel.class;
break;
}
case NETTY_EPOLL: {
try {
// These classes are only available on linux.
Class<?> groupClass = Class.forName("io.netty.channel.epoll.EpollEventLoopGroup");
@SuppressWarnings("unchecked")
Class<? extends io.netty.channel.Channel> channelClass =
(Class<? extends io.netty.channel.Channel>) Class.forName(
"io.netty.channel.epoll.EpollSocketChannel");
group = (EventLoopGroup) groupClass.newInstance();
channelType = channelClass;
break;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
case NETTY_UNIX_DOMAIN_SOCKET: {
try {
// These classes are only available on linux.
Class<?> groupClass = Class.forName("io.netty.channel.epoll.EpollEventLoopGroup");
@SuppressWarnings("unchecked")
Class<? extends io.netty.channel.Channel> channelClass =
(Class<? extends io.netty.channel.Channel>) Class.forName(
"io.netty.channel.epoll.EpollDomainSocketChannel");
group = (EventLoopGroup) groupClass.newInstance();
channelType = channelClass;
break;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
default: {

case NETTY_EPOLL:
// These classes only work on Linux.
group = new EpollEventLoopGroup();
channelType = EpollSocketChannel.class;
break;

case NETTY_UNIX_DOMAIN_SOCKET:
// These classes only work on Linux.
group = new EpollEventLoopGroup();
channelType = EpollDomainSocketChannel.class;
break;

default:
// Should never get here.
throw new IllegalArgumentException("Unsupported transport: " + config.transport);
}
}
return NettyChannelBuilder
.forAddress(config.address)
Expand Down
14 changes: 12 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ subprojects {
}
}

def tcnative_suffix = "";
if (osdetector.classifier in ["linux-x86_64", "osx-x86_64", "windows-x86_64"]) {
// The native code is only pre-compiled on certain platforms.
tcnative_suffix = ":" + osdetector.classifier
}
def epoll_suffix = "";
if (osdetector.classifier in ["linux-x86_64"]) {
// The native code is only pre-compiled on certain platforms.
epoll_suffix = ":" + osdetector.classifier
}
libraries = [
guava: 'com.google.guava:guava:18.0',
// used to collect benchmark results
Expand All @@ -108,8 +118,8 @@ subprojects {
protobuf_plugin: 'com.google.protobuf:protobuf-gradle-plugin:0.4.1',

netty: 'io.netty:netty-codec-http2:4.1.0.Beta5',
netty_tcnative: "io.netty:netty-tcnative:1.1.33.Fork2:${osdetector.classifier}",
netty_transport_native_epoll: "io.netty:netty-transport-native-epoll:4.1.0.Beta5:${osdetector.classifier}",
netty_tcnative: 'io.netty:netty-tcnative:1.1.33.Fork2' + tcnative_suffix,
netty_transport_native_epoll: 'io.netty:netty-transport-native-epoll:4.1.0.Beta5' + epoll_suffix,

// Test dependencies.
junit: 'junit:junit:4.11',
Expand Down

0 comments on commit 65d73c0

Please sign in to comment.