1# Copyright (c) 2011 OpenStack Foundation.
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 oslo_log import log as logging
17
18from cinder.scheduler import filters
19
20LOG = logging.getLogger(__name__)
21
22
23class IgnoreAttemptedHostsFilter(filters.BaseBackendFilter):
24    """Filter out previously attempted hosts
25
26    A host passes this filter if it has not already been attempted for
27    scheduling. The scheduler needs to add previously attempted hosts
28    to the 'retry' key of filter_properties in order for this to work
29    correctly. For example::
30
31     {
32      'retry': {
33                'backends': ['backend1', 'backend2'],
34                'num_attempts': 3,
35               }
36     }
37    """
38
39    def backend_passes(self, backend_state, filter_properties):
40        """Skip nodes that have already been attempted."""
41        attempted = filter_properties.get('retry')
42        if not attempted:
43            # Re-scheduling is disabled
44            LOG.debug("Re-scheduling is disabled.")
45            return True
46
47        # TODO(geguileo): In P - Just use backends
48        backends = attempted.get('backends', attempted.get('hosts', []))
49        backend = backend_state.backend_id
50
51        passes = backend not in backends
52        pass_msg = "passes" if passes else "fails"
53
54        LOG.debug("Backend %(backend)s %(pass_msg)s.  Previously tried "
55                  "backends: %(backends)s", {'backend': backend,
56                                             'pass_msg': pass_msg,
57                                             'backends': backends})
58        return passes
59