Skip to content

Commit

Permalink
Rough implementation of mixed filter-weigher for feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippMatthes committed Jan 6, 2025
1 parent 0b35204 commit 27311c3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
43 changes: 43 additions & 0 deletions nova/scheduler/external.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright (c) 2025 SAP SE
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""
"""
import requests
from oslo_log import log as logging

LOG = logging.getLogger(__name__)


def call_external_scheduler_api(host_objs, spec_obj, url):
"""Reorder and filter hosts using an external scheduler service."""
if not url or not host_objs:
return host_objs
# TODO: Check utils.request_is_rebuild(spec) here?
json_data = {
"hosts": [ o.obj_to_primitive() for o in host_objs ],
"request_spec": spec_obj.obj_to_primitive(),
}
try:
response = requests.post(url, json=json_data, timeout=10)
response.raise_for_status()
except requests.RequestException as e:
LOG.error("Failed to call external scheduler API: %s", e)
return host_objs
response_json = response.json()
# We expect the service to return an ordered list
# of host names. This list can also be empty.
host_names = response_json.get("hosts", [])
return [ o for o in host_objs if o.host in host_names ]
7 changes: 7 additions & 0 deletions nova/scheduler/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from nova import quota
from nova import rpc
from nova.scheduler.client import report
from nova.scheduler.external import call_external_scheduler_api
from nova.scheduler import host_manager
from nova.scheduler import request_filter
from nova.scheduler import utils
Expand Down Expand Up @@ -633,6 +634,12 @@ def _get_sorted_hosts(self, spec_obj, host_states, index):
# Strip off the WeighedHost wrapper class...
weighed_hosts = [h.obj for h in weighed_hosts]

# Call an external service that can modify `weighed_hosts` once more.
# This service may filter out some hosts, or it may re-order them.
if url := CONF.filter_scheduler.external_scheduler_api_url:
weighed_hosts = call_external_scheduler_api(
weighed_hosts, spec_obj, url)

# We randomize the first element in the returned list to alleviate
# congestion where the same host is consistently selected among
# numerous potential hosts for similar request specs.
Expand Down

0 comments on commit 27311c3

Please sign in to comment.