-
-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multiops #295
Merged
Multiops #295
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
af886eb
added POST req for attributes
jreadey e332944
added support for multiple attribute put
jreadey 8ad52f9
updates based on review comments
jreadey e95203c
add putAttributes servicenode_lib func
jreadey efac2b5
support for attribute encoding
jreadey 8ca681c
enable multi-attribute delete
jreadey 2de031f
updates per review comments
jreadey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,326 @@ | ||
############################################################################## | ||
# Copyright by The HDF Group. # | ||
# All rights reserved. # | ||
# # | ||
# This file is part of HSDS (HDF5 Scalable Data Service), Libraries and # | ||
# Utilities. The full HSDS copyright notice, including # | ||
# terms governing use, modification, and redistribution, is contained in # | ||
# the file COPYING, which can be found at the root of the source code # | ||
# distribution tree. If you do not have access to this file, you may # | ||
# request a copy from [email protected]. # | ||
############################################################################## | ||
# | ||
# domain crawler | ||
# | ||
|
||
import asyncio | ||
|
||
from aiohttp.web_exceptions import HTTPServiceUnavailable, HTTPConflict, HTTPBadRequest | ||
from aiohttp.web_exceptions import HTTPInternalServerError, HTTPNotFound, HTTPGone | ||
|
||
|
||
from .util.idUtil import getCollectionForId, getDataNodeUrl | ||
|
||
from .servicenode_lib import getObjectJson, getAttributes, putAttributes | ||
from . import hsds_logger as log | ||
|
||
|
||
class DomainCrawler: | ||
def __init__( | ||
self, | ||
app, | ||
objs, | ||
action="get_obj", | ||
params=None, | ||
max_tasks=40, | ||
max_objects_limit=0, | ||
raise_error=False | ||
): | ||
log.info(f"DomainCrawler.__init__ root_id: {len(objs)} objs") | ||
log.debug(f"params: {params}") | ||
self._app = app | ||
self._action = action | ||
self._max_objects_limit = max_objects_limit | ||
self._params = params | ||
self._max_tasks = max_tasks | ||
self._q = asyncio.Queue() | ||
self._obj_dict = {} | ||
self.seen_ids = set() | ||
self._raise_error = raise_error | ||
if not objs: | ||
log.error("no objs for crawler to crawl!") | ||
raise ValueError() | ||
|
||
for obj_id in objs: | ||
log.debug(f"adding {obj_id} to the queue") | ||
self._q.put_nowait(obj_id) | ||
if isinstance(objs, dict): | ||
self._objs = objs | ||
else: | ||
self._objs = None | ||
|
||
async def get_attributes(self, obj_id, attr_names): | ||
# get the given attributes for the obj_id | ||
msg = f"get_attributes for {obj_id}" | ||
if attr_names: | ||
msg += f", {len(attr_names)} attributes" | ||
log.debug(msg) | ||
|
||
kwargs = {} | ||
for key in ("include_data", "ignore_nan", "bucket"): | ||
if key in self._params: | ||
kwargs[key] = self._params[key] | ||
if attr_names: | ||
kwargs["attr_names"] = attr_names | ||
log.debug(f"using kwargs: {kwargs}") | ||
|
||
status = 200 | ||
# make sure to catch all expected exceptions, otherwise | ||
# the task will never complete | ||
try: | ||
attributes = await getAttributes(self._app, obj_id, **kwargs) | ||
except HTTPBadRequest: | ||
status = 400 | ||
except HTTPNotFound: | ||
status = 404 | ||
except HTTPGone: | ||
status = 410 | ||
except HTTPServiceUnavailable: | ||
status = 503 | ||
except HTTPInternalServerError: | ||
status = 500 | ||
except Exception as e: | ||
log.error(f"unexpected exception from post request: {e}") | ||
status = 500 | ||
|
||
if status == 200: | ||
log.debug(f"got attributes: {attributes}") | ||
self._obj_dict[obj_id] = attributes | ||
else: | ||
log.warn(f"Domain crawler - got {status} status for obj_id {obj_id}") | ||
self._obj_dict[obj_id] = {"status": status} | ||
|
||
async def put_attributes(self, obj_id, attr_items): | ||
# write the given attributes for the obj_id | ||
log.debug(f"put_attributes for {obj_id}, {len(attr_items)} attributes") | ||
req = getDataNodeUrl(self._app, obj_id) | ||
collection = getCollectionForId(obj_id) | ||
req += f"/{collection}/{obj_id}/attributes" | ||
kwargs = {} | ||
if "bucket" in self._params: | ||
kwargs["bucket"] = self._params["bucket"] | ||
if "replace" in self._params: | ||
kwargs["replace"] = self._params["replace"] | ||
status = None | ||
try: | ||
status = await putAttributes(self._app, obj_id, attr_items, **kwargs) | ||
except HTTPConflict: | ||
log.warn("DomainCrawler - got HTTPConflict from http_put") | ||
status = 409 | ||
except HTTPServiceUnavailable: | ||
status = 503 | ||
except HTTPInternalServerError: | ||
status = 500 | ||
except Exception as e: | ||
log.error(f"unexpected exception {e}") | ||
|
||
log.debug(f"DomainCrawler fetch for {obj_id} - returning status: {status}") | ||
self._obj_dict[obj_id] = {"status": status} | ||
|
||
async def get_obj_json(self, obj_id): | ||
""" get the given obj_json for the obj_id. | ||
for each group found, search the links if include_links is set """ | ||
log.debug(f"get_obj_json: {obj_id}") | ||
collection = getCollectionForId(obj_id) | ||
kwargs = {} | ||
|
||
for k in ("include_links", "include_attrs", "bucket"): | ||
if k in self._params: | ||
kwargs[k] = self._params[k] | ||
if collection == "groups" and self._params.get("follow_links"): | ||
follow_links = True | ||
kwargs["include_links"] = True # get them so we can follow them | ||
else: | ||
follow_links = False | ||
if follow_links or self._params.get("include_attrs"): | ||
kwargs["refresh"] = True # don't want a cached version in this case | ||
|
||
log.debug(f"follow_links: {follow_links}") | ||
log.debug(f"getObjectJson kwargs: {kwargs}") | ||
obj_json = None | ||
status = 200 | ||
try: | ||
obj_json = await getObjectJson(self._app, obj_id, **kwargs) | ||
except HTTPNotFound: | ||
status = 404 | ||
except HTTPServiceUnavailable: | ||
status = 503 | ||
except HTTPInternalServerError: | ||
status = 500 | ||
except Exception as e: | ||
log.error(f"unexpected exception {e}") | ||
status = 500 | ||
log.debug(f"getObjectJson status: {status}") | ||
|
||
if obj_json is None: | ||
msg = f"DomainCrawler - getObjectJson for {obj_id} " | ||
if status >= 500: | ||
msg += f"failed, status: {status}" | ||
log.error(msg) | ||
else: | ||
msg += f"returned status: {status}" | ||
log.warn(msg) | ||
return | ||
|
||
log.debug(f"DomainCrawler - got json for {obj_id}") | ||
log.debug(f"obj_json: {obj_json}") | ||
|
||
log.debug("store obj json") | ||
self._obj_dict[obj_id] = obj_json # store the obj_json | ||
|
||
# for groups iterate through all the hard links and | ||
# add to the lookup ids set | ||
|
||
log.debug(f"gotCollection: {collection}") | ||
|
||
if collection == "groups" and follow_links: | ||
if "links" not in obj_json: | ||
log.error("expected links key in obj_json") | ||
return | ||
links = obj_json["links"] | ||
log.debug(f"DomainCrawler links: {links}") | ||
for title in links: | ||
log.debug(f"DomainCrawler - got link: {title}") | ||
link_obj = links[title] | ||
num_objects = len(self._obj_dict) | ||
if self._params.get("max_objects_limit") is not None: | ||
max_objects_limit = self._params["max_objects_limit"] | ||
if num_objects >= max_objects_limit: | ||
msg = "DomainCrawler reached limit of " | ||
msg += f"{max_objects_limit}" | ||
log.info(msg) | ||
break | ||
if link_obj["class"] != "H5L_TYPE_HARD": | ||
# just follow hardlinks | ||
continue | ||
link_id = link_obj["id"] | ||
if link_id not in self._obj_dict: | ||
# haven't seen this object yet, get obj json | ||
log.debug(f"DomainCrawler - adding link_id: {link_id}") | ||
self._obj_dict[link_id] = {} # placeholder for obj id | ||
self._q.put_nowait(link_id) | ||
|
||
def get_status(self): | ||
""" return the highest status of any of the returned objects """ | ||
status = None | ||
for obj_id in self._obj_dict: | ||
item = self._obj_dict[obj_id] | ||
log.debug(f"item: {item}") | ||
if "status" in item: | ||
item_status = item["status"] | ||
if status is None or item_status > status: | ||
# return the more severe error | ||
log.debug(f"setting status to {item_status}") | ||
status = item_status | ||
return status | ||
|
||
async def crawl(self): | ||
workers = [asyncio.Task(self.work()) for _ in range(self._max_tasks)] | ||
# When all work is done, exit. | ||
msg = "DomainCrawler - await queue.join - " | ||
msg += f"count: {len(self._obj_dict)}" | ||
log.info(msg) | ||
await self._q.join() | ||
msg = "DomainCrawler - join complete - " | ||
msg += f"count: {len(self._obj_dict)}" | ||
log.info(msg) | ||
|
||
for w in workers: | ||
w.cancel() | ||
log.debug("DomainCrawler - workers canceled") | ||
|
||
status = self.get_status() | ||
if status: | ||
log.debug(f"DomainCrawler -- status: {status}") | ||
log.debug(f"raise_error: {self._raise_error}") | ||
if self._raise_error: | ||
# throw the approriate exception if other than 200, 201 | ||
if status == 200: | ||
pass # ok | ||
elif status == 201: | ||
pass # also ok | ||
elif status == 400: | ||
log.warn("DomainCrawler - BadRequest") | ||
raise HTTPBadRequest(reason="unkown") | ||
elif status == 404: | ||
log.warn("DomainCrawler - not found") | ||
raise HTTPNotFound() | ||
elif status == 409: | ||
log.warn("DomainCrawler - conflict") | ||
raise HTTPConflict() | ||
elif status == 410: | ||
log.warn("DomainCrawler - gone") | ||
raise HTTPGone() | ||
elif status == 500: | ||
log.error("DomainCrawler - internal server error") | ||
raise HTTPInternalServerError() | ||
elif status == 503: | ||
log.error("DomainCrawler - server busy") | ||
raise HTTPServiceUnavailable() | ||
else: | ||
log.error(f"DomainCrawler - unexpected status: {status}") | ||
raise HTTPInternalServerError() | ||
|
||
async def work(self): | ||
while True: | ||
obj_id = await self._q.get() | ||
await self.fetch(obj_id) | ||
self._q.task_done() | ||
|
||
async def fetch(self, obj_id): | ||
log.debug(f"DomainCrawler fetch for id: {obj_id}") | ||
log.debug(f"action: {self._action}") | ||
if self._action == "get_obj": | ||
log.debug("DomainCrawler - get obj") | ||
# just get the obj json | ||
await self.get_obj_json(obj_id) | ||
elif self._action == "get_attr": | ||
log.debug("DomainCrawler - get attributes") | ||
# fetch the given attributes | ||
if self._objs is None: | ||
log.error("DomainCrawler - self._objs not set") | ||
return | ||
if obj_id not in self._objs: | ||
log.error(f"couldn't find {obj_id} in self._objs") | ||
return | ||
attr_names = self._objs[obj_id] | ||
if attr_names is None: | ||
log.debug(f"fetch all attributes for {obj_id}") | ||
else: | ||
if not isinstance(attr_names, list): | ||
log.error("expected list for attribute names") | ||
return | ||
if len(attr_names) == 0: | ||
log.warn("expected at least one name in attr_names list") | ||
return | ||
|
||
log.debug(f"DomainCrawler - got attribute names: {attr_names}") | ||
await self.get_attributes(obj_id, attr_names) | ||
elif self._action == "put_attr": | ||
log.debug("DomainCrawler - put attributes") | ||
# write attributes | ||
if self._objs and obj_id not in self._objs: | ||
log.error(f"couldn't find {obj_id} in self._objs") | ||
return | ||
attr_items = self._objs[obj_id] | ||
log.debug(f"got {len(attr_items)} attr_items") | ||
|
||
await self.put_attributes(obj_id, attr_items) | ||
else: | ||
msg = f"DomainCrawler: unexpected action: {self._action}" | ||
log.error(msg) | ||
|
||
msg = f"DomainCrawler - fetch complete obj_id: {obj_id}, " | ||
msg += f"{len(self._obj_dict)} objects found" | ||
log.debug(msg) | ||
log.debug(f"obj_dict: {self._obj_dict}") |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be merged with
_get_attributes
fromattr_sn
? The domain crawlerself
seems like it contains theapp
and parameter fields needed for_get_attributes
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea. Created a getAttributes function in servicenode_lib.py that both attr_sn and DomainCrawl call.