Skip to content

Commit

Permalink
[ResourceTag](tag) Unified tag format verification (apache#13312)
Browse files Browse the repository at this point in the history
  • Loading branch information
yangzhg authored Oct 14, 2022
1 parent 71f167a commit a2a2be2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,8 @@ public static Map<String, String> analyzeBackendTagsProperties(Map<String, Strin
continue;
}
String val = entry.getValue().replaceAll(" ", "");
tagMap.put(keyParts[1], val);
Tag tag = Tag.create(keyParts[1], val);
tagMap.put(tag.type, tag.value);
iter.remove();
}
if (tagMap.isEmpty() && defaultValue != null) {
Expand Down
15 changes: 10 additions & 5 deletions fe/fe-core/src/main/java/org/apache/doris/resource/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ public class Tag implements Writable {
public static final ImmutableSet<String> RESERVED_TAG_VALUES = ImmutableSet.of(
VALUE_FRONTEND, VALUE_BACKEND, VALUE_BROKER, VALUE_REMOTE_STORAGE, VALUE_STORE, VALUE_COMPUTATION,
VALUE_DEFAULT_CLUSTER);
private static final String TAG_REGEX = "^[a-zA-Z][a-zA-Z0-9_]{0,32}$";
private static final String TAG_TYPE_REGEX = "^[a-z][a-z0-9_]{0,32}$";
private static final String TAG_VALUE_REGEX = "^[a-zA-Z][a-zA-Z0-9_]{0,32}$";


public static final Tag DEFAULT_BACKEND_TAG;
public static final Tag INVALID_TAG;
Expand All @@ -87,13 +89,16 @@ public class Tag implements Writable {
public String value;

private Tag(String type, String val) {
this.type = type.toLowerCase();
this.value = val.toLowerCase();
this.type = type;
this.value = val;
}

public static Tag create(String type, String value) throws AnalysisException {
if (!type.matches(TAG_REGEX) || !value.matches(TAG_REGEX)) {
throw new AnalysisException("Invalid tag format: " + type + ":" + value);
if (!type.matches(TAG_TYPE_REGEX)) {
throw new AnalysisException("Invalid tag type format: " + type);
}
if (!value.matches(TAG_VALUE_REGEX)) {
throw new AnalysisException("Invalid tag value format: " + value);
}
return new Tag(type, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void testAbnormal() {

properties.clear();
properties.put(PropertyAnalyzer.PROPERTIES_REPLICATION_ALLOCATION, "tag.location.12321:1");
ExceptionChecker.expectThrowsWithMsg(AnalysisException.class, "Invalid tag format: location:12321",
ExceptionChecker.expectThrowsWithMsg(AnalysisException.class, "Invalid tag value format: 12321",
() -> PropertyAnalyzer.analyzeReplicaAllocation(properties, ""));
}

Expand Down

0 comments on commit a2a2be2

Please sign in to comment.