[Groonga-commit] droonga/droonga-engine at f110b12 [master] Observe changes of cluster-state.json by the master process itself

Back to archive index

YUKI Hiroshi null+****@clear*****
Wed Mar 25 14:32:32 JST 2015


YUKI Hiroshi	2015-03-25 14:32:32 +0900 (Wed, 25 Mar 2015)

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

  Message:
    Observe changes of cluster-state.json by the master process itself

  Modified files:
    lib/droonga/cluster.rb
    lib/droonga/command/droonga_engine.rb

  Modified: lib/droonga/cluster.rb (+22 -51)
===================================================================
--- lib/droonga/cluster.rb    2015-03-25 12:36:22 +0900 (75e3baa)
+++ lib/droonga/cluster.rb    2015-03-25 14:32:32 +0900 (29c4545)
@@ -16,10 +16,8 @@
 require "droonga/loggable"
 require "droonga/changable"
 require "droonga/path"
-require "droonga/file_observer"
 require "droonga/engine_node"
 require "droonga/node_metadata"
-require "droonga/restarter"
 
 module Droonga
   class Cluster
@@ -29,6 +27,27 @@ module Droonga
     class NoCatalogLoaded < StandardError
     end
 
+    class << self
+      def load_state_file
+        path = Path.cluster_state
+
+        return default_state unless path.exist?
+
+        contents = path.read
+        return default_state if contents.empty?
+
+        begin
+          JSON.parse(contents)
+        rescue JSON::ParserError
+          default_state
+        end
+      end
+
+      def default_state
+        {}
+      end
+    end
+
     attr_accessor :catalog
 
     def initialize(loop, params)
@@ -42,30 +61,13 @@ module Droonga
       reload
     end
 
-    def start_observe
-      return if @file_observer
-      @file_observer = FileObserver.new(@loop, Path.cluster_state)
-      @file_observer.on_change = lambda do
-        on_state_change
-      end
-      @file_observer.start
-    end
-
-    def stop_observe
-      return unless @file_observer
-      @file_observer.stop
-      @file_observer = nil
-    end
-
     def start
       engine_nodes.each do |node|
         node.start
       end
-      start_observe
     end
 
     def shutdown
-      stop_observe
       engine_nodes.each do |node|
         node.shutdown
       end
@@ -78,7 +80,7 @@ module Droonga
         old_state = nil
       end
       clear_cache
-      @state = load_state_file
+      @state = self.class.load_state_file
       if @state == old_state
         logger.info("cluster state not changed")
       else
@@ -133,21 +135,6 @@ module Droonga
       @writable_nodes    = nil
     end
 
-    def load_state_file
-      path = Path.cluster_state
-
-      return default_state unless path.exist?
-
-      contents = path.read
-      return default_state if contents.empty?
-
-      begin
-        JSON.parse(contents)
-      rescue JSON::ParserError
-        default_state
-      end
-    end
-
     def all_node_names
       raise NoCatalogLoaded.new unless @catalog
       @catalog.all_nodes
@@ -163,22 +150,6 @@ module Droonga
       end
     end
 
-    def on_state_change
-      unles****@state*****?
-        old_state =****@state*****
-        new_state = load_state_file
-        if old_state[@my_name] != new_state[@my_name]
-          Restarter.restart
-          return
-        end
-      end
-      reload
-    end
-
-    def default_state
-      {}
-    end
-
     def log_tag
       "cluster_state"
     end

  Modified: lib/droonga/command/droonga_engine.rb (+20 -0)
===================================================================
--- lib/droonga/command/droonga_engine.rb    2015-03-25 12:36:22 +0900 (165499d)
+++ lib/droonga/command/droonga_engine.rb    2015-03-25 14:32:32 +0900 (57f401d)
@@ -28,6 +28,7 @@ require "droonga/deferrable"
 require "droonga/path"
 require "droonga/node_name"
 require "droonga/serf"
+require "droonga/cluster"
 require "droonga/node_metadata"
 require "droonga/file_observer"
 require "droonga/process_supervisor"
@@ -415,6 +416,7 @@ module Droonga
             setup_initial_on_ready
             @restart_observer = run_restart_observer
             @catalog_observer = run_catalog_observer
+            @cluster_state_observer = run_cluster_state_observer
             @command_runner = run_command_runner
           end
 
@@ -463,6 +465,7 @@ module Droonga
 
         def stop_gracefully
           @command_runner.stop
+          @cluster_state_observer.stop
           @catalog_observer.stop
           @restart_observer.stop
           stop_serf
@@ -471,6 +474,7 @@ module Droonga
 
         def stop_immediately
           @command_runner.stop
+          @cluster_state_observer.stop
           @catalog_observer.stop
           @restart_observer.stop
           stop_serf
@@ -547,6 +551,22 @@ module Droonga
           catalog_observer
         end
 
+        def run_cluster_state_observer
+          previous_state = nil
+          cluster_state_observer = FileObserver.new(@loop, Path.cluster_state)
+          cluster_state_observer.on_change = lambda do
+            my_name   =****@confi*****_name
+            new_state = Cluster.load_state_file
+            if new_state and previous_state and
+                 new_state[my_name] != previous_state[my_name]
+              restart_graceful
+            end
+            previous_state = new_state
+          end
+          cluster_state_observer.start
+          cluster_state_observer
+        end
+
         def run_command_runner
           command_runner = CommandRunner.new(@loop)
           command_runner.on_command = lambda do |command|
-------------- next part --------------
HTML����������������������������...
Download 



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