org.apache.pulsar.common.util.FutureUtil Java Examples

The following examples show how to use org.apache.pulsar.common.util.FutureUtil. 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: ZooKeeperCache.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
public CompletableFuture<Boolean> existsAsync(String path, Watcher watcher) {
    return existsCache.get(path, (p, executor) -> {
        ZooKeeper zk = zkSession.get();
        if (zk == null) {
            return FutureUtil.failedFuture(new IOException("ZK session not ready"));
        }

        CompletableFuture<Boolean> future = new CompletableFuture<>();
        zk.exists(path, watcher, (rc, path1, ctx, stat) -> {
            if (rc == Code.OK.intValue()) {
                future.complete(true);
            } else if (rc == Code.NONODE.intValue()) {
                future.complete(false);
            } else {
                future.completeExceptionally(KeeperException.create(rc));
            }
        }, null);

        return future;
    });
}
 
Example #2
Source File: MultiTopicsConsumerImpl.java    From pulsar with Apache License 2.0 6 votes vote down vote up
private CompletableFuture<Void> subscribeAsync(String topicName, int numberPartitions) {
    if (!topicNameValid(topicName)) {
        return FutureUtil.failedFuture(
            new PulsarClientException.AlreadyClosedException("Topic name not valid"));
    }

    if (getState() == State.Closing || getState() == State.Closed) {
        return FutureUtil.failedFuture(
            new PulsarClientException.AlreadyClosedException("Topics Consumer was already closed"));
    }

    CompletableFuture<Void> subscribeResult = new CompletableFuture<>();
    subscribeTopicPartitions(subscribeResult, topicName, numberPartitions, true /* createTopicIfDoesNotExist */);

    return subscribeResult;
}
 
Example #3
Source File: PatternMultiTopicsConsumerImpl.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<Void> onTopicsRemoved(Collection<String> removedTopics) {
    CompletableFuture<Void> removeFuture = new CompletableFuture<>();

    if (removedTopics.isEmpty()) {
        removeFuture.complete(null);
        return removeFuture;
    }

    List<CompletableFuture<Void>> futures = Lists.newArrayListWithExpectedSize(topics.size());
    removedTopics.stream().forEach(topic -> futures.add(removeConsumerAsync(topic)));
    FutureUtil.waitForAll(futures)
        .thenAccept(finalFuture -> removeFuture.complete(null))
        .exceptionally(ex -> {
            log.warn("[{}] Failed to subscribe topics: {}", topic, ex.getMessage());
            removeFuture.completeExceptionally(ex);
        return null;
    });
    return removeFuture;
}
 
Example #4
Source File: AuthorizationService.java    From pulsar with Apache License 2.0 6 votes vote down vote up
/**
 * Check if the specified role has permission to receive messages from the specified fully qualified topic name.
 *
 * @param topicName
 *            the fully qualified topic name associated with the topic.
 * @param role
 *            the app id used to receive messages from the topic.
 * @param subscription
 *            the subscription name defined by the client
 */
public CompletableFuture<Boolean> canConsumeAsync(TopicName topicName, String role,
        AuthenticationDataSource authenticationData, String subscription) {
    if (!this.conf.isAuthorizationEnabled()) {
        return CompletableFuture.completedFuture(true);
    }
    if (provider != null) {
        return provider.isSuperUser(role, authenticationData, conf).thenComposeAsync(isSuperUser -> {
            if (isSuperUser) {
                return CompletableFuture.completedFuture(true);
            } else {
                return provider.canConsumeAsync(topicName, role, authenticationData, subscription);
            }
        });
    }
    return FutureUtil.failedFuture(new IllegalStateException("No authorization provider configured"));
}
 
Example #5
Source File: AuthorizationService.java    From pulsar with Apache License 2.0 6 votes vote down vote up
/**
 * Grant authorization-action permission on a namespace to the given client
 *
 * @param namespaceName
 * @param operation
 * @param originalRole
 * @param role
 * @param authData
 *            additional authdata in json for targeted authorization provider
 * @return IllegalArgumentException when namespace not found
 * @throws IllegalStateException
 *             when failed to grant permission
 */
public CompletableFuture<Boolean> allowNamespaceOperationAsync(NamespaceName namespaceName,
                                                               NamespaceOperation operation,
                                                               String originalRole, String role,
                                                               AuthenticationDataSource authData) {
    if (!this.conf.isAuthorizationEnabled()) {
        return CompletableFuture.completedFuture(true);
    }

    if (provider != null) {
        return provider.allowNamespaceOperationAsync(namespaceName, originalRole, role, operation, authData);
    }

    return FutureUtil.failedFuture(new IllegalStateException("No authorization provider configured for " +
            "allowNamespaceOperationAsync"));
}
 
