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

Add prometheus bandwidth metrics to bitswap and kademlia #69

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.peergos.protocol.bitswap;

import io.libp2p.core.Stream;
import io.prometheus.client.*;
import kotlin.*;
import org.peergos.protocol.bitswap.pb.*;

Expand All @@ -9,14 +10,17 @@
public class BitswapConnection implements BitswapController {

private final Stream conn;
private final Counter sentBytes;

public BitswapConnection(Stream conn) {
public BitswapConnection(Stream conn, Counter sentBytes) {
this.conn = conn;
this.sentBytes = sentBytes;
}

@Override
public void send(MessageOuterClass.Message msg) {
conn.writeAndFlush(msg);
sentBytes.inc(msg.getSerializedSize());
}

@Override
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/org/peergos/protocol/bitswap/BitswapEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.libp2p.core.*;
import io.libp2p.core.Stream;
import io.libp2p.core.multiformats.*;
import io.prometheus.client.*;
import org.peergos.*;
import org.peergos.blockstore.*;
import org.peergos.protocol.bitswap.pb.*;
Expand Down Expand Up @@ -122,7 +123,7 @@ private static byte[] prefixBytes(Cid c) {
}
}

public void receiveMessage(MessageOuterClass.Message msg, Stream source) {
public void receiveMessage(MessageOuterClass.Message msg, Stream source, Counter sentBytes) {

List<MessageOuterClass.Message.BlockPresence> presences = new ArrayList<>();
List<MessageOuterClass.Message.Block> blocks = new ArrayList<>();
Expand Down Expand Up @@ -263,7 +264,10 @@ public void receiveMessage(MessageOuterClass.Message msg, Stream source) {
if (presences.isEmpty() && blocks.isEmpty() && wants.isEmpty())
return;

buildAndSendMessages(wants, presences, blocks, source::writeAndFlush);
buildAndSendMessages(wants, presences, blocks, reply -> {
sentBytes.inc(reply.getSerializedSize());
source.writeAndFlush(reply);
});
}

public void buildAndSendMessages(List<MessageOuterClass.Message.Wantlist.Entry> wants,
Expand Down
33 changes: 27 additions & 6 deletions src/main/java/org/peergos/protocol/bitswap/BitswapProtocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,30 @@

import io.libp2p.core.*;
import io.libp2p.protocol.*;
import io.prometheus.client.*;
import org.jetbrains.annotations.*;
import org.peergos.protocol.bitswap.pb.*;

import java.util.concurrent.*;

public class BitswapProtocol extends ProtobufProtocolHandler<BitswapController> {

private static final Counter initiatorReceivedBytes = Counter.build()
.name("bitswap_initiator_received_bytes")
.help("Total received bytes in bitswap initiator")
.register();
private static final Counter initiatorSentBytes = Counter.build()
.name("bitswap_initiator_sent_bytes")
.help("Total sent bytes in bitswap initiator")
.register();
private static final Counter responderReceivedBytes = Counter.build()
.name("bitswap_responder_received_bytes")
.help("Total received bytes in bitswap responder")
.register();
private static final Counter responderSentBytes = Counter.build()
.name("bitswap_responder_sent_bytes")
.help("Total sent bytes in bitswap responder")
.register();
private final BitswapEngine engine;

public BitswapProtocol(BitswapEngine engine) {
Expand All @@ -19,31 +36,35 @@ public BitswapProtocol(BitswapEngine engine) {
@NotNull
@Override
protected CompletableFuture<BitswapController> onStartInitiator(@NotNull Stream stream) {
BitswapConnection conn = new BitswapConnection(stream);
BitswapConnection conn = new BitswapConnection(stream, initiatorSentBytes);
engine.addConnection(stream.remotePeerId(), stream.getConnection().remoteAddress());
stream.pushHandler(new MessageHandler(engine));
stream.pushHandler(new MessageHandler(engine, initiatorSentBytes, initiatorReceivedBytes));
return CompletableFuture.completedFuture(conn);
}

@NotNull
@Override
protected CompletableFuture<BitswapController> onStartResponder(@NotNull Stream stream) {
BitswapConnection conn = new BitswapConnection(stream);
BitswapConnection conn = new BitswapConnection(stream, responderSentBytes);
engine.addConnection(stream.remotePeerId(), stream.getConnection().remoteAddress());
stream.pushHandler(new MessageHandler(engine));
stream.pushHandler(new MessageHandler(engine, responderSentBytes, responderReceivedBytes));
return CompletableFuture.completedFuture(conn);
}

class MessageHandler implements ProtocolMessageHandler<MessageOuterClass.Message> {
private BitswapEngine engine;
private final Counter sentBytes, receivedBytes;

public MessageHandler(BitswapEngine engine) {
public MessageHandler(BitswapEngine engine, Counter sentBytes, Counter receivedBytes) {
this.engine = engine;
this.sentBytes = sentBytes;
this.receivedBytes = receivedBytes;
}

@Override
public void onMessage(@NotNull Stream stream, MessageOuterClass.Message msg) {
engine.receiveMessage(msg, stream);
receivedBytes.inc(msg.getSerializedSize());
engine.receiveMessage(msg, stream, sentBytes);
}
}
}
16 changes: 12 additions & 4 deletions src/main/java/org/peergos/protocol/dht/KademliaEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.libp2p.core.*;
import io.libp2p.core.Stream;
import io.libp2p.core.multiformats.*;
import io.prometheus.client.*;
import org.peergos.*;
import org.peergos.blockstore.*;
import org.peergos.protocol.dht.pb.*;
Expand Down Expand Up @@ -67,13 +68,14 @@ public List<PeerAddresses> getKClosestPeers(byte[] key) {
.collect(Collectors.toList());
}

public void receiveRequest(Dht.Message msg, PeerId source, Stream stream) {
public void receiveRequest(Dht.Message msg, PeerId source, Stream stream, Counter sentBytes) {
switch (msg.getType()) {
case PUT_VALUE: {
Optional<IpnsMapping> mapping = IPNS.validateIpnsEntry(msg);
if (mapping.isPresent()) {
ipnsStore.put(mapping.get().publisher, mapping.get().value);
stream.writeAndFlush(msg);
sentBytes.inc(msg.getSerializedSize());
}
break;
}
Expand All @@ -90,7 +92,9 @@ public void receiveRequest(Dht.Message msg, PeerId source, Stream stream) {
.stream()
.map(PeerAddresses::toProtobuf)
.collect(Collectors.toList()));
stream.writeAndFlush(builder.build());
Dht.Message reply = builder.build();
stream.writeAndFlush(reply);
sentBytes.inc(reply.getSerializedSize());
break;
}
case ADD_PROVIDER: {
Expand All @@ -117,7 +121,9 @@ public void receiveRequest(Dht.Message msg, PeerId source, Stream stream) {
.stream()
.map(PeerAddresses::toProtobuf)
.collect(Collectors.toList()));
stream.writeAndFlush(builder.build());
Dht.Message reply = builder.build();
stream.writeAndFlush(reply);
sentBytes.inc(reply.getSerializedSize());
break;
}
case FIND_NODE: {
Expand All @@ -126,7 +132,9 @@ public void receiveRequest(Dht.Message msg, PeerId source, Stream stream) {
.stream()
.map(PeerAddresses::toProtobuf)
.collect(Collectors.toList()));
stream.writeAndFlush(builder.build());
Dht.Message reply = builder.build();
stream.writeAndFlush(reply);
sentBytes.inc(reply.getSerializedSize());
break;
}
case PING: {break;} // Not used any more
Expand Down
38 changes: 33 additions & 5 deletions src/main/java/org/peergos/protocol/dht/KademliaProtocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,30 @@

import io.libp2p.core.*;
import io.libp2p.protocol.*;
import io.prometheus.client.*;
import org.jetbrains.annotations.*;
import org.peergos.protocol.dht.pb.Dht;

import java.util.concurrent.*;

public class KademliaProtocol extends ProtobufProtocolHandler<KademliaController> {

private static final Counter initiatorReceivedBytes = Counter.build()
.name("kademlia_initiator_received_bytes")
.help("Total received bytes in kademlia protocol initiator")
.register();
private static final Counter initiatorSentBytes = Counter.build()
.name("kademlia_initiator_sent_bytes")
.help("Total sent bytes in kademlia protocol initiator")
.register();
private static final Counter responderReceivedBytes = Counter.build()
.name("kademlia_responder_received_bytes")
.help("Total received bytes in kademlia protocol responder")
.register();
private static final Counter responderSentBytes = Counter.build()
.name("kademlia_responder_sent_bytes")
.help("Total sent bytes in kademlia protocol responder")
.register();
public static final int MAX_MESSAGE_SIZE = 1024*1024;

private final KademliaEngine engine;
Expand All @@ -21,7 +39,7 @@ public KademliaProtocol(KademliaEngine engine) {
@Override
protected CompletableFuture<KademliaController> onStartInitiator(@NotNull Stream stream) {
engine.addOutgoingConnection(stream.remotePeerId(), stream.getConnection().remoteAddress());
ReplyHandler handler = new ReplyHandler(stream);
ReplyHandler handler = new ReplyHandler(stream, initiatorSentBytes, initiatorReceivedBytes);
stream.pushHandler(handler);
return CompletableFuture.completedFuture(handler);
}
Expand All @@ -30,33 +48,39 @@ protected CompletableFuture<KademliaController> onStartInitiator(@NotNull Stream
@Override
protected CompletableFuture<KademliaController> onStartResponder(@NotNull Stream stream) {
engine.addIncomingConnection(stream.remotePeerId(), stream.getConnection().remoteAddress());
IncomingRequestHandler handler = new IncomingRequestHandler(engine);
IncomingRequestHandler handler = new IncomingRequestHandler(engine, responderSentBytes, responderReceivedBytes);
stream.pushHandler(handler);
return CompletableFuture.completedFuture(handler);
}

class ReplyHandler implements ProtocolMessageHandler<Dht.Message>, KademliaController {
private final CompletableFuture<Dht.Message> resp = new CompletableFuture<>();
private final Stream stream;
private final Counter sentBytes, receivedBytes;

public ReplyHandler(Stream stream) {
public ReplyHandler(Stream stream, Counter sentBytes, Counter receivedBytes) {
this.stream = stream;
this.sentBytes = sentBytes;
this.receivedBytes = receivedBytes;
}

@Override
public CompletableFuture<Dht.Message> rpc(Dht.Message msg) {
stream.writeAndFlush(msg);
sentBytes.inc(msg.getSerializedSize());
return resp;
}

@Override
public CompletableFuture<Boolean> send(Dht.Message msg) {
stream.writeAndFlush(msg);
sentBytes.inc(msg.getSerializedSize());
return CompletableFuture.completedFuture(true);
}

@Override
public void onMessage(@NotNull Stream stream, Dht.Message msg) {
receivedBytes.inc(msg.getSerializedSize());
resp.complete(msg);
stream.closeWrite();
}
Expand All @@ -74,14 +98,18 @@ public void onException(@Nullable Throwable cause) {

class IncomingRequestHandler implements ProtocolMessageHandler<Dht.Message>, KademliaController {
private final KademliaEngine engine;
private final Counter sentBytes, receivedBytes;

public IncomingRequestHandler(KademliaEngine engine) {
public IncomingRequestHandler(KademliaEngine engine, Counter sentBytes, Counter receivedBytes) {
this.engine = engine;
this.sentBytes = sentBytes;
this.receivedBytes = receivedBytes;
}

@Override
public void onMessage(@NotNull Stream stream, Dht.Message msg) {
engine.receiveRequest(msg, stream.remotePeerId(), stream);
receivedBytes.inc(msg.getSerializedSize());
engine.receiveRequest(msg, stream.remotePeerId(), stream, sentBytes);
}

@Override
Expand Down