Search in sources :

Example 1 with ConsumerStatsImpl

use of org.apache.pulsar.common.policies.data.stats.ConsumerStatsImpl in project pulsar by apache.

the class PersistentSubscription method removeConsumer.

@Override
public synchronized void removeConsumer(Consumer consumer, boolean isResetCursor) throws BrokerServiceException {
    cursor.updateLastActive();
    if (dispatcher != null) {
        dispatcher.removeConsumer(consumer);
    }
    // preserve accumulative stats form removed consumer
    ConsumerStatsImpl stats = consumer.getStats();
    bytesOutFromRemovedConsumers.add(stats.bytesOutCounter);
    msgOutFromRemovedConsumer.add(stats.msgOutCounter);
    if (dispatcher != null && dispatcher.getConsumers().isEmpty()) {
        deactivateCursor();
        topic.getManagedLedger().removeWaitingCursor(cursor);
        if (!cursor.isDurable()) {
            // If cursor is not durable, we need to clean up the subscription as well
            this.close().thenRun(() -> {
                synchronized (this) {
                    if (dispatcher != null) {
                        dispatcher.close().thenRun(() -> {
                            log.info("[{}][{}] Successfully closed dispatcher for reader", topicName, subName);
                        }).exceptionally(ex -> {
                            log.error("[{}][{}] Failed to close dispatcher for reader", topicName, subName, ex);
                            return null;
                        });
                    }
                }
            }).exceptionally(exception -> {
                log.error("[{}][{}] Failed to close subscription for reader", topicName, subName, exception);
                return null;
            });
            // when topic closes: it iterates through concurrent-subscription map to close each subscription. so,
            // topic.remove again try to access same map which creates deadlock. so, execute it in different thread.
            topic.getBrokerService().pulsar().getExecutor().submit(() -> {
                topic.removeSubscription(subName);
                // Because data deletion depends on the mark delete position of all cursors.
                if (!isResetCursor) {
                    try {
                        topic.getManagedLedger().deleteCursor(cursor.getName());
                    } catch (InterruptedException | ManagedLedgerException e) {
                        log.warn("[{}] [{}] Failed to remove non durable cursor", topic.getName(), subName, e);
                    }
                }
            });
        }
    }
    // invalid consumer remove will throw an exception
    // decrement usage is triggered only for valid consumer close
    topic.decrementUsageCount();
    if (log.isDebugEnabled()) {
        log.debug("[{}] [{}] [{}] Removed consumer -- count: {}", topic.getName(), subName, consumer.consumerName(), topic.currentUsageCount());
    }
}
Also used : SubscriptionStatsImpl(org.apache.pulsar.common.policies.data.stats.SubscriptionStatsImpl) Topic(org.apache.pulsar.broker.service.Topic) NotAllowedException(org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException) LoggerFactory(org.slf4j.LoggerFactory) DeleteCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteCallback) ClearBacklogCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.ClearBacklogCallback) TxnID(org.apache.pulsar.client.api.transaction.TxnID) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) MutablePair(org.apache.commons.lang3.tuple.MutablePair) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) Map(java.util.Map) PendingAckHandleDisabled(org.apache.pulsar.broker.transaction.pendingack.impl.PendingAckHandleDisabled) TxnAction(org.apache.pulsar.common.api.proto.TxnAction) BrokerInterceptor(org.apache.pulsar.broker.intercept.BrokerInterceptor) SubscriptionFencedException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionFencedException) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) Position(org.apache.bookkeeper.mledger.Position) IndividualDeletedEntries(org.apache.bookkeeper.mledger.ManagedCursor.IndividualDeletedEntries) Collectors(java.util.stream.Collectors) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) SubscriptionBusyException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) ManagedCursorImpl(org.apache.bookkeeper.mledger.impl.ManagedCursorImpl) List(java.util.List) FutureUtil(org.apache.pulsar.common.util.FutureUtil) TransactionPendingAckStats(org.apache.pulsar.common.policies.data.TransactionPendingAckStats) Optional(java.util.Optional) InvalidCursorPositionException(org.apache.bookkeeper.mledger.ManagedLedgerException.InvalidCursorPositionException) AsyncCallbacks(org.apache.bookkeeper.mledger.AsyncCallbacks) TransactionInPendingAckStats(org.apache.pulsar.common.policies.data.TransactionInPendingAckStats) LongAdder(java.util.concurrent.atomic.LongAdder) AtomicIntegerFieldUpdater(java.util.concurrent.atomic.AtomicIntegerFieldUpdater) Subscription(org.apache.pulsar.broker.service.Subscription) TopicName(org.apache.pulsar.common.naming.TopicName) AckType(org.apache.pulsar.common.api.proto.CommandAck.AckType) Entry(org.apache.bookkeeper.mledger.Entry) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ReadEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntryCallback) Consumer(org.apache.pulsar.broker.service.Consumer) KeySharedMeta(org.apache.pulsar.common.api.proto.KeySharedMeta) LinkedHashMap(java.util.LinkedHashMap) SubType(org.apache.pulsar.common.api.proto.CommandSubscribe.SubType) ServerMetadataException(org.apache.pulsar.broker.service.BrokerServiceException.ServerMetadataException) ReplicatedSubscriptionsSnapshot(org.apache.pulsar.common.api.proto.ReplicatedSubscriptionsSnapshot) ConsumerStatsImpl(org.apache.pulsar.common.policies.data.stats.ConsumerStatsImpl) ManagedLedgerImpl(org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl) MapUtils(org.apache.commons.collections4.MapUtils) Logger(org.slf4j.Logger) Dispatcher(org.apache.pulsar.broker.service.Dispatcher) ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) PendingAckHandleImpl(org.apache.pulsar.broker.transaction.pendingack.impl.PendingAckHandleImpl) BrokerServiceException(org.apache.pulsar.broker.service.BrokerServiceException) MoreObjects(com.google.common.base.MoreObjects) ConcurrentFindCursorPositionException(org.apache.bookkeeper.mledger.ManagedLedgerException.ConcurrentFindCursorPositionException) EventsTopicNames.checkTopicIsEventsNames(org.apache.pulsar.common.events.EventsTopicNames.checkTopicIsEventsNames) ExecutionException(java.util.concurrent.ExecutionException) PendingAckHandle(org.apache.pulsar.broker.transaction.pendingack.PendingAckHandle) TreeMap(java.util.TreeMap) MarkDeleteCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback) Range(org.apache.pulsar.client.api.Range) SubscriptionInvalidCursorPosition(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionInvalidCursorPosition) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ConsumerStatsImpl(org.apache.pulsar.common.policies.data.stats.ConsumerStatsImpl)

