Skip to content

Conversation

@vymao
Copy link

@vymao vymao commented Aug 26, 2025

If we aren't using the Metaflow metadata service provider, Metaflow defaults to generating task IDs locally. But these task IDs are just simple integers based on how many tasks/steps there are and are sequentially incremented based on new_task_id in metaflow/plugins/metadata_providers/local.py. This presents a problem when we're doing AWS Batch MNP, since currently we try and mass replace based on the task ID in the secondary command. If this is a simple integer, this will replace many erroneous places.

For example, if the task ID is "3", there could be many instances of "3" in the secondary command that then have many replacements with "-node-$AWS_BATCH_JOB_NODE_INDEX" when really we just want to replace the actual task ID.

Here, I've identified two places - the input task ID via --task-id and the task ID in MF_PATHSPEC, that should be the only two places in the command that have the actual task ID in them that need replacing. It is better to have more specific regexes this way.

Furthermore, if there is no metadata provider, I've added a new check for control MNP jobs to finish by checking the S3 datastore instead.

@savingoyal savingoyal requested a review from saikonen August 26, 2025 21:43
@vymao vymao requested a review from savingoyal August 28, 2025 20:23
@saikonen saikonen linked an issue Sep 5, 2025 that may be closed by this pull request
Copy link
Collaborator

@saikonen saikonen left a comment

Choose a reason for hiding this comment

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

functionally this PR is now working as expected. Had some suggestions for cleanup

Comment on lines 435 to 442
# Set the ulimit of number of open files to 65536. This is because we cannot set it easily once worker processes start on Batch.
# job_definition["containerProperties"]["linuxParameters"]["ulimits"] = [
# {
# "name": "nofile",
# "softLimit": 65536,
# "hardLimit": 65536,
# }
# ]
Copy link
Collaborator

Choose a reason for hiding this comment

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

can this be cleaned up?

Copy link
Author

Choose a reason for hiding this comment

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

Yep! Removed.

Comment on lines +23 to +50
# Prefer the task role by default when running inside AWS Batch containers
# by temporarily removing higher-precedence env credentials for this process.
# This avoids AMI-injected AWS_* env vars from overriding the task role.
# Outside of Batch, we leave env vars untouched unless explicitly opted-in.
if "AWS_BATCH_JOB_ID" in os.environ:
_aws_env_keys = [
"AWS_ACCESS_KEY_ID",
"AWS_SECRET_ACCESS_KEY",
"AWS_SESSION_TOKEN",
"AWS_PROFILE",
"AWS_DEFAULT_PROFILE",
]
_present = [k for k in _aws_env_keys if k in os.environ]
print(
"[Metaflow] AWS credential-related env vars present before Batch client init:",
_present,
)
_saved_env = {
k: os.environ.pop(k) for k in _aws_env_keys if k in os.environ
}
try:
self._client = get_aws_client("batch")
finally:
# Restore prior env for the rest of the process
for k, v in _saved_env.items():
os.environ[k] = v
else:
self._client = get_aws_client("batch")
Copy link
Collaborator

Choose a reason for hiding this comment

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

is this change relevant to the batch parallel issue, or something different? the PR seems to work fine without this part as well

Copy link
Author

Choose a reason for hiding this comment

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

Indeed it works, this was to cover the instances where particular AWS keys have already been set in the environment, which messed up getting the AWS client. This is relevant for the batch process given that we're using the batch client now.

Copy link
Collaborator

Choose a reason for hiding this comment

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

what confuses me with these is that the same environment variables should then also interfere with the task running inside a batch process from getting a working S3 client as well, which would break datastore access.

do you have an example AMI which exhibits this issue, or an example on how to reproduce the issue? I'm not running into any issues with my test setup even without these additions.

Copy link
Collaborator

Choose a reason for hiding this comment

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

If the environment modification part is not critical to this fix working for your use case, could you introduce that part as a separate PR?

@vymao vymao requested a review from saikonen October 30, 2025 20:47
if tds.has_metadata(TaskDataStore.METADATA_DONE_SUFFIX):
completed += 1
except Exception as e:
self.logger.warning("Datastore wait: error checking %s: %s", ps, e)
Copy link
Collaborator

Choose a reason for hiding this comment

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

self.logger doesn't actually have any methods, it is just click.secho being passed in. This also adds unnecessary (duplicate) timestamps to the log lines so sticking to print for now is fine.

Copy link
Collaborator

Choose a reason for hiding this comment

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

also note all other instances of self.logger

Comment on lines +23 to +50
# Prefer the task role by default when running inside AWS Batch containers
# by temporarily removing higher-precedence env credentials for this process.
# This avoids AMI-injected AWS_* env vars from overriding the task role.
# Outside of Batch, we leave env vars untouched unless explicitly opted-in.
if "AWS_BATCH_JOB_ID" in os.environ:
_aws_env_keys = [
"AWS_ACCESS_KEY_ID",
"AWS_SECRET_ACCESS_KEY",
"AWS_SESSION_TOKEN",
"AWS_PROFILE",
"AWS_DEFAULT_PROFILE",
]
_present = [k for k in _aws_env_keys if k in os.environ]
print(
"[Metaflow] AWS credential-related env vars present before Batch client init:",
_present,
)
_saved_env = {
k: os.environ.pop(k) for k in _aws_env_keys if k in os.environ
}
try:
self._client = get_aws_client("batch")
finally:
# Restore prior env for the rest of the process
for k, v in _saved_env.items():
os.environ[k] = v
else:
self._client = get_aws_client("batch")
Copy link
Collaborator

Choose a reason for hiding this comment

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

what confuses me with these is that the same environment variables should then also interfere with the task running inside a batch process from getting a working S3 client as well, which would break datastore access.

do you have an example AMI which exhibits this issue, or an example on how to reproduce the issue? I'm not running into any issues with my test setup even without these additions.

Comment on lines +23 to +50
# Prefer the task role by default when running inside AWS Batch containers
# by temporarily removing higher-precedence env credentials for this process.
# This avoids AMI-injected AWS_* env vars from overriding the task role.
# Outside of Batch, we leave env vars untouched unless explicitly opted-in.
if "AWS_BATCH_JOB_ID" in os.environ:
_aws_env_keys = [
"AWS_ACCESS_KEY_ID",
"AWS_SECRET_ACCESS_KEY",
"AWS_SESSION_TOKEN",
"AWS_PROFILE",
"AWS_DEFAULT_PROFILE",
]
_present = [k for k in _aws_env_keys if k in os.environ]
print(
"[Metaflow] AWS credential-related env vars present before Batch client init:",
_present,
)
_saved_env = {
k: os.environ.pop(k) for k in _aws_env_keys if k in os.environ
}
try:
self._client = get_aws_client("batch")
finally:
# Restore prior env for the rest of the process
for k, v in _saved_env.items():
os.environ[k] = v
else:
self._client = get_aws_client("batch")
Copy link
Collaborator

Choose a reason for hiding this comment

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

If the environment modification part is not critical to this fix working for your use case, could you introduce that part as a separate PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Is it possible to use @metaflow_ray with foreach on AWS Batch?

3 participants