Skip to content

Commit 7a9ceac

Browse files
authored
benchmarks: Modernize client to use target and credentials
This allows using LoadClient with xDS. The changes to SocketAddressValidator are a bit hacky, but we really don't care about cleanliness there. Eventually on client-side, we should be deleting the unix special case entirely, as we'll have a unix name resolver. Fixes #8877
1 parent f0a7132 commit 7a9ceac

File tree

7 files changed

+74
-66
lines changed

7 files changed

+74
-66
lines changed

benchmarks/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ dependencies {
2727
project(':grpc-stub'),
2828
project(':grpc-protobuf'),
2929
project(':grpc-testing'),
30+
project(path: ':grpc-xds', configuration: 'shadow'),
3031
libraries.hdrhistogram,
3132
libraries.netty_tcnative,
3233
libraries.netty_epoll,

benchmarks/src/main/java/io/grpc/benchmarks/SocketAddressValidator.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ public interface SocketAddressValidator {
3333
public boolean isValidSocketAddress(SocketAddress address) {
3434
return address instanceof InetSocketAddress;
3535
}
36+
37+
@Override
38+
public boolean isValidSocketAddress(String address) {
39+
return !address.startsWith("unix://");
40+
}
3641
};
3742

3843
/**
@@ -43,10 +48,20 @@ public boolean isValidSocketAddress(SocketAddress address) {
4348
public boolean isValidSocketAddress(SocketAddress address) {
4449
return "DomainSocketAddress".equals(address.getClass().getSimpleName());
4550
}
51+
52+
@Override
53+
public boolean isValidSocketAddress(String address) {
54+
return address.startsWith("unix://");
55+
}
4656
};
4757

4858
/**
4959
* Returns {@code true} if the given address is valid.
5060
*/
5161
boolean isValidSocketAddress(SocketAddress address);
62+
63+
/**
64+
* Returns {@code true} if the given address is valid.
65+
*/
66+
boolean isValidSocketAddress(String address);
5267
}

benchmarks/src/main/java/io/grpc/benchmarks/Transport.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package io.grpc.benchmarks;
1818

