Search in sources :

Example 1 with PropertyAdmin

use of org.apache.pulsar.common.policies.data.PropertyAdmin in project incubator-pulsar by apache.

the class PropertiesBase method updateProperty.

@POST
@Path("/{property}")
@ApiOperation(value = "Update the admins for a property.", notes = "This operation requires Pulsar super-user privileges.")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"), @ApiResponse(code = 404, message = "Property does not exist"), @ApiResponse(code = 409, message = "Property already exist") })
public void updateProperty(@PathParam("property") String property, PropertyAdmin newPropertyAdmin) {
    validateSuperUserAccess();
    validatePoliciesReadOnlyAccess();
    Stat nodeStat = new Stat();
    try {
        byte[] content = globalZk().getData(path(POLICIES, property), null, nodeStat);
        PropertyAdmin oldPropertyAdmin = jsonMapper().readValue(content, PropertyAdmin.class);
        List<String> clustersWithActiveNamespaces = Lists.newArrayList();
        if (oldPropertyAdmin.getAllowedClusters().size() > newPropertyAdmin.getAllowedClusters().size()) {
            // Get the colo(s) being removed from the list
            oldPropertyAdmin.getAllowedClusters().removeAll(newPropertyAdmin.getAllowedClusters());
            log.debug("Following clusters are being removed : [{}]", oldPropertyAdmin.getAllowedClusters());
            for (String cluster : oldPropertyAdmin.getAllowedClusters()) {
                List<String> activeNamespaces = Lists.newArrayList();
                try {
                    activeNamespaces = globalZk().getChildren(path(POLICIES, property, cluster), false);
                    if (activeNamespaces.size() != 0) {
                        // There are active namespaces in this cluster
                        clustersWithActiveNamespaces.add(cluster);
                    }
                } catch (KeeperException.NoNodeException nne) {
                // Fine, some cluster does not have active namespace. Move on!
                }
            }
            if (!clustersWithActiveNamespaces.isEmpty()) {
                // Throw an exception because colos being removed are having active namespaces
                String msg = String.format("Failed to update the property because active namespaces are present in colos %s. Please delete those namespaces first", clustersWithActiveNamespaces);
                throw new RestException(Status.CONFLICT, msg);
            }
        }
        String propertyPath = path(POLICIES, property);
        globalZk().setData(propertyPath, jsonMapper().writeValueAsBytes(newPropertyAdmin), -1);
        globalZkCache().invalidate(propertyPath);
        log.info("[{}] updated property {}", clientAppId(), property);
    } catch (RestException re) {
        throw re;
    } catch (KeeperException.NoNodeException e) {
        log.warn("[{}] Failed to update property {}: does not exist", clientAppId(), property);
        throw new RestException(Status.NOT_FOUND, "Property does not exist");
    } catch (Exception e) {
        log.error("[{}] Failed to update property {}", clientAppId(), property, e);
        throw new RestException(e);
    }
}
Also used : Stat(org.apache.zookeeper.data.Stat) PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) RestException(org.apache.pulsar.broker.web.RestException) KeeperException(org.apache.zookeeper.KeeperException) KeeperException(org.apache.zookeeper.KeeperException) RestException(org.apache.pulsar.broker.web.RestException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with PropertyAdmin

use of org.apache.pulsar.common.policies.data.PropertyAdmin in project incubator-pulsar by apache.

the class PulsarStandaloneStarter method start.

void start() throws Exception {
    if (config == null) {
        System.exit(1);
    }
    log.debug("--- setup PulsarStandaloneStarter ---");
    if (!onlyBroker) {
        // Start LocalBookKeeper
        bkEnsemble = new LocalBookkeeperEnsemble(numOfBk, zkPort, bkPort, zkDir, bkDir, wipeData, config.getAdvertisedAddress());
        bkEnsemble.startStandalone();
    }
    if (noBroker) {
        return;
    }
    // load aspectj-weaver agent for instrumentation
    AgentLoader.loadAgentClass(Agent.class.getName(), null);
    // initialize the functions worker
    if (!noFunctionsWorker) {
        WorkerConfig workerConfig;
        if (isBlank(fnWorkerConfigFile)) {
            workerConfig = new WorkerConfig();
        } else {
            workerConfig = WorkerConfig.load(fnWorkerConfigFile);
        }
        // worker talks to local broker
        workerConfig.setPulsarServiceUrl("pulsar://127.0.0.1:" + config.getBrokerServicePort());
        workerConfig.setPulsarWebServiceUrl("http://127.0.0.1:" + config.getWebServicePort());
        String hostname = ServiceConfigurationUtils.getDefaultOrConfiguredAddress(config.getAdvertisedAddress());
        workerConfig.setWorkerHostname(hostname);
        workerConfig.setWorkerId("c-" + config.getClusterName() + "-fw-" + hostname + "-" + workerConfig.getWorkerPort());
        fnWorkerService = new WorkerService(workerConfig);
    }
    // Start Broker
    broker = new PulsarService(config, Optional.ofNullable(fnWorkerService));
    broker.start();
    // Create a sample namespace
    URL webServiceUrl = new URL(String.format("http://%s:%d", config.getAdvertisedAddress(), config.getWebServicePort()));
    final String brokerServiceUrl = String.format("pulsar://%s:%d", config.getAdvertisedAddress(), config.getBrokerServicePort());
    admin = new PulsarAdmin(webServiceUrl, config.getBrokerClientAuthenticationPlugin(), config.getBrokerClientAuthenticationParameters());
    final String property = "sample";
    final String cluster = config.getClusterName();
    final String globalCluster = "global";
    final String namespace = property + "/" + cluster + "/ns1";
    try {
        ClusterData clusterData = new ClusterData(webServiceUrl.toString(), null, /* serviceUrlTls */
        brokerServiceUrl, null);
        if (!admin.clusters().getClusters().contains(cluster)) {
            admin.clusters().createCluster(cluster, clusterData);
        } else {
            admin.clusters().updateCluster(cluster, clusterData);
        }
        // Create marker for "global" cluster
        if (!admin.clusters().getClusters().contains(globalCluster)) {
            admin.clusters().createCluster(globalCluster, new ClusterData(null, null));
        }
        if (!admin.properties().getProperties().contains(property)) {
            admin.properties().createProperty(property, new PropertyAdmin(Lists.newArrayList(config.getSuperUserRoles()), Sets.newHashSet(cluster)));
        }
        if (!admin.namespaces().getNamespaces(property).contains(namespace)) {
            admin.namespaces().createNamespace(namespace);
        }
    } catch (PulsarAdminException e) {
        log.info(e.getMessage());
    }
    if (null != fnWorkerService) {
        fnWorkerService.start();
    }
    log.debug("--- setup completed ---");
}
Also used : Agent(org.aspectj.weaver.loadtime.Agent) ClusterData(org.apache.pulsar.common.policies.data.ClusterData) PulsarService(org.apache.pulsar.broker.PulsarService) PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) WorkerConfig(org.apache.pulsar.functions.worker.WorkerConfig) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) LocalBookkeeperEnsemble(org.apache.pulsar.zookeeper.LocalBookkeeperEnsemble) WorkerService(org.apache.pulsar.functions.worker.WorkerService) URL(java.net.URL)

