public static boolean isValid(String topic) { try { get(topic); return true; } catch (Exception e) { return false; } }
public static NamespaceName getNameSpaceFromPattern(Pattern pattern) { return TopicName.get(pattern.pattern()).getNamespaceObject(); }
private boolean topicNameValid(String topicName) { checkArgument(TopicName.isValid(topicName), "Invalid topic name:" + topicName); checkArgument(!topics.containsKey(topicName), "Topics already contains topic:" + topicName); if (this.namespaceName != null) { checkArgument(TopicName.get(topicName).getNamespace().toString().equals(this.namespaceName.toString()), "Topic " + topicName + " not in same namespace with Topics"); } return true; }
@Override public CompletableFuture<List<String>> getTopicsUnderNamespace(NamespaceName namespace, Mode mode) { CompletableFuture<List<String>> future = new CompletableFuture<>(); String format = namespace.isV2() ? "admin/v2/namespaces/%s/topics?mode=%s" : "admin/namespaces/%s/destinations?mode=%s"; httpClient .get(String.format(format, namespace, mode.toString()), String[].class) .thenAccept(topics -> { List<String> result = Lists.newArrayList(); // do not keep partition part of topic name Arrays.asList(topics).forEach(topic -> { String filtered = TopicName.get(topic).getPartitionedTopicName(); if (!result.contains(filtered)) { result.add(filtered); } }); future.complete(result);}) .exceptionally(ex -> { log.warn("Failed to getTopicsUnderNamespace namespace: {}.", namespace, ex.getMessage()); future.completeExceptionally(ex); return null; }); return future; }
@Override public CompletableFuture<List<String>> getPartitionsForTopic(String topic) { return getPartitionedTopicMetadata(topic).thenApply(metadata -> { if (metadata.partitions > 1) { TopicName topicName = TopicName.get(topic); List<String> partitions = new ArrayList<>(metadata.partitions); for (int i = 0; i < metadata.partitions; i++) { partitions.add(topicName.getPartition(i).toString()); } return partitions; } else { return Collections.singletonList(topic); } }); }
public static TopicName get(String domain, String tenant, String namespace, String topic) { String name = domain + "://" + tenant + '/' + namespace + '/' + topic; return TopicName.get(name); }
String regex = conf.getTopicsPattern().pattern(); Mode subscriptionMode = convertRegexSubscriptionMode(conf.getRegexSubscriptionMode()); TopicName destination = TopicName.get(regex); NamespaceName namespaceName = destination.getNamespaceObject(); Optional<ConsumerBase<T>> subscriber = subscriptionExist(conf);
private static boolean topicNamesValid(Collection<String> topics) { checkState(topics != null && topics.size() >= 1, "topics should should contain more than 1 topic"); final String namespace = TopicName.get(topics.stream().findFirst().get()).getNamespace(); Optional<String> result = topics.stream() .filter(topic -> { boolean topicInvalid = !TopicName.isValid(topic); if (topicInvalid) { return true; } String newNamespace = TopicName.get(topic).getNamespace(); if (!namespace.equals(newNamespace)) { return true; } else { return false; } }).findFirst(); if (result.isPresent()) { log.warn("[{}] Received invalid topic name. {}/{}", result.get()); return false; } // check topic names are unique HashSet<String> set = new HashSet<>(topics); if (set.size() == topics.size()) { return true; } else { log.warn("Topic names not unique. unique/all : {}/{}", set.size(), topics.size()); return false; } }
private void handleSubscribeOneTopicError(String topicName, Throwable error, CompletableFuture<Void> subscribeFuture) { log.warn("[{}] Failed to subscribe for topic [{}] in topics consumer {}", topic, topicName, error.getMessage()); client.externalExecutorProvider().getExecutor().submit(() -> { AtomicInteger toCloseNum = new AtomicInteger(0); consumers.values().stream().filter(consumer1 -> { String consumerTopicName = consumer1.getTopic(); if (TopicName.get(consumerTopicName).getPartitionedTopicName().equals(topicName)) { toCloseNum.incrementAndGet(); return true; } else { return false; } }).collect(Collectors.toList()).forEach(consumer2 -> { consumer2.closeAsync().whenComplete((r, ex) -> { consumer2.subscribeFuture().completeExceptionally(error); allTopicPartitionsNumber.decrementAndGet(); consumers.remove(consumer2.getTopic()); if (toCloseNum.decrementAndGet() == 0) { log.warn("[{}] Failed to subscribe for topic [{}] in topics consumer, subscribe error: {}", topic, topicName, error.getMessage()); topics.remove(topicName); checkState(allTopicPartitionsNumber.get() == consumers.values().size()); subscribeFuture.completeExceptionally(error); } return; }); }); }); }
public static TopicName get(String domain, String tenant, String cluster, String namespace, String topic) { String name = domain + "://" + tenant + '/' + cluster + '/' + namespace + '/' + topic; return TopicName.get(name); }
public PersistentOfflineTopicStats getEstimatedUnloadedTopicBacklog(ManagedLedgerFactoryImpl factory, String managedLedgerName) throws Exception { return estimateUnloadedTopicBacklog(factory, TopicName.get("persistent://" + managedLedgerName)); }
public static MessageId fromByteArrayWithTopic(byte[] data, String topicName) throws IOException { return fromByteArrayWithTopic(data, TopicName.get(topicName)); }
public static TopicName get(String domain, NamespaceName namespaceName, String topic) { String name = domain + "://" + namespaceName.toString() + '/' + topic; return TopicName.get(name); }
protected CompletableFuture<ClientCnx> getConnection(final String topic) { TopicName topicName = TopicName.get(topic); return lookup.getBroker(topicName) .thenCompose(pair -> cnxPool.getConnection(pair.getLeft(), pair.getRight())); }
public TopicName getPartition(int index) { if (index == -1 || this.toString().contains(PARTITIONED_TOPIC_SUFFIX)) { return this; } String partitionName = this.toString() + PARTITIONED_TOPIC_SUFFIX + index; return get(partitionName); }
public CompletableFuture<PartitionedTopicMetadata> getPartitionedTopicMetadata(String topic) { CompletableFuture<PartitionedTopicMetadata> metadataFuture; try { TopicName topicName = TopicName.get(topic); metadataFuture = lookup.getPartitionedTopicMetadata(topicName); } catch (IllegalArgumentException e) { return FutureUtil.failedFuture(new PulsarClientException.InvalidConfigurationException(e.getMessage())); } return metadataFuture; }
/** * Read the schema information for a given topic. * * If the topic does not exist or it has no schema associated, it will return an empty response */ public CompletableFuture<Optional<SchemaInfo>> getSchema(String topic) { TopicName topicName; try { topicName = TopicName.get(topic); } catch (Throwable t) { return FutureUtil .failedFuture(new PulsarClientException.InvalidTopicNameException("Invalid topic name: " + topic)); } return lookup.getSchema(topicName); }
public <T> CompletableFuture<Reader<T>> createReaderAsync(ReaderConfigurationData<T> conf, Schema<T> schema) { if (schema instanceof AutoConsumeSchema) { AutoConsumeSchema autoConsumeSchema = (AutoConsumeSchema) schema; return lookup.getSchema(TopicName.get(conf.getTopicName())) .thenCompose(schemaInfoOptional -> { if (schemaInfoOptional.isPresent() && schemaInfoOptional.get().getType() == SchemaType.AVRO) { GenericSchema genericSchema = GenericSchema.of(schemaInfoOptional.get()); log.info("Auto detected schema for topic {} : {}", conf.getTopicName(), new String(schemaInfoOptional.get().getSchema(), UTF_8)); autoConsumeSchema.setSchema(genericSchema); return doCreateReaderAsync(conf, schema); } else { return FutureUtil.failedFuture( new PulsarClientException.LookupException("Currently schema detection only works for topics with avro schemas")); } }); } else { return doCreateReaderAsync(conf, schema); } } <T> CompletableFuture<Reader<T>> doCreateReaderAsync(ReaderConfigurationData<T> conf, Schema<T> schema) {
private <T> CompletableFuture<Consumer<T>> singleTopicSubscribeAsync(ConsumerConfigurationData<T> conf, Schema<T> schema, ConsumerInterceptors<T> interceptors) { if (schema instanceof AutoConsumeSchema) { AutoConsumeSchema autoConsumeSchema = (AutoConsumeSchema) schema; return lookup.getSchema(TopicName.get(conf.getSingleTopic())) .thenCompose(schemaInfoOptional -> { if (schemaInfoOptional.isPresent() && schemaInfoOptional.get().getType() == SchemaType.AVRO) { GenericSchema genericSchema = GenericSchema.of(schemaInfoOptional.get()); log.info("Auto detected schema for topic {} : {}", conf.getSingleTopic(), new String(schemaInfoOptional.get().getSchema(), UTF_8)); autoConsumeSchema.setSchema(genericSchema); return doSingleTopicSubscribeAsync(conf, schema, interceptors); } else { return FutureUtil.failedFuture( new PulsarClientException.LookupException("Currently schema detection only works for topics with avro schemas")); } }); } else { return doSingleTopicSubscribeAsync(conf, schema, interceptors); } }
return lookup.getSchema(TopicName.get(conf.getTopicName())) .thenCompose(schemaInfoOptional -> { if (schemaInfoOptional.isPresent()) {