Example 2 with ConsumerStatsImpl

use of org.apache.pulsar.common.policies.data.stats.ConsumerStatsImpl in project pulsar by apache.

the class ConsumerStatsTest method testConsumerStats.

@Test
public void testConsumerStats() {
    ConsumerStatsImpl stats = new ConsumerStatsImpl();
    assertNull(stats.getAddress());
    assertNull(stats.getClientVersion());
    assertNull(stats.getConnectedSince());
    stats.setAddress("address");
    assertEquals(stats.getAddress(), "address");
    stats.setAddress("address1");
    assertEquals(stats.getAddress(), "address1");
    stats.setClientVersion("version");
    assertEquals(stats.getClientVersion(), "version");
    assertEquals(stats.getAddress(), "address1");
    stats.setConnectedSince("connected");
    assertEquals(stats.getConnectedSince(), "connected");
    assertEquals(stats.getAddress(), "address1");
    assertEquals(stats.getClientVersion(), "version");
    stats.setAddress(null);
    assertNull(stats.getAddress());
    stats.setConnectedSince("");
    assertEquals(stats.getConnectedSince(), "");
    stats.setClientVersion("version2");
    assertEquals(stats.getClientVersion(), "version2");
    assertNull(stats.getAddress());
    assertEquals(stats.getClientVersion(), "version2");
    stats.setConnectedSince(null);
    stats.setClientVersion(null);
    assertNull(stats.getConnectedSince());
    assertNull(stats.getClientVersion());
}
Also used : ConsumerStatsImpl(org.apache.pulsar.common.policies.data.stats.ConsumerStatsImpl) Test(org.testng.annotations.Test)

Example 3 with ConsumerStatsImpl

use of org.apache.pulsar.common.policies.data.stats.ConsumerStatsImpl in project pulsar by apache.

the class ServerCnx method createConsumerStatsResponse.

ByteBuf createConsumerStatsResponse(Consumer consumer, long requestId) {
    ConsumerStatsImpl consumerStats = consumer.getStats();
    Subscription subscription = consumer.getSubscription();
    BaseCommand cmd = Commands.newConsumerStatsResponseCommand(ServerError.UnknownError, null, requestId);
    cmd.getConsumerStatsResponse().clearErrorCode().setRequestId(requestId).setMsgRateOut(consumerStats.msgRateOut).setMsgThroughputOut(consumerStats.msgThroughputOut).setMsgRateRedeliver(consumerStats.msgRateRedeliver).setConsumerName(consumerStats.consumerName).setAvailablePermits(consumerStats.availablePermits).setUnackedMessages(consumerStats.unackedMessages).setBlockedConsumerOnUnackedMsgs(consumerStats.blockedConsumerOnUnackedMsgs).setAddress(consumerStats.getAddress()).setConnectedSince(consumerStats.getConnectedSince()).setMsgBacklog(subscription.getNumberOfEntriesInBacklog(false)).setMsgRateExpired(subscription.getExpiredMessageRate()).setType(subscription.getTypeString());
    return Commands.serializeWithSize(cmd);
}
Also used : BaseCommand(org.apache.pulsar.common.api.proto.BaseCommand) ConsumerStatsImpl(org.apache.pulsar.common.policies.data.stats.ConsumerStatsImpl) CommandEndTxnOnSubscription(org.apache.pulsar.common.api.proto.CommandEndTxnOnSubscription) PersistentSubscription(org.apache.pulsar.broker.service.persistent.PersistentSubscription)

