diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/resourcegroup/ResourceGroupConfigListenerTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/resourcegroup/ResourceGroupConfigListenerTest.java index 3071edb74f2ac..ce5479b9a3f08 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/resourcegroup/ResourceGroupConfigListenerTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/resourcegroup/ResourceGroupConfigListenerTest.java @@ -18,12 +18,16 @@ */ package org.apache.pulsar.broker.resourcegroup; +import static org.apache.pulsar.broker.cache.ConfigurationCacheService.RESOURCEGROUPS; +import static org.apache.pulsar.common.policies.path.PolicyPath.path; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNull; import static org.testng.Assert.assertThrows; import com.google.common.collect.Sets; import java.util.Random; + +import lombok.extern.slf4j.Slf4j; import org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest; import org.apache.pulsar.client.admin.PulsarAdminException; import org.apache.pulsar.common.policies.data.ClusterData; @@ -34,6 +38,7 @@ import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; +@Slf4j public class ResourceGroupConfigListenerTest extends MockedPulsarServiceBaseTest { ResourceGroup testAddRg = new ResourceGroup(); @@ -166,6 +171,51 @@ public void testResourceGroupCreateMany() throws Exception { }); } + @Test + public void testResourceGroupUpdateLoop() throws PulsarAdminException { + + ResourceGroup zooRg = new ResourceGroup(); + pulsar.getPulsarResources().getResourcegroupResources().getStore().registerListener( + notification -> { + String notifyPath = notification.getPath(); + String rgName = notifyPath.substring(notifyPath.lastIndexOf('/') + 1); + if (!notifyPath.startsWith(path(RESOURCEGROUPS))) { + return; + } + if (RESOURCEGROUPS.equals(rgName)) { + return; + } + pulsar.getPulsarResources().getResourcegroupResources() + .getAsync(notifyPath).whenComplete((optionalRg, ex) -> { + if (ex != null) { + return; + } + if (optionalRg.isPresent()) { + ResourceGroup resourceGroup = optionalRg.get(); + + zooRg.setDispatchRateInBytes(resourceGroup.getDispatchRateInBytes()); + zooRg.setDispatchRateInMsgs(resourceGroup.getDispatchRateInMsgs()); + zooRg.setPublishRateInBytes(resourceGroup.getPublishRateInBytes()); + zooRg.setPublishRateInMsgs(resourceGroup.getPublishRateInMsgs()); + } + }); + } + ); + ResourceGroup rg = new ResourceGroup(); + rg.setPublishRateInMsgs(-1); + rg.setPublishRateInBytes(10); + rg.setDispatchRateInMsgs(10); + rg.setDispatchRateInBytes(20); + createResourceGroup("myrg", rg); + + for (int i = 0; i < 1000; i++) { + rg.setPublishRateInMsgs(i); + updateResourceGroup("myrg", rg); + } + + Awaitility.await().untilAsserted(() -> assertEquals(zooRg.getPublishRateInMsgs(), rg.getPublishRateInMsgs())); + } + private void prepareData() throws PulsarAdminException { admin.clusters().createCluster(clusterName, ClusterData.builder().serviceUrl(pulsar.getWebServiceAddress()).build()); diff --git a/pulsar-metadata/src/main/java/org/apache/pulsar/metadata/cache/impl/MetadataCacheImpl.java b/pulsar-metadata/src/main/java/org/apache/pulsar/metadata/cache/impl/MetadataCacheImpl.java index 7af568d8780ee..f8554a428e559 100644 --- a/pulsar-metadata/src/main/java/org/apache/pulsar/metadata/cache/impl/MetadataCacheImpl.java +++ b/pulsar-metadata/src/main/java/org/apache/pulsar/metadata/cache/impl/MetadataCacheImpl.java @@ -161,8 +161,8 @@ public CompletableFuture readModifyUpdateOrCreate(String path, Function { // Make sure we have the value cached before the operation is completed - objCache.put(path, - FutureUtils.value(Optional.of(new CacheGetResult<>(newValueObj, stat)))); + //objCache.put(path, + // FutureUtils.value(Optional.of(new CacheGetResult<>(newValueObj, stat)))); }).thenApply(__ -> newValueObj); }), path); } @@ -192,8 +192,8 @@ public CompletableFuture readModifyUpdate(String path, Function modifyF return store.put(path, newValue, Optional.of(expectedVersion)).thenAccept(stat -> { // Make sure we have the value cached before the operation is completed - objCache.put(path, - FutureUtils.value(Optional.of(new CacheGetResult<>(newValueObj, stat)))); + //objCache.put(path, + // FutureUtils.value(Optional.of(new CacheGetResult<>(newValueObj, stat)))); }).thenApply(__ -> newValueObj); }), path); }