Example 3 with PropertyAdmin

use of org.apache.pulsar.common.policies.data.PropertyAdmin in project incubator-pulsar by apache.

the class PatternTopicsConsumerImplTest method testBinaryProtoToGetTopicsOfNamespace.

// verify consumer create success, and works well.
@Test(timeOut = testTimeout)
public void testBinaryProtoToGetTopicsOfNamespace() throws Exception {
    String key = "BinaryProtoToGetTopics";
    String subscriptionName = "my-ex-subscription-" + key;
    String topicName1 = "persistent://prop/use/ns-abc/pattern-topic-1-" + key;
    String topicName2 = "persistent://prop/use/ns-abc/pattern-topic-2-" + key;
    String topicName3 = "persistent://prop/use/ns-abc/pattern-topic-3-" + key;
    Pattern pattern = Pattern.compile("persistent://prop/use/ns-abc/pattern-topic.*");
    // 1. create partition
    admin.properties().createProperty("prop", new PropertyAdmin());
    admin.persistentTopics().createPartitionedTopic(topicName2, 2);
    admin.persistentTopics().createPartitionedTopic(topicName3, 3);
    // 2. create producer
    String messagePredicate = "my-message-" + key + "-";
    int totalMessages = 30;
    Producer<byte[]> producer1 = pulsarClient.newProducer().topic(topicName1).create();
    Producer<byte[]> producer2 = pulsarClient.newProducer().topic(topicName2).messageRoutingMode(org.apache.pulsar.client.api.MessageRoutingMode.RoundRobinPartition).create();
    Producer<byte[]> producer3 = pulsarClient.newProducer().topic(topicName3).messageRoutingMode(org.apache.pulsar.client.api.MessageRoutingMode.RoundRobinPartition).create();
    Consumer<byte[]> consumer = pulsarClient.newConsumer().topicsPattern(pattern).patternAutoDiscoveryPeriod(2).subscriptionName(subscriptionName).subscriptionType(SubscriptionType.Shared).ackTimeout(ackTimeOutMillis, TimeUnit.MILLISECONDS).receiverQueueSize(4).subscribe();
    // 4. verify consumer get methods, to get right number of partitions and topics.
    assertSame(pattern, ((PatternTopicsConsumerImpl<?>) consumer).getPattern());
    List<String> topics = ((PatternTopicsConsumerImpl<?>) consumer).getPartitionedTopics();
    List<ConsumerImpl<byte[]>> consumers = ((PatternTopicsConsumerImpl<byte[]>) consumer).getConsumers();
    assertEquals(topics.size(), 6);
    assertEquals(consumers.size(), 6);
    assertEquals(((PatternTopicsConsumerImpl<?>) consumer).getTopics().size(), 3);
    topics.forEach(topic -> log.debug("topic: {}", topic));
    consumers.forEach(c -> log.debug("consumer: {}", c.getTopic()));
    IntStream.range(0, topics.size()).forEach(index -> assertTrue(topics.get(index).equals(consumers.get(index).getTopic())));
    ((PatternTopicsConsumerImpl<?>) consumer).getTopics().forEach(topic -> log.debug("getTopics topic: {}", topic));
    // 5. produce data
    for (int i = 0; i < totalMessages / 3; i++) {
        producer1.send((messagePredicate + "producer1-" + i).getBytes());
        producer2.send((messagePredicate + "producer2-" + i).getBytes());
        producer3.send((messagePredicate + "producer3-" + i).getBytes());
    }
    // 6. should receive all the message
    int messageSet = 0;
    Message<byte[]> message = consumer.receive();
    do {
        assertTrue(message instanceof TopicMessageImpl);
        messageSet++;
        consumer.acknowledge(message);
        log.debug("Consumer acknowledged : " + new String(message.getData()));
        message = consumer.receive(500, TimeUnit.MILLISECONDS);
    } while (message != null);
    assertEquals(messageSet, totalMessages);
    consumer.unsubscribe();
    consumer.close();
    producer1.close();
    producer2.close();
    producer3.close();
}
Also used : Pattern(java.util.regex.Pattern) PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) Test(org.testng.annotations.Test)

