summaryrefslogtreecommitdiff
path: root/heat/engine/check_resource.py
blob: 1cd0bcaa79833cd0a36e15326b32f6ac373be32a (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# Copyright (c) 2016 Hewlett-Packard Development Company, L.P.
#
# 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 six

import eventlet.queue
import functools

from oslo_log import log as logging

from heat.common import exception
from heat.common.i18n import _LE
from heat.common.i18n import _LI
from heat.engine import resource
from heat.engine import scheduler
from heat.engine import stack as parser
from heat.engine import sync_point
from heat.objects import resource as resource_objects
from heat.rpc import api as rpc_api
from heat.rpc import listener_client

LOG = logging.getLogger(__name__)


class CancelOperation(BaseException):
    """Exception to cancel an in-progress operation on a resource.

    This exception is raised when operations on a resource are cancelled.
    """
    def __init__(self):
        return super(CancelOperation, self).__init__('user triggered cancel')


class CheckResource(object):

    def __init__(self,
                 engine_id,
                 rpc_client,
                 thread_group_mgr,
                 msg_queue):
        self.engine_id = engine_id
        self._rpc_client = rpc_client
        self.thread_group_mgr = thread_group_mgr
        self.msg_queue = msg_queue

    def _try_steal_engine_lock(self, cnxt, resource_id):
        rs_obj = resource_objects.Resource.get_obj(cnxt,
                                                   resource_id)
        if rs_obj.engine_id not in (None, self.engine_id):
            if not listener_client.EngineListenerClient(
                    rs_obj.engine_id).is_alive(cnxt):
                # steal the lock.
                rs_obj.update_and_save({'engine_id': None})
                return True
        return False

    def _trigger_rollback(self, stack):
        LOG.info(_LI("Triggering rollback of %(stack_name)s %(action)s "),
                 {'action': stack.action, 'stack_name': stack.name})
        stack.rollback()

    def _handle_failure(self, cnxt, stack, failure_reason):
        updated = stack.state_set(stack.action, stack.FAILED, failure_reason)
        if not updated:
            return False

        if (not stack.disable_rollback and
                stack.action in (stack.CREATE, stack.ADOPT, stack.UPDATE)):
            self._trigger_rollback(stack)
        else:
            stack.purge_db()
        return True

    def _handle_resource_failure(self, cnxt, is_update, rsrc_id,
                                 stack, failure_reason):
        failure_handled = self._handle_failure(cnxt, stack, failure_reason)
        if not failure_handled:
            # Another concurrent update has taken over. But there is a
            # possibility for that update to be waiting for this rsrc to
            # complete, hence retrigger current rsrc for latest traversal.
            traversal = stack.current_traversal
            latest_stack = parser.Stack.load(cnxt, stack_id=stack.id,
                                             force_reload=True)
            if traversal != latest_stack.current_traversal:
                self._retrigger_check_resource(cnxt, is_update, rsrc_id,
                                               latest_stack)

    def _handle_stack_timeout(self, cnxt, stack):
        failure_reason = u'Timed out'
        self._handle_failure(cnxt, stack, failure_reason)

    def _do_check_resource(self, cnxt, current_traversal, tmpl, resource_data,
                           is_update, rsrc, stack, adopt_stack_data):
        try:
            if is_update:
                try:
                    check_resource_update(rsrc, tmpl.id, resource_data,
                                          self.engine_id,
                                          stack, self.msg_queue)
                except resource.UpdateReplace:
                    new_res_id = rsrc.make_replacement(tmpl.id)
                    LOG.info(_LI("Replacing resource with new id %s"),
                             new_res_id)
                    rpc_data = sync_point.serialize_input_data(resource_data)
                    self._rpc_client.check_resource(cnxt,
                                                    new_res_id,
                                                    current_traversal,
                                                    rpc_data, is_update,
                                                    adopt_stack_data)
                    return False

            else:
                check_resource_cleanup(rsrc, tmpl.id, resource_data,
                                       self.engine_id,
                                       stack.time_remaining(), self.msg_queue)

            return True
        except exception.UpdateInProgress:
            if self._try_steal_engine_lock(cnxt, rsrc.id):
                rpc_data = sync_point.serialize_input_data(resource_data)
                # set the resource state as failed
                status_reason = ('Worker went down '
                                 'during resource %s' % rsrc.action)
                rsrc.state_set(rsrc.action,
                               rsrc.FAILED,
                               six.text_type(status_reason))
                self._rpc_client.check_resource(cnxt,
                                                rsrc.id,
                                                current_traversal,
                                                rpc_data, is_update,
                                                adopt_stack_data)
        except exception.ResourceFailure as ex:
            action = ex.action or rsrc.action
            reason = 'Resource %s failed: %s' % (action,
                                                 six.text_type(ex))
            self._handle_resource_failure(cnxt, is_update, rsrc.id,
                                          stack, reason)
        except scheduler.Timeout:
            # reload the stack to verify current traversal
            stack = parser.Stack.load(cnxt, stack_id=stack.id)
            if stack.current_traversal != current_traversal:
                return
            self._handle_stack_timeout(cnxt, stack)
        except CancelOperation:
            pass

        return False

    def _retrigger_check_resource(self, cnxt, is_update, resource_id, stack):
        current_traversal = stack.current_traversal
        graph = stack.convergence_dependencies.graph()
        key = (resource_id, is_update)
        if is_update:
            # When re-trigger received for update in latest traversal, first
            # check if update key is available in graph.
            # if No, then latest traversal is waiting for delete.
            if (resource_id, is_update) not in graph:
                key = (resource_id, not is_update)
        else:
            # When re-trigger received for delete in latest traversal, first
            # check if update key is available in graph,
            # if yes, then latest traversal is waiting for update.
            if (resource_id, True) in graph:
                # not is_update evaluates to True below, which means update
                key = (resource_id, not is_update)
        LOG.info(_LI('Re-trigger resource: (%(key1)s, %(key2)s)'),
                 {'key1': key[0], 'key2': key[1]})
        predecessors = set(graph[key])

        try:
            propagate_check_resource(cnxt, self._rpc_client, resource_id,
                                     current_traversal, predecessors, key,
                                     None, key[1], None)
        except exception.EntityNotFound as e:
            if e.entity != "Sync Point":
                raise

    def _initiate_propagate_resource(self, cnxt, resource_id,
                                     current_traversal, is_update, rsrc,
                                     stack):
        deps = stack.convergence_dependencies
        graph = deps.graph()
        graph_key = (resource_id, is_update)

        if graph_key not in graph and rsrc.replaces is not None:
            # If we are a replacement, impersonate the replaced resource for
            # the purposes of calculating whether subsequent resources are
            # ready, since everybody has to work from the same version of the
            # graph. Our real resource ID is sent in the input_data, so the
            # dependencies will get updated to point to this resource in time
            # for the next traversal.
            graph_key = (rsrc.replaces, is_update)

        def _get_input_data(req, fwd):
            if fwd:
                return construct_input_data(rsrc, stack)
            else:
                # Don't send data if initiating clean-up for self i.e.
                # initiating delete of a replaced resource
                if req not in graph_key:
                    # send replaced resource as needed_by if it exists
                    return (rsrc.replaced_by
                            if rsrc.replaced_by is not None
                            else resource_id)
            return None

        try:
            for req, fwd in deps.required_by(graph_key):
                input_data = _get_input_data(req, fwd)
                propagate_check_resource(
                    cnxt, self._rpc_client, req, current_traversal,
                    set(graph[(req, fwd)]), graph_key, input_data, fwd,
                    stack.adopt_stack_data)

            check_stack_complete(cnxt, stack, current_traversal,
                                 graph_key[0], deps, graph_key[1])
        except exception.EntityNotFound as e:
            if e.entity == "Sync Point":
                # Reload the stack to determine the current traversal, and
                # check the SyncPoint for the current node to determine if
                # it is ready. If it is, then retrigger the current node
                # with the appropriate data for the latest traversal.
                stack = parser.Stack.load(cnxt, stack_id=rsrc.stack.id,
                                          force_reload=True)
                if current_traversal == stack.current_traversal:
                    LOG.debug('[%s] Traversal sync point missing.',
                              current_traversal)
                    return

                self._retrigger_check_resource(cnxt, is_update,
                                               resource_id, stack)
            else:
                raise

    def check(self, cnxt, resource_id, current_traversal,
              resource_data, is_update, adopt_stack_data,
              rsrc, stack):
        """Process a node in the dependency graph.

        The node may be associated with either an update or a cleanup of its
        associated resource.
        """
        if stack.has_timed_out():
            self._handle_stack_timeout(cnxt, stack)
            return

        tmpl = stack.t
        stack.adopt_stack_data = adopt_stack_data
        stack.thread_group_mgr = self.thread_group_mgr

        if is_update:
            if (rsrc.replaced_by is not None and
                    rsrc.current_template_id != tmpl.id):
                return

        check_resource_done = self._do_check_resource(cnxt, current_traversal,
                                                      tmpl, resource_data,
                                                      is_update,
                                                      rsrc, stack,
                                                      adopt_stack_data)

        if check_resource_done:
            # initiate check on next set of resources from graph
            self._initiate_propagate_resource(cnxt, resource_id,
                                              current_traversal, is_update,
                                              rsrc, stack)


def load_resource(cnxt, resource_id, resource_data, is_update):
    if is_update:
        cache_data = {in_data.get(
            'name'): in_data for in_data in resource_data.values()
            if in_data is not None}
    else:
        # no data to resolve in cleanup phase
        cache_data = {}

    try:
        return resource.Resource.load(cnxt, resource_id,
                                      is_update, cache_data)
    except (exception.ResourceNotFound, exception.NotFound):
        # can be ignored
        return None, None, None


def _resolve_attributes(dep_attrs, rsrc):
    resolved_attributes = {}
    for attr in dep_attrs:
        try:
            if isinstance(attr, six.string_types):
                resolved_attributes[attr] = rsrc.get_attribute(attr)
            else:
                resolved_attributes[attr] = rsrc.get_attribute(*attr)
        except exception.InvalidTemplateAttribute as ita:
            LOG.info(six.text_type(ita))
    return resolved_attributes


def construct_input_data(rsrc, curr_stack):
    dep_attrs = curr_stack.get_dep_attrs(
        six.itervalues(curr_stack.resources),
        curr_stack.outputs,
        rsrc.name,
        curr_stack.t.OUTPUT_VALUE)
    input_data = {'id': rsrc.id,
                  'name': rsrc.name,
                  'reference_id': rsrc.get_reference_id(),
                  'attrs': _resolve_attributes(dep_attrs, rsrc),
                  'status': rsrc.status,
                  'action': rsrc.action,
                  'uuid': rsrc.uuid}
    return input_data


def check_stack_complete(cnxt, stack, current_traversal, sender_id, deps,
                         is_update):
    """Mark the stack complete if the update is complete.

    Complete is currently in the sense that all desired resources are in
    service, not that superfluous ones have been cleaned up.
    """
    roots = set(deps.roots())

    if (sender_id, is_update) not in roots:
        return

    def mark_complete(stack_id, data):
        stack.mark_complete()

    sender_key = (sender_id, is_update)
    sync_point.sync(cnxt, stack.id, current_traversal, True,
                    mark_complete, roots, {sender_key: None})


def propagate_check_resource(cnxt, rpc_client, next_res_id,
                             current_traversal, predecessors, sender_key,
                             sender_data, is_update, adopt_stack_data):
    """Trigger processing of node if all of its dependencies are satisfied."""
    def do_check(entity_id, data):
        rpc_client.check_resource(cnxt, entity_id, current_traversal,
                                  data, is_update, adopt_stack_data)

    sync_point.sync(cnxt, next_res_id, current_traversal,
                    is_update, do_check, predecessors,
                    {sender_key: sender_data})


def _check_for_message(msg_queue):
    if msg_queue is None:
        return
    try:
        message = msg_queue.get_nowait()
    except eventlet.queue.Empty:
        return

    if message == rpc_api.THREAD_CANCEL:
        raise CancelOperation

    LOG.error(_LE('Unknown message "%s" received'), message)


def check_resource_update(rsrc, template_id, resource_data, engine_id,
                          stack, msg_queue):
    """Create or update the Resource if appropriate."""
    check_message = functools.partial(_check_for_message, msg_queue)
    if rsrc.action == resource.Resource.INIT:
        rsrc.create_convergence(template_id, resource_data, engine_id,
                                stack.time_remaining(), check_message)
    else:
        rsrc.update_convergence(template_id, resource_data, engine_id,
                                stack.time_remaining(), stack,
                                check_message)


def check_resource_cleanup(rsrc, template_id, resource_data, engine_id,
                           timeout, msg_queue):
    """Delete the Resource if appropriate."""
    check_message = functools.partial(_check_for_message, msg_queue)
    rsrc.delete_convergence(template_id, resource_data, engine_id, timeout,
                            check_message)