Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Move to fully connected network #13010

Merged
merged 23 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
f23c2ac
12984 use peers for Network component
kfa-aguda Apr 24, 2024
e27c958
12984 use peers for Network component
kfa-aguda Apr 25, 2024
5297e23
Revert "12984 use peers for Network component"
kfa-aguda Apr 25, 2024
9793eae
12984 use peers for Network component
kfa-aguda Apr 25, 2024
2d70913
12984 use peers for Network component
kfa-aguda Apr 25, 2024
5368df8
12984 use peers for Network component
kfa-aguda Apr 25, 2024
3808eae
12984 use peers for Network component
kfa-aguda Apr 30, 2024
e46101c
Merge branch 'develop' into 12984-no-addressBook
kfa-aguda Apr 30, 2024
79b929b
12984 use peers for Network component
kfa-aguda Apr 30, 2024
74d668d
12984 use peers for Network component
kfa-aguda Apr 30, 2024
dd516d0
12984 use peers for Network component
kfa-aguda Apr 30, 2024
01a3870
12984 use peers for Network component
kfa-aguda May 24, 2024
28830a4
12984 use peers for Network component
kfa-aguda May 24, 2024
490ce6e
Merge branch 'develop' into 12984-no-addressBook
kfa-aguda May 24, 2024
284ff3f
12984 use peers for Network component
kfa-aguda May 24, 2024
1dce814
12984 use peers for Network component
kfa-aguda May 24, 2024
b3de315
spotless
kfa-aguda May 24, 2024
06b7dcb
adjust neighbours
kfa-aguda May 24, 2024
3343437
Merge branch 'develop' into 12984-no-addressBook
kfa-aguda May 24, 2024
a60dec7
adjust neighbours
kfa-aguda May 29, 2024
c602e58
adjust neighbours
kfa-aguda Jun 3, 2024
024e0f7
Merge branch 'develop' into 12984-no-addressBook
kfa-aguda Jun 3, 2024
c25245b
adjust neighbours
kfa-aguda Jun 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,10 @@ protected SyncGossip(
final ThreadConfig threadConfig = platformContext.getConfiguration().getConfigData(ThreadConfig.class);

final BasicConfig basicConfig = platformContext.getConfiguration().getConfigData(BasicConfig.class);

topology = new StaticTopology(random, addressBook, selfId, basicConfig.numConnections());
final List<PeerInfo> peers = Utilities.createPeerInfoList(addressBook, selfId);

topology =
new StaticTopology(random, peers, addressBook.getIndexOfNodeId(selfId), basicConfig.numConnections());
kfa-aguda marked this conversation as resolved.
Show resolved Hide resolved
final NetworkPeerIdentifier peerIdentifier = new NetworkPeerIdentifier(platformContext, peers);
final SocketFactory socketFactory =
NetworkUtils.createSocketFactory(selfId, peers, keysAndCerts, platformContext.getConfiguration());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.swirlds.common.threading.locks.locked.LockedResource;
import com.swirlds.platform.network.connection.NotConnectedConnection;
import com.swirlds.platform.network.connectivity.OutboundConnectionCreator;
import java.util.Objects;

/**
* Manages a connection that is initiated by this node. If the connection in use is broken, it will try to establish a
Expand All @@ -46,7 +47,7 @@ public OutboundConnectionManager(final NodeId peerId, final OutboundConnectionCr
@Override
public Connection waitForConnection() {
try (final LockedResource<Connection> resource = lock.lock()) {
while (!resource.getResource().connected()) {
while (!Objects.requireNonNull(resource.getResource()).connected()) {
kfa-aguda marked this conversation as resolved.
Show resolved Hide resolved
resource.getResource().disconnect();
resource.setResource(connectionCreator.createConnection(peerId));
}
Expand Down
kfa-aguda marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
package com.swirlds.platform.network.topology;

import com.swirlds.common.platform.NodeId;
import com.swirlds.platform.network.PeerInfo;
import com.swirlds.platform.network.RandomGraph;
import com.swirlds.platform.system.address.AddressBook;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Random;
Expand All @@ -32,35 +31,33 @@
public class StaticTopology implements NetworkTopology {
private static final long SEED = 0;

private final NodeId selfId;
private final List<NodeId> peerNodes;

/**
* Two nodes are neighbors if their indexes in the address book are neighbors in the connection graph.
* Two nodes are neighbors if their node indexes are neighbors in the connection graph.
*/
private final AddressBook addressBook;

private final RandomGraph connectionGraph;

private final int selfIndex;

/**
* Constructor.
*
* @param random a source of randomness, used to chose random neighbors, does not need to be
* cryptographically secure
* @param addressBook the current address book
* @param selfId the ID of this node
* @param peers the set of peers in the network
* @param selfIndex the index of this node in the address book
* @param numberOfNeighbors the number of neighbors each node should have
*/
public StaticTopology(
@NonNull final Random random,
@NonNull final AddressBook addressBook,
@NonNull final NodeId selfId,
@NonNull final List<PeerInfo> peers,
kfa-aguda marked this conversation as resolved.
Show resolved Hide resolved
final int selfIndex,
final int numberOfNeighbors) {
this.addressBook = Objects.requireNonNull(addressBook, "addressBook must not be null");
this.selfId = Objects.requireNonNull(selfId, "selfId must not be null");
this.connectionGraph = new RandomGraph(random, addressBook.getSize(), numberOfNeighbors, SEED);

if (!addressBook.contains(selfId)) {
throw new IllegalArgumentException("Address book does not contain selfId");
}
this.peerNodes =
Objects.requireNonNull(peers.stream().map(PeerInfo::nodeId).toList(), "peers must not be null");
kfa-aguda marked this conversation as resolved.
Show resolved Hide resolved
this.selfIndex = selfIndex;
this.connectionGraph = new RandomGraph(random, peers.size() + 1, numberOfNeighbors, SEED);
}

/**
Expand All @@ -76,19 +73,16 @@ public List<NodeId> getNeighbors() {
*/
@Override
public List<NodeId> getNeighbors(final Predicate<NodeId> filter) {
kfa-aguda marked this conversation as resolved.
Show resolved Hide resolved
final int selfIndex = addressBook.getIndexOfNodeId(selfId);
return Arrays.stream(connectionGraph.getNeighbors(selfIndex))
.mapToObj(addressBook::getNodeId)
.filter(filter)
.toList();
return peerNodes;
}

/**
* {@inheritDoc}
*/
@Override
public boolean shouldConnectToMe(final NodeId nodeId) {
return isNeighbor(nodeId) && addressBook.getIndexOfNodeId(nodeId) < addressBook.getIndexOfNodeId(selfId);
final int nodeIndex = getIndexOfNodeId(nodeId);
return isNeighbor(nodeId) && nodeIndex < selfIndex;
}

/**
Expand All @@ -98,11 +92,10 @@ public boolean shouldConnectToMe(final NodeId nodeId) {
* @return true if this node is my neighbor, false if not
*/
private boolean isNeighbor(final NodeId nodeId) {
kfa-aguda marked this conversation as resolved.
Show resolved Hide resolved
if (!addressBook.contains(nodeId)) {
if (!peerNodes.contains(nodeId)) {
return false;
}
final int selfIndex = addressBook.getIndexOfNodeId(selfId);
final int nodeIndex = addressBook.getIndexOfNodeId(nodeId);
final int nodeIndex = getIndexOfNodeId(nodeId);
return connectionGraph.isAdjacent(selfIndex, nodeIndex);
}

Expand All @@ -111,7 +104,8 @@ private boolean isNeighbor(final NodeId nodeId) {
*/
@Override
public boolean shouldConnectTo(final NodeId nodeId) {
return isNeighbor(nodeId) && addressBook.getIndexOfNodeId(nodeId) > addressBook.getIndexOfNodeId(selfId);
final int nodeIndex = getIndexOfNodeId(nodeId);
return isNeighbor(nodeId) && nodeIndex > selfIndex;
}

/**
Expand All @@ -121,4 +115,15 @@ public boolean shouldConnectTo(final NodeId nodeId) {
public RandomGraph getConnectionGraph() {
return connectionGraph;
}

/**
* Returns the index of the given node in the peer list, which must not be the self index
*
* @param nodeId the node ID
* @return the index of the node in the peer list
*/
private int getIndexOfNodeId(@NonNull final NodeId nodeId) {
alittley marked this conversation as resolved.
Show resolved Hide resolved
final int index = peerNodes.indexOf(nodeId);
kfa-aguda marked this conversation as resolved.
Show resolved Hide resolved
return selfIndex == index ? index + 1 : index;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@

import com.swirlds.common.platform.NodeId;
import com.swirlds.common.test.fixtures.RandomUtils;
import com.swirlds.platform.Utilities;
import com.swirlds.platform.network.Connection;
import com.swirlds.platform.network.ConnectionManager;
import com.swirlds.platform.network.PeerInfo;
import com.swirlds.platform.network.connectivity.OutboundConnectionCreator;
import com.swirlds.platform.network.topology.NetworkTopology;
import com.swirlds.platform.network.topology.StaticConnectionManagers;
import com.swirlds.platform.network.topology.StaticTopology;
import com.swirlds.platform.system.address.AddressBook;
Expand Down Expand Up @@ -57,7 +60,11 @@ void testShouldConnectToMe(final int numNodes, final int numNeighbors) throws Ex
final AddressBook addressBook =
new RandomAddressBookGenerator(r).setSize(numNodes).build();
final NodeId selfId = addressBook.getNodeId(r.nextInt(numNodes));
final StaticTopology topology = new StaticTopology(r, addressBook, selfId, numNeighbors);

final List<PeerInfo> peers = Utilities.createPeerInfoList(addressBook, selfId);
final NetworkTopology topology =
new StaticTopology(r, peers, addressBook.getIndexOfNodeId(selfId), numNeighbors);

final StaticConnectionManagers managers = new StaticConnectionManagers(topology, connectionCreator);
final List<NodeId> neighbors = topology.getNeighbors();
final NodeId neighbor = neighbors.get(r.nextInt(neighbors.size()));
Expand Down Expand Up @@ -90,7 +97,10 @@ void testShouldConnectTo(final int numNodes, final int numNeighbors) throws Exce
final AddressBook addressBook =
new RandomAddressBookGenerator(r).setSize(numNodes).build();
final NodeId selfId = addressBook.getNodeId(r.nextInt(numNodes));
final StaticTopology topology = new StaticTopology(r, addressBook, selfId, numNeighbors);
final List<PeerInfo> peers = Utilities.createPeerInfoList(addressBook, selfId);
final NetworkTopology topology =
new StaticTopology(r, peers, addressBook.getIndexOfNodeId(selfId), numNeighbors);

final StaticConnectionManagers managers = new StaticConnectionManagers(topology, connectionCreator);
final List<NodeId> neighbors = topology.getNeighbors();
final NodeId neighbor = neighbors.get(r.nextInt(neighbors.size()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.swirlds.common.platform.NodeId;
import com.swirlds.platform.Utilities;
import com.swirlds.platform.network.PeerInfo;
import com.swirlds.platform.network.RandomGraph;
import com.swirlds.platform.network.topology.NetworkTopology;
import com.swirlds.platform.network.topology.StaticTopology;
Expand Down Expand Up @@ -124,7 +126,11 @@ void testFullyConnectedTopology(final int numNodes, final int numNeighbors, fina
final NodeId outOfBoundsId = addressBook.getNextNodeId();
final NodeId thisNodeId = addressBook.getNodeId(thisNode);
final Random random = getRandomPrintSeed();
final NetworkTopology topology = new StaticTopology(random, addressBook, thisNodeId, numNeighbors);

final List<PeerInfo> peers = Utilities.createPeerInfoList(addressBook, thisNodeId);

final NetworkTopology topology =
new StaticTopology(random, peers, addressBook.getIndexOfNodeId(thisNodeId), numNeighbors);
final List<NodeId> neighbors = topology.getNeighbors();
final List<NodeId> expected = IntStream.range(0, numNodes)
.mapToObj(addressBook::getNodeId)
Expand Down
Binary file not shown.
Binary file not shown.