Skip to content

Commit

Permalink
Enable pulsar-perf to load WebSocket service URL from conf file (apac…
Browse files Browse the repository at this point in the history
…he#9000)

### Motivation

Currently, the command line option `--proxy-url` is required when running the subcommand `websocket-producer` of `pulsar-perf`.

```sh
$ ./bin/pulsar-perf websocket-producer

The following option is required: -u, --proxy-url
Usage: pulsar-perf websocket-producer [options] persistent://tenant/ns/my-topic
  Options:
    ...
  * -u, --proxy-url
       Pulsar Proxy URL, e.g., "ws://localhost:8080/"
```

I think it would be useful to be able to load the WebSocket service URL from the config file (such as `client.conf`) as well as other parameters.

### Modifications

`pulsar-perf` loads the WebSocket service URL from the config file if `--proxy-url` is not specified. The priorities are as follows:

1. `--proxy-url` option
2. `webSocketServiceUrl` in the config file
3. `webServiceUrl` in the config file
4. `serviceUrl` in the config file
  • Loading branch information
Masahiro Sakamoto authored Dec 20, 2020
1 parent 1966d68 commit b43e83c
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
*/
package org.apache.pulsar.proxy.socket.client;

import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
Expand Down Expand Up @@ -72,7 +75,7 @@ static class Arguments {
@Parameter(names = { "--conf-file" }, description = "Configuration file")
public String confFile;

@Parameter(names = { "-u", "--proxy-url" }, description = "Pulsar Proxy URL, e.g., \"ws://localhost:8080/\"", required = true)
@Parameter(names = { "-u", "--proxy-url" }, description = "Pulsar Proxy URL, e.g., \"ws://localhost:8080/\"")
public String proxyURL;

@Parameter(description = "persistent://tenant/ns/my-topic", required = true)
Expand Down Expand Up @@ -144,8 +147,22 @@ public Arguments loadArguments(String[] args) {
System.exit(1);
}

if (arguments.proxyURL == null) {
arguments.proxyURL = prop.getProperty("serviceUrl", "http://localhost:8080/");
if (isBlank(arguments.proxyURL)) {
String webSocketServiceUrl = prop.getProperty("webSocketServiceUrl");
if (isNotBlank(webSocketServiceUrl)) {
arguments.proxyURL = webSocketServiceUrl;
} else {
String webServiceUrl = isNotBlank(prop.getProperty("webServiceUrl"))
? prop.getProperty("webServiceUrl")
: prop.getProperty("serviceUrl");
if (isNotBlank(webServiceUrl)) {
if (webServiceUrl.startsWith("ws://") || webServiceUrl.startsWith("wss://")) {
arguments.proxyURL = webServiceUrl;
} else if (webServiceUrl.startsWith("http://") || webServiceUrl.startsWith("https://")) {
arguments.proxyURL = webServiceUrl.replaceFirst("^http", "ws");
}
}
}
}

if (arguments.authPluginClassName == null) {
Expand All @@ -157,6 +174,14 @@ public Arguments loadArguments(String[] args) {
}
}

if (isBlank(arguments.proxyURL)) {
arguments.proxyURL = "ws://localhost:8080/";
}

if (!arguments.proxyURL.endsWith("/")) {
arguments.proxyURL += "/";
}

arguments.testTime = TimeUnit.SECONDS.toMillis(arguments.testTime);

return arguments;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.pulsar.proxy.socket.client;

import static org.testng.Assert.assertEquals;

import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.List;

public class PerformanceClientTest {
@Test(timeOut = 5000)
public void testLoadArguments() throws Exception {
PerformanceClient client = new PerformanceClient();

// "--proxy-url" has the highest priority
PerformanceClient.Arguments arguments = client.loadArguments(
getArgs("ws://broker0.pulsar.apache.org:8080/", "./src/test/resources/websocket_client1.conf"));
assertEquals(arguments.proxyURL, "ws://broker0.pulsar.apache.org:8080/");

// "webSocketServiceUrl" written in the conf file has the second priority
arguments = client.loadArguments(getArgs(null, "./src/test/resources/websocket_client1.conf"));
assertEquals(arguments.proxyURL, "ws://broker1.pulsar.apache.org:8080/");

// "webServiceUrl" written in the conf file has the third priority
arguments = client.loadArguments(getArgs(null, "./src/test/resources/websocket_client2.conf"));
assertEquals(arguments.proxyURL, "ws://broker2.pulsar.apache.org:8080/");

// "serviceUrl" written in the conf file has the fourth priority
arguments = client.loadArguments(getArgs(null, "./src/test/resources/websocket_client3.conf"));
assertEquals(arguments.proxyURL, "wss://broker3.pulsar.apache.org:8443/");

// The default value is "ws://localhost:8080/"
arguments = client.loadArguments(getArgs(null, null));
assertEquals(arguments.proxyURL, "ws://localhost:8080/");

// If the URL does not end with "/", it will be added
arguments = client.loadArguments(getArgs("ws://broker0.pulsar.apache.org:8080", null));
assertEquals(arguments.proxyURL, "ws://broker0.pulsar.apache.org:8080/");
}

private String[] getArgs(String proxyUrl, String confFile) {
List<String> args = new ArrayList<>();

if (proxyUrl != null) {
args.add("--proxy-url");
args.add(proxyUrl);
}

if (confFile != null) {
args.add("--conf-file");
args.add(confFile);
}

args.add("persistent://public/default/dummy");
return args.toArray(new String[args.size()]);
}
}
22 changes: 22 additions & 0 deletions pulsar-testclient/src/test/resources/websocket_client1.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
#

webSocketServiceUrl=ws://broker1.pulsar.apache.org:8080/
webServiceUrl=http://broker2.pulsar.apache.org:8080/
serviceUrl=https://broker3.pulsar.apache.org:8443/
21 changes: 21 additions & 0 deletions pulsar-testclient/src/test/resources/websocket_client2.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
#

webServiceUrl=http://broker2.pulsar.apache.org:8080/
serviceUrl=https://broker3.pulsar.apache.org:8443/
20 changes: 20 additions & 0 deletions pulsar-testclient/src/test/resources/websocket_client3.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
#

serviceUrl=https://broker3.pulsar.apache.org:8443/

0 comments on commit b43e83c

Please sign in to comment.