Skip to content

Commit

Permalink
WIP [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
CMCDragonkai committed Nov 10, 2023
1 parent ce2b603 commit f9fd313
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/nodes/NodeGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,12 @@ class NodeGraph {
public async setNode(
nodeId: NodeId,
nodeAddress: NodeAddress,
lastUpdated: number = utils.getUnixtime(),
tran?: DBTransaction,
): Promise<void> {
if (tran == null) {
return this.db.withTransactionF((tran) =>
this.setNode(nodeId, nodeAddress, tran),
this.setNode(nodeId, nodeAddress, lastUpdated, tran),
);
}
const [bucketIndex, bucketKey] = this.bucketIndex(nodeId);
Expand All @@ -298,7 +299,6 @@ class NodeGraph {
}
await this.setBucketMetaProp(bucketIndex, 'count', count + 1, tran);
}
const lastUpdated = utils.getUnixtime();
await tran.put(bucketPath, {
address: nodeAddress,
lastUpdated,
Expand Down
4 changes: 2 additions & 2 deletions src/nodes/NodeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ class NodeManager {
if (nodeData != null || count < this.nodeGraph.nodeBucketLimit) {
// Either already exists or has room in the bucket
// We want to add or update the node
await this.nodeGraph.setNode(nodeId, nodeAddress, tran);
await this.nodeGraph.setNode(nodeId, nodeAddress, undefined, tran);
// Updating the refreshBucket timer
await this.updateRefreshBucketDelay(
bucketIndex,
Expand All @@ -787,7 +787,7 @@ class NodeManager {
)} and adding ${nodesUtils.encodeNodeId(nodeId)}`,
);
await this.nodeGraph.unsetNode(oldNodeId, tran);
await this.nodeGraph.setNode(nodeId, nodeAddress, tran);
await this.nodeGraph.setNode(nodeId, nodeAddress, undefined, tran);
// Updating the refreshBucket timer
await this.updateRefreshBucketDelay(
bucketIndex,
Expand Down
25 changes: 18 additions & 7 deletions tests/nodes/NodeGraph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,24 +273,35 @@ describe(`${NodeGraph.name} test`, () => {
await nodeGraph.stop();
});
test('get bucket with multiple nodes', async () => {
const nodeBucketLimit = 25;
const nodeGraph = await NodeGraph.createNodeGraph({
db,
keyRing,
logger,
nodeBucketLimit
});
// Contiguous node IDs starting from 0
let nodeIds = Array.from({ length: 25 }, (_, i) =>
let nodeIds = Array.from({ length: nodeBucketLimit }, (_, i) =>
IdInternal.create<NodeId>(
utils.bigInt2Bytes(BigInt(i), keyRing.getNodeId().byteLength),
),
);
nodeIds = nodeIds.filter((nodeId) => !nodeId.equals(keyRing.getNodeId()));
for (const nodeId of nodeIds) {
await utils.sleep(100);
await nodeGraph.setNode(nodeId, {
host: '127.0.0.1',
port: 55555,
} as NodeAddress);
const lastUpdatedNow = utils.getUnixtime();
const lastUpdatedTimes = Array.from({ length: nodeBucketLimit }, (_, i) => {
return lastUpdatedNow - i * 100;
});
for (let i = 0; i < nodeIds.length; i++) {
const nodeId = nodeIds[i];
const lastUpdated = lastUpdatedTimes[i];
await nodeGraph.setNode(
nodeId,
{
host: '127.0.0.1',
port: 55555,
} as NodeAddress,
lastUpdated
);
}
// Use first and last buckets because node IDs may be split between buckets
const bucketIndexFirst = nodesUtils.bucketIndex(
Expand Down

0 comments on commit f9fd313

Please sign in to comment.