use of com.yahoo.pulsar.common.naming.NamespaceBundle in project pulsar by yahoo.
the class OwnershipCacheTest method testGetOwner.
@Test
public void testGetOwner() throws Exception {
OwnershipCache cache = new OwnershipCache(this.pulsar, bundleFactory);
NamespaceBundle testBundle = bundleFactory.getFullBundle(new NamespaceName("pulsar/test/ns-3"));
// case 1: no one owns the namespace
assertFalse(cache.getOwnerAsync(testBundle).get().isPresent());
// case 2: someone owns the namespace
ServiceUnitZkUtils.acquireNameSpace(zkCache.getZooKeeper(), ServiceUnitZkUtils.path(testBundle), new NamespaceEphemeralData("pulsar://otherhost:8881", "pulsar://otherhost:8884", "http://otherhost:8080", "https://otherhost:4443", false));
// try to acquire, which will load the read-only cache
NamespaceEphemeralData data1 = cache.tryAcquiringOwnership(testBundle).get();
assertEquals(data1.getNativeUrl(), "pulsar://otherhost:8881");
assertEquals(data1.getNativeUrlTls(), "pulsar://otherhost:8884");
assertTrue(!data1.isDisabled());
// Now do getOwner and compare w/ the returned values
NamespaceEphemeralData readOnlyData = cache.getOwnerAsync(testBundle).get().get();
assertEquals(data1, readOnlyData);
MockZooKeeper mockZk = (MockZooKeeper) zkCache.getZooKeeper();
mockZk.failNow(KeeperException.Code.NONODE);
Optional<NamespaceEphemeralData> res = cache.getOwnerAsync(bundleFactory.getFullBundle(new NamespaceName("pulsar/test/ns-none"))).get();
assertFalse(res.isPresent());
}
use of com.yahoo.pulsar.common.naming.NamespaceBundle in project pulsar by yahoo.
the class OwnershipCacheTest method testGetOwnedServiceUnits.
@Test
public void testGetOwnedServiceUnits() throws Exception {
OwnershipCache cache = new OwnershipCache(this.pulsar, bundleFactory);
NamespaceName testNs = new NamespaceName("pulsar/test/ns-6");
NamespaceBundle testBundle = bundleFactory.getFullBundle(testNs);
// case 1: no one owns the namespace
assertFalse(cache.getOwnerAsync(testBundle).get().isPresent());
assertTrue(cache.getOwnedBundles().isEmpty());
// case 2: someone else owns the namespace
ServiceUnitZkUtils.acquireNameSpace(zkCache.getZooKeeper(), ServiceUnitZkUtils.path(testBundle), new NamespaceEphemeralData("pulsar://otherhost:8881", "pulsar://otherhost:8884", "http://otherhost:8080", "https://otherhost:4443", false));
assertTrue(cache.getOwnedBundles().isEmpty());
// try to acquire, which will load the read-only cache
NamespaceEphemeralData data1 = cache.tryAcquiringOwnership(testBundle).get();
assertEquals(data1.getNativeUrl(), "pulsar://otherhost:8881");
assertEquals(data1.getNativeUrlTls(), "pulsar://otherhost:8884");
assertTrue(!data1.isDisabled());
assertTrue(cache.getOwnedBundles().isEmpty());
// case 3: this broker owns the namespace
// delete the ephemeral node by others
zkCache.getZooKeeper().delete(ServiceUnitZkUtils.path(testBundle), -1);
// force to read directly from ZK
localCache.ownerInfoCache().invalidate(ServiceUnitZkUtils.path(testBundle));
data1 = cache.tryAcquiringOwnership(testBundle).get();
assertEquals(data1.getNativeUrl(), selfBrokerUrl);
assertTrue(!data1.isDisabled());
assertTrue(cache.getOwnedBundles().size() == 1);
}
use of com.yahoo.pulsar.common.naming.NamespaceBundle in project pulsar by yahoo.
the class ResourceQuotaCacheTest method testGetSetBundleQuota.
@Test
public void testGetSetBundleQuota() throws Exception {
ResourceQuotaCache cache = new ResourceQuotaCache(zkCache);
NamespaceBundle testBundle = bundleFactory.getFullBundle(new NamespaceName("pulsar/test/ns-2"));
ResourceQuota quota1 = ResourceQuotaCache.getInitialQuotaValue();
ResourceQuota quota2 = new ResourceQuota();
quota2.setMsgRateIn(10);
quota2.setMsgRateOut(20);
quota2.setBandwidthIn(10000);
quota2.setBandwidthOut(20000);
quota2.setMemory(100);
quota2.setDynamic(false);
assertEquals(cache.getQuota(testBundle), quota1);
cache.setQuota(testBundle, quota2);
assertEquals(cache.getQuota(testBundle), quota2);
cache.unsetQuota(testBundle);
assertEquals(cache.getQuota(testBundle), quota1);
}
use of com.yahoo.pulsar.common.naming.NamespaceBundle in project pulsar by yahoo.
the class NamespaceService method searchForCandidateBroker.
private void searchForCandidateBroker(NamespaceBundle bundle, CompletableFuture<LookupResult> lookupFuture, boolean authoritative) {
String candidateBroker = null;
try {
// check if this is Heartbeat or SLAMonitor namespace
candidateBroker = checkHeartbeatNamespace(bundle);
if (candidateBroker == null) {
String broker = getSLAMonitorBrokerName(bundle);
// checking if the broker is up and running
if (broker != null && isBrokerActive(broker)) {
candidateBroker = broker;
}
}
if (candidateBroker == null) {
if (!this.loadManager.isCentralized() || pulsar.getLeaderElectionService().isLeader()) {
candidateBroker = getLeastLoadedFromLoadManager(bundle);
} else {
if (authoritative) {
// leader broker already assigned the current broker as owner
candidateBroker = pulsar.getWebServiceAddress();
} else {
// forward to leader broker to make assignment
candidateBroker = pulsar.getLeaderElectionService().getCurrentLeader().getServiceUrl();
}
}
}
} catch (Exception e) {
LOG.warn("Error when searching for candidate broker to acquire {}: {}", bundle, e.getMessage(), e);
lookupFuture.completeExceptionally(e);
return;
}
try {
checkNotNull(candidateBroker);
if (pulsar.getWebServiceAddress().equals(candidateBroker)) {
// Load manager decided that the local broker should try to become the owner
ownershipCache.tryAcquiringOwnership(bundle).thenAccept(ownerInfo -> {
if (ownerInfo.isDisabled()) {
if (LOG.isDebugEnabled()) {
LOG.debug("Namespace bundle {} is currently being unloaded", bundle);
}
lookupFuture.completeExceptionally(new IllegalStateException(String.format("Namespace bundle %s is currently being unloaded", bundle)));
} else {
pulsar.loadNamespaceDestinations(bundle);
lookupFuture.complete(new LookupResult(ownerInfo));
}
}).exceptionally(exception -> {
LOG.warn("Failed to acquire ownership for namespace bundle {}: ", bundle, exception.getMessage(), exception);
lookupFuture.completeExceptionally(new PulsarServerException("Failed to acquire ownership for namespace bundle " + bundle, exception));
return null;
});
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("Redirecting to broker {} to acquire ownership of bundle {}", candidateBroker, bundle);
}
// Now setting the redirect url
createLookupResult(candidateBroker).thenAccept(lookupResult -> lookupFuture.complete(lookupResult)).exceptionally(ex -> {
lookupFuture.completeExceptionally(ex);
return null;
});
}
} catch (Exception e) {
LOG.warn("Error in trying to acquire namespace bundle ownership for {}: {}", bundle, e.getMessage(), e);
lookupFuture.completeExceptionally(e);
}
}
use of com.yahoo.pulsar.common.naming.NamespaceBundle in project pulsar by yahoo.
the class NamespaceService method registerNamespace.
/**
* Tried to registers a namespace to this instance
*
* @param namespace
* @param ensureOwned
* @return
* @throws PulsarServerException
* @throws Exception
*/
private boolean registerNamespace(String namespace, boolean ensureOwned) throws PulsarServerException {
String myUrl = pulsar.getBrokerServiceUrl();
try {
NamespaceName nsname = new NamespaceName(namespace);
// [Bug 6504511] Enable writing with JSON format
String otherUrl = null;
NamespaceBundle nsFullBundle = null;
// all pre-registered namespace is assumed to have bundles disabled
nsFullBundle = bundleFactory.getFullBundle(nsname);
// v2 namespace will always use full bundle object
otherUrl = ownershipCache.tryAcquiringOwnership(nsFullBundle).get().getNativeUrl();
if (myUrl.equals(otherUrl)) {
if (nsFullBundle != null) {
// preload heartbeat namespace
pulsar.loadNamespaceDestinations(nsFullBundle);
}
return true;
}
String msg = String.format("namespace already owned by other broker : ns=%s expected=%s actual=%s", namespace, myUrl, otherUrl);
// ignore if not be owned for now
if (!ensureOwned) {
LOG.info(msg);
return false;
}
// should not happen
throw new IllegalStateException(msg);
} catch (Exception e) {
LOG.error(e.getMessage(), e);
throw new PulsarServerException(e);
}
}
Aggregations