Search in sources :

Example 1 with Policies

use of com.yahoo.pulsar.common.policies.data.Policies in project pulsar by yahoo.

the class PersistentTopic method checkMessageExpiry.

@Override
public void checkMessageExpiry() {
    DestinationName name = DestinationName.get(topic);
    Policies policies;
    try {
        policies = brokerService.pulsar().getConfigurationCache().policiesCache().get(AdminResource.path("policies", name.getNamespace())).orElseThrow(() -> new KeeperException.NoNodeException());
        if (policies.message_ttl_in_seconds != 0) {
            subscriptions.forEach((subName, sub) -> sub.expireMessages(policies.message_ttl_in_seconds));
            replicators.forEach((region, replicator) -> replicator.expireMessages(policies.message_ttl_in_seconds));
        }
    } catch (Exception e) {
        if (log.isDebugEnabled()) {
            log.debug("[{}] Error getting policies", topic);
        }
    }
}
Also used : Policies(com.yahoo.pulsar.common.policies.data.Policies) DestinationName(com.yahoo.pulsar.common.naming.DestinationName) NamingException(com.yahoo.pulsar.broker.service.BrokerServiceException.NamingException) TopicBusyException(com.yahoo.pulsar.broker.service.BrokerServiceException.TopicBusyException) PersistenceException(com.yahoo.pulsar.broker.service.BrokerServiceException.PersistenceException) TopicFencedException(com.yahoo.pulsar.broker.service.BrokerServiceException.TopicFencedException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedLedgerFencedException(org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerFencedException) UnsupportedVersionException(com.yahoo.pulsar.broker.service.BrokerServiceException.UnsupportedVersionException) SubscriptionBusyException(com.yahoo.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) ConsumerBusyException(com.yahoo.pulsar.broker.service.BrokerServiceException.ConsumerBusyException) KeeperException(org.apache.zookeeper.KeeperException) BrokerServiceException(com.yahoo.pulsar.broker.service.BrokerServiceException) ServerMetadataException(com.yahoo.pulsar.broker.service.BrokerServiceException.ServerMetadataException)

Example 2 with Policies

use of com.yahoo.pulsar.common.policies.data.Policies in project pulsar by yahoo.

the class AdminTest method resourceQuotas.

@Test
void resourceQuotas() throws Exception {
    // get Default Resource Quota
    ResourceQuota quota = resourceQuotas.getDefaultResourceQuota();
    assertNotNull(quota);
    assertTrue(quota.getBandwidthIn() > 0);
    // set Default Resource Quota
    double defaultBandwidth = 1000;
    quota.setBandwidthIn(defaultBandwidth);
    quota.setBandwidthOut(defaultBandwidth);
    resourceQuotas.setDefaultResourceQuota(quota);
    assertTrue(resourceQuotas.getDefaultResourceQuota().getBandwidthIn() == defaultBandwidth);
    assertTrue(resourceQuotas.getDefaultResourceQuota().getBandwidthOut() == defaultBandwidth);
    String property = "prop-xyz";
    String cluster = "use";
    String namespace = "ns";
    String bundleRange = "0x00000000_0xffffffff";
    Policies policies = new Policies();
    doReturn(policies).when(resourceQuotas).getNamespacePolicies(property, cluster, namespace);
    doReturn("client-id").when(resourceQuotas).clientAppId();
    try {
        resourceQuotas.setNamespaceBundleResourceQuota(property, cluster, namespace, bundleRange, quota);
        fail();
    } catch (Exception e) {
    // OK : should fail without creating policies
    }
    try {
        resourceQuotas.removeNamespaceBundleResourceQuota(property, cluster, namespace, bundleRange);
        fail();
    } catch (Exception e) {
    // OK : should fail without creating policies
    }
    // create policies
    PropertyAdmin admin = new PropertyAdmin();
    admin.getAllowedClusters().add(cluster);
    mockZookKeeper.create(PulsarWebResource.path("policies", property), ObjectMapperFactory.getThreadLocal().writeValueAsBytes(admin), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    // customized bandwidth for this namespace
    double customizeBandwidth = 3000;
    quota.setBandwidthIn(customizeBandwidth);
    quota.setBandwidthOut(customizeBandwidth);
    // set and get Resource Quota
    resourceQuotas.setNamespaceBundleResourceQuota(property, cluster, namespace, bundleRange, quota);
    ResourceQuota bundleQuota = resourceQuotas.getNamespaceBundleResourceQuota(property, cluster, namespace, bundleRange);
    assertEquals(quota, bundleQuota);
    // remove quota which sets to default quota
    resourceQuotas.removeNamespaceBundleResourceQuota(property, cluster, namespace, bundleRange);
    bundleQuota = resourceQuotas.getNamespaceBundleResourceQuota(property, cluster, namespace, bundleRange);
    assertTrue(bundleQuota.getBandwidthIn() == defaultBandwidth);
    assertTrue(bundleQuota.getBandwidthOut() == defaultBandwidth);
}
Also used : Policies(com.yahoo.pulsar.common.policies.data.Policies) ResourceQuota(com.yahoo.pulsar.common.policies.data.ResourceQuota) PropertyAdmin(com.yahoo.pulsar.common.policies.data.PropertyAdmin) RestException(com.yahoo.pulsar.broker.web.RestException) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 3 with Policies

use of com.yahoo.pulsar.common.policies.data.Policies in project pulsar by yahoo.

the class NamespacesTest method testGrantAndRevokePermissions.

@Test(enabled = false)
public void testGrantAndRevokePermissions() throws Exception {
    Policies expectedPolicies = new Policies();
    assertEquals(namespaces.getPolicies(this.testProperty, this.testLocalCluster, this.testLocalNamespaces.get(0).getLocalName()), expectedPolicies);
    assertEquals(namespaces.getPermissions(this.testProperty, this.testLocalCluster, this.testLocalNamespaces.get(0).getLocalName()), expectedPolicies.auth_policies.namespace_auth);
    namespaces.grantPermissionOnNamespace(this.testProperty, this.testLocalCluster, this.testLocalNamespaces.get(0).getLocalName(), "my-role", EnumSet.of(AuthAction.produce));
    expectedPolicies.auth_policies.namespace_auth.put("my-role", EnumSet.of(AuthAction.produce));
    assertEquals(namespaces.getPolicies(this.testProperty, this.testLocalCluster, this.testLocalNamespaces.get(0).getLocalName()), expectedPolicies);
    assertEquals(namespaces.getPermissions(this.testProperty, this.testLocalCluster, this.testLocalNamespaces.get(0).getLocalName()), expectedPolicies.auth_policies.namespace_auth);
    namespaces.grantPermissionOnNamespace(this.testProperty, this.testLocalCluster, this.testLocalNamespaces.get(0).getLocalName(), "other-role", EnumSet.of(AuthAction.consume));
    expectedPolicies.auth_policies.namespace_auth.put("other-role", EnumSet.of(AuthAction.consume));
    assertEquals(namespaces.getPolicies(this.testProperty, this.testLocalCluster, this.testLocalNamespaces.get(0).getLocalName()), expectedPolicies);
    assertEquals(namespaces.getPermissions(this.testProperty, this.testLocalCluster, this.testLocalNamespaces.get(0).getLocalName()), expectedPolicies.auth_policies.namespace_auth);
    namespaces.revokePermissionsOnNamespace(this.testProperty, this.testLocalCluster, this.testLocalNamespaces.get(0).getLocalName(), "my-role");
    expectedPolicies.auth_policies.namespace_auth.remove("my-role");
    assertEquals(namespaces.getPolicies(this.testProperty, this.testLocalCluster, this.testLocalNamespaces.get(0).getLocalName()), expectedPolicies);
    assertEquals(namespaces.getPermissions(this.testProperty, this.testLocalCluster, this.testLocalNamespaces.get(0).getLocalName()), expectedPolicies.auth_policies.namespace_auth);
    // Non-existing namespaces
    try {
        namespaces.getPolicies(this.testProperty, this.testLocalCluster, "non-existing-namespace-1");
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.NOT_FOUND.getStatusCode());
    }
    try {
        namespaces.getPermissions(this.testProperty, this.testLocalCluster, "non-existing-namespace-1");
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.NOT_FOUND.getStatusCode());
    }
    try {
        namespaces.grantPermissionOnNamespace(this.testProperty, this.testLocalCluster, "non-existing-namespace-1", "my-role", EnumSet.of(AuthAction.produce));
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.NOT_FOUND.getStatusCode());
    }
    try {
        namespaces.revokePermissionsOnNamespace(this.testProperty, this.testLocalCluster, "non-existing-namespace-1", "my-role");
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.NOT_FOUND.getStatusCode());
    }
    NamespaceName testNs = this.testLocalNamespaces.get(1);
    mockZookKeeper.failNow(Code.SESSIONEXPIRED);
    try {
        namespaces.getPolicies(testNs.getProperty(), testNs.getCluster(), testNs.getLocalName());
        fail("should have failed");
    } catch (RestException e) {
    // Ok
    }
    mockZookKeeper.failNow(Code.SESSIONEXPIRED);
    try {
        namespaces.getPermissions(testNs.getProperty(), testNs.getCluster(), testNs.getLocalName());
        fail("should have failed");
    } catch (RestException e) {
    // Ok
    }
    mockZookKeeper.failNow(Code.SESSIONEXPIRED);
    try {
        namespaces.grantPermissionOnNamespace(testNs.getProperty(), testNs.getCluster(), testNs.getLocalName(), "other-role", EnumSet.of(AuthAction.consume));
        fail("should have failed");
    } catch (RestException e) {
    // Ok
    }
    mockZookKeeper.failNow(Code.BADVERSION);
    try {
        namespaces.grantPermissionOnNamespace(testNs.getProperty(), testNs.getCluster(), testNs.getLocalName(), "other-role", EnumSet.of(AuthAction.consume));
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.CONFLICT.getStatusCode());
    }
    mockZookKeeper.failNow(Code.BADVERSION);
    try {
        namespaces.revokePermissionsOnNamespace(testNs.getProperty(), testNs.getCluster(), testNs.getLocalName(), "other-role");
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.CONFLICT.getStatusCode());
    }
    mockZookKeeper.failNow(Code.SESSIONEXPIRED);
    try {
        namespaces.revokePermissionsOnNamespace(testNs.getProperty(), testNs.getCluster(), testNs.getLocalName(), "other-role");
        fail("should have failed");
    } catch (RestException e) {
    // Ok
    }
}
Also used : NamespaceName(com.yahoo.pulsar.common.naming.NamespaceName) Policies(com.yahoo.pulsar.common.policies.data.Policies) RetentionPolicies(com.yahoo.pulsar.common.policies.data.RetentionPolicies) RestException(com.yahoo.pulsar.broker.web.RestException) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 4 with Policies

