[Groonga-commit] droonga/droonga-engine at 1e135b7 [buffered-forward] Extract common codes to handle on_ready and on_failure

Back to archive index

YUKI Hiroshi null+****@clear*****
Fri Mar 20 15:52:36 JST 2015


YUKI Hiroshi	2015-03-20 15:52:36 +0900 (Fri, 20 Mar 2015)

  New Revision: 1e135b78014604c72cd7989861cf4d57eebef15e
  https://github.com/droonga/droonga-engine/commit/1e135b78014604c72cd7989861cf4d57eebef15e

  Message:
    Extract common codes to handle on_ready and on_failure

  Added files:
    lib/droonga/deferrable.rb
  Modified files:
    lib/droonga/command/droonga_engine.rb
    lib/droonga/dispatcher.rb
    lib/droonga/engine.rb
    lib/droonga/engine_state.rb
    lib/droonga/farm.rb
    lib/droonga/job_pusher.rb
    lib/droonga/process_supervisor.rb
    lib/droonga/serf/agent.rb
    lib/droonga/slice.rb
    lib/droonga/supervisor.rb

  Modified: lib/droonga/command/droonga_engine.rb (+3 -18)
===================================================================
--- lib/droonga/command/droonga_engine.rb    2015-03-20 15:50:51 +0900 (1fe797e)
+++ lib/droonga/command/droonga_engine.rb    2015-03-20 15:52:36 +0900 (b7b2514)
@@ -24,6 +24,7 @@ require "sigdump/setup"
 
 require "droonga/engine/version"
 require "droonga/loggable"
+require "droonga/deferrable"
 require "droonga/path"
 require "droonga/address"
 require "droonga/serf"
@@ -586,20 +587,12 @@ module Droonga
       end
 
       class ServiceRunner
+        include Deferrable
+
         def initialize(raw_loop, configuration)
           @raw_loop = raw_loop
           @configuration = configuration
           @success = false
-          @on_ready = nil
-          @on_failure = nil
-        end
-
-        def on_ready=(callback)
-          @on_ready = callback
-        end
-
-        def on_failure=(callback)
-          @on_failure = callback
         end
 
         def run
@@ -656,14 +649,6 @@ module Droonga
           supervisor
         end
 
-        def on_ready
-          @on_ready.call if @on_ready
-        end
-
-        def on_failure
-          @on_failure.call if @on_failure
-        end
-
         def on_finish
           _, status = Process.waitpid2(@pid)
           @success = status.success?

  Added: lib/droonga/deferrable.rb (+35 -0) 100644
===================================================================
--- /dev/null
+++ lib/droonga/deferrable.rb    2015-03-20 15:52:36 +0900 (2b0dee4)
@@ -0,0 +1,35 @@
+# Copyright (C) 2015 Droonga Project
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License version 2.1 as published by the Free Software Foundation.
+#
+# 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
+
+module Droonga
+  module Deferrable
+    attr_writer :on_ready, :on_failure
+
+    def wait_until_ready(target)
+      target.on_ready = lambda do
+        on_ready
+      end
+    end
+
+    private
+    def on_ready
+      @on_ready.call if @on_ready
+    end
+
+    def on_failure
+      @on_failure.call if @on_failure
+    end
+  end
+end

  Modified: lib/droonga/dispatcher.rb (+2 -4)
===================================================================
--- lib/droonga/dispatcher.rb    2015-03-20 15:50:51 +0900 (beaaaa0)
+++ lib/droonga/dispatcher.rb    2015-03-20 15:52:36 +0900 (b71305b)
@@ -1,4 +1,4 @@
-# Copyright (C) 2013-2014 Droonga Project
+# Copyright (C) 2013-2015 Droonga Project
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -60,14 +60,12 @@ module Droonga
                        :cluster => @cluster,
                        :dispatcher => self,
                        :forwarder  => @forwarder)
+      @engine_state.wait_until_ready(@farm)
       @collector_runners = create_collector_runners
       @step_runners = create_step_runners
     end
 
     def start
-      @farm.on_ready = lambda do
-        @engine_state.on_ready
-      end
       @farm.start
     end
 

  Modified: lib/droonga/engine.rb (+7 -5)
===================================================================
--- lib/droonga/engine.rb    2015-03-20 15:50:51 +0900 (0887a32)
+++ lib/droonga/engine.rb    2015-03-20 15:52:36 +0900 (a4b34f6)
@@ -1,6 +1,6 @@
 # -*- coding: utf-8 -*-
 #
-# Copyright (C) 2013-2014 Droonga Project
+# Copyright (C) 2013-2015 Droonga Project
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -19,6 +19,7 @@ require "time"
 require "fileutils"
 require "droonga/engine/version"
 require "droonga/loggable"
+require "droonga/deferrable"
 require "droonga/engine_state"
 require "droonga/cluster"
 require "droonga/catalog_loader"
@@ -29,8 +30,8 @@ require "droonga/node_metadata"
 module Droonga
   class Engine
     include Loggable
