Skip to content
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

hotfix: add error handling for indexer jobs #76

Merged
merged 2 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions src/aind_data_asset_indexer/aind_bucket_indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,17 @@ def _dask_task_to_process_record_list(
authSource="admin",
)
for record in record_list:
self._process_docdb_record(
docdb_record=record,
docdb_client=doc_db_client,
s3_client=s3_client,
)
try:
self._process_docdb_record(
docdb_record=record,
docdb_client=doc_db_client,
s3_client=s3_client,
)
except Exception as e:
logging.error(
f'Error processing docdb {record.get("_id")}, '
f'{record.get("location")}: {repr(e)}'
)
s3_client.close()
doc_db_client.close()

Expand Down Expand Up @@ -274,6 +280,7 @@ def _process_prefix(
self.job_settings.doc_db_collection_name
]
if "_id" in json_contents:
# TODO: check is_dict_corrupt(json_contents)
response = collection.update_one(
{"_id": json_contents["_id"]},
{"$set": json_contents},
Expand Down Expand Up @@ -380,12 +387,19 @@ def _dask_task_to_process_prefix_list(self, prefix_list: List[str]):
docdb_client=doc_db_client,
)
for prefix in prefix_list:
self._process_prefix(
s3_prefix=prefix,
s3_client=s3_client,
location_to_id_map=location_to_id_map,
docdb_client=doc_db_client,
)
try:
self._process_prefix(
s3_prefix=prefix,
s3_client=s3_client,
location_to_id_map=location_to_id_map,
docdb_client=doc_db_client,
)
except Exception as e:
logging.error(
f"Error processing "
f"{get_s3_location(self.job_settings.s3_bucket, prefix)}: "
f"{repr(e)}"
)
s3_client.close()
doc_db_client.close()

Expand Down
24 changes: 17 additions & 7 deletions src/aind_data_asset_indexer/codeocean_bucket_indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,16 @@ def _dask_task_to_process_record_list(self, record_list: List[dict]):
)

for record in record_list:
self._process_codeocean_record(
codeocean_record=record,
docdb_client=doc_db_client,
s3_client=s3_client,
)
try:
self._process_codeocean_record(
codeocean_record=record,
docdb_client=doc_db_client,
s3_client=s3_client,
)
except Exception as e:
logging.error(
f'Error processing {record.get("location")}: {repr(e)}'
)
s3_client.close()
doc_db_client.close()

Expand Down Expand Up @@ -165,8 +170,13 @@ def _dask_task_to_delete_record_list(self, record_list: List[str]):
)
db = docdb_client[self.job_settings.doc_db_db_name]
collection = db[self.job_settings.doc_db_collection_name]
response = collection.delete_many(filter={"_id": {"$in": record_list}})
logging.info(response.raw_result)
try:
response = collection.delete_many(
filter={"_id": {"$in": record_list}}
)
logging.info(response.raw_result)
except Exception as e:
logging.error(f"Error deleting records: {repr(e)}")
docdb_client.close()

