Skip to content

Commit

Permalink
Support setting aggregates on the created providers
Browse files Browse the repository at this point in the history
Each rp is a member of 1, 2, or 3 of 3 aggregates.

/cc @2uasimojo @jaypipes so they're aware
  • Loading branch information
cdent committed Aug 12, 2018
1 parent 0cb2106 commit 0c07d42
Showing 1 changed file with 58 additions and 9 deletions.
67 changes: 58 additions & 9 deletions placeload/__init__.py
@@ -1,6 +1,6 @@

import sys
import json
import itertools
from urllib.parse import urljoin, urlsplit
import uuid

Expand All @@ -17,6 +17,20 @@
'x-auth-token': 'admin',
}


AGGREGATES = [
'14a5c8a3-5a99-4e8f-88be-00d85fcb1c17',
'66d98e7c-3c25-485d-a0dc-1cea651884de',
'a59dbb28-fd98-4c6e-9ec5-ae5f3d04b0aa',
]
AGG_CHUNKS = [
AGGREGATES[0:1],
AGGREGATES[0:2],
AGGREGATES[0:3],
]
AGG_CYCLE = itertools.cycle(AGG_CHUNKS)


# For now we default to allocation_ratio always being 1 because
# we don't want to think.
# Simple homogeneous topology for now.
Expand Down Expand Up @@ -47,7 +61,8 @@ class LoaderException(Exception):
async def version(session, url):
async with session.get(url) as resp:
if resp.status != 200:
raise LoaderException('Unable to reach %s: %d' % (url, resp.status))
raise LoaderException(
'Unable to reach %s: %d' % (url, resp.status))
data = await resp.json()
version = data['versions'][0]['max_version']
print(f'Placement is {version}')
Expand All @@ -58,6 +73,29 @@ async def verify(service):
await version(session, service)


async def _set_agg(session, url):
"""Set aggregates.
Each resource provider is a member of one, two or three
aggregates.
"""
# you can call __next__ on a cycle, but not next(). *shrug*
aggregates = AGG_CYCLE.__next__()
data = {
'resource_provider_generation': 1,
'aggregates': aggregates,
}
try:
async with session.put(url, json=data) as resp:
if resp.status == 200:
print('a', end='', flush=True)
else:
uu = urlsplit(url).path.rsplit('/')[-2]
print(f'X{resp.status}, {uu}', flush=True)
except aiohttp.client_exceptions.ClientError as exc:
print(f'C{url}...{exc}')


async def _set_inv(session, url):
"""Set inventory.
Expand All @@ -71,6 +109,10 @@ async def _set_inv(session, url):
async with session.put(url, json=data) as resp:
if resp.status == 200:
print('i', end='', flush=True)
agg_url = url.replace('inventories', 'aggregates')
async with aiohttp.ClientSession(
headers=DEFAULT_HEADERS) as isession:
await _set_agg(isession, agg_url)
else:
uu = urlsplit(url).path.rsplit('/')[-2]
print(f'X{resp.status}, {uu}', flush=True)
Expand All @@ -93,7 +135,8 @@ async def _create_rp(session, url, uu):
print('r', end='', flush=True)
inv_url = urljoin(f'{url}/', f'{uu}/inventories')
# we need a different session otherwise the one we had closes
async with aiohttp.ClientSession(headers=DEFAULT_HEADERS) as isession:
async with aiohttp.ClientSession(
headers=DEFAULT_HEADERS) as isession:
await _set_inv(isession, inv_url)
else:
print(f'X{resp.status}, {uu}', end='', flush=True)
Expand All @@ -112,11 +155,11 @@ async def create_rp(service, uu, semaphore):
await _create_rp(session, url, uu)


async def create(service, count=500):
async def create(service, count):
"""Create 500 separate resource providers with inventory."""
tasks = []
uuids = []
semaphore = asyncio.Semaphore(count//5)
semaphore = asyncio.Semaphore(count//min(5, count))
while count:
uuids.append(str(uuid.uuid4()))
count -= 1
Expand All @@ -125,7 +168,7 @@ async def create(service, count=500):
await asyncio.gather(*tasks)


def start(service):
def start(service, count):
loop = asyncio.get_event_loop()

# Confirm the presence of a working placement service at the URL
Expand All @@ -137,10 +180,16 @@ def start(service):
sys.exit(1)

# Create some resource providers with inventory described in
# INVENTORY_DICT.
loop.run_until_complete(create(service))
# INVENTORY_DICT and aggregates from AGGREGATES.
loop.run_until_complete(create(service, count))
# output the aggregates so we can query for them easily
print()
print('\n'.join(AGGREGATES))


if __name__ == '__main__':
service = sys.argv[1]
start(service)
count = 500
if len(sys.argv) == 3:
count = int(sys.argv[2])
start(service, count)

0 comments on commit 0c07d42

Please sign in to comment.