Create and use subclass of sqlalchemy Query with soft_delete() method

Create subclass of sqlalchemy.orm.query.Query that has soft_delete() method

Change get_maker() method to create Session with our Query class

The main idea of soft_delete() method is to get good performance with clean
syntax: Let we have already `query` instance.

Clean syntax example:
If we want to make soft delete for each entry that correspond to query:
we should write:
        query.soft_delete()
instead of:
        query.update({
                deleted:True,
                'deleted_at': timeutils.utcnow(),
                'updated_at': literal_column('updated_at')})

Performance examples:
If we make:
        query.soft_delete()
we will make only one request to soft delete corresponding entries.
But if we make:
        for model_ref in query.all():
                model_ref.delete(session=session)
it will produce >= N + 1 queries.

In almost all situation it is better to use query.soft_delete() than
model.delete() method, because in second approach we should make
2 request (get model_ref and then soft delete it).

In future patch model.delete() will be at least renamed to model.soft_delete().
Probably it should be removed at all, to avoid situation in code with N+1
requests for soft deletion.

blueprint db-session-cleanup

Change-Id: I6059982b83648133b6d8becf94ad8980b056c6ff
Using query.soft_delete() method insead of soft deleting by hand

Change all occurences of:
	query.update({
		deleted:True,
		'deleted_at': timeutils.utcnow(),
                'updated_at': literal_column('updated_at')})
to:
	query.soft_delete()

Make instance_type_access_remove() with 2 db requests instead of 3

Make agent_build_destroy() with 1 db request instead of 2

Fix bug in aggregate_delete() added missing transaction
make it with 2 db requests instead of 3

Make aggregate_metadata_delete() with 1 db request instead of 2

Make aggregate_host_delete() with 1 db request instead of 2

blueprint db-session-cleanup

Change-Id: I23a85c48474126743c5d02f642baf6047358aae8
Database reservations methods performance optimization

In reservation_commit():
Was:
*) 1 request to get all (N) reservations,
*) 1 requrest to get all (M) usages,
*) N query to update all reservations
*) M query to update all usages
Now there is only 1 db request to soft delete N reservations.
result: 1 + 1 + N + M -> 1 + 1 + 1 + M
remove usages save loop, because it will be automaticly make sqlalchemy
session in transaction.__exit__() hook.

In reservation_rollback():
Same as in reservation_commit()

In quota_destroy_all_by_project():
Was:
*) 1 request to get all (N) Quotas
*) N request to soft delete all Quotas
*) 1 request to get all (M) QuotaUsage
*) M request to soft delete all QuotaUsages
*) 1 request to get all (K) Reservations
*) K request to soft delete Reservations
Now there are only 3 requests.
result: 3 + N + K -> 3

In reservation_expire():
Was:
*) 1 request to get all (N) reservations
*) N request to get each usage for reservation
*) N request to update each usage
*) N request to soft delete each reservations
Now:
*) 1 request to get all (N) reservations with usages
*) N request to save each reservation
*) 1 request to soft delete N reservations
result: 1 + N + N + N -> 2 + N

blueprint db-session-cleanup

Change-Id: If6ddbe552b8f562cf6e8649eab96a375c4875487
Merge "Create and use subclass of sqlalchemy Query with soft_delete() method"
Merge "Using query.soft_delete() method insead of soft deleting by hand"
Merge "Database reservations methods performance optimization"