Skip to content
This repository has been archived by the owner on Apr 1, 2024. It is now read-only.

Commit

Permalink
[improve][cli][PIP-280] Retrofit pulsar-cli-utils into `pulsar-brok…
Browse files Browse the repository at this point in the history
…er` and `pulsar-client-tools` (apache#21412)

Co-authored-by: tison <[email protected]>
  • Loading branch information
JooHyukKim and tisonkun authored Nov 14, 2023
1 parent 6115a1e commit 36d4708
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 46 deletions.
6 changes: 6 additions & 0 deletions pulsar-broker/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>pulsar-cli-utils</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>managed-ledger</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.beust.jcommander.IUsageFormatter;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterException;
import com.beust.jcommander.Parameters;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwt;
Expand All @@ -41,11 +40,10 @@
import java.security.KeyPair;
import java.util.Date;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import javax.crypto.SecretKey;
import lombok.Cleanup;
import org.apache.pulsar.broker.authentication.utils.AuthTokenUtils;
import org.apache.pulsar.common.util.RelativeTimeUtil;
import org.apache.pulsar.cli.converters.TimeUnitToSecondsConverter;
import org.apache.pulsar.docs.tools.CmdGenerateDocs;

public class TokensCliUtils {
Expand Down Expand Up @@ -120,8 +118,9 @@ public static class CommandCreateToken {
@Parameter(names = {"-e",
"--expiry-time"},
description = "Relative expiry time for the token (eg: 1h, 3d, 10y)."
+ " (m=minutes) Default: no expiration")
private String expiryTime;
+ " (m=minutes) Default: no expiration",
converter = TimeUnitToSecondsConverter.class)
private Long expiryTime = null;

@Parameter(names = {"-sk",
"--secret-key"},
Expand Down Expand Up @@ -154,17 +153,9 @@ public void run() throws Exception {
signingKey = AuthTokenUtils.decodeSecretKey(encodedKey);
}

Optional<Date> optExpiryTime = Optional.empty();
if (expiryTime != null) {
long relativeTimeMillis;
try {
relativeTimeMillis = TimeUnit.SECONDS.toMillis(
RelativeTimeUtil.parseRelativeTimeInSeconds(expiryTime));
} catch (IllegalArgumentException exception) {
throw new ParameterException(exception.getMessage());
}
optExpiryTime = Optional.of(new Date(System.currentTimeMillis() + relativeTimeMillis));
}
Optional<Date> optExpiryTime = (expiryTime == null)
? Optional.empty()
: Optional.of(new Date(System.currentTimeMillis() + expiryTime));

String token = AuthTokenUtils.createToken(signingKey, subject, optExpiryTime);
System.out.println(token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import static org.apache.commons.lang3.StringUtils.isNotBlank;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterException;
import com.beust.jcommander.Parameters;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
Expand All @@ -33,6 +32,7 @@
import java.util.concurrent.ExecutionException;
import java.util.function.Supplier;
import org.apache.pulsar.cli.converters.TimeUnitToMillisConverter;
import org.apache.pulsar.cli.converters.TimeUnitToSecondsConverter;
import org.apache.pulsar.client.admin.LongRunningProcessStatus;
import org.apache.pulsar.client.admin.PulsarAdmin;
import org.apache.pulsar.client.admin.PulsarAdminException;
Expand All @@ -42,7 +42,6 @@
import org.apache.pulsar.client.cli.NoSplitter;
import org.apache.pulsar.client.impl.BatchMessageIdImpl;
import org.apache.pulsar.client.impl.MessageIdImpl;
import org.apache.pulsar.common.util.RelativeTimeUtil;

@Parameters(commandDescription = "Operations on persistent topics. The persistent-topics "
+ "has been deprecated in favor of topics", hidden = true)
Expand Down Expand Up @@ -458,17 +457,12 @@ private class ExpireMessages extends CliCommand {
private String subName;

@Parameter(names = { "-t", "--expireTime" }, description = "Expire messages older than time in seconds "
+ "(or minutes, hours, days, weeks eg: 100m, 3h, 2d, 5w)", required = true)
private String expireTimeStr;
+ "(or minutes, hours, days, weeks eg: 100m, 3h, 2d, 5w)", required = true,
converter = TimeUnitToSecondsConverter.class)
private Long expireTimeInSeconds;

@Override
void run() throws PulsarAdminException {
long expireTimeInSeconds;
try {
expireTimeInSeconds = RelativeTimeUtil.parseRelativeTimeInSeconds(expireTimeStr);
} catch (IllegalArgumentException e) {
throw new ParameterException(e.getMessage());
}
String persistentTopic = validatePersistentTopic(params);
getPersistentTopics().expireMessages(persistentTopic, subName, expireTimeInSeconds);
}
Expand All @@ -481,17 +475,12 @@ private class ExpireMessagesForAllSubscriptions extends CliCommand {
private java.util.List<String> params;

@Parameter(names = { "-t", "--expireTime" }, description = "Expire messages older than time in seconds "
+ "(or minutes, hours, days, weeks eg: 100m, 3h, 2d, 5w)", required = true)
private String expireTimeStr;
+ "(or minutes, hours, days, weeks eg: 100m, 3h, 2d, 5w)", required = true,
converter = TimeUnitToSecondsConverter.class)
private Long expireTimeInSeconds;

@Override
void run() throws PulsarAdminException {
long expireTimeInSeconds;
try {
expireTimeInSeconds = RelativeTimeUtil.parseRelativeTimeInSeconds(expireTimeStr);
} catch (IllegalArgumentException e) {
throw new ParameterException(e.getMessage());
}
String persistentTopic = validatePersistentTopic(params);
getPersistentTopics().expireMessagesForAllSubscriptions(persistentTopic, expireTimeInSeconds);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@
package org.apache.pulsar.admin.cli;

import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterException;
import com.beust.jcommander.Parameters;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.apache.pulsar.cli.converters.TimeUnitToMillisConverter;
import org.apache.pulsar.client.admin.PulsarAdmin;
import org.apache.pulsar.client.api.transaction.TxnID;
import org.apache.pulsar.common.policies.data.TransactionCoordinatorInfo;
import org.apache.pulsar.common.util.RelativeTimeUtil;

@Parameters(commandDescription = "Operations on transactions")
public class CmdTransactions extends CmdBase {
Expand Down Expand Up @@ -144,22 +143,17 @@ private class GetSlowTransactions extends CliCommand {
private Integer coordinatorId;

@Parameter(names = { "-t", "--time" }, description = "The transaction timeout time. "
+ "(eg: 1s, 10s, 1m, 5h, 3d)", required = true)
private String timeoutStr = "1s";
+ "(eg: 1s, 10s, 1m, 5h, 3d)", required = true,
converter = TimeUnitToMillisConverter.class)
private Long timeoutInMillis = 1L;

@Override
void run() throws Exception {
long timeout;
try {
timeout = TimeUnit.SECONDS.toMillis(RelativeTimeUtil.parseRelativeTimeInSeconds(timeoutStr));
} catch (IllegalArgumentException exception) {
throw new ParameterException(exception.getMessage());
}
if (coordinatorId != null) {
print(getAdmin().transactions().getSlowTransactionsByCoordinatorId(coordinatorId,
timeout, TimeUnit.MILLISECONDS));
timeoutInMillis, TimeUnit.MILLISECONDS));
} else {
print(getAdmin().transactions().getSlowTransactions(timeout, TimeUnit.MILLISECONDS));
print(getAdmin().transactions().getSlowTransactions(timeoutInMillis, TimeUnit.MILLISECONDS));
}
}
}
Expand Down

0 comments on commit 36d4708

Please sign in to comment.