Coverage for manila/scheduler/drivers/chance.py: 29%

30 statements  

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

1# Copyright (c) 2010 OpenStack, LLC. 

2# Copyright 2010 United States Government as represented by the 

3# Administrator of the National Aeronautics and Space Administration. 

4# All Rights Reserved. 

5# 

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

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

8# a copy of the License at 

9# 

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

11# 

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

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

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

15# License for the specific language governing permissions and limitations 

16# under the License. 

17 

18""" 

19Chance (Random) Scheduler implementation 

20""" 

21 

22import random 

23 

24from oslo_config import cfg 

25 

26from manila import exception 

27from manila.i18n import _ 

28from manila.scheduler.drivers import base 

29 

30CONF = cfg.CONF 

31 

32 

33class ChanceScheduler(base.Scheduler): 

34 """Implements Scheduler as a random node selector.""" 

35 

36 def _filter_hosts(self, request_spec, hosts, **kwargs): 

37 """Filter a list of hosts based on request_spec.""" 

38 

39 filter_properties = kwargs.get('filter_properties', {}) 

40 ignore_hosts = filter_properties.get('ignore_hosts', []) 

41 hosts = [host for host in hosts if host not in ignore_hosts] 

42 return hosts 

43 

44 def _schedule(self, context, topic, request_spec, **kwargs): 

45 """Picks a host that is up at random.""" 

46 

47 elevated = context.elevated() 

48 hosts = self.hosts_up(elevated, topic) 

49 if not hosts: 

50 msg = _("Is the appropriate service running?") 

51 raise exception.NoValidHost(reason=msg) 

52 

53 hosts = self._filter_hosts(request_spec, hosts, **kwargs) 

54 if not hosts: 

55 msg = _("Could not find another host") 

56 raise exception.NoValidHost(reason=msg) 

57 

58 return hosts[int(random.random() * len(hosts))] 

59 

60 def schedule_create_share(self, context, request_spec, filter_properties): 

61 """Picks a host that is up at random.""" 

62 topic = CONF.share_topic 

63 host = self._schedule(context, topic, request_spec, 

64 filter_properties=filter_properties) 

65 share_id = request_spec['share_id'] 

66 snapshot_id = request_spec['snapshot_id'] 

67 

68 updated_share = base.share_update_db(context, share_id, host) 

69 self.share_rpcapi.create_share_instance( 

70 context, 

71 updated_share.instance, 

72 host, 

73 request_spec, 

74 filter_properties, 

75 snapshot_id 

76 )