use of com.yahoo.pulsar.broker.namespace.NamespaceService in project pulsar by yahoo.
the class PersistentDispatcherFailoverConsumerTest method setup.
@BeforeMethod
public void setup() throws Exception {
ServiceConfiguration svcConfig = spy(new ServiceConfiguration());
PulsarService pulsar = spy(new PulsarService(svcConfig));
doReturn(svcConfig).when(pulsar).getConfiguration();
mlFactoryMock = mock(ManagedLedgerFactory.class);
doReturn(mlFactoryMock).when(pulsar).getManagedLedgerFactory();
ZooKeeper mockZk = mock(ZooKeeper.class);
doReturn(mockZk).when(pulsar).getZkClient();
configCacheService = mock(ConfigurationCacheService.class);
@SuppressWarnings("unchecked") ZooKeeperDataCache<Policies> zkDataCache = mock(ZooKeeperDataCache.class);
doReturn(zkDataCache).when(configCacheService).policiesCache();
doReturn(configCacheService).when(pulsar).getConfigurationCache();
brokerService = spy(new BrokerService(pulsar));
doReturn(brokerService).when(pulsar).getBrokerService();
serverCnx = spy(new ServerCnx(brokerService));
doReturn(true).when(serverCnx).isActive();
doReturn(true).when(serverCnx).isWritable();
doReturn(new InetSocketAddress("localhost", 1234)).when(serverCnx).clientAddress();
NamespaceService nsSvc = mock(NamespaceService.class);
doReturn(nsSvc).when(pulsar).getNamespaceService();
doReturn(true).when(nsSvc).isServiceUnitOwned(any(NamespaceBundle.class));
doReturn(true).when(nsSvc).isServiceUnitActive(any(DestinationName.class));
setupMLAsyncCallbackMocks();
}
use of com.yahoo.pulsar.broker.namespace.NamespaceService in project pulsar by yahoo.
the class PulsarWebResource method validateDestinationOwnership.
/**
* Checks whether the broker is the owner of the namespace. Otherwise it will raise an exception to redirect the
* client to the appropriate broker. If no broker owns the namespace yet, this function will try to acquire the
* ownership by default.
*
* @param authoritative
*
* @param property
* @param cluster
* @param namespace
*/
protected void validateDestinationOwnership(DestinationName fqdn, boolean authoritative) {
NamespaceService nsService = pulsar().getNamespaceService();
try {
// per function name, this is trying to acquire the whole namespace ownership
URL webUrl = nsService.getWebServiceUrl(fqdn, authoritative, isRequestHttps(), false);
// Ensure we get a url
if (webUrl == null) {
log.info("Unable to get web service url");
throw new RestException(Status.PRECONDITION_FAILED, "Failed to find ownership for destination:" + fqdn);
}
if (!nsService.isServiceUnitOwned(fqdn)) {
boolean newAuthoritative = this.isLeaderBroker(pulsar());
// Replace the host and port of the current request and redirect
URI redirect = UriBuilder.fromUri(uri.getRequestUri()).host(webUrl.getHost()).port(webUrl.getPort()).replaceQueryParam("authoritative", newAuthoritative).build();
// Redirect
log.debug("Redirecting the rest call to {}", redirect);
throw new WebApplicationException(Response.temporaryRedirect(redirect).build());
}
} catch (IllegalArgumentException iae) {
// namespace format is not valid
log.debug(String.format("Failed to find owner for destination:%s", fqdn), iae);
throw new RestException(Status.PRECONDITION_FAILED, "Can't find owner for destination " + fqdn);
} catch (IllegalStateException ise) {
log.debug(String.format("Failed to find owner for destination:%s", fqdn), ise);
throw new RestException(Status.PRECONDITION_FAILED, "Can't find owner for destination " + fqdn);
} catch (WebApplicationException wae) {
throw wae;
} catch (Exception oe) {
log.debug(String.format("Failed to find owner for destination:%s", fqdn), oe);
throw new RestException(oe);
}
}
Aggregations