/
steelyx.clj
398 lines (364 loc) · 15.2 KB
/
steelyx.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
(ns steelyx
(:import
[java.util UUID])
(:require
[clojure.core.async :as async :refer [go <!]]
[clojure.tools.logging :as log]
[arctype.service.protocol :refer :all]
[arctype.service.util :refer [<?? thread-try]]
[arctype.service.curator :as curator]
[arctype.service.quartzite :as quartzite]
[cheshire.core :as json]
[compojure.core :as compojure-core]
[compojure.api.sweet :as compojure-api :refer [context GET POST ANY]]
[muuntaja.core :as muuntaja]
[onyx.api :as onyx-api]
[onyx.extensions :as extensions]
[onyx.log.zookeeper :as zk-log]
[schema.core :as S]
[sundbry.resource :as resource :refer [with-resources]]
[steelyx.engine :as onyx-engine]
[steelyx.janitor :as janitor]
[steelyx.schema :refer :all]))
(def Config
{:onyx onyx-engine/Config
:curator curator/Config
:jobs {S/Keyword S/Any}
(S/optional-key :janitor) janitor/Config})
(def ^:private default-config
{:janitor {}})
(def ^:private api-info
{:version "0.2" :title "Steelyx API" :description "Onyx service engine"})
(defn- maybe-int
[x]
(when (integer? x) x))
(defn handle-internal-exception
[^Exception ex data request]
(let [data (condp = (class ex)
org.httpkit.client.TimeoutException (assoc data :http-status 504)
data)
response {:status (or (:http-status data) (maybe-int (:status data)) 500)
:headers (merge {"Access-Control-Allow-Origin" "*"} (:http-headers data))
:body (or (:http-body data) {:message (str "Service error: " (.getMessage ex))})}]
(when (= 500 (:status response))
(log/error ex {:message (str "Internal server error:" (.getMessage ex))
:data data}))
response))
(defn handle-validation-exception
[^Exception ex data request]
(log/error {:message "Request schema error"
:data (select-keys data [:schema :value :errors])})
(let [error-type (case (:type data)
:compojure.api.exception/request-validation "request "
:compojure.api.exception/response-validation "response "
"")]
{:status 400
:headers {"Access-Control-Allow-Origin" "*"}
:body {:message (str "Server " error-type "schema error: " (print-str (:error data)))}}))
(defn- response-str
[response]
(update response :body str))
(defn- sanitize-request
[req]
(-> req
(update :headers
(fn [headers]
(cond-> headers
(some? (get headers "authorization"))
(assoc "authorization" "<redacted>"))))))
(defn wrap-tracing
[handler]
(fn [req]
(let [request-id (str (UUID/randomUUID))
nano-time (System/nanoTime)
req (assoc req :request-id request-id)]
(log/trace {:message "API request"
:request-id request-id
:request (sanitize-request req)})
(let [response (handler req)
dt (- (System/nanoTime) nano-time)]
(log/trace {:message "API response"
:request-id request-id
:elapsed (/ (double dt) 1e9)
:response (response-str response)})
response))))
(defn- check-error
[result]
(when-let [ex (:e result)]
(throw ex))
(when-not (:success? result)
(throw (ex-info "Onyx job submittal unknown error, check the logs." result)))
result)
(S/defn ^:private submit-job! :- OnyxJob
[{:keys [state] :as this} job-builder job-config]
(with-resources this [:onyx]
(let [onyx-client (client onyx)
job-spec (job-builder (:job-context @state) job-config)
result (onyx-api/submit-job onyx-client job-spec)]
(check-error result)
(log/info {:message "Onyx job submitted"
:spec job-spec
:result result})
{:job-id (str (:job-id result))
:job-name (:job-name job-spec)})))
(S/defn ^:private resume-job! :- OnyxResumedJob
[{:keys [state] :as this}
{:keys [job-id job-name] :as params} :- ResumeJobParams
job-builder
job-config]
(with-resources this [:onyx]
(let [onyx-client (client onyx)
tenancy-id (onyx-engine/tenancy-id onyx)
new-job (job-builder (:job-context @state) job-config)
snapshot (if (some? job-id)
(onyx-api/job-snapshot-coordinates onyx-client tenancy-id job-id)
(onyx-api/named-job-snapshot-coordinates onyx-client tenancy-id job-name))
_ (when (nil? snapshot)
(throw (ex-info "Job snapshot not found" params)))
resume-point (onyx-api/build-resume-point new-job snapshot)
new-job (assoc new-job :resume-point resume-point)
result (onyx-api/submit-job onyx-client new-job)]
(check-error result)
(log/info {:message "Onyx job resumed"
:snapshot snapshot
:resume-point resume-point
:result result})
{:snapshot snapshot
:job-id (str (:job-id result))
:job-name (:job-name new-job)})))
(S/defn ^:private kill-job!
[this
{:keys [job-id job-name]} :- JobKillRequest]
(with-resources this [:onyx]
(let [job-id (or job-id (:job-id (last (onyx-api/job-ids-history (client onyx) (onyx-engine/tenancy-id onyx) job-name))))
result (onyx-engine/kill-job onyx job-id)]
(if result
(do
(log/info {:message "Job killed"
:job-name job-name
:job-id job-id
:result result})
{:job-id job-id})
(throw (ex-info "Failed to kill job" {:result result :job-name job-name :job-id job-id}))))))
(S/defn ^:private job-state
[{:keys [state] :as this} {:keys [job-id] :as params}]
(with-resources this [:onyx]
(let [onyx-client (client onyx)
tenancy-id (onyx-engine/tenancy-id onyx)
current-replica {:replica (:replica @state)
:alive? (zk-log/tenancy-alive? (:conn (:log onyx-client)) tenancy-id)}]
(onyx-api/job-state current-replica tenancy-id (UUID/fromString job-id)))))
(defn- submit-job-name!
[{:keys [config job-defs] :as this} {:keys [job-name] :as params}]
(if-let [job-def (get job-defs job-name)]
(let [job-config (get-in config [:jobs (keyword job-name)])]
(submit-job! this job-def job-config))
(throw (ex-info "Undefined job name" params))))
(defn- resume-job-name!
[{:keys [config job-defs] :as this}
{:keys [job-name] :as params}]
(if-let [job-def (get job-defs job-name)]
(let [job-config (get-in config [:jobs (keyword job-name)])]
(resume-job! this params job-def job-config))
(throw (ex-info "Undefined job name" params))))
(defn- job-ids
[{:keys [state]}]
(->> (select-keys (:replica @state) [:jobs :killed-jobs])
(vals)
(apply concat)))
(defn- job-index
[{:keys [state] :as this}]
(with-resources this [:onyx]
(let [onyx-client (client onyx)
tenancy-id (onyx-engine/tenancy-id onyx)
current-replica {:replica (:replica @state)
:alive? (zk-log/tenancy-alive? (:conn (:log onyx-client)) tenancy-id)}
jobs (->> (for [job-id (job-ids this)]
[job-id (assoc (onyx-api/job-state current-replica tenancy-id job-id)
:job-id job-id)])
(into {}))]
jobs)))
(defn- onyx-status
[{:keys [config state] :as this}]
(with-resources this [:onyx]
(let [onyx-client (client onyx)
tenancy-id (onyx-engine/tenancy-id onyx)
current-replica {:replica (:replica @state)
:alive? (zk-log/tenancy-alive? (:conn (:log onyx-client)) tenancy-id)}
jobs (->> (for [[job-name job-cfg] (:jobs config)]
(let [{:keys [job-id]} (last (onyx-api/job-ids-history onyx-client tenancy-id (name job-name)))
j-state (onyx-api/job-state current-replica tenancy-id job-id)]
[job-name (assoc j-state
:job-id job-id)]))
(into {}))]
{:jobs jobs})))
(defn- healthy-onyx-status?
[{:keys [jobs]}]
(->> (vals jobs)
(map (fn [{:keys [job-state]}]
(case job-state
:running true
false)))
(reduce #(and %1 %2) true)))
(defn- http-health-check
[this req]
(let [result (onyx-status this)
healthy? (healthy-onyx-status? result)]
(if healthy?
(log/info {:message "Health check OK"
:status result})
(log/warn {:message "Health check FAILED"
:status result}))
{:status (if healthy? 200 500)
:body result}))
(defn- deep-gc!
[this {:keys [force?]}]
(with-resources this [:onyx]
(if (or force? (healthy-onyx-status? (onyx-status this)))
(let [onyx-client (client onyx)
tenancy-id (onyx-engine/tenancy-id onyx)]
(log/info {:message "Performing checkpoint garbage collection"})
(doseq [job-id (job-ids this)]
(log/info (merge
{:message "Garbage collected checkpoints for job"
:tenancy-id tenancy-id
:job-id job-id}
(onyx-api/gc-checkpoints onyx-client tenancy-id job-id))))
(if (or force? (healthy-onyx-status? (onyx-status this)))
(do
(log/info {:message "Performing log garbage collection"})
(onyx-api/gc onyx-client)
(log/info {:message "Garbage collection complete"}))
(log/warn {:message "Log garbage collection blocked"})))
(log/warn {:message "Garbage collection blocked"}))))
(defn- handle-log-entry
[{:keys [state]} entry]
(swap! state update :replica
#(extensions/apply-log-entry entry (assoc % :version (:message-id entry)))))
(defn- handle-events
[{:keys [events-chan] :as this}]
(go
(try
(loop []
(when-let [event (<! events-chan)]
(log/debug {:message "Handle onyx event"
:event event})
(handle-log-entry this event)
(recur)))
(catch Throwable e
(log/fatal e {:message "Onyx event handler crashed!"})))
(log/debug {:message "Event handler stopped"})))
(defn- initial-state
[]
{:job-context nil
:replica nil})
(defn set-job-context!
"Set the context for job construction"
[{:keys [state]} context]
(swap! state assoc :job-context context))
(defrecord DataService [config state job-defs]
PLifecycle
(start [this]
(with-resources this [:onyx :janitor]
(log/info {:message "Starting Steelyx service"
:job-defs (keys job-defs)})
(reset! state (initial-state))
(janitor/set-cleaner! janitor #(deep-gc! this %))
(as-> this this
(assoc this :events-chan (async/chan))
(assoc this :sub (onyx-api/subscribe-to-log (client onyx) (:events-chan this)))
(do
(swap! state assoc :replica (:replica (:sub this)))
this)
(assoc this :events-handler (handle-events this)))))
(stop [this]
(log/info {:message "Stopping Steelyx service"})
(async/close! (:events-chan this))
(<?? (:events-handler this))
(dissoc this :events-chan :sub :events-handler))
PHttpHandler
(ring-handler [this]
(-> (compojure-api/api
{:formats muuntaja/default-options
:exceptions {:handlers {:compojure.api.exception/default handle-internal-exception
:compojure.api.exception/request-validation handle-validation-exception
:compojure.api.exception/response-validation handle-validation-exception}}
:swagger {:spec "/swagger.json"
:ui "/swagger"
:data {:info api-info}}}
(context "/" []
:dynamic true
(GET "/" []
{:status 200
:body (:title api-info)})
(GET "/health" req
:summary "Perform a health check"
(http-health-check this req))
(context "/job" []
(GET "/" req
:return S/Any ;JobIndex
:summary "Get job list"
{:status 200
:body (job-index this)})
(POST "/kill" req
:body [params JobKillRequest]
:return S/Any
:summary "Kill a job"
{:status 200
:body (kill-job! this params)})
(GET "/id/:job-id" req
:return JobState
:summary "Get job state"
{:status 200
:body (job-state this (:params req))})
(context "/:job-name" []
(POST "/start" req
:return OnyxJob
:summary "Submit a named job"
{:status 202
:body (submit-job-name! this (:params req))})
(POST "/resume" req
:body [params (S/maybe JobSelect)]
:return OnyxResumedJob
:summary "Resume a named job"
{:status 202
:body (resume-job-name! this (:params req))})
(POST "/kill" req
:return S/Any
:summary "Kill a named job"
{:status 200
:body (kill-job! this (:params req))})))
(POST "/gc" req
:summary "Garbage collect"
:body [params (S/maybe GcOptions)]
(with-resources this [:janitor]
(janitor/request-gc! janitor params)
{:status 201
:body nil}))))
wrap-tracing))
)
(defn- index-job-defs
[job-defs-seq]
(->> (seq job-defs-seq)
(map (fn [job-def]
(let [job-meta (meta job-def)
job-name (or (:job-name job-meta)
(throw (ex-info "Undefined job name" (or job-meta {}))))]
[job-name @job-def])))
(into {})))
(S/defn create
[resource-name
config :- Config
job-def-set]
(let [config (merge default-config config)]
(resource/make-resource
(map->DataService
{:config config
:state (atom {})
:job-defs (index-job-defs job-def-set)})
resource-name
#{}
[(curator/create :curator (:curator config))
(onyx-engine/create :onyx (:onyx config))
(janitor/create :janitor (:janitor config))
(quartzite/create :quartzite {})])))