Merge "Fix aggregate metadata update/remove error"

This commit is contained in:
Jenkins 2017-08-07 19:54:52 +00:00 committed by Gerrit Code Review
commit e7c202cf7a
2 changed files with 31 additions and 31 deletions

View File

@ -841,38 +841,39 @@ class Connection(api.Connection):
def aggregate_metadata_update_or_create(self, context, aggregate_id, def aggregate_metadata_update_or_create(self, context, aggregate_id,
metadata, max_retries=10): metadata, max_retries=10):
for attempt in range(max_retries): for attempt in range(max_retries):
try: with _session_for_write() as session:
query = model_query( try:
context, models.AggregateMetadata).\ query = model_query(
filter_by(aggregate_id=aggregate_id).\ context, models.AggregateMetadata).\
filter(models.AggregateMetadata.key.in_( filter_by(aggregate_id=aggregate_id).\
metadata.keys())).with_lockmode('update').all() filter(models.AggregateMetadata.key.in_(
metadata.keys())).with_lockmode('update').all()
already_existing_keys = set() already_existing_keys = set()
for meta_ref in query: for meta_ref in query:
key = meta_ref["key"] key = meta_ref["key"]
meta_ref.update({"value": metadata[key]}) meta_ref.update({"value": metadata[key]})
already_existing_keys.add(key) already_existing_keys.add(key)
for key, value in metadata.items(): for key, value in metadata.items():
if key in already_existing_keys: if key in already_existing_keys:
continue continue
metadata_ref = models.AggregateMetadata() metadata_ref = models.AggregateMetadata()
metadata_ref.update({"key": key, metadata_ref.update({"key": key,
"value": value, "value": value,
"aggregate_id": aggregate_id}) "aggregate_id": aggregate_id})
with _session_for_write() as session:
session.add(metadata_ref) session.add(metadata_ref)
session.flush() session.flush()
return metadata return metadata
except db_exc.DBDuplicateEntry: except db_exc.DBDuplicateEntry:
# a concurrent transaction has been committed, # a concurrent transaction has been committed,
# try again unless this was the last attempt # try again unless this was the last attempt
if attempt < max_retries - 1: if attempt < max_retries - 1:
LOG.warning("Add metadata failed for aggregate %(id)s " LOG.warning("Add metadata failed for aggregate %(id)s "
"after %(retries)s retries", "after %(retries)s retries",
{"id": aggregate_id, "retries": max_retries}) {"id": aggregate_id,
"retries": max_retries})
def aggregate_metadata_get(self, context, aggregate_id): def aggregate_metadata_get(self, context, aggregate_id):
rows = model_query(context, models.AggregateMetadata). \ rows = model_query(context, models.AggregateMetadata). \

View File

@ -1,7 +1,6 @@
# Copyright 2017 Huawei Technologies Co.,LTD. # Copyright 2017 Huawei Technologies Co.,LTD.
# All Rights Reserved. # All Rights Reserved.
# #
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may # 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 # not use this file except in compliance with the License. You may obtain
# a copy of the License at # a copy of the License at
@ -54,9 +53,9 @@ class Aggregate(base.MoganObject, object_base.VersionedObjectDictCompat):
super(Aggregate, self).obj_reset_changes(fields=fields, super(Aggregate, self).obj_reset_changes(fields=fields,
recursive=recursive) recursive=recursive)
if fields is None or 'metadata' in fields: if fields is None or 'metadata' in fields:
self.orig_metadata = (dict(self.metadata) self._orig_metadata = (dict(self.metadata)
if self.obj_attr_is_set('metadata') if self.obj_attr_is_set('metadata')
else {}) else {})
def obj_what_changed(self): def obj_what_changed(self):
changes = super(Aggregate, self).obj_what_changed() changes = super(Aggregate, self).obj_what_changed()