use of com.yahoo.pulsar.common.policies.data.Policies in project pulsar by yahoo.

the class HttpDestinationLookupv2Test method testValidateReplicationSettingsOnNamespace.

@Test
public void testValidateReplicationSettingsOnNamespace() throws Exception {
    final String property = "my-prop";
    final String cluster = "global";
    final String ns1 = "ns1";
    final String ns2 = "ns2";
    Policies policies1 = new Policies();
    doReturn(Optional.of(policies1)).when(policiesCache).get(AdminResource.path("policies", property, cluster, ns1));
    Policies policies2 = new Policies();
    policies2.replication_clusters = Lists.newArrayList("invalid-localCluster");
    doReturn(Optional.of(policies2)).when(policiesCache).get(AdminResource.path("policies", property, cluster, ns2));
    DestinationLookup destLookup = spy(new DestinationLookup());
    doReturn(false).when(destLookup).isRequestHttps();
    destLookup.setPulsar(pulsar);
    doReturn("null").when(destLookup).clientAppId();
    Field uriField = PulsarWebResource.class.getDeclaredField("uri");
    uriField.setAccessible(true);
    UriInfo uriInfo = mock(UriInfo.class);
    uriField.set(destLookup, uriInfo);
    doReturn(false).when(config).isAuthorizationEnabled();
    AsyncResponse asyncResponse = mock(AsyncResponse.class);
    destLookup.lookupDestinationAsync(property, cluster, ns1, "empty-cluster", false, asyncResponse);
    ArgumentCaptor<Throwable> arg = ArgumentCaptor.forClass(Throwable.class);
    verify(asyncResponse).resume(arg.capture());
    assertEquals(arg.getValue().getClass(), RestException.class);
    AsyncResponse asyncResponse2 = mock(AsyncResponse.class);
    destLookup.lookupDestinationAsync(property, cluster, ns2, "invalid-localCluster", false, asyncResponse2);
    ArgumentCaptor<Throwable> arg2 = ArgumentCaptor.forClass(Throwable.class);
    verify(asyncResponse2).resume(arg2.capture());
    // Should have raised exception for invalid cluster
    assertEquals(arg2.getValue().getClass(), RestException.class);
}
Also used : Field(java.lang.reflect.Field) Policies(com.yahoo.pulsar.common.policies.data.Policies) DestinationLookup(com.yahoo.pulsar.broker.lookup.DestinationLookup) AsyncResponse(javax.ws.rs.container.AsyncResponse) UriInfo(javax.ws.rs.core.UriInfo) Test(org.testng.annotations.Test)

