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: algorithm-queue scale #1318

Merged
merged 3 commits into from
Jul 6, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion core/algorithm-operator/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ const db = require('./lib/helpers/db');
const etcd = require('./lib/helpers/etcd');
const kubernetes = require('./lib/helpers/kubernetes');
const operator = require('./lib/operator');
const jobsMessageQueue = require('./lib/helpers/jobs-message-queue');
const { setFromConfig } = require('./lib/helpers/settings');

const modules = [
db,
etcd,
kubernetes,
operator
operator,
jobsMessageQueue
];

class Bootstrap {
Expand Down
12 changes: 12 additions & 0 deletions core/algorithm-operator/config/main/config.base.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const packageJson = require(process.cwd() + '/package.json');
const formatter = require('../../lib/helpers/formatters');
const config = module.exports = {};

const useSentinel = !!process.env.REDIS_SENTINEL_SERVICE_HOST;
config.serviceName = packageJson.name;
config.version = packageJson.version;
config.intervalMs = process.env.INTERVAL_MS || 10000;
Expand All @@ -15,6 +16,17 @@ config.algorithmQueueBalancer = {
limit: formatter.parseInt(process.env.ALGORITHM_QUEUE_CONCURRENCY_LIMIT, 5)
};

config.JobsMessageQueue = {
enableCheckStalledJobs: false,
prefix: 'algorithm-queue',
};

config.redis = {
host: useSentinel ? process.env.REDIS_SENTINEL_SERVICE_HOST : process.env.REDIS_SERVICE_HOST || 'localhost',
port: useSentinel ? process.env.REDIS_SENTINEL_SERVICE_PORT : process.env.REDIS_SERVICE_PORT || 6379,
sentinel: useSentinel,
};

config.driversSetting = {
name: 'pipeline-driver',
concurrency: formatter.parseInt(process.env.PIPELINE_DRIVERS_CONCURRENCY_LIMIT, 5),
Expand Down
37 changes: 37 additions & 0 deletions core/algorithm-operator/lib/helpers/jobs-message-queue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const { Producer } = require('@hkube/producer-consumer');

class JobsMessageQueue {
async init(options) {
this._producer = new Producer({
setting: {
redis: options.redis,
...options.JobsMessageQueue
}
});
}

async getWaitingCount(algorithms) {
const map = {};
await Promise.all(algorithms.map(async (a) => {
const count = await this._getWaitingCount(a.name);
if (count >= 0) {
map[a.name] = count;
}
}));
return map;
}

async _getWaitingCount(algorithm) {
let count = 0;
try {
const queue = this._producer._createQueue(algorithm);
count = await queue.getWaitingCount();
}
catch {
count = null;
}
return count;
}
}

module.exports = new JobsMessageQueue();
8 changes: 5 additions & 3 deletions core/algorithm-operator/lib/reconcile/algorithm-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { findVersion } = require('../helpers/images');
const component = require('../consts/componentNames').ALGORITHM_QUEUE_RECONCILER;
const QueueActions = require('../consts/queue-actions');
const { normalizeQueuesDeployments, normalizeQueuesDiscovery, normalizeAlgorithms } = require('./normalize');
const jobsMessageQueue = require('../helpers/jobs-message-queue');
const CONTAINERS = require('../consts/containers');

const _createDeployment = async ({ queueId, options }) => {
Expand Down Expand Up @@ -117,15 +118,16 @@ const reconcile = async ({ deployments, algorithms, discovery, versions, registr
const normAlgorithms = normalizeAlgorithms(algorithms);
const normDeployments = normalizeQueuesDeployments(deployments);
const availableQueues = _findAvailableQueues({ queueToAlgorithms, limit });
const addAlgorithms = normAlgorithms.filter(a => !algorithmsToQueue[a.name]);
const removeAlgorithms = _findObsoleteAlgorithms({ algorithmsToQueue, normAlgorithms });
const waitingCount = await jobsMessageQueue.getWaitingCount(algorithms);
const requiredAlgorithms = normAlgorithms.filter(a => !algorithmsToQueue[a.name] && waitingCount[a.name] > 0);

if (!devMode) {
await _addDeployments({ limit, algorithms: normAlgorithms.length, deployments: normDeployments.length, versions, registry, clusterOptions, resources, options });
await _addDeployments({ limit, algorithms: requiredAlgorithms.length, deployments: normDeployments.length, versions, registry, clusterOptions, resources, options });
await _updateDeployments({ normDeployments, options: { versions, registry, clusterOptions, resources, options } });
await _deleteDeployments({ queues: queueToAlgorithms, normDeployments });
}
await _matchAlgorithmsToQueue({ algorithms: addAlgorithms, queues: availableQueues, limit });
await _matchAlgorithmsToQueue({ algorithms: requiredAlgorithms, queues: availableQueues, limit });
await _removeAlgorithmsFromQueue({ algorithms: removeAlgorithms });
await _removeDuplicatesAlgorithms({ algorithms: duplicateAlgorithms });
};
Expand Down
Loading