summaryrefslogtreecommitdiff
path: root/heat/engine/resources/openstack/heat/delay.py
blob: 5a6623273eafbff63765f8af34582402883487fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#
#    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.

import random

from heat.common import exception
from heat.common.i18n import _
from heat.engine import constraints
from heat.engine import properties
from heat.engine import resource
from heat.engine import support

from oslo_utils import timeutils


class Delay(resource.Resource):
    """A resource that pauses for a configurable delay.

    By manipulating the dependency relationships between resources in the
    template, a delay can be inserted at an arbitrary point during e.g. stack
    creation or deletion. They delay will occur after any resource that it
    depends on during CREATE or SUSPEND, and before any resource that it
    depends on during DELETE or RESUME. Similarly, it will occur before any
    resource that depends on it during CREATE or SUSPEND, and after any
    resource thet depends on it during DELETE or RESUME.

    If a non-zero maximum jitter is specified, a random amount of jitter -
    chosen with uniform probability in the range from 0 to the product of the
    maximum jitter value and the jitter multiplier (1s by default) - is added
    to the minimum delay time. This can be used, for example, in the scaled
    unit of a large scaling group to prevent 'thundering herd' issues.
    """

    support_status = support.SupportStatus(version='11.0.0')

    _ALLOWED_ACTIONS = (
        resource.Resource.CREATE,
        resource.Resource.DELETE,
        resource.Resource.SUSPEND,
        resource.Resource.RESUME,
    )

    PROPERTIES = (
        MIN_WAIT_SECS, MAX_JITTER, JITTER_MULTIPLIER_SECS, DELAY_ACTIONS,
    ) = (
        'min_wait', 'max_jitter', 'jitter_multiplier', 'actions',
    )

    properties_schema = {
        MIN_WAIT_SECS: properties.Schema(
            properties.Schema.NUMBER,
            _('Minimum time in seconds to wait during the specified actions.'),
            update_allowed=True,
            default=0,
            constraints=[
                constraints.Range(min=0)
            ]
        ),
        MAX_JITTER: properties.Schema(
            properties.Schema.NUMBER,
            _('Maximum jitter to add to the minimum wait time.'),
            update_allowed=True,
            default=0,
            constraints=[
                constraints.Range(min=0),
            ]
        ),
        JITTER_MULTIPLIER_SECS: properties.Schema(
            properties.Schema.NUMBER,
            _('Number of seconds to multiply the maximum jitter value by.'),
            update_allowed=True,
            default=1.0,
            constraints=[
                constraints.Range(min=0),
            ]
        ),
        DELAY_ACTIONS: properties.Schema(
            properties.Schema.LIST,
            _('Actions during which the delay will occur.'),
            update_allowed=True,
            default=[resource.Resource.CREATE],
            constraints=[constraints.AllowedValues(_ALLOWED_ACTIONS)]
        ),
    }

    attributes_schema = {}

    def _delay_parameters(self):
        """Return a tuple of the min delay and max jitter, in seconds."""
        min_wait_secs = self.properties[self.MIN_WAIT_SECS]
        max_jitter_secs = (self.properties[self.MAX_JITTER] *
                           self.properties[self.JITTER_MULTIPLIER_SECS])
        return min_wait_secs, max_jitter_secs

    def validate(self):
        result = super(Delay, self).validate()
        if not self.stack.strict_validate:
            return result

        min_wait_secs, max_jitter_secs = self._delay_parameters()
        max_wait = min_wait_secs + max_jitter_secs
        if max_wait > self.stack.timeout_secs():
            raise exception.StackValidationFailed(_('%(res_type)s maximum '
                                                    'delay %(max_wait)ss '
                                                    'exceeds stack timeout.') %
                                                  {'res_type': self.type,
                                                   'max_wait': max_wait})
        return result

    def _wait_secs(self, action):
        """Return a (randomised) wait time for the specified action."""
        if action not in self.properties[self.DELAY_ACTIONS]:
            return 0

        min_wait_secs, max_jitter_secs = self._delay_parameters()
        return min_wait_secs + (max_jitter_secs * random.random())

    def _handle_action(self):
        """Return a tuple of the start time in UTC and the time to wait."""
        return timeutils.utcnow(), self._wait_secs(self.action)

    @staticmethod
    def _check_complete(started_at, wait_secs):
        if not wait_secs:
            return True
        elapsed_secs = (timeutils.utcnow() - started_at).total_seconds()
        if elapsed_secs >= wait_secs:
            return True
        remaining = wait_secs - elapsed_secs
        if remaining >= 4:
            raise resource.PollDelay(int(remaining // 2))
        return False

    def handle_create(self):
        return self._handle_action()

    def handle_delete(self):
        return self._handle_action()

    def handle_suspend(self):
        return self._handle_action()

    def handle_resume(self):
        return self._handle_action()

    def check_create_complete(self, cookie):
        return self._check_complete(*cookie)

    def check_delete_complete(self, cookie):
        return self._check_complete(*cookie)

    def check_suspend_complete(self, cookie):
        return self._check_complete(*cookie)

    def check_resume_complete(self, cookie):
        return self._check_status_complete(*cookie)


def resource_mapping():
    return {
        'OS::Heat::Delay': Delay,
    }