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: Query neighbors by stream #15

Merged
merged 3 commits into from
Mar 21, 2024
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
2 changes: 2 additions & 0 deletions src/api/NodeResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ export class NodeResolver {
async neighbors(
@Arg("node", { nullable: true }) nodeId?: DhtAddress,
@Arg("streamPart", { nullable: true }) streamPart?: string,
@Arg("stream", { nullable: true }) streamId?: string,
@Arg("pageSize", () => Int, { nullable: true }) pageSize?: number,
@Arg("cursor", { nullable: true }) cursor?: string
): Promise<Neighbors> {
return this.repository.getNeighbors(
(nodeId !== undefined) ? nodeId as DhtAddress : undefined,
(streamPart !== undefined) ? StreamPartIDUtils.parse(streamPart) : undefined,
(streamId !== undefined) ? toStreamID(streamId) : undefined,
pageSize,
cursor
)
Expand Down
5 changes: 5 additions & 0 deletions src/repository/NodeRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export class NodeRepository {
async getNeighbors(
nodeId?: DhtAddress,
streamPartId?: StreamPartID,
streamId?: StreamID,
pageSize?: number,
cursor?: string
): Promise<PaginatedListFragment<NeighborRow>> {
Expand All @@ -84,6 +85,10 @@ export class NodeRepository {
whereClauses.push('streamPartId = ?')
params.push(streamPartId)
}
if (streamId !== undefined) {
whereClauses.push('streamPartId LIKE ?')
params.push(`${streamId}#%`)
}
const sql = createSqlQuery(
'SELECT streamPartId, nodeId1, nodeId2 FROM neighbors',
whereClauses
Expand Down
76 changes: 45 additions & 31 deletions test/APIServer.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'reflect-metadata'

import { range } from 'lodash'
import { range, without } from 'lodash'
import Container from 'typedi'
import { APIServer } from '../src/api/APIServer'
import { CONFIG_TOKEN } from '../src/Config'
Expand All @@ -16,28 +16,27 @@ import { StreamPartID, StreamPartIDUtils } from '@streamr/protocol'
const storeTestTopology = async (
streamParts: {
id: StreamPartID
node1: DhtAddress
node2: DhtAddress
nodeIds: DhtAddress[]
}[]
) => {
const nodeRepository = Container.get(NodeRepository)
const nodes: any[] = []
for (const streamPart of streamParts) {
const streamPartNeighbors1 = new Multimap()
streamPartNeighbors1.add(streamPart.id, streamPart.node2)
const streamPartNeighbors2 = new Multimap()
streamPartNeighbors2.add(streamPart.id, streamPart.node1)
nodes.push({
id: streamPart.node1,
streamPartNeighbors: streamPartNeighbors1,
ipAddress: '123.1.2.3'
}, {
id: streamPart.node2,
streamPartNeighbors: streamPartNeighbors2,
ipAddress: '123.1.2.3'
const nodeIds: Set<DhtAddress> = new Set(streamParts.map((sp) => sp.nodeIds).flat())
const getNodes = () => {
return [...nodeIds].map((nodeId: DhtAddress) => {
const streamPartNeighbors = new Multimap()
for (const streamPart of streamParts) {
if (streamPart.nodeIds.includes(nodeId)) {
streamPartNeighbors.addAll(streamPart.id, without(streamPart.nodeIds, nodeId))
}
}
return {
id: nodeId,
streamPartNeighbors,
ipAddress: '123.1.2.3'
}
})
}
await nodeRepository.replaceNetworkTopology({ getNodes: () => nodes } as any)
await nodeRepository.replaceNetworkTopology({ getNodes } as any)
}

describe('APIServer', () => {
Expand Down Expand Up @@ -275,13 +274,12 @@ describe('APIServer', () => {
const node1 = createRandomDhtAddress()
const node2 = createRandomDhtAddress()
const node3 = createRandomDhtAddress()
const node4 = createRandomDhtAddress()

beforeEach(async () => {
await storeTestTopology([
{ id: StreamPartIDUtils.parse('stream1#0'), node1, node2 },
{ id: StreamPartIDUtils.parse('stream1#1'), node1: node3, node2: node4 },
{ id: StreamPartIDUtils.parse('stream2#0'), node1: createRandomDhtAddress(), node2: createRandomDhtAddress() }
{ id: StreamPartIDUtils.parse('stream1#0'), nodeIds: [node1, node2] },
{ id: StreamPartIDUtils.parse('stream1#1'), nodeIds: [node2, node3] },
{ id: StreamPartIDUtils.parse('stream2#0'), nodeIds: [createRandomDhtAddress(), createRandomDhtAddress()] }
])
})

Expand Down Expand Up @@ -322,7 +320,7 @@ describe('APIServer', () => {
}
}`, apiPort)
const actualNodeIds = response.items.map((node: any) => node.id)
expect(actualNodeIds).toIncludeSameMembers([node1, node2, node3, node4])
expect(actualNodeIds).toIncludeSameMembers([node1, node2, node3])
})
})

Expand All @@ -332,11 +330,13 @@ describe('APIServer', () => {
const node2 = createRandomDhtAddress()
const node3 = createRandomDhtAddress()
const node4 = createRandomDhtAddress()
const node5 = createRandomDhtAddress()

beforeEach(async () => {
await storeTestTopology([
{ id: StreamPartIDUtils.parse('stream#0'), node1, node2 },
{ id: StreamPartIDUtils.parse('stream#1'), node1: node3, node2: node4 }
{ id: StreamPartIDUtils.parse('stream1#0'), nodeIds: [node1, node2] },
{ id: StreamPartIDUtils.parse('stream1#1'), nodeIds: [node2, node3] },
{ id: StreamPartIDUtils.parse('stream2#0'), nodeIds: [node4, node5] }
])
})

Expand All @@ -351,8 +351,8 @@ describe('APIServer', () => {
}
}`, apiPort)
const neighbors = response['items']
const actualNodes = [neighbors[0].nodeId1, neighbors[0].nodeId2, neighbors[1].nodeId1, neighbors[1].nodeId2]
expect(actualNodes).toIncludeSameMembers([node1, node2, node3, node4])
const actualNodes = neighbors.map((n: any) => [n.nodeId1, n.nodeId2]).flat()
expect(actualNodes).toIncludeSameMembers([node1, node2, node2, node3, node4, node5])
})

it('filter by node', async () => {
Expand All @@ -366,23 +366,37 @@ describe('APIServer', () => {
}
}`, apiPort)
const neighbors = response1['items']
const actualNodes = [neighbors[0].nodeId1, neighbors[0].nodeId2]
const actualNodes = neighbors.map((n: any) => [n.nodeId1, n.nodeId2]).flat()
expect(actualNodes).toIncludeSameMembers([node1, node2])
})

it('filter by stream part', async () => {
const response = await queryAPI(`{
neighbors(streamPart: "stream#0") {
neighbors(streamPart: "stream1#0") {
items {
nodeId1
nodeId2
}
}
}`, apiPort)
const neighbors = response['items']
const actualNodes = [neighbors[0].nodeId1, neighbors[0].nodeId2]
const actualNodes = neighbors.map((n: any) => [n.nodeId1, n.nodeId2]).flat()
expect(actualNodes).toIncludeSameMembers([node1, node2])
})

it('filter by stream', async () => {
const response = await queryAPI(`{
neighbors(stream: "stream1") {
items {
nodeId1
nodeId2
}
}
}`, apiPort)
const neighbors = response['items']
const actualNodes = neighbors.map((n: any) => [n.nodeId1, n.nodeId2]).flat()
expect(actualNodes).toIncludeSameMembers([node1, node2, node2, node3])
})
})

it('summary', async () => {
Expand All @@ -403,7 +417,7 @@ describe('APIServer', () => {
publisherCount: null,
subscriberCount: null
})
await storeTestTopology([{ id: StreamPartIDUtils.parse('stream#0'), node1: createRandomDhtAddress(), node2: createRandomDhtAddress() }])
await storeTestTopology([{ id: StreamPartIDUtils.parse('stream#0'), nodeIds: [createRandomDhtAddress(), createRandomDhtAddress()] }])
const summary = await queryAPI(`{
summary {
streamCount
Expand Down
Loading