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

Provide an accurate error message when set autoTopicCreation #14684

Merged
merged 10 commits into from
Mar 17, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
import org.apache.pulsar.common.policies.data.SubscriptionAuthMode;
import org.apache.pulsar.common.policies.data.TenantOperation;
import org.apache.pulsar.common.policies.data.TopicHashPositions;
import org.apache.pulsar.common.policies.data.ValidateResult;
import org.apache.pulsar.common.policies.data.impl.AutoTopicCreationOverrideImpl;
import org.apache.pulsar.common.policies.data.impl.DispatchRateImpl;
import org.apache.pulsar.common.util.Codec;
Expand Down Expand Up @@ -832,9 +833,11 @@ protected void internalSetAutoTopicCreation(AsyncResponse asyncResponse,
validateNamespacePolicyOperation(namespaceName, PolicyName.AUTO_TOPIC_CREATION, PolicyOperation.WRITE);
validatePoliciesReadOnlyAccess();
if (autoTopicCreationOverride != null) {
if (!AutoTopicCreationOverrideImpl.isValidOverride(autoTopicCreationOverride)) {
ValidateResult validateResult = AutoTopicCreationOverrideImpl.validateOverride(autoTopicCreationOverride);
if (!validateResult.isSuccess()) {
throw new RestException(Status.PRECONDITION_FAILED,
"Invalid configuration for autoTopicCreationOverride");
"Invalid configuration for autoTopicCreationOverride. the detail is "
+ validateResult.getErrorInfo());
}
if (maxPartitions > 0 && autoTopicCreationOverride.getDefaultNumPartitions() > maxPartitions) {
throw new RestException(Status.NOT_ACCEPTABLE,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* 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.common.policies.data;

import lombok.Getter;

@Getter
public class ValidateResult {
private final boolean success;
private final String errorInfo;

private ValidateResult(boolean success, String errorInfo) {
this.success = success;
this.errorInfo = errorInfo;
}

public static ValidateResult fail(String errorInfo) {
return new ValidateResult(false, errorInfo);
}

public static ValidateResult success() {
return new ValidateResult(true, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import lombok.NoArgsConstructor;
import org.apache.pulsar.common.policies.data.AutoTopicCreationOverride;
import org.apache.pulsar.common.policies.data.TopicType;
import org.apache.pulsar.common.policies.data.ValidateResult;

/**
* Override of autoTopicCreation settings on a namespace level.
Expand All @@ -35,28 +36,29 @@ public final class AutoTopicCreationOverrideImpl implements AutoTopicCreationOve
private String topicType;
private Integer defaultNumPartitions;

public static boolean isValidOverride(AutoTopicCreationOverride override) {
public static ValidateResult validateOverride(AutoTopicCreationOverride override) {
if (override == null) {
return false;
return ValidateResult.fail("[AutoTopicCreationOverride] can not be null");
}
if (override.isAllowAutoTopicCreation()) {
if (!TopicType.isValidTopicType(override.getTopicType())) {
return false;
return ValidateResult.fail(String.format("Unknown topic type [%s]", override.getTopicType()));
}
if (TopicType.PARTITIONED.toString().equals(override.getTopicType())) {
if (override.getDefaultNumPartitions() == null) {
return false;
return ValidateResult.fail("[defaultNumPartitions] cannot be null when the type is partitioned.");
}
if (override.getDefaultNumPartitions() <= 0) {
return false;
return ValidateResult.fail("[defaultNumPartitions] cannot be less than 1 for partition type.");
}
} else if (TopicType.NON_PARTITIONED.toString().equals(override.getTopicType())) {
if (override.getDefaultNumPartitions() != null) {
return false;
return ValidateResult.fail("[defaultNumPartitions] is not allowed to be"
+ " set when the type is non-partition.");
}
}
}
return true;
return ValidateResult.success();
}

public static AutoTopicCreationOverrideImplBuilder builder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void testValidOverrideNonPartitioned() {
.allowAutoTopicCreation(true)
.topicType(TopicType.NON_PARTITIONED.toString())
.build();
assertTrue(AutoTopicCreationOverrideImpl.isValidOverride(override));
assertTrue(AutoTopicCreationOverrideImpl.validateOverride(override).isSuccess());
}

@Test
Expand All @@ -42,7 +42,7 @@ public void testValidOverridePartitioned() {
.topicType(TopicType.PARTITIONED.toString())
.defaultNumPartitions(2)
.build();
assertTrue(AutoTopicCreationOverrideImpl.isValidOverride(override));
assertTrue(AutoTopicCreationOverrideImpl.validateOverride(override).isSuccess());
}

@Test
Expand All @@ -51,7 +51,7 @@ public void testInvalidTopicType() {
.allowAutoTopicCreation(true)
.topicType("aaa")
.build();
assertFalse(AutoTopicCreationOverrideImpl.isValidOverride(override));
assertFalse(AutoTopicCreationOverrideImpl.validateOverride(override).isSuccess());
}

@Test
Expand All @@ -61,7 +61,7 @@ public void testNumPartitionsTooLow() {
.topicType(TopicType.PARTITIONED.toString())
.defaultNumPartitions(0)
.build();
assertFalse(AutoTopicCreationOverrideImpl.isValidOverride(override));
assertFalse(AutoTopicCreationOverrideImpl.validateOverride(override).isSuccess());
}

@Test
Expand All @@ -70,7 +70,7 @@ public void testNumPartitionsNotSet() {
.allowAutoTopicCreation(true)
.topicType(TopicType.PARTITIONED.toString())
.build();
assertFalse(AutoTopicCreationOverrideImpl.isValidOverride(override));
assertFalse(AutoTopicCreationOverrideImpl.validateOverride(override).isSuccess());
}

@Test
Expand All @@ -80,6 +80,6 @@ public void testNumPartitionsOnNonPartitioned() {
.topicType(TopicType.NON_PARTITIONED.toString())
.defaultNumPartitions(2)
.build();
assertFalse(AutoTopicCreationOverrideImpl.isValidOverride(override));
assertFalse(AutoTopicCreationOverrideImpl.validateOverride(override).isSuccess());
}
}