Example #6
Source File: PulsarClientImpl.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public CompletableFuture<PartitionedTopicMetadata> getPartitionedTopicMetadata(String topic) {

        CompletableFuture<PartitionedTopicMetadata> metadataFuture = new CompletableFuture<>();

        try {
            TopicName topicName = TopicName.get(topic);
            AtomicLong opTimeoutMs = new AtomicLong(conf.getOperationTimeoutMs());
            Backoff backoff = new BackoffBuilder()
                    .setInitialTime(100, TimeUnit.MILLISECONDS)
                    .setMandatoryStop(opTimeoutMs.get() * 2, TimeUnit.MILLISECONDS)
                    .setMax(1, TimeUnit.MINUTES)
                    .create();
            getPartitionedTopicMetadata(topicName, backoff, opTimeoutMs, metadataFuture);
        } catch (IllegalArgumentException e) {
            return FutureUtil.failedFuture(new PulsarClientException.InvalidConfigurationException(e.getMessage()));
        }
        return metadataFuture;
    }
 
Example #7
Source File: PulsarKafkaConsumer.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public void seekToEnd(TopicPartition... partitions) {
    List<CompletableFuture<Void>> futures = new ArrayList<>();

    if (partitions.length == 0) {
        partitions = consumers.keySet().toArray(new TopicPartition[0]);
    }
    lastCommittedOffset.clear();
    lastReceivedOffset.clear();

    for (TopicPartition tp : partitions) {
        org.apache.pulsar.client.api.Consumer<byte[]> c = consumers.get(tp);
        if (c == null) {
            futures.add(FutureUtil.failedFuture(
                    new IllegalArgumentException("Cannot seek on a partition where we are not subscribed")));
        } else {
            futures.add(c.seekAsync(MessageId.latest));
        }
    }

    FutureUtil.waitForAll(futures).join();
}
 
Example #8
Source File: TwoPhaseCompactor.java    From pulsar with Apache License 2.0 6 votes vote down vote up
private CompletableFuture<LedgerHandle> createLedger(BookKeeper bk, Map<String,byte[]> metadata) {
    CompletableFuture<LedgerHandle> bkf = new CompletableFuture<>();

    try {
        bk.asyncCreateLedger(conf.getManagedLedgerDefaultEnsembleSize(),
                conf.getManagedLedgerDefaultWriteQuorum(),
                conf.getManagedLedgerDefaultAckQuorum(),
                Compactor.COMPACTED_TOPIC_LEDGER_DIGEST_TYPE,
                Compactor.COMPACTED_TOPIC_LEDGER_PASSWORD,
                (rc, ledger, ctx) -> {
                    if (rc != BKException.Code.OK) {
                        bkf.completeExceptionally(BKException.create(rc));
                    } else {
                        bkf.complete(ledger);
                    }
                }, null, metadata);
    } catch (Throwable t) {
        log.error("Encountered unexpected error when creating compaction ledger", t);
        return FutureUtil.failedFuture(t);
    }
    return bkf;
}
 
Example #9
Source File: PatternMultiTopicsConsumerImpl.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<Void> onTopicsAdded(Collection<String> addedTopics) {
    CompletableFuture<Void> addFuture = new CompletableFuture<>();

    if (addedTopics.isEmpty()) {
        addFuture.complete(null);
        return addFuture;
    }

    List<CompletableFuture<Void>> futures = Lists.newArrayListWithExpectedSize(topics.size());
    addedTopics.stream().forEach(topic -> futures.add(subscribeAsync(topic, false /* createTopicIfDoesNotExist */)));
    FutureUtil.waitForAll(futures)
        .thenAccept(finalFuture -> addFuture.complete(null))
        .exceptionally(ex -> {
            log.warn("[{}] Failed to unsubscribe topics: {}", topic, ex.getMessage());
            addFuture.completeExceptionally(ex);
            return null;
        });
    return addFuture;
}
 
