Skip to content

Commit

Permalink
throw proper error
Browse files Browse the repository at this point in the history
  • Loading branch information
v-chen_data committed Sep 30, 2024
1 parent 107d246 commit 133045b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 10 deletions.
30 changes: 20 additions & 10 deletions llmfoundry/command_utils/data_prep/convert_delta_to_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from uuid import uuid4

import google.protobuf.any_pb2 as any_pb2
import grpc
import pandas as pd
import pyarrow as pa
import requests
Expand All @@ -24,6 +25,7 @@
FailedToConnectToDatabricksError,
FailedToCreateSQLConnectionError,
InsufficientPermissionsError,
InternalError,
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -660,16 +662,24 @@ def fetch_DT(
)

formatted_delta_table_name = format_tablename(delta_table_name)

fetch(
method,
formatted_delta_table_name,
json_output_folder,
batch_size,
processes,
sparkSession,
dbsql,
)
try:
fetch(
method,
formatted_delta_table_name,
json_output_folder,
batch_size,
processes,
sparkSession,
dbsql,
)
except grpc.RpcError as e:
if e.code(
) == grpc.StatusCode.INTERNAL and 'Job aborted due to stage failure' in e.details(
):
raise InternalError(
message=f'Possible Hardware Failure: {e.details()}'
) from e
raise e

if dbsql is not None:
dbsql.close()
Expand Down
52 changes: 52 additions & 0 deletions tests/a_scripts/data_prep/test_convert_delta_to_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
from typing import Any
from unittest.mock import MagicMock, mock_open, patch

import grpc

from llmfoundry.command_utils.data_prep.convert_delta_to_json import (
InsufficientPermissionsError,
InternalError,
download,
fetch,
fetch_DT,
Expand Down Expand Up @@ -524,3 +527,52 @@ def test_format_tablename(self):
format_tablename('hyphenated-catalog.schema.test_table'),
'`hyphenated-catalog`.`schema`.`test_table`',
)

@patch('llmfoundry.command_utils.data_prep.convert_delta_to_json.fetch')
@patch(
'llmfoundry.command_utils.data_prep.convert_delta_to_json.validate_and_get_cluster_info'
)
def test_fetch_DT_grpc_error_handling(
self, mock_validate_cluster_info: MagicMock, mock_fetch: MagicMock
):
# Arrange
# Mock the validate_and_get_cluster_info to return test values
mock_validate_cluster_info.return_value = ('dbconnect', None, None)

# Create a grpc.RpcError with StatusCode.INTERNAL and specific details
grpc_error = grpc.RpcError()
grpc_error.code = lambda: grpc.StatusCode.INTERNAL
grpc_error.details = lambda: 'Job aborted due to stage failure: Task failed due to an error.'

# Configure the fetch function to raise the grpc.RpcError
mock_fetch.side_effect = grpc_error

# Test inputs
delta_table_name = 'test_table'
json_output_folder = '/tmp/to/jsonl'
http_path = None
cluster_id = None
use_serverless = False
DATABRICKS_HOST = 'https://test-host'
DATABRICKS_TOKEN = 'test-token'

# Act & Assert
with self.assertRaises(InternalError) as context:
fetch_DT(
delta_table_name=delta_table_name,
json_output_folder=json_output_folder,
http_path=http_path,
cluster_id=cluster_id,
use_serverless=use_serverless,
DATABRICKS_HOST=DATABRICKS_HOST,
DATABRICKS_TOKEN=DATABRICKS_TOKEN,
)

# Verify that the InternalError contains the expected message
self.assertIn('Possible Hardware Failure', str(context.exception))
self.assertIn(
'Job aborted due to stage failure', str(context.exception)
)

# Verify that fetch was called
mock_fetch.assert_called_once()

0 comments on commit 133045b

Please sign in to comment.