From 14da1ef28abb4779dac53fde4da67a2ed132d647 Mon Sep 17 00:00:00 2001 From: Justin Stoller Date: Thu, 30 May 2024 09:34:35 -0700 Subject: [PATCH 1/3] (PE-37376) Initialize one JRuby instance first when initializing pool Puppet may apply a "settings catalog" when it initializes. This will check the filesystem and create any directories specified by settings that do not already exist. This will cause errors if Puppet does need to create directories and is initialized in parallel. To resolve this when priming the pool we now instantiate one instance first before initializing the remaining instances in parallel. --- .../jruby_pool_manager/impl/jruby_agents.clj | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/clj/puppetlabs/services/jruby_pool_manager/impl/jruby_agents.clj b/src/clj/puppetlabs/services/jruby_pool_manager/impl/jruby_agents.clj index 31c80351..a366c1fe 100644 --- a/src/clj/puppetlabs/services/jruby_pool_manager/impl/jruby_agents.clj +++ b/src/clj/puppetlabs/services/jruby_pool_manager/impl/jruby_agents.clj @@ -70,9 +70,10 @@ (schema/defn ^:always-validate prime-pool! - "Sequentially fill the pool with new JRubyInstances. NOTE: this - function should never be called except by the modify-instance-agent - to create a pool's initial jruby instances." + "Fill the pool with new JRubyInstances. Instantiates the first JRuby (Puppet + will sometimes alter the filesystem on first instantiation) and the remaining + instances in parallel. NOTE: this function should never be called except by + the modify-instance-agent to create a pool's initial jruby instances." [{:keys [config] :as pool-context} :- jruby-schemas/PoolContext] (log/debug (format "%s\n%s" (i18n/trs "Initializing JRubyInstances with the following settings:") @@ -80,15 +81,18 @@ (let [pool (jruby-internal/get-pool pool-context) creation-service (jruby-internal/get-creation-service pool-context) total (.remainingCapacity pool) - ids (->> total range (map inc)) + [first-id & ids] (->> total range (map inc)) add-instance* (fn [id] (log/debug (i18n/trs "Priming JRubyInstance {0} of {1}" id count)) (add-instance pool-context id) (log/info (i18n/trs "Finished creating JRubyInstance {0} of {1}" id count))) + initial-task (fn [] (add-instance* first-id)) tasks (for [id ids] (fn [] (add-instance* id)))] - (execute-tasks! tasks creation-service))) + (execute-tasks! [initial-task] creation-service) + (when (seq ids) + (execute-tasks! tasks creation-service)))) (schema/defn ^:always-validate flush-instance! From b151955db65ac2bce55eb3f815d6e5ecd1a40791 Mon Sep 17 00:00:00 2001 From: Justin Stoller Date: Mon, 3 Jun 2024 09:26:18 -0700 Subject: [PATCH 2/3] (PE-37376) Initialize one JRuby instance first even when refilling a pool --- .../services/jruby_pool_manager/impl/jruby_agents.clj | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/clj/puppetlabs/services/jruby_pool_manager/impl/jruby_agents.clj b/src/clj/puppetlabs/services/jruby_pool_manager/impl/jruby_agents.clj index a366c1fe..0370e4e2 100644 --- a/src/clj/puppetlabs/services/jruby_pool_manager/impl/jruby_agents.clj +++ b/src/clj/puppetlabs/services/jruby_pool_manager/impl/jruby_agents.clj @@ -183,9 +183,13 @@ (throw (IllegalStateException. (i18n/trs "There was a problem creating a JRubyInstance for the pool.") e))))) - cleanup-and-refill-tasks (for [[old-instance new-id] (zipmap old-instances new-instance-ids)] + [[first-old-inst first-new-id] & remaining] (zipmap old-instances new-instance-ids) + first-task [(fn [] (cleanup-and-refill-instance first-old-inst first-new-id))] + remaining-tasks (for [[old-instance new-id] remaining] (fn [] (cleanup-and-refill-instance old-instance new-id)))] - (execute-tasks! cleanup-and-refill-tasks creation-service)) + (execute-tasks! first-task creation-service) + (when remaining-tasks + (execute-tasks! remaining-tasks creation-service))) (if refill? (log/info (i18n/trs "Finished draining and refilling pool.")) (log/info (i18n/trs "Finished draining pool.")))) From ef6a9472b882b123deb83ee4593c8f5cb2bde77c Mon Sep 17 00:00:00 2001 From: Justin Stoller Date: Mon, 3 Jun 2024 09:27:02 -0700 Subject: [PATCH 3/3] (PE-37376) Refactor instance-creation-concurrency default, set to 3 --- .../services/jruby_pool_manager/impl/jruby_internal.clj | 2 +- src/clj/puppetlabs/services/jruby_pool_manager/jruby_core.clj | 1 + .../puppetlabs/services/jruby_pool_manager/jruby_schemas.clj | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/clj/puppetlabs/services/jruby_pool_manager/impl/jruby_internal.clj b/src/clj/puppetlabs/services/jruby_pool_manager/impl/jruby_internal.clj index 6eba33e1..4a8d9e21 100644 --- a/src/clj/puppetlabs/services/jruby_pool_manager/impl/jruby_internal.clj +++ b/src/clj/puppetlabs/services/jruby_pool_manager/impl/jruby_internal.clj @@ -184,7 +184,7 @@ [config :- jruby-schemas/JRubyConfig] (let [multithreaded (:multithreaded config) size (:max-active-instances config) - creation-concurrency (get config :instance-creation-concurrency 4) + creation-concurrency (:instance-creation-concurrency config) creation-service (Executors/newFixedThreadPool creation-concurrency)] (if multithreaded {:pool (instantiate-reference-pool size) diff --git a/src/clj/puppetlabs/services/jruby_pool_manager/jruby_core.clj b/src/clj/puppetlabs/services/jruby_pool_manager/jruby_core.clj index 4ee88b80..b32378b6 100644 --- a/src/clj/puppetlabs/services/jruby_pool_manager/jruby_core.clj +++ b/src/clj/puppetlabs/services/jruby_pool_manager/jruby_core.clj @@ -174,6 +174,7 @@ (update-in [:environment-vars] #(or % {})) (update-in [:lifecycle] initialize-lifecycle-fns) (update-in [:multithreaded] #(if (nil? %) false %)) + (update-in [:instance-creation-concurrency] #(if (nil? %) 3 %)) jruby-internal/initialize-gem-path)) (schema/defn register-event-handler diff --git a/src/clj/puppetlabs/services/jruby_pool_manager/jruby_schemas.clj b/src/clj/puppetlabs/services/jruby_pool_manager/jruby_schemas.clj index 4aab587f..812eeffa 100644 --- a/src/clj/puppetlabs/services/jruby_pool_manager/jruby_schemas.clj +++ b/src/clj/puppetlabs/services/jruby_pool_manager/jruby_schemas.clj @@ -95,7 +95,7 @@ :profiling-mode SupportedJRubyProfilingModes :profiler-output-file schema/Str :multithreaded schema/Bool - (schema/optional-key :instance-creation-concurrency) schema/Int}) + :instance-creation-concurrency schema/Int}) (def JRubyPoolAgent "An agent configured for use in managing JRuby pools"