Coverage for manila/scheduler/weighers/host_affinity.py: 100%

24 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2026-02-18 22:19 +0000

1# Copyright 2019 NetApp, Inc. 

2# All Rights Reserved. 

3# 

4# Licensed under the Apache License, Version 2.0 (the "License"); you may 

5# not use this file except in compliance with the License. You may obtain 

6# a copy of the License at 

7# 

8# http://www.apache.org/licenses/LICENSE-2.0 

9# 

10# Unless required by applicable law or agreed to in writing, software 

11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 

12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 

13# License for the specific language governing permissions and limitations 

14# under the License. 

15 

16from manila import context 

17from manila.db import api as db_api 

18from manila.scheduler.weighers import base_host 

19from manila.share import utils as share_utils 

20 

21 

22class HostAffinityWeigher(base_host.BaseHostWeigher): 

23 

24 def _weigh_object(self, obj, weight_properties): 

25 """Weigh hosts based on their proximity to the source's share pool. 

26 

27 If no snapshot_id was provided will return 0, otherwise, if source and 

28 destination hosts are located on: 

29 1. same back ends and pools: host is a perfect choice (100) 

30 2. same back ends and different pools: host is a very good choice (75) 

31 3. different back ends with the same AZ: host is a good choice (50) 

32 4. different back ends and AZs: host isn't so good choice (25) 

33 """ 

34 

35 ctx = context.get_admin_context() 

36 request_spec = weight_properties.get('request_spec') 

37 snapshot_id = request_spec.get('snapshot_id') 

38 snapshot_host = request_spec.get('snapshot_host') 

39 

40 if None in [snapshot_id, snapshot_host]: 

41 # NOTE(silvacarlose): if the request does not contain a snapshot_id 

42 # or a snapshot_host, the user is not creating a share from a 

43 # snapshot and we don't need to weigh the host. 

44 return 0 

45 

46 snapshot_ref = db_api.share_snapshot_get(ctx, snapshot_id) 

47 # Source host info: pool, backend and availability zone 

48 src_pool = share_utils.extract_host(snapshot_host, 'pool') 

49 src_backend = share_utils.extract_host( 

50 request_spec.get('snapshot_host'), 'backend') 

51 src_az = snapshot_ref['share']['availability_zone'] 

52 # Destination host info: pool, backend and availability zone 

53 dst_pool = share_utils.extract_host(obj.host, 'pool') 

54 dst_backend = share_utils.extract_host(obj.host, 'backend') 

55 # NOTE(dviroel): All hosts were already filtered by the availability 

56 # zone parameter. 

57 dst_az = None 

58 if weight_properties['availability_zone_id']: 

59 dst_az = db_api.availability_zone_get( 

60 ctx, weight_properties['availability_zone_id']).name 

61 

62 if src_backend == dst_backend: 

63 return 100 if (src_pool and src_pool == dst_pool) else 75 

64 else: 

65 return 50 if (src_az and src_az == dst_az) else 25