[Groonga-commit] droonga/droonga-engine at ab4bfc9 [buffered-forward] Unify LiveNodesList to ClusterState

Zurück zum Archiv-Index

YUKI Hiroshi null+****@clear*****
Fri Dec 26 15:31:32 JST 2014


YUKI Hiroshi	2014-12-26 15:31:32 +0900 (Fri, 26 Dec 2014)

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

  Message:
    Unify LiveNodesList to ClusterState
    
    Conflicts:
    	lib/droonga/cluster_state.rb
    	lib/droonga/engine_state.rb

  Removed files:
    lib/droonga/live_nodes_list.rb
    lib/droonga/live_nodes_list_loader.rb
  Modified files:
    lib/droonga/cluster_state.rb
    lib/droonga/command/remote.rb
    lib/droonga/command/serf_event_handler.rb
    lib/droonga/engine.rb
    lib/droonga/engine_state.rb
    lib/droonga/path.rb
    lib/droonga/serf.rb

  Modified: lib/droonga/cluster_state.rb (+103 -20)
===================================================================
--- lib/droonga/cluster_state.rb    2014-12-26 13:08:41 +0900 (efe61fe)
+++ lib/droonga/cluster_state.rb    2014-12-26 15:31:32 +0900 (04e7e3e)
@@ -14,6 +14,7 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
 require "droonga/loggable"
+require "droonga/file_observer"
 require "droonga/node_metadata"
 
 module Droonga
@@ -23,43 +24,74 @@ module Droonga
     attr_accessor :catalog
     attr_writer :on_change
 
-    def initialize
+    def initialize(loop)
+      @loop = loop
+
       @catalog = nil
-      @live_nodes_list = nil
+      @state = nil
       @on_change = nil
+
+      @file_observer = FileObserver.new(loop, Path.cluster_state)
+      @file_observer.on_change = lambda do
+        reload
+      end
+
+      reload
+    end
+
+    def start_observe
+      @file_observer.start
+    end
+
+    def stop_observe
+      @file_observer.stop
+    end
+
+    def reload
+      old_state = to_hash
+      clear_cache
+      @state = state
+      logger.info("live-nodes-list loaded")
+      unless to_hash == old_state
+        on_change
+      end
     end
 
     def all_nodes
-      @catalog.all_nodes
+      if @catalog
+        @catalog.all_nodes
+      else
+        []
+      end
     end
 
     def dead_nodes
-      if @live_nodes_list
-        @live_nodes_list.dead_nodes
+      if @state
+        @dead_nodes ||= collect_dead_nodes
       else
         []
       end
     end
 
     def service_provider_nodes
-      if @live_nodes_list
-        @live_nodes_list.service_provider_nodes
+      if @state
+        @service_provider_nodes ||= collect_nodes_by_role(NodeMetadata::Role::SERVICE_PROVIDER)
       else
         all_nodes
       end
     end
 
     def absorb_source_nodes
-      if @live_nodes_list
-        @live_nodes_list.absorb_source_nodes
+      if @state
+        @absorb_source_nodes ||= collect_nodes_by_role(NodeMetadata::Role::ABSORB_SOURCE)
       else
         []
       end
     end
 
     def absorb_destination_nodes
-      if @live_nodes_list
-        @live_nodes_list.absorb_destination_nodes
+      if @state
+        @absorb_destination_nodes ||= collect_nodes_by_role(NodeMetadata::Role::ABSORB_DESTINATION)
       else
         []
       end
@@ -107,20 +139,71 @@ module Droonga
       end
     end
 
-    def live_nodes_list=(new_nodes_list)
-      old_live_nodes_list = @live_nodes_list
-      @live_nodes_list = new_nodes_list
-      unless old_live_nodes_list == new_nodes_list
-        on_change
-      end
-      @live_nodes_list
-    end
-
     def on_change
       @on_change.call if @on_change
     end
 
     private
+    def to_hash
+      return nil unless @state
+
+      {
+        :all                => @state.keys,
+        :dead               => dead_nodes,
+        :service_provider   => service_provider_nodes,
+        :absorb_source      => absorb_source_nodes,
+        :absorb_destination => absorb_destination_nodes,
+      }
+    end
+
+    def clear_cache
+      @dead_nodes = nil
+      @service_provider_nodes = nil
+      @absorb_source_nodes = nil
+      @absorb_destination_nodes = nil
+    end
+
+    def state
+      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
+
+    def collect_dead_nodes
+      nodes = []
+      @state.each do |name, state|
+        unless state["live"]
+          nodes << name
+        end
+      end
+      nodes.sort
+    end
+
+    def collect_nodes_by_role(role)
+      nodes = []
+      @state.each do |name, state|
+        if not state["foreign"] and
+             state["tags"]["type"] == "engine" and
+             state["tags"]["role"] == role
+          nodes << name
+        end
+      end
+      nodes.sort
+    end
+
     def node_metadata
       @node_metadata ||= NodeMetadata.new
     end

  Modified: lib/droonga/command/remote.rb (+2 -2)
