Merge "Select only necessary columns in allocation API"

This commit is contained in:
Zuul 2019-09-13 11:01:35 +00:00 committed by Gerrit Code Review
commit 8663d58d1f
4 changed files with 55 additions and 88 deletions

View File

@ -100,8 +100,14 @@ def get_reservation_allocations_by_host_ids(host_ids, start_date, end_date,
session = get_session() session = get_session()
border0 = start_date <= models.Lease.end_date border0 = start_date <= models.Lease.end_date
border1 = models.Lease.start_date <= end_date border1 = models.Lease.start_date <= end_date
query = (session.query(models.Reservation, models.ComputeHostAllocation) query = (session.query(models.Reservation.id,
.join(models.Lease).join(models.ComputeHostAllocation) models.Reservation.lease_id,
models.ComputeHostAllocation.compute_host_id)
.join(models.Lease,
models.Lease.id == models.Reservation.lease_id)
.join(models.ComputeHostAllocation,
models.ComputeHostAllocation.reservation_id ==
models.Reservation.id)
.filter(models.ComputeHostAllocation.compute_host_id .filter(models.ComputeHostAllocation.compute_host_id
.in_(host_ids)) .in_(host_ids))
.filter(sa.and_(border0, border1))) .filter(sa.and_(border0, border1)))

View File

@ -14,6 +14,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import collections
import datetime import datetime
from novaclient import exceptions as nova_exceptions from novaclient import exceptions as nova_exceptions
@ -541,21 +542,12 @@ class PhysicalHostPlugin(base.BasePlugin, nova.NovaClientWrapper):
# To reduce overhead, this method only executes one query # To reduce overhead, this method only executes one query
# to get the allocation information # to get the allocation information
allocs = db_utils.get_reservation_allocations_by_host_ids( rsv_lease_host = db_utils.get_reservation_allocations_by_host_ids(
hosts, start, end, lease_id, reservation_id) hosts, start, end, lease_id, reservation_id)
hosts_allocs = {} hosts_allocs = collections.defaultdict(list)
for reservation, alloc in allocs: for rsv, lease, host in rsv_lease_host:
if alloc['compute_host_id'] in hosts_allocs: hosts_allocs[host].append({'lease_id': lease, 'id': rsv})
hosts_allocs[alloc['compute_host_id']].append({
'lease_id': reservation['lease_id'],
'id': reservation['id']
})
else:
hosts_allocs[alloc['compute_host_id']] = [{
'lease_id': reservation['lease_id'],
'id': reservation['id']
}]
return hosts_allocs return hosts_allocs
def _matching_hosts(self, hypervisor_properties, resource_properties, def _matching_hosts(self, hypervisor_properties, resource_properties,

View File

@ -407,73 +407,63 @@ class SQLAlchemyDBUtilsTestCase(tests.DBTestCase):
self.check_reservation([], ['r4'], self.check_reservation([], ['r4'],
'2030-01-01 07:00', '2030-01-01 15:00') '2030-01-01 07:00', '2030-01-01 15:00')
def test_get_reservation_allocations_by_host_ids(self): def _create_allocation_tuple(self, lease_id):
def create_allocation_tuple(lease_id): reservation = db_api.reservation_get_all_by_lease_id(lease_id)[0]
reservation = db_api.reservation_get_all_by_lease_id(lease_id)[0] allocation = db_api.host_allocation_get_all_by_values(
allocation = db_api.host_allocation_get_all_by_values( reservation_id=reservation['id'])[0]
reservation_id=reservation['id'])[0] return (reservation['id'],
return (reservation['id'], allocation['id']) reservation['lease_id'],
allocation['compute_host_id'])
def test_get_reservation_allocations_by_host_ids(self):
self._setup_leases() self._setup_leases()
# query all allocations of lease1, lease2 and lease3 # query all allocations of lease1, lease2 and lease3
expected = [ expected = [
create_allocation_tuple('lease1'), self._create_allocation_tuple('lease1'),
create_allocation_tuple('lease2'), self._create_allocation_tuple('lease2'),
create_allocation_tuple('lease3'), self._create_allocation_tuple('lease3'),
] ]
ret = db_utils.get_reservation_allocations_by_host_ids( ret = db_utils.get_reservation_allocations_by_host_ids(
['r1', 'r2'], '2030-01-01 08:00', '2030-01-01 15:00') ['r1', 'r2'], '2030-01-01 08:00', '2030-01-01 15:00')
self.assertListEqual(expected, [(r['id'], a['id']) for r, a in ret]) self.assertListEqual(expected, ret)
# query allocations of lease2 and lease3 # query allocations of lease2 and lease3
expected = [ expected = [
create_allocation_tuple('lease2'), self._create_allocation_tuple('lease2'),
create_allocation_tuple('lease3'), self._create_allocation_tuple('lease3'),
] ]
ret = db_utils.get_reservation_allocations_by_host_ids( ret = db_utils.get_reservation_allocations_by_host_ids(
['r1', 'r2'], '2030-01-01 11:30', '2030-01-01 15:00') ['r1', 'r2'], '2030-01-01 11:30', '2030-01-01 15:00')
self.assertListEqual(expected, [(r['id'], a['id']) for r, a in ret]) self.assertListEqual(expected, ret)
def test_get_reservation_allocations_by_host_ids_with_lease_id(self): def test_get_reservation_allocations_by_host_ids_with_lease_id(self):
def create_allocation_tuple(lease_id):
reservation = db_api.reservation_get_all_by_lease_id(lease_id)[0]
allocation = db_api.host_allocation_get_all_by_values(
reservation_id=reservation['id'])[0]
return (reservation['id'], allocation['id'])
self._setup_leases() self._setup_leases()
# query all allocations of lease1, lease2 and lease3 # query all allocations of lease1, lease2 and lease3
expected = [ expected = [
create_allocation_tuple('lease1'), self._create_allocation_tuple('lease1'),
] ]
ret = db_utils.get_reservation_allocations_by_host_ids( ret = db_utils.get_reservation_allocations_by_host_ids(
['r1', 'r2'], '2030-01-01 08:00', '2030-01-01 15:00', 'lease1') ['r1', 'r2'], '2030-01-01 08:00', '2030-01-01 15:00', 'lease1')
self.assertListEqual(expected, [(r['id'], a['id']) for r, a in ret]) self.assertListEqual(expected, ret)
def test_get_reservation_allocations_by_host_ids_with_reservation_id(self): def test_get_reservation_allocations_by_host_ids_with_reservation_id(self):
def create_allocation_tuple(lease_id):
reservation = db_api.reservation_get_all_by_lease_id(lease_id)[0]
allocation = db_api.host_allocation_get_all_by_values(
reservation_id=reservation['id'])[0]
return (reservation['id'], allocation['id'])
self._setup_leases() self._setup_leases()
reservation1 = db_api.reservation_get_all_by_lease_id('lease1')[0] reservation1 = db_api.reservation_get_all_by_lease_id('lease1')[0]
# query allocations of lease1 # query allocations of lease1
expected = [ expected = [
create_allocation_tuple('lease1'), self._create_allocation_tuple('lease1'),
] ]
ret = db_utils.get_reservation_allocations_by_host_ids( ret = db_utils.get_reservation_allocations_by_host_ids(
['r1', 'r2'], '2030-01-01 08:00', '2030-01-01 15:00', ['r1', 'r2'], '2030-01-01 08:00', '2030-01-01 15:00',
reservation_id=reservation1['id']) reservation_id=reservation1['id'])
self.assertListEqual(expected, [(r['id'], a['id']) for r, a in ret]) self.assertListEqual(expected, ret)
def test_get_plugin_reservation_with_host(self): def test_get_plugin_reservation_with_host(self):
patch_host_reservation_get = self.patch(db_api, 'host_reservation_get') patch_host_reservation_get = self.patch(db_api, 'host_reservation_get')

View File

@ -430,19 +430,16 @@ class PhysicalHostPluginTestCase(tests.TestCase):
self.fake_host_id) self.fake_host_id)
def test_list_allocations(self): def test_list_allocations(self):
def reservation_allocation_tuple(r_id, l_id, h_id):
return ({'id': r_id, 'lease_id': l_id}, {'compute_host_id': h_id})
self.db_get_reserv_allocs = self.patch( self.db_get_reserv_allocs = self.patch(
self.db_utils, 'get_reservation_allocations_by_host_ids') self.db_utils, 'get_reservation_allocations_by_host_ids')
# Expecting a list of (Reservation, Allocation) # Expecting a list of (Reservation, Allocation)
self.db_get_reserv_allocs.return_value = [ self.db_get_reserv_allocs.return_value = [
reservation_allocation_tuple('reservation-1', 'lease-1', 'host-1'), ('reservation-1', 'lease-1', 'host-1'),
reservation_allocation_tuple('reservation-1', 'lease-1', 'host-2'), ('reservation-1', 'lease-1', 'host-2'),
reservation_allocation_tuple('reservation-2', 'lease-1', 'host-2'), ('reservation-2', 'lease-1', 'host-2'),
reservation_allocation_tuple('reservation-2', 'lease-1', 'host-3'), ('reservation-2', 'lease-1', 'host-3'),
reservation_allocation_tuple('reservation-3', 'lease-2', 'host-1'), ('reservation-3', 'lease-2', 'host-1'),
] ]
expected = [ expected = [
@ -477,18 +474,15 @@ class PhysicalHostPluginTestCase(tests.TestCase):
self.assertListEqual(expected, ret) self.assertListEqual(expected, ret)
def test_list_allocations_with_lease_id(self): def test_list_allocations_with_lease_id(self):
def reservation_allocation_tuple(r_id, l_id, h_id):
return ({'id': r_id, 'lease_id': l_id}, {'compute_host_id': h_id})
self.db_get_reserv_allocs = self.patch( self.db_get_reserv_allocs = self.patch(
self.db_utils, 'get_reservation_allocations_by_host_ids') self.db_utils, 'get_reservation_allocations_by_host_ids')
# Expecting a list of (Reservation, Allocation) # Expecting a list of (Reservation, Allocation)
self.db_get_reserv_allocs.return_value = [ self.db_get_reserv_allocs.return_value = [
reservation_allocation_tuple('reservation-1', 'lease-1', 'host-1'), ('reservation-1', 'lease-1', 'host-1'),
reservation_allocation_tuple('reservation-1', 'lease-1', 'host-2'), ('reservation-1', 'lease-1', 'host-2'),
reservation_allocation_tuple('reservation-2', 'lease-1', 'host-2'), ('reservation-2', 'lease-1', 'host-2'),
reservation_allocation_tuple('reservation-2', 'lease-1', 'host-3'), ('reservation-2', 'lease-1', 'host-3'),
] ]
expected = [ expected = [
@ -522,16 +516,13 @@ class PhysicalHostPluginTestCase(tests.TestCase):
self.assertListEqual(expected, ret) self.assertListEqual(expected, ret)
def test_list_allocations_with_reservation_id(self): def test_list_allocations_with_reservation_id(self):
def reservation_allocation_tuple(r_id, l_id, h_id):
return ({'id': r_id, 'lease_id': l_id}, {'compute_host_id': h_id})
self.db_get_reserv_allocs = self.patch( self.db_get_reserv_allocs = self.patch(
self.db_utils, 'get_reservation_allocations_by_host_ids') self.db_utils, 'get_reservation_allocations_by_host_ids')
# Expecting a list of (Reservation, Allocation) # Expecting a list of (Reservation, Allocation)
self.db_get_reserv_allocs.return_value = [ self.db_get_reserv_allocs.return_value = [
reservation_allocation_tuple('reservation-1', 'lease-1', 'host-1'), ('reservation-1', 'lease-1', 'host-1'),
reservation_allocation_tuple('reservation-1', 'lease-1', 'host-2'), ('reservation-1', 'lease-1', 'host-2'),
] ]
expected = [ expected = [
@ -559,19 +550,16 @@ class PhysicalHostPluginTestCase(tests.TestCase):
self.assertListEqual(expected, ret) self.assertListEqual(expected, ret)
def test_get_allocations(self): def test_get_allocations(self):
def reservation_allocation_tuple(r_id, l_id, h_id):
return ({'id': r_id, 'lease_id': l_id}, {'compute_host_id': h_id})
self.db_get_reserv_allocs = self.patch( self.db_get_reserv_allocs = self.patch(
self.db_utils, 'get_reservation_allocations_by_host_ids') self.db_utils, 'get_reservation_allocations_by_host_ids')
# Expecting a list of (Reservation, Allocation) # Expecting a list of (Reservation, Allocation)
self.db_get_reserv_allocs.return_value = [ self.db_get_reserv_allocs.return_value = [
reservation_allocation_tuple('reservation-1', 'lease-1', 'host-1'), ('reservation-1', 'lease-1', 'host-1'),
reservation_allocation_tuple('reservation-1', 'lease-1', 'host-2'), ('reservation-1', 'lease-1', 'host-2'),
reservation_allocation_tuple('reservation-2', 'lease-1', 'host-2'), ('reservation-2', 'lease-1', 'host-2'),
reservation_allocation_tuple('reservation-2', 'lease-1', 'host-3'), ('reservation-2', 'lease-1', 'host-3'),
reservation_allocation_tuple('reservation-3', 'lease-2', 'host-1'), ('reservation-3', 'lease-2', 'host-1'),
] ]
expected = { expected = {
@ -589,15 +577,12 @@ class PhysicalHostPluginTestCase(tests.TestCase):
self.assertDictEqual(expected, ret) self.assertDictEqual(expected, ret)
def test_get_allocations_with_lease_id(self): def test_get_allocations_with_lease_id(self):
def reservation_allocation_tuple(r_id, l_id, h_id):
return ({'id': r_id, 'lease_id': l_id}, {'compute_host_id': h_id})
self.db_get_reserv_allocs = self.patch( self.db_get_reserv_allocs = self.patch(
self.db_utils, 'get_reservation_allocations_by_host_ids') self.db_utils, 'get_reservation_allocations_by_host_ids')
# Expecting a list of (Reservation, Allocation) # Expecting a list of (Reservation, Allocation)
self.db_get_reserv_allocs.return_value = [ self.db_get_reserv_allocs.return_value = [
reservation_allocation_tuple('reservation-1', 'lease-1', 'host-1'), ('reservation-1', 'lease-1', 'host-1'),
] ]
expected = { expected = {
@ -615,15 +600,12 @@ class PhysicalHostPluginTestCase(tests.TestCase):
self.assertDictEqual(expected, ret) self.assertDictEqual(expected, ret)
def test_get_allocations_with_reservation_id(self): def test_get_allocations_with_reservation_id(self):
def reservation_allocation_tuple(r_id, l_id, h_id):
return ({'id': r_id, 'lease_id': l_id}, {'compute_host_id': h_id})
self.db_get_reserv_allocs = self.patch( self.db_get_reserv_allocs = self.patch(
self.db_utils, 'get_reservation_allocations_by_host_ids') self.db_utils, 'get_reservation_allocations_by_host_ids')
# Expecting a list of (Reservation, Allocation) # Expecting a list of (Reservation, Allocation)
self.db_get_reserv_allocs.return_value = [ self.db_get_reserv_allocs.return_value = [
reservation_allocation_tuple('reservation-1', 'lease-1', 'host-1'), ('reservation-1', 'lease-1', 'host-1'),
] ]
expected = { expected = {
@ -641,19 +623,16 @@ class PhysicalHostPluginTestCase(tests.TestCase):
self.assertDictEqual(expected, ret) self.assertDictEqual(expected, ret)
def test_get_allocations_with_invalid_host(self): def test_get_allocations_with_invalid_host(self):
def reservation_allocation_tuple(r_id, l_id, h_id):
return ({'id': r_id, 'lease_id': l_id}, {'compute_host_id': h_id})
self.db_get_reserv_allocs = self.patch( self.db_get_reserv_allocs = self.patch(
self.db_utils, 'get_reservation_allocations_by_host_ids') self.db_utils, 'get_reservation_allocations_by_host_ids')
# Expecting a list of (Reservation, Allocation) # Expecting a list of (Reservation, Allocation)
self.db_get_reserv_allocs.return_value = [ self.db_get_reserv_allocs.return_value = [
reservation_allocation_tuple('reservation-1', 'lease-1', 'host-1'), ('reservation-1', 'lease-1', 'host-1'),
reservation_allocation_tuple('reservation-1', 'lease-1', 'host-2'), ('reservation-1', 'lease-1', 'host-2'),
reservation_allocation_tuple('reservation-2', 'lease-1', 'host-2'), ('reservation-2', 'lease-1', 'host-2'),
reservation_allocation_tuple('reservation-2', 'lease-1', 'host-3'), ('reservation-2', 'lease-1', 'host-3'),
reservation_allocation_tuple('reservation-3', 'lease-2', 'host-1'), ('reservation-3', 'lease-2', 'host-1'),
] ]
expected = {'resource_id': 'no-reserved-host', 'reservations': []} expected = {'resource_id': 'no-reserved-host', 'reservations': []}
ret = self.fake_phys_plugin.get_allocations('no-reserved-host', {}) ret = self.fake_phys_plugin.get_allocations('no-reserved-host', {})