Example #10
Source File: AbstractTopic.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<SchemaVersion> addSchema(SchemaData schema) {
    if (schema == null) {
        return CompletableFuture.completedFuture(SchemaVersion.Empty);
    }

    String base = TopicName.get(getName()).getPartitionedTopicName();
    String id = TopicName.get(base).getSchemaName();
    SchemaRegistryService schemaRegistryService = brokerService.pulsar().getSchemaRegistryService();
    return isAllowAutoUpdateSchema ? schemaRegistryService
            .putSchemaIfAbsent(id, schema, schemaCompatibilityStrategy)
            : schemaRegistryService.trimDeletedSchemaAndGetList(id).thenCompose(schemaAndMetadataList ->
            schemaRegistryService.getSchemaVersionBySchemaData(schemaAndMetadataList, schema)
                    .thenCompose(schemaVersion -> {
                if (schemaVersion == null) {
                    return FutureUtil
                            .failedFuture(
                                    new IncompatibleSchemaException(
                                            "Schema not found and schema auto updating is disabled."));
                } else {
                    return CompletableFuture.completedFuture(schemaVersion);
                }
            }));
}
 
Example #11
Source File: FunctionCommonTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testValidateLocalFileUrl() throws Exception {
    String fileLocation = FutureUtil.class.getProtectionDomain().getCodeSource().getLocation().getPath();
    try {
        // eg: fileLocation : /dir/fileName.jar (invalid)
        FunctionCommon.extractClassLoader(fileLocation);
        Assert.fail("should fail with invalid url: without protocol");
    } catch (IllegalArgumentException ie) {
        // Ok.. expected exception
    }
    String fileLocationWithProtocol = "file://" + fileLocation;
    // eg: fileLocation : file:///dir/fileName.jar (valid)
    FunctionCommon.extractClassLoader(fileLocationWithProtocol);
    // eg: fileLocation : file:/dir/fileName.jar (valid)
    fileLocationWithProtocol = "file:" + fileLocation;
    FunctionCommon.extractClassLoader(fileLocationWithProtocol);
}
 
Example #12
Source File: BookkeeperSchemaStorage.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@NotNull
private CompletableFuture<LedgerHandle> createLedger(String schemaId) {
    Map<String, byte[]> metadata = LedgerMetadataUtils.buildMetadataForSchema(schemaId);
    final CompletableFuture<LedgerHandle> future = new CompletableFuture<>();
    try {
        bookKeeper.asyncCreateLedger(
                config.getManagedLedgerDefaultEnsembleSize(),
                config.getManagedLedgerDefaultWriteQuorum(),
                config.getManagedLedgerDefaultAckQuorum(),
                BookKeeper.DigestType.fromApiDigestType(config.getManagedLedgerDigestType()),
                LedgerPassword,
                (rc, handle, ctx) -> {
                    if (rc != BKException.Code.OK) {
                        future.completeExceptionally(bkException("Failed to create ledger", rc, -1, -1));
                    } else {
                        future.complete(handle);
                    }
                }, null, metadata);
    } catch (Throwable t) {
        log.error("[{}] Encountered unexpected error when creating schema ledger", schemaId, t);
        return FutureUtil.failedFuture(t);
    }
    return future;
}
 
Example #13
Source File: BatchMessageTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "containerBuilder")
public void testRetrieveSequenceIdSpecify(BatcherBuilder builder) throws Exception {

    int numMsgs = 10;
    final String topicName = "persistent://prop/ns-abc/testRetrieveSequenceIdSpecify-" + UUID.randomUUID();
    final String subscriptionName = "sub-1";

    Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName(subscriptionName)
            .subscriptionType(SubscriptionType.Shared).subscribe();

    Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName)
            .batchingMaxPublishDelay(5, TimeUnit.SECONDS).batchingMaxMessages(numMsgs).enableBatching(true)
            .batcherBuilder(builder)
            .create();

    List<CompletableFuture<MessageId>> sendFutureList = Lists.newArrayList();
    for (int i = 0; i < numMsgs; i++) {
        byte[] message = ("my-message-" + i).getBytes();
        sendFutureList.add(producer.newMessage().sequenceId(i + 100).value(message).sendAsync());
    }
    FutureUtil.waitForAll(sendFutureList).get();

    for (int i = 0; i < numMsgs; i++) {
        Message<byte[]> received = consumer.receive();
        Assert.assertEquals(received.getSequenceId(), i + 100);
        consumer.acknowledge(received);
    }

    producer.close();
    consumer.close();
}
 
Example #14
Source File: ConsumerBase.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Messages<T>> batchReceiveAsync() {
    try {
        verifyBatchReceive();
        verifyConsumerState();
        return internalBatchReceiveAsync();
    } catch (PulsarClientException e) {
        return FutureUtil.failedFuture(e);
    }
}
 