===================================================================
--- lib/droonga/command/remote.rb    2014-12-26 13:08:41 +0900 (47a05e2)
+++ lib/droonga/command/remote.rb    2014-12-26 15:31:32 +0900 (2ffd9ec)
@@ -395,9 +395,9 @@ module Droonga
         end
       end
 
-      class UpdateLiveNodesList < Base
+      class UpdateClusterState < Base
         def process
-          @serf.update_live_nodes_list
+          @serf.update_cluster_state
         end
       end
     end

  Modified: lib/droonga/command/serf_event_handler.rb (+1 -1)
===================================================================
--- lib/droonga/command/serf_event_handler.rb    2014-12-26 13:08:41 +0900 (be992aa)
+++ lib/droonga/command/serf_event_handler.rb    2014-12-26 15:31:32 +0900 (b19d281)
@@ -66,7 +66,7 @@ module Droonga
           @payload = JSON.parse($stdin.gets)
           detect_command_class_from_custom_event(ENV["SERF_QUERY_NAME"])
         when "member-join", "member-leave", "member-update", "member-reap"
-          Remote::UpdateLiveNodesList
+          Remote::UpdateClusterState
         else
           nil
         end

  Modified: lib/droonga/engine.rb (+3 -18)
===================================================================
--- lib/droonga/engine.rb    2014-12-26 13:08:41 +0900 (e9d0912)
+++ lib/droonga/engine.rb    2014-12-26 15:31:32 +0900 (d5fb0da)
@@ -23,7 +23,6 @@ require "droonga/engine_state"
 require "droonga/catalog_loader"
 require "droonga/dispatcher"
 require "droonga/file_observer"
-require "droonga/live_nodes_list_loader"
 require "droonga/node_metadata"
 
 module Droonga
@@ -33,14 +32,9 @@ module Droonga
     attr_writer :on_ready
     def initialize(loop, name, internal_name)
       @state = EngineState.new(loop, name, internal_name)
-      @state.cluster.live_nodes_list = load_live_nodes_list
       @catalog = load_catalog
       @state.catalog = @catalog
       @dispatcher = create_dispatcher
-      @live_nodes_list_observer = FileObserver.new(loop, Path.live_nodes_list)
-      @live_nodes_list_observer.on_change = lambda do
-        @state.cluster.live_nodes_list = load_live_nodes_list
-      end
       @node_metadata_observer = FileObserver.new(loop, Path.node_metadata)
       @node_metadata_observer.on_change = lambda do
         logger.trace("reloading node_metadata: start")
@@ -56,7 +50,7 @@ module Droonga
         @on_ready.call if @on_ready
       end
       @state.start
-      @live_nodes_list_observer.start
+      @state.cluster.start_observe
       @node_metadata_observer.start
       @dispatcher.start
       logger.trace("start: done")
@@ -64,7 +58,7 @@ module Droonga
 
     def stop_gracefully
       logger.trace("stop_gracefully: start")
-      @live_nodes_list_observer.stop
+      @state.cluster.stop_observe
       @node_metadata_observer.stop
       on_finish = lambda do
         logger.trace("stop_gracefully/on_finish: start")
@@ -89,7 +83,7 @@ module Droonga
     def stop_immediately
       logger.trace("stop_immediately: start")
       save_last_processed_message_timestamp
-      @live_nodes_list_observer.stop
+      @state.cluster.stop_observe
       @node_metadata_observer.stop
       @dispatcher.stop_immediately
       @state.shutdown
@@ -117,15 +111,6 @@ module Droonga
       catalog
     end
 
-    def load_live_nodes_list
-      path = Path.live_nodes_list
-      loader = LiveNodesListLoader.new(path)
-      live_nodes_list = loader.load
-      logger.info("live-nodes-list loaded",
-                  :path  => path.to_s)
-      live_nodes_list
-    end
-
     def create_dispatcher
       Dispatcher.new(@state, @catalog)
     end

  Modified: lib/droonga/engine_state.rb (+1 -1)
===================================================================
--- lib/droonga/engine_state.rb    2014-12-26 13:08:41 +0900 (05e12ab)
+++ lib/droonga/engine_state.rb    2014-12-26 15:31:32 +0900 (b9d1f84)
@@ -43,7 +43,7 @@ module Droonga
       @internal_name = internal_name
       @sessions = {}
       @current_id = 0
-      @cluster = ClusterState.new
+      @cluster = ClusterState.new(@loop)
       @forwarder = BufferedForwarder.new(@loop,
                                          :cluster_state => @cluster)
       @cluster.on_change = lambda do

  Deleted: lib/droonga/live_nodes_list.rb (+0 -88) 100644
