Skip to content
Snippets Groups Projects
Commit 3b85ace7 authored by Logan Attwood's avatar Logan Attwood Committed by Cam Saul
Browse files

Improve Presto support for dispatching queries (#10006)

* Follow HTTP 307 redirects when POSTing a query to Presto

This is how the Presto team communicated on IRC to properly "dispatch"
queries to Presto clusters, to enable replacing a cluster without
impacting users of the cluster.

* Ensure we talk to the correct Presto coordinator when cancelling a query

The previous behaviour would build a cancellation URI from the query ID
and the configured host (unless SSH tunneling is configured). This
commit builds on that existing behaviour, replacing the host and port
in the URI with ones extracted from the `infoUri` in the polling
responses, but only if they match the ones configured in the datasource.
This should not impact connections going over an SSH tunnel as sending
the request over the SSH tunnel requires using a different host than the
one configured.
parent ccdfdfb2
No related branches found
No related tags found
No related merge requests found
......@@ -59,6 +59,11 @@
(when password
{:basic-auth [user password]})))
(defn ^:private create-cancel-url [cancel-uri host port info-uri]
;; Replace the host in the cancel-uri with the host from the info-uri provided from the presto response- this doesn't
;; break SSH tunneling as the host in the cancel-uri is different if it's enabled
(str/replace cancel-uri (str host ":" port) (get (str/split info-uri #"/") 2)))
(defn- parse-time-with-tz [s]
;; Try parsing with offset first then with full ZoneId
(or (u/ignore-exceptions (du/parse-date "HH:mm:ss.SSS ZZ" s))
......@@ -119,8 +124,10 @@
[details query]
{:pre [(map? details)]}
(ssh/with-ssh-tunnel [details-with-tunnel details]
(let [{{:keys [columns data nextUri error id]} :body} (http/post (details->uri details-with-tunnel "/v1/statement")
(assoc (details->request details-with-tunnel) :body query, :as :json))]
(let [{{:keys [columns data nextUri error id infoUri]} :body}
(http/post (details->uri details-with-tunnel "/v1/statement")
(assoc (details->request details-with-tunnel)
:body query, :as :json, :redirect-strategy :lax))]
(when error
(throw (ex-info (or (:message error) "Error preparing query.") error)))
(let [rows (parse-presto-results (:report-timezone details) (or columns []) (or data []))
......@@ -138,14 +145,15 @@
(if id
;; If we have a query id, we can cancel the query
(try
(http/delete (details->uri details-with-tunnel (str "/v1/query/" id))
(details->request details-with-tunnel))
(let [tunneledUri (details->uri details-with-tunnel (str "/v1/query/" id))
adjustedUri (create-cancel-url tunneledUri (get details :host) (get details :port) infoUri)]
(http/delete adjustedUri(details->request details-with-tunnel)))
;; If we fail to cancel the query, log it but propogate the interrupted exception, instead of
;; covering it up with a failed cancel
(catch Exception e
(log/error e (trs "Error canceling query with ID {0}" id))))
(log/warn (trs "Client connection closed, no query-id found, can't cancel query")))
;; Propogate the error so that any finalizers can still run
;; Propagate the error so that any finalizers can still run
(throw e)))))))))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment