cinder/cinder
Gorka Eguileor 95fcb50a42 Fix quota usage duplicate entries
Our current quota system has a race condition on reservations that only
happens when we are creating new entries in the quota_usages table.

We normally lock quota_usages rows using a SELECT ... FOR UPDATE query,
but that's only effective when the entries exist, and current code just
creates them and proceeds without a lock on them.

This, together with the table not having unique constraint means that we
can get duplicated entries and we can have one entry overwriting the
data written by another request.

The quota_usages table does soft deletes, so the project_id and resource
fields are not enough for a unique constraint, so we add a new column
called race_preventer so we can have a unique constraint with the
3 fields.

Additionally we have to make sure that we acquire the locks before doing
the reservation calculations or the syncs, so once we create any missing
entries we close the session and try to get the locks again.

With these 2 changes we'll avoid duplicated entries as well as avoid
getting our quota usage out of sync right from the start.

For the unique constraint part of the code there were 2 alternatives
(one was even used in an earlier patchset):

- Create a virtual/computed column for the Table that sets it to a fixed
  value when deleted_at is NULL and to NULL in any other case, then use
  this virtual/computed column together with project_id and resource
  fields for a unique constraint.

  This change was my preferred solution, but it requires bumping the
  SQLAlchemy version to 1.3.11 where the feature was added as computed
  columns [1] and in some DB engines requires a relatively new version,
  for example for PostgreSQL is only supported on version 12 or later.

- Set deleted_at to a non NULL value by default on creation, and make
  sure our code always uses the deleted field to filter values.

  This is a bit nasty, but it has the advantage of not requiring new DB
  fields, no DB data migrations for existing entries, and easy to
  rollback once we figure out the underlying issue (although it may
  require a DB data migration on rollback if we want to leave the
  deleted_at entry at NULL).

The decision to add a new field was because one of the alternatives is
kind of hacky and the other one depends on specific DBMS versions and
requires a SQLAlchemy version bump.

[1]: https://docs.sqlalchemy.org/en/13/core/defaults.html#computed-generated-always-as-columns

Closes-Bug: #1484343
Change-Id: I9000c16c5b3e6f313f02256a10cb4bc0a26379f7
(cherry picked from commit 1fb0767d88)
2021-04-05 06:21:23 +00:00
..
api Remove nested quota leftovers 2021-04-05 06:19:07 +00:00
backup Merge "Backup manager: Synchronously call remove_export" 2021-03-25 17:00:42 +00:00
brick LVM: fix _create_vg 2021-02-11 13:55:50 -05:00
cmd Add quota utils to cinder-manage 2021-04-01 12:08:13 +02:00
common Support mTLS when calling the glance API 2021-03-22 22:00:44 +00:00
compute nova: use EndpointNotFound from keystoneauth1 2019-09-03 10:58:59 -04:00
db Fix quota usage duplicate entries 2021-04-05 06:21:23 +00:00
group Use resource_backend for volumes and groups 2020-08-14 08:13:42 +00:00
image Support mTLS when calling the glance API 2021-03-22 22:00:44 +00:00
interface Add explanations on safe delete 2021-03-17 14:04:20 +01:00
keymgr Introduce flake8-import-order extension 2020-01-06 09:59:35 -06:00
locale Imported Translations from Zanata 2021-03-24 06:25:01 +00:00
message Add user messages for some volume snapshot actions 2019-04-26 17:02:05 -04:00
objects Fix volume OVO create method 2021-03-17 13:07:09 +01:00
policies Simplify composite check strings for project personas 2021-02-17 17:44:40 +00:00
privsep Enable flake8-logging-format extension 2020-01-09 14:35:20 -06:00
scheduler Remove six of dir cinder/scheduler/* 2020-10-08 08:36:17 +08:00
tests Fix quota usage duplicate entries 2021-04-05 06:21:23 +00:00
transfer Fix: show volume transfer by name for non-admins 2020-08-03 12:46:31 +00:00
volume Change snapshots type with volume retype 2021-04-01 12:08:03 +02:00
wsgi Introduce flake8-import-order extension 2020-01-06 09:59:35 -06:00
zonemanager Brocade: Fix lookup UnboundLocalError 2020-08-07 15:24:44 +02:00
__init__.py
context.py mypy: annotate volume manager 2021-02-10 12:27:47 -05:00
coordination.py
exception.py Merge "Remove NestedQuotaDriver" 2021-02-16 16:26:03 +00:00
flow_utils.py
i18n.py
manager.py mypy: annotate volume manager 2021-02-10 12:27:47 -05:00
opts.py Merge "Update code layout and missing Zadara features" 2021-03-19 19:00:48 +00:00
policy.py Merge "Make sure we pass context objects directly to policy enforcement" 2021-03-07 00:07:42 +00:00
quota.py Remove nested quota leftovers 2021-04-05 06:19:07 +00:00
quota_utils.py Change snapshots type with volume retype 2021-04-01 12:08:03 +02:00
rpc.py Remove six in files under cinder/* 2020-10-08 14:00:14 +08:00
service.py Fix typo on service cluster change method 2020-05-06 19:36:07 -05:00
service_auth.py
ssh_utils.py Remove six in files under cinder/* 2020-10-08 14:00:14 +08:00
utils.py Move trace methods from utils to volume_utils 2021-02-12 20:16:55 +00:00
version.py