Example 4 with PropertyAdmin

use of org.apache.pulsar.common.policies.data.PropertyAdmin in project incubator-pulsar by apache.

the class PatternTopicsConsumerImplTest method testStartEmptyPatternConsumer.

// simulate subscribe a pattern which has no topics, but then matched topics added in.
@Test(timeOut = testTimeout)
public void testStartEmptyPatternConsumer() throws Exception {
    String key = "StartEmptyPatternConsumerTest";
    String subscriptionName = "my-ex-subscription-" + key;
    String topicName1 = "persistent://prop/use/ns-abc/pattern-topic-1-" + key;
    String topicName2 = "persistent://prop/use/ns-abc/pattern-topic-2-" + key;
    String topicName3 = "persistent://prop/use/ns-abc/pattern-topic-3-" + key;
    Pattern pattern = Pattern.compile("persistent://prop/use/ns-abc/pattern-topic.*");
    // 1. create partition
    admin.properties().createProperty("prop", new PropertyAdmin());
    admin.persistentTopics().createPartitionedTopic(topicName2, 2);
    admin.persistentTopics().createPartitionedTopic(topicName3, 3);
    // 2. Create consumer, this should success, but with empty sub-consumser internal
    Consumer<byte[]> consumer = pulsarClient.newConsumer().topicsPattern(pattern).patternAutoDiscoveryPeriod(2).subscriptionName(subscriptionName).subscriptionType(SubscriptionType.Shared).ackTimeout(ackTimeOutMillis, TimeUnit.MILLISECONDS).receiverQueueSize(4).subscribe();
    // 3. verify consumer get methods, to get 0 number of partitions and topics.
    assertSame(pattern, ((PatternTopicsConsumerImpl<?>) consumer).getPattern());
    assertEquals(((PatternTopicsConsumerImpl<?>) consumer).getPartitionedTopics().size(), 0);
    assertEquals(((PatternTopicsConsumerImpl<?>) consumer).getConsumers().size(), 0);
    assertEquals(((PatternTopicsConsumerImpl<?>) consumer).getTopics().size(), 0);
    // 4. create producer
    String messagePredicate = "my-message-" + key + "-";
    int totalMessages = 30;
    Producer<byte[]> producer1 = pulsarClient.newProducer().topic(topicName1).create();
    Producer<byte[]> producer2 = pulsarClient.newProducer().topic(topicName2).messageRoutingMode(org.apache.pulsar.client.api.MessageRoutingMode.RoundRobinPartition).create();
    Producer<byte[]> producer3 = pulsarClient.newProducer().topic(topicName3).messageRoutingMode(org.apache.pulsar.client.api.MessageRoutingMode.RoundRobinPartition).create();
    // 5. call recheckTopics to subscribe each added topics above
    log.debug("recheck topics change");
    PatternTopicsConsumerImpl<byte[]> consumer1 = ((PatternTopicsConsumerImpl<byte[]>) consumer);
    consumer1.run(consumer1.getRecheckPatternTimeout());
    Thread.sleep(100);
    // 6. verify consumer get methods, to get number of partitions and topics, value 6=1+2+3.
    assertSame(pattern, ((PatternTopicsConsumerImpl<?>) consumer).getPattern());
    assertEquals(((PatternTopicsConsumerImpl<?>) consumer).getPartitionedTopics().size(), 6);
    assertEquals(((PatternTopicsConsumerImpl<?>) consumer).getConsumers().size(), 6);
    assertEquals(((PatternTopicsConsumerImpl<?>) consumer).getTopics().size(), 3);
    // 7. produce data
    for (int i = 0; i < totalMessages / 3; i++) {
        producer1.send((messagePredicate + "producer1-" + i).getBytes());
        producer2.send((messagePredicate + "producer2-" + i).getBytes());
        producer3.send((messagePredicate + "producer3-" + i).getBytes());
    }
    // 8. should receive all the message
    int messageSet = 0;
    Message<byte[]> message = consumer.receive();
    do {
        assertTrue(message instanceof TopicMessageImpl);
        messageSet++;
        consumer.acknowledge(message);
        log.debug("Consumer acknowledged : " + new String(message.getData()));
        message = consumer.receive(500, TimeUnit.MILLISECONDS);
    } while (message != null);
    assertEquals(messageSet, totalMessages);
    consumer.unsubscribe();
    consumer.close();
    producer1.close();
    producer2.close();
    producer3.close();
}
Also used : Pattern(java.util.regex.Pattern) PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) Test(org.testng.annotations.Test)

