forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PIP-82] [pulsar-broker] Add resource-group configuration listener. (a…
…pache#10657) * Add resource-group configuration listener. * Fix ResourceGroupServiceTest unit test. * Incorporate review feedback. * Added javadoc comments. Co-authored-by: Bharani Chadalavada <[email protected]>
- Loading branch information
1 parent
dbcaa98
commit b83ea98
Showing
12 changed files
with
960 additions
and
376 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
36 changes: 0 additions & 36 deletions
36
...-broker/src/main/java/org/apache/pulsar/broker/resourcegroup/ResourceGroupConfigInfo.java
This file was deleted.
Oops, something went wrong.
159 changes: 159 additions & 0 deletions
159
...ker/src/main/java/org/apache/pulsar/broker/resourcegroup/ResourceGroupConfigListener.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,159 @@ | ||
/** | ||
* 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.resourcegroup; | ||
|
||
import static org.apache.pulsar.broker.cache.ConfigurationCacheService.RESOURCEGROUPS; | ||
import static org.apache.pulsar.common.policies.path.PolicyPath.path; | ||
import com.google.common.collect.Sets; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.function.Consumer; | ||
import org.apache.pulsar.broker.PulsarService; | ||
import org.apache.pulsar.broker.resources.ResourceGroupResources; | ||
import org.apache.pulsar.client.admin.PulsarAdminException; | ||
import org.apache.pulsar.common.policies.data.ResourceGroup; | ||
import org.apache.pulsar.metadata.api.Notification; | ||
import org.apache.pulsar.metadata.api.NotificationType; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Resource Group Config Listener | ||
* | ||
* <P>Meta data store listener of updates to resource group config. | ||
* <P>Listens to resource group configuration changes and updates internal datastructures. | ||
* | ||
* @see <a href="https://github.com/apache/pulsar/wiki/PIP-82%3A-Tenant-and-namespace-level-rate-limiting">Global-quotas</a> | ||
* | ||
*/ | ||
public class ResourceGroupConfigListener implements Consumer<Notification> { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(ResourceGroupConfigListener.class); | ||
private final ResourceGroupService rgService; | ||
private final PulsarService pulsarService; | ||
private final ResourceGroupResources rgResources; | ||
private final ResourceGroupNamespaceConfigListener rgNamespaceConfigListener; | ||
|
||
public ResourceGroupConfigListener(ResourceGroupService rgService, PulsarService pulsarService) { | ||
this.rgService = rgService; | ||
this.pulsarService = pulsarService; | ||
this.rgResources = pulsarService.getPulsarResources().getResourcegroupResources(); | ||
loadAllResourceGroups(); | ||
this.rgResources.getStore().registerListener(this); | ||
rgNamespaceConfigListener = new ResourceGroupNamespaceConfigListener( | ||
rgService, pulsarService, this); | ||
} | ||
|
||
private void loadAllResourceGroups() { | ||
rgResources.getChildrenAsync(path(RESOURCEGROUPS)).whenCompleteAsync((rgList, ex) -> { | ||
if (ex != null) { | ||
LOG.error("Exception when fetching resource groups", ex); | ||
return; | ||
} | ||
final Set<String> existingSet = rgService.resourceGroupGetAll(); | ||
HashSet<String> newSet = new HashSet<>(); | ||
|
||
for (String rgName : rgList) { | ||
newSet.add(rgName); | ||
} | ||
|
||
final Sets.SetView<String> deleteList = Sets.difference(existingSet, newSet); | ||
|
||
for (String rgName: deleteList) { | ||
deleteResourceGroup(rgName); | ||
} | ||
|
||
final Sets.SetView<String> addList = Sets.difference(newSet, existingSet); | ||
for (String rgName: addList) { | ||
final String resourceGroupPath = path(RESOURCEGROUPS, rgName); | ||
pulsarService.getPulsarResources().getResourcegroupResources() | ||
.getAsync(resourceGroupPath).thenAcceptAsync((optionalRg) -> { | ||
ResourceGroup rg = optionalRg.get(); | ||
createResourceGroup(rgName, rg); | ||
}).exceptionally((ex1) -> { | ||
LOG.error("Failed to fetch resourceGroup", ex1); | ||
return null; | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
public synchronized void deleteResourceGroup(String rgName) { | ||
try { | ||
if (rgService.resourceGroupGet(rgName) != null) { | ||
LOG.info("Deleting resource group {}", rgName); | ||
rgService.resourceGroupDelete(rgName); | ||
} | ||
} catch (PulsarAdminException e) { | ||
LOG.error("Got exception while deleting resource group {}, {}", rgName, e); | ||
} | ||
} | ||
|
||
public synchronized void createResourceGroup(String rgName, ResourceGroup rg) { | ||
if (rgService.resourceGroupGet(rgName) == null) { | ||
LOG.info("Creating resource group {}, {}", rgName, rg.toString()); | ||
try { | ||
rgService.resourceGroupCreate(rgName, rg); | ||
} catch (PulsarAdminException ex1) { | ||
LOG.error("Got an exception while creating RG {}", rgName, ex1); | ||
} | ||
} | ||
} | ||
|
||
private void updateResourceGroup(String notifyPath) { | ||
String rgName = notifyPath.substring(notifyPath.lastIndexOf('/') + 1); | ||
|
||
rgResources.getAsync(notifyPath).whenComplete((optionalRg, ex) -> { | ||
if (ex != null) { | ||
LOG.error("Exception when getting resource group {}", rgName, ex); | ||
return; | ||
} | ||
ResourceGroup rg = optionalRg.get(); | ||
try { | ||
LOG.info("Updating resource group {}, {}", rgName, rg.toString()); | ||
rgService.resourceGroupUpdate(rgName, rg); | ||
} catch (PulsarAdminException ex1) { | ||
LOG.error("Got an exception while creating resource group {}", rgName, ex1); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void accept(Notification notification) { | ||
String notifyPath = notification.getPath(); | ||
|
||
if (!notifyPath.startsWith(path(RESOURCEGROUPS))) { | ||
return; | ||
} | ||
LOG.info("Metadata store notification: Path {}, Type {}", notifyPath, notification.getType()); | ||
|
||
String rgName = notifyPath.substring(notifyPath.lastIndexOf('/') + 1); | ||
if (notification.getType() == NotificationType.ChildrenChanged) { | ||
loadAllResourceGroups(); | ||
} else if (!RESOURCEGROUPS.equals(rgName)) { | ||
switch (notification.getType()) { | ||
case Modified: | ||
updateResourceGroup(notifyPath); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.