fix: Reconnect on a closed connection when checked out#304
fix: Reconnect on a closed connection when checked out#304jrogov wants to merge 1 commit intoplausible:masterfrom
Conversation
| def request_chunked(conn, method, path, headers, stream, opts) do | ||
| with {:ok, conn, ref} <- send_request(conn, method, path, headers, :stream), | ||
| {:ok, conn} <- stream_body(conn, ref, stream), | ||
| do: receive_full_response(conn, timeout(conn, opts)) | ||
| with_retry_if_stale_connection(conn, fn conn -> | ||
| with {:ok, conn, ref} <- send_request(conn, method, path, headers, :stream), | ||
| {:ok, conn} <- stream_body(conn, ref, stream), | ||
| do: receive_full_response(conn, timeout(conn, opts)) | ||
| end) |
There was a problem hiding this comment.
I'm pretty sure we should not do this on request_chunked when stream-writing the data, as it would mean that we could write duplicated data. Just wanted to check if that's correct.
If that's so, let me know, and I'll remove this part
There was a problem hiding this comment.
as it would mean that we could write duplicated data
I think since it's an ongoing HTTP request, if the connection gets closed for it, ClickHouse would drop all received data, at least for non-async inserts. But I haven't actually checked that. So it might be that we don't gain anything from reconnecting since we need to start sending the stream anew anyway.
There was a problem hiding this comment.
Also request_chunked should probably be removed (I'll open a PR), I added it because I didn't know at the time about ability to do Stream.into/2 with DBConnection.
DBConnection.run(ch, fn conn ->
File.stream!("demo.csv", 512_000)
|> Stream.into(Ch.stream(conn, "insert into demo format CSV"))
|> Stream.run()
end)But the issue would remain, do we reconnect in those scenarios or not.
|
👋 @jrogov I wonder, what's your use-case? If something is pausing for that long, why not do two separate checkouts? |
(In collaboration with @trollfred)
Hiya 👋
We've been experimenting with
checkouta bit more, and stumbled upon a rather inconvenient behaviour ofChwhen a connection's beencheckoutofecho_chrepo.TL;DR: due to Clickhouse's
keep_alive_timeout(defaults to 10 seconds), when a connection is checked out and there's a pause between DB operations, connection gets closed by server, and instead of reconnecting,chjust fails.This PR provides a simple single reconnect in such cases.
Steps to reproduce:
Result: