From 2a3f39d2b552d77b341050073f3022c5c1b16bee Mon Sep 17 00:00:00 2001 From: Daijiro Fukuda Date: Tue, 14 Mar 2023 10:54:43 +0900 Subject: [PATCH] SocketManager: Use new feature to select path automatically Use ServerEngine's new feature: https://github.com/treasure-data/serverengine/pull/143 On Windows, this prevents SocketManager from wrongly selecting an unavailable port, such as a port in excluded port range. Signed-off-by: Daijiro Fukuda --- lib/fluent/supervisor.rb | 10 ++++------ lib/fluent/test/driver/base.rb | 14 ++++++++++---- lib/fluent/test/startup_shutdown.rb | 14 ++++++-------- test/plugin/test_in_http.rb | 5 ++--- test/plugin_helper/test_server.rb | 13 +++++++++---- 5 files changed, 31 insertions(+), 25 deletions(-) diff --git a/lib/fluent/supervisor.rb b/lib/fluent/supervisor.rb index 043e447f5a..a9c1225df3 100644 --- a/lib/fluent/supervisor.rb +++ b/lib/fluent/supervisor.rb @@ -66,9 +66,8 @@ def before_run if config[:disable_shared_socket] $log.info "shared socket for multiple workers is disabled" else - socket_manager_path = ServerEngine::SocketManager::Server.generate_path - ServerEngine::SocketManager::Server.open(socket_manager_path) - ENV['SERVERENGINE_SOCKETMANAGER_PATH'] = socket_manager_path.to_s + server = ServerEngine::SocketManager::Server.open + ENV['SERVERENGINE_SOCKETMANAGER_PATH'] = server.path.to_s end end @@ -801,9 +800,8 @@ def configure(supervisor: false) private def create_socket_manager - socket_manager_path = ServerEngine::SocketManager::Server.generate_path - ServerEngine::SocketManager::Server.open(socket_manager_path) - ENV['SERVERENGINE_SOCKETMANAGER_PATH'] = socket_manager_path.to_s + server = ServerEngine::SocketManager::Server.open + ENV['SERVERENGINE_SOCKETMANAGER_PATH'] = server.path.to_s end def show_plugin_config diff --git a/lib/fluent/test/driver/base.rb b/lib/fluent/test/driver/base.rb index b53f863db4..9ba26edaf6 100644 --- a/lib/fluent/test/driver/base.rb +++ b/lib/fluent/test/driver/base.rb @@ -16,6 +16,7 @@ require 'fluent/config' require 'fluent/config/element' +require 'fluent/env' require 'fluent/log' require 'fluent/clock' @@ -102,11 +103,16 @@ def run(timeout: nil, start: true, shutdown: true, &block) def instance_start if @instance.respond_to?(:server_wait_until_start) - @socket_manager_path = ServerEngine::SocketManager::Server.generate_path - if @socket_manager_path.is_a?(String) && File.exist?(@socket_manager_path) - FileUtils.rm_f @socket_manager_path + if Fluent.windows? + @socket_manager_server = ServerEngine::SocketManager::Server.open + @socket_manager_path = @socket_manager_server.path + else + @socket_manager_path = ServerEngine::SocketManager::Server.generate_path + if @socket_manager_path.is_a?(String) && File.exist?(@socket_manager_path) + FileUtils.rm_f @socket_manager_path + end + @socket_manager_server = ServerEngine::SocketManager::Server.open(@socket_manager_path) end - @socket_manager_server = ServerEngine::SocketManager::Server.open(@socket_manager_path) ENV['SERVERENGINE_SOCKETMANAGER_PATH'] = @socket_manager_path.to_s end diff --git a/lib/fluent/test/startup_shutdown.rb b/lib/fluent/test/startup_shutdown.rb index 62da263f24..229fc407d2 100644 --- a/lib/fluent/test/startup_shutdown.rb +++ b/lib/fluent/test/startup_shutdown.rb @@ -21,9 +21,8 @@ module Fluent module Test module StartupShutdown def startup - socket_manager_path = ServerEngine::SocketManager::Server.generate_path - @server = ServerEngine::SocketManager::Server.open(socket_manager_path) - ENV['SERVERENGINE_SOCKETMANAGER_PATH'] = socket_manager_path.to_s + @server = ServerEngine::SocketManager::Server.open + ENV['SERVERENGINE_SOCKETMANAGER_PATH'] = @server.path.to_s end def shutdown @@ -31,15 +30,14 @@ def shutdown end def self.setup - @socket_manager_path = ServerEngine::SocketManager::Server.generate_path - @server = ServerEngine::SocketManager::Server.open(@socket_manager_path) - ENV['SERVERENGINE_SOCKETMANAGER_PATH'] = @socket_manager_path.to_s + @server = ServerEngine::SocketManager::Server.open + ENV['SERVERENGINE_SOCKETMANAGER_PATH'] = @server.path.to_s end def self.teardown @server.close - # on Windows, socket_manager_path is a TCP port number - FileUtils.rm_f @socket_manager_path unless Fluent.windows? + # on Windows, the path is a TCP port number + FileUtils.rm_f @server.path unless Fluent.windows? end end end diff --git a/test/plugin/test_in_http.rb b/test/plugin/test_in_http.rb index 93cada30a8..e66845520a 100644 --- a/test/plugin/test_in_http.rb +++ b/test/plugin/test_in_http.rb @@ -7,9 +7,8 @@ class HttpInputTest < Test::Unit::TestCase class << self def startup - socket_manager_path = ServerEngine::SocketManager::Server.generate_path - @server = ServerEngine::SocketManager::Server.open(socket_manager_path) - ENV['SERVERENGINE_SOCKETMANAGER_PATH'] = socket_manager_path.to_s + @server = ServerEngine::SocketManager::Server.open + ENV['SERVERENGINE_SOCKETMANAGER_PATH'] = @server.path.to_s end def shutdown diff --git a/test/plugin_helper/test_server.rb b/test/plugin_helper/test_server.rb index a08734ae46..0cf1aa5812 100644 --- a/test/plugin_helper/test_server.rb +++ b/test/plugin_helper/test_server.rb @@ -16,11 +16,16 @@ class Dummy < Fluent::Plugin::TestBase setup do @port = unused_port - @socket_manager_path = ServerEngine::SocketManager::Server.generate_path - if @socket_manager_path.is_a?(String) && File.exist?(@socket_manager_path) - FileUtils.rm_f @socket_manager_path + if Fluent.windows? + @socket_manager_server = ServerEngine::SocketManager::Server.open + @socket_manager_path = @socket_manager_server.path + else + @socket_manager_path = ServerEngine::SocketManager::Server.generate_path + if @socket_manager_path.is_a?(String) && File.exist?(@socket_manager_path) + FileUtils.rm_f @socket_manager_path + end + @socket_manager_server = ServerEngine::SocketManager::Server.open(@socket_manager_path) end - @socket_manager_server = ServerEngine::SocketManager::Server.open(@socket_manager_path) ENV['SERVERENGINE_SOCKETMANAGER_PATH'] = @socket_manager_path.to_s @d = Dummy.new