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());
}
}
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());
}
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);
}
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;
}
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());
}
}
Aggregations