Java Code Examples for org.apache.pulsar.common.util.FutureUtil#failedFuture()
The following examples show how to use
org.apache.pulsar.common.util.FutureUtil#failedFuture() .
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: PulsarClientImpl.java From pulsar with Apache License 2.0 | 6 votes |
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 2
Source File: TwoPhaseCompactor.java From pulsar with Apache License 2.0 | 6 votes |
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 3
Source File: ConnectionPool.java From pulsar with Apache License 2.0 | 6 votes |
/** * Resolve DNS asynchronously and attempt to connect to any IP address returned by DNS server */ private CompletableFuture<Channel> createConnection(InetSocketAddress unresolvedAddress) { String hostname = unresolvedAddress.getHostString(); int port = unresolvedAddress.getPort(); try { // For non-sni-proxy: Resolve DNS --> Attempt to connect to all IP addresses until once succeeds CompletableFuture<List<InetAddress>> resolvedAddress = isSniProxy() ? CompletableFuture.completedFuture(Lists.newArrayList(InetAddress.getByName(hostname))) : resolveName(hostname); return resolvedAddress .thenCompose(inetAddresses -> connectToResolvedAddresses(inetAddresses.iterator(), port)); } catch (UnknownHostException e) { log.error("Invalid remote url {}", hostname, e); return FutureUtil.failedFuture(new InvalidServiceURL("Invalid url " + hostname, e)); } }
Example 4
Source File: ProducerBuilderImpl.java From pulsar with Apache License 2.0 | 6 votes |
@Override public CompletableFuture<Producer<T>> createAsync() { // config validation checkArgument(!(conf.isBatchingEnabled() && conf.isChunkingEnabled()), "Batching and chunking of messages can't be enabled together"); if (conf.getTopicName() == null) { return FutureUtil .failedFuture(new IllegalArgumentException("Topic name must be set on the producer builder")); } try { setMessageRoutingMode(); } catch(PulsarClientException pce) { return FutureUtil.failedFuture(pce); } return interceptorList == null || interceptorList.size() == 0 ? client.createProducerAsync(conf, schema, null) : client.createProducerAsync(conf, schema, new ProducerInterceptors(interceptorList)); }
Example 5
Source File: TransactionMetadataStoreService.java From pulsar with Apache License 2.0 | 5 votes |
public CompletableFuture<Void> updateTxnStatus(TxnID txnId, TxnStatus newStatus, TxnStatus expectedStatus) { TransactionCoordinatorID tcId = getTcIdFromTxnId(txnId); TransactionMetadataStore store = stores.get(tcId); if (store == null) { return FutureUtil.failedFuture(new CoordinatorNotFoundException(tcId)); } return store.updateTxnStatus(txnId, newStatus, expectedStatus); }
Example 6
Source File: InMemTransactionBuffer.java From pulsar with Apache License 2.0 | 5 votes |
@Override public CompletableFuture<TransactionMeta> commitTxn(long committedAtLedgerId, long committedAtEntryId) { try { return CompletableFuture.completedFuture(commitAt(committedAtLedgerId, committedAtEntryId)); } catch (UnexpectedTxnStatusException e) { return FutureUtil.failedFuture(e); } }
Example 7
Source File: SchemaRegistryServiceWithSchemaDataValidator.java From pulsar with Apache License 2.0 | 5 votes |
@Override public CompletableFuture<Boolean> isCompatible(String schemaId, SchemaData schema, SchemaCompatibilityStrategy strategy) { try { SchemaDataValidator.validateSchemaData(schema); } catch (InvalidSchemaDataException e) { return FutureUtil.failedFuture(e); } return service.isCompatible(schemaId, schema, strategy); }
Example 8
Source File: TransactionMetadataStoreService.java From pulsar with Apache License 2.0 | 5 votes |
public CompletableFuture<TxnMeta> getTxnMeta(TxnID txnId) { TransactionCoordinatorID tcId = getTcIdFromTxnId(txnId); TransactionMetadataStore store = stores.get(tcId); if (store == null) { return FutureUtil.failedFuture(new CoordinatorNotFoundException(tcId)); } return store.getTxnMeta(txnId); }
Example 9
Source File: ConsumerBase.java From pulsar with Apache License 2.0 | 5 votes |
@Override public CompletableFuture<Message<T>> receiveAsync() { if (listener != null) { return FutureUtil.failedFuture(new PulsarClientException.InvalidConfigurationException( "Cannot use receive() when a listener has been set")); } try { verifyConsumerState(); } catch (PulsarClientException e) { return FutureUtil.failedFuture(e); } return internalReceiveAsync(); }
Example 10
Source File: MultiVersionSchemaInfoProvider.java From pulsar with Apache License 2.0 | 5 votes |
@Override public CompletableFuture<SchemaInfo> getSchemaByVersion(byte[] schemaVersion) { try { if (null == schemaVersion) { return CompletableFuture.completedFuture(null); } return cache.get(BytesSchemaVersion.of(schemaVersion)); } catch (ExecutionException e) { LOG.error("Can't get schema for topic {} schema version {}", topicName.toString(), new String(schemaVersion, StandardCharsets.UTF_8), e); return FutureUtil.failedFuture(e.getCause()); } }
Example 11
Source File: MultiTopicsConsumerImpl.java From pulsar with Apache License 2.0 | 5 votes |
@Override public CompletableFuture<Void> unsubscribeAsync() { if (getState() == State.Closing || getState() == State.Closed) { return FutureUtil.failedFuture( new PulsarClientException.AlreadyClosedException("Topics Consumer was already closed")); } setState(State.Closing); CompletableFuture<Void> unsubscribeFuture = new CompletableFuture<>(); List<CompletableFuture<Void>> futureList = consumers.values().stream() .map(c -> c.unsubscribeAsync()).collect(Collectors.toList()); FutureUtil.waitForAll(futureList) .whenComplete((r, ex) -> { if (ex == null) { setState(State.Closed); unAckedMessageTracker.close(); unsubscribeFuture.complete(null); log.info("[{}] [{}] [{}] Unsubscribed Topics Consumer", topic, subscription, consumerName); } else { setState(State.Failed); unsubscribeFuture.completeExceptionally(ex); log.error("[{}] [{}] [{}] Could not unsubscribe Topics Consumer", topic, subscription, consumerName, ex.getCause()); } }); return unsubscribeFuture; }
Example 12
Source File: TransactionMetadataStoreService.java From pulsar with Apache License 2.0 | 5 votes |
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 13
Source File: MultiTopicsConsumerImpl.java From pulsar with Apache License 2.0 | 4 votes |
public CompletableFuture<Void> removeConsumerAsync(String topicName) { checkArgument(TopicName.isValid(topicName), "Invalid topic name:" + topicName); if (getState() == State.Closing || getState() == State.Closed) { return FutureUtil.failedFuture( new PulsarClientException.AlreadyClosedException("Topics Consumer was already closed")); } CompletableFuture<Void> unsubscribeFuture = new CompletableFuture<>(); String topicPartName = TopicName.get(topicName).getPartitionedTopicName(); List<ConsumerImpl<T>> consumersToClose = consumers.values().stream() .filter(consumer -> { String consumerTopicName = consumer.getTopic(); return TopicName.get(consumerTopicName).getPartitionedTopicName().equals(topicPartName); }).collect(Collectors.toList()); List<CompletableFuture<Void>> futureList = consumersToClose.stream() .map(ConsumerImpl::closeAsync).collect(Collectors.toList()); FutureUtil.waitForAll(futureList) .whenComplete((r, ex) -> { if (ex == null) { consumersToClose.forEach(consumer1 -> { consumers.remove(consumer1.getTopic()); pausedConsumers.remove(consumer1); allTopicPartitionsNumber.decrementAndGet(); }); topics.remove(topicName); ((UnAckedTopicMessageTracker) unAckedMessageTracker).removeTopicMessages(topicName); unsubscribeFuture.complete(null); log.info("[{}] [{}] [{}] Removed Topics Consumer, allTopicPartitionsNumber: {}", topicName, subscription, consumerName, allTopicPartitionsNumber); } else { unsubscribeFuture.completeExceptionally(ex); setState(State.Failed); log.error("[{}] [{}] [{}] Could not remove Topics Consumer", topicName, subscription, consumerName, ex.getCause()); } }); return unsubscribeFuture; }
Example 14
Source File: PulsarClientImpl.java From pulsar with Apache License 2.0 | 4 votes |
@Override public CompletableFuture<Void> closeAsync() { log.info("Client closing. URL: {}", lookup.getServiceUrl()); if (!state.compareAndSet(State.Open, State.Closing)) { return FutureUtil.failedFuture(new PulsarClientException.AlreadyClosedException("Client already closed")); } final CompletableFuture<Void> closeFuture = new CompletableFuture<>(); List<CompletableFuture<Void>> futures = Lists.newArrayList(); synchronized (producers) { // Copy to a new list, because the closing will trigger a removal from the map // and invalidate the iterator List<ProducerBase<?>> producersToClose = Lists.newArrayList(producers.keySet()); producersToClose.forEach(p -> futures.add(p.closeAsync())); } synchronized (consumers) { List<ConsumerBase<?>> consumersToClose = Lists.newArrayList(consumers.keySet()); consumersToClose.forEach(c -> futures.add(c.closeAsync())); } // Need to run the shutdown sequence in a separate thread to prevent deadlocks // If there are consumers or producers that need to be shutdown we cannot use the same thread // to shutdown the EventLoopGroup as well as that would be trying to shutdown itself thus a deadlock // would happen FutureUtil.waitForAll(futures).thenRun(() -> new Thread(() -> { // All producers & consumers are now closed, we can stop the client safely try { shutdown(); closeFuture.complete(null); state.set(State.Closed); } catch (PulsarClientException e) { closeFuture.completeExceptionally(e); } }, "pulsar-client-shutdown-thread").start()).exceptionally(exception -> { closeFuture.completeExceptionally(exception); return null; }); return closeFuture; }
Example 15
Source File: PulsarClientImpl.java From pulsar with Apache License 2.0 | 4 votes |
public <T> CompletableFuture<Consumer<T>> subscribeAsync(ConsumerConfigurationData<T> conf, Schema<T> schema, ConsumerInterceptors<T> interceptors) { if (state.get() != State.Open) { return FutureUtil.failedFuture(new PulsarClientException.AlreadyClosedException("Client already closed")); } if (conf == null) { return FutureUtil.failedFuture( new PulsarClientException.InvalidConfigurationException("Consumer configuration undefined")); } if (!conf.getTopicNames().stream().allMatch(TopicName::isValid)) { return FutureUtil.failedFuture(new PulsarClientException.InvalidTopicNameException("Invalid topic name")); } if (isBlank(conf.getSubscriptionName())) { return FutureUtil .failedFuture(new PulsarClientException.InvalidConfigurationException("Empty subscription name")); } if (conf.isReadCompacted() && (!conf.getTopicNames().stream() .allMatch(topic -> TopicName.get(topic).getDomain() == TopicDomain.persistent) || (conf.getSubscriptionType() != SubscriptionType.Exclusive && conf.getSubscriptionType() != SubscriptionType.Failover))) { return FutureUtil.failedFuture(new PulsarClientException.InvalidConfigurationException( "Read compacted can only be used with exclusive or failover persistent subscriptions")); } if (conf.getConsumerEventListener() != null && conf.getSubscriptionType() != SubscriptionType.Failover) { return FutureUtil.failedFuture(new PulsarClientException.InvalidConfigurationException( "Active consumer listener is only supported for failover subscription")); } if (conf.getTopicsPattern() != null) { // If use topicsPattern, we should not use topic(), and topics() method. if (!conf.getTopicNames().isEmpty()){ return FutureUtil .failedFuture(new IllegalArgumentException("Topic names list must be null when use topicsPattern")); } return patternTopicSubscribeAsync(conf, schema, interceptors); } else if (conf.getTopicNames().size() == 1) { return singleTopicSubscribeAsync(conf, schema, interceptors); } else { return multiTopicSubscribeAsync(conf, schema, interceptors); } }
Example 16
Source File: TransactionBufferClientImpl.java From pulsar with Apache License 2.0 | 4 votes |
@Override public CompletableFuture<Void> commitTxnOnTopic(String topic, long txnIdMostBits, long txnIdLeastBits) { return FutureUtil.failedFuture( new UnsupportedOperationException("Not Implemented Yet")); }
Example 17
Source File: OwnedBundle.java From pulsar with Apache License 2.0 | 4 votes |
/** * It unloads the bundle by closing all topics concurrently under this bundle. * * <pre> * a. disable bundle ownership in memory and not in zk * b. close all the topics concurrently * c. delete ownership znode from zookeeper. * </pre> * * @param pulsar * @param timeout * timeout for unloading bundle. It doesn't throw exception if it times out while waiting on closing all * topics * @param timeoutUnit * @throws Exception */ public CompletableFuture<Void> handleUnloadRequest(PulsarService pulsar, long timeout, TimeUnit timeoutUnit) { long unloadBundleStartTime = System.nanoTime(); // Need a per namespace RenetrantReadWriteLock // Here to do a writeLock to set the flag and proceed to check and close connections try { while (!this.nsLock.writeLock().tryLock(1, TimeUnit.SECONDS)) { // Using tryLock to avoid deadlocks caused by 2 threads trying to acquire 2 readlocks (eg: replicators) // while a handleUnloadRequest happens in the middle LOG.warn("Contention on OwnedBundle rw lock. Retrying to acquire lock write lock"); } try { // set the flag locally s.t. no more producer/consumer to this namespace is allowed if (!IS_ACTIVE_UPDATER.compareAndSet(this, TRUE, FALSE)) { // An exception is thrown when the namespace is not in active state (i.e. another thread is // removing/have removed it) return FutureUtil.failedFuture(new IllegalStateException( "Namespace is not active. ns:" + this.bundle + "; state:" + IS_ACTIVE_UPDATER.get(this))); } } finally { // no matter success or not, unlock this.nsLock.writeLock().unlock(); } } catch (InterruptedException e) { return FutureUtil.failedFuture(e); } AtomicInteger unloadedTopics = new AtomicInteger(); LOG.info("Disabling ownership: {}", this.bundle); // close topics forcefully CompletableFuture<Void> future = pulsar.getNamespaceService().getOwnershipCache() .updateBundleState(this.bundle, false) .thenCompose(v -> pulsar.getBrokerService().unloadServiceUnit(bundle, true, timeout, timeoutUnit)) .handle((numUnloadedTopics, ex) -> { if (ex != null) { // ignore topic-close failure to unload bundle LOG.error("Failed to close topics under namespace {}", bundle.toString(), ex); } else { unloadedTopics.set(numUnloadedTopics); } return null; }) .thenCompose(v -> { // delete ownership node on zk return pulsar.getNamespaceService().getOwnershipCache().removeOwnership(bundle); }).thenAccept(v -> { double unloadBundleTime = TimeUnit.NANOSECONDS .toMillis((System.nanoTime() - unloadBundleStartTime)); LOG.info("Unloading {} namespace-bundle with {} topics completed in {} ms", this.bundle, unloadedTopics, unloadBundleTime); }); future.exceptionally(ex -> { // Failed to remove ownership node: enable namespace-bundle again so, it can serve new topics pulsar.getNamespaceService().getOwnershipCache().updateBundleState(this.bundle, true); return null; }); return future; }
Example 18
Source File: AuthorizationProvider.java From pulsar with Apache License 2.0 | 3 votes |
/** * Grant authorization-action permission on a namespace to the given client * @param namespaceName * @param originalRole role not overriden by proxy role if request do pass through proxy * @param role originalRole | proxyRole if the request didn't pass through proxy * @param operation * @param authData * @return CompletableFuture<Boolean> */ default CompletableFuture<Boolean> allowNamespaceOperationAsync(NamespaceName namespaceName, String originalRole, String role, NamespaceOperation operation, AuthenticationDataSource authData) { return FutureUtil.failedFuture( new IllegalStateException("NamespaceOperation is not supported by the Authorization provider you are using.")); }
Example 19
Source File: AuthorizationProvider.java From pulsar with Apache License 2.0 | 3 votes |
/** * Grant authorization-action permission on a topic to the given client * @param topic * @param originalRole role not overriden by proxy role if request do pass through proxy * @param role originalRole | proxyRole if the request didn't pass through proxy * @param operation * @param authData * @return CompletableFuture<Boolean> */ default CompletableFuture<Boolean> allowTopicOperationAsync(TopicName topic, String originalRole, String role, TopicOperation operation, AuthenticationDataSource authData) { return FutureUtil.failedFuture( new IllegalStateException("TopicOperation is not supported by the Authorization provider you are using.")); }
Example 20
Source File: AuthorizationService.java From pulsar with Apache License 2.0 | 3 votes |
/** * Grant permission to roles that can access subscription-admin api * * @param namespace * @param subscriptionName * @param roles * @param authDataJson * additional authdata in json for targeted authorization provider * @return */ public CompletableFuture<Void> grantSubscriptionPermissionAsync(NamespaceName namespace, String subscriptionName, Set<String> roles, String authDataJson) { if (provider != null) { return provider.grantSubscriptionPermissionAsync(namespace, subscriptionName, roles, authDataJson); } return FutureUtil.failedFuture(new IllegalStateException("No authorization provider configured")); }