Example 4 with ConsumerStatsImpl

use of org.apache.pulsar.common.policies.data.stats.ConsumerStatsImpl in project pulsar by apache.

the class NonPersistentSubscription method getStats.

public NonPersistentSubscriptionStatsImpl getStats() {
    NonPersistentSubscriptionStatsImpl subStats = new NonPersistentSubscriptionStatsImpl();
    subStats.bytesOutCounter = bytesOutFromRemovedConsumers.longValue();
    subStats.msgOutCounter = msgOutFromRemovedConsumer.longValue();
    NonPersistentDispatcher dispatcher = this.dispatcher;
    if (dispatcher != null) {
        dispatcher.getConsumers().forEach(consumer -> {
            ConsumerStatsImpl consumerStats = consumer.getStats();
            subStats.consumers.add(consumerStats);
            subStats.msgRateOut += consumerStats.msgRateOut;
            subStats.msgThroughputOut += consumerStats.msgThroughputOut;
            subStats.bytesOutCounter += consumerStats.bytesOutCounter;
            subStats.msgOutCounter += consumerStats.msgOutCounter;
            subStats.msgRateRedeliver += consumerStats.msgRateRedeliver;
        });
    }
    subStats.type = getTypeString();
    subStats.msgDropRate = dispatcher.getMessageDropRate().getValueRate();
    KeySharedMode keySharedMode = this.keySharedMode;
    if (getType() == SubType.Key_Shared && keySharedMode != null) {
        subStats.keySharedMode = keySharedMode.toString();
    }
    return subStats;
}
Also used : KeySharedMode(org.apache.pulsar.common.api.proto.KeySharedMode) ConsumerStatsImpl(org.apache.pulsar.common.policies.data.stats.ConsumerStatsImpl) NonPersistentSubscriptionStatsImpl(org.apache.pulsar.common.policies.data.stats.NonPersistentSubscriptionStatsImpl)

Example 5 with ConsumerStatsImpl

use of org.apache.pulsar.common.policies.data.stats.ConsumerStatsImpl in project pulsar by apache.

the class NonPersistentSubscription method removeConsumer.

@Override
public synchronized void removeConsumer(Consumer consumer, boolean isResetCursor) throws BrokerServiceException {
    updateLastActive();
    if (dispatcher != null) {
        dispatcher.removeConsumer(consumer);
    }
    // preserve accumulative stats form removed consumer
    ConsumerStatsImpl stats = consumer.getStats();
    bytesOutFromRemovedConsumers.add(stats.bytesOutCounter);
    msgOutFromRemovedConsumer.add(stats.msgOutCounter);
    if (!isDurable) {
        topic.unsubscribe(subName);
    }
    // invalid consumer remove will throw an exception
    // decrement usage is triggered only for valid consumer close
    topic.decrementUsageCount();
    if (log.isDebugEnabled()) {
        log.debug("[{}] [{}] [{}] Removed consumer -- count: {}", topic.getName(), subName, consumer.consumerName(), topic.currentUsageCount());
    }
}
Also used : ConsumerStatsImpl(org.apache.pulsar.common.policies.data.stats.ConsumerStatsImpl)

Aggregations

ConsumerStatsImpl (org.apache.pulsar.common.policies.data.stats.ConsumerStatsImpl)10 ExecutionException (java.util.concurrent.ExecutionException)4 PulsarServerException (org.apache.pulsar.broker.PulsarServerException)3 BrokerServiceException (org.apache.pulsar.broker.service.BrokerServiceException)3 NotAllowedException (org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException)3 SubscriptionBusyException (org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException)3 Consumer (org.apache.pulsar.broker.service.Consumer)3 List (java.util.List)2 Optional (java.util.Optional)2 LongAdder (java.util.concurrent.atomic.LongAdder)2 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)2 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)2 ManagedLedgerImpl (org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl)2 PositionImpl (org.apache.bookkeeper.mledger.impl.PositionImpl)2 ConsumerBusyException (org.apache.pulsar.broker.service.BrokerServiceException.ConsumerBusyException)2 NamingException (org.apache.pulsar.broker.service.BrokerServiceException.NamingException)2 TopicBusyException (org.apache.pulsar.broker.service.BrokerServiceException.TopicBusyException)2 TopicFencedException (org.apache.pulsar.broker.service.BrokerServiceException.TopicFencedException)2 UnsupportedVersionException (org.apache.pulsar.broker.service.BrokerServiceException.UnsupportedVersionException)2 PersistentTopic (org.apache.pulsar.broker.service.persistent.PersistentTopic)2