Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CLOUDOPS-548] [stable/1.0]Allow retry on openstack HttpException #128

Open
wants to merge 3 commits into
base: stable/1.0
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .github/workflows/unittests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: unittests

on:
workflow_dispatch:
push:
branches:
- 'stable/1.0'
tags:
- 'v*'
pull_request:
branches:
- 'stable/1.0'

jobs:
unuttest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Setup Python
uses: actions/setup-python@v4

- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y tox
- name: Run tox -e py3
run: tox -e py3
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -14,5 +14,4 @@ oslo_versionedobjects
openstacksdk>0.28.0
pymysql
parse
# email
# smtplib
tenacity
71 changes: 67 additions & 4 deletions staffeln/common/openstack.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
from openstack import exceptions, proxy
from oslo_log import log
from staffeln.common import auth
from staffeln import conf
from staffeln.i18n import _

import tenacity

CONF = conf.CONF
LOG = log.getLogger(__name__)

class RetryHTTPError(tenacity.retry_if_exception):
"""Retry strategy that retries if the exception is an ``HTTPError`` with
a abnormal status code.
"""

def __init__(self):
def is_http_error(exception):
# Make sure we don't retry on codes in skip list (default: 404),
# as not found could be an expected status.
skip_codes = CONF.openstack.skip_retry_codes.replace(
' ', '').split(',')
result = (isinstance(exception, exceptions.HttpException) and
str(exception.status_code) not in skip_codes)
if result:
LOG.debug(f"Getting HttpException {exception} (status "
f"code: {exception.status_code}), "
"retry till timeout...")
return result

super().__init__(predicate=is_http_error)


class OpenstackSDK:
def __init__(self):
@@ -23,6 +48,11 @@ def set_project(self, project):
self.conn = self.conn_list[project_id]

# user
@tenacity.retry(
retry=RetryHTTPError(),
wait=tenacity.wait_exponential(max=CONF.openstack.max_retry_interval),
reraise=True,
stop=tenacity.stop_after_delay(CONF.openstack.retry_timeout))
def get_user_id(self):
user_name = self.conn.config.auth["username"]
if "user_domain_id" in self.conn.config.auth:
@@ -35,17 +65,40 @@ def get_user_id(self):
user = self.conn.get_user(name_or_id=user_name)
return user.id

@tenacity.retry(
retry=RetryHTTPError(),
wait=tenacity.wait_exponential(max=CONF.openstack.max_retry_interval),
reraise=True,
stop=tenacity.stop_after_delay(CONF.openstack.retry_timeout))
def get_projects(self):
return self.conn.list_projects()

def get_servers(self, project_id, all_projects=True, details=True):
return self.conn.compute.servers(
details=details, all_projects=all_projects, project_id=project_id
)
@tenacity.retry(
retry=RetryHTTPError(),
wait=tenacity.wait_exponential(max=CONF.openstack.max_retry_interval),
reraise=True,
stop=tenacity.stop_after_delay(CONF.openstack.retry_timeout))
def get_servers(self, project_id=None, all_projects=True, details=True):
if project_id is not None:
return self.conn.compute.servers(
details=details, all_projects=all_projects, project_id=project_id
)
else:
return self.conn.compute.servers(details=details, all_projects=all_projects)

@tenacity.retry(
retry=RetryHTTPError(),
wait=tenacity.wait_exponential(max=CONF.openstack.max_retry_interval),
reraise=True,
stop=tenacity.stop_after_delay(CONF.openstack.retry_timeout))
def get_volume(self, uuid, project_id):
return self.conn.get_volume_by_id(uuid)

@tenacity.retry(
retry=RetryHTTPError(),
wait=tenacity.wait_exponential(max=CONF.openstack.max_retry_interval),
reraise=True,
stop=tenacity.stop_after_delay(CONF.openstack.retry_timeout))
def get_backup(self, uuid, project_id=None):
# return conn.block_storage.get_backup(
# project_id=project_id, backup_id=uuid,
@@ -66,6 +119,11 @@ def create_backup(self, volume_id, project_id, force=True, wait=False):
wait=wait,
)

