@@ -19,16 +19,15 @@ function tid_to_uid(thunk_id)
19
19
end
20
20
function Base. put! (store:: StreamStore{T,B} , value) where {T,B}
21
21
thunk_id = STREAM_THUNK_ID[]
22
- uid = tid_to_uid (thunk_id)
23
22
@lock store. lock begin
24
23
if ! isopen (store)
25
- @dagdebug thunk_id :stream " [ $uid ] closed!"
24
+ @dagdebug thunk_id :stream " closed!"
26
25
throw (InvalidStateException (" Stream is closed" , :closed ))
27
26
end
28
- @dagdebug thunk_id :stream " [ $uid ] adding $value "
27
+ @dagdebug thunk_id :stream " adding $value "
29
28
for buffer in values (store. buffers)
30
29
while isfull (buffer)
31
- @dagdebug thunk_id :stream " [ $uid ] buffer full, waiting"
30
+ @dagdebug thunk_id :stream " buffer full, waiting"
32
31
wait (store. lock)
33
32
end
34
33
put! (buffer, value)
@@ -38,16 +37,15 @@ function Base.put!(store::StreamStore{T,B}, value) where {T,B}
38
37
end
39
38
function Base. take! (store:: StreamStore , id:: UInt )
40
39
thunk_id = STREAM_THUNK_ID[]
41
- uid = tid_to_uid (thunk_id)
42
40
@lock store. lock begin
43
41
buffer = store. buffers[id]
44
42
while isempty (buffer) && isopen (store, id)
45
- @dagdebug thunk_id :stream " [ $uid ] no elements, not taking"
43
+ @dagdebug thunk_id :stream " no elements, not taking"
46
44
wait (store. lock)
47
45
end
48
- @dagdebug thunk_id :stream " [ $uid ] wait finished"
46
+ @dagdebug thunk_id :stream " wait finished"
49
47
if ! isopen (store, id)
50
- @dagdebug thunk_id :stream " [ $uid ] closed!"
48
+ @dagdebug thunk_id :stream " closed!"
51
49
throw (InvalidStateException (" Stream is closed" , :closed ))
52
50
end
53
51
unlock (store. lock)
@@ -56,7 +54,7 @@ function Base.take!(store::StreamStore, id::UInt)
56
54
finally
57
55
lock (store. lock)
58
56
end
59
- @dagdebug thunk_id :stream " [ $uid ] value accepted"
57
+ @dagdebug thunk_id :stream " value accepted"
60
58
notify (store. lock)
61
59
return value
62
60
end
@@ -129,46 +127,53 @@ function Base.take!(stream::Stream{T,B}, id::UInt) where {T,B}
129
127
return take! (stream. input_buffer)
130
128
end
131
129
function Base. isopen (stream:: Stream , id:: UInt ):: Bool
132
- return remotecall_fetch (stream. store_ref. handle. owner, stream . store_ref . handle ) do ref
133
- return isopen (MemPool . poolget (ref) :: StreamStore , id)
130
+ return MemPool . access_ref (stream. store_ref. handle, id ) do store, id
131
+ return isopen (store :: StreamStore , id)
134
132
end
135
133
end
136
134
function Base. close (stream:: Stream )
137
- remotecall_wait (stream. store_ref. handle. owner, stream. store_ref. handle) do ref
138
- close (MemPool. poolget (ref):: StreamStore )
135
+ MemPool. access_ref (stream. store_ref. handle) do store
136
+ close (store:: StreamStore )
137
+ return
139
138
end
139
+ return
140
140
end
141
141
function add_waiters! (stream:: Stream , waiters:: Vector{Int} )
142
- remotecall_wait (stream. store_ref. handle. owner, stream. store_ref. handle) do ref
143
- add_waiters! (MemPool. poolget (ref):: StreamStore , waiters)
142
+ MemPool. access_ref (stream. store_ref. handle, waiters) do store, waiters
143
+ add_waiters! (store:: StreamStore , waiters)
144
+ return
144
145
end
146
+ return
145
147
end
146
148
add_waiters! (stream:: Stream , waiter:: Integer ) =
147
149
add_waiters! (stream:: Stream , Int[waiter])
148
150
function remove_waiters! (stream:: Stream , waiters:: Vector{Int} )
149
- remotecall_wait (stream. store_ref. handle. owner, stream. store_ref. handle) do ref
150
- remove_waiters! (MemPool. poolget (ref):: StreamStore , waiters)
151
+ MemPool. access_ref (stream. store_ref. handle, waiters) do store, waiters
152
+ remove_waiters! (store:: StreamStore , waiters)
153
+ return
151
154
end
155
+ return
152
156
end
153
157
remove_waiters! (stream:: Stream , waiter:: Integer ) =
154
158
remove_waiters! (stream:: Stream , Int[waiter])
155
159
156
160
function migrate_stream! (stream:: Stream , w:: Integer = myid ())
157
- if ! isdefined (MemPool, :migrate! )
158
- @warn " MemPool migration support not enabled!\n Performance may be degraded" maxlog= 1
159
- return
160
- end
161
-
162
161
# Perform migration of the StreamStore
163
162
# MemPool will block access to the new ref until the migration completes
163
+ # FIXME : Do this with MemPool.access_ref, in case stream was already migrated
164
164
if stream. store_ref. handle. owner != w
165
- # Take lock to prevent any further modifications
166
- # N.B. Serialization automatically unlocks
167
- remotecall_wait (stream. store_ref. handle. owner, stream. store_ref. handle) do ref
168
- lock ((MemPool. poolget (ref):: StreamStore ). lock)
165
+ new_store_ref = MemPool. migrate! (stream. store_ref. handle, w; pre_migration= store-> begin
166
+ # Lock store to prevent any further modifications
167
+ # N.B. Serialization automatically unlocks the migrated copy
168
+ lock ((store:: StreamStore ). lock)
169
+ end , post_migration= store-> begin
170
+ # Unlock the store
171
+ # FIXME : Indicate to all waiters that this store is dead
172
+ unlock ((store:: StreamStore ). lock)
173
+ end )
174
+ if w == myid ()
175
+ stream. store = MemPool. access_ref (identity, new_store_ref; local_only= true )
169
176
end
170
-
171
- MemPool. migrate! (stream. store_ref. handle, w)
172
177
end
173
178
end
174
179
@@ -272,6 +277,7 @@ function (sf::StreamingFunction)(args...; kwargs...)
272
277
# Migrate our output stream to this worker
273
278
if sf. stream isa Stream
274
279
migrate_stream! (sf. stream)
280
+ @dagdebug thunk_id :stream " Migration complete"
275
281
end
276
282
277
283
try
@@ -299,13 +305,13 @@ function (sf::StreamingFunction)(args...; kwargs...)
299
305
end
300
306
end
301
307
for stream in streams
302
- @dagdebug thunk_id :stream " [ $uid ] dropping waiter"
308
+ @dagdebug thunk_id :stream " dropping waiter"
303
309
remove_waiters! (stream, uid)
304
- @dagdebug thunk_id :stream " [ $uid ] dropped waiter"
310
+ @dagdebug thunk_id :stream " dropped waiter"
305
311
end
306
312
307
313
# Ensure downstream tasks also terminate
308
- @dagdebug thunk_id :stream " [ $uid ] closed stream"
314
+ @dagdebug thunk_id :stream " closed stream"
309
315
close (sf. stream)
310
316
end
311
317
end
0 commit comments