org.apache.pulsar.client.admin.PulsarAdminException.ConflictException Java Examples

The following examples show how to use org.apache.pulsar.client.admin.PulsarAdminException.ConflictException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KafkaProtocolHandler.java    From kop with Apache License 2.0 5 votes vote down vote up
private String createKafkaOffsetsTopic(BrokerService service) throws PulsarServerException, PulsarAdminException {
    String offsetsTopic = kafkaConfig.getKafkaMetadataTenant() + "/" + kafkaConfig.getKafkaMetadataNamespace()
        + "/" + Topic.GROUP_METADATA_TOPIC_NAME;

    PartitionedTopicMetadata offsetsTopicMetadata =
        service.pulsar().getAdminClient().topics().getPartitionedTopicMetadata(offsetsTopic);
    if (offsetsTopicMetadata.partitions <= 0) {
        log.info("Kafka group metadata topic {} doesn't exist. Creating it ...",
                offsetsTopic);
        try {
            service.pulsar().getAdminClient().topics().createPartitionedTopic(
                    offsetsTopic,
                    kafkaConfig.getOffsetsTopicNumPartitions()
            );

            for (int i = 0; i < kafkaConfig.getOffsetsTopicNumPartitions(); i++) {
                service.pulsar().getAdminClient().topics()
                        .createNonPartitionedTopic(offsetsTopic + PARTITIONED_TOPIC_SUFFIX + i);
            }
        } catch (ConflictException e) {
            log.info("Topic {} concurrent creating and cause e: ", offsetsTopic, e);
            return offsetsTopic;
        }

        log.info("Successfully created group metadata topic {} with {} partitions.",
                offsetsTopic, kafkaConfig.getOffsetsTopicNumPartitions());
    }

    return offsetsTopic;
}
 
Example #2
Source File: V1_AdminApiTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testTriggerCompaction() throws Exception {
    String topicName = "persistent://prop-xyz/use/ns1/topic1";

    // create a topic by creating a producer
    pulsarClient.newProducer(Schema.BYTES).topic(topicName).create().close();
    assertNotNull(pulsar.getBrokerService().getTopicReference(topicName));

    // mock actual compaction, we don't need to really run it
    CompletableFuture<Long> promise = new CompletableFuture<Long>();
    Compactor compactor = pulsar.getCompactor();
    doReturn(promise).when(compactor).compact(topicName);
    admin.topics().triggerCompaction(topicName);

    // verify compact called once
    verify(compactor).compact(topicName);
    try {
        admin.topics().triggerCompaction(topicName);

        fail("Shouldn't be able to run while already running");
    } catch (ConflictException e) {
        // expected
    }
    // compact shouldn't have been called again
    verify(compactor).compact(topicName);

    // complete first compaction, and trigger again
    promise.complete(1L);
    admin.topics().triggerCompaction(topicName);

    // verify compact was called again
    verify(compactor, times(2)).compact(topicName);
}
 
Example #3
Source File: AdminApiTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testTriggerCompaction() throws Exception {
    String topicName = "persistent://prop-xyz/ns1/topic1";

    // create a topic by creating a producer
    pulsarClient.newProducer(Schema.BYTES).topic(topicName).create().close();
    assertNotNull(pulsar.getBrokerService().getTopicReference(topicName));

    // mock actual compaction, we don't need to really run it
    CompletableFuture<Long> promise = new CompletableFuture<Long>();
    Compactor compactor = pulsar.getCompactor();
    doReturn(promise).when(compactor).compact(topicName);
    admin.topics().triggerCompaction(topicName);

    // verify compact called once
    verify(compactor).compact(topicName);
    try {
        admin.topics().triggerCompaction(topicName);

        fail("Shouldn't be able to run while already running");
    } catch (ConflictException e) {
        // expected
    }
    // compact shouldn't have been called again
    verify(compactor).compact(topicName);

    // complete first compaction, and trigger again
    promise.complete(1L);
    admin.topics().triggerCompaction(topicName);

    // verify compact was called again
    verify(compactor, times(2)).compact(topicName);
}
 
