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

[Fetch Migration] Suppress exit code for graceful termination (SIGTERM) #478

Merged
merged 1 commit into from
Jan 23, 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
9 changes: 7 additions & 2 deletions FetchMigration/python/fetch_orchestrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def write_inline_target_host(pipeline_file_path: str, inline_target_host: str):
yaml.safe_dump(pipeline_yaml, pipeline_file)


def run(params: FetchOrchestratorParams) -> Optional[int]:
def run(params: FetchOrchestratorParams) -> int:
# This is expected to be a base64 encoded string
inline_pipeline = __get_env_string("INLINE_PIPELINE")
inline_target_host = __get_env_string("INLINE_TARGET_HOST")
Expand All @@ -93,6 +93,7 @@ def run(params: FetchOrchestratorParams) -> Optional[int]:
report=True, dryrun=params.is_dry_run)
logging.info("Running metadata migration...\n")
metadata_migration_result = metadata_migration.run(metadata_migration_params)
return_code: int = 0
if metadata_migration_result.target_doc_count == 0:
logging.warning("Target document count is zero, skipping data migration...")
elif len(metadata_migration_result.migration_indices) > 0 and not params.is_only_metadata_migration():
Expand All @@ -103,8 +104,12 @@ def run(params: FetchOrchestratorParams) -> Optional[int]:
migration_monitor_params = MigrationMonitorParams(metadata_migration_result.target_doc_count,
params.get_local_endpoint())
logging.info("Starting migration monitor...\n")
return migration_monitor.run(migration_monitor_params, proc)
return_code = migration_monitor.run(migration_monitor_params, proc)
# Suppress non-zero return code for graceful termination (SIGTERM)
if return_code == 143:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A citation to the DataPrepper code (github) showing where/why it returns 143 would be great. 143 seems like it could be a fragile number to pin on. This should also be a constant to document it a bit more.
I guess DataPrepper can also just run in "one-off" mode where it exits after a task is done (and then exits with code 0)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

143 is the standard exit code for a SIGTERM signal to indicate a graceful shutdown of the process. This behavior isn't managed by Data Prepper but rather by the JVM.

I wasn't able to find any system libraries in Python that have this declared as a constant. IMO a test case isn't needed for this behavior since it is the expected behavior for any Linux process. Let me know if you disagree?

return_code = 0
logging.info("Fetch Migration workflow concluded\n")
return return_code