Example 5 with PropertyAdmin

use of org.apache.pulsar.common.policies.data.PropertyAdmin in project incubator-pulsar by apache.

the class PatternTopicsConsumerImplTest method testPatternTopicsSubscribeWithBuilderFail.

@Test(timeOut = testTimeout)
public void testPatternTopicsSubscribeWithBuilderFail() throws Exception {
    String key = "PatternTopicsSubscribeWithBuilderFail";
    final String subscriptionName = "my-ex-subscription-" + key;
    final String topicName1 = "persistent://prop/use/ns-abc/topic-1-" + key;
    final String topicName2 = "persistent://prop/use/ns-abc/topic-2-" + key;
    final String topicName3 = "persistent://prop/use/ns-abc/topic-3-" + key;
    List<String> topicNames = Lists.newArrayList(topicName1, topicName2, topicName3);
    final String patternString = "persistent://prop/use/ns-abc/pattern-topic.*";
    Pattern pattern = Pattern.compile(patternString);
    admin.properties().createProperty("prop", new PropertyAdmin());
    admin.persistentTopics().createPartitionedTopic(topicName2, 2);
    admin.persistentTopics().createPartitionedTopic(topicName3, 3);
    // test failing builder with pattern and topic should fail
    try {
        pulsarClient.newConsumer().topicsPattern(pattern).topic(topicName1).subscriptionName(subscriptionName).subscriptionType(SubscriptionType.Shared).ackTimeout(ackTimeOutMillis, TimeUnit.MILLISECONDS).subscribe();
        fail("subscribe1 with pattern and topic should fail.");
    } catch (PulsarClientException e) {
    // expected
    }
    // test failing builder with pattern and topics should fail
    try {
        pulsarClient.newConsumer().topicsPattern(pattern).topics(topicNames).subscriptionName(subscriptionName).subscriptionType(SubscriptionType.Shared).ackTimeout(ackTimeOutMillis, TimeUnit.MILLISECONDS).subscribe();
        fail("subscribe2 with pattern and topics should fail.");
    } catch (PulsarClientException e) {
    // expected
    }
    // test failing builder with pattern and patternString should fail
    try {
        pulsarClient.newConsumer().topicsPattern(pattern).topicsPattern(patternString).subscriptionName(subscriptionName).subscriptionType(SubscriptionType.Shared).ackTimeout(ackTimeOutMillis, TimeUnit.MILLISECONDS).subscribe();
        fail("subscribe3 with pattern and patternString should fail.");
    } catch (IllegalArgumentException e) {
    // expected
    }
}
Also used : Pattern(java.util.regex.Pattern) PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) Test(org.testng.annotations.Test)

Aggregations

PropertyAdmin (org.apache.pulsar.common.policies.data.PropertyAdmin)83 Test (org.testng.annotations.Test)60 ClusterData (org.apache.pulsar.common.policies.data.ClusterData)29 MockedPulsarServiceBaseTest (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)13 PulsarClient (org.apache.pulsar.client.api.PulsarClient)12 BeforeMethod (org.testng.annotations.BeforeMethod)12 PulsarAdmin (org.apache.pulsar.client.admin.PulsarAdmin)11 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)9 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)9 AuthenticationTls (org.apache.pulsar.client.impl.auth.AuthenticationTls)8 HashSet (java.util.HashSet)6 URI (java.net.URI)5 URL (java.net.URL)5 Pattern (java.util.regex.Pattern)5 PulsarService (org.apache.pulsar.broker.PulsarService)5 RestException (org.apache.pulsar.broker.web.RestException)5 Authentication (org.apache.pulsar.client.api.Authentication)5 AuthAction (org.apache.pulsar.common.policies.data.AuthAction)5 KeeperException (org.apache.zookeeper.KeeperException)5 PulsarServerException (org.apache.pulsar.broker.PulsarServerException)4