use of org.apache.pulsar.common.util.collections.ConcurrentOpenHashMap in project incubator-pulsar by apache.
the class BrokerService method addTopicToStatsMaps.
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);
}
}
use of org.apache.pulsar.common.util.collections.ConcurrentOpenHashMap in project incubator-pulsar by apache.
the class PersistentTopicTest method testMaxConsumersShared.
public void testMaxConsumersShared() throws Exception {
PersistentTopic topic = new PersistentTopic(successTopicName, ledgerMock, brokerService);
PersistentSubscription sub = new PersistentSubscription(topic, "sub-1", cursorMock);
PersistentSubscription sub2 = new PersistentSubscription(topic, "sub-2", cursorMock);
// for count consumers on topic
ConcurrentOpenHashMap<String, PersistentSubscription> subscriptions = new ConcurrentOpenHashMap<>(16, 1);
subscriptions.put("sub-1", sub);
subscriptions.put("sub-2", sub2);
Field field = topic.getClass().getDeclaredField("subscriptions");
field.setAccessible(true);
field.set(topic, subscriptions);
// 1. add consumer1
Consumer consumer = new Consumer(sub, SubType.Shared, topic.getName(), 1, /* consumer id */
0, "Cons1", /* consumer name */
50000, serverCnx, "myrole-1", Collections.emptyMap(), false, /* read compacted */
InitialPosition.Latest);
sub.addConsumer(consumer);
assertEquals(sub.getConsumers().size(), 1);
// 2. add consumer2
Consumer consumer2 = new Consumer(sub, SubType.Shared, topic.getName(), 2, /* consumer id */
0, "Cons2", /* consumer name */
50000, serverCnx, "myrole-1", Collections.emptyMap(), false, /* read compacted */
InitialPosition.Latest);
sub.addConsumer(consumer2);
assertEquals(sub.getConsumers().size(), 2);
// 3. add consumer3 but reach maxConsumersPerSubscription
try {
Consumer consumer3 = new Consumer(sub, SubType.Shared, topic.getName(), 3, /* consumer id */
0, "Cons3", /* consumer name */
50000, serverCnx, "myrole-1", Collections.emptyMap(), false, /* read compacted */
InitialPosition.Latest);
sub.addConsumer(consumer3);
fail("should have failed");
} catch (BrokerServiceException e) {
assertTrue(e instanceof BrokerServiceException.ConsumerBusyException);
}
// check number of consumers on topic
assertEquals(topic.getNumberOfConsumers(), 2);
// 4. add consumer4 to sub2
Consumer consumer4 = new Consumer(sub2, SubType.Shared, topic.getName(), 4, /* consumer id */
0, "Cons4", /* consumer name */
50000, serverCnx, "myrole-1", Collections.emptyMap(), false, /* read compacted */
InitialPosition.Latest);
sub2.addConsumer(consumer4);
assertEquals(sub2.getConsumers().size(), 1);
// check number of consumers on topic
assertEquals(topic.getNumberOfConsumers(), 3);
// 5. add consumer5 to sub2 but reach maxConsumersPerTopic
try {
Consumer consumer5 = new Consumer(sub2, SubType.Shared, topic.getName(), 5, /* consumer id */
0, "Cons5", /* consumer name */
50000, serverCnx, "myrole-1", Collections.emptyMap(), false, /* read compacted */
InitialPosition.Latest);
sub2.addConsumer(consumer5);
fail("should have failed");
} catch (BrokerServiceException e) {
assertTrue(e instanceof BrokerServiceException.ConsumerBusyException);
}
}
use of org.apache.pulsar.common.util.collections.ConcurrentOpenHashMap in project incubator-pulsar by apache.
the class PersistentTopicTest method testMaxConsumersFailover.
public void testMaxConsumersFailover() throws Exception {
PersistentTopic topic = new PersistentTopic(successTopicName, ledgerMock, brokerService);
PersistentSubscription sub = new PersistentSubscription(topic, "sub-1", cursorMock);
PersistentSubscription sub2 = new PersistentSubscription(topic, "sub-2", cursorMock);
// for count consumers on topic
ConcurrentOpenHashMap<String, PersistentSubscription> subscriptions = new ConcurrentOpenHashMap<>(16, 1);
subscriptions.put("sub-1", sub);
subscriptions.put("sub-2", sub2);
Field field = topic.getClass().getDeclaredField("subscriptions");
field.setAccessible(true);
field.set(topic, subscriptions);
// 1. add consumer1
Consumer consumer = new Consumer(sub, SubType.Failover, topic.getName(), 1, /* consumer id */
0, "Cons1", /* consumer name */
50000, serverCnx, "myrole-1", Collections.emptyMap(), false, /* read compacted */
InitialPosition.Latest);
sub.addConsumer(consumer);
assertEquals(sub.getConsumers().size(), 1);
// 2. add consumer2
Consumer consumer2 = new Consumer(sub, SubType.Failover, topic.getName(), 2, /* consumer id */
0, "Cons2", /* consumer name */
50000, serverCnx, "myrole-1", Collections.emptyMap(), false, /* read compacted */
InitialPosition.Latest);
sub.addConsumer(consumer2);
assertEquals(sub.getConsumers().size(), 2);
// 3. add consumer3 but reach maxConsumersPerSubscription
try {
Consumer consumer3 = new Consumer(sub, SubType.Failover, topic.getName(), 3, /* consumer id */
0, "Cons3", /* consumer name */
50000, serverCnx, "myrole-1", Collections.emptyMap(), false, /* read compacted */
InitialPosition.Latest);
sub.addConsumer(consumer3);
fail("should have failed");
} catch (BrokerServiceException e) {
assertTrue(e instanceof BrokerServiceException.ConsumerBusyException);
}
// check number of consumers on topic
assertEquals(topic.getNumberOfConsumers(), 2);
// 4. add consumer4 to sub2
Consumer consumer4 = new Consumer(sub2, SubType.Failover, topic.getName(), 4, /* consumer id */
0, "Cons4", /* consumer name */
50000, serverCnx, "myrole-1", Collections.emptyMap(), false, /* read compacted */
InitialPosition.Latest);
sub2.addConsumer(consumer4);
assertEquals(sub2.getConsumers().size(), 1);
// check number of consumers on topic
assertEquals(topic.getNumberOfConsumers(), 3);
// 5. add consumer5 to sub2 but reach maxConsumersPerTopic
try {
Consumer consumer5 = new Consumer(sub2, SubType.Failover, topic.getName(), 5, /* consumer id */
0, "Cons5", /* consumer name */
50000, serverCnx, "myrole-1", Collections.emptyMap(), false, /* read compacted */
InitialPosition.Latest);
sub2.addConsumer(consumer5);
fail("should have failed");
} catch (BrokerServiceException e) {
assertTrue(e instanceof BrokerServiceException.ConsumerBusyException);
}
}
use of org.apache.pulsar.common.util.collections.ConcurrentOpenHashMap in project incubator-pulsar by apache.
the class ReplicatorTest method testConcurrentReplicator.
@SuppressWarnings("unchecked")
@Test(timeOut = 30000)
public void testConcurrentReplicator() throws Exception {
log.info("--- Starting ReplicatorTest::testConcurrentReplicator ---");
final String namespace = "pulsar/global/concurrent";
admin1.namespaces().createNamespace(namespace);
admin1.namespaces().setNamespaceReplicationClusters(namespace, Lists.newArrayList("r1", "r2"));
final TopicName topicName = TopicName.get(String.format("persistent://" + namespace + "/topic-%d", 0));
PulsarClient client1 = PulsarClient.builder().serviceUrl(url1.toString()).statsInterval(0, TimeUnit.SECONDS).build();
Producer<byte[]> producer = client1.newProducer().topic(topicName.toString()).create();
producer.close();
PersistentTopic topic = (PersistentTopic) pulsar1.getBrokerService().getTopic(topicName.toString()).get();
PulsarClientImpl pulsarClient = spy((PulsarClientImpl) pulsar1.getBrokerService().getReplicationClient("r3"));
final Method startRepl = PersistentTopic.class.getDeclaredMethod("startReplicator", String.class);
startRepl.setAccessible(true);
Field replClientField = BrokerService.class.getDeclaredField("replicationClients");
replClientField.setAccessible(true);
ConcurrentOpenHashMap<String, PulsarClient> replicationClients = (ConcurrentOpenHashMap<String, PulsarClient>) replClientField.get(pulsar1.getBrokerService());
replicationClients.put("r3", pulsarClient);
admin1.namespaces().setNamespaceReplicationClusters(namespace, Lists.newArrayList("r1", "r2", "r3"));
ExecutorService executor = Executors.newFixedThreadPool(5);
for (int i = 0; i < 5; i++) {
executor.submit(() -> {
try {
startRepl.invoke(topic, "r3");
} catch (Exception e) {
fail("setting replicator failed", e);
}
});
}
Thread.sleep(3000);
Mockito.verify(pulsarClient, Mockito.times(1)).createProducerAsync(Mockito.any(ProducerConfigurationData.class), Mockito.any(Schema.class));
client1.shutdown();
}
use of org.apache.pulsar.common.util.collections.ConcurrentOpenHashMap in project incubator-pulsar by apache.
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();
}
Aggregations