Skip to content

Commit dbf7626

Browse files
BewareMyPowerlhotari
authored andcommitted
[improve][broker] Improve the performance of TopicName constructor (#24463)
(cherry picked from commit 3130a93)
1 parent 8306462 commit dbf7626

1 file changed

Lines changed: 45 additions & 52 deletions

File tree

  • pulsar-common/src/main/java/org/apache/pulsar/common/naming

pulsar-common/src/main/java/org/apache/pulsar/common/naming/TopicName.java

Lines changed: 45 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -121,77 +121,70 @@ private TopicName(String completeTopicName) {
121121
try {
122122
// The topic name can be in two different forms, one is fully qualified topic name,
123123
// the other one is short topic name
124-
if (!completeTopicName.contains("://")) {
124+
int index = completeTopicName.indexOf("://");
125+
if (index < 0) {
125126
// The short topic name can be:
126127
// - <topic>
127128
// - <property>/<namespace>/<topic>
128-
String[] parts = StringUtils.split(completeTopicName, '/');
129-
if (parts.length == 3) {
130-
completeTopicName = TopicDomain.persistent.name() + "://" + completeTopicName;
131-
} else if (parts.length == 1) {
132-
completeTopicName = TopicDomain.persistent.name() + "://"
133-
+ PUBLIC_TENANT + "/" + DEFAULT_NAMESPACE + "/" + parts[0];
129+
List<String> parts = splitBySlash(completeTopicName, 0);
130+
this.domain = TopicDomain.persistent;
131+
this.cluster = null;
132+
if (parts.size() == 3) {
133+
this.tenant = parts.get(0);
134+
this.namespacePortion = parts.get(1);
135+
this.localName = parts.get(2);
136+
} else if (parts.size() == 1) {
137+
this.tenant = PUBLIC_TENANT;
138+
this.namespacePortion = DEFAULT_NAMESPACE;
139+
this.localName = parts.get(0);
134140
} else {
135141
throw new IllegalArgumentException(
136142
"Invalid short topic name '" + completeTopicName + "', it should be in the format of "
137143
+ "<tenant>/<namespace>/<topic> or <topic>");
138144
}
139-
}
140-
141-
// The fully qualified topic name can be in two different forms:
142-
// new: persistent://tenant/namespace/topic
143-
// legacy: persistent://tenant/cluster/namespace/topic
144-
145-
List<String> parts = Splitter.on("://").limit(2).splitToList(completeTopicName);
146-
this.domain = TopicDomain.getEnum(parts.get(0));
147-
148-
String rest = parts.get(1);
149-
150-
// The rest of the name can be in different forms:
151-
// new: tenant/namespace/<localName>
152-
// legacy: tenant/cluster/namespace/<localName>
153-
// Examples of localName:
154-
// 1. some, name, xyz
155-
// 2. xyz-123, feeder-2
156-
157-
158-
parts = Splitter.on("/").limit(4).splitToList(rest);
159-
if (parts.size() == 3) {
160-
// New topic name without cluster name
161-
this.tenant = parts.get(0);
162-
this.cluster = null;
163-
this.namespacePortion = parts.get(1);
164-
this.localName = parts.get(2);
165-
this.partitionIndex = getPartitionIndex(completeTopicName);
166-
this.namespaceName = NamespaceName.get(tenant, namespacePortion);
167-
} else if (parts.size() == 4) {
168-
// Legacy topic name that includes cluster name
169-
this.tenant = parts.get(0);
170-
this.cluster = parts.get(1);
171-
this.namespacePortion = parts.get(2);
172-
this.localName = parts.get(3);
173-
this.partitionIndex = getPartitionIndex(completeTopicName);
174-
this.namespaceName = NamespaceName.get(tenant, cluster, namespacePortion);
145+
this.completeTopicName = domain.name() + "://" + tenant + "/" + namespacePortion + "/" + localName;
175146
} else {
176-
throw new IllegalArgumentException("Invalid topic name: " + completeTopicName);
147+
// The fully qualified topic name can be in two different forms:
148+
// new: persistent://tenant/namespace/topic
149+
// legacy: persistent://tenant/cluster/namespace/topic
150+
//
151+
// Examples of localName:
152+
// 1. some, name, xyz
153+
// 2. xyz-123, feeder-2
154+
List<String> parts = splitBySlash(completeTopicName.substring(index + "://".length()), 4);
155+
if (parts.size() == 3) {
156+
// New topic name without cluster name
157+
this.cluster = null;
158+
this.tenant = parts.get(0);
159+
this.namespacePortion = parts.get(1);
160+
this.localName = parts.get(2);
161+
} else if (parts.size() == 4) {
162+
// Legacy topic name that includes cluster name
163+
this.tenant = parts.get(0);
164+
this.cluster = parts.get(1);
165+
this.namespacePortion = parts.get(2);
166+
this.localName = parts.get(3);
167+
} else {
168+
throw new IllegalArgumentException("Invalid topic name " + completeTopicName);
169+
}
170+
this.completeTopicName = completeTopicName;
171+
this.domain = TopicDomain.getEnum(completeTopicName.substring(0, index));
177172
}
178173

179174
if (StringUtils.isBlank(localName)) {
180175
throw new IllegalArgumentException(String.format("Invalid topic name: %s. Topic local name must not"
181176
+ " be blank.", completeTopicName));
182177
}
183178

179+
this.partitionIndex = getPartitionIndex(localName);
180+
if (cluster == null) {
181+
this.namespaceName = NamespaceName.get(tenant, namespacePortion);
182+
} else {
183+
this.namespaceName = NamespaceName.get(tenant, cluster, namespacePortion);
184+
}
184185
} catch (NullPointerException e) {
185186
throw new IllegalArgumentException("Invalid topic name: " + completeTopicName, e);
186187
}
187-
if (isV2()) {
188-
this.completeTopicName = String.format("%s://%s/%s/%s",
189-
domain, tenant, namespacePortion, localName);
190-
} else {
191-
this.completeTopicName = String.format("%s://%s/%s/%s/%s",
192-
domain, tenant, cluster,
193-
namespacePortion, localName);
194-
}
195188
}
196189

197190
public boolean isPersistent() {

0 commit comments

Comments
 (0)