Search in sources :

Example 1 with NamespaceService

use of com.yahoo.pulsar.broker.namespace.NamespaceService in project pulsar by yahoo.

the class MockedPulsarServiceBaseTest method setupBrokerMocks.

protected void setupBrokerMocks(PulsarService pulsar) throws Exception {
    // Override default providers with mocked ones
    doReturn(mockZooKeeperClientFactory).when(pulsar).getZooKeeperClientFactory();
    doReturn(mockBookKeeperClientFactory).when(pulsar).getBookKeeperClientFactory();
    Supplier<NamespaceService> namespaceServiceSupplier = () -> spy(new NamespaceService(pulsar));
    doReturn(namespaceServiceSupplier).when(pulsar).getNamespaceServiceProvider();
    doReturn(sameThreadOrderedSafeExecutor).when(pulsar).getOrderedExecutor();
}
Also used : NamespaceService(com.yahoo.pulsar.broker.namespace.NamespaceService)

Example 2 with NamespaceService

use of com.yahoo.pulsar.broker.namespace.NamespaceService in project pulsar by yahoo.

the class PulsarWebResource method validateBundleOwnership.

public void validateBundleOwnership(NamespaceBundle bundle, boolean authoritative, boolean readOnly) throws Exception {
    NamespaceService nsService = pulsar().getNamespaceService();
    try {
        // Call getWebServiceUrl() to acquire or redirect the request
        // Get web service URL of owning broker.
        // 1: If namespace is assigned to this broker, continue
        // 2: If namespace is assigned to another broker, redirect to the webservice URL of another broker
        // authoritative flag is ignored
        // 3: If namespace is unassigned and readOnly is true, return 412
        // 4: If namespace is unassigned and readOnly is false:
        // - If authoritative is false and this broker is not leader, forward to leader
        // - If authoritative is false and this broker is leader, determine owner and forward w/ authoritative=true
        // - If authoritative is true, own the namespace and continue
        URL webUrl = nsService.getWebServiceUrl(bundle, authoritative, isRequestHttps(), readOnly);
        // Ensure we get a url
        if (webUrl == null) {
            log.warn("Unable to get web service url");
            throw new RestException(Status.PRECONDITION_FAILED, "Failed to find ownership for ServiceUnit:" + bundle.toString());
        }
        if (!nsService.isServiceUnitOwned(bundle)) {
            boolean newAuthoritative = this.isLeaderBroker();
            // 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();
            log.debug("{} is not a service unit owned", bundle);
            // 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 ServiceUnit %s", bundle), iae);
        throw new RestException(Status.PRECONDITION_FAILED, "ServiceUnit format is not expected. ServiceUnit " + bundle);
    } catch (IllegalStateException ise) {
        log.debug(String.format("Failed to find owner for ServiceUnit %s", bundle), ise);
        throw new RestException(Status.PRECONDITION_FAILED, "ServiceUnit bundle is actived. ServiceUnit " + bundle);
    } catch (NullPointerException e) {
        log.warn("Unable to get web service url");
        throw new RestException(Status.PRECONDITION_FAILED, "Failed to find ownership for ServiceUnit:" + bundle);
    } catch (WebApplicationException wae) {
        throw wae;
    }
}
Also used : NamespaceService(com.yahoo.pulsar.broker.namespace.NamespaceService) WebApplicationException(javax.ws.rs.WebApplicationException) URI(java.net.URI) URL(java.net.URL)

Example 3 with NamespaceService

use of com.yahoo.pulsar.broker.namespace.NamespaceService in project pulsar by yahoo.

the class ServerCnxTest method setup.

@BeforeMethod
public void setup() throws Exception {
    svcConfig = spy(new ServiceConfiguration());
    pulsar = spy(new PulsarService(svcConfig));
    svcConfig.setKeepAliveIntervalSeconds(inSec(1, TimeUnit.SECONDS));
    svcConfig.setBacklogQuotaCheckEnabled(false);
    doReturn(svcConfig).when(pulsar).getConfiguration();
    doReturn("use").when(svcConfig).getClusterName();
    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(Optional.empty()).when(zkDataCache).get(anyObject());
    doReturn(zkDataCache).when(configCacheService).policiesCache();
    doReturn(configCacheService).when(pulsar).getConfigurationCache();
    brokerService = spy(new BrokerService(pulsar));
    doReturn(brokerService).when(pulsar).getBrokerService();
    namespaceService = mock(NamespaceService.class);
    doReturn(namespaceService).when(pulsar).getNamespaceService();
    doReturn(true).when(namespaceService).isServiceUnitOwned(any(NamespaceBundle.class));
    doReturn(true).when(namespaceService).isServiceUnitActive(any(DestinationName.class));
    setupMLAsyncCallbackMocks();
    clientChannelHelper = new ClientChannelHelper();
}
Also used : NamespaceBundle(com.yahoo.pulsar.common.naming.NamespaceBundle) Policies(com.yahoo.pulsar.common.policies.data.Policies) ClientChannelHelper(com.yahoo.pulsar.broker.service.utils.ClientChannelHelper) 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) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 4 with NamespaceService

use of com.yahoo.pulsar.broker.namespace.NamespaceService in project pulsar by yahoo.

the class PersistentTopicConcurrentTest method setup.

@BeforeMethod
public void setup(Method m) throws Exception {
    super.setUp(m);
    ServiceConfiguration svcConfig = spy(new ServiceConfiguration());
    PulsarService pulsar = spy(new PulsarService(svcConfig));
    doReturn(svcConfig).when(pulsar).getConfiguration();
    mlFactoryMock = mock(ManagedLedgerFactory.class);
    ManagedLedgerFactory factory = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
    ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(2));
    final ManagedCursor cursor = ledger.openCursor("c1");
    cursorMock = cursor;
    ledgerMock = ledger;
    mlFactoryMock = factory;
    doReturn(mlFactoryMock).when(pulsar).getManagedLedgerFactory();
    ZooKeeper mockZk = mock(ZooKeeper.class);
    doReturn(mockZk).when(pulsar).getZkClient();
    brokerService = spy(new BrokerService(pulsar));
    doReturn(brokerService).when(pulsar).getBrokerService();
    serverCnx = spy(new ServerCnx(brokerService));
    doReturn(true).when(serverCnx).isActive();
    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));
    final List<Position> addedEntries = Lists.newArrayList();
    for (int i = 0; i < 100; i++) {
        Position pos = ledger.addEntry("entry".getBytes());
        addedEntries.add(pos);
    }
}
Also used : NamespaceBundle(com.yahoo.pulsar.common.naming.NamespaceBundle) Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ServerCnx(com.yahoo.pulsar.broker.service.ServerCnx) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) 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) DestinationName(com.yahoo.pulsar.common.naming.DestinationName) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) BrokerService(com.yahoo.pulsar.broker.service.BrokerService) ManagedLedgerFactoryImpl(org.apache.bookkeeper.mledger.impl.ManagedLedgerFactoryImpl) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 5 with NamespaceService

use of com.yahoo.pulsar.broker.namespace.NamespaceService in project pulsar by yahoo.

the class PersistentTopicTest method setup.

@BeforeMethod
public void setup() throws Exception {
    ServiceConfiguration svcConfig = spy(new ServiceConfiguration());
    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();
    doReturn(Optional.empty()).when(zkDataCache).get(anyString());
    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) 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) BeforeMethod(org.testng.annotations.BeforeMethod)

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