From d44a210a507c731b66c87cfdf1987716df37d0e3 Mon Sep 17 00:00:00 2001 From: Ruby Loo Date: Tue, 5 Sep 2017 14:34:46 -0400 Subject: [PATCH] Fix race condition in backfill_version_column() Fixes a race condition in backfill_version_column(). It was fetching the objects whose version==None. Then it did an update to set the value of those object versions. However, it is possible for one of the object versionss to have been updated after the fetch but before the update operation, in which case the update operation might set the version to be an older version. Change-Id: I882fdd3e83582a4d7110c68b0d84f243234ea7dd Closes-Bug: #1715190 --- ironic/db/sqlalchemy/api.py | 17 ++++++++++++----- ...lumn_db_race_condition-713fa05832b93ca5.yaml | 7 +++++++ 2 files changed, 19 insertions(+), 5 deletions(-) create mode 100644 releasenotes/notes/backfill_version_column_db_race_condition-713fa05832b93ca5.yaml diff --git a/ironic/db/sqlalchemy/api.py b/ironic/db/sqlalchemy/api.py index d4b8f868d9..c2c39f0ccd 100644 --- a/ironic/db/sqlalchemy/api.py +++ b/ironic/db/sqlalchemy/api.py @@ -1218,11 +1218,18 @@ class Connection(api.Connection): ids = [] for obj in query.slice(0, max_to_migrate): ids.append(obj['id']) - query = model_query(model).filter(model.id.in_(ids)) - - num_migrated = query.update( - {model.version: mapping[model.__name__][0]}, - synchronize_session=False) + num_migrated = ( + model_query(model). + filter(sql.and_(model.id.in_(ids), + model.version.is_(None))). + update({model.version: mapping[model.__name__][0]}, + synchronize_session=False)) + else: + num_migrated = ( + model_query(model). + filter(model.version.is_(None)). + update({model.version: mapping[model.__name__][0]}, + synchronize_session=False)) total_migrated += num_migrated max_to_migrate -= num_migrated diff --git a/releasenotes/notes/backfill_version_column_db_race_condition-713fa05832b93ca5.yaml b/releasenotes/notes/backfill_version_column_db_race_condition-713fa05832b93ca5.yaml new file mode 100644 index 0000000000..febe06281f --- /dev/null +++ b/releasenotes/notes/backfill_version_column_db_race_condition-713fa05832b93ca5.yaml @@ -0,0 +1,7 @@ +--- +fixes: + - | + Fixes an issue when running ``ironic-dbsync online_data_migrations``. The + value of an object's new ``version`` column might have been incorrectly + changed from a newer object version to an older object version, due to a + race condition. This is no longer the case.