Class: GroongaQueryLog::Command::RunRegressionTest::GroongaServer

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/groonga-query-log/command/run-regression-test.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#puts

Constructor Details

#initialize(groonga, groonga_options, groonga_env, database_path, options) ⇒ GroongaServer

Returns a new instance of GroongaServer.



574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
# File 'lib/groonga-query-log/command/run-regression-test.rb', line 574

def initialize(groonga,
               groonga_options,
               groonga_env,
               database_path,
               options)
  @input_directory = options[:input_directory] || Pathname.new(".")
  @working_directory = options[:working_directory] || Pathname.new(".")
  @groonga = groonga
  @groonga_options = groonga_options
  @groonga_env = groonga_env
  @database_path = @working_directory + database_path
  @host = "127.0.0.1"
  @port = find_unused_port
  @options = options
  @pid = nil
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



573
574
575
# File 'lib/groonga-query-log/command/run-regression-test.rb', line 573

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



573
574
575
# File 'lib/groonga-query-log/command/run-regression-test.rb', line 573

def port
  @port
end

Instance Method Details

#ensure_databaseObject



633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
# File 'lib/groonga-query-log/command/run-regression-test.rb', line 633

def ensure_database
  if @options[:recreate_database]
    FileUtils.rm_rf(@database_path.dirname.to_s)
  end
  return if @database_path.exist?

  FileUtils.mkdir_p(@database_path.dirname.to_s)
  create_db_command = [@groonga, "-n", @database_path.to_s, "quit"]
  unless system(*create_db_command)
    create_db_command_line = create_db_command.join(" ")
    raise "Failed to run: #{create_db_command_line}"
  end

  load_files.each do |load_file|
    filter_command = nil
    case load_file.extname
    when ".rb"
      env = {
        "GROONGA_LOG_PATH" => log_path.to_s,
      }
      command = [
        RbConfig.ruby,
        load_file.to_s,
        @database_path.to_s,
      ]
    when ".zst"
      env = {}
      command = [
        @groonga,
        "--log-path", log_path.to_s,
        @database_path.to_s,
      ]
      filter_command = [
        "zstdcat",
        load_file.to_s,
      ]
    else
      env = {}
      command = [
        @groonga,
        "--log-path", log_path.to_s,
        "--file", load_file.to_s,
        @database_path.to_s,
      ]
    end
    command_line = command.join(" ")
    if filter_command
      filter_command_line = filter_command.join(" ")
      command_line = "#{filter_command_line} | #{command_line}"
    end
    puts("Running...: #{command_line}")
    status = nil
    if filter_command
      IO.pipe do |input, output|
        filter_pid = spawn(*filter_command, out: output)
        output.close
        pid = spawn(env, *command, in: input)
        input.close
        begin
          pid, status = Process.waitpid2(pid)
          filter_pid, _filter_status = Process.waitpid2(filter_pid)
        rescue Interrupt
          Process.kill(:TERM, pid)
          Process.kill(:TERM, filter_pid)
          pid, status = Process.waitpid2(pid)
          filter_pid, _filter_status = Process.waitpid2(filter_pid)
        end
      end
    else
      pid = spawn(env, *command)
      begin
        pid, status = Process.waitpid2(pid)
      rescue Interrupt
        Process.kill(:TERM, pid)
        pid, status = Process.waitpid2(pid)
      end
    end
    unless status.success?
      raise "Failed to run: #{command_line}"
    end
  end
end

#n_leaked_objectsObject



731
732
733
734
735
736
737
738
739
740
741
742
743
# File 'lib/groonga-query-log/command/run-regression-test.rb', line 731

def n_leaked_objects
  n = 0
  File.open(log_path, encoding: "UTF-8") do |log|
    log.each_line do |line|
      next unless line.valid_encoding?
      case line
      when /grn_fin \((\d+)\)/
        n += Integer($1, 10)
      end
    end
  end
  n
end

#runObject



591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
# File 'lib/groonga-query-log/command/run-regression-test.rb', line 591

def run
  return unless @options[:run_queries]

  spawn_args = []
  spawn_args << @groonga_env if @groonga_env
  spawn_args << @groonga
  spawn_args.concat(@groonga_options)
  spawn_args.concat(["--bind-address", @host])
  spawn_args.concat(["--port", @port.to_s])
  spawn_args.concat(["--protocol", "http"])
  spawn_args.concat(["--log-path", log_path.to_s])
  if @options[:output_query_log]
    spawn_args.concat(["--query-log-path", query_log_path.to_s])
  end
  spawn_args << "-s"
  spawn_args << @database_path.to_s
  @pid = spawn(*spawn_args)

  begin
    n_retries = 60
    begin
      send_command("status")
    rescue SystemCallError
      sleep(1)
      n_retries -= 1
      raise if n_retries.zero?
      retry
    end

    if @options[:warm_up]
      send_command("dump?dump_records=no")
      warm_up_commands = @options[:warm_up_commands] || []
      warm_up_commands.each do |command|
        send_command(command)
      end
    end
  rescue
    shutdown
    raise
  end
end

#shutdownObject



720
721
722
723
724
725
726
727
728
729
# File 'lib/groonga-query-log/command/run-regression-test.rb', line 720

def shutdown
  return if @pid.nil?
  begin
    send_command("shutdown")
  rescue SystemCallError
    Process.kill(:KILL, @pid)
  end
  Process.waitpid(@pid)
  @pid = nil
end

#use_persistent_cache?Boolean

Returns:

  • (Boolean)


716
717
718
# File 'lib/groonga-query-log/command/run-regression-test.rb', line 716

def use_persistent_cache?
  @groonga_options.include?("--cache-base-path")
end