X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/777e716728a7da63ece00df7e5bb8be7f9a2a1a3..3ccc70f4bb06bab6c0b3c71f555cba24cc5c6a47:/services/nodemanager/arvnodeman/computenode/dispatch/__init__.py diff --git a/services/nodemanager/arvnodeman/computenode/dispatch/__init__.py b/services/nodemanager/arvnodeman/computenode/dispatch/__init__.py index 9ee26e336d..597a011e72 100644 --- a/services/nodemanager/arvnodeman/computenode/dispatch/__init__.py +++ b/services/nodemanager/arvnodeman/computenode/dispatch/__init__.py @@ -1,12 +1,18 @@ #!/usr/bin/env python +# Copyright (C) The Arvados Authors. All rights reserved. +# +# SPDX-License-Identifier: AGPL-3.0 from __future__ import absolute_import, print_function import functools import logging import time +import re import libcloud.common.types as cloud_types +from libcloud.common.exceptions import BaseHTTPError + import pykka from .. import \ @@ -16,6 +22,8 @@ from ...clientactor import _notify_subscribers from ... import config from .transitions import transitions +QuotaExceeded = "QuotaExceeded" + class ComputeNodeStateChangeBase(config.actor_class, RetryMixin): """Base class for actors that change a compute node's state. @@ -96,6 +104,7 @@ class ComputeNodeSetupActor(ComputeNodeStateChangeBase): self.cloud_size = cloud_size self.arvados_node = None self.cloud_node = None + self.error = None if arvados_node is None: self._later.create_arvados_node() else: @@ -104,14 +113,16 @@ class ComputeNodeSetupActor(ComputeNodeStateChangeBase): @ComputeNodeStateChangeBase._finish_on_exception @RetryMixin._retry(config.ARVADOS_ERRORS) def create_arvados_node(self): - self.arvados_node = self._arvados.nodes().create(body={}).execute() + self.arvados_node = self._arvados.nodes().create( + body={}, assign_slot=True).execute() self._later.create_cloud_node() @ComputeNodeStateChangeBase._finish_on_exception @RetryMixin._retry(config.ARVADOS_ERRORS) def prepare_arvados_node(self, node): - self.arvados_node = self._clean_arvados_node( - node, "Prepared by Node Manager") + self._clean_arvados_node(node, "Prepared by Node Manager") + self.arvados_node = self._arvados.nodes().update( + body={}, assign_slot=True).execute() self._later.create_cloud_node() @ComputeNodeStateChangeBase._finish_on_exception @@ -119,11 +130,31 @@ class ComputeNodeSetupActor(ComputeNodeStateChangeBase): def create_cloud_node(self): self._logger.info("Sending create_node request for node size %s.", self.cloud_size.name) - self.cloud_node = self._cloud.create_node(self.cloud_size, - self.arvados_node) + try: + self.cloud_node = self._cloud.create_node(self.cloud_size, + self.arvados_node) + except BaseHTTPError as e: + if e.code == 429 or "RequestLimitExceeded" in e.message: + # Don't consider API rate limits to be quota errors. + # re-raise so the Retry logic applies. + raise + + # The set of possible error codes / messages isn't documented for + # all clouds, so use a keyword heuristic to determine if the + # failure is likely due to a quota. + if re.search(r'(exceed|quota|limit)', e.message, re.I): + self.error = QuotaExceeded + self._logger.warning("Quota exceeded: %s", e) + self._finished() + return + else: + # Something else happened, re-raise so the Retry logic applies. + raise + except Exception as e: + raise # The information included in the node size object we get from libcloud - # is inconsistent between cloud providers. Replace libcloud NodeSize + # is inconsistent between cloud drivers. Replace libcloud NodeSize # object with compatible CloudSizeWrapper object which merges the size # info reported from the cloud with size information from the # configuration file. @@ -211,6 +242,9 @@ class ComputeNodeShutdownActor(ComputeNodeStateChangeBase): return super(ComputeNodeShutdownActor, self)._finished() def cancel_shutdown(self, reason, **kwargs): + if self.cancel_reason is not None: + # already cancelled + return self.cancel_reason = reason self._logger.info("Shutdown cancelled: %s.", reason) self._finished(success_flag=False) @@ -228,6 +262,9 @@ class ComputeNodeShutdownActor(ComputeNodeStateChangeBase): @_cancel_on_exception def shutdown_node(self): + if self.cancel_reason is not None: + # already cancelled + return if self.cancellable: self._logger.info("Checking that node is still eligible for shutdown") eligible, reason = self._monitor.shutdown_eligible().get() @@ -256,7 +293,7 @@ class ComputeNodeShutdownActor(ComputeNodeStateChangeBase): self._finished(success_flag=True) -class ComputeNodeUpdateActor(config.actor_class): +class ComputeNodeUpdateActor(config.actor_class, RetryMixin): """Actor to dispatch one-off cloud management requests. This actor receives requests for small cloud updates, and @@ -265,12 +302,12 @@ class ComputeNodeUpdateActor(config.actor_class): dedicated actor for this gives us the opportunity to control the flow of requests; e.g., by backing off when errors occur. """ - def __init__(self, cloud_factory, max_retry_wait=180): + def __init__(self, cloud_factory, timer_actor, max_retry_wait=180): super(ComputeNodeUpdateActor, self).__init__() + RetryMixin.__init__(self, 1, max_retry_wait, + None, cloud_factory(), timer_actor) self._cloud = cloud_factory() - self.max_retry_wait = max_retry_wait - self.error_streak = 0 - self.next_request_time = time.time() + self._later = self.actor_ref.tell_proxy() def _set_logger(self): self._logger = logging.getLogger("%s.%s" % (self.__class__.__name__, self.actor_urn[33:])) @@ -278,30 +315,10 @@ class ComputeNodeUpdateActor(config.actor_class): def on_start(self): self._set_logger() - def _throttle_errors(orig_func): - @functools.wraps(orig_func) - def throttle_wrapper(self, *args, **kwargs): - throttle_time = self.next_request_time - time.time() - if throttle_time > 0: - time.sleep(throttle_time) - self.next_request_time = time.time() - try: - result = orig_func(self, *args, **kwargs) - except Exception as error: - if self._cloud.is_cloud_exception(error): - self.error_streak += 1 - self.next_request_time += min(2 ** self.error_streak, - self.max_retry_wait) - self._logger.warn( - "Unhandled exception: %s", error, exc_info=error) - else: - self.error_streak = 0 - return result - return throttle_wrapper - - @_throttle_errors + @RetryMixin._retry() def sync_node(self, cloud_node, arvados_node): - return self._cloud.sync_node(cloud_node, arvados_node) + if self._cloud.node_fqdn(cloud_node) != arvados_node_fqdn(arvados_node): + return self._cloud.sync_node(cloud_node, arvados_node) class ComputeNodeMonitorActor(config.actor_class): @@ -312,14 +329,13 @@ class ComputeNodeMonitorActor(config.actor_class): for shutdown. """ def __init__(self, cloud_node, cloud_node_start_time, shutdown_timer, - cloud_fqdn_func, timer_actor, update_actor, cloud_client, + timer_actor, update_actor, cloud_client, arvados_node=None, poll_stale_after=600, node_stale_after=3600, boot_fail_after=1800 ): super(ComputeNodeMonitorActor, self).__init__() self._later = self.actor_ref.tell_proxy() self._shutdowns = shutdown_timer - self._cloud_node_fqdn = cloud_fqdn_func self._timer = timer_actor self._update = update_actor self._cloud = cloud_client @@ -350,9 +366,15 @@ class ComputeNodeMonitorActor(config.actor_class): def get_state(self): """Get node state, one of ['unpaired', 'busy', 'idle', 'down'].""" - # If this node is not associated with an Arvados node, return 'unpaired'. + # If this node is not associated with an Arvados node, return + # 'unpaired' if we're in the boot grace period, and 'down' if not, + # so it isn't counted towards usable nodes. if self.arvados_node is None: - return 'unpaired' + if timestamp_fresh(self.cloud_node_start_time, + self.boot_fail_after): + return 'unpaired' + else: + return 'down' state = self.arvados_node['crunch_worker_state'] @@ -466,8 +488,11 @@ class ComputeNodeMonitorActor(config.actor_class): self._later.consider_shutdown() def update_arvados_node(self, arvados_node): - # If the cloud node's FQDN doesn't match what's in the Arvados node - # record, make them match. + """Called when the latest Arvados node record is retrieved. + + Calls the updater's sync_node() method. + + """ # This method is a little unusual in the way it just fires off the # request without checking the result or retrying errors. That's # because this update happens every time we reload the Arvados node @@ -476,7 +501,5 @@ class ComputeNodeMonitorActor(config.actor_class): # the logic to throttle those effective retries when there's trouble. if arvados_node is not None: self.arvados_node = arvados_node - if (self._cloud_node_fqdn(self.cloud_node) != - arvados_node_fqdn(self.arvados_node)): - self._update.sync_node(self.cloud_node, self.arvados_node) + self._update.sync_node(self.cloud_node, self.arvados_node) self._later.consider_shutdown()