public List<V> values() { List<V> values = Lists.newArrayList(); forEach((key, value) -> values.add(value)); return values; }
final CountDownLatch allCursorsCounter = new CountDownLatch(1); final long errorInReadingCursor = (long) -1; ConcurrentOpenHashMap<String, Long> ledgerRetryMap = new ConcurrentOpenHashMap<>(); if (accurate && ledgerRetryMap.size() > 0) { ledgerRetryMap.forEach((cursorName, ledgerId) -> { if (log.isDebugEnabled()) { log.debug("Cursor {} Ledger {} Trying to obtain MD from BkAdmin", cursorName, ledgerId);
public boolean containsKey(K key) { return get(key) != null; }
public static boolean validateDynamicConfiguration(String key, String value) { if (dynamicConfigurationMap.containsKey(key) && dynamicConfigurationMap.get(key).validator != null) { return dynamicConfigurationMap.get(key).validator.test(value); } return true; }
public List<String> getListOfNonPersistentTopics(NamespaceName namespaceName) throws Exception { List<String> topics = Lists.newArrayList(); ClusterData peerClusterData; try { peerClusterData = PulsarWebResource.checkLocalOrGetPeerReplicationCluster(pulsar, namespaceName) .get(cacheTimeOutInSec, SECONDS); } catch (InterruptedException | ExecutionException | TimeoutException e) { throw new RuntimeException("Failed to contact peer replication cluster.", e); } // if peer-cluster-data is present it means namespace is owned by that peer-cluster and request should be // redirect to the peer-cluster if (peerClusterData != null) { return getNonPersistentTopicsFromPeerCluster(peerClusterData, namespaceName); } // Non-persistent topics don't have managed ledgers so we have to retrieve them from local cache. synchronized (pulsar.getBrokerService().getMultiLayerTopicMap()) { if (pulsar.getBrokerService().getMultiLayerTopicMap().containsKey(namespaceName.toString())) { pulsar.getBrokerService().getMultiLayerTopicMap().get(namespaceName.toString()).values() .forEach(bundle -> { bundle.forEach((topicName, topic) -> { if (topic instanceof NonPersistentTopic && ((NonPersistentTopic)topic).isActive()) { topics.add(topicName); } }); }); } } topics.sort(null); return topics; }
public void removeTopicFromCache(String topic) { try { TopicName topicName = TopicName.get(topic); NamespaceBundle namespaceBundle = pulsar.getNamespaceService().getBundle(topicName); checkArgument(namespaceBundle instanceof NamespaceBundle); String bundleName = namespaceBundle.toString(); String namespaceName = topicName.getNamespaceObject().toString(); synchronized (multiLayerTopicsMap) { ConcurrentOpenHashMap<String, ConcurrentOpenHashMap<String, Topic>> namespaceMap = multiLayerTopicsMap .get(namespaceName); ConcurrentOpenHashMap<String, Topic> bundleMap = namespaceMap.get(bundleName); bundleMap.remove(topic); if (bundleMap.isEmpty()) { namespaceMap.remove(bundleName); } if (namespaceMap.isEmpty()) { multiLayerTopicsMap.remove(namespaceName); final ClusterReplicationMetrics clusterReplicationMetrics = pulsarStats .getClusterReplicationMetrics(); replicationClients.forEach((cluster, client) -> { clusterReplicationMetrics.remove(clusterReplicationMetrics.getKeyName(namespaceName, cluster)); }); } } } catch (Exception e) { log.warn("Got exception when retrieving bundle name during removeTopicFromCache", e); } topics.remove(topic); }
if (closeIfClientsConnected) { List<CompletableFuture<Void>> futures = Lists.newArrayList(); replicators.forEach((cluster, replicator) -> futures.add(replicator.disconnect())); producers.forEach(producer -> futures.add(producer.disconnect())); subscriptions.forEach((s, sub) -> futures.add(sub.disconnect())); FutureUtil.waitForAll(futures).thenRun(() -> { closeClientFuture.complete(null); if (!subscriptions.isEmpty()) { isFenced = false; deleteFuture.completeExceptionally(new TopicBusyException("Topic has subscriptions")); subscriptions.forEach((s, sub) -> futures.add(sub.delete()));
if (!replicators.containsKey(cluster)) { futures.add(startReplicator(cluster)); replicators.forEach((cluster, replicator) -> {
topicStats.clear(); service.getProducers().forEach((topic, handlers) -> { if (log.isDebugEnabled()) { log.debug("Collect stats from {} producer handlers for topic {}", handlers.size(), topic); ProxyNamespaceStats nsStat = topicStats.computeIfAbsent(namespaceName, ns -> new ProxyNamespaceStats()); handlers.forEach(handler -> { nsStat.numberOfMsgPublished += handler.getAndResetNumMsgsSent(); }); }); service.getConsumers().forEach((topic, handlers) -> { if (log.isDebugEnabled()) { log.debug("Collect stats from {} consumer handlers for topic {}", handlers.size(), topic); ProxyNamespaceStats nsStat = topicStats.computeIfAbsent(namespaceName, ns -> new ProxyNamespaceStats()); handlers.forEach(handler -> { nsStat.numberOfMsgDelivered += handler.getAndResetNumMsgsAcked(); topicStats.forEach((namespace, stats) -> { if (log.isDebugEnabled()) { log.debug("Add ns-stats of namespace {} to metrics", namespace);
topicsMap.forEach((namespaceName, bundles) -> { if (bundles.isEmpty()) { return; bundles.forEach((bundle, topics) -> { NamespaceBundleStats currentBundleStats = bundleStats.computeIfAbsent(bundle, k -> new NamespaceBundleStats()); currentBundleStats.reset(); currentBundleStats.topics = topics.size(); topics.forEach((name, topic) -> { if (topic instanceof PersistentTopic) { try {
public ClusterReplicationMetrics(String localCluster, boolean metricsEnabled) { metricsList = new ArrayList<>(); this.localCluster = localCluster; metricsMap = new ConcurrentOpenHashMap<>(); this.metricsEnabled = metricsEnabled; }
this.brokerService = brokerService; this.producers = new ConcurrentOpenHashSet<Producer>(16, 1); this.subscriptions = new ConcurrentOpenHashMap<>(16, 1); this.replicators = new ConcurrentOpenHashMap<>(16, 1); this.isFenced = false; this.replicatorPrefix = brokerService.pulsar().getConfiguration().getReplicatorPrefix(); subscriptions.put(subscriptionName, createPersistentSubscription(subscriptionName, cursor)); subscriptions.get(subscriptionName).deactivateCursor();
public PulsarClientImpl getNamespaceClient(ClusterData cluster) { PulsarClientImpl client = namespaceClients.get(cluster); if (client != null) { return client; return namespaceClients.computeIfAbsent(cluster, key -> { try { ClientBuilder clientBuilder = PulsarClient.builder()
private void addTopicToStatsMaps(TopicName topicName, Topic topic) { try { NamespaceBundle namespaceBundle = pulsar.getNamespaceService().getBundle(topicName); if (namespaceBundle != null) { synchronized (multiLayerTopicsMap) { String serviceUnit = namespaceBundle.toString(); multiLayerTopicsMap // .computeIfAbsent(topicName.getNamespace(), k -> new ConcurrentOpenHashMap<>()) // .computeIfAbsent(serviceUnit, k -> new ConcurrentOpenHashMap<>()) // .put(topicName.toString(), topic); } } invalidateOfflineTopicStatCache(topicName); } catch (Exception e) { log.warn("Got exception when retrieving bundle name during create persistent topic", e); } }
private CompletableFuture<Optional<Topic>> getTopic(final String topic, boolean createIfMissing) { try { CompletableFuture<Optional<Topic>> topicFuture = topics.get(topic); if (topicFuture != null) { if (topicFuture.isCompletedExceptionally() || (topicFuture.isDone() && !topicFuture.getNow(Optional.empty()).isPresent())) { // Exceptional topics should be recreated. topics.remove(topic, topicFuture); } else { return topicFuture; } } final boolean isPersistentTopic = TopicName.get(topic).getDomain().equals(TopicDomain.persistent); return topics.computeIfAbsent(topic, (topicName) -> { return isPersistentTopic ? this.loadOrCreatePersistentTopic(topicName, createIfMissing) : createNonPersistentTopic(topicName); }); } catch (IllegalArgumentException e) { log.warn("[{}] Illegalargument exception when loading topic", topic, e); return failedFuture(e); } catch (RuntimeException e) { Throwable cause = e.getCause(); if (cause instanceof ServiceUnitNotReadyException) { log.warn("[{}] Service unit is not ready when loading the topic", topic); } else { log.warn("[{}] Unexpected exception when loading topic: {}", topic, cause); } return failedFuture(cause); } }
protected boolean addReplicationCluster(String remoteCluster, NonPersistentTopic nonPersistentTopic, String localCluster) { AtomicBoolean isReplicatorStarted = new AtomicBoolean(true); replicators.computeIfAbsent(remoteCluster, r -> { try { return new NonPersistentReplicator(NonPersistentTopic.this, localCluster, remoteCluster, brokerService); } catch (NamingException e) { isReplicatorStarted.set(false); log.error("[{}] Replicator startup failed due to partitioned-topic {}", topic, remoteCluster); } return null; }); // clean up replicator if startup is failed if (!isReplicatorStarted.get()) { replicators.remove(remoteCluster); } return isReplicatorStarted.get(); }
public List<Topic> getAllTopicsFromNamespaceBundle(String namespace, String bundle) { ConcurrentOpenHashMap<String, ConcurrentOpenHashMap<String, Topic>> map1 = multiLayerTopicsMap.get(namespace); if (map1 == null) { return Collections.emptyList(); } ConcurrentOpenHashMap<String, Topic> map2 = map1.get(bundle); if (map2 == null) { return Collections.emptyList(); } return map2.values(); }
private static ConcurrentOpenHashMap<String, ConfigField> prepareDynamicConfigurationMap() { ConcurrentOpenHashMap<String, ConfigField> dynamicConfigurationMap = new ConcurrentOpenHashMap<>(); for (Field field : ServiceConfiguration.class.getDeclaredFields()) { if (field != null && field.isAnnotationPresent(FieldContext.class)) { field.setAccessible(true); if (((FieldContext) field.getAnnotation(FieldContext.class)).dynamic()) { dynamicConfigurationMap.put(field.getName(), new ConfigField(field)); } } } return dynamicConfigurationMap; }