• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 The gRPC Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.grpc.benchmarks.qps;
18 
19 import static io.grpc.benchmarks.Utils.parseBoolean;
20 import static java.lang.Integer.parseInt;
21 
22 import io.grpc.benchmarks.SocketAddressValidator;
23 import io.grpc.benchmarks.Utils;
24 import io.grpc.netty.NettyChannelBuilder;
25 import java.net.InetSocketAddress;
26 import java.net.SocketAddress;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.Collection;
30 import java.util.Collections;
31 import java.util.List;
32 
33 /**
34  * Configuration options for benchmark servers.
35  */
36 class ServerConfiguration implements Configuration {
37   private static final ServerConfiguration DEFAULT = new ServerConfiguration();
38 
39   Transport transport = Transport.NETTY_NIO;
40   boolean tls;
41   boolean directExecutor;
42   SocketAddress address;
43   int flowControlWindow = NettyChannelBuilder.DEFAULT_FLOW_CONTROL_WINDOW;
44 
ServerConfiguration()45   private ServerConfiguration() {
46   }
47 
newBuilder()48   static Builder newBuilder() {
49     return new Builder();
50   }
51 
52   static class Builder extends AbstractConfigurationBuilder<ServerConfiguration> {
53     private static final List<Param> PARAMS = supportedParams();
54 
Builder()55     private Builder() {
56     }
57 
58     @Override
newConfiguration()59     protected ServerConfiguration newConfiguration() {
60       return new ServerConfiguration();
61     }
62 
63     @Override
getParams()64     protected Collection<Param> getParams() {
65       return PARAMS;
66     }
67 
68     @Override
build0(ServerConfiguration config)69     protected ServerConfiguration build0(ServerConfiguration config) {
70       if (config.tls && !config.transport.tlsSupported) {
71         throw new IllegalArgumentException(
72             "TLS unsupported with the " + config.transport.name().toLowerCase() + " transport");
73       }
74 
75       // Verify that the address type is correct for the transport type.
76       config.transport.validateSocketAddress(config.address);
77       return config;
78     }
79 
supportedParams()80     private static List<Param> supportedParams() {
81       return Collections.unmodifiableList(new ArrayList<Param>(
82           Arrays.asList(ServerParam.values())));
83     }
84   }
85 
86   /**
87    * All of the supported transports.
88    */
89   public enum Transport {
90     NETTY_NIO(true, "The Netty Java NIO transport. Using this with TLS requires "
91         + "that the Java bootclasspath be configured with Jetty ALPN boot.",
92         SocketAddressValidator.INET),
93     NETTY_EPOLL(true, "The Netty native EPOLL transport. Using this with TLS requires that "
94         + "OpenSSL be installed and configured as described in "
95         + "http://netty.io/wiki/forked-tomcat-native.html. Only supported on Linux.",
96         SocketAddressValidator.INET),
97     NETTY_UNIX_DOMAIN_SOCKET(false, "The Netty Unix Domain Socket transport. This currently "
98         + "does not support TLS.",
99         SocketAddressValidator.UDS);
100 
101     private final boolean tlsSupported;
102     private final String description;
103     private final SocketAddressValidator socketAddressValidator;
104 
Transport(boolean tlsSupported, String description, SocketAddressValidator socketAddressValidator)105     Transport(boolean tlsSupported, String description,
106               SocketAddressValidator socketAddressValidator) {
107       this.tlsSupported = tlsSupported;
108       this.description = description;
109       this.socketAddressValidator = socketAddressValidator;
110     }
111 
112     /**
113      * Validates the given address for this transport.
114      *
115      * @throws IllegalArgumentException if the given address is invalid for this transport.
116      */
validateSocketAddress(SocketAddress address)117     void validateSocketAddress(SocketAddress address) {
118       if (!socketAddressValidator.isValidSocketAddress(address)) {
119         throw new IllegalArgumentException(
120             "Invalid address " + address + " for transport " + this);
121       }
122     }
123 
getDescriptionString()124     static String getDescriptionString() {
125       StringBuilder builder = new StringBuilder("Select the transport to use. Options:\n");
126       boolean first = true;
127       for (Transport transport : Transport.values()) {
128         if (!first) {
129           builder.append("\n");
130         }
131         builder.append(transport.name().toLowerCase());
132         builder.append(": ");
133         builder.append(transport.description);
134         first = false;
135       }
136       return builder.toString();
137     }
138   }
139 
140   enum ServerParam implements AbstractConfigurationBuilder.Param {
141     ADDRESS("STR", "Socket address (host:port) or Unix Domain Socket file name "
142         + "(unix:///path/to/file), depending on the transport selected.", null, true) {
143       @Override
setServerValue(ServerConfiguration config, String value)144       protected void setServerValue(ServerConfiguration config, String value) {
145         SocketAddress address = Utils.parseServerSocketAddress(value);
146         if (address instanceof InetSocketAddress) {
147           InetSocketAddress addr = (InetSocketAddress) address;
148           int port = addr.getPort() == 0 ? Utils.pickUnusedPort() : addr.getPort();
149           // Re-create the address so that the server is available on all local addresses.
150           address = new InetSocketAddress(port);
151         }
152         config.address = address;
153       }
154     },
155     TLS("", "Enable TLS.", "" + DEFAULT.tls) {
156       @Override
setServerValue(ServerConfiguration config, String value)157       protected void setServerValue(ServerConfiguration config, String value) {
158         config.tls = parseBoolean(value);
159       }
160     },
161     TRANSPORT("STR", Transport.getDescriptionString(), DEFAULT.transport.name().toLowerCase()) {
162       @Override
setServerValue(ServerConfiguration config, String value)163       protected void setServerValue(ServerConfiguration config, String value) {
164         config.transport = Transport.valueOf(value.toUpperCase());
165       }
166     },
167     DIRECTEXECUTOR("", "Don't use a threadpool for RPC calls, instead execute calls directly "
168         + "in the transport thread.", "" + DEFAULT.directExecutor) {
169       @Override
setServerValue(ServerConfiguration config, String value)170       protected void setServerValue(ServerConfiguration config, String value) {
171         config.directExecutor = parseBoolean(value);
172       }
173     },
174     FLOW_CONTROL_WINDOW("BYTES", "The HTTP/2 flow control window.",
175         "" + DEFAULT.flowControlWindow) {
176       @Override
setServerValue(ServerConfiguration config, String value)177       protected void setServerValue(ServerConfiguration config, String value) {
178         config.flowControlWindow = parseInt(value);
179       }
180     };
181 
182     private final String type;
183     private final String description;
184     private final String defaultValue;
185     private final boolean required;
186 
ServerParam(String type, String description, String defaultValue)187     ServerParam(String type, String description, String defaultValue) {
188       this(type, description, defaultValue, false);
189     }
190 
ServerParam(String type, String description, String defaultValue, boolean required)191     ServerParam(String type, String description, String defaultValue, boolean required) {
192       this.type = type;
193       this.description = description;
194       this.defaultValue = defaultValue;
195       this.required = required;
196     }
197 
198     @Override
getName()199     public String getName() {
200       return name().toLowerCase();
201     }
202 
203     @Override
getType()204     public String getType() {
205       return type;
206     }
207 
208     @Override
getDescription()209     public String getDescription() {
210       return description;
211     }
212 
213     @Override
getDefaultValue()214     public String getDefaultValue() {
215       return defaultValue;
216     }
217 
218     @Override
isRequired()219     public boolean isRequired() {
220       return required;
221     }
222 
223     @Override
setValue(Configuration config, String value)224     public void setValue(Configuration config, String value) {
225       setServerValue((ServerConfiguration) config, value);
226     }
227 
setServerValue(ServerConfiguration config, String value)228     protected abstract void setServerValue(ServerConfiguration config, String value);
229   }
230 }
231