Piro / YUKI Hiroshi
null+****@clear*****
Fri Apr 24 00:50:39 JST 2015
Piro / YUKI Hiroshi 2015-04-24 00:50:39 +0900 (Fri, 24 Apr 2015) New Revision: 43e011b7036e80234d20606dcfac9eae5fce87f0 https://github.com/droonga/droonga-engine/commit/43e011b7036e80234d20606dcfac9eae5fce87f0 Message: Export gracefully stopping process's last processed timestamp to cluster via file, not directly via serf Modified files: lib/droonga/engine.rb lib/droonga/path.rb lib/droonga/serf/remote_command.rb Modified: lib/droonga/engine.rb (+42 -11) =================================================================== --- lib/droonga/engine.rb 2015-04-23 23:25:15 +0900 (80fba7b) +++ lib/droonga/engine.rb 2015-04-24 00:50:39 +0900 (ae87e90) @@ -28,6 +28,7 @@ require "droonga/dispatcher" require "droonga/serf" require "droonga/serf/tag" require "droonga/file_observer" +require "droonga/safe_file_writer" module Droonga class Engine @@ -70,7 +71,7 @@ module Droonga @state.start @cluster.start @dispatcher.start - @export_last_processed_message_timestamp_observer = run_export_last_processed_message_timestamp_observer + @last_processed_message_timestamp_observer = run_last_processed_message_timestamp_observer logger.trace("start: done") end @@ -79,14 +80,14 @@ module Droonga @cluster.shutdown on_finish = lambda do logger.trace("stop_gracefully: middle") - @export_last_processed_message_timestamp_observer.stop + @last_processed_message_timestamp_observer.stop @dispatcher.stop_gracefully do @state.shutdown yield #XXX We must save last processed message timstamp # based on forwarded/dispatched messages while # "graceful stop" operations. - export_last_processed_message_timestamp + export_last_processed_message_timestamp_to_file logger.trace("stop_gracefully: done") end end @@ -102,11 +103,11 @@ module Droonga # It may be called after stop_gracefully. def stop_immediately logger.trace("stop_immediately: start") - @export_last_processed_message_timestamp_observer.stop + @last_processed_message_timestamp_observer.stop @dispatcher.stop_immediately @cluster.shutdown @state.shutdown - export_last_processed_message_timestamp + export_last_processed_message_timestamp_to_file logger.trace("stop_immediately: done") end @@ -143,8 +144,8 @@ module Droonga MICRO_SECONDS_DECIMAL_PLACE = 6 - def export_last_processed_message_timestamp - logger.trace("export_last_processed_message_timestamp: start") + def export_last_processed_message_timestamp_to_cluster + logger.trace("export_last_processed_message_timestamp_to_cluster: start") if @last_processed_message_timestamp timestamp = @last_processed_message_timestamp serf = Serf.new(@name) @@ -157,18 +158,48 @@ module Droonga :timestamp => timestamp) end end - logger.trace("export_last_processed_message_timestamp: done") + logger.trace("export_last_processed_message_timestamp_to_cluster: done") end - def run_export_last_processed_message_timestamp_observer - observer = FileObserver.new(@loop, Path.export_last_processed_message_timestamp) + def export_last_processed_message_timestamp_to_file + old_timestamp = read_last_processed_message_timestamp + if old_timestamp and + old_timestamp > @last_processed_message_timestamp + return + end + path = Path.last_processed_message_timestamp + SafeFileWriter.write(path) do |output, file| + timestamp = @last_processed_message_timestamp + timestamp = timestamp.utc.iso8601(MICRO_SECONDS_DECIMAL_PLACE) + output.puts(timestamp) + end + end + + def run_last_processed_message_timestamp_observer + path = Path.last_processed_message_timestamp + observer = FileObserver.new(@loop, path) observer.on_change = lambda do - export_last_processed_message_timestamp + timestamp = read_last_processed_message_timestamp + if timestamp + if @last_processed_message_timestamp.nil? or + timestamp > @last_processed_message_timestamp + @last_processed_message_timestamp = timestamp + end + end + export_last_processed_message_timestamp_to_cluster end observer.start observer end + def read_last_processed_message_timestamp + file = Path.last_processed_message_timestamp + return nil unless file.exist? + timestamp = file.read + return nil if timestamp.nil? or timestamp.empty? + Time.parse(timestamp) + end + def log_tag "engine" end Modified: lib/droonga/path.rb (+2 -2) =================================================================== --- lib/droonga/path.rb 2015-04-23 23:25:15 +0900 (05be6d8) +++ lib/droonga/path.rb 2015-04-24 00:50:39 +0900 (00ebe4c) @@ -61,8 +61,8 @@ module Droonga Pathname.new(base_file_name).expand_path(base) end - def export_last_processed_message_timestamp - base + "export-last-processed-message-timestamp.txt" + def last_processed_message_timestamp + base + "last-processed-message-timestamp.txt" end def accidental_buffer Modified: lib/droonga/serf/remote_command.rb (+1 -1) =================================================================== --- lib/droonga/serf/remote_command.rb 2015-04-23 23:25:15 +0900 (8451df0) +++ lib/droonga/serf/remote_command.rb 2015-04-24 00:50:39 +0900 (e8e1e21) @@ -123,7 +123,7 @@ module Droonga class ExportLastProcessedMessageTimestamp < Base def process - FileUtils.touch(Path.export_last_processed_message_timestamp.to_s) + FileUtils.touch(Path.last_processed_message_timestamp.to_s) end end -------------- next part -------------- HTML����������������������������...Download