diff --git a/stdlib/Distributed/src/cluster.jl b/stdlib/Distributed/src/cluster.jl index 3caf002b3cd30..47b2fc2f3a082 100644 --- a/stdlib/Distributed/src/cluster.jl +++ b/stdlib/Distributed/src/cluster.jl @@ -361,6 +361,8 @@ process as a worker using TCP/IP sockets for transport. `cookie` is a [`cluster_cookie`](@ref). """ function init_worker(cookie::AbstractString, manager::ClusterManager=DefaultClusterManager()) + myrole!(:worker) + # On workers, the default cluster manager connects via TCP sockets. Custom # transports will need to call this function with their own manager. global cluster_manager @@ -783,12 +785,19 @@ end # globals const LPROC = LocalProcess() +const LPROCROLE = Ref{Symbol}(:master) const HDR_VERSION_LEN=16 const HDR_COOKIE_LEN=16 const map_pid_wrkr = Dict{Int, Union{Worker, LocalProcess}}() const map_sock_wrkr = IdDict() const map_del_wrkr = Set{Int}() +# whether process is a master or worker in a distributed setup +myrole() = LPROCROLE[] +function myrole!(proctype::Symbol) + LPROCROLE[] = proctype +end + # cluster management related API """ myid() @@ -1108,7 +1117,7 @@ function deregister_worker(pg, pid) end end - if myid() == 1 && isdefined(w, :config) + if myid() == 1 && (myrole() === :master) && isdefined(w, :config) # Notify the cluster manager of this workers death manage(w.manager, w.id, w.config, :deregister) if PGRP.topology != :all_to_all || isclusterlazy() diff --git a/stdlib/Distributed/test/distributed_exec.jl b/stdlib/Distributed/test/distributed_exec.jl index b76a9414cd558..737381869851d 100644 --- a/stdlib/Distributed/test/distributed_exec.jl +++ b/stdlib/Distributed/test/distributed_exec.jl @@ -45,6 +45,16 @@ end id_me = myid() id_other = filter(x -> x != id_me, procs())[rand(1:(nprocs()-1))] +# Test role +@everywhere using Distributed +@test Distributed.myrole() === :master +for wid = workers() + wrole = remotecall_fetch(wid) do + Distributed.myrole() + end + @test wrole === :worker +end + # Test remote() let pool = default_worker_pool()