Example #4
Source File: CreateSubscriptionTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void createSubscriptionSingleTopic() throws Exception {
    String topic = "persistent://my-property/my-ns/my-topic";
    admin.topics().createSubscription(topic, "sub-1", MessageId.latest);

    // Create should fail if the subscription already exists
    try {
        admin.topics().createSubscription(topic, "sub-1", MessageId.latest);
        fail("Should have failed");
    } catch (ConflictException e) {
        assertEquals(((ClientErrorException) e.getCause()).getResponse().getStatus(),
                Status.CONFLICT.getStatusCode());
    }

    assertEquals(admin.topics().getSubscriptions(topic), Lists.newArrayList("sub-1"));

    Producer<byte[]> p1 = pulsarClient.newProducer().topic(topic).create();
    p1.send("test-1".getBytes());
    p1.send("test-2".getBytes());
    MessageId m3 = p1.send("test-3".getBytes());

    assertEquals(admin.topics().getStats(topic).subscriptions.get("sub-1").msgBacklog, 3);

    admin.topics().createSubscription(topic, "sub-2", MessageId.latest);
    assertEquals(admin.topics().getStats(topic).subscriptions.get("sub-2").msgBacklog, 0);

    admin.topics().createSubscription(topic, "sub-3", MessageId.earliest);
    assertEquals(admin.topics().getStats(topic).subscriptions.get("sub-3").msgBacklog, 3);

    admin.topics().createSubscription(topic, "sub-5", m3);
    assertEquals(admin.topics().getStats(topic).subscriptions.get("sub-5").msgBacklog, 1);
}
 
Example #5
Source File: BaseResource.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public PulsarAdminException getApiException(Throwable e) {
    if (e instanceof PulsarAdminException) {
        return (PulsarAdminException) e;
    } else if (e instanceof ServiceUnavailableException) {
        if (e.getCause() instanceof java.net.ConnectException) {
            return new ConnectException(e.getCause());
        } else {
            return new PulsarAdminException((ServerErrorException) e);
        }
    } else if (e instanceof WebApplicationException) {
        // Handle 5xx exceptions
        if (e instanceof ServerErrorException) {
            ServerErrorException see = (ServerErrorException) e;
            return new ServerSideErrorException(see, e.getMessage());
        } else if (e instanceof ClientErrorException) {
            // Handle 4xx exceptions
            ClientErrorException cee = (ClientErrorException) e;
            int statusCode = cee.getResponse().getStatus();
            switch (statusCode) {
                case 401:
                case 403:
                    return new NotAuthorizedException(cee);
                case 404:
                    return new NotFoundException(cee);
                case 405:
                    return new NotAllowedException(cee);
                case 409:
                    return new ConflictException(cee);
                case 412:
                    return new PreconditionFailedException(cee);
                default:
                    return new PulsarAdminException(cee);
            }
        } else {
            return new PulsarAdminException((WebApplicationException) e);
        }
    } else {
        return new PulsarAdminException(e);
    }
}
 
