class Grntest::Executors::HTTPExecutor

Constants

DEBUG
LOAD_DEBUG
MAX_URI_SIZE

Public Class Methods

new(host, port, context) click to toggle source
Calls superclass method Grntest::Executors::BaseExecutor::new
# File lib/grntest/executors/http-executor.rb, line 47
def initialize(host, port, context)
  super(context)
  @host = host
  @port = port
end

Public Instance Methods

create_sub_executor(context) click to toggle source
# File lib/grntest/executors/http-executor.rb, line 73
def create_sub_executor(context)
  self.class.new(@host, @port, context)
end
ensure_groonga_ready() click to toggle source
# File lib/grntest/executors/http-executor.rb, line 61
def ensure_groonga_ready
  n_retried = 0
  begin
    @raw_status_response = send_command(command("status"))
  rescue Error
    n_retried += 1
    sleep(0.1)
    retry if n_retried < 100
    raise
  end
end
send_command(command) click to toggle source
# File lib/grntest/executors/http-executor.rb, line 53
def send_command(command)
  if command.name == "load"
    send_load_command(command)
  else
    send_normal_command(command)
  end
end

Private Instance Methods

check_response(response) click to toggle source
# File lib/grntest/executors/http-executor.rb, line 81
def check_response(response)
  case response
  when Net::HTTPBadRequest,
       Net::HTTPNotFound
    # Groonga returns them for an invalid request.
  when Net::HTTPRequestTimeOut
    # Groonga returns this for a canceled request.
  when Net::HTTPInternalServerError
    # Groonga returns this for an internal error.
  else
    response.value
  end
end
normalize_response_data(command, raw_response_data) click to toggle source
# File lib/grntest/executors/http-executor.rb, line 204
def normalize_response_data(command, raw_response_data)
  return raw_response_data if raw_response_data.empty?
  return raw_response_data if command.output_type == :none
  return raw_response_data if command.output_type == :"apache-arrow"
  "#{raw_response_data}\n"
end
read_timeout() click to toggle source
# File lib/grntest/executors/http-executor.rb, line 211
def read_timeout
  if @context.timeout.zero?
    nil
  else
    @context.timeout
  end
end
run_http_request(url) { || ... } click to toggle source
# File lib/grntest/executors/http-executor.rb, line 180
def run_http_request(url)
  begin
    yield
  rescue SystemCallError
    message = "failed to read response from Groonga: <#{url}>: #{$!}"
    raise Error.new(message)
  rescue EOFError
    message = "unexpected EOF response from Groonga: <#{url}>: #{$!}"
    raise Error.new(message)
  rescue Net::HTTPBadResponse
    message = "bad response from Groonga: <#{url}>: "
    message << "#{$!.class}: #{$!.message}"
    raise Error.new(message)
  rescue Net::HTTPHeaderSyntaxError
    message = "bad HTTP header syntax in Groonga response: <#{url}>: "
    message << "#{$!.class}: #{$!.message}"
    raise Error.new(message)
  rescue Net::HTTPServerException
    message = "exception from Groonga: <#{url}>: "
    message << "#{$!.class}: #{$!.message}"
    raise Error.new(message)
  end
end
send_load_command(command) click to toggle source
# File lib/grntest/executors/http-executor.rb, line 96
def send_load_command(command)
  lines = command.original_source.lines
  if lines.size == 1 and command.to_uri_format.size <= MAX_URI_SIZE
    return send_normal_command(command)
  end

  case @context.input_type
  when "apache-arrow"
    command[:input_type] = "apache-arrow"
    content_type = "application/x-apache-arrow-streaming"
    arrow_table = command.build_arrow_table
    if arrow_table
      buffer = Arrow::ResizableBuffer.new(1024)
      arrow_table.save(buffer, format: :stream)
      body = buffer.data.to_s
    else
      body = ""
    end
    command.arguments.delete(:values)
  else
    content_type = "application/json; charset=UTF-8"
    values = command.arguments.delete(:values)
    if lines.size >= 2 and lines[1].start_with?("[")
      unless /\s--columns\s/ =~ lines.first
        command.arguments.delete(:columns)
      end
      body = lines[1..-1].join
    else
      body = values
    end
  end
  path = command.to_uri_format
  url = "http://#{@host}:#{@port}#{path}"
  request = Net::HTTP::Post.new(path)
  request.content_type = content_type
  set_request_body(request, body)
  @benchmark_result.measure do
    run_http_request(url) do
      http = Net::HTTP.new(@host, @port)
      http.set_debug_output($stderr) if LOAD_DEBUG
      response = http.start do
        http.read_timeout = read_timeout
        http.request(request)
      end
      check_response(response)
      normalize_response_data(command, response.body)
    end
  end
end
send_normal_command(command) click to toggle source
# File lib/grntest/executors/http-executor.rb, line 146
def send_normal_command(command)
  path_with_query = command.to_uri_format
  if @context.use_http_post?
    path, query = path_with_query.split("?", 2)
    request = Net::HTTP::Post.new(path)
    request.content_type = "application/x-www-form-urlencoded"
    set_request_body(request, query)
  else
    request = Net::HTTP::Get.new(path_with_query)
  end
  url = "http://#{@host}:#{@port}#{path_with_query}"
  @benchmark_result.measure do
    run_http_request(url) do
      http = Net::HTTP.new(@host, @port)
      http.set_debug_output($stderr) if DEBUG
      response = http.start do
        http.read_timeout = read_timeout
        http.request(request)
      end
      check_response(response)
      normalize_response_data(command, response.body)
    end
  end
end
set_request_body(request, body) click to toggle source
# File lib/grntest/executors/http-executor.rb, line 171
def set_request_body(request, body)
  if @context.use_http_chunked?
    request["Transfer-Encoding"] = "chunked"
    request.body_stream = SlowBodyStream.new(body)
  else
    request.body = body
  end
end