Skip to content
Merged
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
21 changes: 20 additions & 1 deletion create-a-container/routers/containers.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,26 @@ router.post('/', async (req, res) => {
nodeWhere.nvidiaAvailable = true;
}

const node = await Node.findOne({ where: nodeWhere });
// Select the node with the fewest existing containers to balance load.
// LEFT JOINs containers, groups by node, and orders by count ascending
// so the least-loaded node is chosen first.
const node = await Node.findOne({
where: nodeWhere,
include: [{
model: Container,
as: 'containers',
attributes: [],
required: false
}],
attributes: {
include: [
[Sequelize.fn('COUNT', Sequelize.col('containers.id')), 'containerCount']
]
},
group: ['Node.id'],
order: [[Sequelize.fn('COUNT', Sequelize.col('containers.id')), 'ASC']],
subQuery: false
});
if (!node && wantsNvidia) {
throw new Error('NVIDIA requested but no NVIDIA-capable nodes are available in this site');
}
Expand Down
Loading