Search in sources :

Example 6 with NamespaceService

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();
}
Also used : NamespaceBundle(com.yahoo.pulsar.common.naming.NamespaceBundle) Policies(com.yahoo.pulsar.common.policies.data.Policies) InetSocketAddress(java.net.InetSocketAddress) ServerCnx(com.yahoo.pulsar.broker.service.ServerCnx) ZooKeeper(org.apache.zookeeper.ZooKeeper) ServiceConfiguration(com.yahoo.pulsar.broker.ServiceConfiguration) PulsarService(com.yahoo.pulsar.broker.PulsarService) NamespaceService(com.yahoo.pulsar.broker.namespace.NamespaceService) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) ConfigurationCacheService(com.yahoo.pulsar.broker.cache.ConfigurationCacheService) DestinationName(com.yahoo.pulsar.common.naming.DestinationName) BrokerService(com.yahoo.pulsar.broker.service.BrokerService) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 7 with NamespaceService

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);
    }
}
Also used : NamespaceService(com.yahoo.pulsar.broker.namespace.NamespaceService) WebApplicationException(javax.ws.rs.WebApplicationException) URI(java.net.URI) URL(java.net.URL) KeeperException(org.apache.zookeeper.KeeperException) ExecutionException(java.util.concurrent.ExecutionException) WebApplicationException(javax.ws.rs.WebApplicationException)

Aggregations

NamespaceService (com.yahoo.pulsar.broker.namespace.NamespaceService)7 PulsarService (com.yahoo.pulsar.broker.PulsarService)4 ServiceConfiguration (com.yahoo.pulsar.broker.ServiceConfiguration)4 DestinationName (com.yahoo.pulsar.common.naming.DestinationName)4 NamespaceBundle (com.yahoo.pulsar.common.naming.NamespaceBundle)4 ManagedLedgerFactory (org.apache.bookkeeper.mledger.ManagedLedgerFactory)4 ZooKeeper (org.apache.zookeeper.ZooKeeper)4 BeforeMethod (org.testng.annotations.BeforeMethod)4 ConfigurationCacheService (com.yahoo.pulsar.broker.cache.ConfigurationCacheService)3 Policies (com.yahoo.pulsar.common.policies.data.Policies)3 BrokerService (com.yahoo.pulsar.broker.service.BrokerService)2 ServerCnx (com.yahoo.pulsar.broker.service.ServerCnx)2 InetSocketAddress (java.net.InetSocketAddress)2 URI (java.net.URI)2 URL (java.net.URL)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 ClientChannelHelper (com.yahoo.pulsar.broker.service.utils.ClientChannelHelper)1 ExecutionException (java.util.concurrent.ExecutionException)1 ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)1 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)1