hashgraph / hedera-services

Crypto, token, consensus, file, and smart contract services for the Hedera public ledger
Apache License 2.0
266 stars 119 forks source link

Learner virtual tree view processes teacher responses in a too synchronized manner #14067

Open artemananiev opened 4 days ago

artemananiev commented 4 days ago

This is a part of https://github.com/hashgraph/hedera-services/issues/12108 and a follow-up for https://github.com/hashgraph/hedera-services/issues/13898.

After #13898 is implemented, the next bottleneck is on the learner side, this method in LearnerPullVirtualTreeView:

synchronized void responseReceived(final PullVirtualTreeResponse response) {}

There are many threads receiving responses from the teacher, but all of them in the end call the method above. Since the method is synchronized, all threads get stuck waiting to acquire the monitor.

The primary reason this method is synchronized is because it iterates over unsynchronized collections like responses or anticipatedPaths. This is needed to re-order teacher responses, so they are processed in the same order as they were originally sent by the learner. The order is not critical for internal nodes, but when it comes to leaves, they must be processed in ascending path order, this is the current restriction of virtual hasher.

A solution could be to change the method above to be not synchronized, and move leaf path ordering code to dirty leaf iterator. This is the iterator that supplies leaves to virtual hasher.