Skip to content

add pre_exec job parameter to allow for setup commands - #556

Closed
jolange wants to merge 1 commit into
dask:mainfrom
jolange:pre_exec
Closed

jolange wants to merge 1 commit into
dask:mainfrom
jolange:pre_exec

Conversation

@jolange

@jolange jolange commented Jul 8, 2022

Copy link
Copy Markdown
Contributor

This allows to specify additional commands to be run before the actual "%(python)s -m distributed.cli.dask_worker"-call that starts the worker. This can be used to set up/source a virtual environment (or conda, mamba, ...), load a module, or whatever setup is needed.

Example usage for a venv:

pre_exec = ['cd ~/projects/dask-jobqueue/example', 'source venv/bin/activate']
cluster = HTCondorCluster(cores=1, memory="2GB", disk="4GB", log_directory = 'logs',
                          pre_exec=pre_exec, python='python3')

@guillaumeeb guillaumeeb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hi @jolange,

Thanks for raising this PR. It's really appreciated.

In this case, I'm not sure it is needed: doesn't the env-extra kwarg already suites your need?

See https://github.com/dask/dask-jobqueue/blob/main/dask_jobqueue/core.py#L248.

Or in the docs: https://jobqueue.dask.org/en/latest/generated/dask_jobqueue.SLURMCluster.html for example.

Maybe the name is not rightly chosen.

@jolange

jolange commented Jul 11, 2022

Copy link
Copy Markdown
Contributor Author

Hi @guillaumeeb ,
thanks for looking into this! I also first thought that env_extra might work, but unfortunately this is not sufficient: At least for HTCondor this only lets you specify environment variables (the function parsing that is defined here).
So something like

cluster = HTCondorCluster(cores=1, memory="2GB", disk="4GB", log_directory = 'logs',
                          env_extra=['TESTVAR=1', 'export TESTVAR2=2'],
                          )

Generates this in an HTCondor submission file:

Environment = "TESTVAR=1 TESTVAR2=2"

But things like this

env_extra=['cd ~/projects/dask-jobqueue/example', 'source venv/bin/activate'],

simply get swallowed and do not appear anywhere.

@jolange

jolange commented Jul 11, 2022

Copy link
Copy Markdown
Contributor Author

Addendum:
You are right that this works for e.g. SLURM. There all lines are simply added to the script (which is much simpler than the HTCondor submit files).
Do you consider this a "bug" in HTCondorCluster? I could also take all lines that are not selected by env_lines_to_dict() (see link above) and prepend those to the command instead of introducing a new parameter. This would be a bit intransparent to the user, though, because some commands are prepended and some are put into the HTCondor Environment parameter.

@guillaumeeb

Copy link
Copy Markdown
Member

OK, so this is a specific behavior from HTCondorCluster.

I don't recall why things where made like this, must be some specificities of HTCondor. cc @riedel who maybe knows more.

I would still try to see if we can improve thing in HTCondorCluster side, not adding a new feature for every other Cluster implementation that overlaps with existing feature.

Maybe we should also make the env_extra kwarg a bit more explicit.

@jolange

jolange commented Jul 12, 2022

Copy link
Copy Markdown
Contributor Author

I agree that we should not introduce a new parameter if the old one is supposed to do what I intent to, already.

Maybe we can also ask @matyasselmeci who implemented this in #245.
To me it looks like there is no specific reason for this treatment but it might be a misunderstanding caused by the parameter name ("env") and the usage examples with other cluster types. If that would be true, I would propose to simply handle the env_extra like the new parameter I wanted to introduce and drop my proposal. I also think that this would not be a behaviour breaking change, because setting environment variables would still work, although not using the HTCondor Environment = variable.

@mivade

mivade commented Jul 12, 2022

Copy link
Copy Markdown

