7311: Rename PeerManager to PeerSelector

Signed-off-by: Matilda Clerke <matilda.clerke@consensys.net>
pull/7628/head
Matilda Clerke 2 months ago
parent 6e349e16f8
commit ad86ae6e71
  1. 6
      ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/peertask/DefaultPeerSelector.java
  2. 16
      ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/peertask/PeerSelector.java
  3. 8
      ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/peertask/PeerTaskExecutor.java
  4. 16
      ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/peertask/DefaultPeerSelectorTest.java
  5. 8
      ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/peertask/PeerTaskExecutorTest.java

@ -28,11 +28,11 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
/** /**
* This is a simple PeerManager implementation that can be used the default implementation in most * This is a simple PeerSelector implementation that can be used the default implementation in most
* situations * situations
*/ */
public class DefaultPeerManager implements PeerManager { public class DefaultPeerSelector implements PeerSelector {
private static final Logger LOG = LoggerFactory.getLogger(DefaultPeerManager.class); private static final Logger LOG = LoggerFactory.getLogger(DefaultPeerSelector.class);
// use a synchronized map to ensure the map is never modified by multiple threads at once // use a synchronized map to ensure the map is never modified by multiple threads at once
private final Map<PeerId, EthPeer> ethPeersByPeerId = private final Map<PeerId, EthPeer> ethPeersByPeerId =

@ -20,11 +20,11 @@ import org.hyperledger.besu.ethereum.p2p.peers.PeerId;
import java.util.Optional; import java.util.Optional;
import java.util.function.Predicate; import java.util.function.Predicate;
/** "Manages" the EthPeers for the PeerTaskExecutor */ /** Selects the EthPeers for the PeerTaskExecutor */
public interface PeerManager { public interface PeerSelector {
/** /**
* Gets the highest reputation peer matching the supplies filter * Gets the highest reputation peer matching the supplied filter
* *
* @param filter a filter to match prospective peers with * @param filter a filter to match prospective peers with
* @return the highest reputation peer matching the supplies filter * @return the highest reputation peer matching the supplies filter
@ -37,21 +37,21 @@ public interface PeerManager {
* *
* @param peerId the peerId of the desired EthPeer * @param peerId the peerId of the desired EthPeer
* @return An Optional\<EthPeer\> containing the EthPeer identified by peerId if present in the * @return An Optional\<EthPeer\> containing the EthPeer identified by peerId if present in the
* PeerManager, or empty otherwise * PeerSelector, or empty otherwise
*/ */
Optional<EthPeer> getPeerByPeerId(final PeerId peerId); Optional<EthPeer> getPeerByPeerId(final PeerId peerId);
/** /**
* Add the supplied EthPeer to the PeerManager * Add the supplied EthPeer to the PeerSelector
* *
* @param ethPeer the EthPeer to be added to the PeerManager * @param ethPeer the EthPeer to be added to the PeerSelector
*/ */
void addPeer(final EthPeer ethPeer); void addPeer(final EthPeer ethPeer);
/** /**
* Remove the EthPeer identified by peerId from the PeerManager * Remove the EthPeer identified by peerId from the PeerSelector
* *
* @param peerId the PeerId of the EthPeer to be removed from the PeerManager * @param peerId the PeerId of the EthPeer to be removed from the PeerSelector
*/ */
void removePeer(final PeerId peerId); void removePeer(final PeerId peerId);
} }

@ -34,17 +34,17 @@ import java.util.function.Supplier;
public class PeerTaskExecutor { public class PeerTaskExecutor {
private static final long[] WAIT_TIME_BEFORE_RETRY = {0, 20000, 5000}; private static final long[] WAIT_TIME_BEFORE_RETRY = {0, 20000, 5000};
private final PeerManager peerManager; private final PeerSelector peerSelector;
private final PeerTaskRequestSender requestSender; private final PeerTaskRequestSender requestSender;
private final Supplier<ProtocolSpec> protocolSpecSupplier; private final Supplier<ProtocolSpec> protocolSpecSupplier;
private final LabelledMetric<OperationTimer> requestTimer; private final LabelledMetric<OperationTimer> requestTimer;
public PeerTaskExecutor( public PeerTaskExecutor(
final PeerManager peerManager, final PeerSelector peerSelector,
final PeerTaskRequestSender requestSender, final PeerTaskRequestSender requestSender,
final Supplier<ProtocolSpec> protocolSpecSupplier, final Supplier<ProtocolSpec> protocolSpecSupplier,
final MetricsSystem metricsSystem) { final MetricsSystem metricsSystem) {
this.peerManager = peerManager; this.peerSelector = peerSelector;
this.requestSender = requestSender; this.requestSender = requestSender;
this.protocolSpecSupplier = protocolSpecSupplier; this.protocolSpecSupplier = protocolSpecSupplier;
requestTimer = requestTimer =
@ -61,7 +61,7 @@ public class PeerTaskExecutor {
EthPeer peer; EthPeer peer;
try { try {
peer = peer =
peerManager.getPeer( peerSelector.getPeer(
(candidatePeer) -> (candidatePeer) ->
isPeerUnused(candidatePeer, usedEthPeers) isPeerUnused(candidatePeer, usedEthPeers)
&& (protocolSpecSupplier.get().isPoS() && (protocolSpecSupplier.get().isPoS()

@ -28,31 +28,31 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
public class DefaultPeerManagerTest { public class DefaultPeerSelectorTest {
public DefaultPeerManager peerManager; public DefaultPeerSelector peerSelector;
@BeforeEach @BeforeEach
public void beforeTest() { public void beforeTest() {
peerManager = new DefaultPeerManager(); peerSelector = new DefaultPeerSelector();
} }
@Test @Test
public void testGetPeer() throws NoAvailablePeerException { public void testGetPeer() throws NoAvailablePeerException {
EthPeer protocol1With5ReputationPeer = EthPeer protocol1With5ReputationPeer =
createTestPeer(Set.of(Capability.create("capability1", 1)), "protocol1", 5); createTestPeer(Set.of(Capability.create("capability1", 1)), "protocol1", 5);
peerManager.addPeer(protocol1With5ReputationPeer); peerSelector.addPeer(protocol1With5ReputationPeer);
EthPeer protocol1With4ReputationPeer = EthPeer protocol1With4ReputationPeer =
createTestPeer(Set.of(Capability.create("capability1", 1)), "protocol1", 4); createTestPeer(Set.of(Capability.create("capability1", 1)), "protocol1", 4);
peerManager.addPeer(protocol1With4ReputationPeer); peerSelector.addPeer(protocol1With4ReputationPeer);
EthPeer protocol2With50ReputationPeer = EthPeer protocol2With50ReputationPeer =
createTestPeer(Set.of(Capability.create("capability1", 1)), "protocol2", 50); createTestPeer(Set.of(Capability.create("capability1", 1)), "protocol2", 50);
peerManager.addPeer(protocol2With50ReputationPeer); peerSelector.addPeer(protocol2With50ReputationPeer);
EthPeer protocol2With4ReputationPeer = EthPeer protocol2With4ReputationPeer =
createTestPeer(Set.of(Capability.create("capability1", 1)), "protocol2", 4); createTestPeer(Set.of(Capability.create("capability1", 1)), "protocol2", 4);
peerManager.addPeer(protocol2With4ReputationPeer); peerSelector.addPeer(protocol2With4ReputationPeer);
EthPeer result = peerManager.getPeer((p) -> p.getProtocolName().equals("protocol1")); EthPeer result = peerSelector.getPeer((p) -> p.getProtocolName().equals("protocol1"));
Assertions.assertSame(protocol1With5ReputationPeer, result); Assertions.assertSame(protocol1With5ReputationPeer, result);
} }

@ -35,7 +35,7 @@ import org.mockito.Mockito;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
public class PeerTaskExecutorTest { public class PeerTaskExecutorTest {
private @Mock PeerManager peerManager; private @Mock PeerSelector peerSelector;
private @Mock PeerTaskRequestSender requestSender; private @Mock PeerTaskRequestSender requestSender;
private @Mock ProtocolSpec protocolSpec; private @Mock ProtocolSpec protocolSpec;
private @Mock PeerTask<Object> peerTask; private @Mock PeerTask<Object> peerTask;
@ -51,7 +51,7 @@ public class PeerTaskExecutorTest {
mockCloser = MockitoAnnotations.openMocks(this); mockCloser = MockitoAnnotations.openMocks(this);
peerTaskExecutor = peerTaskExecutor =
new PeerTaskExecutor( new PeerTaskExecutor(
peerManager, requestSender, () -> protocolSpec, new NoOpMetricsSystem()); peerSelector, requestSender, () -> protocolSpec, new NoOpMetricsSystem());
} }
@AfterEach @AfterEach
@ -205,7 +205,7 @@ public class PeerTaskExecutorTest {
String subprotocol = "subprotocol"; String subprotocol = "subprotocol";
Object responseObject = new Object(); Object responseObject = new Object();
Mockito.when(peerManager.getPeer(Mockito.any(Predicate.class))).thenReturn(ethPeer); Mockito.when(peerSelector.getPeer(Mockito.any(Predicate.class))).thenReturn(ethPeer);
Mockito.when(peerTask.getRequestMessage()).thenReturn(requestMessageData); Mockito.when(peerTask.getRequestMessage()).thenReturn(requestMessageData);
Mockito.when(peerTask.getPeerTaskBehaviors()).thenReturn(Collections.emptyList()); Mockito.when(peerTask.getPeerTaskBehaviors()).thenReturn(Collections.emptyList());
@ -238,7 +238,7 @@ public class PeerTaskExecutorTest {
int requestMessageDataCode = 123; int requestMessageDataCode = 123;
EthPeer peer2 = Mockito.mock(EthPeer.class); EthPeer peer2 = Mockito.mock(EthPeer.class);
Mockito.when(peerManager.getPeer(Mockito.any(Predicate.class))) Mockito.when(peerSelector.getPeer(Mockito.any(Predicate.class)))
.thenReturn(ethPeer) .thenReturn(ethPeer)
.thenReturn(peer2); .thenReturn(peer2);

Loading…
Cancel
Save