-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster_stats.clj
224 lines (192 loc) · 8 KB
/
cluster_stats.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
(ns lotuc.examples.akka.cluster-stats
(:require
[clojure.string :as s]
[lotuc.akka.actor.typed.actor-ref :as actor-ref]
[lotuc.akka.actor.typed.receptionist :as receptionist]
[lotuc.akka.actor.scaladsl :as dsl]
[lotuc.akka.cluster.member :as cluster.member]
[lotuc.akka.cluster.typed.cluster :as cluster]
[lotuc.akka.cluster.typed.cluster-singleton :as cluster-singleton]
[lotuc.akka.common.slf4j :refer [slf4j-log]]
[lotuc.akka.actor.typed.actor-system :as actor-system]))
(set! *warn-on-reflection* true)
;;; https://developer.lightbend.com/start/?group=akka&project=akka-samples-cluster-java
;;; stats
(defmacro info [ctx msg & args]
`(slf4j-log (dsl/log ~ctx) info ~msg ~@args))
(def stats-service-key "StatsService")
(defn- stats-worker*
[{:keys [context timers]}]
(info context "Worker starting up")
(dsl/start-timer timers :EvictCache {:timer-key :EvictCache
:timer-type :fix-rate
:interval "30.sec"})
(let [!cache (atom {})]
(dsl/receive-message
(fn [{:keys [action] :as m}]
(cond
(= m :EvictCache)
(do (reset! !cache {})
:same)
(= action :Process)
(let [{:keys [word ^akka.actor.typed.ActorRef reply-to]} m]
(info context "Worker processing request [{}]" word)
(let [length (get (swap! !cache update word (fn [v] (or v (count word)))) word)]
(.tell reply-to {:action :Processed :word word :length length}))
:same)
:else
(do (info context "Unhandled message {}" m)
:unhandled))))))
(defn stats-worker []
(dsl/setup
(fn [ctx]
(dsl/with-timers
(fn [timers]
(stats-worker* {:context ctx :timers timers}))))))
(defn stats-aggregator [words workers reply-to]
(dsl/setup
(fn [ctx]
(let [expected-responses (count words)
self (dsl/self ctx)
!results (atom [])]
(dsl/set-receive-timeout ctx "3.sec" :Timeout)
(doseq [word words]
(actor-ref/tell workers {:action :Process :word word :reply-to self}))
(dsl/receive-message
(fn [{:keys [action] :as m}]
(cond
(= m :Timeout)
(do (actor-ref/tell reply-to {:action :JobFailed :reason "Service unavailable, try again later"})
:same)
(= action :Processed)
(let [results (swap! !results conj (:length m))
result-size (count results)]
(if (= result-size expected-responses)
(let [sum (reduce + results)
mean-word-length (/ (double sum) result-size)]
(actor-ref/tell reply-to {:action :JobResult :mean-word-length mean-word-length})
:stopped)
:same))
:else
(do (info ctx "Unhandled message {}" m)
:unhandled))))))))
(defn stats-service [workers]
(dsl/receive
(fn [ctx {:keys [action] :as m}]
(cond
(= m :Stop)
:stopped
(= action :ProcessText)
(let [{:keys [text reply-to]} m
words (s/split text #" ")]
(dsl/spawn-anonymous ctx (stats-aggregator words workers reply-to))
:same)
:else
(do (info ctx "Unhandled message {}" m)
:unhandled)))))
(defn- stats-service-client*
[service {:keys [context timers]}]
(dsl/start-timer timers :Tick {:timer-key :Tick
:timer-type :fix-rate
:interval "2.sec"})
(dsl/receive-message
(fn [{:keys [action] :as m}]
(cond
(= m :Tick)
(do (info context "Sending process request")
(actor-ref/tell service {:action :ProcessText
:text "this is the text that will be analyzed"
:reply-to (dsl/self context)})
:same)
(= action :JobResult)
(do (info context "Service result: {}" (:mean-word-length m))
:same)
:else
(do (info context "Unhandled message: {}" m)
:unhandled)))))
(defn stats-service-client [service]
(dsl/setup
(fn [ctx]
(dsl/with-timers
(fn [timers]
(stats-service-client* service {:context ctx :timers timers}))))))
;;; corresponds to original example's App.java
(defn root-behavior []
(dsl/setup
(fn [ctx]
(let [system (dsl/system ctx)
cluster (cluster/get-cluster system)
self-member (.selfMember cluster)]
(cond
(cluster.member/has-role self-member "compute")
(let [number-of-workers
(.. system settings config
(getInt "stats-service.workers-per-node"))
worker-pool-behavior
(-> (dsl/pool-router (stats-worker) number-of-workers)
(dsl/pool-with-consistent-hashing-routing 1 :word))
workers (dsl/spawn ctx worker-pool-behavior "WorkerRouter")
service (dsl/spawn ctx (stats-service workers) "StatsService")]
(actor-ref/tell (.receptionist system)
(receptionist/register-service stats-service-key service))
:empty)
(.hasRole self-member "client")
(let [service-router (dsl/spawn ctx (dsl/group-router stats-service-key) "ServiceRouter")]
(dsl/spawn ctx (stats-service-client service-router) "Client")
:empty))))))
(def worker-service-key "Worker")
;;; corresponds to original example's AppOneMaster.java
(defn root-behavior-one-master []
(dsl/setup
(fn [ctx]
(let [system (dsl/system ctx)
cluster (cluster/get-cluster system)
service-behavior (dsl/setup
(fn [singleton-ctx]
(let [worker-group-behavior
(-> (dsl/group-router worker-service-key)
(dsl/group-with-consistent-hashing-routing 1 :word))
workers-router
(dsl/spawn singleton-ctx worker-group-behavior "WorkersRouter")]
(stats-service workers-router))))
service-singleton (->> {:stop-message :Stop
:settings (cluster-singleton/create-cluster-singleton-setting system {:role "compute"})}
(cluster-singleton/singleton-actor-of service-behavior "StatsService"))
service-proxy (-> (cluster-singleton/get-cluster-singleton system)
(.init service-singleton))
self-member (.selfMember cluster)]
(cond
(cluster.member/has-role self-member "compute")
(let [number-of-workers (.. system settings config
(getInt "stats-service.workers-per-node"))]
(info ctx "Starting {} workers" number-of-workers)
(doseq [i (range 4)]
(let [worker (dsl/spawn ctx (stats-worker) (str "StatsWorker" i))]
(actor-ref/tell (.receptionist system)
(receptionist/register-service worker-service-key worker))))
:empty)
(cluster.member/has-role self-member "client")
(do (dsl/spawn ctx (stats-service-client service-proxy) "Client")
:empty)
:else
:empty)))))
(defn startup [behavior role port]
(actor-system/create-system-from-config
behavior
"ClusterSystem"
"cluster-application.conf"
{"akka.remote.artery.canonical.port" port
"stats-service.workers-per-node" 2
"akka.cluster.roles" [role]}))
(comment
(let [behavior (root-behavior-one-master)]
(def s0 (startup behavior "compute" 25251))
(def s1 (startup behavior "compute" 25252))
(def s2 (startup behavior "client" 0)))
(let [behavior (root-behavior)]
(def s0 (startup behavior "compute" 25251))
(def s1 (startup behavior "compute" 25252))
(def s2 (startup behavior "client" 0)))
(do (.terminate s0)
(.terminate s1)
(.terminate s2)))