+    include Deferrable
 
-    attr_writer :on_ready
     def initialize(loop, name, internal_name)
       @catalog = load_catalog
       @node_metadata = NodeMetadata.new
@@ -43,14 +44,15 @@ module Droonga
                              :metadata => @node_metadata)
 
       @dispatcher = create_dispatcher
-
-      @on_ready = nil
     end
 
     def start
       logger.trace("start: start")
       @state.on_ready = lambda do
-        @on_ready.call if @on_ready
+        on_ready
+      end
+      @state.on_failure = lambda do
+        on_failure
       end
       @state.start
       @cluster.start

  Modified: lib/droonga/engine_state.rb (+3 -7)
===================================================================
--- lib/droonga/engine_state.rb    2015-03-20 15:50:51 +0900 (32e826c)
+++ lib/droonga/engine_state.rb    2015-03-20 15:52:36 +0900 (d4fe1e3)
@@ -1,4 +1,4 @@
-# Copyright (C) 2014 Droonga Project
+# Copyright (C) 2014-2015 Droonga Project
 #
 # 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,7 @@ require "English"
 require "coolio"
 
 require "droonga/loggable"
+require "droonga/deferrable"
 require "droonga/event_loop"
 require "droonga/forwarder"
 require "droonga/replier"
@@ -26,13 +27,13 @@ require "droonga/node_metadata"
 module Droonga
   class EngineState
     include Loggable
+    include Deferrable
 
     attr_reader :loop
     attr_reader :name
     attr_reader :internal_name
     attr_reader :forwarder
     attr_reader :replier
-    attr_writer :on_ready
     attr_accessor :catalog
     attr_accessor :on_finish
 
@@ -44,7 +45,6 @@ module Droonga
       @current_id = 0
       @forwarder = Forwarder.new(@loop)
       @replier = Replier.new(@forwarder)
-      @on_ready = nil
       @on_finish = nil
       @catalog = params[:catalog]
       @node_metadata = params[:metadata]
@@ -110,10 +110,6 @@ module Droonga
       @node_metadata.role
     end
 
-    def on_ready
-      @on_ready.call if @on_ready
-    end
-
     private
     def log_tag
       "engine_state"

  Modified: lib/droonga/farm.rb (+3 -6)
===================================================================
--- lib/droonga/farm.rb    2015-03-20 15:50:51 +0900 (792b318)
+++ lib/droonga/farm.rb    2015-03-20 15:52:36 +0900 (8bb8ade)
@@ -15,11 +15,13 @@
 # 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 "droonga/deferrable"
 require "droonga/slice"
 
 module Droonga
   class Farm
-    attr_writer :on_ready
+    include Deferrable
+
     def initialize(name, catalog, loop, options={})
       @name = name
       @catalog = catalog
@@ -82,10 +84,5 @@ module Droonga
     def process(slice_name, message)
       @slices[slice_name].process(message)
     end
-
-    private
-    def on_ready
-      @on_ready.call if @on_ready
-    end
   end
 end

  Modified: lib/droonga/job_pusher.rb (+1 -1)
===================================================================
--- lib/droonga/job_pusher.rb    2015-03-20 15:50:51 +0900 (cf88a6d)
+++ lib/droonga/job_pusher.rb    2015-03-20 15:52:36 +0900 (9111a54)
@@ -1,4 +1,4 @@
-# Copyright (C) 2013-2014 Droonga Project
+# Copyright (C) 2013-2015 Droonga Project
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public

  Modified: lib/droonga/process_supervisor.rb (+5 -15)
===================================================================
--- lib/droonga/process_supervisor.rb    2015-03-20 15:50:51 +0900 (ae1cc61)
+++ lib/droonga/process_supervisor.rb    2015-03-20 15:52:36 +0900 (751d3ae)
@@ -1,4 +1,4 @@
-# Copyright (C) 2014 Droonga Project
+# Copyright (C) 2014-2015 Droonga Project
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -15,19 +15,21 @@
 
 require "coolio"
 
+require "droonga/deferrable"
 require "droonga/process_control_protocol"
 require "droonga/line_buffer"
 
 module Droonga
   class ProcessSupervisor
+    include Deferrable
     include ProcessControlProtocol
 
+    attr_writer :on_finish
+
     def initialize(loop, input, output)
       @loop = loop
       @input = create_input(input)
       @output = create_output(output)
-      @on_ready = nil
-      @on_finish = nil
     end
 
     def start
@@ -48,14 +50,6 @@ module Droonga
       @output.write(Messages::STOP_IMMEDIATELY)
     end
 
-    def on_ready=(callback)
-      @on_ready = callback
-    end
-
-    def on_finish=(callback)
-      @on_finish = callback
-    end
-
     private
     def create_input(raw_input)
       input = Coolio::IO.new(raw_input)
@@ -80,10 +74,6 @@ module Droonga
       Coolio::IO.new(raw_output)
     end
 
