Skip to content

Commit 44a8af7

Browse files
Merge pull request #118 from justinstoller/parallelize-creation
(PE-37376) Initialize one JRuby instance first when filling a pool
2 parents 23d2d63 + ef6a947 commit 44a8af7

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

src/clj/puppetlabs/services/jruby_pool_manager/impl/jruby_agents.clj

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,25 +70,29 @@
7070

7171
(schema/defn ^:always-validate
7272
prime-pool!
73-
"Sequentially fill the pool with new JRubyInstances. NOTE: this
74-
function should never be called except by the modify-instance-agent
75-
to create a pool's initial jruby instances."
73+
"Fill the pool with new JRubyInstances. Instantiates the first JRuby (Puppet
74+
will sometimes alter the filesystem on first instantiation) and the remaining
75+
instances in parallel. NOTE: this function should never be called except by
76+
the modify-instance-agent to create a pool's initial jruby instances."
7677
[{:keys [config] :as pool-context} :- jruby-schemas/PoolContext]
7778
(log/debug (format "%s\n%s"
7879
(i18n/trs "Initializing JRubyInstances with the following settings:")
7980
(ks/pprint-to-string config)))
8081
(let [pool (jruby-internal/get-pool pool-context)
8182
creation-service (jruby-internal/get-creation-service pool-context)
8283
total (.remainingCapacity pool)
83-
ids (->> total range (map inc))
84+
[first-id & ids] (->> total range (map inc))
8485
add-instance* (fn [id]
8586
(log/debug (i18n/trs "Priming JRubyInstance {0} of {1}"
8687
id count))
8788
(add-instance pool-context id)
8889
(log/info (i18n/trs "Finished creating JRubyInstance {0} of {1}"
8990
id count)))
91+
initial-task (fn [] (add-instance* first-id))
9092
tasks (for [id ids] (fn [] (add-instance* id)))]
91-
(execute-tasks! tasks creation-service)))
93+
(execute-tasks! [initial-task] creation-service)
94+
(when (seq ids)
95+
(execute-tasks! tasks creation-service))))
9296

9397
(schema/defn ^:always-validate
9498
flush-instance!
@@ -179,9 +183,13 @@
179183
(throw (IllegalStateException.
180184
(i18n/trs "There was a problem creating a JRubyInstance for the pool.")
181185
e)))))
182-
cleanup-and-refill-tasks (for [[old-instance new-id] (zipmap old-instances new-instance-ids)]
186+
[[first-old-inst first-new-id] & remaining] (zipmap old-instances new-instance-ids)
187+
first-task [(fn [] (cleanup-and-refill-instance first-old-inst first-new-id))]
188+
remaining-tasks (for [[old-instance new-id] remaining]
183189
(fn [] (cleanup-and-refill-instance old-instance new-id)))]
184-
(execute-tasks! cleanup-and-refill-tasks creation-service))
190+
(execute-tasks! first-task creation-service)
191+
(when remaining-tasks
192+
(execute-tasks! remaining-tasks creation-service)))
185193
(if refill?
186194
(log/info (i18n/trs "Finished draining and refilling pool."))
187195
(log/info (i18n/trs "Finished draining pool."))))

src/clj/puppetlabs/services/jruby_pool_manager/impl/jruby_internal.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@
184184
[config :- jruby-schemas/JRubyConfig]
185185
(let [multithreaded (:multithreaded config)
186186
size (:max-active-instances config)
187-
creation-concurrency (get config :instance-creation-concurrency 4)
187+
creation-concurrency (:instance-creation-concurrency config)
188188
creation-service (Executors/newFixedThreadPool creation-concurrency)]
189189
(if multithreaded
190190
{:pool (instantiate-reference-pool size)

src/clj/puppetlabs/services/jruby_pool_manager/jruby_core.clj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@
174174
(update-in [:environment-vars] #(or % {}))
175175
(update-in [:lifecycle] initialize-lifecycle-fns)
176176
(update-in [:multithreaded] #(if (nil? %) false %))
177+
(update-in [:instance-creation-concurrency] #(if (nil? %) 3 %))
177178
jruby-internal/initialize-gem-path))
178179

179180
(schema/defn register-event-handler

src/clj/puppetlabs/services/jruby_pool_manager/jruby_schemas.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
:profiling-mode SupportedJRubyProfilingModes
9696
:profiler-output-file schema/Str
9797
:multithreaded schema/Bool
98-
(schema/optional-key :instance-creation-concurrency) schema/Int})
98+
:instance-creation-concurrency schema/Int})
9999

100100
(def JRubyPoolAgent
101101
"An agent configured for use in managing JRuby pools"

0 commit comments

Comments
 (0)