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

Add option to distribute jobs to clusters based on cluster size #360

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions deploy/infrabox/templates/api/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ spec:
-
name: INFRABOX_GERRIT_ENABLED
value: {{ .Values.gerrit.enabled | quote }}
{{ if .Values.job.weighted_cluster_assignment }}
-
name: INFRABOX_WEIGHTED_CLUSTER_ASSIGNMENT
value: {{ .Values.job.weighted_cluster_assignment | quote }}
{{ end }}
volumes:
{{ include "volumes_database" . | indent 16 }}
{{ include "volumes_rsa" . | indent 16 }}
Expand Down
2 changes: 2 additions & 0 deletions deploy/infrabox/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ job:

storage_driver: overlay

weighted_cluster_assignment: false

monitoring:
# Enable InfraBox Monitoring. Requires prometheus-operator to be available.
enabled: false
Expand Down
15 changes: 14 additions & 1 deletion src/api/handlers/job_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,8 @@ def find_leaf_jobs(jobs):
@api.route("/api/job/create_jobs", doc=False)
class CreateJobs(Resource):
def get_target_cluster(self, clusters, cluster_selector):
possible_clusters = []
total_weight = 0
for c in clusters:
matches = True
for s in cluster_selector:
Expand All @@ -672,13 +674,24 @@ def get_target_cluster(self, clusters, cluster_selector):
break

if matches:
if os.environ.get('INFRABOX_WEIGHTED_CLUSTER_ASSIGNMENT', 'true') != 'true':
return c['name']
possible_clusters.append(c)
total_weight += c['weight']

r = random.randrange(0, total_weight)
l = n = 0
for c in possible_clusters:
n = l + c['weight']
if r >= l and r < n:
return c['name']
l = n

return None

def assign_cluster(self, jobs):
clusters = g.db.execute_many_dict('''
SELECT name, labels
SELECT name, labels, weight
FROM cluster
WHERE active = true
AND enabled = true
Expand Down
1 change: 1 addition & 0 deletions src/db/migrations/00033.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE cluster ADD COLUMN weight INTEGER NOT NULL DEFAULT 1;
17 changes: 15 additions & 2 deletions src/scheduler/kubernetes/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1171,7 +1171,7 @@ def handle_orphaned_jobs(self):
def get_default_cluster(self):
cursor = self.conn.cursor()
cursor.execute("""
SELECT name, labels
SELECT name, labels, weight
FROM cluster
WHERE active = true AND
enabled = true
Expand All @@ -1181,13 +1181,26 @@ def get_default_cluster(self):

random.shuffle(result)

possible_clusters = []
total_weight = 0
for row in result:
cluster_name = row[0]
labels = row[1]

for l in labels:
if l == 'default':
return cluster_name
if os.environ.get('INFRABOX_WEIGHTED_CLUSTER_ASSIGNMENT', 'true') != 'true':
return cluster_name
possible_clusters.append(row)
total_weight += row['weight']

r = random.randrange(0, total_weight)
l = n = 0
for c in possible_clusters:
n = l + c['weight']
if r >= l and r < n:
return c['name']
l = n

return None

Expand Down