Example #6
Source File: WebServiceTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
private void setupEnv(boolean enableFilter, String minApiVersion, boolean allowUnversionedClients,
        boolean enableTls, boolean enableAuth, boolean allowInsecure) throws Exception {
    Set<String> providers = new HashSet<>();
    providers.add("org.apache.pulsar.broker.authentication.AuthenticationProviderTls");

    Set<String> roles = new HashSet<>();
    roles.add("client");

    ServiceConfiguration config = new ServiceConfiguration();
    config.setAdvertisedAddress("localhost");
    config.setBrokerServicePort(Optional.of(0));
    config.setWebServicePort(Optional.of(0));
    if (enableTls) {
        config.setWebServicePortTls(Optional.of(0));
    }
    config.setClientLibraryVersionCheckEnabled(enableFilter);
    config.setAuthenticationEnabled(enableAuth);
    config.setAuthenticationProviders(providers);
    config.setAuthorizationEnabled(false);
    config.setSuperUserRoles(roles);
    config.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
    config.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
    config.setTlsAllowInsecureConnection(allowInsecure);
    config.setTlsTrustCertsFilePath(allowInsecure ? "" : TLS_CLIENT_CERT_FILE_PATH);
    config.setClusterName("local");
    config.setAdvertisedAddress("localhost"); // TLS certificate expects localhost
    config.setZookeeperServers("localhost:2181");
    config.setHttpMaxRequestSize(10 * 1024);
    pulsar = spy(new PulsarService(config));
    doReturn(zkFactory).when(pulsar).getZooKeeperClientFactory();
    doReturn(new MockedBookKeeperClientFactory()).when(pulsar).newBookKeeperClientFactory();
    pulsar.start();

    try {
        pulsar.getZkClient().delete("/minApiVersion", -1);
    } catch (Exception ex) {
    }
    pulsar.getZkClient().create("/minApiVersion", minApiVersion.getBytes(), null, CreateMode.PERSISTENT);

    String BROKER_URL_BASE = "http://localhost:" + pulsar.getListenPortHTTP().get();
    String BROKER_URL_BASE_TLS = "https://localhost:" + pulsar.getListenPortHTTPS().orElse(-1);
    String serviceUrl = BROKER_URL_BASE;

    PulsarAdminBuilder adminBuilder = PulsarAdmin.builder();
    if (enableTls && enableAuth) {
        serviceUrl = BROKER_URL_BASE_TLS;

        Map<String, String> authParams = new HashMap<>();
        authParams.put("tlsCertFile", TLS_CLIENT_CERT_FILE_PATH);
        authParams.put("tlsKeyFile", TLS_CLIENT_KEY_FILE_PATH);

        adminBuilder.authentication(AuthenticationTls.class.getName(), authParams).allowTlsInsecureConnection(true);
    }

    BROKER_LOOKUP_URL = BROKER_URL_BASE
            + "/lookup/v2/destination/persistent/my-property/local/my-namespace/my-topic";
    BROKER_LOOKUP_URL_TLS = BROKER_URL_BASE_TLS
            + "/lookup/v2/destination/persistent/my-property/local/my-namespace/my-topic";

    PulsarAdmin pulsarAdmin = adminBuilder.serviceHttpUrl(serviceUrl).build();

    try {
        pulsarAdmin.clusters().createCluster(config.getClusterName(),
                new ClusterData(pulsar.getSafeWebServiceAddress()));
    } catch (ConflictException ce) {
        // This is OK.
    } finally {
        pulsarAdmin.close();
    }
}
 
Example #7
Source File: AdminApiOffloadTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
private void testOffload(String topicName, String mlName) throws Exception {
    LedgerOffloader offloader = mock(LedgerOffloader.class);
    when(offloader.getOffloadDriverName()).thenReturn("mock");

    doReturn(offloader).when(pulsar).getManagedLedgerOffloader(any(), any());

    CompletableFuture<Void> promise = new CompletableFuture<>();
    doReturn(promise).when(offloader).offload(any(), any(), any());

    MessageId currentId = MessageId.latest;
    try (Producer<byte[]> p = pulsarClient.newProducer().topic(topicName).enableBatching(false).create()) {
        for (int i = 0; i < 15; i++) {
            currentId = p.send("Foobar".getBytes());
        }
    }

    ManagedLedgerInfo info = pulsar.getManagedLedgerFactory().getManagedLedgerInfo(mlName);
    Assert.assertEquals(info.ledgers.size(), 2);

    Assert.assertEquals(admin.topics().offloadStatus(topicName).status,
                        LongRunningProcessStatus.Status.NOT_RUN);

    admin.topics().triggerOffload(topicName, currentId);

    Assert.assertEquals(admin.topics().offloadStatus(topicName).status,
                        LongRunningProcessStatus.Status.RUNNING);

    try {
        admin.topics().triggerOffload(topicName, currentId);
        Assert.fail("Should have failed");
    } catch (ConflictException e) {
        // expected
    }

    // fail first time
    promise.completeExceptionally(new Exception("Some random failure"));

    Assert.assertEquals(admin.topics().offloadStatus(topicName).status,
                        LongRunningProcessStatus.Status.ERROR);
    Assert.assertTrue(admin.topics().offloadStatus(topicName).lastError.contains("Some random failure"));

    // Try again
    doReturn(CompletableFuture.completedFuture(null))
        .when(offloader).offload(any(), any(), any());

    admin.topics().triggerOffload(topicName, currentId);

    Assert.assertEquals(admin.topics().offloadStatus(topicName).status,
                        LongRunningProcessStatus.Status.SUCCESS);
    MessageIdImpl firstUnoffloaded = admin.topics().offloadStatus(topicName).firstUnoffloadedMessage;
    // First unoffloaded is the first entry of current ledger
    Assert.assertEquals(firstUnoffloaded.getLedgerId(), info.ledgers.get(1).ledgerId);
    Assert.assertEquals(firstUnoffloaded.getEntryId(), 0);

    verify(offloader, times(2)).offload(any(), any(), any());
}