@tenacity.retry(
retry=RetryHTTPError(),
wait=tenacity.wait_exponential(max=CONF.openstack.max_retry_interval),
reraise=True,
stop=tenacity.stop_after_delay(CONF.openstack.retry_timeout))
def delete_backup(self, uuid, project_id=None, force=False):
# Note(Alex): v3 is not supporting force delete?
# conn.block_storage.delete_backup(
@@ -77,6 +135,11 @@ def delete_backup(self, uuid, project_id=None, force=False):
except exceptions.ResourceNotFound:
return None

@tenacity.retry(
retry=RetryHTTPError(),
wait=tenacity.wait_exponential(max=CONF.openstack.max_retry_interval),
reraise=True,
stop=tenacity.stop_after_delay(CONF.openstack.retry_timeout))
def get_backup_quota(self, project_id):
# quota = conn.get_volume_quotas(project_id)
quota = self._get_volume_quotas(project_id)
18 changes: 6 additions & 12 deletions staffeln/conductor/backup.py
Original file line number Diff line number Diff line change
@@ -238,12 +238,8 @@ def check_instance_volumes(self):
try:
servers = self.openstacksdk.get_servers(project_id=project.id)
except OpenstackHttpException as ex:
LOG.warn(
_(
"Failed to list servers in project %s. %s"
% (project.id, str(ex))
)
)
LOG.warn(f"Failed to list servers in project {project.id}. "
f"{str(ex)} (status code: {ex.status_code}).")
continue
for server in servers:
if not self.filter_by_server_metadata(server.metadata):
@@ -349,18 +345,16 @@ def process_pre_failed_backup(self, task):

def process_failed_backup(self, task):
# 1. notify via email
reason = _("The status of backup for the volume %s is error." % task.volume_id)
reason = f"The status of backup for the volume {task.volume_id} is error."
self.result.add_failed_backup(task.project_id, task.volume_id, reason)
LOG.warn(reason)
# 2. delete backup generator
try:
self.openstacksdk.delete_backup(uuid=task.backup_id, force=True)
except OpenstackHttpException as ex:
LOG.error(
_(
"Failed to delete volume backup %s. %s. Need to delete manually."
% (task.backup_id, str(ex))
)
LOG.warn(
f"Failed to delete volume backup {task.backup_id}. {ex} "
f"(status code: {ex.status_code}). Need to delete manually."
)
# 3. remove failed task from the task queue
task.delete_queue()
40 changes: 39 additions & 1 deletion staffeln/conf/conductor.py
Original file line number Diff line number Diff line change
@@ -8,6 +8,15 @@
help=_("Options under this group are used " "to define Conductor's configuration."),
)

openstack_group = cfg.OptGroup(
"openstack",
title="OpenStack Options",
help=_(
"Options under this group are used "
"to define OpneStack related configuration."
),
)

backup_opts = [
cfg.IntOpt(
"backup_workers",
@@ -41,6 +50,30 @@
),
]

openstack_opts = [
cfg.IntOpt(
"retry_timeout",
default=300,
min=1,
help=_("The timeout for retry OpenStackSDK HTTP exceptions, "
"the unit is one second."),
),
cfg.IntOpt(
"max_retry_interval",
default=30,
min=0,
help=_("Max time interval for retry OpenStackSDK HTTP exceptions, "
"the unit is one second."),
),
cfg.StrOpt(
"skip_retry_codes",
default="404,",
help=_("A comma separated string that provides a list of HTTP codes "
"to skip retry on for OpenStackSDK HTTP "
"exception. Default only `404` is skipped."),
),
]

rotation_opts = [
cfg.IntOpt(
"rotation_workers",
@@ -85,7 +118,12 @@ def register_opts(conf):
conf.register_group(conductor_group)
conf.register_opts(backup_opts, group=conductor_group)
conf.register_opts(rotation_opts, group=conductor_group)
conf.register_opts(openstack_opts, group=openstack_group)


def list_opts():
return {"DEFAULT": rotation_opts, conductor_group: backup_opts}
return {
"DEFAULT": rotation_opts,
conductor_group: backup_opts,
openstack_group: openstack_opts,
}
Empty file.
Loading