From 0fe48693c2e222fe773ad8bac43688798352c6e1 Mon Sep 17 00:00:00 2001 From: Danish Arora Date: Fri, 13 Sep 2024 15:20:31 +0530 Subject: [PATCH 1/4] chore: restructure reliabiltiy monitors --- packages/sdk/src/index.ts | 4 +- .../protocols/filter/subscription_manager.ts | 5 +- .../{light_push.ts => lightpush/index.ts} | 2 +- .../protocols/{store.ts => store/index.ts} | 2 +- packages/sdk/src/reliability_monitor/index.ts | 55 +++++++++++++++++++ .../receiver.ts} | 47 ---------------- packages/sdk/src/waku.ts | 6 +- 7 files changed, 65 insertions(+), 56 deletions(-) rename packages/sdk/src/protocols/{light_push.ts => lightpush/index.ts} (98%) rename packages/sdk/src/protocols/{store.ts => store/index.ts} (99%) create mode 100644 packages/sdk/src/reliability_monitor/index.ts rename packages/sdk/src/{protocols/filter/reliability_monitor.ts => reliability_monitor/receiver.ts} (82%) diff --git a/packages/sdk/src/index.ts b/packages/sdk/src/index.ts index 3b0aa54dda..2cbcfce937 100644 --- a/packages/sdk/src/index.ts +++ b/packages/sdk/src/index.ts @@ -14,9 +14,9 @@ export { defaultLibp2p, createLibp2pAndUpdateOptions } from "./create/index.js"; -export { wakuLightPush } from "./protocols/light_push.js"; +export { wakuLightPush } from "./protocols/lightpush/index.js"; export { wakuFilter } from "./protocols/filter/index.js"; -export { wakuStore } from "./protocols/store.js"; +export { wakuStore } from "./protocols/store/index.js"; export * as waku from "@waku/core"; export * as utils from "@waku/utils"; diff --git a/packages/sdk/src/protocols/filter/subscription_manager.ts b/packages/sdk/src/protocols/filter/subscription_manager.ts index 6d3d50f13b..70fccae33f 100644 --- a/packages/sdk/src/protocols/filter/subscription_manager.ts +++ b/packages/sdk/src/protocols/filter/subscription_manager.ts @@ -20,11 +20,12 @@ import { import { WakuMessage } from "@waku/proto"; import { groupByContentTopic, Logger } from "@waku/utils"; -import { DEFAULT_KEEP_ALIVE, DEFAULT_SUBSCRIBE_OPTIONS } from "./constants.js"; import { ReceiverReliabilityMonitor, ReliabilityMonitorManager -} from "./reliability_monitor.js"; +} from "../../reliability_monitor/receiver.js"; + +import { DEFAULT_KEEP_ALIVE, DEFAULT_SUBSCRIBE_OPTIONS } from "./constants.js"; const log = new Logger("sdk:filter:subscription_manager"); diff --git a/packages/sdk/src/protocols/light_push.ts b/packages/sdk/src/protocols/lightpush/index.ts similarity index 98% rename from packages/sdk/src/protocols/light_push.ts rename to packages/sdk/src/protocols/lightpush/index.ts index 4b303118bd..2cfdadde01 100644 --- a/packages/sdk/src/protocols/light_push.ts +++ b/packages/sdk/src/protocols/lightpush/index.ts @@ -13,7 +13,7 @@ import { } from "@waku/interfaces"; import { ensurePubsubTopicIsConfigured, Logger } from "@waku/utils"; -import { BaseProtocolSDK } from "./base_protocol.js"; +import { BaseProtocolSDK } from "../base_protocol.js"; const log = new Logger("sdk:light-push"); diff --git a/packages/sdk/src/protocols/store.ts b/packages/sdk/src/protocols/store/index.ts similarity index 99% rename from packages/sdk/src/protocols/store.ts rename to packages/sdk/src/protocols/store/index.ts index c851fbefe4..3aba99b87c 100644 --- a/packages/sdk/src/protocols/store.ts +++ b/packages/sdk/src/protocols/store/index.ts @@ -10,7 +10,7 @@ import { import { messageHash } from "@waku/message-hash"; import { ensurePubsubTopicIsConfigured, isDefined, Logger } from "@waku/utils"; -import { BaseProtocolSDK } from "./base_protocol.js"; +import { BaseProtocolSDK } from "../base_protocol.js"; const DEFAULT_NUM_PEERS = 1; diff --git a/packages/sdk/src/reliability_monitor/index.ts b/packages/sdk/src/reliability_monitor/index.ts new file mode 100644 index 0000000000..936a54700f --- /dev/null +++ b/packages/sdk/src/reliability_monitor/index.ts @@ -0,0 +1,55 @@ +import type { Peer, PeerId } from "@libp2p/interface"; +import { + ContentTopic, + CoreProtocolResult, + PubsubTopic +} from "@waku/interfaces"; + +import { ReceiverReliabilityMonitor } from "./receiver.js"; + +export class ReliabilityMonitorManager { + private static receiverMonitors: Map< + PubsubTopic, + ReceiverReliabilityMonitor + > = new Map(); + + public static createReceiverMonitor( + pubsubTopic: PubsubTopic, + getPeers: () => Peer[], + renewPeer: (peerId: PeerId) => Promise, + getContentTopics: () => ContentTopic[], + protocolSubscribe: ( + pubsubTopic: PubsubTopic, + peer: Peer, + contentTopics: ContentTopic[] + ) => Promise + ): ReceiverReliabilityMonitor { + if (ReliabilityMonitorManager.receiverMonitors.has(pubsubTopic)) { + return ReliabilityMonitorManager.receiverMonitors.get(pubsubTopic)!; + } + + const monitor = new ReceiverReliabilityMonitor( + pubsubTopic, + getPeers, + renewPeer, + getContentTopics, + protocolSubscribe + ); + ReliabilityMonitorManager.receiverMonitors.set(pubsubTopic, monitor); + return monitor; + } + + private constructor() {} + + public static destroy(pubsubTopic: PubsubTopic): void { + this.receiverMonitors.delete(pubsubTopic); + } + + public static destroyAll(): void { + for (const [pubsubTopic, monitor] of this.receiverMonitors) { + monitor.setMaxMissedMessagesThreshold(undefined); + monitor.setMaxPingFailures(undefined); + this.receiverMonitors.delete(pubsubTopic); + } + } +} diff --git a/packages/sdk/src/protocols/filter/reliability_monitor.ts b/packages/sdk/src/reliability_monitor/receiver.ts similarity index 82% rename from packages/sdk/src/protocols/filter/reliability_monitor.ts rename to packages/sdk/src/reliability_monitor/receiver.ts index 84d386f716..440e35829c 100644 --- a/packages/sdk/src/protocols/filter/reliability_monitor.ts +++ b/packages/sdk/src/reliability_monitor/receiver.ts @@ -21,53 +21,6 @@ const log = new Logger("sdk:receiver:reliability_monitor"); const DEFAULT_MAX_PINGS = 3; -export class ReliabilityMonitorManager { - private static receiverMonitors: Map< - PubsubTopic, - ReceiverReliabilityMonitor - > = new Map(); - - public static createReceiverMonitor( - pubsubTopic: PubsubTopic, - getPeers: () => Peer[], - renewPeer: (peerId: PeerId) => Promise, - getContentTopics: () => ContentTopic[], - protocolSubscribe: ( - pubsubTopic: PubsubTopic, - peer: Peer, - contentTopics: ContentTopic[] - ) => Promise - ): ReceiverReliabilityMonitor { - if (ReliabilityMonitorManager.receiverMonitors.has(pubsubTopic)) { - return ReliabilityMonitorManager.receiverMonitors.get(pubsubTopic)!; - } - - const monitor = new ReceiverReliabilityMonitor( - pubsubTopic, - getPeers, - renewPeer, - getContentTopics, - protocolSubscribe - ); - ReliabilityMonitorManager.receiverMonitors.set(pubsubTopic, monitor); - return monitor; - } - - private constructor() {} - - public static destroy(pubsubTopic: PubsubTopic): void { - this.receiverMonitors.delete(pubsubTopic); - } - - public static destroyAll(): void { - for (const [pubsubTopic, monitor] of this.receiverMonitors) { - monitor.setMaxMissedMessagesThreshold(undefined); - monitor.setMaxPingFailures(undefined); - this.receiverMonitors.delete(pubsubTopic); - } - } -} - export class ReceiverReliabilityMonitor { private receivedMessagesHashes: ReceivedMessageHashes; private missedMessagesByPeer: Map = new Map(); diff --git a/packages/sdk/src/waku.ts b/packages/sdk/src/waku.ts index 57c9c16d2a..2ed722ac1c 100644 --- a/packages/sdk/src/waku.ts +++ b/packages/sdk/src/waku.ts @@ -17,9 +17,9 @@ import { Protocols } from "@waku/interfaces"; import { Logger } from "@waku/utils"; import { wakuFilter } from "./protocols/filter/index.js"; -import { ReliabilityMonitorManager } from "./protocols/filter/reliability_monitor.js"; -import { wakuLightPush } from "./protocols/light_push.js"; -import { wakuStore } from "./protocols/store.js"; +import { wakuLightPush } from "./protocols/lightpush/index.js"; +import { wakuStore } from "./protocols/store/index.js"; +import { ReliabilityMonitorManager } from "./reliability_monitor/index.js"; export const DefaultPingKeepAliveValueSecs = 5 * 60; export const DefaultRelayKeepAliveValueSecs = 5 * 60; From c0e6e05de8fd5320b620438ec6a81704c75cf40f Mon Sep 17 00:00:00 2001 From: Danish Arora Date: Fri, 13 Sep 2024 15:27:55 +0530 Subject: [PATCH 2/4] feat: setup sender monitor --- packages/sdk/src/protocols/lightpush/index.ts | 29 +++++++--- packages/sdk/src/reliability_monitor/index.ts | 19 ++++++- .../sdk/src/reliability_monitor/sender.ts | 57 +++++++++++++++++++ 3 files changed, 96 insertions(+), 9 deletions(-) create mode 100644 packages/sdk/src/reliability_monitor/sender.ts diff --git a/packages/sdk/src/protocols/lightpush/index.ts b/packages/sdk/src/protocols/lightpush/index.ts index 2cfdadde01..f3be55d54a 100644 --- a/packages/sdk/src/protocols/lightpush/index.ts +++ b/packages/sdk/src/protocols/lightpush/index.ts @@ -13,6 +13,8 @@ import { } from "@waku/interfaces"; import { ensurePubsubTopicIsConfigured, Logger } from "@waku/utils"; +import { ReliabilityMonitorManager } from "../../reliability_monitor/index.js"; +import { SenderReliabilityMonitor } from "../../reliability_monitor/sender.js"; import { BaseProtocolSDK } from "../base_protocol.js"; const log = new Logger("sdk:light-push"); @@ -20,6 +22,8 @@ const log = new Logger("sdk:light-push"); class LightPushSDK extends BaseProtocolSDK implements ILightPushSDK { public readonly protocol: LightPushCore; + private readonly reliabilityMonitor: SenderReliabilityMonitor; + public constructor( connectionManager: ConnectionManager, libp2p: Libp2p, @@ -33,6 +37,10 @@ class LightPushSDK extends BaseProtocolSDK implements ILightPushSDK { } ); + this.reliabilityMonitor = ReliabilityMonitorManager.createSenderMonitor( + this.renewPeer.bind(this) + ); + this.protocol = this.core as LightPushCore; } @@ -89,16 +97,23 @@ class LightPushSDK extends BaseProtocolSDK implements ILightPushSDK { successes.push(success); } if (failure) { + failures.push(failure); if (failure.peerId) { - try { - await this.renewPeer(failure.peerId); - log.info("Renewed peer", failure.peerId.toString()); - } catch (error) { - log.error("Failed to renew peer", error); + const peer = this.connectedPeers.find((connectedPeer) => + connectedPeer.id.equals(failure.peerId) + ); + if (peer) { + log.info(` + Failed to send message to peer ${failure.peerId}. + Retrying the message with the same peer in the background. + If this fails, the peer will be renewed. + `); + void this.reliabilityMonitor.attemptRetriesOrRenew( + failure.peerId, + () => this.protocol.send(encoder, message, peer) + ); } } - - failures.push(failure); } } else { log.error("Failed unexpectedly while sending:", result.reason); diff --git a/packages/sdk/src/reliability_monitor/index.ts b/packages/sdk/src/reliability_monitor/index.ts index 936a54700f..1e420dd921 100644 --- a/packages/sdk/src/reliability_monitor/index.ts +++ b/packages/sdk/src/reliability_monitor/index.ts @@ -6,12 +6,14 @@ import { } from "@waku/interfaces"; import { ReceiverReliabilityMonitor } from "./receiver.js"; +import { SenderReliabilityMonitor } from "./sender.js"; export class ReliabilityMonitorManager { private static receiverMonitors: Map< PubsubTopic, ReceiverReliabilityMonitor > = new Map(); + private static senderMonitor: SenderReliabilityMonitor | undefined; public static createReceiverMonitor( pubsubTopic: PubsubTopic, @@ -39,17 +41,30 @@ export class ReliabilityMonitorManager { return monitor; } + public static createSenderMonitor( + renewPeer: (peerId: PeerId) => Promise + ): SenderReliabilityMonitor { + if (!ReliabilityMonitorManager.senderMonitor) { + ReliabilityMonitorManager.senderMonitor = new SenderReliabilityMonitor( + renewPeer + ); + } + return ReliabilityMonitorManager.senderMonitor; + } + private constructor() {} - public static destroy(pubsubTopic: PubsubTopic): void { + public static stop(pubsubTopic: PubsubTopic): void { this.receiverMonitors.delete(pubsubTopic); + this.senderMonitor = undefined; } - public static destroyAll(): void { + public static stopAll(): void { for (const [pubsubTopic, monitor] of this.receiverMonitors) { monitor.setMaxMissedMessagesThreshold(undefined); monitor.setMaxPingFailures(undefined); this.receiverMonitors.delete(pubsubTopic); + this.senderMonitor = undefined; } } } diff --git a/packages/sdk/src/reliability_monitor/sender.ts b/packages/sdk/src/reliability_monitor/sender.ts new file mode 100644 index 0000000000..0ffe9a1659 --- /dev/null +++ b/packages/sdk/src/reliability_monitor/sender.ts @@ -0,0 +1,57 @@ +import type { Peer, PeerId } from "@libp2p/interface"; +import { CoreProtocolResult, PeerIdStr } from "@waku/interfaces"; +import { Logger } from "@waku/utils"; + +const log = new Logger("sdk:sender:reliability_monitor"); + +const DEFAULT_MAX_ATTEMPTS_BEFORE_RENEWAL = 3; + +export class SenderReliabilityMonitor { + private attempts: Map = new Map(); + private readonly maxAttemptsBeforeRenewal = + DEFAULT_MAX_ATTEMPTS_BEFORE_RENEWAL; + + public constructor(private renewPeer: (peerId: PeerId) => Promise) {} + + public async attemptRetriesOrRenew( + peerId: PeerId, + protocolSend: () => Promise + ): Promise { + const peerIdStr = peerId.toString(); + const currentAttempts = this.attempts.get(peerIdStr) || 0; + this.attempts.set(peerIdStr, currentAttempts + 1); + + if (currentAttempts + 1 < this.maxAttemptsBeforeRenewal) { + try { + const result = await protocolSend(); + if (result.success) { + log.info(`Successfully sent message after retry to ${peerIdStr}`); + this.attempts.delete(peerIdStr); + } else { + log.error( + `Failed to send message after retry to ${peerIdStr}: ${result.failure}` + ); + await this.attemptRetriesOrRenew(peerId, protocolSend); + } + } catch (error) { + log.error( + `Failed to send message after retry to ${peerIdStr}: ${error}` + ); + await this.attemptRetriesOrRenew(peerId, protocolSend); + } + } else { + try { + const newPeer = await this.renewPeer(peerId); + log.info( + `Renewed peer ${peerId.toString()} to ${newPeer.id.toString()}` + ); + + this.attempts.delete(peerIdStr); + this.attempts.set(newPeer.id.toString(), 0); + await protocolSend(); + } catch (error) { + log.error(`Failed to renew peer ${peerId.toString()}: ${error}`); + } + } + } +} From 6b58f08bc1fbe8b6b362521ca64c6125a31e99ad Mon Sep 17 00:00:00 2001 From: Danish Arora Date: Fri, 13 Sep 2024 15:28:39 +0530 Subject: [PATCH 3/4] chore: update tests --- .../tests/tests/light-push/peer_management.spec.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/tests/tests/light-push/peer_management.spec.ts b/packages/tests/tests/light-push/peer_management.spec.ts index 1c7fa59d44..5156a3c89b 100644 --- a/packages/tests/tests/light-push/peer_management.spec.ts +++ b/packages/tests/tests/light-push/peer_management.spec.ts @@ -1,5 +1,6 @@ import { LightNode } from "@waku/interfaces"; import { createEncoder, utf8ToBytes } from "@waku/sdk"; +import { delay } from "@waku/utils"; import { expect } from "chai"; import { describe } from "mocha"; @@ -78,18 +79,21 @@ describe("Waku Light Push: Peer Management: E2E", function () { expect(response2.failures).to.have.length(1); expect(response2.failures?.[0].peerId).to.equal(peerToDisconnect); - // send another lightpush request -- renewal should have triggerred and new peer should be used instead of the disconnected one + // send another lightpush request + // reattempts to send should be triggerred + // then renewal should happen + // so one failure should exist const response3 = await waku.lightPush.send(encoder, { payload: utf8ToBytes("Hello_World") }); + // wait for reattempts to finish as they are async and not awaited + await delay(500); + expect(response3.successes.length).to.be.equal( - waku.lightPush.numPeersToUse + waku.lightPush.numPeersToUse - 1 ); expect(response3.successes).to.not.include(peerToDisconnect); - if (response3.failures) { - expect(response3.failures.length).to.equal(0); - } }); }); From 89c7bae801f54e3ee58dc36ebf9f33ad68727095 Mon Sep 17 00:00:00 2001 From: Danish Arora Date: Fri, 13 Sep 2024 16:12:55 +0530 Subject: [PATCH 4/4] chore: minor fixes --- packages/sdk/src/protocols/filter/subscription_manager.ts | 6 ++---- packages/sdk/src/waku.ts | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/sdk/src/protocols/filter/subscription_manager.ts b/packages/sdk/src/protocols/filter/subscription_manager.ts index 70fccae33f..51a9e16e8d 100644 --- a/packages/sdk/src/protocols/filter/subscription_manager.ts +++ b/packages/sdk/src/protocols/filter/subscription_manager.ts @@ -20,10 +20,8 @@ import { import { WakuMessage } from "@waku/proto"; import { groupByContentTopic, Logger } from "@waku/utils"; -import { - ReceiverReliabilityMonitor, - ReliabilityMonitorManager -} from "../../reliability_monitor/receiver.js"; +import { ReliabilityMonitorManager } from "../../reliability_monitor/index.js"; +import { ReceiverReliabilityMonitor } from "../../reliability_monitor/receiver.js"; import { DEFAULT_KEEP_ALIVE, DEFAULT_SUBSCRIBE_OPTIONS } from "./constants.js"; diff --git a/packages/sdk/src/waku.ts b/packages/sdk/src/waku.ts index 2ed722ac1c..a5314a99f8 100644 --- a/packages/sdk/src/waku.ts +++ b/packages/sdk/src/waku.ts @@ -196,7 +196,7 @@ export class WakuNode implements Waku { } public async stop(): Promise { - ReliabilityMonitorManager.destroyAll(); + ReliabilityMonitorManager.stopAll(); this.connectionManager.stop(); await this.libp2p.stop(); }