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

View File

@ -1,7 +1,6 @@
# Copyright 2017 Huawei Technologies Co.,LTD.
# All Rights Reserved.
#
#
# 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
# 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,
recursive=recursive)
if fields is None or 'metadata' in fields:
self.orig_metadata = (dict(self.metadata)
if self.obj_attr_is_set('metadata')
else {})
self._orig_metadata = (dict(self.metadata)
if self.obj_attr_is_set('metadata')
else {})
def obj_what_changed(self):
changes = super(Aggregate, self).obj_what_changed()