Example #15
Source File: TransactionMetadataStoreService.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public CompletableFuture<Void> addAckedPartitionToTxn(TxnID txnId, List<String> partitions) {
    TransactionCoordinatorID tcId = getTcIdFromTxnId(txnId);
    TransactionMetadataStore store = stores.get(tcId);
    if (store == null) {
        return FutureUtil.failedFuture(new CoordinatorNotFoundException(tcId));
    }
    return store.addAckedPartitionToTxn(txnId, partitions);
}
 
Example #16
Source File: TransactionCoordinatorClientImpl.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Void> addPublishPartitionToTxnAsync(TxnID txnID, List<String> partitions) {
    TransactionMetaStoreHandler handler = handlerMap.get(txnID.getMostSigBits());
    if (handler == null) {
        return FutureUtil.failedFuture(
                new TransactionCoordinatorClientException.MetaStoreHandlerNotExistsException(txnID.getMostSigBits()));
    }
    return handler.addPublishPartitionToTxnAsync(txnID, partitions);
}
 
Example #17
Source File: ConsumerBase.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Void> reconsumeLaterAsync(Messages<?> messages, long delayTime, TimeUnit unit) {
    try {
        messages.forEach(message -> reconsumeLaterAsync(message,delayTime, unit));
        return CompletableFuture.completedFuture(null);
    } catch (NullPointerException npe) {
        return FutureUtil.failedFuture(new PulsarClientException.InvalidMessageException(npe.getMessage()));
    }
}
 
Example #18
Source File: ContextImpl.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public <O> CompletableFuture<Void> publish(String topicName, O object, Schema<O> schema) {
    try {
        return newOutputMessage(topicName, schema).value(object).sendAsync().thenApply(msgId -> null);
    } catch (PulsarClientException e) {
        logger.error("Failed to create Producer while doing user publish", e);
        return FutureUtil.failedFuture(e);
    }
}
 
Example #19
Source File: TransactionMetadataStoreService.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public CompletableFuture<TxnID> newTransaction(TransactionCoordinatorID tcId) {
    TransactionMetadataStore store = stores.get(tcId);
    if (store == null) {
        return FutureUtil.failedFuture(new CoordinatorNotFoundException(tcId));
    }
    return store.newTransaction();
}
 
Example #20
Source File: ConsumerImpl.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Void> unsubscribeAsync() {
    if (getState() == State.Closing || getState() == State.Closed) {
        return FutureUtil
                .failedFuture(new PulsarClientException.AlreadyClosedException("Consumer was already closed"));
    }
    final CompletableFuture<Void> unsubscribeFuture = new CompletableFuture<>();
    if (isConnected()) {
        setState(State.Closing);
        long requestId = client.newRequestId();
        ByteBuf unsubscribe = Commands.newUnsubscribe(consumerId, requestId);
        ClientCnx cnx = cnx();
        cnx.sendRequestWithId(unsubscribe, requestId).thenRun(() -> {
            cnx.removeConsumer(consumerId);
            unAckedMessageTracker.close();
            if (possibleSendToDeadLetterTopicMessages != null) {
                possibleSendToDeadLetterTopicMessages.clear();
            }
            client.cleanupConsumer(ConsumerImpl.this);
            log.info("[{}][{}] Successfully unsubscribed from topic", topic, subscription);
            setState(State.Closed);
            unsubscribeFuture.complete(null);
        }).exceptionally(e -> {
            log.error("[{}][{}] Failed to unsubscribe: {}", topic, subscription, e.getCause().getMessage());
            setState(State.Ready);
            unsubscribeFuture.completeExceptionally(
                PulsarClientException.wrap(e.getCause(),
                    String.format("Failed to unsubscribe the subscription %s of topic %s",
                        topicName.toString(), subscription)));
            return null;
        });
    } else {
        unsubscribeFuture.completeExceptionally(
            new PulsarClientException(
                String.format("The client is not connected to the broker when unsubscribing the " +
                    "subscription %s of the topic %s", subscription, topicName.toString())));
    }
    return unsubscribeFuture;
}
 
Example #21
Source File: PersistentTopicsBase.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private CompletableFuture<Void> updatePartitionInOtherCluster(int numPartitions, Set<String> clusters) {
    List<CompletableFuture<Void>> results = new ArrayList<>(clusters.size() -1);
    clusters.forEach(cluster -> {
        if (cluster.equals(pulsar().getConfig().getClusterName())) {
            return;
        }
        results.add(pulsar().getBrokerService().getClusterPulsarAdmin(cluster).topics()
                .updatePartitionedTopicAsync(topicName.toString(), numPartitions, true));
    });
    return FutureUtil.waitForAll(results);
}
 