def _delete_records_from_docdb(self, record_list: List[str]):
Expand Down
133 changes: 133 additions & 0 deletions tests/test_aind_bucket_indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,64 @@ def test_dask_task_to_process_record_list(
mock_s3_client.close.assert_called_once_with()
mock_mongo_client.close.assert_called_once_with()

@patch(
"aind_data_asset_indexer.aind_bucket_indexer.AindIndexBucketJob."
"_process_docdb_record"
)
@patch("aind_data_asset_indexer.aind_bucket_indexer.MongoClient")
@patch("boto3.client")
@patch("logging.error")
def test_dask_task_to_process_record_list_error(
self,
mock_log_error: MagicMock,
mock_boto3_client: MagicMock,
mock_docdb_client: MagicMock,
mock_process_docdb_record: MagicMock,
):
"""Tests _dask_task_to_process_record_list when there is an error in 1
record."""
mock_s3_client = MagicMock()
mock_boto3_client.return_value = mock_s3_client
mock_mongo_client = MagicMock()
mock_docdb_client.return_value = mock_mongo_client
records = [
self.example_md_record,
self.example_md_record1,
self.example_md_record2,
]
mock_process_docdb_record.side_effect = [
None,
Exception("Error processing record"),
None,
]
self.basic_job._dask_task_to_process_record_list(record_list=records)
mock_process_docdb_record.assert_has_calls(
[
call(
docdb_record=self.example_md_record,
docdb_client=mock_mongo_client,
s3_client=mock_s3_client,
),
call(
docdb_record=self.example_md_record1,
docdb_client=mock_mongo_client,
s3_client=mock_s3_client,
),
call(
docdb_record=self.example_md_record2,
docdb_client=mock_mongo_client,
s3_client=mock_s3_client,
),
]
)
mock_log_error.assert_called_once_with(
"Error processing docdb 5ca4a951-d374-4f4b-8279-d570a35b2286, s3:"
"//aind-ephys-data-dev-u5u0i5/ecephys_567890_2000-01-01_04-00-00: "
"Exception('Error processing record')"
)
mock_s3_client.close.assert_called_once_with()
mock_mongo_client.close.assert_called_once_with()

@patch("dask.bag.map_partitions")
def test_process_records(self, mock_dask_bag_map_parts: MagicMock):
"""Test _process_records method."""
Expand Down Expand Up @@ -789,6 +847,81 @@ def test_dask_task_to_process_prefix_list(
mock_s3_client.close.assert_called_once_with()
mock_mongo_client.close.assert_called_once_with()

@patch(
"aind_data_asset_indexer.aind_bucket_indexer."
"build_docdb_location_to_id_map"
)
@patch(
"aind_data_asset_indexer.aind_bucket_indexer.AindIndexBucketJob."
"_process_prefix"
)
@patch("aind_data_asset_indexer.aind_bucket_indexer.MongoClient")
@patch("boto3.client")
@patch("logging.error")
def test_dask_task_to_process_prefix_list_error(
self,
mock_log_error: MagicMock,
mock_boto3_client: MagicMock,
mock_docdb_client: MagicMock,
mock_process_prefix: MagicMock,
mock_build_location_to_id_map: MagicMock,
):
"""Tests _dask_task_to_process_prefix_list when there is an error in 1
prefix."""
mock_s3_client = MagicMock()
mock_boto3_client.return_value = mock_s3_client
mock_mongo_client = MagicMock()
mock_docdb_client.return_value = mock_mongo_client
prefixes = [
"ecephys_642478_2023-01-17_13-56-29",
"ecephys_567890_2000-01-01_04-00-00",
"ecephys_655019_2000-01-01_01-01-02",
]
mock_location_to_id_map = {
"ecephys_642478_2023-01-17_13-56-29": (
"488bbe42-832b-4c37-8572-25eb87cc50e2"
),
"ecephys_567890_2000-01-01_04-00-00": (
"5ca4a951-d374-4f4b-8279-d570a35b2286"
),
}
mock_build_location_to_id_map.return_value = mock_location_to_id_map
mock_process_prefix.side_effect = [
None,
Exception("Error processing prefix"),
None,
]
self.basic_job._dask_task_to_process_prefix_list(prefix_list=prefixes)
mock_process_prefix.assert_has_calls(
[
call(
s3_prefix="ecephys_642478_2023-01-17_13-56-29",
s3_client=mock_s3_client,
location_to_id_map=mock_location_to_id_map,
docdb_client=mock_mongo_client,
),
call(
s3_prefix="ecephys_567890_2000-01-01_04-00-00",
s3_client=mock_s3_client,
location_to_id_map=mock_location_to_id_map,
docdb_client=mock_mongo_client,
),
call(
s3_prefix="ecephys_655019_2000-01-01_01-01-02",
s3_client=mock_s3_client,
location_to_id_map=mock_location_to_id_map,
docdb_client=mock_mongo_client,
),
]
)
mock_log_error.assert_called_once_with(
"Error processing s3://aind-ephys-data-dev-u5u0i5/"
"ecephys_567890_2000-01-01_04-00-00: "
"Exception('Error processing prefix')"
)
mock_s3_client.close.assert_called_once_with()
mock_mongo_client.close.assert_called_once_with()

@patch("dask.bag.map_partitions")
def test_process_prefixes(self, mock_dask_bag_map_parts: MagicMock):
"""Test _process_prefixes method."""
Expand Down
69 changes: 69 additions & 0 deletions tests/test_codeocean_bucket_indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,54 @@ def test_dask_task_to_process_record_list(
mock_s3_client.close.assert_called_once_with()
mock_mongo_client.close.assert_called_once_with()

@patch(
"aind_data_asset_indexer.codeocean_bucket_indexer."
"CodeOceanIndexBucketJob._process_codeocean_record"
)
@patch("aind_data_asset_indexer.codeocean_bucket_indexer.MongoClient")
@patch("boto3.client")
@patch("logging.error")
def test_dask_task_to_process_record_list_error(
self,
mock_log_error: MagicMock,
mock_boto3_client: MagicMock,
mock_docdb_client: MagicMock,
mock_process_codeocean_record: MagicMock,
):
"""Tests _dask_task_to_process_record_list when there is an error in 1
record"""
mock_s3_client = MagicMock()
mock_boto3_client.return_value = mock_s3_client
mock_mongo_client = MagicMock()
mock_docdb_client.return_value = mock_mongo_client
records = self.example_codeocean_records
mock_process_codeocean_record.side_effect = [
Exception("Error processing record"),
None,
]
self.basic_job._dask_task_to_process_record_list(record_list=records)
mock_process_codeocean_record.assert_has_calls(
[
call(
codeocean_record=records[0],
docdb_client=mock_mongo_client,
s3_client=mock_s3_client,
),
call(
codeocean_record=records[1],
docdb_client=mock_mongo_client,
s3_client=mock_s3_client,
),
]
)
mock_log_error.assert_called_once_with(
"Error processing s3://some_co_bucket/"
"11ee1e1e-11e1-1111-1111-e11eeeee1e11: "
"Exception('Error processing record')"
)
mock_s3_client.close.assert_called_once_with()
mock_mongo_client.close.assert_called_once_with()

@patch("dask.bag.map_partitions")
def test_process_codeocean_records(
self, mock_dask_bag_map_parts: MagicMock
Expand Down Expand Up @@ -247,6 +295,27 @@ def test_dask_task_to_delete_record_list(
)
mock_log_info.assert_called_once_with({"message": "success"})

@patch("logging.error")
@patch("aind_data_asset_indexer.codeocean_bucket_indexer.MongoClient")
def test_dask_task_to_delete_record_list_error(
self, mock_docdb_client: MagicMock, mock_log_error: MagicMock
):
"""Tests _dask_task_to_delete_record_list"""
mock_db = MagicMock()
mock_docdb_client.return_value.__getitem__.return_value = mock_db
mock_collection = MagicMock()
mock_db.__getitem__.return_value = mock_collection
mock_collection.delete_many.side_effect = Exception(
"Error deleting records"
)
records_to_delete = [r["_id"] for r in self.example_docdb_records]
self.basic_job._dask_task_to_delete_record_list(
record_list=records_to_delete
)
mock_log_error.assert_called_once_with(
"Error deleting records: Exception('Error deleting records')"
)

@patch("dask.bag.map_partitions")
def test_delete_records_from_docdb(
self, mock_dask_bag_map_parts: MagicMock
Expand Down
Loading