Add ComputeNode Tracker

A ComputeNodeTracker class, very similar to the resource_tracker.py
of nova[1] at nova/compute/resource_tracker.py with a few changes.

This does not implement any in memory cache for the compute_nodes
but reads the data everytime from the db. This can be improved or
kept as is. This also imposes the compute_node object contract on
the driver API for available resources so that random keys are not
used instead the object fields are honored.

[1]: https://github.com/openstack/nova/

Change-Id: I1e3aa35ad9927b0d29c8970a5fa3cb6530df281f
This commit is contained in:
Sudipta Biswas 2017-02-21 23:18:38 +05:30 committed by Hongbin Lu
parent 7255e44328
commit ab04ac91ca
3 changed files with 55 additions and 6 deletions

View File

@ -0,0 +1,49 @@
# 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.
from oslo_log import log as logging
from zun.common import exception
from zun.common.i18n import _LI
from zun.common.i18n import _LW
from zun import objects
LOG = logging.getLogger(__name__)
class ComputeNodeTracker(object):
def __init__(self, host, container_driver):
self.host = host
self.container_driver = container_driver
def update_available_resources(self, context):
# Check if the compute_node is already registered
node = self._get_compute_node(context, self.host)
if not node:
# If not, register it and pass the object to the driver
node = objects.NodeInfo(context)
node.hostname = self.host
node.create()
LOG.info(_LI('Node created for :%(host)s'), {'host': self.host})
self.container_driver.get_available_resources(node)
# NOTE(sbiswas7): Consider removing the return statement if not needed
return node
def _get_compute_node(self, context):
"""Returns compute node for the host"""
try:
return objects.NodeInfo.get_by_hostname(context, self.host)
except exception.NotFound:
LOG.warning(_LW("No compute node record for: %(host)s"),
{'host': self.host})

View File

@ -158,9 +158,7 @@ class ContainerDriver(object):
"""Update a container."""
raise NotImplementedError()
def get_available_resources(self):
data = {}
def get_available_resources(self, compute_node_obj):
numa_topo_obj = objects.NUMATopology()
os_capability_linux.LinuxHost().get_host_numa_topology(numa_topo_obj)
data['numa_topology'] = numa_topo_obj
return data
compute_node_obj.numa_topology = numa_topo_obj

View File

@ -17,6 +17,7 @@ from zun import conf
from zun.container.docker.driver import DockerDriver
from zun.container.docker.driver import NovaDockerDriver
from zun.container.docker import utils as docker_utils
from zun import objects
from zun.objects import fields
from zun.tests.unit.container import base
from zun.tests.unit.db import utils as db_utils
@ -540,5 +541,6 @@ class TestNovaDockerDriver(base.DriverTestCase):
def test_get_available_resources(self, mock_output):
mock_output.return_value = LSCPU_ON
conf.CONF.set_override('floating_cpu_set', "0")
output = self.driver.get_available_resources()
self.assertEqual(_numa_topo_spec, output['numa_topology'].to_list())
node_obj = objects.ComputeNode()
self.driver.get_available_resources(node_obj)
self.assertEqual(_numa_topo_spec, node_obj.numa_topology.to_list())