Example #22
Source File: AdminResource.java    From pulsar with Apache License 2.0 5 votes vote down vote up
protected CompletableFuture<Policies> getNamespacePoliciesAsync(NamespaceName namespaceName) {
    final String namespace = namespaceName.toString();
    final String policyPath = AdminResource.path(POLICIES, namespace);

    return policiesCache().getAsync(policyPath).thenCompose(policies -> {
        if (policies.isPresent()) {
            return pulsar()
                    .getNamespaceService()
                    .getNamespaceBundleFactory()
                    .getBundlesAsync(namespaceName)
                    .thenCompose(bundles -> {
                BundlesData bundleData = null;
                try {
                    bundleData = NamespaceBundleFactory.getBundlesData(bundles);
                } catch (Exception e) {
                    log.error("[{}] Failed to get namespace policies {}", clientAppId(), namespaceName, e);
                    return FutureUtil.failedFuture(new RestException(e));
                }
                policies.get().bundles = bundleData != null ? bundleData : policies.get().bundles;
                // hydrate the namespace polices
                mergeNamespaceWithDefaults(policies.get(), namespace, policyPath);
                return CompletableFuture.completedFuture(policies.get());
            });
        } else {
            return FutureUtil.failedFuture(new RestException(Status.NOT_FOUND, "Namespace does not exist"));
        }
    });
}
 
Example #23
Source File: RuntimeSpawner.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public CompletableFuture<FunctionStatus> getFunctionStatus(int instanceId) {
    Runtime runtime = this.runtime;
    if (null == runtime) {
        return FutureUtil.failedFuture(new IllegalStateException("Function runtime is not started yet"));
    }
    return runtime.getFunctionStatus(instanceId).thenApply(f -> {
       FunctionStatus.Builder builder = FunctionStatus.newBuilder();
       builder.mergeFrom(f).setNumRestarts(numRestarts).setInstanceId(String.valueOf(instanceId));
        if (!f.getRunning() && runtimeDeathException != null) {
            builder.setFailureException(runtimeDeathException.getMessage());
        }
       return builder.build();
    });
}
 
Example #24
Source File: PulsarService.java    From pulsar with Apache License 2.0 5 votes vote down vote up
/**
 * Load all the topics contained in a namespace
 *
 * @param bundle
 *            <code>NamespaceBundle</code> to identify the service unit
 * @throws Exception
 */
public void loadNamespaceTopics(NamespaceBundle bundle) {
    executor.submit(() -> {
        LOG.info("Loading all topics on bundle: {}", bundle);

        NamespaceName nsName = bundle.getNamespaceObject();
        List<CompletableFuture<Topic>> persistentTopics = Lists.newArrayList();
        long topicLoadStart = System.nanoTime();

        for (String topic : getNamespaceService().getListOfPersistentTopics(nsName).join()) {
            try {
                TopicName topicName = TopicName.get(topic);
                if (bundle.includes(topicName)) {
                    CompletableFuture<Topic> future = brokerService.getOrCreateTopic(topic);
                    if (future != null) {
                        persistentTopics.add(future);
                    }
                }
            } catch (Throwable t) {
                LOG.warn("Failed to preload topic {}", topic, t);
            }
        }

        if (!persistentTopics.isEmpty()) {
            FutureUtil.waitForAll(persistentTopics).thenRun(() -> {
                double topicLoadTimeSeconds = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - topicLoadStart)
                        / 1000.0;
                LOG.info("Loaded {} topics on {} -- time taken: {} seconds", persistentTopics.size(), bundle,
                        topicLoadTimeSeconds);
            });
        }
        return null;
    });
}
 
Example #25
Source File: InMemTransactionBuffer.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<TransactionMeta> abortTxn() {
    try {
        return CompletableFuture.completedFuture(abort());
    } catch (UnexpectedTxnStatusException e) {
        return FutureUtil.failedFuture(e);
    }
}
 
Example #26
Source File: ConsumerBase.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Void> reconsumeLaterCumulativeAsync(Message<?> message, long delayTime, TimeUnit unit) {
    if (!conf.isRetryEnable()) {
        return FutureUtil.failedFuture(new PulsarClientException("reconsumeLater method not support!"));
    }
    if (!isCumulativeAcknowledgementAllowed(conf.getSubscriptionType())) {
        return FutureUtil.failedFuture(new PulsarClientException.InvalidConfigurationException(
                "Cannot use cumulative acks on a non-exclusive subscription"));
    }
    return doReconsumeLater(message, AckType.Cumulative, Collections.emptyMap(), delayTime, unit);
}
 