Example 5 with Policies

use of com.yahoo.pulsar.common.policies.data.Policies 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

Policies (com.yahoo.pulsar.common.policies.data.Policies)49 ApiOperation (io.swagger.annotations.ApiOperation)30 ApiResponses (io.swagger.annotations.ApiResponses)30 Path (javax.ws.rs.Path)30 RetentionPolicies (com.yahoo.pulsar.common.policies.data.RetentionPolicies)27 PersistencePolicies (com.yahoo.pulsar.common.policies.data.PersistencePolicies)26 KeeperException (org.apache.zookeeper.KeeperException)25 RestException (com.yahoo.pulsar.broker.web.RestException)24 SubscriptionBusyException (com.yahoo.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException)19 NamespaceName (com.yahoo.pulsar.common.naming.NamespaceName)18 WebApplicationException (javax.ws.rs.WebApplicationException)17 PulsarServerException (com.yahoo.pulsar.broker.PulsarServerException)16 PulsarAdminException (com.yahoo.pulsar.client.admin.PulsarAdminException)15 NamespaceBundle (com.yahoo.pulsar.common.naming.NamespaceBundle)13 NoNodeException (org.apache.zookeeper.KeeperException.NoNodeException)13 Stat (org.apache.zookeeper.data.Stat)13 POST (javax.ws.rs.POST)11 GET (javax.ws.rs.GET)9 Test (org.testng.annotations.Test)9 DestinationName (com.yahoo.pulsar.common.naming.DestinationName)8