Skip to content

Commit

Permalink
linux: guard duplicated instance (not adding a new option)
Browse files Browse the repository at this point in the history
Closes: fluent#611

Before:

We can launch Fluentd by /usr/sbin/fluentd even though fluentd service
is running.

Launching multiple Fluentd with the same config may cause
inconsistency of the buffers or the pos files.

After:

We can't launch Fluentd by /usr/sbin/fluentd with the default config
path if fluentd service is running.  If one of the following options is
specified, we can execute fluentd as before.

* --config (-c)
* --dry-run

Signed-off-by: Kentaro Hayashi <hayashi@clear-code.com>
  • Loading branch information
kenhys committed Jun 28, 2024
1 parent d15ce81 commit 36cbf6b
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 0 deletions.
8 changes: 8 additions & 0 deletions fluent-package/apt/systemd-test/install-newly.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ sudo fluent-gem install fluent-plugin-concat
/opt/fluent/bin/fluent-diagtool -t fluentd -o /tmp
test $(find /tmp/ -name gem_local_list.output | xargs cat) = "fluent-plugin-concat"

# Test: Guard duplicated instance
if [ "$1" = "local" ]; then
# FIXME: until guard feature was released, skip v5 and lts.
(! sudo /usr/sbin/fluentd)
(! sudo /usr/sbin/fluentd -v)
sudo /usr/sbin/fluentd --dry-run
fi

sudo apt remove -y fluent-package

case ${code_name} in
Expand Down
6 changes: 6 additions & 0 deletions fluent-package/apt/systemd-test/update-from-v4.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ sudo fluent-gem install fluent-plugin-concat
/opt/fluent/bin/fluent-diagtool -t fluentd -o /tmp
test $(find /tmp/ -name gem_local_list.output | xargs cat) = "fluent-plugin-concat"

# Test: Guard duplicated instance
(! sudo /usr/sbin/fluentd)
(! sudo /usr/sbin/td-agent)
(! sudo /usr/sbin/fluentd -v)
sudo /usr/sbin/fluentd --dry-run

# Uninstall
sudo apt remove -y fluent-package
(! systemctl status --no-pager td-agent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ sleep 3
test -e /var/log/fluent/fluentd.log
(! grep -e '\[error\]' -e '\[fatal\]' /var/log/fluent/fluentd.log)

# Test: Guard duplicated instance
(! sudo /usr/sbin/fluentd)
(! sudo /usr/sbin/td-agent)
(! sudo /usr/sbin/fluentd -v)
sudo /usr/sbin/fluentd --dry-run

# Uninstall
sudo apt remove -y fluent-package
(! systemctl status --no-pager td-agent)
Expand Down
6 changes: 6 additions & 0 deletions fluent-package/apt/systemd-test/update-to-next-version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ sleep 3
test -e /var/log/fluent/fluentd.log
(! grep -q -e '\[warn\]' -e '\[error\]' -e '\[fatal\]' /var/log/fluent/fluentd.log)

# Test: Guard duplicated instance
(! sudo /usr/sbin/fluentd)
(! sudo /usr/sbin/td-agent)
(! sudo /usr/sbin/fluentd -v)
sudo /usr/sbin/fluentd --dry-run

# Uninstall
sudo apt remove -y fluent-package
(! systemctl status --no-pager td-agent)
Expand Down
37 changes: 37 additions & 0 deletions fluent-package/templates/usr/sbin/fluentd.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,41 @@ if ARGV.include?("--version")
puts "fluent-package #{PACKAGE_VERSION} fluentd #{Fluent::VERSION} (#{FLUENTD_REVISION})"
exit 0
end

class DuplicateInstanceChecker
def running_fluentd?
IO.popen(["systemctl", "is-active", "fluentd"]) do |_io|
return true if _io.readlines.last.chomp == "active"
end
false
end

def prevent_duplicate_launch?
# check whether main process or not.
# e.g. directly spawned from shell
unless running_fluentd?
return false
end
true
end
end

if RUBY_PLATFORM =~ /linux/
checker = DuplicateInstanceChecker.new
if checker.prevent_duplicate_launch?
unless ARGV.include?("-c") or ARGV.include?("--config") or ARGV.include?("--dry-run")
puts("Error: Can't start duplicate Fluentd instance with the default config.")
if ARGV.include?("-v")
puts("To take the version, please use '--version', not '-v' ('--verbose').")
end
puts <<EOS
To start Fluentd, please do one of the following:
(Caution: Please be careful not to start multiple instances with the same config.)
- Stop the Fluentd service 'fluentd'.
- Specify the config path explicitly by '-c' ('--config').
EOS
exit 2
end
end
end
load "<%= install_path %>/bin/fluentd"
4 changes: 4 additions & 0 deletions fluent-package/yum/systemd-test/install-newly.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ if [ $1 = "local" ]; then
sudo $DNF install -y tar findutils
sudo /opt/fluent/bin/fluent-diagtool -t fluentd -o /tmp
test $(find /tmp/ -name gem_local_list.output | xargs cat) = "fluent-plugin-concat"
# FIXME: until guard feature was released, skip v5 and lts.
(! sudo /usr/sbin/fluentd)
(! sudo /usr/sbin/fluentd -v)
sudo /usr/sbin/fluentd --dry-run
fi

sudo $DNF remove -y fluent-package
Expand Down
6 changes: 6 additions & 0 deletions fluent-package/yum/systemd-test/update-from-v4.sh
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ sudo fluent-gem install fluent-plugin-concat
sudo /opt/fluent/bin/fluent-diagtool -t fluentd -o /tmp
test $(find /tmp/ -name gem_local_list.output | xargs cat) = "fluent-plugin-concat"

# Test: Guard duplicated instance
(! sudo /usr/sbin/fluentd)
(! sudo /usr/sbin/td-agent)
(! sudo /usr/sbin/fluentd -v)
sudo /usr/sbin/fluentd --dry-run

# Uninstall
sudo $DNF remove -y fluent-package
sudo systemctl daemon-reload
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ sudo fluent-gem install fluent-plugin-concat
sudo /opt/fluent/bin/fluent-diagtool -t fluentd -o /tmp
test $(find /tmp/ -name gem_local_list.output | xargs cat) = "fluent-plugin-concat"

# Test: Guard duplicated instance
(! sudo /usr/sbin/fluentd)
(! sudo /usr/sbin/td-agent)
(! sudo /usr/sbin/fluentd -v)
sudo /usr/sbin/fluentd --dry-run

# Uninstall
sudo $DNF remove -y fluent-package
(! systemctl status --no-pager td-agent)
Expand Down
6 changes: 6 additions & 0 deletions fluent-package/yum/systemd-test/update-to-next-version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ sudo fluent-gem install fluent-plugin-concat
sudo /opt/fluent/bin/fluent-diagtool -t fluentd -o /tmp
test $(find /tmp/ -name gem_local_list.output | xargs cat) = "fluent-plugin-concat"

# Test: Guard duplicated instance
(! sudo /usr/sbin/fluentd)
(! sudo /usr/sbin/td-agent)
(! sudo /usr/sbin/fluentd -v)
sudo /usr/sbin/fluentd --dry-run

# Uninstall
sudo $DNF remove -y fluent-package
sudo systemctl daemon-reload
Expand Down

0 comments on commit 36cbf6b

Please sign in to comment.