Using Indexes instead of checking duplicating entities by hand.
It would be good to use DB Indexes instead of checking duplicating entities by hand in sqlalchemy DB API.
For example code in nova/db/
@require_
def instance_
"""Create a new instance type. In order to pass in extra specs,
the values dict should contain a 'extra_specs' key/value pair:
{'extra_specs' : {'k1': 'v1', 'k2': 'v2', ...}}
"""
session = get_session()
with session.begin():
try:
raise exception.
except exception.
pass
try:
raise exception.
except exception.
pass
try:
specs = values.
if specs:
for k, v in specs.iteritems():
except Exception, e:
raise exception.
return _dict_with_
So before we can create new instance_type in DB we must check that instance type with the same name or flavor_id doesn't exist in DB. So we make 3 requests to DB instead of 1. I think it is a not good approach.
Also there are other entities that have create and update methods in API, so we should make such checks in both methods. So we can easily make mistake. For example I've found such code in sqlalchemy DB API:
@require_
def sm_backend_
session = get_session()
with session.begin():
if backend_conf:
raise exception.
else:
return backend_conf
@require_
def sm_backend_
session = get_session()
with session.begin():
if not backend_conf:
raise exception.NotFound(
return backend_conf
In function sm_backend_
It is not so easy to use indexes, because the entities are not physically deleted from DB (they are only marked as "deleted").
The best way that I've found is to use composite unique indexes like this one: (column1, column2, ...,columnN, deleted_at).
So if an entity is `deleted` its `deleted_at` column is set to some date , so we can create new entity with same values of columns (column1, column2,
Unfortunately it doesn't work, because in mysql NULL is not equal to NULL.
To fix this issue we can set default value of `deleted_at` column to, for example, '1970-1-1'. This will not break existing code, because `deleted_at` column isn't used to check the deletion status of an entity . ('deleted' column is used instead)
So I propose a better version of instance_
First of all we should add UniqueConstraint for name and flavorid in existing instance_types table.
instance_types = Table('
)
And then our function will look like this:
@require_
def instance_
session = get_session()
with session.begin():
try:
specs = values.
if specs:
for k, v in specs.iteritems():
except exception.
raise exception.
except Exception, e:
raise exception.
return _dict_with_
Question information
- Language:
- English Edit question
- Status:
- Answered
- Assignee:
- No assignee Edit question
- Last query:
- Last reply:
Can you help with this problem?
Provide an answer of your own, or ask Boris Pavlovic for more information if necessary.