-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhost.jl
215 lines (192 loc) · 6.56 KB
/
host.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# SPDX-License-Identifier: MPL-2.0
using DataStructures
import Base.getproperty
#const MSG_BUFFER_SIZE = 100_000
struct HostProfile{TInner <: CircoCore.Profiles.AbstractProfile} <: CircoCore.Profiles.AbstractProfile
innerprofile::TInner
options
HostProfile(innerprofile; options...) = new{typeof(innerprofile)}(innerprofile, options)
end
CircoCore.Profiles.core_plugins(p::HostProfile) = begin
retval = CircoCore.Profiles.core_plugins(p.innerprofile)
po_idx = findfirst(p -> p isa Type{<:Circo.Cluster.ClusterService}, retval) # Insert the HostService just before the ClusterService (Dirty way to get initialization order right)
if isnothing(po_idx)
po_idx = 1
end
insert!(retval, po_idx, HostService)
return retval
end
struct HostContext{TInner <: CircoCore.AbstractContext} <: CircoCore.AbstractContext
innerctx::TInner
options
profile
plugins
end
function HostContext(innerctx; hostoptions...)
options = merge(innerctx.options, hostoptions)
target_module = get(options, :target_module, Main)
profile = HostProfile(innerctx.profile; options...)
plugins = CircoCore.instantiate_plugins(profile, innerctx.userpluginsfn)
types = CircoCore.generate_types(plugins; target_module)
@assert types.corestate_type == innerctx.corestate_type
@assert types.msg_type == innerctx.msg_type
return HostContext(innerctx, options, profile, plugins)
end
Base.getproperty(ctx::HostContext, name::Symbol) = begin
if name == :options || name == :profile || name == :plugins || name == :innerctx
return getfield(ctx, name)
end
return getproperty(ctx.innerctx, name)
end
mutable struct HostActor{TCore} <: Actor{TCore}
core::TCore
end
Circo.monitorprojection(::Type{ <: HostActor }) = Circo.Monitor.JS("projections.nonimportant")
abstract type HostService <: Plugin end
mutable struct HostServiceImpl <: HostService
in_msg::Deque
in_lock::Threads.SpinLock
iamzygote::Bool
hostid::Int64
peercache::Dict{PostCode, HostServiceImpl}
tmp_msg::Vector{Any}
hostroot::PostCode
helper::Addr
postcode::PostCode
HostServiceImpl(;options...) = new(
Deque{Any}(),#get(options, :buffer_size, MSG_BUFFER_SIZE)
Threads.SpinLock(),
get(options, :iamzygote, false),
get(options, :hostid, 0),
Dict(),
[],
)
end
Plugins.symbol(::HostService) = :host
Circo.postcode(hs::HostService) = hs.postcode
function Circo.schedule_start(hs::HostServiceImpl, scheduler)
hs.postcode = postcode(scheduler)
hs.helper = spawn(scheduler.service, HostActor(emptycore(scheduler.service)))
cluster = scheduler.plugins[:cluster]
@async begin # TODO eliminate
if hs.hostroot != postcode(hs) && length(cluster.roots) == 0
forceadd_msg = Circo.Cluster.ForceAddRoot(hs.hostroot)
send(scheduler, cluster.helper, forceadd_msg)
end
end
end
@inline function CircoCore.remoteroutes(hostservice::HostServiceImpl, scheduler, msg)::Bool
target_postcode = postcode(target(msg))
if CircoCore.network_host(target_postcode) != CircoCore.network_host(hostservice.postcode)
return false
end
@debug "remoteroutes in host.jl $msg"
peer = get(hostservice.peercache, target_postcode, nothing)
if !isnothing(peer)
@debug "Inter-thread delivery of $(hostservice.postcode): $msg"
lock(peer.in_lock)
try
push!(peer.in_msg, msg)
finally
unlock(peer.in_lock)
end
return true
end
return false
end
@inline function CircoCore.letin_remote(hs::HostServiceImpl, scheduler)::Bool
isempty(hs.in_msg) && return false
tmp_msgs = hs.tmp_msg
lock(hs.in_lock)
try
for i = 1:min(length(hs.in_msg), 30)
push!(tmp_msgs, popfirst!(hs.in_msg))
#@debug "arrived at $(hs.postcode): $msg"
end
finally
unlock(hs.in_lock)
end
for msg in tmp_msgs # The lock must be released before delivering (hostroutes aquires the peer lock)
Circo.deliver!(scheduler, msg)
end
empty!(tmp_msgs)
return false
end
struct Host
schedulers::Vector{Circo.AbstractScheduler}
id::UInt64
end
function Host(ctx, threadcount::Int; zygote=[])
hostid = rand(UInt64)
schedulers = create_schedulers(ctx, threadcount; zygote=zygote)
return Host(schedulers, hostid)
end
Base.show(io::IO, ::MIME"text/plain", host::Host) = begin
print(io, "Circo.Host with $(length(host.schedulers)) schedulers")
end
function create_schedulers(ctx, threadcount; zygote)
schedulers = []
for i = 1:threadcount
iamzygote = i == 1
myzygote = iamzygote ? zygote : []
sdl_ctx = HostContext(ctx; iamzygote = iamzygote)
scheduler = CircoCore.Scheduler(sdl_ctx, myzygote)
push!(schedulers, scheduler)
end
return schedulers
end
function crossadd_peers(schedulers)
hostroot = postcode(schedulers[1])
for scheduler in schedulers
hs = scheduler.plugins[:host]
hs.hostroot = hostroot
for peer_scheduler in schedulers
if postcode(peer_scheduler) != postcode(scheduler)
hs.peercache[postcode(peer_scheduler)] = peer_scheduler.plugins[:host]
end
end
end
end
Circo.send(host::Host, to::Actor, msgbody; kwargs...) = send(host, addr(to), msgbody; kwargs...)
CircoCore.send(host::Host, target::Addr, msgbody; kwargs...) = CircoCore.send(host.schedulers[1], target, msgbody; kwargs...)
# From https://discourse.julialang.org/t/lightweight-tasks-julia-vs-elixir-otp/35082/22
function onthread(f, id::Int)
t = Task(nothing)
@assert id in 1:Threads.nthreads() "thread $id not available!"
Threads.@threads for i in 1:Threads.nthreads()
if i == id
t = @async f()
end
end
return t
end
function (ts::Host)(;remote=true, first_threadidx=2)
crossadd_peers(ts.schedulers) # TODO only once
tasks = []
next_threadid = min(Threads.nthreads(), first_threadidx)
for scheduler in ts.schedulers
t = onthread(next_threadid) do
try
scheduler(;remote=remote)
catch e
@show e
end
end
push!(tasks, t)
next_threadid = next_threadid == Threads.nthreads() ? 1 : next_threadid + 1
end
for task in tasks
wait(task)
end
return nothing
end
function Circo.shutdown!(host::Host)
for scheduler in host.schedulers
CircoCore.shutdown!(scheduler)
end
end
function Circo.pause!(host::Host)
for scheduler in host.schedulers
CircoCore.pause!(scheduler)
end
end