Skip to content

Commit

Permalink
Merge pull request #165 from nrkno/fix/lingering-availableWorkers
Browse files Browse the repository at this point in the history
Fix: closed workers are note cleaned up in availableWorkers & queriedWorkers  (SOFIE-3073)
  • Loading branch information
nytamin authored Apr 4, 2024
2 parents f0f08d0 + e9640eb commit 5e4e8e8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,23 @@ export class TrackedExpectationAPI {
if (trackedExp.session.assignedWorker)
throw new Error('Internal error: noWorkerAssigned can only be called when assignedWorker is falsy')

if (trackedExp.availableWorkers.size === 0) {
// Special case: No workers are available at all
// Return to NEW state, so that new workers can be assigned:

this.updateTrackedExpectationStatus(trackedExp, {
state: ExpectedPackageStatusAPI.WorkStatusState.NEW,
reason: {
user: 'No workers available',
tech: 'availableWorkers=0',
},
// Don't update the package status, since this means that we don't know anything about the package:
dontUpdatePackage: true,
})

return
}

let noAssignedWorkerReason: ExpectedPackageStatusAPI.Reason
if (!trackedExp.session.noAssignedWorkerReason) {
this.logger.error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ export class TrackedWorkerAgents {
*/
public async determineBestWorkerForExpectation(trackedExp: TrackedExpectation): Promise<{
bestWorker: WorkerAgentAssignment | undefined
countQueried: number
countInfinite: number
noCostReason: Reason
}> {
/** How many requests to send out simultaneously */
Expand All @@ -104,10 +102,7 @@ export class TrackedWorkerAgents {

const workerIds = Array.from(trackedExp.availableWorkers.keys())

let noCostReason: Reason = {
user: `${workerIds.length} workers are currently busy`,
tech: `${workerIds.length} busy, ${trackedExp.queriedWorkers.size} queried`,
}
let noCostReason: Reason | undefined = undefined

const workerCosts: WorkerAgentAssignment[] = []

Expand Down Expand Up @@ -145,6 +140,8 @@ export class TrackedWorkerAgents {
tech: `${stringifyError(error, true)}`,
}
}
} else {
this.logger.error(`Worker "${workerId}" not found in determineBestWorkerForExpectation`)
}
})

Expand All @@ -162,10 +159,20 @@ export class TrackedWorkerAgents {
return 0
})

if (!noCostReason) {
noCostReason = {
user: `${countInfinite} workers are currently busy`,
tech:
`availableWorkers: ${trackedExp.availableWorkers.size}, ` +
`queriedWorkers: ${trackedExp.queriedWorkers.size}, ` +
`countQueried: ${countQueried}, ` +
`countInfinite: ${countInfinite} ` +
`(Worker costs: ${workerCosts.map((c) => `${c.id}: ${c.cost}`).join(', ')}`,
}
}

return {
bestWorker: workerCosts[0],
countQueried,
countInfinite,
noCostReason,
}
}
Expand All @@ -180,23 +187,36 @@ export class TrackedWorkerAgents {
return
}

// Remove any workers that no longer exist:
// (Like if a worker has shut down)
{
for (const workerId of trackedExp.availableWorkers.keys()) {
if (!this.get(workerId)) {
trackedExp.availableWorkers.delete(workerId)
}
}
for (const workerId of trackedExp.queriedWorkers.keys()) {
if (!this.get(workerId)) {
trackedExp.queriedWorkers.delete(workerId)
}
}
}

if (!trackedExp.availableWorkers.size) {
session.noAssignedWorkerReason = { user: `No workers available`, tech: `No workers available` }
}

// Send a number of requests simultaneously:

const { bestWorker, countQueried, countInfinite, noCostReason } = await this.determineBestWorkerForExpectation(
trackedExp
)
const { bestWorker, noCostReason } = await this.determineBestWorkerForExpectation(trackedExp)

if (bestWorker) {
session.assignedWorker = bestWorker
trackedExp.noWorkerAssignedTime = null
} else {
session.noAssignedWorkerReason = {
user: `Waiting for a free worker, ${noCostReason.user}`,
tech: `Waiting for a free worker ${noCostReason.tech} (${trackedExp.availableWorkers.size} busy, ${countQueried} asked, ${countInfinite} infinite cost)`,
tech: `Waiting for a free worker, ${noCostReason.tech}`,
}
}
}
Expand Down

0 comments on commit 5e4e8e8

Please sign in to comment.