The way HTCondor submit files work I think it would be rather confusing to allow env_extra to include arbitrary commands (frankly I don't think that makes sense for any cluster). I think having some kind of helper script argument like the one proposed here would make sense if it's necessary, but I'm also confused about the purpose. As implemented sys.executable sets the Python interpreter to use which should be the same as the one in your virtual environment. Is there a particular reason that was not sufficient?

@ocaisa

ocaisa commented Jul 12, 2022

Copy link
Copy Markdown
Member

A common use case that I've had is to use env_extra to load environment modules to get the computing environment that I need for a particular set of tasks:

GROMACS_gpu_cluster = CustomSLURMCluster(
    name="GROMACS_gpu_cluster",
    nodes=2,
    mpi_mode=True,
    fork_mpi=True,
    queue_type="gpus",
    maximum_jobs=5,
    env_extra=[
        "module load Intel",
        "module load IntelMPI",
        "module load GROMACS",
        "module load dask",
        "module load jobqueue_features",
    ],
)

The code here is customised but the use case is hopefully clear.

@jolange

jolange commented Jul 13, 2022

Copy link
Copy Markdown
Contributor Author

The way HTCondor submit files work I think it would be rather confusing to allow env_extra to include arbitrary commands (frankly I don't think that makes sense for any cluster).

What I got from the discussion so far is that the parameter rather is a misnomer.

... but I'm also confused about the purpose. As implemented sys.executable sets the Python interpreter to use which should be the same as the one in your virtual environment.

Unfortunately that's not sufficient, because that's a symlink (venv/bin/python3 -> /usr/bin/python3) and sys.executable is set to the target. Furthermore we regularly need to load environment modules like @ocaisa described and also load software/packages from other sources (like cvmfs).

@mivade

mivade commented Jul 13, 2022

Copy link
Copy Markdown

Got it, thanks for the clarification. I think I'd still be in favor of not using env_extra for this but instead some new keyword argument like originally proposed here (and possibly deprecating env_extra since the name is misleading).

@jolange

jolange commented Jul 18, 2022

Copy link
Copy Markdown
Contributor Author

Either way would be fine for me. I guess it's up to the maintainers to decide here.

@riedel

riedel commented Jul 19, 2022 •

Copy link
Copy Markdown
Member

Sorry for not replying last week

I don't recall why things where made like this, must be some specificities of HTCondor. cc @riedel who maybe knows more.

this code dates back to #245 and @mivade is right: htcondor submit files differ from slurm in that they are no scripts themselves but have their own syntax which makes things a bit different. However, I wouldn't have expected the behaviour behind the param for slurm here.

I think what would make things easier to move htcondor closer to slurm in the sense that it also is a script that is submitted. one option could be to actually use use a script that submits itself to condor_submit (via stdin). Sth like:

#!/bin/bash
if [ -z ${_CONDOR_JOB_AD+x} ]; then 
exec condor_submit - <<EOF
 <all the normal stuff goes here>
EOF
fi

<env_extra>

%(python) -m distributed.cli.dask_worker ...

would that make htcondor more manageable?

Edit: see next 2 comments

@jolange

jolange commented Jul 20, 2022

Copy link
Copy Markdown
Contributor Author

Thanks @riedel for the feedback!

differ from slurm in that they are no scripts themselves but have their own syntax which makes things a bit different

Right, this is why I prepend commands in the Arguments = parameter to achieve what the scripts do for e.g. SLURM. Here the commands are separated by semicolon instead of newlines, but the logic is the same, isn't it?
Anyway, I would be happy to have a solution with a "proper" script (=with statements separated with newlines) to run in the job, because currently it is a quoting nightmare.

That being said, I don't fully get your proposal, yet:

  • Do you mean condor_submit in the script instead of condor_q?
  • Is EOF supposed to go before the fi?
  • What is submit command supposed to be? The actual python call starting the worker? (%(python)s -m distributed.cli.dask_worker ...)
  • I don't get where the "script that submits itself"-magic would happen.

@riedel

riedel commented Jul 20, 2022 •

Copy link
Copy Markdown
Member

too hot here, sorry (I edit the above, thanks to your comments)

  • Do you mean condor_submit in the script instead of condor_q?
    yes
  • Is EOF supposed to go before the fi?
    yes
    What is submit command supposed to be?

I first thought to make the submit command blank or just env

It would be just a script that has two execution modes: one on the submit node and one on the execution nodes. I think protecting it via the _CONDOR_JOB_AD might not be too smart because it would probably give a few false positives when the Job is submitted from another (e.g. longer running, that hosts the scheduler)

One could however use env -u _CONDOR_JOB_AD as submit_command

Disclaimer: this is just an idea, because I stumbled across this in other places (e.g. in the tests because the bangline was missing). I would only do it if others like the idea as well.

@guillaumeeb

Copy link
Copy Markdown
Member

Hi everyone,

We all agree this is important for any queuing system to be able to run some commands before starting the Dask worker.

Currently, this is supposed to be done using env_extra as stated in the doc:

Other commands to add to script before launching worker.

For HTCondor, the command only add environment variables, probably because of a misunderstanding due to the kwarg name, and also because HTCondor is a bit specific.

So I will probably reject this as is, but two things could be done:

  • propose and implement a better name for env_extra, with deprecation and everything.
  • Correct the behavior of HTCondorCluster. I'd say this would be by moving the current behavior of env_extra to another kwarg specific to HTCondor, and in the same time implementing env_extra as it should, with any of your proposal.

Any thoughts?

@riedel

riedel commented Jul 24, 2022 •

Copy link
Copy Markdown
Member
  • propose and implement a better name for env_extra, with deprecation and everything.

#323 proposes job_script_extra, see also

  • Correct the behavior of HTCondorCluster

see #393

would suggest to continue / reactivate the discussion there to come to a conclusion

@jolange

jolange commented Jul 25, 2022

Copy link
Copy Markdown
Contributor Author

propose and implement a better name for env_extra, with deprecation and everything.

#323 proposes job_script_extra

Sounds good.

I'd say this would be by moving the current behavior of env_extra to another kwarg specific to HTCondor

I think that's not even necessary, because you can already use job_extra to inject any key-value pair to the submit file.

@guillaumeeb

Copy link
Copy Markdown
Member

Wow, good catch for both issues @riedel, I did not remember those. So I'm closing this PR in favor of those two issues we need to fix.

jolange added a commit to jolange/dask-jobqueue that referenced this pull request Jul 26, 2022
Before only environment variables were considered using HTCondor's
`Environment =` feature, whereas the parameter description generally
says "Other commands to add".

If the `Environment =` is needed, one can still use the generic
`job_extra` parameter to set it.

fixes dask#393
related to dask#323 dask#556
guillaumeeb pushed a commit that referenced this pull request Aug 1, 2022
* fix behaviour of `env_extra` for HTCondor

Before only environment variables were considered using HTCondor's
`Environment =` feature, whereas the parameter description generally
says "Other commands to add".

If the `Environment =` is needed, one can still use the generic
`job_extra` parameter to set it.

fixes #393
related to #323 #556

* adapt htcondor tests for new `env_extra` behaviour

* adapt htcondor tests for new `env_extra` behaviour

"export" is preserved now

* docs: made "Moab Deployments" heading the same level as the others

* docs: added description of HTCondorCluster + env_extra

- example in the docstring
- description in "example deployments""
- description in "advanced tops an tricks"

* docs: removed the HTCondorCluster section from examples

* formatting according to black and flake8
guillaumeeb pushed a commit to ikabadzhov/dask-jobqueue that referenced this pull request Aug 7, 2022
* fix behaviour of `env_extra` for HTCondor

Before only environment variables were considered using HTCondor's
`Environment =` feature, whereas the parameter description generally
says "Other commands to add".

If the `Environment =` is needed, one can still use the generic
`job_extra` parameter to set it.

fixes dask#393
related to dask#323 dask#556

* adapt htcondor tests for new `env_extra` behaviour

* adapt htcondor tests for new `env_extra` behaviour

"export" is preserved now

* docs: made "Moab Deployments" heading the same level as the others

* docs: added description of HTCondorCluster + env_extra

- example in the docstring
- description in "example deployments""
- description in "advanced tops an tricks"

* docs: removed the HTCondorCluster section from examples

* formatting according to black and flake8
guillaumeeb added a commit that referenced this pull request Aug 7, 2022
…ob scripts (#560)

* Use `--nworkers` in stead of deprecated `--nprocs` in the generated job scripts

Fix #559.

* Update the requirements for dask and distributed

This is needed to support the `--nworkers` option.

* Fix behaviour of `env_extra` for HTCondor (#563)

* fix behaviour of `env_extra` for HTCondor

Before only environment variables were considered using HTCondor's
`Environment =` feature, whereas the parameter description generally
says "Other commands to add".

If the `Environment =` is needed, one can still use the generic
`job_extra` parameter to set it.

fixes #393
related to #323 #556

* adapt htcondor tests for new `env_extra` behaviour

* adapt htcondor tests for new `env_extra` behaviour

"export" is preserved now

* docs: made "Moab Deployments" heading the same level as the others

* docs: added description of HTCondorCluster + env_extra

- example in the docstring
- description in "example deployments""
- description in "advanced tops an tricks"

* docs: removed the HTCondorCluster section from examples

* formatting according to black and flake8

* Drop Python 3.7 (#562)

* Drop Python 3.7

* Fix cleanup fixture probem (see fistributed#9137)

* Override cleanup distributed fixture, and reconfigure dask-jobqueue when called

* Use power of 2 for the memory checks in tests (see dask#7484)

* Apply Black

* Apply correct version of black...

* conda install Python in HTCondor Docker image

* Fix HTCondor Dockerfile

* Fix PBS Issue

* Add a timeout for every wait_for_workers call

* Fix HTCondor tests, leave room for scheduling cycle to take place for HTCondor

* flake check

* debugging HTCondor tests on CI

* Flake

* reduce negotiator interval for faster job queuing, more debugging logs

* move condor command at the right place

* always run cleanup step, and print more things on HTCondor

* Clean temporary debug and other not necessary modifications

* Disable HTCondor CI for now

* Import loop_in_thread fixture from distributed

* Override method from distributed to make tests pass

Co-authored-by: Johannes Lange <[email protected]>
Co-authored-by: Guillaume Eynard-Bontemps <[email protected]>
@jolange
jolange deleted the pre_exec branch August 10, 2022 13:44
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.

5 participants