Skip to content

Commit

Permalink
Improve exception info for invaild time-related option (#12828)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuruguo authored Nov 17, 2021
1 parent bab2a81 commit 60f5475
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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 com.google.common.base.Charsets;
import io.jsonwebtoken.Claims;
Expand Down Expand Up @@ -155,8 +156,13 @@ public void run() throws Exception {

Optional<Date> optExpiryTime = Optional.empty();
if (expiryTime != null) {
long relativeTimeMillis = TimeUnit.SECONDS
.toMillis(RelativeTimeUtil.parseRelativeTimeInSeconds(expiryTime));
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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,12 @@ private class SetRetention extends CliCommand {
void run() throws PulsarAdminException {
String namespace = validateNamespace(params);
long sizeLimit = validateSizeString(limitStr);
long retentionTimeInSec = RelativeTimeUtil.parseRelativeTimeInSeconds(retentionTimeStr);
long retentionTimeInSec;
try {
retentionTimeInSec = RelativeTimeUtil.parseRelativeTimeInSeconds(retentionTimeStr);
} catch (IllegalArgumentException exception) {
throw new ParameterException(exception.getMessage());
}

final int retentionTimeInMin;
if (retentionTimeInSec != -1) {
Expand Down Expand Up @@ -1449,7 +1454,13 @@ private class SetInactiveTopicPolicies extends CliCommand {
@Override
void run() throws PulsarAdminException {
String namespace = validateNamespace(params);
long maxInactiveDurationInSeconds = TimeUnit.SECONDS.toSeconds(RelativeTimeUtil.parseRelativeTimeInSeconds(deleteInactiveTopicsMaxInactiveDuration));
long maxInactiveDurationInSeconds;
try {
maxInactiveDurationInSeconds = TimeUnit.SECONDS.toSeconds(
RelativeTimeUtil.parseRelativeTimeInSeconds(deleteInactiveTopicsMaxInactiveDuration));
} catch (IllegalArgumentException exception) {
throw new ParameterException(exception.getMessage());
}

if (enableDeleteWhileInactive == disableDeleteWhileInactive) {
throw new ParameterException("Need to specify either enable-delete-while-inactive or disable-delete-while-inactive");
Expand Down Expand Up @@ -1482,7 +1493,13 @@ private class SetDelayedDelivery extends CliCommand {
@Override
void run() throws PulsarAdminException {
String namespace = validateNamespace(params);
long delayedDeliveryTimeInMills = TimeUnit.SECONDS.toMillis(RelativeTimeUtil.parseRelativeTimeInSeconds(delayedDeliveryTimeStr));
long delayedDeliveryTimeInMills;
try {
delayedDeliveryTimeInMills = TimeUnit.SECONDS.toMillis(
RelativeTimeUtil.parseRelativeTimeInSeconds(delayedDeliveryTimeStr));
} catch (IllegalArgumentException exception) {
throw new ParameterException(exception.getMessage());
}

if (enable == disable) {
throw new ParameterException("Need to specify either --enable or --disable");
Expand Down Expand Up @@ -1864,7 +1881,13 @@ private class SetOffloadDeletionLag extends CliCommand {
@Override
void run() throws PulsarAdminException {
String namespace = validateNamespace(params);
getAdmin().namespaces().setOffloadDeleteLag(namespace, RelativeTimeUtil.parseRelativeTimeInSeconds(lag),
long lagInSec;
try {
lagInSec = RelativeTimeUtil.parseRelativeTimeInSeconds(lag);
} catch (IllegalArgumentException exception) {
throw new ParameterException(exception.getMessage());
}
getAdmin().namespaces().setOffloadDeleteLag(namespace, lagInSec,
TimeUnit.SECONDS);
}
}
Expand Down Expand Up @@ -2196,7 +2219,13 @@ && maxValueCheck("ReadBufferSize", readBufferSize, Integer.MAX_VALUE)) {

Long offloadAfterElapsedInMillis = OffloadPoliciesImpl.DEFAULT_OFFLOAD_DELETION_LAG_IN_MILLIS;
if (StringUtils.isNotEmpty(offloadAfterElapsedStr)) {
Long offloadAfterElapsed = TimeUnit.SECONDS.toMillis(RelativeTimeUtil.parseRelativeTimeInSeconds(offloadAfterElapsedStr));
Long offloadAfterElapsed;
try {
offloadAfterElapsed = TimeUnit.SECONDS.toMillis(
RelativeTimeUtil.parseRelativeTimeInSeconds(offloadAfterElapsedStr));
} catch (IllegalArgumentException exception) {
throw new ParameterException(exception.getMessage());
}
if (positiveCheck("OffloadAfterElapsed", offloadAfterElapsed)
&& maxValueCheck("OffloadAfterElapsed", offloadAfterElapsed, Long.MAX_VALUE)) {
offloadAfterElapsedInMillis = offloadAfterElapsed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

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 Down Expand Up @@ -527,8 +528,13 @@ void run() throws PulsarAdminException {
MessageId messageId = validateMessageIdString(resetMessageIdStr);
getPersistentTopics().resetCursor(persistentTopic, subName, messageId);
} else if (isNotBlank(resetTimeStr)) {
long resetTimeInMillis = TimeUnit.SECONDS
.toMillis(RelativeTimeUtil.parseRelativeTimeInSeconds(resetTimeStr));
long resetTimeInMillis;
try {
resetTimeInMillis = TimeUnit.SECONDS.toMillis(
RelativeTimeUtil.parseRelativeTimeInSeconds(resetTimeStr));
} catch (IllegalArgumentException exception) {
throw new ParameterException(exception.getMessage());
}
// now - go back time
long timestamp = System.currentTimeMillis() - resetTimeInMillis;
getPersistentTopics().resetCursor(persistentTopic, subName, timestamp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -804,8 +804,13 @@ void run() throws PulsarAdminException {
getTopics().resetCursor(persistentTopic, subName, messageId);
}
} else if (isNotBlank(resetTimeStr)) {
long resetTimeInMillis = TimeUnit.SECONDS
.toMillis(RelativeTimeUtil.parseRelativeTimeInSeconds(resetTimeStr));
long resetTimeInMillis;
try {
resetTimeInMillis = TimeUnit.SECONDS.toMillis(
RelativeTimeUtil.parseRelativeTimeInSeconds(resetTimeStr));
} catch (IllegalArgumentException exception) {
throw new ParameterException(exception.getMessage());
}
// now - go back time
long timestamp = System.currentTimeMillis() - resetTimeInMillis;
getTopics().resetCursor(persistentTopic, subName, timestamp);
Expand Down Expand Up @@ -1336,7 +1341,13 @@ private class SetDelayedDelivery extends CliCommand {
@Override
void run() throws PulsarAdminException {
String topicName = validateTopicName(params);
long delayedDeliveryTimeInMills = TimeUnit.SECONDS.toMillis(RelativeTimeUtil.parseRelativeTimeInSeconds(delayedDeliveryTimeStr));
long delayedDeliveryTimeInMills;
try {
delayedDeliveryTimeInMills = TimeUnit.SECONDS.toMillis(
RelativeTimeUtil.parseRelativeTimeInSeconds(delayedDeliveryTimeStr));
} catch (IllegalArgumentException exception) {
throw new ParameterException(exception.getMessage());
}

if (enable == disable) {
throw new ParameterException("Need to specify either --enable or --disable");
Expand Down Expand Up @@ -1486,7 +1497,12 @@ private class SetRetention extends CliCommand {
void run() throws PulsarAdminException {
String persistentTopic = validatePersistentTopic(params);
long sizeLimit = validateSizeString(limitStr);
long retentionTimeInSec = RelativeTimeUtil.parseRelativeTimeInSeconds(retentionTimeStr);
long retentionTimeInSec;
try {
retentionTimeInSec = RelativeTimeUtil.parseRelativeTimeInSeconds(retentionTimeStr);
} catch (IllegalArgumentException exception) {
throw new ParameterException(exception.getMessage());
}

final int retentionTimeInMin;
if (retentionTimeInSec != -1) {
Expand Down Expand Up @@ -2352,7 +2368,13 @@ private class SetInactiveTopicPolicies extends CliCommand {
@Override
void run() throws PulsarAdminException {
String persistentTopic = validatePersistentTopic(params);
long maxInactiveDurationInSeconds = TimeUnit.SECONDS.toSeconds(RelativeTimeUtil.parseRelativeTimeInSeconds(deleteInactiveTopicsMaxInactiveDuration));
long maxInactiveDurationInSeconds;
try {
maxInactiveDurationInSeconds = TimeUnit.SECONDS.toSeconds(
RelativeTimeUtil.parseRelativeTimeInSeconds(deleteInactiveTopicsMaxInactiveDuration));
} catch (IllegalArgumentException exception) {
throw new ParameterException(exception.getMessage());
}

if (enableDeleteWhileInactive == disableDeleteWhileInactive) {
throw new ParameterException("Need to specify either enable-delete-while-inactive or disable-delete-while-inactive");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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;
Expand Down Expand Up @@ -133,8 +134,12 @@ private class GetSlowTransactions extends CliCommand {

@Override
void run() throws Exception {
long timeout =
TimeUnit.SECONDS.toMillis(RelativeTimeUtil.parseRelativeTimeInSeconds(timeoutStr));
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));
Expand Down

0 comments on commit 60f5475

Please sign in to comment.