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

SAP: Introduce external scheduler filter #502

Open
wants to merge 1 commit into
base: stable/xena-m3
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
7 changes: 7 additions & 0 deletions nova/conf/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,13 @@
Related options:

* ``[DEFAULT] bigvm_mb``
"""),

# External scheduler filter.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

afaics, this comment doesn't contain any content. What's the intent?

cfg.StrOpt("external_scheduler_api_url",
default="",
help="""
The API URL of the external scheduler. If not provided, the filter is skipped.
""")
]

Expand Down
78 changes: 78 additions & 0 deletions nova/scheduler/filters/external_scheduler_filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Copyright 2024 SAP SE or an SAP affiliate company.
#
# 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

import nova.conf
from nova.scheduler import filters
from oslo_log import log as logging

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oslo_log not being part of this project (Nova) should go to another import block (the one where requests is)


LOG = logging.getLogger(__name__)
CONF = nova.conf.CONF


class ExternalSchedulerFilter(filters.BaseHostFilter):
"""Filter using an out of tree scheduler via a REST API."""

RUN_ON_REBUILD = False
fwiesel marked this conversation as resolved.
Show resolved Hide resolved

def __init__(self):
super().__init__()
self.api_url = CONF.filter_scheduler.external_scheduler_api_url

def filter_all(self, filter_obj_list, spec_obj):
"""Yield objects that pass the filter."""
if not self.api_url:
LOG.debug(
"external_scheduler_api_url not configured. skipping filter"
)
return filter_obj_list

hosts_data = []
for obj in filter_obj_list:
hosts_data.append({
"name": obj.host,
"status": obj.status,
"vcpus_total": obj.vcpus_total,
"vcpus_used": obj.vcpus_used,
"memory_mb": obj.memory_mb,
"memory_mb_used": obj.memory_mb_used
})

spec_data = {
"vcpus": spec_obj.vcpus,
"memory_mb": spec_obj.memory_mb,
}

json_data = {
"hosts": hosts_data,
"request_spec": spec_data
}

try:
response = requests.post(
self.api_url, json=json_data, timeout=10
)
response.raise_for_status()
response_json = response.json()
valid_hosts = response_json.get('valid_hosts', [])
for obj in filter_obj_list:
if obj.host in valid_hosts:
yield obj

except requests.RequestException as e:
# Log an error if the request fails

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this comment doesn't add anything helpful as the next line starts with LOG.error and thus easily conveys the same information.

LOG.error("Failed to query the external API: %s", e)
return filter_obj_list

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this does nothing. I tried with this code:

def a():
  try:
      for i in range(7):
        if i == 3:
          raise Exception
        yield i
  except Exception:
    return list(range(7))

breakpoint()
$ python3 /tmp/bla.py 
--Return--
> /tmp/bla.py(10)<module>()->None
-> breakpoint()
(Pdb) p a()
<generator object a at 0x7e701c04d4d0>
(Pdb) p list(a())
[0, 1, 2]

Since a function containing yield, not matter if it's called or not, is a Generator, it doesn't have a proper return-value - afaik.

Any return is broken, also the first one allowing to skip the filter:

def a():
  return ["early"]
  try:
      for i in range(7):
        if i == 3:
          raise Exception
        yield i
  except Exception:
    return list(range(7))

breakpoint()
$ python3 /tmp/bla.py 
--Return--
> /tmp/bla.py(11)<module>()->None
-> breakpoint()
(Pdb) p list(a())
[]
(Pdb) 

Am I holding it wrong? Shouldn't the test for "the filter returns everything if it's disabled" have found this?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you have all the hosts in a list already and are not reading from the request anymore, you might as well return a list instead of yielding, i.e. return [obj for obj in filter_obj_list if obj.host in valid_hosts]

Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Copyright 2024 SAP SE or an SAP affiliate company.
#
# 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.

# Tests for external scheduler filter.

from unittest.mock import MagicMock
from unittest.mock import patch
from unittest.mock import sentinel

import nova.conf
from nova import objects
from nova.scheduler.filters import external_scheduler_filter
from nova import test
from nova.tests.unit.scheduler import fakes


CONF = nova.conf.CONF


class ExternalSchedulerFilterTestCase(test.NoDBTestCase):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing at least 3 tests:
a) we're disabled
b) we're enabled, but the api is unreachable (mock_post.side_effect = requests.RequestException() or something)
c) we're enabled, the api returns an empty reply
d) we're enabled, the api returns some error code (500? 503?) (is this this same as b? we could make sure we catch a broad-enough exception from requests)


def setUp(self):
super(test.NoDBTestCase, self).setUp()
CONF.set_override('external_scheduler_api_url',
'http://127.0.0.1:1234', 'filter_scheduler')

@patch('requests.post')
def test_filter_all(self, mock_post):
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.json.return_value = {
'valid_hosts': ['host1', 'host3']
}
mock_post.return_value = mock_response

host_attributes = {
"status": "up",
"vcpus_total": 100,
"vcpus_used": 10,
"memory_mb": 1000,
"memory_mb_used": 10,
}
all_hosts = [
fakes.FakeHostState('host1', 'node1', host_attributes),
fakes.FakeHostState('host2', 'node2', host_attributes),
fakes.FakeHostState('host3', 'node3', host_attributes)
]
instance_request_spec = objects.RequestSpec(
context=sentinel.ctx,
flavor=objects.Flavor(
name="small",
vcpus=4,
memory_mb=1024
),
)

f = external_scheduler_filter.ExternalSchedulerFilter()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other test classes, mainly because they run multiple tests, instantiate their filter in __init__() already.

valid_hosts = f.filter_all(all_hosts, instance_request_spec)
self.assertEqual(
['host1', 'host3'],
[h.host for h in valid_hosts]
)