moquette: High CPU usage when run Ctrie.insert function

Expected behavior

I found that the cpu usage of mqtt broker is high after running for one month,the cpu usage increase from 9% to 95% The number of CPU cores of the server is 24U I found that the ctrie tree is very large when the cpu usage is high, the tree contains a large number of tomb nodes (TNode) In this case, a large number of CPU resources are consumed by running function: CNode.anychidrenmatch.

1、Why do a large number of TNode nodes exist in the system and are not cleared?

2、Hope that the TNode node can be automatically cleared or a thread can be used to automatically clear the TNode when the TNode is not used.

3、What the following scenarios does the system not automatically clear TNodes?

if (cnode instanceof TNode) { // this inode is a tomb, has no clients and should be cleaned up // Because we implemented cleanTomb below, this should be rare, but possible // Consider calling cleanTomb here too return Action.OK; }

Actual behavior

when the usage is 5%, the number of TNodes is 151, but when the cpu usage is up to 95% , the number of TNodes is 7,065,591 The TNodes are not cleared, and the number is been increasing over time.

Steps to reproduce

Minimal yet complete reproducer code (or URL to code) or complete log file

Moquette MQTT version

0.16 https://github.com/moquette-io/moquette/archive/refs/tags/v0.16.tar.gz

JVM version (e.g. java -version)

1.8

OS version (e.g. uname -a)

About this issue

Most upvoted comments

OK, if (cnode instanceof TNode) should cleantomb again, until deleted successfully.

private Action remove(String clientId, Topic topic, INode inode, INode iParent) { Token token = topic.headToken(); if (!topic.isEmpty() && (inode.mainNode().anyChildrenMatch(token))) { Topic remainingTopic = topic.exceptHeadToken(); INode nextInode = inode.mainNode().childOf(token); return remove(clientId, remainingTopic, nextInode, inode); } else { final CNode cnode = inode.mainNode(); if (cnode instanceof TNode) { // this inode is a tomb, has no clients and should be cleaned up // Because we implemented cleanTomb below, this should be rare, but possible // Consider calling cleanTomb here too return cleanTomb(inode, iParent); } if (cnode.containsOnly(clientId) && topic.isEmpty() && cnode.allChildren().isEmpty()) { // last client to leave this node, AND there are no downstream children, remove via TNode tomb if (inode == this.root) { return inode.compareAndSet(cnode, inode.mainNode().copy()) ? Action.OK : Action.REPEAT; } TNode tnode = new TNode(); return inode.compareAndSet(cnode, tnode) ? cleanTomb(inode, iParent) : Action.REPEAT; } else if (cnode.contains(clientId) && topic.isEmpty()) { CNode updatedCnode = cnode.copy(); updatedCnode.removeSubscriptionsFor(clientId); return inode.compareAndSet(cnode, updatedCnode) ? Action.OK : Action.REPEAT; } else { //someone else already removed return Action.OK; }