Skip to content

Commit

Permalink
[Run] Set the default run kind to local (#5709)
Browse files Browse the repository at this point in the history
  • Loading branch information
moranbental committed Jun 5, 2024
1 parent 8ab508a commit aa3d12d
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 12 deletions.
11 changes: 8 additions & 3 deletions mlrun/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -1039,9 +1039,14 @@ def set_if_not_none(_struct, key, val):
"status.last_update": to_date_str(self._last_update),
}

# completion of runs is not decided by the execution as there may be
# multiple executions for a single run (e.g. mpi)
if self._state != "completed":
# Completion of runs is decided by the API runs monitoring as there may be
# multiple executions for a single run (e.g. mpi).
# For kinds that are not monitored by the API (local) we allow changing the state.
run_kind = self.labels.get(mlrun_constants.MLRunInternalLabels.kind, "")
if (
mlrun.runtimes.RuntimeKinds.is_local_runtime(run_kind)
or self._state != "completed"
):
struct["status.state"] = self._state

if self.is_logging_worker():
Expand Down
5 changes: 5 additions & 0 deletions mlrun/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from mlrun_pipelines.common.models import RunStatuses
from mlrun_pipelines.common.ops import format_summary_from_kfp_run, show_kfp_run

import mlrun.common.constants as mlrun_constants
import mlrun.common.formatters
import mlrun.common.schemas
import mlrun.errors
Expand Down Expand Up @@ -292,6 +293,10 @@ def get_or_create_ctx(
newspec["metadata"]["project"] = (
newspec["metadata"].get("project") or project or mlconf.default_project
)
newspec["metadata"].setdefault("labels", {})
newspec["metadata"]["labels"] = {
mlrun_constants.MLRunInternalLabels.kind: RuntimeKinds.local
}

ctx = MLClientCtx.from_dict(
newspec, rundb=out, autocommit=autocommit, tmp=tmp, host=socket.gethostname()
Expand Down
22 changes: 15 additions & 7 deletions tests/integration/sdk_api/run/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,8 @@ def test_ctx_run_labels(self):
)
assert len(runs) == 1

# remove the host label
assert mlrun_constants.MLRunInternalLabels.host in runs[0]["metadata"]["labels"]
del runs[0]["metadata"]["labels"][mlrun_constants.MLRunInternalLabels.host]
_remove_internal_labels(runs)

assert runs[0]["metadata"]["labels"] == {}

ctx.set_label("label-key", "label-value")
Expand All @@ -76,8 +75,9 @@ def test_ctx_run_labels(self):
name=ctx_name, project=mlrun.mlconf.default_project
)
assert len(runs) == 1
assert mlrun_constants.MLRunInternalLabels.host in runs[0]["metadata"]["labels"]
del runs[0]["metadata"]["labels"][mlrun_constants.MLRunInternalLabels.host]

_remove_internal_labels(runs)

assert runs[0]["metadata"]["labels"] == {"label-key": "label-value"}

# mock not logging worker
Expand All @@ -91,6 +91,14 @@ def test_ctx_run_labels(self):
name=ctx_name, project=mlrun.mlconf.default_project
)
assert len(runs) == 1
assert mlrun_constants.MLRunInternalLabels.host in runs[0]["metadata"]["labels"]
del runs[0]["metadata"]["labels"][mlrun_constants.MLRunInternalLabels.host]

_remove_internal_labels(runs)

assert runs[0]["metadata"]["labels"] == {"label-key": "label-value"}


def _remove_internal_labels(runs):
assert mlrun_constants.MLRunInternalLabels.host in runs[0]["metadata"]["labels"]
del runs[0]["metadata"]["labels"][mlrun_constants.MLRunInternalLabels.host]
assert mlrun_constants.MLRunInternalLabels.kind in runs[0]["metadata"]["labels"]
del runs[0]["metadata"]["labels"][mlrun_constants.MLRunInternalLabels.kind]
8 changes: 8 additions & 0 deletions tests/run/assets/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import mlrun


def handler(context, x=1):
context.log_result("y", x * 2)


def get_ctx_kind_label():
ctx = mlrun.get_or_create_ctx("func-func")
return ctx.labels.get("kind")
25 changes: 25 additions & 0 deletions tests/run/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,28 @@ def test_args_integrity():
verify_state(result)

assert output.find("It's, a, nice, day!") != -1, "params not detected in argv"


def test_get_or_create_ctx_run_kind():
# varify the default run kind is local
context = mlrun.get_or_create_ctx("ctx")
assert context.labels.get("kind") == "local"
assert context.state == "running"
context.commit(completed=True)
assert context.state == "completed"


def test_get_or_create_ctx_run_kind_local_from_function():
project = mlrun.get_or_create_project("dummy-project")
project.set_function(
name="func",
func=f"{assets_path}/simple.py",
handler="get_ctx_kind_label",
image="mlrun/mlrun",
)
run = project.run_function(
"func",
local=True,
)
assert run.state() == "completed"
assert run.output("return") == "local"
4 changes: 2 additions & 2 deletions tests/test_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ def test_local_context(rundb_mock):

run = db.read_run(context._uid, project=project_name)

# run state should not be updated by the context
assert run["status"]["state"] == "running", "run status was updated in db"
# run state should be updated by the context for local run
assert run["status"]["state"] == "completed", "run status was not updated in db"
assert (
run["status"]["artifacts"][0]["metadata"]["key"] == "xx"
), "artifact not updated in db"
Expand Down

0 comments on commit aa3d12d

Please sign in to comment.