public PersistentReplicator getPersistentReplicator(String remoteCluster) { return replicators.get(remoteCluster); }
public boolean containsKey(K key) { return get(key) != null; }
public ReplicationMetrics get(String namespaceCluster) { return metricsMap.get(namespaceCluster); }
public PersistentOfflineTopicStats getOfflineTopicStat(DestinationName topicName) { return offlineTopicStatCache.get(topicName); }
public PersistentSubscription getPersistentSubscription(String subscriptionName) { return subscriptions.get(subscriptionName); }
public Topic getTopicReference(String topic) throws Exception { CompletableFuture<Topic> future = topics.get(topic); if (future != null && future.isDone() && !future.isCompletedExceptionally()) { return future.get(); } else { return null; } }
public void remove(String namespaceCluster) { ReplicationMetrics replicationMetrics = metricsMap.get(namespaceCluster); if (replicationMetrics != null) { replicationMetrics.recycle(); metricsMap.remove(namespaceCluster); } }
CompletableFuture<Void> removeReplicator(String remoteCluster) { log.info("[{}] Removing replicator to {}", topic, remoteCluster); final CompletableFuture<Void> future = new CompletableFuture<>(); String name = PersistentReplicator.getReplicatorName(replicatorPrefix, remoteCluster); replicators.get(remoteCluster).disconnect().thenRun(() -> { ledger.asyncDeleteCursor(name, new DeleteCursorCallback() { @Override public void deleteCursorComplete(Object ctx) { replicators.remove(remoteCluster); future.complete(null); } @Override public void deleteCursorFailed(ManagedLedgerException exception, Object ctx) { log.error("[{}] Failed to delete cursor {} {}", topic, name, exception.getMessage(), exception); future.completeExceptionally(new PersistenceException(exception)); } }, null); }).exceptionally(e -> { log.error("[{}] Failed to close replication producer {} {}", topic, name, e.getMessage(), e); future.completeExceptionally(e); return null; }); return future; }
public CompletableFuture<Topic> getTopic(final String topic) { try { CompletableFuture<Topic> topicFuture = topics.get(topic); if (topicFuture != null) { if (topicFuture.isCompletedExceptionally()) { // Exceptional topics should be recreated. topics.remove(topic, topicFuture); } else { return topicFuture; } } return topics.computeIfAbsent(topic, this::createPersistentTopic); } 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); } }
public void refreshTopicToStatsMaps(NamespaceBundle oldBundle) { checkNotNull(oldBundle); try { // retrieve all topics under existing old bundle List<PersistentTopic> topics = getAllTopicsFromNamespaceBundle(oldBundle.getNamespaceObject().toString(), oldBundle.toString()); if (!isEmpty(topics)) { // add topic under new split bundles which already updated into NamespaceBundleFactory.bundleCache topics.stream().forEach(t -> { addTopicToStatsMaps(DestinationName.get(t.getName()), t); }); // remove old bundle from the map synchronized (multiLayerTopicsMap) { multiLayerTopicsMap.get(oldBundle.getNamespaceObject().toString()).remove(oldBundle.toString()); } } } catch (Exception e) { log.warn("Got exception while refreshing topicStats map", e); } }
public void removeTopicFromCache(String topic) { try { DestinationName destination = DestinationName.get(topic); NamespaceBundle namespaceBundle = pulsar.getNamespaceService().getBundle(destination); checkArgument(namespaceBundle instanceof NamespaceBundle); String bundleName = namespaceBundle.toString(); String namespaceName = destination.getNamespaceObject().toString(); synchronized (multiLayerTopicsMap) { ConcurrentOpenHashMap<String, ConcurrentOpenHashMap<String, PersistentTopic>> namespaceMap = multiLayerTopicsMap .get(namespaceName); ConcurrentOpenHashMap<String, PersistentTopic> 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); }
public PulsarClient getReplicationClient(String cluster) { PulsarClient client = replicationClients.get(cluster); if (client != null) { return client; } return replicationClients.computeIfAbsent(cluster, key -> { try { String path = PulsarWebResource.path("clusters", cluster); ClusterData data = this.pulsar.getConfigurationCache().clustersCache().get(path) .orElseThrow(() -> new KeeperException.NoNodeException(path)); ClientConfiguration configuration = new ClientConfiguration(); configuration.setUseTcpNoDelay(false); configuration.setConnectionsPerBroker(pulsar.getConfiguration().getReplicationConnectionsPerBroker()); configuration.setStatsInterval(0, TimeUnit.SECONDS); if (pulsar.getConfiguration().isAuthenticationEnabled()) { configuration.setAuthentication(pulsar.getConfiguration().getBrokerClientAuthenticationPlugin(), pulsar.getConfiguration().getBrokerClientAuthenticationParameters()); } String clusterUrl = configuration.isUseTls() ? (isNotBlank(data.getBrokerServiceUrlTls()) ? data.getBrokerServiceUrlTls() : data.getServiceUrlTls()) : null; clusterUrl = (isNotBlank(clusterUrl)) ? clusterUrl : (isNotBlank(data.getBrokerServiceUrl()) ? data.getBrokerServiceUrl() : data.getServiceUrl()); return new PulsarClientImpl(clusterUrl, configuration, this.workerGroup); } catch (Exception e) { throw new RuntimeException(e); } }); }
public PersistentTopic(String topic, ManagedLedger ledger, BrokerService brokerService) { this.topic = topic; this.ledger = ledger; this.brokerService = brokerService; this.producers = new ConcurrentOpenHashSet<Producer>(); this.subscriptions = new ConcurrentOpenHashMap<>(); this.replicators = new ConcurrentOpenHashMap<>(); this.isFenced = false; this.replicatorPrefix = brokerService.pulsar().getConfiguration().getReplicatorPrefix(); USAGE_COUNT_UPDATER.set(this, 0); for (ManagedCursor cursor : ledger.getCursors()) { if (cursor.getName().startsWith(replicatorPrefix)) { String localCluster = brokerService.pulsar().getConfiguration().getClusterName(); String remoteCluster = PersistentReplicator.getRemoteCluster(cursor.getName()); replicators.put(remoteCluster, new PersistentReplicator(this, cursor, localCluster, remoteCluster, brokerService)); } else { final String subscriptionName = Codec.decode(cursor.getName()); subscriptions.put(subscriptionName, new PersistentSubscription(this, subscriptionName, cursor)); // subscription-cursor gets activated by default: deactivate as there is no active subscription right // now subscriptions.get(subscriptionName).deactivateCursor(); } } this.lastActive = System.nanoTime(); }