19-
import java.net.SocketAddress;
20-
2119
/**
2220
* All of the supported transports.
2321
*/
@@ -49,7 +47,7 @@ public enum Transport {
4947
*
5048
* @throws IllegalArgumentException if the given address is invalid for this transport.
5149
*/
52-
public void validateSocketAddress(SocketAddress address) {
50+
public void validateSocketAddress(String address) {
5351
if (!socketAddressValidator.isValidSocketAddress(address)) {
5452
throw new IllegalArgumentException(
5553
"Invalid address " + address + " for transport " + this);

benchmarks/src/main/java/io/grpc/benchmarks/Utils.java

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,22 @@
1818

1919
import static java.util.concurrent.ForkJoinPool.defaultForkJoinWorkerThreadFactory;
2020

21+
import com.google.common.base.Preconditions;
2122
import com.google.common.util.concurrent.UncaughtExceptionHandlers;
2223
import com.google.protobuf.ByteString;
24+
import io.grpc.ChannelCredentials;
25+
import io.grpc.InsecureChannelCredentials;
2326
import io.grpc.ManagedChannel;
2427
import io.grpc.ManagedChannelBuilder;
2528
import io.grpc.Status;
29+
import io.grpc.TlsChannelCredentials;
2630
import io.grpc.benchmarks.proto.Messages;
2731
import io.grpc.benchmarks.proto.Messages.Payload;
2832
import io.grpc.benchmarks.proto.Messages.SimpleRequest;
2933
import io.grpc.benchmarks.proto.Messages.SimpleResponse;
30-
import io.grpc.internal.testing.TestUtils;
31-
import io.grpc.netty.GrpcSslContexts;
3234
import io.grpc.netty.NettyChannelBuilder;
3335
import io.grpc.okhttp.OkHttpChannelBuilder;
34-
import io.grpc.okhttp.internal.Platform;
36+
import io.grpc.testing.TlsTesting;
3537
import io.netty.channel.epoll.EpollDomainSocketChannel;
3638
import io.netty.channel.epoll.EpollEventLoopGroup;
3739
import io.netty.channel.epoll.EpollSocketChannel;
@@ -79,15 +81,10 @@ public static boolean parseBoolean(String value) {
7981
/**
8082
* Parse a {@link SocketAddress} from the given string.
8183
*/
82-
public static SocketAddress parseSocketAddress(String value) {
84+
public static SocketAddress parseServerSocketAddress(String value) {
8385
if (value.startsWith(UNIX_DOMAIN_SOCKET_PREFIX)) {
84-
// Unix Domain Socket address.
85-
// Create the underlying file for the Unix Domain Socket.
86-
String filePath = value.substring(UNIX_DOMAIN_SOCKET_PREFIX.length());
87-
File file = new File(filePath);
88-
if (!file.isAbsolute()) {
89-
throw new IllegalArgumentException("File path must be absolute: " + filePath);
90-
}
86+
DomainSocketAddress domainAddress = parseUnixSocketAddress(value);
87+
File file = new File(domainAddress.path());
9188
try {
9289
if (file.createNewFile()) {
9390
// If this application created the file, delete it when the application exits.
@@ -96,8 +93,7 @@ public static SocketAddress parseSocketAddress(String value) {
9693
} catch (IOException ex) {
9794
throw new RuntimeException(ex);
9895
}
99-
// Create the SocketAddress referencing the file.
100-
return new DomainSocketAddress(file);
96+
return domainAddress;
10197
} else {
10298
// Standard TCP/IP address.
10399
String[] parts = value.split(":", 2);
@@ -111,37 +107,24 @@ public static SocketAddress parseSocketAddress(String value) {
111107
}
112108
}
113109

114-
private static OkHttpChannelBuilder newOkHttpClientChannel(
115-
SocketAddress address, boolean tls, boolean testca) {
116-
InetSocketAddress addr = (InetSocketAddress) address;
117-
OkHttpChannelBuilder builder =
118-
OkHttpChannelBuilder.forAddress(addr.getHostName(), addr.getPort());
119-
if (!tls) {
120-
builder.usePlaintext();
121-
} else if (testca) {
122-
try {
123-
builder.sslSocketFactory(TestUtils.newSslSocketFactoryForCa(
124-
Platform.get().getProvider(),
125-
TestUtils.loadCert("ca.pem")));
126-
} catch (Exception e) {
127-
throw new RuntimeException(e);
128-
}
110+
private static DomainSocketAddress parseUnixSocketAddress(String value) {
111+
Preconditions.checkArgument(
112+
value.startsWith(UNIX_DOMAIN_SOCKET_PREFIX),
113+
"Must start with %s: %s", UNIX_DOMAIN_SOCKET_PREFIX, value);
114+
// Unix Domain Socket address.
115+
// Create the underlying file for the Unix Domain Socket.
116+
String filePath = value.substring(UNIX_DOMAIN_SOCKET_PREFIX.length());
117+
File file = new File(filePath);
118+
if (!file.isAbsolute()) {
119+
throw new IllegalArgumentException("File path must be absolute: " + filePath);
129120
}
130-
return builder;
121+
// Create the SocketAddress referencing the file.
122+
return new DomainSocketAddress(file);
131123
}
132124

133-
private static NettyChannelBuilder newNettyClientChannel(Transport transport,
134-
SocketAddress address, boolean tls, boolean testca, int flowControlWindow)
135-
throws IOException {
136-
NettyChannelBuilder builder =
137-
NettyChannelBuilder.forAddress(address).flowControlWindow(flowControlWindow);
138-
if (!tls) {
139-
builder.usePlaintext();
140-
} else if (testca) {
141-
File cert = TestUtils.loadCert("ca.pem");
142-
builder.sslContext(GrpcSslContexts.forClient().trustManager(cert).build());
143-
}
144-
125+
private static NettyChannelBuilder configureNetty(
126+
NettyChannelBuilder builder, Transport transport, int flowControlWindow) {
127+
builder.flowControlWindow(flowControlWindow);
145128
DefaultThreadFactory tf = new DefaultThreadFactory("client-elg-", true /*daemon */);
146129
switch (transport) {
147130
case NETTY_NIO:
@@ -194,17 +177,38 @@ public ForkJoinWorkerThread newThread(ForkJoinPool pool) {
194177
/**
195178
* Create a {@link ManagedChannel} for the given parameters.
196179
*/
197-
public static ManagedChannel newClientChannel(Transport transport, SocketAddress address,
180+
public static ManagedChannel newClientChannel(Transport transport, String target,
198181
boolean tls, boolean testca, @Nullable String authorityOverride,
199182
int flowControlWindow, boolean directExecutor) {
183+
ChannelCredentials credentials;
184+
if (tls) {
185+
if (testca) {
186+
try {
187+
credentials = TlsChannelCredentials.newBuilder()
188+
.trustManager(TlsTesting.loadCert("ca.pem"))
189+
.build();
190+
} catch (IOException ex) {
191+
throw new RuntimeException(ex);
192+
}
193+
} else {
194+
credentials = TlsChannelCredentials.create();
195+
}
196+
} else {
197+
credentials = InsecureChannelCredentials.create();
198+
}
200199
ManagedChannelBuilder<?> builder;
201200
if (transport == Transport.OK_HTTP) {
202-
builder = newOkHttpClientChannel(address, tls, testca);
201+
builder = OkHttpChannelBuilder.forTarget(target, credentials)
202+
.flowControlWindow(flowControlWindow);
203203
} else {
204-
try {
205-
builder = newNettyClientChannel(transport, address, tls, testca, flowControlWindow);
206-
} catch (Exception e) {
207-
throw new RuntimeException(e);
204+
if (target.startsWith(UNIX_DOMAIN_SOCKET_PREFIX)) {
205+
builder = configureNetty(
206+
NettyChannelBuilder.forAddress(parseUnixSocketAddress(target), credentials),
207+
transport, flowControlWindow);
208+
} else {
209+
builder = configureNetty(
210+
NettyChannelBuilder.forTarget(target, credentials),
211+
transport, flowControlWindow);
208212
}
209213
}
210214
if (authorityOverride != null) {

benchmarks/src/main/java/io/grpc/benchmarks/driver/LoadClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class LoadClient {
8282
channels[i] =
8383
Utils.newClientChannel(
8484
Epoll.isAvailable() ? Transport.NETTY_EPOLL : Transport.NETTY_NIO,
85-
Utils.parseSocketAddress(config.getServerTargets(i % config.getServerTargetsCount())),
85+
config.getServerTargets(i % config.getServerTargetsCount()),
8686
config.hasSecurityParams(),
8787
config.hasSecurityParams() && config.getSecurityParams().getUseTestCa(),
8888
config.hasSecurityParams()

benchmarks/src/main/java/io/grpc/benchmarks/qps/ClientConfiguration.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
import io.grpc.benchmarks.proto.Messages.PayloadType;
2929
import io.grpc.internal.testing.TestUtils;
3030
import java.io.IOException;
31-
import java.net.InetSocketAddress;
32-
import java.net.SocketAddress;
3331
import java.util.Collection;
3432
import java.util.Collections;
3533
import java.util.LinkedHashSet;
@@ -47,7 +45,7 @@ public class ClientConfiguration implements Configuration {
4745
String authorityOverride = TestUtils.TEST_SERVER_HOST;
4846
boolean useDefaultCiphers;
4947
boolean directExecutor;
50-
SocketAddress address;
48+
String target;
5149
int channels = 4;
5250
int outstandingRpcsPerChannel = 10;
5351
int serverPayload;
@@ -66,7 +64,7 @@ private ClientConfiguration() {
6664
}
6765

6866
public ManagedChannel newChannel() throws IOException {
69-
return Utils.newClientChannel(transport, address, tls, testca, authorityOverride,
67+
return Utils.newClientChannel(transport, target, tls, testca, authorityOverride,
7068
flowControlWindow, directExecutor);
7169
}
7270

@@ -106,18 +104,10 @@ protected ClientConfiguration build0(ClientConfiguration config) {
106104
throw new IllegalArgumentException(
107105
"Transport " + config.transport.name().toLowerCase() + " does not support TLS.");
108106
}
109-
110-
if (config.transport != Transport.OK_HTTP
111-
&& config.testca && config.address instanceof InetSocketAddress) {
112-
// Override the socket address with the host from the testca.
113-
InetSocketAddress address = (InetSocketAddress) config.address;
114-
config.address = TestUtils.testServerAddress(address.getHostName(),
115-
address.getPort());
116-
}
117107
}
118108

119109
// Verify that the address type is correct for the transport type.
120-
config.transport.validateSocketAddress(config.address);
110+
config.transport.validateSocketAddress(config.target);
121111

122112
return config;
123113
}
@@ -136,7 +126,7 @@ enum ClientParam implements AbstractConfigurationBuilder.Param {
136126
+ "(unix:///path/to/file), depending on the transport selected.", null, true) {
137127
@Override
138128
protected void setClientValue(ClientConfiguration config, String value) {
139-
config.address = Utils.parseSocketAddress(value);
129+
config.target = value;
140130
}
141131
},
142132
CHANNELS("INT", "Number of Channels.", "" + DEFAULT.channels) {

benchmarks/src/main/java/io/grpc/benchmarks/qps/ServerConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ enum ServerParam implements AbstractConfigurationBuilder.Param {
142142
+ "(unix:///path/to/file), depending on the transport selected.", null, true) {
143143
@Override
144144
protected void setServerValue(ServerConfiguration config, String value) {
145-
SocketAddress address = Utils.parseSocketAddress(value);
145+
SocketAddress address = Utils.parseServerSocketAddress(value);
146146
if (address instanceof InetSocketAddress) {
147147
InetSocketAddress addr = (InetSocketAddress) address;
148148
int port = addr.getPort() == 0 ? Utils.pickUnusedPort() : addr.getPort();

0 commit comments

Comments
 (0)