Search in sources :

Example 6 with ConcurrentOpenHashMap

use of com.yahoo.pulsar.common.util.collections.ConcurrentOpenHashMap in project pulsar by yahoo.

the class BrokerService method removeTopicFromCache.

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);
}
Also used : NamespaceBundle(com.yahoo.pulsar.common.naming.NamespaceBundle) ConcurrentOpenHashMap(com.yahoo.pulsar.common.util.collections.ConcurrentOpenHashMap) ClusterReplicationMetrics(com.yahoo.pulsar.broker.stats.ClusterReplicationMetrics) PersistentTopic(com.yahoo.pulsar.broker.service.persistent.PersistentTopic) DestinationName(com.yahoo.pulsar.common.naming.DestinationName) PersistenceException(com.yahoo.pulsar.broker.service.BrokerServiceException.PersistenceException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException) KeeperException(org.apache.zookeeper.KeeperException) ServerMetadataException(com.yahoo.pulsar.broker.service.BrokerServiceException.ServerMetadataException) IOException(java.io.IOException) ServiceUnitNotReadyException(com.yahoo.pulsar.broker.service.BrokerServiceException.ServiceUnitNotReadyException)

Example 7 with ConcurrentOpenHashMap

use of com.yahoo.pulsar.common.util.collections.ConcurrentOpenHashMap in project pulsar by yahoo.

the class ConcurrentOpenHashMapTest method concurrentInsertionsAndReads.

@Test
public void concurrentInsertionsAndReads() throws Throwable {
    ConcurrentOpenHashMap<Long, String> map = new ConcurrentOpenHashMap<>();
    ExecutorService executor = Executors.newCachedThreadPool();
    final int nThreads = 16;
    final int N = 100_000;
    String value = "value";
    List<Future<?>> futures = new ArrayList<>();
    for (int i = 0; i < nThreads; i++) {
        final int threadIdx = i;
        futures.add(executor.submit(() -> {
            Random random = new Random();
            for (int j = 0; j < N; j++) {
                long key = random.nextLong();
                // Ensure keys are uniques
                key -= key % (threadIdx + 1);
                map.put(key, value);
            }
        }));
    }
    for (Future<?> future : futures) {
        future.get();
    }
    assertEquals(map.size(), N * nThreads);
    executor.shutdown();
}
Also used : ConcurrentOpenHashMap(com.yahoo.pulsar.common.util.collections.ConcurrentOpenHashMap) Random(java.util.Random) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) Test(org.testng.annotations.Test)

Aggregations

ConcurrentOpenHashMap (com.yahoo.pulsar.common.util.collections.ConcurrentOpenHashMap)7 ExecutorService (java.util.concurrent.ExecutorService)4 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)4 Test (org.testng.annotations.Test)4 PersistentTopic (com.yahoo.pulsar.broker.service.persistent.PersistentTopic)3 PulsarClientException (com.yahoo.pulsar.client.api.PulsarClientException)3 DestinationName (com.yahoo.pulsar.common.naming.DestinationName)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Random (java.util.Random)3 Future (java.util.concurrent.Future)3 Lists (com.google.common.collect.Lists)2 PersistenceException (com.yahoo.pulsar.broker.service.BrokerServiceException.PersistenceException)2 ServerMetadataException (com.yahoo.pulsar.broker.service.BrokerServiceException.ServerMetadataException)2 ServiceUnitNotReadyException (com.yahoo.pulsar.broker.service.BrokerServiceException.ServiceUnitNotReadyException)2 ClusterReplicationMetrics (com.yahoo.pulsar.broker.stats.ClusterReplicationMetrics)2 ClientConfiguration (com.yahoo.pulsar.client.api.ClientConfiguration)2 PulsarClient (com.yahoo.pulsar.client.api.PulsarClient)2 PulsarClientImpl (com.yahoo.pulsar.client.impl.PulsarClientImpl)2 NamespaceBundle (com.yahoo.pulsar.common.naming.NamespaceBundle)2