Skip to content

Commit

Permalink
Changed query throttling to opt-in
Browse files Browse the repository at this point in the history
This commit flips the logic for the query throttle. With this commit
query throttling is off by default. Users wanting to enable it will
use `MB_ENABLE_QUERY_THROTTLE=true`.
  • Loading branch information
iethree committed Oct 15, 2018
1 parent aaceb9d commit bc48c0a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
10 changes: 5 additions & 5 deletions src/metabase/query_processor/middleware/add_query_throttle.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns metabase.query-processor.middleware.add-query-throttle
"Middleware that constraints the number of concurrent queries, rejects queries by throwing an exception and
"Middleware that constrains the number of concurrent queries, rejects queries by throwing an exception and
returning a 503 when we exceed our capacity"
(:require [metabase.config :as config]
[puppetlabs.i18n.core :refer [tru]])
Expand Down Expand Up @@ -44,8 +44,8 @@
(throw-503-unavailable))))

(defn maybe-add-query-throttle
"Adds the query throttle middleware as not as `MB_DISABLE_QUERY_THROTTLE` hasn't been set"
"Adds the query throttle middleware if `MB_ENABLE_QUERY_THROTTLE` has been set"
[qp]
(if (config/config-bool :mb-disable-query-throttle)
qp
(throttle-queries query-semaphore qp)))
(if (config/config-bool :mb-enable-query-throttle)
(throttle-queries query-semaphore qp)
qp))
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
(ns metabase.query-processor.middleware.add-query-throttle-test
(:require [expectations :refer :all]
[metabase.query-processor.middleware
[add-query-throttle :as throttle :refer :all]
[catch-exceptions :as catch-exceptions]]
[metabase.test.data :as data]
[metabase.util :as u])
(:require [environ.core :as environ]
[expectations :refer :all]
[metabase.query-processor.middleware
[add-query-throttle :as throttle :refer :all]
[catch-exceptions :as catch-exceptions]]
[metabase.test
[data :as data]
[util :as tu]]
[metabase.util :as u])
(:import java.util.concurrent.Semaphore))

(defmacro ^:private exception-and-message [& body]
Expand Down Expand Up @@ -103,7 +106,7 @@
(throw (Exception. "failure")))
query-future (future
(u/ignore-exceptions
((#'throttle/throttle-queries semaphore coordinate-then-fail) {:query "map"})))]
((#'throttle/throttle-queries semaphore coordinate-then-fail) {:query "map"})))]
{:beinning-permits begin-num-permits
:before-failure-permits (do
@start-middleware-promise
Expand All @@ -112,3 +115,32 @@
(deliver finish-middleware-promise true)
@query-future
(.availablePermits semaphore))})))

;; Test the function that adds the middleware only when MB_ENABLE_QUERY_THROTTLE is set to true

(defmacro ^:private with-query-throttle-value [enable-query-thottle-str & body]
`(with-redefs [environ/env {:mb-enable-query-throttle ~enable-query-thottle-str}]
~@body))

;; By default the query throttle should not be applied
(expect
#'identity
(tu/throw-if-called throttle/throttle-queries
(with-query-throttle-value nil
(throttle/maybe-add-query-throttle #'identity))))

;; The query throttle should not be applied if MB_ENABLE_QUERY_THROTTLE is false
(expect
#'identity
(tu/throw-if-called throttle/throttle-queries
(with-query-throttle-value "false"
(throttle/maybe-add-query-throttle #'identity))))

;; The query throttle should be applied if MB_ENABLE_QUERY_THROTTLE is true
(expect
(let [called? (atom false)]
(with-redefs [throttle/throttle-queries (fn [& args]
(reset! called? true))]
(with-query-throttle-value "true"
(throttle/maybe-add-query-throttle #'identity)
@called?))))

0 comments on commit bc48c0a

Please sign in to comment.