Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use session ID in connection #23

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions lib/clickhouse/cluster.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
module Clickhouse
class Cluster < BasicObject

attr_reader :pond
attr_reader :pond, :use_session, :session_connection

def initialize(config)
config = config.dup
@use_session = config[:use_session]
urls = config.delete(:urls) || config.delete("urls")
urls.collect!{|url| ::Clickhouse::Utils.normalize_url(url)}
urls.shuffle! if use_session

@pond = ::Pond.new :maximum_size => urls.size, :timeout => 5.0
block = ::Proc.new do
Expand All @@ -32,12 +34,34 @@ def initialize(config)
private

def method_missing(*args, &block)
pond.checkout do |connection|
connection.send(*args, &block)
use_session ? call_in_session(*args,&block) : call_with_retry(*args,&block)
end

def call_in_session *args, &block

if @session_connection
@session_connection.send(*args, &block)
else
pond.checkout do |connection|
checked = connection.send(*args, &block)
@session_connection = connection
checked
end
end
rescue ::Clickhouse::ConnectionError
retry if pond.available.any?

end

def call_with_retry *args, &block
begin
pond.checkout do |connection|
connection.send(*args, &block)
end
rescue ::Clickhouse::ConnectionError
retry if pond.available.any?
end
end



end
end
7 changes: 7 additions & 0 deletions lib/clickhouse/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Connection

def initialize(config = {})
@config = normalize_config(config)
@session_id = generate_session_id if @config[:use_session]
end

private
Expand All @@ -38,5 +39,11 @@ def normalize_config(config)
DEFAULT_CONFIG.merge(config.reject{|_k, v| v.nil?})
end

SESSIONID_LETTERS = (48..57).collect{|c| c.chr} + (65..90).collect{|c| c.chr} + (97..122).collect{|c| c.chr}

def generate_session_id
Array.new(20) { SESSIONID_LETTERS[rand(SESSIONID_LETTERS.size)] }.join
end

end
end
21 changes: 17 additions & 4 deletions lib/clickhouse/connection/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ def url
private

def client
@client ||= Faraday.new(:url => url)
@client ||= connect_faraday
end

def connect_faraday
Faraday.new(url: url, request: @config.slice(:open_timeout, :write_timeout, :timeout)) do |conn|
conn.request(:retry, max: 0)
conn.adapter(:net_http)
end
end

def ensure_authentication
Expand All @@ -49,7 +56,9 @@ def path(query)
params = @config.select{|k, _v| k == :database}
params[:query] = query
params[:output_format_write_statistics] = 1
query_string = params.collect{|k, v| "#{k}=#{CGI.escape(v.to_s)}"}.join("&")
params[:session_id] = @session_id
params[:session_timeout] = @config[:session_timeout]
query_string = params.compact.collect{|k, v| "#{k}=#{CGI.escape(v.to_s)}"}.join("&")

"/?#{query_string}"
end
Expand All @@ -62,12 +71,16 @@ def request(method, query, body = nil)
response = client.send(method, path(query), body)
status = response.status
duration = Time.now - start
query, format = Utils.extract_format(query)
query, format = Utils.extract_format("#{query} #{body}")
response = parse_body(format, response.body)
stats = parse_stats(response)

write_log duration, query, stats
raise QueryError, "Got status #{status} (expected 200): #{response}" unless status == 200

if status!=200
raise QueryError, "#{response}, status returned #{status} (expected 200)"
end

response

rescue Faraday::Error => e
Expand Down
8 changes: 7 additions & 1 deletion lib/clickhouse/connection/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ def execute(query, body = nil)
def query(query)
query = Utils.extract_format(query)[0]
query += " FORMAT JSONCompact"
parse_data get(query)

if query.size>4000 && ( comps = query.split /\A\s*(\w+)(.*)/m ).size==3
parse_data post(comps[1], comps[2])
else
parse_data get(query)
end

end

def databases
Expand Down
22 changes: 18 additions & 4 deletions lib/clickhouse/connection/query/result_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@ def parse_value(type, value)
parse_int_value value
when "Float32", "Float64"
parse_float_value value
when "String", "Enum8", "Enum16"
when /^Decimal/
parse_decimal_value value
when "String", "Enum8", "Enum16", "LowCardinality(String)"
parse_string_value value
when /FixedString\(\d+\)/
parse_fixed_string_value value
when "Date"
parse_date_value value
when "DateTime"
when /^DateTime/
parse_date_time_value value
when /Array\(/
parse_array_value value
Expand All @@ -77,6 +79,10 @@ def parse_float_value(value)
value.to_f
end

def parse_decimal_value(value)
value.to_d
end

def parse_string_value(value)
value.force_encoding("UTF-8")
end
Expand All @@ -86,11 +92,19 @@ def parse_fixed_string_value(value)
end

def parse_date_value(value)
Date.parse(value)
if '0000-00-00'==value
nil
else
Date.parse(value)
end
end

def parse_date_time_value(value)
Time.parse(value)
if '0000-00-00 00:00:00'==value
nil
else
Time.parse(value)
end
end

def parse_array_value(value)
Expand Down
4 changes: 2 additions & 2 deletions lib/clickhouse/version.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Clickhouse
MAJOR = 0
MINOR = 1
TINY = 10
TINY = 11

VERSION = [MAJOR, MINOR, TINY].join(".")
VERSION = [MAJOR, MINOR, TINY].join(".") + '-sessionId-to-11'
end