-
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.
[improve][broker] Optimise msgOutCounter and bytesOutCounter (#16214)
### Motivation AbstractTopic#getBytesOutCounter and AbstractTopic#getMsgOutCounter both generate full stats only to pick the single required counter for the getters. These can be optimised to perform only the necessary work to calculate the counters. ### Modification Provided a scoped implementation of the above methods for the single counter required for each getter.
- Loading branch information
Dave Maughan
committed
Jun 29, 2022
1 parent
f230d15
commit 0a1afed
Showing
10 changed files
with
259 additions
and
18 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/AbstractSubscription.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,42 @@ | ||
/** | ||
* 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.broker.service; | ||
|
||
import java.util.Optional; | ||
import java.util.concurrent.atomic.LongAdder; | ||
import java.util.function.ToLongFunction; | ||
|
||
public abstract class AbstractSubscription implements Subscription { | ||
protected final LongAdder bytesOutFromRemovedConsumers = new LongAdder(); | ||
protected final LongAdder msgOutFromRemovedConsumer = new LongAdder(); | ||
|
||
public long getMsgOutCounter() { | ||
return msgOutFromRemovedConsumer.longValue() + sumConsumers(Consumer::getMsgOutCounter); | ||
} | ||
|
||
public long getBytesOutCounter() { | ||
return bytesOutFromRemovedConsumers.longValue() + sumConsumers(Consumer::getBytesOutCounter); | ||
} | ||
|
||
private long sumConsumers(ToLongFunction<Consumer> toCounter) { | ||
return Optional.ofNullable(getDispatcher()) | ||
.map(dispatcher -> dispatcher.getConsumers().stream().mapToLong(toCounter).sum()) | ||
.orElse(0L); | ||
} | ||
} |
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
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
57 changes: 57 additions & 0 deletions
57
pulsar-broker/src/test/java/org/apache/pulsar/broker/service/AbstractSubscriptionTest.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,57 @@ | ||
/** | ||
* 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.broker.service; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.spy; | ||
import static org.mockito.Mockito.when; | ||
import static org.testng.Assert.assertEquals; | ||
import java.util.List; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
|
||
@Test(groups = "broker") | ||
public class AbstractSubscriptionTest { | ||
private Consumer consumer; | ||
private AbstractSubscription subscription; | ||
|
||
@BeforeMethod | ||
public void beforeMethod() { | ||
Dispatcher dispatcher = mock(Dispatcher.class); | ||
consumer = mock(Consumer.class); | ||
subscription = spy(AbstractSubscription.class); | ||
|
||
when(subscription.getDispatcher()).thenReturn(dispatcher); | ||
when(dispatcher.getConsumers()).thenReturn(List.of(consumer)); | ||
} | ||
|
||
@Test | ||
public void testGetMsgOutCounter() { | ||
subscription.msgOutFromRemovedConsumer.add(1L); | ||
when(consumer.getMsgOutCounter()).thenReturn(2L); | ||
assertEquals(subscription.getMsgOutCounter(), 3L); | ||
} | ||
|
||
@Test | ||
public void testGetBytesOutCounter() { | ||
subscription.bytesOutFromRemovedConsumers.add(1L); | ||
when(consumer.getBytesOutCounter()).thenReturn(2L); | ||
assertEquals(subscription.getBytesOutCounter(), 3L); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
pulsar-broker/src/test/java/org/apache/pulsar/broker/service/AbstractTopicTest.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,58 @@ | ||
package org.apache.pulsar.broker.service; | ||
|
||
import static org.mockito.Mockito.CALLS_REAL_METHODS; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
import static org.mockito.Mockito.withSettings; | ||
import static org.testng.Assert.assertEquals; | ||
import org.apache.pulsar.broker.PulsarService; | ||
import org.apache.pulsar.broker.ServiceConfiguration; | ||
import org.apache.pulsar.broker.service.persistent.PersistentSubscription; | ||
import org.apache.pulsar.common.util.collections.ConcurrentOpenHashMap; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
|
||
@Test(groups = "broker") | ||
public class AbstractTopicTest { | ||
private AbstractSubscription subscription; | ||
private AbstractTopic topic; | ||
|
||
@BeforeMethod | ||
public void beforeMethod() { | ||
BrokerService brokerService = mock(BrokerService.class); | ||
PulsarService pulsarService = mock(PulsarService.class); | ||
ServiceConfiguration serviceConfiguration = mock(ServiceConfiguration.class); | ||
BacklogQuotaManager backlogQuotaManager = mock(BacklogQuotaManager.class); | ||
subscription = mock(AbstractSubscription.class); | ||
|
||
when(brokerService.pulsar()).thenReturn(pulsarService); | ||
when(pulsarService.getConfiguration()).thenReturn(serviceConfiguration); | ||
when(brokerService.getBacklogQuotaManager()).thenReturn(backlogQuotaManager); | ||
|
||
topic = mock(AbstractTopic.class, withSettings() | ||
.useConstructor("topic", brokerService) | ||
.defaultAnswer(CALLS_REAL_METHODS)); | ||
|
||
ConcurrentOpenHashMap<String, Subscription> subscriptions = | ||
ConcurrentOpenHashMap.<String, Subscription>newBuilder() | ||
.expectedItems(16) | ||
.concurrencyLevel(1) | ||
.build(); | ||
subscriptions.put("subscription", subscription); | ||
when(topic.getSubscriptions()).thenAnswer(invocation -> subscriptions); | ||
} | ||
|
||
@Test | ||
public void testGetMsgOutCounter() { | ||
topic.msgOutFromRemovedSubscriptions.add(1L); | ||
when(subscription.getMsgOutCounter()).thenReturn(2L); | ||
assertEquals(topic.getMsgOutCounter(), 3L); | ||
} | ||
|
||
@Test | ||
public void testGetBytesOutCounter() { | ||
topic.bytesOutFromRemovedSubscriptions.add(1L); | ||
when(subscription.getBytesOutCounter()).thenReturn(2L); | ||
assertEquals(topic.getBytesOutCounter(), 3L); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
pulsar-broker/src/test/java/org/apache/pulsar/broker/service/ConsumerTest.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.broker.service; | ||
|
||
import static java.util.Collections.emptyMap; | ||
import static org.apache.pulsar.client.api.MessageId.latest; | ||
import static org.apache.pulsar.common.api.proto.CommandSubscribe.SubType.Exclusive; | ||
import static org.apache.pulsar.common.api.proto.KeySharedMode.AUTO_SPLIT; | ||
import static org.apache.pulsar.common.protocol.Commands.DEFAULT_CONSUMER_EPOCH; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
import static org.testng.Assert.assertEquals; | ||
import java.net.SocketAddress; | ||
import org.apache.pulsar.broker.PulsarService; | ||
import org.apache.pulsar.broker.ServiceConfiguration; | ||
import org.apache.pulsar.common.api.proto.KeySharedMeta; | ||
import org.apache.pulsar.common.policies.data.stats.ConsumerStatsImpl; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
|
||
@Test(groups = "broker") | ||
public class ConsumerTest { | ||
private Consumer consumer; | ||
private final ConsumerStatsImpl stats = new ConsumerStatsImpl(); | ||
|
||
@BeforeMethod | ||
public void beforeMethod() { | ||
Subscription subscription = mock(Subscription.class); | ||
ServerCnx cnx = mock(ServerCnx.class); | ||
SocketAddress address = mock(SocketAddress.class); | ||
Topic topic = mock(Topic.class); | ||
BrokerService brokerService = mock(BrokerService.class); | ||
PulsarService pulsarService = mock(PulsarService.class); | ||
ServiceConfiguration serviceConfiguration = mock(ServiceConfiguration.class); | ||
|
||
when(cnx.clientAddress()).thenReturn(address); | ||
when(subscription.getTopic()).thenReturn(topic); | ||
when(topic.getBrokerService()).thenReturn(brokerService); | ||
when(brokerService.getPulsar()).thenReturn(pulsarService); | ||
when(pulsarService.getConfiguration()).thenReturn(serviceConfiguration); | ||
|
||
consumer = | ||
new Consumer(subscription, Exclusive, "topic", 1, 0, "Cons1", true, cnx, "myrole-1", emptyMap(), false, | ||
new KeySharedMeta().setKeySharedMode(AUTO_SPLIT), latest, DEFAULT_CONSUMER_EPOCH); | ||
} | ||
|
||
@Test | ||
public void testGetMsgOutCounter() { | ||
stats.msgOutCounter = 1L; | ||
consumer.updateStats(stats); | ||
assertEquals(consumer.getMsgOutCounter(), 1L); | ||
} | ||
|
||
@Test | ||
public void testGetBytesOutCounter() { | ||
stats.bytesOutCounter = 1L; | ||
consumer.updateStats(stats); | ||
assertEquals(consumer.getBytesOutCounter(), 1L); | ||
} | ||
} |