if __name__ == '__main__': # pragma no cover
Expand Down
52 changes: 45 additions & 7 deletions FetchMigration/python/tests/test_fetch_orchestrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def tearDown(self) -> None:
def test_orchestrator_run(self, mock_metadata_migration: MagicMock, mock_subprocess: MagicMock,
mock_monitor: MagicMock):
fetch_params = FetchOrchestratorParams("test_dp_path", "test_pipeline_file")
# Setup mock pre-migration
# Setup mock metadata migration
expected_metadata_migration_input = \
MetadataMigrationParams(fetch_params.pipeline_file_path,
fetch_params.data_prepper_path + "/pipelines/pipeline.yaml",
Expand All @@ -63,13 +63,15 @@ def test_orchestrator_run(self, mock_metadata_migration: MagicMock, mock_subproc
@mock.patch.dict(os.environ, {}, clear=True)
def test_orchestrator_no_migration(self, mock_metadata_migration: MagicMock, mock_subprocess: MagicMock,
mock_monitor: MagicMock):
# Setup empty result from pre-migration
# Setup empty result from metadata migration
mock_metadata_migration.return_value = MetadataMigrationResult()
orchestrator.run(FetchOrchestratorParams("test", "test"))
result = orchestrator.run(FetchOrchestratorParams("test", "test"))
mock_metadata_migration.assert_called_once_with(ANY)
# Subsequent steps should not be called
mock_subprocess.assert_not_called()
mock_monitor.assert_not_called()
# Expect successful exit code
self.assertEqual(0, result)

@patch('migration_monitor.run')
@patch('subprocess.Popen')
Expand All @@ -79,18 +81,20 @@ def test_orchestrator_no_migration(self, mock_metadata_migration: MagicMock, moc
def test_orchestrator_run_create_only(self, mock_metadata_migration: MagicMock, mock_subprocess: MagicMock,
mock_monitor: MagicMock):
fetch_params = FetchOrchestratorParams("test_dp_path", "test_pipeline_file", create_only=True)
# Setup mock pre-migration
# Setup mock metadata migration
expected_metadata_migration_input = \
MetadataMigrationParams(fetch_params.pipeline_file_path,
fetch_params.data_prepper_path + "/pipelines/pipeline.yaml",
report=True)
test_result = MetadataMigrationResult(10, {"index1", "index2"})
mock_metadata_migration.return_value = test_result
# Run test
orchestrator.run(fetch_params)
result = orchestrator.run(fetch_params)
mock_metadata_migration.assert_called_once_with(expected_metadata_migration_input)
mock_subprocess.assert_not_called()
mock_monitor.assert_not_called()
# Expect successful exit code
self.assertEqual(0, result)

@patch('migration_monitor.run')
@patch('subprocess.Popen')
Expand All @@ -100,18 +104,52 @@ def test_orchestrator_run_create_only(self, mock_metadata_migration: MagicMock,
def test_orchestrator_dryrun(self, mock_metadata_migration: MagicMock, mock_subprocess: MagicMock,
mock_monitor: MagicMock):
fetch_params = FetchOrchestratorParams("test_dp_path", "test_pipeline_file", dryrun=True)
# Setup mock pre-migration
# Setup mock metadata migration
expected_metadata_migration_input = \
MetadataMigrationParams(fetch_params.pipeline_file_path,
fetch_params.data_prepper_path + "/pipelines/pipeline.yaml",
report=True, dryrun=True)
test_result = MetadataMigrationResult(10, {"index1", "index2"})
mock_metadata_migration.return_value = test_result
# Run test
orchestrator.run(fetch_params)
result = orchestrator.run(fetch_params)
mock_metadata_migration.assert_called_once_with(expected_metadata_migration_input)
mock_subprocess.assert_not_called()
mock_monitor.assert_not_called()
# Expect successful exit code
self.assertEqual(0, result)

@patch('migration_monitor.run')
@patch('subprocess.Popen')
@patch('metadata_migration.run')
# Note that mock objects are passed bottom-up from the patch order above
@mock.patch.dict(os.environ, {}, clear=True)
def test_orchestrator_suppressed_exit_code(self, mock_metadata_migration: MagicMock, mock_subprocess: MagicMock,
mock_monitor: MagicMock):
# Setup mock metadata migration
test_result = MetadataMigrationResult(10, {"index1", "index2"})
mock_metadata_migration.return_value = test_result
# Set up graceful termination exit code
mock_monitor.return_value = 143
result = orchestrator.run(FetchOrchestratorParams("test_dp_path", "test_pipeline_file"))
# Expect non-zero exit code to be suppressed
self.assertEqual(0, result)

@patch('migration_monitor.run')
@patch('subprocess.Popen')
@patch('metadata_migration.run')
# Note that mock objects are passed bottom-up from the patch order above
@mock.patch.dict(os.environ, {}, clear=True)
def test_orchestrator_nonzero_exit_code(self, mock_metadata_migration: MagicMock, mock_subprocess: MagicMock,
mock_monitor: MagicMock):
# Setup mock metadata migration
test_result = MetadataMigrationResult(10, {"index1", "index2"})
mock_metadata_migration.return_value = test_result
sigkill_exit_code: int = 137
mock_monitor.return_value = sigkill_exit_code
result = orchestrator.run(FetchOrchestratorParams("test_dp_path", "test_pipeline_file"))
# Expect non-zero exit code to be returned
self.assertEqual(sigkill_exit_code, result)

@patch('fetch_orchestrator.write_inline_target_host')
@patch('metadata_migration.run')
Expand Down
Loading