class Drntest::Engine

Public Class Methods

new(config) click to toggle source
# File lib/drntest/engine.rb, line 23
def initialize(config)
  @config = config
end

Public Instance Methods

catalog_file() click to toggle source
# File lib/drntest/engine.rb, line 35
def catalog_file
  @config.engine_config_path + "catalog.json"
end
start(target_path) click to toggle source
# File lib/drntest/engine.rb, line 27
def start(target_path)
  setup(target_path)
end
stop() click to toggle source
# File lib/drntest/engine.rb, line 31
def stop
  teardown
end

Private Instance Methods

extract_connection_info_catalog_v1(catalog_json) click to toggle source
# File lib/drntest/engine.rb, line 57
def extract_connection_info_catalog_v1(catalog_json)
  zone = catalog_json["zones"].first
  /\A([^:]+):(\d+)\/(.+)\z/ =~ zone
  @config.host = $1
  @config.port = $2.to_i
  @config.tag  = $3
end
extract_connection_info_catalog_v2(catalog_json) click to toggle source
# File lib/drntest/engine.rb, line 83
def extract_connection_info_catalog_v2(catalog_json)
  datasets = catalog_json["datasets"]
  datasets.each do |name, dataset|
    dataset["replicas"].each do |replica|
      replica["slices"].each do |slice|
        if /\A([^:]+):(\d+)\/([^.]+)/ =~ slice["volume"]["address"]
          @config.host = $1
          @config.port = $2.to_i
          @config.tag  = $3
          return
        end
      end
    end
  end
end
load_catalog_json(target_path) click to toggle source
# File lib/drntest/engine.rb, line 40
def load_catalog_json(target_path)
  catalog_json = JSON.parse(catalog_file.read)
  @config.catalog_version = catalog_json["version"]
  case @config.catalog_version
  when 1
    extract_connection_info_catalog_v1(catalog_json)
  when 2
    custom_catalog_json_file = target_path.sub_ext(".catalog.json")
    if custom_catalog_json_file.exist?
      custom_catalog_json = JSON.parse(custom_catalog_json_file.read)
      merge_catalog_v2!(catalog_json, custom_catalog_json)
    end
    extract_connection_info_catalog_v2(catalog_json)
  end
  catalog_json
end
merge_catalog_v2!(catalog_json, custom_catalog_json) click to toggle source
# File lib/drntest/engine.rb, line 65
def merge_catalog_v2!(catalog_json, custom_catalog_json)
  base_datasets = catalog_json["datasets"]
  custom_catalog_json["datasets"].each do |name, dataset|
    base_dataset = base_datasets[name]
    if base_dataset
      base_dataset["fact"] = dataset["fact"] || base_dataset["fact"]
      base_dataset["schema"] = dataset["schema"] || base_dataset["schema"]
      replicas = dataset["replicas"] || []
      base_replicas = base_dataset["replicas"]
      replicas.each_with_index do |replica, i|
        base_replicas[i].merge!(replica)
      end
    else
      base_datasets[name] = dataset
    end
  end
end
setup(target_path) click to toggle source
# File lib/drntest/engine.rb, line 99
def setup(target_path)
  setup_temporary_dir

  catalog_json = load_catalog_json(target_path)
  temporary_catalog = temporary_dir + "catalog.json"
  temporary_catalog.open("w") do |output|
    output.puts(JSON.pretty_generate(catalog_json))
  end

  ready_notify_in, ready_notify_out = IO.pipe
  command = [
    @config.droonga_engine,
    "--host", @config.host,
    "--port", @config.port.to_s,
    "--tag", @config.tag,
    "--base-dir", temporary_dir.to_s,
    "--ready-notify-fd", ready_notify_out.fileno.to_s,
    *@config.droonga_engine_options,
  ]
  env = {
  }
  options = {
    :err => :out,
    ready_notify_out => ready_notify_out,
  }
  arguments = [env, *command]
  arguments << options
  @pid = Process.spawn(*arguments)
  ready_notify_out.close
  wait_until_ready(ready_notify_in)
  ready_notify_in.close
end
setup_temporary_dir() click to toggle source
# File lib/drntest/engine.rb, line 139
def setup_temporary_dir
  tmpfs_candidates = [
    "/dev/shm",
    "/run/shm",
  ]
  tmpfs_candidates.each do |tmpfs_candidate|
    tmpfs = Pathname(tmpfs_candidate)
    if tmpfs.directory? and tmpfs.writable?
      FileUtils.rm_rf(temporary_base_dir)
      FileUtils.ln_s(tmpfs.to_s, temporary_base_dir.to_s)
      break
    end
  end
  FileUtils.rm_rf(temporary_dir)
  FileUtils.mkdir_p(temporary_dir)
end
teardown() click to toggle source
# File lib/drntest/engine.rb, line 132
def teardown
  Process.kill(:TERM, @pid)
  Process.wait(@pid)

  teardown_temporary_dir
end
teardown_temporary_dir() click to toggle source
# File lib/drntest/engine.rb, line 156
def teardown_temporary_dir
  FileUtils.rm_rf(temporary_dir.to_s)
end
temporary_base_dir() click to toggle source
# File lib/drntest/engine.rb, line 160
def temporary_base_dir
  @config.base_path + "tmp"
end
temporary_dir() click to toggle source
# File lib/drntest/engine.rb, line 164
def temporary_dir
  temporary_base_dir + "drntest"
end
wait_until_ready(ready_notify_in) click to toggle source
# File lib/drntest/engine.rb, line 168
def wait_until_ready(ready_notify_in)
  ready_notify_in.gets
end