===================================================================
--- lib/droonga/live_nodes_list.rb    2014-12-26 13:08:41 +0900 (deeaddb)
+++ /dev/null
@@ -1,88 +0,0 @@
-# Copyright (C) 2014 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
-
-require "droonga/node_metadata"
-
-module Droonga
-  class LiveNodesList
-    def initialize(nodes)
-      @nodes = nodes
-    end
-
-    def all_nodes
-      @all_nodes ||=****@nodes*****
-    end
-
-    def dead_nodes
-      @dead_nodes ||= collect_dead_nodes
-    end
-
-    def absorb_source_nodes
-      @absorb_source_nodes ||= collect_absorb_source_nodes
-    end
-
-    def absorb_destination_nodes
-      @absorb_destination_nodes ||= collect_absorb_destination_nodes
-    end
-
-    def service_provider_nodes
-      @service_provider_nodes ||= collect_service_provider_nodes
-    end
-
-    def ==(nodes_list)
-      nodes_list.is_a?(self.class) and
-        nodes_list.all_nodes == all_nodes and
-        nodes_list.dead_nodes == dead_nodes and
-        nodes_list.absorb_source_nodes == absorb_source_nodes and
-        nodes_list.absorb_destination_nodes == absorb_destination_nodes and
-        nodes_list.service_provider_nodes == service_provider_nodes
-    end
-
-    private
-    def collect_dead_nodes
-      nodes = []
-      @nodes.each do |name, state|
-        unless state["live"]
-          nodes << name
-        end
-      end
-      nodes.sort
-    end
-
-    def collect_nodes_by_role(role)
-      nodes = []
-      @nodes.each do |name, state|
-        if not state["foreign"] and
-             state["tags"]["type"] == "engine" and
-             state["tags"]["role"] == role
-          nodes << name
-        end
-      end
-      nodes.sort
-    end
-
-    def collect_service_provider_nodes
-      collect_nodes_by_role(NodeMetadata::Role::SERVICE_PROVIDER)
-    end
-
-    def collect_absorb_source_nodes
-      collect_nodes_by_role(NodeMetadata::Role::ABSORB_SOURCE)
-    end
-
-    def collect_absorb_destination_nodes
-      collect_nodes_by_role(NodeMetadata::Role::ABSORB_DESTINATION)
-    end
-  end
-end

  Deleted: lib/droonga/live_nodes_list_loader.rb (+0 -50) 100644
===================================================================
--- lib/droonga/live_nodes_list_loader.rb    2014-12-26 13:08:41 +0900 (3df662d)
+++ /dev/null
@@ -1,50 +0,0 @@
-# Copyright (C) 2014 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
-
-require "pathname"
-require "json"
-
-require "droonga/live_nodes_list"
-
-module Droonga
-  class LiveNodesListLoader
-    def initialize(path)
-      @path = path
-    end
-
-    def load
-      list = parse
-      LiveNodesList.new(list)
-    end
-
-    private
-    def parse
-      return default_list unles****@path*****?
-
-      contents =****@path*****
-      return default_list if contents.empty?
-
-      begin
-        JSON.parse(contents)
-      rescue JSON::ParserError
-        default_list
-      end
-    end
-
-    def default_list
-      {}
-    end
-  end
-end

  Modified: lib/droonga/path.rb (+2 -2)
===================================================================
--- lib/droonga/path.rb    2014-12-26 13:08:41 +0900 (ab547a1)
+++ lib/droonga/path.rb    2014-12-26 15:31:32 +0900 (1b9278b)
@@ -48,8 +48,8 @@ module Droonga
         state + "metadata.json"
       end
 
-      def live_nodes_list
-        state + "live-nodes.json"
+      def cluster_state
+        state + "cluster-state.json"
       end
 
       def config

  Modified: lib/droonga/serf.rb (+5 -5)
===================================================================
--- lib/droonga/serf.rb    2014-12-26 13:08:41 +0900 (cc53f14)
+++ lib/droonga/serf.rb    2014-12-26 15:31:32 +0900 (5e2eb09)
@@ -70,7 +70,7 @@ module Droonga
                    *retry_joins)
       Thread.new do
         sleep 1 # wait until the serf agent becomes running
-        update_live_nodes_list if****@agent*****?
+        update_cluster_state if****@agent*****?
       end
       logger.trace("start: done")
     end
@@ -124,9 +124,9 @@ module Droonga
       result
     end
 
-    def update_live_nodes_list
-      path = Path.live_nodes_list
-      new_list = live_nodes_list
+    def update_cluster_state
+      path = Path.cluster_state
+      new_state = current_cluster_state
       file_contents = JSON.pretty_generate(new_list)
       SafeFileWriter.write(path) do |output, file|
         output.puts(file_contents)
@@ -134,7 +134,7 @@ module Droonga
       end
     end
 
-    def live_nodes_list
+    def current_cluster_state
       ensure_serf
       nodes_list = {}
       result = run_once("members", "-format", "json")
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Zurück zum Archiv-Index