Skip to content

Commit

Permalink
[api][pulsar-client]Add get version command for pulsar rest api, puls…
Browse files Browse the repository at this point in the history
…ar-admin, pulsar-client (#9975)

### Motivation

Add the version command:
Get broker version by rest API:

```
curl http://localhost:8080/admin/v2/brokers/version
```
Get broker version by `pulsar-admin`:
```
./bin/pulsar-admin brokers version
```
Get broker version by the command `pulsar`:
```
./bin/pulsar version
```

Get version of pulsar admin client:
```
./bin/pulsar-admin -v
./bin/pulsra-admin --version
```

Get version of pulsar client:
```
./bin/pulsar-client -v
./bin/pulsra-client --version
  • Loading branch information
tuteng authored Mar 23, 2021
1 parent fb45a14 commit 5d85a2f
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 2 deletions.
5 changes: 4 additions & 1 deletion bin/pulsar
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ where command is one of:
zookeeper-shell Open a ZK shell client
broker-tool CLI to operate a specific broker
tokens Utility to create authentication tokens
version Get the current version of pulsar
help This help message
Expand Down Expand Up @@ -365,7 +366,9 @@ elif [ $COMMAND == "sql-worker" ]; then
check_presto_libraries
exec ${PRESTO_HOME}/bin/launcher --etc-dir ${PULSAR_PRESTO_CONF} "${@}"
elif [ $COMMAND == "tokens" ]; then
exec $JAVA $OPTS org.apache.pulsar.utils.auth.tokens.TokensCliUtils $@
exec $JAVA $OPTS org.apache.pulsar.utils.auth.tokens.TokensCliUtils $@
elif [ $COMMAND == "version" ]; then
exec $JAVA $OPTS org.apache.pulsar.PulsarVersionStarter $@
elif [ $COMMAND == "help" -o $COMMAND == "--help" -o $COMMAND == "-h" ]; then
pulsar_help;
else
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* 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;

/**
* Pulsar version entry point.
*/
public class PulsarVersionStarter {

public static void main(String args[]) {
System.out.println("Current version of pulsar is: " + PulsarVersion.getVersion());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import javax.ws.rs.container.Suspended;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.apache.pulsar.PulsarVersion;
import org.apache.pulsar.broker.PulsarService.State;
import org.apache.pulsar.broker.ServiceConfiguration;
import org.apache.pulsar.broker.loadbalance.LeaderBroker;
Expand Down Expand Up @@ -418,5 +419,15 @@ private synchronized void deleteDynamicConfigurationOnZk(String configName) {
throw new RestException(ie);
}
}

@GET
@Path("/version")
@ApiOperation(value = "Get version of current broker")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Everything is OK"),
@ApiResponse(code = 500, message = "Internal server error")})
public String version() throws Exception {
return PulsarVersion.getVersion();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,10 @@ Map<String, NamespaceOwnershipStatus> getOwnedNamespaces(String cluster, String
* Run a healthcheck on the broker asynchronously.
*/
CompletableFuture<Void> healthcheckAsync();

/**
* Get version of broker.
* @return version of broker.
*/
String getVersion() throws PulsarAdminException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -388,4 +388,31 @@ public void failed(Throwable throwable) {
});
return future;
}

@Override
public String getVersion() throws PulsarAdminException {
WebTarget path = adminBrokers.path("version");
try {
final CompletableFuture<String> future = new CompletableFuture<>();
asyncGetRequest(path, new InvocationCallback<String>() {
@Override
public void completed(String version) {
future.complete(version);
}

@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future.get(this.readTimeoutMs, TimeUnit.MILLISECONDS);
} catch (ExecutionException e) {
throw (PulsarAdminException) e.getCause();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new PulsarAdminException(e);
} catch (TimeoutException e) {
throw new PulsarAdminException.TimeoutException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ public void brokers() throws Exception {

brokers.run(split("healthcheck"));
verify(mockBrokers).healthcheck();

brokers.run(split("version"));
verify(mockBrokers).getVersion();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ void run() throws Exception {

}

@Parameters(commandDescription = "Get the version of the currently connected broker")
private class PulsarVersion extends CliCommand {

@Override
void run() throws Exception {
System.out.println(getAdmin().brokers().getVersion());
}
}

public CmdBrokers(Supplier<PulsarAdmin> admin) {
super("brokers", admin);
jcommander.addCommand("list", new List());
Expand All @@ -159,5 +168,6 @@ public CmdBrokers(Supplier<PulsarAdmin> admin) {
jcommander.addCommand("get-runtime-config", new GetRuntimeConfigCmd());
jcommander.addCommand("healthcheck", new HealthcheckCmd());
jcommander.addCommand("backlog-quota-check", new BacklogQuotaCheckCmd());
jcommander.addCommand("version", new PulsarVersion());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
import java.util.function.Function;
import java.util.function.Supplier;

import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.pulsar.PulsarVersion;
import org.apache.pulsar.client.admin.PulsarAdmin;
import org.apache.pulsar.client.admin.PulsarAdminBuilder;
import org.apache.pulsar.client.admin.internal.PulsarAdminImpl;
Expand Down Expand Up @@ -73,6 +73,9 @@ public class PulsarAdminTool {
@Parameter(names = { "--tls-enable-hostname-verification" }, description = "Enable TLS common name verification")
Boolean tlsEnableHostnameVerification;

@Parameter(names = { "-v", "--version" }, description = "Get version of pulsar admin client")
boolean version;

@Parameter(names = { "-h", "--help", }, help = true, description = "Show this help.")
boolean help;

Expand Down Expand Up @@ -245,6 +248,11 @@ boolean run(String[] args, Function<PulsarAdminBuilder, ? extends PulsarAdmin> a
return false;
}

if (version) {
System.out.println("Current version of pulsar admin client is: " + PulsarVersion.getVersion());
return true;
}

if (help) {
setupCommands(adminFactory);
jcommander.usage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;
import org.apache.pulsar.PulsarVersion;
import org.apache.pulsar.client.api.Authentication;
import org.apache.pulsar.client.api.AuthenticationFactory;
import org.apache.pulsar.client.api.ClientBuilder;
Expand Down Expand Up @@ -65,6 +66,9 @@ public class PulsarClientTool {
"or \"{\"key1\":\"val1\",\"key2\":\"val2\"}.")
String authParams = null;

@Parameter(names = { "-v", "--version" }, description = "Get version of pulsar client")
boolean version;

@Parameter(names = { "-h", "--help", }, help = true, description = "Show this help.")
boolean help;

Expand Down Expand Up @@ -156,6 +160,11 @@ public int run(String[] args) {
return -1;
}

if (version) {
System.out.println("Current version of pulsar client is: " + PulsarVersion.getVersion());
return 0;
}

if (help) {
commandParser.usage();
return 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* 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.tests.integration.cli;

import org.apache.pulsar.tests.integration.docker.ContainerExecResult;
import org.apache.pulsar.tests.integration.topologies.PulsarCluster;
import org.apache.pulsar.tests.integration.topologies.PulsarClusterSpec;
import org.testcontainers.shaded.org.apache.commons.lang.RandomStringUtils;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import static org.testng.Assert.assertTrue;

/**
* Pulsar version test class.
*/
public class PulsarVersionTest {

private final static String clusterNamePrefix = "pulsar-version";
private PulsarCluster pulsarCluster;

@BeforeClass
public void setup() throws Exception {
PulsarClusterSpec spec = PulsarClusterSpec.builder()
.clusterName(String.format("%s-%s", clusterNamePrefix, RandomStringUtils.randomAlphabetic(6)))
.build();
pulsarCluster = PulsarCluster.forSpec(spec);
pulsarCluster.start();
}

@AfterClass(alwaysRun = true)
public void teardown() {
if (pulsarCluster != null) {
pulsarCluster.stop();
pulsarCluster = null;
}
}

@Test
public void getVersion() throws Exception {
ContainerExecResult result = pulsarCluster.runAdminCommandOnAnyBroker("brokers", "version");
String version = result.getStdout();
ContainerExecResult adminVersionShortOption = pulsarCluster.runAdminCommandOnAnyBroker("-v");
assertTrue(adminVersionShortOption.getStdout().contains(version));
ContainerExecResult adminVersionLongOption = pulsarCluster.runAdminCommandOnAnyBroker("--version");
assertTrue(adminVersionLongOption.getStdout().contains(version));
ContainerExecResult clientVersionShortOption = pulsarCluster.getAnyBroker().execCmd(
PulsarCluster.CLIENT_SCRIPT, "-v");
assertTrue(clientVersionShortOption.getStdout().contains(version));
ContainerExecResult clientVersionLongOption = pulsarCluster.getAnyBroker().execCmd(
PulsarCluster.CLIENT_SCRIPT, "--version");
assertTrue(clientVersionLongOption.getStdout().contains(version));
}

}
1 change: 1 addition & 0 deletions tests/integration/src/test/resources/pulsar-cli.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<class name="org.apache.pulsar.tests.integration.cli.AdminMultiHostTest"/>
<class name="org.apache.pulsar.tests.integration.cli.FunctionsCLITest"/>
<class name="org.apache.pulsar.tests.integration.cli.PackagesCliTest"/>
<class name="org.apache.pulsar.tests.integration.cli.PulsarVersionTest"/>
</classes>
</test>
</suite>

0 comments on commit 5d85a2f

Please sign in to comment.