-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[fix][proxy] Fix proxy OOM by replacing TopicName with a simple conversion method #24465
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
Open
BewareMyPower
wants to merge
5
commits into
apache:master
Choose a base branch
from
BewareMyPower:bewaremypower/proxy-remove-topic-name
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+133
−15
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
933bcac
[fix][proxy] Fix proxy OOM by using a topic cache per channel
BewareMyPower 2018066
Avoid splitting the topic when it starts with ://
BewareMyPower 71ca674
Fix incorrect implementation
BewareMyPower 161f6f8
Remove cache
BewareMyPower 83d9e82
Move toFullTopicName to TopicName
BewareMyPower File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
pulsar-common/src/main/java/org/apache/pulsar/common/naming/TopicNameUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* 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.naming; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
/** | ||
* {@link TopicName} is over heavy in many simple cases. This util class provides many methods to perform topic name | ||
* conversions quickly without a global cache. | ||
*/ | ||
public class TopicNameUtils { | ||
|
||
/** | ||
* Convert a topic name to a full topic name. | ||
* In Pulsar, a full topic name is "<domain>://<tenant>/<namespace>/<local-topic>" (v2) or | ||
* "<domain>://<tenant>/<cluster>/<namespace>/<local-topic>" (v1). However, for convenient, it's allowed for clients | ||
* to pass a short topic name with v2 format: | ||
* - "<local-topic>", which represents "persistent://public/default/<local-topic>" | ||
* - "<tenant>/<namespace>/<local-topic>, which represents "persistent://<tenant>/<namespace>/<local-topic>" | ||
* | ||
* @param topic the topic name from client | ||
* @return the full topic name. | ||
*/ | ||
public static String toFullTopicName(String topic) { | ||
final int index = topic.indexOf("://"); | ||
if (index >= 0) { | ||
TopicDomain.getEnum(topic.substring(0, index)); | ||
final List<String> parts = splitBySlash(topic, 4); | ||
if (parts.size() != 3 && parts.size() != 4) { | ||
throw new IllegalArgumentException(topic + " is invalid"); | ||
} | ||
if (parts.size() == 3) { | ||
NamespaceName.validateNamespaceName(parts.get(0), parts.get(1)); | ||
if (StringUtils.isBlank(parts.get(2))) { | ||
throw new IllegalArgumentException(topic + " has blank local topic"); | ||
} | ||
} else { | ||
NamespaceName.validateNamespaceName(parts.get(0), parts.get(1), parts.get(2)); | ||
if (StringUtils.isBlank(parts.get(3))) { | ||
throw new IllegalArgumentException(topic + " has blank local topic"); | ||
} | ||
} | ||
return topic; // it's a valid full topic name | ||
} else { | ||
List<String> parts = splitBySlash(topic, 0); | ||
if (parts.size() != 1 && parts.size() != 3) { | ||
throw new IllegalArgumentException(topic + " is invalid"); | ||
} | ||
if (parts.size() == 1) { | ||
if (StringUtils.isBlank(parts.get(0))) { | ||
throw new IllegalArgumentException(topic + " has blank local topic"); | ||
} | ||
return "persistent://public/default/" + parts.get(0); | ||
} else { | ||
NamespaceName.validateNamespaceName(parts.get(0), parts.get(1)); | ||
if (StringUtils.isBlank(parts.get(2))) { | ||
throw new IllegalArgumentException(topic + " has blank local topic"); | ||
} | ||
return "persistent://" + topic; | ||
} | ||
} | ||
} | ||
|
||
private static List<String> splitBySlash(String topic, int limit) { | ||
final List<String> tokens = new ArrayList<>(3); | ||
final int loopCount = (limit <= 0) ? Integer.MAX_VALUE : limit - 1; | ||
int beginIndex = 0; | ||
for (int i = 0; i < loopCount; i++) { | ||
final int endIndex = topic.indexOf('/', beginIndex); | ||
if (endIndex < 0) { | ||
tokens.add(topic.substring(beginIndex)); | ||
return tokens; | ||
} else if (endIndex > beginIndex) { | ||
tokens.add(topic.substring(beginIndex, endIndex)); | ||
} else { | ||
throw new IllegalArgumentException("Invalid topic name " + topic); | ||
} | ||
beginIndex = endIndex + 1; | ||
} | ||
if (beginIndex >= topic.length()) { | ||
throw new IllegalArgumentException("Invalid topic name " + topic); | ||
} | ||
tokens.add(topic.substring(beginIndex)); | ||
return tokens; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When multiple connections access the same topic, will it cause more heap memory occupation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. However, once a connection is disconnected, the cache will be garbage collected. In extreme cases, if many consumers subscribe
How about removing the cache? This conversion is actually very fast. It's only about 2.2x slower than reading from a
TopicName
cache.and it can execute 30000*284 operations in 1 second.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i.e. each operation only spends only 0.12 MICROSECONDS. The "space-for-time tradeoff" might not worth here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed the cache and updated the PR description
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, if there are 500k keys,
TopicName#get
is slower.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
200k keys: