Skip to content

Commit 8cc7fc4

Browse files
authored
Merge pull request #980 from hubmapconsortium/karlburke/ReindexPriorityParam
Add reindex-priority parameter to create, update, and component creations endpoints
2 parents 5cb99cc + 2a508b5 commit 8cc7fc4

3 files changed

Lines changed: 80 additions & 16 deletions

File tree

src/app.py

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from schema import schema_triggers
3333
from schema import schema_validators
3434
from schema import schema_neo4j_queries
35-
from schema.schema_constants import SchemaConstants
35+
from schema.schema_constants import SchemaConstants, ReindexPriorityLevelEnum
3636
from schema.schema_constants import DataVisibilityEnum
3737
from schema.schema_constants import MetadataScopeEnum
3838
from schema.schema_constants import TriggerTypeEnum
@@ -1185,9 +1185,12 @@ def create_entity(entity_type):
11851185

11861186
# Check URL parameters before proceeding to any CRUD operations, halting on validation failures.
11871187
#
1188-
# Check if re-indexing is to be suppressed after entity creation.
11891188
try:
1190-
supress_reindex = schema_manager.suppress_reindex(request.args)
1189+
# Check if re-indexing is to be suppressed after entity creation.
1190+
suppress_reindex = schema_manager.suppress_reindex(request_args=request.args)
1191+
# Determine valid re-indexing priority using Request parameters.
1192+
reindex_priority = schema_manager.get_reindex_priority(request_args=request.args
1193+
, calc_suppress_reindex=suppress_reindex)
11911194
except Exception as e:
11921195
bad_request_error(e)
11931196

@@ -1312,7 +1315,7 @@ def create_entity(entity_type):
13121315
# Will also filter the result based on schema
13131316
normalized_complete_dict = schema_manager.normalize_entity_result_for_response(complete_dict)
13141317

1315-
if supress_reindex:
1318+
if suppress_reindex:
13161319
logger.log(level=logging.INFO
13171320
, msg=f"Re-indexing suppressed during creation of {complete_dict['entity_type']}"
13181321
f" with UUID {complete_dict['uuid']}")
@@ -1321,7 +1324,9 @@ def create_entity(entity_type):
13211324
logger.log(level=logging.INFO
13221325
, msg=f"Re-indexing for creation of {complete_dict['entity_type']}"
13231326
f" with UUID {complete_dict['uuid']}")
1324-
reindex_entity(complete_dict['uuid'], user_token)
1327+
reindex_entity(uuid=complete_dict['uuid']
1328+
, user_token=user_token
1329+
, priority_level=reindex_priority)
13251330

13261331
return jsonify(normalized_complete_dict)
13271332

@@ -1494,9 +1499,12 @@ def update_entity(id):
14941499

14951500
# Check URL parameters before proceeding to any CRUD operations, halting on validation failures.
14961501
#
1497-
# Check if re-indexing is to be suppressed after entity creation.
14981502
try:
1499-
suppress_reindex = schema_manager.suppress_reindex(request.args)
1503+
# Check if re-indexing is to be suppressed after entity creation.
1504+
suppress_reindex = schema_manager.suppress_reindex(request_args=request.args)
1505+
# Determine valid re-indexing priority using Request parameters.
1506+
reindex_priority = schema_manager.get_reindex_priority(request_args=request.args
1507+
, calc_suppress_reindex=suppress_reindex)
15001508
except Exception as e:
15011509
bad_request_error(e)
15021510

@@ -1594,7 +1602,9 @@ def update_entity(id):
15941602
logger.log(level=logging.INFO
15951603
, msg=f"Re-indexing for modification of {normalized_entity_type}"
15961604
f" with UUID {entity_uuid}")
1597-
reindex_entity(entity_uuid, user_token)
1605+
reindex_entity(uuid=entity_uuid
1606+
, user_token=user_token
1607+
, priority_level=reindex_priority)
15981608

15991609
# Do not return the updated dict to avoid computing overhead - 7/14/2023 by Zhou
16001610
message_returned = f"The update request on {normalized_entity_type} of {id} has been accepted, the backend may still be processing"
@@ -4167,9 +4177,12 @@ def multiple_components():
41674177

41684178
# Check URL parameters before proceeding to any CRUD operations, halting on validation failures.
41694179
#
4170-
# Check if re-indexing is to be suppressed after entity creation.
41714180
try:
4172-
suppress_reindex = schema_manager.suppress_reindex(request.args)
4181+
# Check if re-indexing is to be suppressed after entity creation.
4182+
suppress_reindex = schema_manager.suppress_reindex(request_args=request.args)
4183+
# Determine valid re-indexing priority using Request parameters.
4184+
reindex_priority = schema_manager.get_reindex_priority(request_args=request.args
4185+
, calc_suppress_reindex=suppress_reindex)
41734186
except Exception as e:
41744187
bad_request_error(e)
41754188

@@ -4215,7 +4228,9 @@ def multiple_components():
42154228
logger.log(level=logging.INFO
42164229
, msg=f"Re-indexing for multiple component creation of {complete_dict['entity_type']}"
42174230
f" with UUID {complete_dict['uuid']}")
4218-
reindex_entity(complete_dict['uuid'], user_token)
4231+
reindex_entity(uuid=complete_dict['uuid']
4232+
, user_token=user_token
4233+
, priority_level=reindex_priority)
42194234
# Add back in dataset_link_abs_dir one last time
42204235
normalized_complete_dict['dataset_link_abs_dir'] = dataset_link_abs_dir
42214236
normalized_complete_entity_list.append(normalized_complete_dict)
@@ -5426,19 +5441,23 @@ def delete_cache(entity_uuid, entity_type):
54265441
54275442
Parameters
54285443
----------
5429-
uuid : str
5444+
uuid :
54305445
The uuid of the target entity
5431-
user_token: str
5446+
user_token:
54325447
The user's globus groups token
5448+
priority_level:
5449+
Value from the enumeration ReindexPriorityLevelEnum
54335450
"""
5434-
def reindex_entity(uuid, user_token):
5451+
def reindex_entity(uuid:str, user_token:str, priority_level:int = ReindexPriorityLevelEnum.HIGH.value) -> None:
54355452
headers = {
54365453
'Authorization': f'Bearer {user_token}'
54375454
}
54385455

54395456
logger.info(f"Making a call to search-api to reindex uuid: {uuid}")
54405457

5441-
response = requests.put(f"{app.config['SEARCH_API_URL']}/reindex/{uuid}", headers = headers)
5458+
response = requests.put(f"{app.config['SEARCH_API_URL']}/reindex/{uuid}"
5459+
, headers=headers
5460+
, params={'priority': priority_level})
54425461

54435462
# The reindex takes time, so 202 Accepted response status code indicates that
54445463
# the request has been accepted for processing, but the processing has not been completed

src/schema/schema_constants.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,11 @@ class Neo4jRelationshipEnum(Enum):
6464
REVISION_OF = 'REVISION_OF'
6565
USES_DATA = 'USES_DATA'
6666

67+
# Define an enumeration of re-index priority level types.
68+
# N.B. These are the same values maintained in ingest-api app.py _get_reindex_priority(), which
69+
# must be the same levels defined for the enqueue() method at
70+
# https://github.com/x-atlas-consortia/jobq/blob/main/src/atlas_consortia_jobq/queue.py
71+
class ReindexPriorityLevelEnum(Enum):
72+
HIGH = 1
73+
MEDIUM = 2
74+
LOW = 3

src/schema/schema_manager.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
# Don't confuse urllib (Python native library) with urllib3 (3rd-party library, requests also uses urllib3)
1111
from requests.packages.urllib3.exceptions import InsecureRequestWarning
12+
from werkzeug.datastructures import ImmutableMultiDict
1213

1314
# Local modules
1415
from schema import schema_errors
@@ -18,6 +19,7 @@
1819
from schema.schema_constants import SchemaConstants
1920
from schema.schema_constants import MetadataScopeEnum
2021
from schema.schema_constants import TriggerTypeEnum
22+
from schema.schema_constants import ReindexPriorityLevelEnum
2123

2224
# HuBMAP commons
2325
from hubmap_commons.hm_auth import AuthHelper
@@ -2392,7 +2394,7 @@ def get_organ_types():
23922394

23932395
"""
23942396
See if 'reindex' is a URL parameter passed in with the request and
2395-
if it indicates reindexing should be supressed. Default to reindexing in all other cases.
2397+
if it indicates reindexing should be suppressed. Default to reindexing in all other cases.
23962398
23972399
Parameters
23982400
----------
@@ -2404,6 +2406,9 @@ def get_organ_types():
24042406
bool
24052407
"""
24062408
def suppress_reindex(request_args) -> bool:
2409+
# N.B. This logic should be the same as that used by
2410+
# ingest-api app.py _suppress_reindex()
2411+
# https://github.com/hubmapconsortium/ingest-api/blob/main/src/app.py
24072412
if 'reindex' not in request_args:
24082413
return False
24092414
reindex_str = request_args.get('reindex').lower()
@@ -2414,6 +2419,38 @@ def suppress_reindex(request_args) -> bool:
24142419
raise Exception(f"The value of the 'reindex' parameter must be True or False (case-insensitive)."
24152420
f" '{request_args.get('reindex')}' is not recognized.")
24162421

2422+
"""
2423+
See if 'reindex-priority' is a URL parameter passed in with the request, if it is valid, and
2424+
if it is compatible with the calculated suppress_reindex() result. Default to 1 when not specified.
2425+
2426+
Parameters
2427+
----------
2428+
request_args:
2429+
The Flask request.args passed in from application request
2430+
2431+
calc_suppress_reindex:
2432+
The value returned from the suppress_reindex() method, if previously called.
2433+
Returns
2434+
-------
2435+
int value from the enumeration ReindexPriorityLevelEnum
2436+
"""
2437+
def get_reindex_priority(request_args:ImmutableMultiDict, calc_suppress_reindex:bool) -> int:
2438+
# N.B. This logic should be the same as that used by
2439+
# ingest-api app.py _get_reindex_priority()
2440+
# https://github.com/hubmapconsortium/ingest-api/blob/main/src/app.py
2441+
if calc_suppress_reindex and 'reindex-priority' in request_args:
2442+
raise Exception("Specifying a re-index priority is incompatible with suppressing re-indexing.")
2443+
if 'reindex-priority' not in request_args:
2444+
return ReindexPriorityLevelEnum.HIGH.value
2445+
try:
2446+
priority_int = int(request_args.get('reindex-priority'))
2447+
except ValueError as ve:
2448+
raise Exception("The value of the 'reindex-priority' parameter must be an integer.")
2449+
if priority_int not in ReindexPriorityLevelEnum:
2450+
raise Exception(f"The value of the 'reindex-priority' parameter must be"
2451+
f" greater than or equal to {ReindexPriorityLevelEnum.HIGH.value} (high priority)"
2452+
f" and less than or equal to {ReindexPriorityLevelEnum.LOW.value} (low priority).")
2453+
return priority_int
24172454

24182455
####################################################################################################
24192456
## Internal functions

0 commit comments

Comments
 (0)