Skip to content
Snippets Groups Projects
Commit 43fa0c25 authored by Cédric Moreau's avatar Cédric Moreau
Browse files

[fix] #1325 Do not always select the same nodes for P2P downloading

parent 65ebf762
Branches
Tags
No related merge requests found
...@@ -68,10 +68,7 @@ export class P2PSyncDownloader extends ASyncDownloader implements ISyncDownloade ...@@ -68,10 +68,7 @@ export class P2PSyncDownloader extends ASyncDownloader implements ISyncDownloade
nodesAvailable = nodesAvailable.concat(readyNodes) nodesAvailable = nodesAvailable.concat(readyNodes)
i++ i++
} }
return nodesAvailable.slice(0, needed).map(n => { return nodesAvailable
n.reserve()
return n
})
} }
/** /**
...@@ -81,12 +78,17 @@ export class P2PSyncDownloader extends ASyncDownloader implements ISyncDownloade ...@@ -81,12 +78,17 @@ export class P2PSyncDownloader extends ASyncDownloader implements ISyncDownloade
*/ */
private async getP2Pcandidates(chunkIndex: number): Promise<P2pCandidate[]> { private async getP2Pcandidates(chunkIndex: number): Promise<P2pCandidate[]> {
return this.fifoPromise.pushFIFOPromise('getP2Pcandidates_' + getNanosecondsTime(), async () => { return this.fifoPromise.pushFIFOPromise('getP2Pcandidates_' + getNanosecondsTime(), async () => {
const needed = 1
// We wait a bit to have some available nodes // We wait a bit to have some available nodes
const readyNodes = await this.waitForAvailableNodesAndReserve() const readyNodes = await this.waitForAvailableNodesAndReserve()
// We remove the nodes impossible to reach (timeout) // We remove the nodes impossible to reach (timeout)
let byAvgAnswerTime = Underscore.sortBy(readyNodes, p => p.avgResponseTime()) let byAvgAnswerTime = Underscore.sortBy(readyNodes, p => p.avgResponseTime())
const parallelMax = Math.min(this.PARALLEL_PER_CHUNK, byAvgAnswerTime.length) const parallelMax = Math.min(this.PARALLEL_PER_CHUNK, byAvgAnswerTime.length)
byAvgAnswerTime = byAvgAnswerTime.slice(0, parallelMax) byAvgAnswerTime = byAvgAnswerTime.slice(0, parallelMax)
this.watcher.reserveNodes(byAvgAnswerTime)
byAvgAnswerTime.slice(0, needed).forEach(n => {
n.reserve()
})
if (byAvgAnswerTime.length === 0) { if (byAvgAnswerTime.length === 0) {
this.logger.warn('No node found to download chunk #%s.', chunkIndex) this.logger.warn('No node found to download chunk #%s.', chunkIndex)
this.watcher.unableToDownloadChunk(chunkIndex) this.watcher.unableToDownloadChunk(chunkIndex)
......
...@@ -43,6 +43,7 @@ export class P2pCandidate { ...@@ -43,6 +43,7 @@ export class P2pCandidate {
return Promise.race([ return Promise.race([
// Wait for availablity // Wait for availablity
(async () => !this.isExcluded (async () => !this.isExcluded
&& !this.reserved
&& (this.apiPromise.isRejected() ? await newResolveTimeoutPromise(maxWait, false) : !!(await this.apiPromise)) && (this.apiPromise.isRejected() ? await newResolveTimeoutPromise(maxWait, false) : !!(await this.apiPromise))
&& (this.dlPromise.isRejected() ? await newResolveTimeoutPromise(maxWait, false) : !!(await this.dlPromise)))(), && (this.dlPromise.isRejected() ? await newResolveTimeoutPromise(maxWait, false) : !!(await this.dlPromise)))(),
// Maximum wait trigger // Maximum wait trigger
...@@ -55,6 +56,9 @@ export class P2pCandidate { ...@@ -55,6 +56,9 @@ export class P2pCandidate {
} }
avgResponseTime() { avgResponseTime() {
if (!this.responseTimes.length) {
return 0
}
return this.responseTimes.reduce((sum, rt) => sum + rt, 0) / this.responseTimes.length return this.responseTimes.reduce((sum, rt) => sum + rt, 0) / this.responseTimes.length
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment