[Groonga-commit] groonga/groonga-query-log at 5035c1a [master] Add groonga-query-log-check-crash

Back to archive index

Kouhei Sutou null+****@clear*****
Mon Jan 15 15:24:57 JST 2018


Kouhei Sutou	2018-01-15 15:24:57 +0900 (Mon, 15 Jan 2018)

  New Revision: 5035c1ad3cf9e4b2a52fdd5e73142a5f3bcea6f2
  https://github.com/groonga/groonga-query-log/commit/5035c1ad3cf9e4b2a52fdd5e73142a5f3bcea6f2

  Message:
    Add groonga-query-log-check-crash

  Added files:
    bin/groonga-query-log-check-crash
    lib/groonga-query-log/command/check-crash.rb
  Modified files:
    groonga-query-log.gemspec
    lib/groonga-query-log.rb
    lib/groonga-query-log/parser.rb

  Added: bin/groonga-query-log-check-crash (+22 -0) 100755
===================================================================
--- /dev/null
+++ bin/groonga-query-log-check-crash    2018-01-15 15:24:57 +0900 (e66ceab)
@@ -0,0 +1,22 @@
+#!/usr/bin/env ruby
+#
+# Copyright (C) 2018  Kouhei Sutou <kou �� clear-code.com>
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+
+require "groonga-query-log/command/check-crash"
+
+check_crash = GroongaQueryLog::Command::CheckCrash.new
+exit(check_crash.run(ARGV))

  Modified: groonga-query-log.gemspec (+3 -2)
===================================================================
--- groonga-query-log.gemspec    2018-01-15 14:15:03 +0900 (e01883b)
+++ groonga-query-log.gemspec    2018-01-15 15:24:57 +0900 (171ad72)
@@ -1,6 +1,6 @@
-# -*- mode: ruby; coding: utf-8 -*-
+# -*- ruby -*-
 #
-# Copyright (C) 2012-2013  Kouhei Sutou <kou �� clear-code.com>
+# Copyright (C) 2012-2018  Kouhei Sutou <kou �� clear-code.com>
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -54,6 +54,7 @@ Gem::Specification.new do |spec|
 
   spec.add_runtime_dependency("groonga-command-parser")
   spec.add_runtime_dependency("groonga-client")
+  spec.add_runtime_dependency("groonga-log")
 
   spec.add_development_dependency("test-unit")
   spec.add_development_dependency("test-unit-notify")

  Modified: lib/groonga-query-log.rb (+3 -1)
===================================================================
--- lib/groonga-query-log.rb    2018-01-15 14:15:03 +0900 (82eae24)
+++ lib/groonga-query-log.rb    2018-01-15 15:24:57 +0900 (ce3fd2f)
@@ -1,4 +1,4 @@
-# Copyright (C) 2012-2017  Kouhei Sutou <kou �� clear-code.com>
+# Copyright (C) 2012-2018  Kouhei Sutou <kou �� clear-code.com>
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -14,6 +14,8 @@
 # License along with this library; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
+require "groonga-log"
+
 require "groonga-query-log/version"
 require "groonga-query-log/parser"
 require "groonga-query-log/replayer"

  Added: lib/groonga-query-log/command/check-crash.rb (+108 -0) 100644
===================================================================
--- /dev/null
+++ lib/groonga-query-log/command/check-crash.rb    2018-01-15 15:24:57 +0900 (c76295b)
@@ -0,0 +1,108 @@
+# Copyright (C) 2018  Kouhei Sutou <kou �� clear-code.com>
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+
+require "optparse"
+
+require "groonga-query-log"
+require "groonga-query-log/command-line"
+
+module GroongaQueryLog
+  module Command
+    class CheckCrash < CommandLine
+      def initialize
+        setup_options
+      end
+
+      def run(arguments)
+        begin
+          log_paths = @option_parser.parse!(arguments)
+        rescue OptionParser::InvalidOption => error
+          $stderr.puts(error)
+          return false
+        end
+
+        begin
+          check(log_paths)
+        rescue Interrupt
+        rescue Error
+          $stderr.puts($!.message)
+          return false
+        end
+
+        true
+      end
+
+      private
+      def setup_options
+        @options = {}
+
+        @option_parser = OptionParser.new do |parser|
+          parser.version = VERSION
+          parser.banner += " LOG1 ..."
+        end
+      end
+
+      def open_output
+        if @options[:output] == "-"
+          yield($stdout)
+        else
+          File.open(@options[:output], "w") do |output|
+            yield(output)
+          end
+        end
+      end
+
+      def check(log_paths)
+        general_log_parser = GroongaLog::Parser.new
+        query_log_parser = Parser.new
+        general_log_paths = []
+        query_log_paths = []
+        log_paths.each do |log_path|
+          sample_lines = File.open(log_path) do |log_file|
+            log_file.each_line.take(10)
+          end
+          if sample_lines.any? {|line| Parser.target_line?(line)}
+            query_log_paths << log_path
+          elsif sample_lines.any? {|line| GroongaLog::Parser.target_line?(line)}
+            general_log_paths << log_path
+          end
+        end
+
+        running = true
+        general_log_parser.parse_paths(general_log_paths) do |entry|
+          # p entry
+          case entry.log_level
+          when :emergency, :alert, :critical, :error, :warning
+            p [entry.log_level, entry.message, entry.timestamp.iso8601]
+          end
+
+          case entry.message
+          when /\Agrn_init:/
+            p [:crashed, entry.timestamp.iso8601] if running
+            running = true
+          when /\Agrn_fin \(\d+\)\z/
+            n_leaks = $1.to_i
+            running = false
+            p [:leak, n_leask, entry.timestamp.iso8601] unless n_leaks.zero?
+          end
+        end
+        query_log_parser.parse_paths(query_log_paths) do |statistic|
+          # p statistic
+        end
+      end
+    end
+  end
+end

  Modified: lib/groonga-query-log/parser.rb (+25 -34)
===================================================================
--- lib/groonga-query-log/parser.rb    2018-01-15 14:15:03 +0900 (89af66c)
+++ lib/groonga-query-log/parser.rb    2018-01-15 15:24:57 +0900 (5db5e35)
@@ -1,4 +1,4 @@
-# Copyright (C) 2011-2017  Kouhei Sutou <kou �� clear-code.com>
+# Copyright (C) 2011-2018  Kouhei Sutou <kou �� clear-code.com>
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -18,6 +18,24 @@ require "groonga-query-log/statistic"
 
 module GroongaQueryLog
   class Parser
+    PATTERN =
+      /\A(?<year>\d{4})-(?<month>\d\d)-(?<day>\d\d)
+         \ (?<hour>\d\d):(?<minute>\d\d):(?<second>\d\d)\.(?<microsecond>\d+)
+         \|(?<context_id>.+?)
+         \|(?<type>[>:<])/x
+
+    class << self
+      def target_line?(line)
+        if line.respond_to?(:valid_encoding?)
+          return false unless line.valid_encoding?
+        end
+
+        return false unless PATTERN.match(line)
+
+        true
+      end
+    end
+
     def initialize(options={})
       @options = options
       @slow_operation_threshold = options[:slow_operation_threshold]
@@ -27,12 +45,6 @@ module GroongaQueryLog
       @parsing_statistics = {}
     end
 
-    PATTERN =
-      /\A(?<year>\d{4})-(?<month>\d\d)-(?<day>\d\d)
-         \ (?<hour>\d\d):(?<minute>\d\d):(?<second>\d\d)\.(?<microsecond>\d+)
-         \|(?<context_id>.+?)
-         \|(?<type>[>:<])/x
-
     # Parses query-log file as stream to
     # {GroongaQueryLog::Analyzer::Statistic}s including some
     # informations for each query.
@@ -43,6 +55,8 @@ module GroongaQueryLog
     # @yieldparam [GroongaQueryLog::Statistic] statistic
     #   statistics of each query in log files.
     def parse(input, &block)
+      return to_enum(__method__, input) unless block_given?
+
       input.each_line do |line|
         next unless line.valid_encoding?
 
@@ -71,9 +85,11 @@ module GroongaQueryLog
     end
 
     def parse_paths(paths, &block)
-      target_paths = sort_paths(filter_paths(paths))
+      return to_enum(__method__, paths) unless block_given?
+
+      target_paths = GroongaLog::Parser.sort_paths(paths)
       target_paths.each do |path|
-        File.open(path) do |log|
+        GroongaLog::Input.open(path) do |log|
           parse(log, &block)
         end
       end
@@ -84,31 +100,6 @@ module GroongaQueryLog
     end
 
     private
-    def filter_paths(paths)
-      paths.reject do |path|
-        case File.extname(path).downcase
-        when ".zip", ".gz" # Or support decompress?
-          true
-        else
-          false
-        end
-      end
-    end
-
-    TIMESTAMP_PATTERN = /(\d{4})-(\d{2})-(\d{2})-
-                         (\d{2})-(\d{2})-(\d{2})-(\d{6})\z/x
-    def sort_paths(paths)
-      paths.sort_by do |path|
-        match_data = TIMESTAMP_PATTERN.match(File.basename(path))
-        if match_data
-          values = match_data.to_a[1..-1].collect(&:to_i)
-          Time.local(*values)
-        else
-          Time.now
-        end
-      end
-    end
-
     def parse_line(time_stamp, context_id, type, rest, &block)
       case type
       when ">"
-------------- next part --------------
HTML����������������������������...
URL: https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20180115/7e8029ef/attachment-0001.htm 



More information about the Groonga-commit mailing list
Back to archive index