synchronize peer manager's reported peers to fix concurrency error

This commit is contained in:
woodser 2024-01-01 08:32:54 -05:00
parent 5d88936600
commit 9a6a9ac93e

View file

@ -365,6 +365,7 @@ public final class PeerManager implements ConnectionListener, PersistedDataHost
// We check if the reported msg is not violating our rules // We check if the reported msg is not violating our rules
if (peers.size() <= (MAX_REPORTED_PEERS + maxConnectionsAbsolute + 10)) { if (peers.size() <= (MAX_REPORTED_PEERS + maxConnectionsAbsolute + 10)) {
synchronized (reportedPeers) {
reportedPeers.addAll(peers); reportedPeers.addAll(peers);
purgeReportedPeersIfExceeds(); purgeReportedPeersIfExceeds();
@ -373,6 +374,7 @@ public final class PeerManager implements ConnectionListener, PersistedDataHost
requestPersistence(); requestPersistence();
printReportedPeers(); printReportedPeers();
}
} else { } else {
// If a node is trying to send too many list we treat it as rule violation. // If a node is trying to send too many list we treat it as rule violation.
// Reported list include the connected list. We use the max value and give some extra headroom. // Reported list include the connected list. We use the max value and give some extra headroom.
@ -589,10 +591,13 @@ public final class PeerManager implements ConnectionListener, PersistedDataHost
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
private void removeReportedPeer(Peer reportedPeer) { private void removeReportedPeer(Peer reportedPeer) {
synchronized (reportedPeers) {
reportedPeers.remove(reportedPeer); reportedPeers.remove(reportedPeer);
printReportedPeers(); printReportedPeers();
} }
}
private void removeReportedPeer(NodeAddress nodeAddress) { private void removeReportedPeer(NodeAddress nodeAddress) {
List<Peer> reportedPeersClone = new ArrayList<>(reportedPeers); List<Peer> reportedPeersClone = new ArrayList<>(reportedPeers);
reportedPeersClone.stream() reportedPeersClone.stream()
@ -611,6 +616,7 @@ public final class PeerManager implements ConnectionListener, PersistedDataHost
private void purgeReportedPeersIfExceeds() { private void purgeReportedPeersIfExceeds() {
synchronized (reportedPeers) {
int size = reportedPeers.size(); int size = reportedPeers.size();
if (size > MAX_REPORTED_PEERS) { if (size > MAX_REPORTED_PEERS) {
log.info("We have already {} reported peers which exceeds our limit of {}." + log.info("We have already {} reported peers which exceeds our limit of {}." +
@ -628,8 +634,10 @@ public final class PeerManager implements ConnectionListener, PersistedDataHost
log.trace("No need to purge reported peers.\n\tWe don't have more then {} reported peers yet.", MAX_REPORTED_PEERS); log.trace("No need to purge reported peers.\n\tWe don't have more then {} reported peers yet.", MAX_REPORTED_PEERS);
} }
} }
}
private void printReportedPeers() { private void printReportedPeers() {
synchronized (reportedPeers) {
if (!reportedPeers.isEmpty()) { if (!reportedPeers.isEmpty()) {
if (PRINT_REPORTED_PEERS_DETAILS) { if (PRINT_REPORTED_PEERS_DETAILS) {
StringBuilder result = new StringBuilder("\n\n------------------------------------------------------------\n" + StringBuilder result = new StringBuilder("\n\n------------------------------------------------------------\n" +
@ -642,6 +650,7 @@ public final class PeerManager implements ConnectionListener, PersistedDataHost
log.debug("Number of reported peers: {}", reportedPeers.size()); log.debug("Number of reported peers: {}", reportedPeers.size());
} }
} }
}
private void printNewReportedPeers(Set<Peer> reportedPeers) { private void printNewReportedPeers(Set<Peer> reportedPeers) {
if (PRINT_REPORTED_PEERS_DETAILS) { if (PRINT_REPORTED_PEERS_DETAILS) {