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.
[refactor][broker] PIP-301 Part-1: Add BundleDataResources (apache#21119
) ### Motivation See pip: apache#21129 ### Modifications Add `BundleDataResources`
- Loading branch information
1 parent
fa5b4c8
commit 4ad992f
Showing
14 changed files
with
173 additions
and
93 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
...-broker-common/src/main/java/org/apache/pulsar/broker/resources/LoadBalanceResources.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,72 @@ | ||
/* | ||
* 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.resources; | ||
|
||
import java.util.Optional; | ||
import java.util.concurrent.CompletableFuture; | ||
import lombok.Getter; | ||
import org.apache.pulsar.common.naming.NamespaceName; | ||
import org.apache.pulsar.metadata.api.MetadataStore; | ||
import org.apache.pulsar.policies.data.loadbalancer.BundleData; | ||
|
||
@Getter | ||
public class LoadBalanceResources { | ||
public static final String BUNDLE_DATA_BASE_PATH = "/loadbalance/bundle-data"; | ||
|
||
private final BundleDataResources bundleDataResources; | ||
|
||
public LoadBalanceResources(MetadataStore store, int operationTimeoutSec) { | ||
bundleDataResources = new BundleDataResources(store, operationTimeoutSec); | ||
} | ||
|
||
public static class BundleDataResources extends BaseResources<BundleData> { | ||
public BundleDataResources(MetadataStore store, int operationTimeoutSec) { | ||
super(store, BundleData.class, operationTimeoutSec); | ||
} | ||
|
||
public CompletableFuture<Optional<BundleData>> getBundleData(String bundle) { | ||
return getAsync(getBundleDataPath(bundle)); | ||
} | ||
|
||
public CompletableFuture<Void> updateBundleData(String bundle, BundleData data) { | ||
return setWithCreateAsync(getBundleDataPath(bundle), __ -> data); | ||
} | ||
|
||
public CompletableFuture<Void> deleteBundleData(String bundle) { | ||
return deleteAsync(getBundleDataPath(bundle)); | ||
} | ||
|
||
// clear resource of `/loadbalance/bundle-data/{tenant}/{namespace}/` in metadata-store | ||
public CompletableFuture<Void> deleteBundleDataAsync(NamespaceName ns) { | ||
final String namespaceBundlePath = joinPath(BUNDLE_DATA_BASE_PATH, ns.toString()); | ||
return getStore().deleteRecursive(namespaceBundlePath); | ||
} | ||
|
||
// clear resource of `/loadbalance/bundle-data/{tenant}/` in metadata-store | ||
public CompletableFuture<Void> deleteBundleDataTenantAsync(String tenant) { | ||
final String tenantBundlePath = joinPath(BUNDLE_DATA_BASE_PATH, tenant); | ||
return getStore().deleteRecursive(tenantBundlePath); | ||
} | ||
|
||
// Get the metadata store path for the given bundle full name. | ||
private String getBundleDataPath(final String bundle) { | ||
return BUNDLE_DATA_BASE_PATH + "/" + bundle; | ||
} | ||
} | ||
} |
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
62 changes: 62 additions & 0 deletions
62
...ker-common/src/test/java/org/apache/pulsar/broker/resources/LoadBalanceResourcesTest.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,62 @@ | ||
/* | ||
* 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.resources; | ||
|
||
import static org.apache.pulsar.broker.resources.BaseResources.joinPath; | ||
import static org.apache.pulsar.broker.resources.LoadBalanceResources.BUNDLE_DATA_BASE_PATH; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.testng.Assert.assertThrows; | ||
import org.apache.pulsar.common.naming.NamespaceName; | ||
import org.apache.pulsar.metadata.api.MetadataStore; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
|
||
public class LoadBalanceResourcesTest { | ||
private MetadataStore configurationStore; | ||
private MetadataStore localStore; | ||
private LoadBalanceResources loadBalanceResources; | ||
|
||
@BeforeMethod | ||
public void setup() { | ||
localStore = mock(MetadataStore.class); | ||
configurationStore = mock(MetadataStore.class); | ||
loadBalanceResources = new LoadBalanceResources(localStore, 30); | ||
} | ||
|
||
/** | ||
* Test that the bundle-data node is deleted from the local stores. | ||
*/ | ||
@Test | ||
public void testDeleteBundleDataAsync() { | ||
NamespaceName ns = NamespaceName.get("my-tenant/my-ns"); | ||
String namespaceBundlePath = joinPath(BUNDLE_DATA_BASE_PATH, ns.toString()); | ||
loadBalanceResources.getBundleDataResources().deleteBundleDataAsync(ns); | ||
|
||
String tenant="my-tenant"; | ||
String tenantBundlePath = joinPath(BUNDLE_DATA_BASE_PATH, tenant); | ||
loadBalanceResources.getBundleDataResources().deleteBundleDataTenantAsync(tenant); | ||
|
||
verify(localStore).deleteRecursive(namespaceBundlePath); | ||
verify(localStore).deleteRecursive(tenantBundlePath); | ||
|
||
assertThrows(()-> verify(configurationStore).deleteRecursive(namespaceBundlePath)); | ||
assertThrows(()-> verify(configurationStore).deleteRecursive(tenantBundlePath)); | ||
} | ||
} |
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
Oops, something went wrong.