Example #27
Source File: SchemaRegistryServiceWithSchemaDataValidator.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Void> checkCompatible(String schemaId,
                                               SchemaData schema,
                                               SchemaCompatibilityStrategy strategy) {
    try {
        SchemaDataValidator.validateSchemaData(schema);
    } catch (InvalidSchemaDataException e) {
        return FutureUtil.failedFuture(e);
    }
    return service.checkCompatible(schemaId, schema, strategy);
}
 
Example #28
Source File: PersistentTopic.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized void addFailed(ManagedLedgerException exception, Object ctx) {
    if (exception instanceof ManagedLedgerFencedException) {
        // If the managed ledger has been fenced, we cannot continue using it. We need to close and reopen
        close();
    } else {

        // fence topic when failed to write a message to BK
        isFenced = true;
        // close all producers
        List<CompletableFuture<Void>> futures = Lists.newArrayList();
        producers.values().forEach(producer -> futures.add(producer.disconnect()));
        FutureUtil.waitForAll(futures).handle((BiFunction<Void, Throwable, Void>) (aVoid, throwable) -> {
            decrementPendingWriteOpsAndCheck();
            return null;
        });

        PublishContext callback = (PublishContext) ctx;

        if (exception instanceof ManagedLedgerAlreadyClosedException) {
            if (log.isDebugEnabled()) {
                log.debug("[{}] Failed to persist msg in store: {}", topic, exception.getMessage());
            }

            callback.completed(new TopicClosedException(exception), -1, -1);
            return;

        } else {
            log.warn("[{}] Failed to persist msg in store: {}", topic, exception.getMessage());
        }

        if (exception instanceof ManagedLedgerTerminatedException) {
            // Signal the producer that this topic is no longer available
            callback.completed(new TopicTerminatedException(exception), -1, -1);
        } else {
            // Use generic persistence exception
            callback.completed(new PersistenceException(exception), -1, -1);
        }
    }
}
 
Example #29
Source File: ProducerImpl.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private void tryRegisterSchema(ClientCnx cnx, MessageImpl msg, SendCallback callback) {
    if (!changeToRegisteringSchemaState()) {
        return;
    }
    SchemaInfo schemaInfo = Optional.ofNullable(msg.getSchema())
                                    .map(Schema::getSchemaInfo)
                                    .filter(si -> si.getType().getValue() > 0)
                                    .orElse(Schema.BYTES.getSchemaInfo());
    getOrCreateSchemaAsync(cnx, schemaInfo).handle((v, ex) -> {
        if (ex != null) {
            Throwable t = FutureUtil.unwrapCompletionException(ex);
            log.warn("[{}] [{}] GetOrCreateSchema error", topic, producerName, t);
            if (t instanceof PulsarClientException.IncompatibleSchemaException) {
                msg.setSchemaState(MessageImpl.SchemaState.Broken);
                callback.sendComplete((PulsarClientException.IncompatibleSchemaException) t);
            }
        } else {
            log.warn("[{}] [{}] GetOrCreateSchema succeed", topic, producerName);
            SchemaHash schemaHash = SchemaHash.of(msg.getSchema());
            schemaCache.putIfAbsent(schemaHash, v);
            msg.getMessageBuilder().setSchemaVersion(ByteString.copyFrom(v));
            msg.setSchemaState(MessageImpl.SchemaState.Ready);
        }
        cnx.ctx().channel().eventLoop().execute(() -> {
            synchronized (ProducerImpl.this) {
                recoverProcessOpSendMsgFrom(cnx, msg);
            }
        });
        return null;
    });
}
 
Example #30
Source File: PulsarSqlSchemaInfoProvider.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<SchemaInfo> getSchemaByVersion(byte[] schemaVersion) {
    try {
        if (null == schemaVersion) {
            return completedFuture(null);
        }
        return completedFuture(cache.get(BytesSchemaVersion.of(schemaVersion)));
    } catch (ExecutionException e) {
        LOG.error("Can't get generic schema for topic {} schema version {}",
                topicName.toString(), new String(schemaVersion, StandardCharsets.UTF_8), e);
        return FutureUtil.failedFuture(e.getCause());
    }
}