-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[broker] Avoid unnecessary recalculation of maxSubscriptionsPerTopic …
…in AbstractTopic (#12658) ### Motivation Currently, `AbstractTopic#maxSubscriptionsPerTopic` stores the value of namespace level policy, but we have broker level setting in `org.apache.pulsar.broker.ServiceConfiguration#maxSubscriptionsPerTopic` and topic level setting in `org.apache.pulsar.common.policies.data.TopicPolicies#maxSubscriptionsPerTopic`. And the real value we used of `maxSubscriptionsPerTopic` is calculated every time in `org.apache.pulsar.broker.service.persistent.PersistentTopic#checkMaxSubscriptionsPerTopicExceed`. It can be avoided by cache maxSubscriptionsPerTopic result locally. As we have already registered listeners to namespace policy and topic policy updates, so we can cache the value locally in AbstractTopic to avoid the recalculation. Finally, as some of other policies value have similar issues, I am introducing `PolicyHierarchyValue` to solve the hierarchy value storage and calculation. ### Modifications Introduce `PolicyHierarchyValue` to store policy value in broker, namespace and topic level. It provides corresponding `updateXX` methods, and recalculate real value in it. And the `get()` method returns the policy value we should use directly.
- Loading branch information
Showing
5 changed files
with
142 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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 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 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
76 changes: 76 additions & 0 deletions
76
pulsar-common/src/main/java/org/apache/pulsar/common/policies/data/PolicyHierarchyValue.java
This file contains 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,76 @@ | ||
/** | ||
* 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 com.google.common.annotations.VisibleForTesting; | ||
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; | ||
import lombok.Getter; | ||
|
||
/** | ||
* Policy value holder for different hierarchy level. | ||
* Currently, we have three hierarchy with priority : topic > namespace > broker. | ||
*/ | ||
public class PolicyHierarchyValue<T> { | ||
private static final AtomicReferenceFieldUpdater<PolicyHierarchyValue, Object> VALUE_UPDATER = | ||
AtomicReferenceFieldUpdater.newUpdater(PolicyHierarchyValue.class, Object.class, "value"); | ||
|
||
private volatile T brokerValue; | ||
|
||
@VisibleForTesting | ||
@Getter | ||
private volatile T namespaceValue; | ||
|
||
private volatile T topicValue; | ||
|
||
private volatile T value; | ||
|
||
public PolicyHierarchyValue() { | ||
} | ||
|
||
public void updateBrokerValue(T brokerValue) { | ||
this.brokerValue = brokerValue; | ||
updateValue(); | ||
} | ||
|
||
public void updateNamespaceValue(T namespaceValue) { | ||
this.namespaceValue = namespaceValue; | ||
updateValue(); | ||
} | ||
|
||
public void updateTopicValue(T topicValue) { | ||
this.topicValue = topicValue; | ||
updateValue(); | ||
} | ||
|
||
private void updateValue() { | ||
VALUE_UPDATER.updateAndGet(this, (preValue) -> { | ||
if (topicValue != null) { | ||
return topicValue; | ||
} else if (namespaceValue != null) { | ||
return namespaceValue; | ||
} else { | ||
return brokerValue; | ||
} | ||
}); | ||
} | ||
|
||
public T get() { | ||
return value; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...common/src/test/java/org/apache/pulsar/common/policies/data/PolicyHierarchyValueTest.java
This file contains 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,46 @@ | ||
/** | ||
* 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 org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
|
||
public class PolicyHierarchyValueTest { | ||
@Test | ||
public void testPolicyValue() { | ||
PolicyHierarchyValue<Integer> value = new PolicyHierarchyValue<>(); | ||
|
||
value.updateBrokerValue(1); | ||
Assert.assertEquals(value.get(), Integer.valueOf(1)); | ||
|
||
value.updateNamespaceValue(2); | ||
Assert.assertEquals(value.get(), Integer.valueOf(2)); | ||
|
||
value.updateTopicValue(3); | ||
Assert.assertEquals(value.get(), Integer.valueOf(3)); | ||
|
||
value.updateNamespaceValue(null); | ||
Assert.assertEquals(value.get(), Integer.valueOf(3)); | ||
|
||
value.updateTopicValue(null); | ||
Assert.assertEquals(value.get(), Integer.valueOf(1)); | ||
} | ||
} |