-
Notifications
You must be signed in to change notification settings - Fork 3
/
core.clj
418 lines (366 loc) · 17.6 KB
/
core.clj
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
(ns papaline.core
(:require [clojure.core.async :as a
:refer :all
:exclude [partition-by map into reduce partition take merge
pipeline transduce]]
[papaline.util :refer [defprotocol+ defrecord+]])
(:import [papaline.concurrent ArgumentsAwareCallable]
[java.util.concurrent ExecutorService TimeUnit TimeoutException Callable
ThreadPoolExecutor LinkedBlockingQueue RejectedExecutionHandler
ThreadPoolExecutor$DiscardOldestPolicy ThreadFactory Future CompletableFuture CompletionException]
(java.util.function BiFunction)))
(defrecord Stage [buffer-factory stage-fn]
clojure.lang.IFn
(invoke [this args]
(apply stage-fn args)))
(defrecord RealizedStage [buffer stage-fn name]
clojure.lang.IFn
(applyTo [this args]
(apply stage-fn args)))
(defn start-stage [^Stage s]
(RealizedStage. ((.buffer-factory s)) (.stage-fn s) (:name s)))
(defn stage [stage-fn & {:keys [in-chan
buffer-size
buffer-type]
:or {buffer-size 100}}]
(let [buffer-fn (case buffer-type
:sliding sliding-buffer
:dropping dropping-buffer
buffer)
buffer-factory #(chan (buffer-fn buffer-size))]
(Stage. buffer-factory stage-fn)))
(defn named-stage [name & args]
(assoc (apply stage args) :name name))
(defn copy-stage [stage-fn & options]
(let [sfn (fn [& args]
(apply stage-fn args)
args)]
(apply stage sfn options)))
(def ^:dynamic *pre-execution-hook* nil)
(def ^:dynamic *post-execution-hook* nil)
(defprotocol+ IPipeline
(start! [this])
(run-pipeline [this & args])
(run-pipeline-wait [this & args])
(run-pipeline-timeout [this timeout-interval timeout-val & args])
(stop! [this]))
(defn- process-exception [ctx args e stage-name error-handler]
(let [cause (if (instance? CompletionException e) (.getCause ^Throwable e) e)]
(if (and (instance? clojure.lang.ExceptionInfo cause)
(:abort (ex-data cause)))
(merge ctx (ex-data cause))
(let [ex (ex-info "Papaline stage error"
{:args args
:stage stage-name} e)]
(when error-handler
(error-handler ex))
(assoc ctx :ex ex)))))
(defn- run-task [^RealizedStage current-stage ctx error-handler]
(let [task (.stage-fn current-stage)
args (:args ctx)
args (if (or (nil? args) ;; empty arguments
(sequential? args))
args [args])]
(try
(assoc ctx :args (apply task args))
(catch Exception e
(process-exception ctx args e (:name current-stage) error-handler)))))
(defrecord+ Pipeline [done-chan stages error-handler]
clojure.lang.IFn
(invoke [this ctx]
(let [entry (.buffer ^RealizedStage (first stages))]
(go
(when *pre-execution-hook*
(*pre-execution-hook* ctx))
(>! entry (assoc ctx :post-hook *post-execution-hook*)))))
IPipeline
(start! [this]
(loop [stages* stages]
(when (first stages*)
(let [^RealizedStage current-stage (first stages*)
in-chan (.buffer current-stage)
out-chan (when (second stages*) (.buffer ^RealizedStage (second stages*)))]
(go-loop []
(let [[ctx port] (alts! [done-chan in-chan] :priority true)]
(if (not= port done-chan)
(do
(go
(let [ctx (run-task current-stage ctx error-handler)]
(when (and (:error ctx) (:ex ctx))
(>! (:error ctx) (:ex ctx)))
(when-not out-chan
(when-let [post-hook (:post-hook ctx)]
(post-hook ctx)))
(when-let [out-chan (or out-chan (:wait ctx))]
(cond
(:abort ctx)
(do
(when (:wait ctx) (>! (:wait ctx) ctx))
(when-let [post-hook (:post-hook ctx)] (post-hook ctx)))
;; the results are forked
(:fork (meta (:args ctx)))
(let [ctx (-> ctx
(update-in [:forks]
#(conj (or % []) (count @(:args ctx))))
(update-in [:fork-rets]
#(conj (or % []) (atom []))))]
(doseq [forked-args @(:args ctx)]
(>! out-chan (assoc ctx :args forked-args))))
;; this tasks requires join
(:join (meta (:args ctx)))
(let [fork-rets (swap! (last (:fork-rets ctx)) conj @(:args ctx))]
(when (= (last (:forks ctx)) (count fork-rets))
(>! out-chan (assoc ctx
:args fork-rets
:forks (vec (drop-last (:forks ctx)))
:fork-rets (vec (drop-last (:fork-rets ctx)))))))
;; normal linear
:else (>! out-chan ctx)))))
(recur))
(close! in-chan))))
(recur (rest stages*))))))
(run-pipeline [this & args]
(this {:args args}))
(run-pipeline-wait [this & args]
(let [sync-chan (chan)
error-chan (chan)]
(this {:args args
:wait sync-chan
:error error-chan})
(let [[result port] (alts!! [done-chan error-chan sync-chan] :priority true)]
(condp = port
sync-chan (:args result)
error-chan (throw result)
done-chan (throw (ex-info "Pipeline closed"))))))
(run-pipeline-timeout [this timeout-interval timeout-val & args]
(let [sync-chan (chan)
error-chan (chan)
timeout-chan (timeout timeout-interval)]
(this {:args args
:wait sync-chan
:error error-chan})
(let [[result port] (alts!! [done-chan timeout-chan error-chan sync-chan] :priority true)]
(condp = port
done-chan (throw (ex-info "Pipeline closed"))
sync-chan (:args result)
error-chan (throw result)
timeout-chan timeout-val))))
(stop! [this]
(>!! done-chan 0)))
(defn- ^Callable wrap-with-arguments-aware-callable [callable args-array]
(ArgumentsAwareCallable. ^Callable callable ^"[Ljava.lang.Object;" args-array))
(defrecord+ DedicatedThreadPoolPipeline [executor stages error-handler]
clojure.lang.IFn
(invoke [this ctx]
(let [pre-hook *pre-execution-hook*
post-hook *post-execution-hook*
clos (fn []
(when pre-hook (pre-hook ctx))
(loop [stgs stages ctx ctx]
(if-let [s (first stgs)]
(let [ctx (run-task s ctx error-handler)]
(cond
(:abort ctx)
(do
(when post-hook (post-hook ctx))
ctx)
(:fork (meta (:args ctx)))
(when error-handler
(error-handler (UnsupportedOperationException. "Fork is not supported in DedicatedThreadPoolPipeline")))
(:join (meta (:args ctx)))
(when error-handler
(error-handler (UnsupportedOperationException. "Join is not supported in DedicatedThreadPoolPipeline")))
:else (recur (rest stgs) ctx)))
(do
(when post-hook (post-hook ctx))
ctx))))
clos-wrapper (wrap-with-arguments-aware-callable clos
(if (not-empty (:args ctx))
(.toArray ^clojure.lang.ArraySeq (:args ctx))
(into-array [])))]
(.submit ^ExecutorService executor clos-wrapper)))
IPipeline
(start! [this])
(run-pipeline [this & args]
(this {:args args}))
(run-pipeline-wait [this & args]
(let [^Future future (this {:args args})]
(:args (.get future))))
(run-pipeline-timeout [this timeout-interval timeout-val & args]
(let [^Future future (this {:args args})]
(try
(:args (.get future timeout-interval TimeUnit/MILLISECONDS))
(catch TimeoutException e
timeout-val))))
(stop! [this]))
(defn- run-task2 [^RealizedStage current-stage ctx error-handler]
(let [task (.stage-fn current-stage)
args (:args ctx)
args (if (or (nil? args) ;; empty arguments
(sequential? args))
args [args])]
(try
(let [next-args (apply task args)]
[(if (instance? CompletableFuture next-args)
next-args
(CompletableFuture/completedFuture next-args))
ctx])
(catch Exception e
(let [new-ctx (process-exception ctx args e (:name current-stage) error-handler)]
[(CompletableFuture/completedFuture (:args new-ctx))
new-ctx])))))
(defrecord+ AsyncDedicatedThreadPoolPipeline [executor stages error-handler]
clojure.lang.IFn
(invoke [this ctx]
(let [pre-hook *pre-execution-hook*
post-hook *post-execution-hook*
result (CompletableFuture.)
args-array (if (not-empty (:args ctx))
(.toArray ^clojure.lang.ArraySeq (:args ctx))
(into-array []))]
(letfn [(clos [stgs ctx]
(if-let [^RealizedStage s (first stgs)]
(let [[^CompletableFuture next-args-future new-ctx] (run-task2 s ctx error-handler)
processor (Thread/currentThread)]
(.handle ^CompletableFuture next-args-future
(reify BiFunction
(apply [_ next-args ex]
(if (= processor (Thread/currentThread))
(process-result (assoc new-ctx :args next-args) stgs)
(.submit ^ExecutorService executor
(wrap-with-arguments-aware-callable
(fn []
(let [ctx (if (some? ex)
(process-exception new-ctx (:args new-ctx) ex (:name s) error-handler)
(assoc new-ctx :args next-args))]
(process-result ctx stgs)))
args-array)))))))
(do
(when post-hook (post-hook ctx))
(.complete result ctx))))
(process-result [ctx stgs]
(let [args-meta (meta (:args ctx))
use-fork-or-join? (or (:fork args-meta) (:join args-meta))]
(if (or (:abort ctx) use-fork-or-join?)
(do
(when post-hook (post-hook ctx))
(when (and error-handler use-fork-or-join?)
(try
(error-handler (UnsupportedOperationException. "Fork or Join is not supported in AsyncDedicatedThreadPoolPipeline"))
(catch Exception _)))
(.complete result ctx))
(clos (rest stgs) ctx))))]
(.submit ^ExecutorService executor (wrap-with-arguments-aware-callable (fn []
(when pre-hook (pre-hook ctx))
(clos stages ctx))
args-array)))
result))
IPipeline
(start! [this])
(run-pipeline [this & args]
(this {:args args}))
(run-pipeline-wait [this & args]
(let [future (this {:args args})]
(:args (.get ^Future future))))
(run-pipeline-timeout [this timeout-interval timeout-val & args]
(let [future (this {:args args})]
(try
(:args (.get ^Future future timeout-interval TimeUnit/MILLISECONDS))
(catch TimeoutException e
timeout-val))))
(stop! [this]))
(defn pipeline [stages & {:keys [error-handler]}]
(let [realized-stages (mapv start-stage stages)
done-chan (chan)]
(doto (Pipeline. done-chan realized-stages error-handler)
(start!))))
(defn pipeline-stage
"pipeline as a stage"
[pipeline]
(let [stages (.stages ^Pipeline pipeline)
in-chan (.buffer ^RealizedStage (first stages))]
(stage (fn [& args]
(apply run-pipeline-wait pipeline args))
:in-chan in-chan)))
(defn cancel-pipeline [pipeline]
(stop! pipeline))
(defn counted-thread-factory
[name-format daemon]
(let [counter (atom 0)]
(reify
ThreadFactory
(newThread [this runnable]
(doto (Thread. runnable)
(.setName (format name-format (swap! counter inc)))
(.setDaemon daemon))))))
(defn make-thread-pool [threads queue-size & {:keys [overflow-action]
:or {overflow-action (ThreadPoolExecutor$DiscardOldestPolicy.)}}]
(ThreadPoolExecutor. (int threads) (int threads) (long 0)
TimeUnit/MILLISECONDS
(LinkedBlockingQueue. ^long queue-size)
(counted-thread-factory "papaline-pool-%d" true)
^RejectedExecutionHandler overflow-action))
(defn dedicated-thread-pool-pipeline [stages thread-pool & {:keys [error-handler]}]
(DedicatedThreadPoolPipeline. thread-pool (mapv start-stage stages) error-handler))
(defn async-dedicated-thread-pool-pipeline [stages thread-pool & {:keys [error-handler]}]
(AsyncDedicatedThreadPoolPipeline. thread-pool (mapv start-stage stages) error-handler))
(defrecord+ CompoundPipeline [pipelines]
clojure.lang.IFn
(invoke [this ctx]
(loop [ps pipelines ctx ctx]
(if-let [p (first ps)]
;; FIXME: do not block here
(let [ctx (apply run-pipeline-wait p (:args ctx))]
(cond
(:abort ctx) ctx
(:ex ctx) ctx
(:fork (meta (:args ctx)))
(when-let [error-handler (:error-handler p)]
(error-handler (UnsupportedOperationException.
"Fork is not supported for now")))
(:join (meta (:args ctx)))
(when-let [error-handler (:error-handler p)]
(error-handler (UnsupportedOperationException.
"Join is not supported for now ")))
:else (recur (rest ps) ctx)))
ctx)))
IPipeline
(start! [this])
(run-pipeline [this & args]
(this {:args args}))
(run-pipeline-wait [this & args]
(let [^Future future (this {:args args})]
(:args (.get future))))
(run-pipeline-timeout [this timeout-interval timeout-val & args]
(let [^Future future (this {:args args})]
(try
(:args (.get future timeout-interval TimeUnit/MILLISECONDS))
(catch TimeoutException e
timeout-val))))
(stop! [this]))
(defn compound-pipeline [pipelines]
(CompoundPipeline. pipelines))
(deftype MetadataObj [val meta-map-wrapper]
clojure.lang.IDeref
(deref [this]
val)
clojure.lang.IObj
(withMeta [this m]
(swap! meta-map-wrapper merge m)
this)
clojure.lang.IMeta
(meta [this]
@meta-map-wrapper))
(defn- meta-obj [v]
(MetadataObj. v (atom {})))
(defn- assoc-meta [v & args]
(with-meta v (apply assoc (or (meta v) {}) args)))
(defn abort
([] (throw (ex-info "Aborted" {:abort true})))
([ret] (throw (ex-info "Aborted" {:abort true :args ret}))))
(defn fork [ret]
(when-not (sequential? ret)
(throw (IllegalArgumentException. "Only sequential value is forkable.")))
(assoc-meta (meta-obj ret) :fork true))
(defn join [ret]
(assoc-meta (meta-obj ret) :join true))