"Fossies" - the Fresh Open Source Software Archive

Member "cinder-13.0.7/cinder/scheduler/filters/affinity_filter.py" (4 Oct 2019, 4054 Bytes) of package /linux/misc/openstack/cinder-13.0.7.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "affinity_filter.py" see the Fossies "Dox" file reference documentation.

    1 # Copyright 2014, eBay Inc.
    2 # Copyright 2014, OpenStack Foundation
    3 # All Rights Reserved.
    4 #
    5 # Licensed under the Apache License, Version 2.0 (the "License");
    6 # you may not use this file except in compliance with the License.
    7 # You may obtain a copy of the License at
    8 #
    9 # http://www.apache.org/licenses/LICENSE-2.0
   10 #
   11 # Unless required by applicable law or agreed to in writing, software
   12 # distributed under the License is distributed on an "AS IS" BASIS,
   13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14 # See the License for the specific language governing permissions and
   15 # limitations under the License.
   16 
   17 from oslo_utils import uuidutils
   18 
   19 from cinder.scheduler import filters
   20 from cinder.volume import api as volume
   21 
   22 
   23 class AffinityFilter(filters.BaseBackendFilter):
   24     def __init__(self):
   25         self.volume_api = volume.API()
   26 
   27     def _get_volumes(self, context, affinity_uuids, backend_state):
   28         filters = {'id': affinity_uuids, 'deleted': False}
   29         if backend_state.cluster_name:
   30             filters['cluster_name'] = backend_state.cluster_name
   31         else:
   32             filters['host'] = backend_state.host
   33         return self.volume_api.get_all(context, filters=filters)
   34 
   35 
   36 class DifferentBackendFilter(AffinityFilter):
   37     """Schedule volume on a different back-end from a set of volumes."""
   38 
   39     def backend_passes(self, backend_state, filter_properties):
   40         context = filter_properties['context']
   41         scheduler_hints = filter_properties.get('scheduler_hints') or {}
   42 
   43         affinity_uuids = scheduler_hints.get('different_host', [])
   44 
   45         # scheduler hint verification: affinity_uuids can be a list of uuids
   46         # or single uuid.  The checks here is to make sure every single string
   47         # in the list looks like a uuid, otherwise, this filter will fail to
   48         # pass.  Note that the filter does *NOT* ignore string doesn't look
   49         # like a uuid, it is better to fail the request than serving it wrong.
   50         if isinstance(affinity_uuids, list):
   51             for uuid in affinity_uuids:
   52                 if uuidutils.is_uuid_like(uuid):
   53                     continue
   54                 else:
   55                     return False
   56         elif uuidutils.is_uuid_like(affinity_uuids):
   57             affinity_uuids = [affinity_uuids]
   58         else:
   59             # Not a list, not a string looks like uuid, don't pass it
   60             # to DB for query to avoid potential risk.
   61             return False
   62 
   63         if affinity_uuids:
   64             return not self._get_volumes(context, affinity_uuids,
   65                                          backend_state)
   66         # With no different_host key
   67         return True
   68 
   69 
   70 class SameBackendFilter(AffinityFilter):
   71     """Schedule volume on the same back-end as another volume."""
   72 
   73     def backend_passes(self, backend_state, filter_properties):
   74         context = filter_properties['context']
   75         scheduler_hints = filter_properties.get('scheduler_hints') or {}
   76 
   77         affinity_uuids = scheduler_hints.get('same_host', [])
   78 
   79         # scheduler hint verification: affinity_uuids can be a list of uuids
   80         # or single uuid.  The checks here is to make sure every single string
   81         # in the list looks like a uuid, otherwise, this filter will fail to
   82         # pass.  Note that the filter does *NOT* ignore string doesn't look
   83         # like a uuid, it is better to fail the request than serving it wrong.
   84         if isinstance(affinity_uuids, list):
   85             for uuid in affinity_uuids:
   86                 if uuidutils.is_uuid_like(uuid):
   87                     continue
   88                 else:
   89                     return False
   90         elif uuidutils.is_uuid_like(affinity_uuids):
   91             affinity_uuids = [affinity_uuids]
   92         else:
   93             # Not a list, not a string looks like uuid, don't pass it
   94             # to DB for query to avoid potential risk.
   95             return False
   96 
   97         if affinity_uuids:
   98             return self._get_volumes(context, affinity_uuids, backend_state)
   99 
  100         # With no same_host key
  101         return True