-    def on_ready
-      @on_ready.call if @on_ready
-    end
-
     def on_finish
       @on_finish.call if @on_finish
     end

  Modified: lib/droonga/serf/agent.rb (+4 -6)
===================================================================
--- lib/droonga/serf/agent.rb    2015-03-20 15:50:51 +0900 (3b9bffe)
+++ lib/droonga/serf/agent.rb    2015-03-20 15:52:36 +0900 (dd19732)
@@ -18,6 +18,7 @@ require "English"
 require "coolio"
 
 require "droonga/loggable"
+require "droonga/deferrable"
 
 module Droonga
   class Serf
@@ -26,11 +27,10 @@ module Droonga
       PORT = 7946
 
       include Loggable
+      include Deferrable
 
       MAX_N_READ_CHECKS = 10
 
-      attr_writer :on_ready
-      attr_writer :on_failure
       def initialize(loop, serf, host, bind_port, rpc_port, *options)
         @loop = loop
         @serf = serf
@@ -39,8 +39,6 @@ module Droonga
         @rpc_port = rpc_port
         @options = options
         @pid = nil
-        @on_ready = nil
-        @on_failure = nil
         @n_ready_checks = 0
       end
 
@@ -186,7 +184,7 @@ module Droonga
         checker = Coolio::TCPSocket.connect(@host, @bind_port)
 
         on_connect = lambda do
-          @on_ready.call if @on_ready
+          on_ready
           checker.close
         end
         checker.on_connect do
@@ -195,7 +193,7 @@ module Droonga
 
         on_connect_failed = lambda do
           if @n_ready_checks >= MAX_N_READ_CHECKS
-            @on_failure.call if @on_failure
+            on_failure
           else
             timer = Coolio::TimerWatcher.new(1)
             on_timer = lambda do

  Modified: lib/droonga/slice.rb (+3 -7)
===================================================================
--- lib/droonga/slice.rb    2015-03-20 15:50:51 +0900 (d48b328)
+++ lib/droonga/slice.rb    2015-03-20 15:52:36 +0900 (6d88e65)
@@ -1,4 +1,4 @@
-# Copyright (C) 2013-2014 Droonga Project
+# Copyright (C) 2013-2015 Droonga Project
 #
 # 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,7 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
 require "droonga/loggable"
+require "droonga/deferrable"
 require "droonga/supervisor"
 require "droonga/event_loop"
 require "droonga/job_pusher"
@@ -23,8 +24,8 @@ require "droonga/schema_applier"
 module Droonga
   class Slice
     include Loggable
+    include Deferrable
 
-    attr_writer :on_ready
     def initialize(dataset, loop, options={})
       @dataset = dataset
       @loop = loop
@@ -34,7 +35,6 @@ module Droonga
       @job_pusher = JobPusher.new(@loop, @database_path)
       @processor = Processor.new(@loop, @job_pusher, @options)
       @supervisor = nil
-      @on_ready = nil
     end
 
     def start
@@ -123,10 +123,6 @@ module Droonga
       @supervisor.start
     end
 
-    def on_ready
-      @on_ready.call if @on_ready
-    end
-
     def log_tag
       "slice"
     end

  Modified: lib/droonga/supervisor.rb (+5 -17)
===================================================================
--- lib/droonga/supervisor.rb    2015-03-20 15:50:51 +0900 (97efa4d)
+++ lib/droonga/supervisor.rb    2015-03-20 15:52:36 +0900 (4bb9b0f)
@@ -1,4 +1,4 @@
-# Copyright (C) 2014 Droonga Project
+# Copyright (C) 2014-2015 Droonga Project
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -14,18 +14,18 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
 require "droonga/loggable"
+require "droonga/deferrable"
 require "droonga/process_supervisor"
 
 module Droonga
   class Supervisor
     include Loggable
+    include Deferrable
 
-    attr_writer :on_ready
     def initialize(loop, n_workers, config)
       @loop = loop
       @n_workers = n_workers
       @config = config
-      @on_ready = nil
     end
 
     def start
@@ -35,7 +35,7 @@ module Droonga
         worker_runner.on_ready = lambda do
           n_ready_workers += 1
           if n_ready_workers == @n_workers
-            @on_ready.call if @on_ready
+            on_ready
           end
         end
         worker_runner.start
@@ -87,15 +87,12 @@ module Droonga
 
     class WorkerRunner
       include Loggable
+      include Deferrable
 
-      attr_writer :on_ready
-      attr_writer :on_failure
       def initialize(loop, id, config)
         @loop = loop
         @id = id
         @config = config
-        @on_ready = nil
-        @on_failure = nil
         @stop_gracefully_callback = nil
       end
 
@@ -158,15 +155,6 @@ module Droonga
         supervisor
       end
 
-      def on_ready
-        @on_ready.call if @on_ready
-      end
-
-      def on_failure
-        # TODO: log
-        @on_failure.call if @on_failure
-      end
-
       def on_finish
         _, status = Process.waitpid2(@pid)
         @success = status.success?
-------------- next part --------------
HTML����������������������������...
Download 



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