Extracted parsing logic and objects creation into separate class

This commit is contained in:
Evgeniy L 2016-03-17 18:03:43 +03:00
parent 98452ba62e
commit 66edf86ec3
5 changed files with 111 additions and 48 deletions

View File

@ -23,11 +23,9 @@ from oslo_log import log
from scipy.optimize import linprog
from bareon_dynamic_allocator import errors
from bareon_dynamic_allocator.parsers import DynamicSchemaParser
from bareon_dynamic_allocator import utils
from bareon_dynamic_allocator.objects import Disk
from bareon_dynamic_allocator.objects import Space
from bareon_dynamic_allocator.parser import Parser
from bareon_dynamic_allocator.sequences import CrossSumInequalitySequence
@ -37,56 +35,21 @@ LOG = log.getLogger(__name__)
class DynamicAllocator(object):
def __init__(self, hw_info, schema):
LOG.debug('Hardware information: \n%s', hw_info)
LOG.debug('Spaces schema: \n%s', schema)
self.hw_info = hw_info
self.raw_disks = hw_info['disks']
self.disks = [Disk(**disk) for disk in self.raw_disks]
rendered_spaces = self.convert_disks_to_indexes(
Parser(schema, hw_info).parse(),
hw_info)
LOG.debug('Rendered spaces schema: \n%s', rendered_spaces)
self.spaces = [Space(**space)
for space in rendered_spaces if space['type'] != 'vg']
LOG.debug('Hardware information: %s', hw_info)
LOG.debug('Spaces schema: %s', schema)
dynamic_schema = DynamicSchemaParser(hw_info, schema)
LOG.debug('Spaces objects: %s', dynamic_schema.spaces)
LOG.debug('Disks objects: \n%s', dynamic_schema.disks)
# Unallocated is required in order to be able to specify
# spaces with only minimal
self.spaces.append(Space(
id='unallocated',
type='unallocated',
none_order=True,
weight=0))
# Add fake volume Unallocated, in order to be able
# to have only volumes with minimal size, without
# additional space allocation
self.solver = DynamicAllocationLinearProgram(self.disks, self.spaces)
self.solver = DynamicAllocationLinearProgram(
dynamic_schema.disks,
dynamic_schema.spaces)
def generate_static(self):
sizes = self.solver.solve()
return sizes
def convert_disks_to_indexes(self, spaces, hw_info):
"""Convert disks to indexes.
Convert disks which are specified in `best_with_disks`
to a list of indexes in `disks` list.
"""
for i, space in enumerate(spaces):
if space.get('best_with_disks'):
disks_idx = set()
for disk in space['best_with_disks']:
try:
disks_idx.add(self.raw_disks.index(disk))
except ValueError as exc:
LOG.warn('Warning: %s', exc)
spaces[i]['best_with_disks'] = disks_idx
return spaces
class DynamicAllocationLinearProgram(object):
"""Linear programming allocator.

View File

@ -26,7 +26,7 @@ class Space(BaseObject):
'best_with_disks': set([]),
'weight': 1
}
required = ['id']
required = ['id', 'type']
def __init__(self, **kwargs):
super(Space, self).__init__(**kwargs)

View File

@ -0,0 +1,18 @@
# Copyright 2016 Mirantis, Inc.
#
# 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.
# flake8: noqa
from bareon_dynamic_allocator.parsers.expressions import ExpressionsParser
from bareon_dynamic_allocator.parsers.dynamic_schema_parser import DynamicSchemaParser

View File

@ -0,0 +1,82 @@
# Copyright 2016 Mirantis, Inc.
#
# 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
from bareon_dynamic_allocator.objects import Disk
from bareon_dynamic_allocator.objects import Space
from bareon_dynamic_allocator.parser import Parser
LOG = log.getLogger(__name__)
class DynamicSchemaParser(object):
def __init__(self, hw_info, schema):
self.hw_info = hw_info
self.schema = schema
self.raw_disks = self.hw_info['disks']
self.rendered_spaces = []
self.disks = []
self.spaces = []
self.parse()
self.post_parse()
def parse(self):
self.render_expressions()
self.disks = [
Disk(**disk)
for disk in self.raw_disks]
self.spaces = [
Space(**space)
for space in self.rendered_spaces if space['type'] != 'vg']
def post_parse(self):
# Add fake volume Unallocated, in order to be able
# to have only volumes with minimal size, without
# additional space allocation
self.spaces.append(Space(
id='unallocated',
type='unallocated',
none_order=True,
weight=0))
def render_expressions(self):
self.rendered_spaces = self._convert_disks_to_indexes(
Parser(self.schema, self.hw_info).parse(),
self.hw_info)
def _convert_disks_to_indexes(self, spaces, hw_info):
"""Convert disks to indexes.
Convert disks which are specified in `best_with_disks`
to a list of indexes in `disks` list.
"""
for i, space in enumerate(spaces):
if space.get('best_with_disks'):
disks_idx = set()
for disk in space['best_with_disks']:
try:
disks_idx.add(self.raw_disks.index(disk))
except ValueError as exc:
LOG.warn('Warning: %s', exc)
spaces[i]['best_with_disks'] = disks_idx
return spaces

View File

@ -53,7 +53,7 @@ class NoopParser(object):
return self.data
class Parser(object):
class ExpressionsParser(object):
yaql_re = re.compile(r'^\s*yaql\s*=\s*')