Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[api][pulsar-client]Add get version command for pulsar rest api, pulsar-admin, pulsar-client #9975

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this change?

Copy link
Member Author

@tuteng tuteng Mar 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I found that other commands such as pulsar broker, pulsar proxy are a public class with a main function, pulsar version as a separate command, also needs to be a class with a main function, otherwise, it needs to be a subcommand of other commands


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>