SlideShare ist ein Scribd-Unternehmen logo
1 von 22
Downloaden Sie, um offline zu lesen
Is Ruby Logger
Thread(Process)safe?
RubyConf 2013 @Miami
SEO Naotoshi (@sonots)
2013/11/09
Naotoshi SEO
like Now-toshi Say-oh

@sonots (twitter, github)
Dev Engineer for Operations
Became as a commiter of Fluentd
•realtime pipelining of log streams
•plugins written in ruby
Log Management Tool
•syslog-ng / rsyslog Only collection
Not Free
•splunk
No Plugin
•scribed
Java Plugin
•flumeNG
Ruby Plugin (Not Gem)
•logstash
•fluentd Ruby Plugin (Gem Support)
Try the
fluentd!
It s very nice tool, check it out!
http://fluentd.org/
Today s
Talk
Logger
We needed a
Logger which
works safely
in multiprocess
for Fluentd
Examined Ruby Logger
• 1) Will logs not be mixtured in multi-threads?
• 2) Will logs not be mixtured in multi-processes?
• 3) Does log rotation work safely in multi-threads?
• 4) Does log rotation work safely in multi-processes?
1) Will logs not be mixtured in multi-threads?
Code

require 'logger'
require 'parallel'
logger = Logger.new("/tmp/test.log")
Parallel.map(['a', 'b'], :in_threads => 2) do |letter|
10000.times do
logger.info letter * 5000
end
end
Result

$ egrep -e 'ab' -e 'ba' /tmp/test.log
[empty]

OK
WHY?
logger.rb#L559
def write(message)
begin
@mutex.synchronize do
...
@dev.write(message)
...
end

Ruby’s Logger is taking mutex to be thread-safe.
2) Will logs not be mixtured in multi-processes?
Code
require 'logger'
require 'parallel'
logger = Logger.new("/tmp/test.log")
Parallel.map(['a', 'b'], :in_processes => 2) do |letter|
10000.times do
logger.info letter * 5000
end
end
Result
$ egrep -e 'ab' -e 'ba' /tmp/test.log
[empty]

OK
Really? Why?
Mutex does not work to exclusively lock in multiprocesses environment.
Atomic/non-atomic: A write is atomic if the whole amount written in one
operation is not interleaved with data from any other process. This is
useful when there are multiple writers sending data to a single reader.
Applications need to know how large a write request can be expected to
be performed atomically. This maximum is called {PIPE_BUF}. This volume
of IEEE Std 1003.1-2001 does not say whether write requests for more
than {PIPE_BUF} bytes are atomic, but requires that writes of {PIPE_BUF} or
fewer bytes shall be atomic.

But, Linux system call write(2) itself is atomic as long
as writing to a local file.

OK
3) Does log rotation work safely multi-threads?
Code
require 'logger'
require 'parallel'
logger = Logger.new("/tmp/test.log", 3, 1024 * 10)
Parallel.map(['a', 'b'], :in_threads => 2) do |letter|
10000.times do
logger.info letter * 5000
end
end
Result
$ ls -l /tmp/test*
-rw-r--r-- 1 sonots sonots 10806

9月 29 23:03 /tmp/test.log

-rw-r--r-- 1 sonots sonots 10806

9月 29 23:03 /tmp/test.log.0

-rw-r--r-- 1 sonots sonots 10806

9月 29 23:03 /tmp/test.log.1

No Error, OK
4) Does log rotation work safely in multi-processes?
Code
require 'logger'
require 'parallel'
logger = Logger.new("/tmp/test.log", 3, 1024 * 10)
Parallel.map(['a', 'b'], :in_processes => 2) do |letter|
10000.times do
logger.info letter * 5000
end
end
Result
log
log
log
log
...

writing failed. closed stream
shifting failed. closed stream
writing failed. closed stream
shifting failed. closed stream

Oops, Bad!
WHY?
logger.rb#L559
def write(message)
begin
@mutex.synchronize do
...
check_shift_log
...
end

Ruby’s Logger is taking mutex to be thread-safe,
but was not treating anything for multi-processes.
We(@frsyuki and me) Fixed

• The approach is to use flock(2) without creating
another *.lock file
Pull requested to ruby

My first contribution to ruby
Merged!!
Thanks to @naruse for his review and advice!!
Will be released
with 2.1.0!!
on 12/25
CONCLUSION
• Ruby Logger was not safe in multiprocesses, but it is safe now!!!

• Try ruby-trunk or
https://github.com/sonots/
process_safe_logger
Try the
fluentd!
It s very nice tool, check it out!
http://fluentd.org/

Weitere ähnliche Inhalte

Was ist angesagt?

Staring into the eBPF Abyss
Staring into the eBPF AbyssStaring into the eBPF Abyss
Staring into the eBPF Abyss
Sasha Goldshtein
 
Systemtap
SystemtapSystemtap
Systemtap
Feng Yu
 
netfilter and iptables
netfilter and iptablesnetfilter and iptables
netfilter and iptables
Kernel TLV
 
Modern Linux Tracing Landscape
Modern Linux Tracing LandscapeModern Linux Tracing Landscape
Modern Linux Tracing Landscape
Kernel TLV
 

Was ist angesagt? (20)

Understanding eBPF in a Hurry!
Understanding eBPF in a Hurry!Understanding eBPF in a Hurry!
Understanding eBPF in a Hurry!
 
Tracing MariaDB server with bpftrace - MariaDB Server Fest 2021
Tracing MariaDB server with bpftrace - MariaDB Server Fest 2021Tracing MariaDB server with bpftrace - MariaDB Server Fest 2021
Tracing MariaDB server with bpftrace - MariaDB Server Fest 2021
 
Linux crontab
Linux crontabLinux crontab
Linux crontab
 
Linux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloudLinux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloud
 
Andrea Righi - Spying on the Linux kernel for fun and profit
Andrea Righi - Spying on the Linux kernel for fun and profitAndrea Righi - Spying on the Linux kernel for fun and profit
Andrea Righi - Spying on the Linux kernel for fun and profit
 
Bpf performance tools chapter 4 bcc
Bpf performance tools chapter 4   bccBpf performance tools chapter 4   bcc
Bpf performance tools chapter 4 bcc
 
Linux fundamental - Chap 14 shell script
Linux fundamental - Chap 14 shell scriptLinux fundamental - Chap 14 shell script
Linux fundamental - Chap 14 shell script
 
eBPF Trace from Kernel to Userspace
eBPF Trace from Kernel to UserspaceeBPF Trace from Kernel to Userspace
eBPF Trace from Kernel to Userspace
 
Windows IOCP vs Linux EPOLL Performance Comparison
Windows IOCP vs Linux EPOLL Performance ComparisonWindows IOCP vs Linux EPOLL Performance Comparison
Windows IOCP vs Linux EPOLL Performance Comparison
 
Performance Analysis Tools for Linux Kernel
Performance Analysis Tools for Linux KernelPerformance Analysis Tools for Linux Kernel
Performance Analysis Tools for Linux Kernel
 
Staring into the eBPF Abyss
Staring into the eBPF AbyssStaring into the eBPF Abyss
Staring into the eBPF Abyss
 
Make Your Containers Faster: Linux Container Performance Tools
Make Your Containers Faster: Linux Container Performance ToolsMake Your Containers Faster: Linux Container Performance Tools
Make Your Containers Faster: Linux Container Performance Tools
 
zebra & openconfigd Introduction
zebra & openconfigd Introductionzebra & openconfigd Introduction
zebra & openconfigd Introduction
 
Systemtap
SystemtapSystemtap
Systemtap
 
Security Monitoring with eBPF
Security Monitoring with eBPFSecurity Monitoring with eBPF
Security Monitoring with eBPF
 
netfilter and iptables
netfilter and iptablesnetfilter and iptables
netfilter and iptables
 
Porting and Optimization of Numerical Libraries for ARM SVE
Porting and Optimization of Numerical Libraries for ARM SVEPorting and Optimization of Numerical Libraries for ARM SVE
Porting and Optimization of Numerical Libraries for ARM SVE
 
Modern Linux Tracing Landscape
Modern Linux Tracing LandscapeModern Linux Tracing Landscape
Modern Linux Tracing Landscape
 
Profiling and optimizing go programs
Profiling and optimizing go programsProfiling and optimizing go programs
Profiling and optimizing go programs
 
A brief history of system calls
A brief history of system callsA brief history of system calls
A brief history of system calls
 

Ähnlich wie Is ruby logger thread(process)-safe? at RubyConf 2013

Efficient logging in multithreaded C++ server
Efficient logging in multithreaded C++ serverEfficient logging in multithreaded C++ server
Efficient logging in multithreaded C++ server
Shuo Chen
 

Ähnlich wie Is ruby logger thread(process)-safe? at RubyConf 2013 (20)

Will iPython replace Bash?
Will iPython replace Bash?Will iPython replace Bash?
Will iPython replace Bash?
 
Will iPython replace bash?
Will iPython replace bash?Will iPython replace bash?
Will iPython replace bash?
 
OSDC 2016 - Ingesting Logs with Style by Pere Urbon-Bayes
OSDC 2016 - Ingesting Logs with Style by Pere Urbon-BayesOSDC 2016 - Ingesting Logs with Style by Pere Urbon-Bayes
OSDC 2016 - Ingesting Logs with Style by Pere Urbon-Bayes
 
Efficient logging in multithreaded C++ server
Efficient logging in multithreaded C++ serverEfficient logging in multithreaded C++ server
Efficient logging in multithreaded C++ server
 
Nodejs a-practical-introduction-oredev
Nodejs a-practical-introduction-oredevNodejs a-practical-introduction-oredev
Nodejs a-practical-introduction-oredev
 
epoll() - The I/O Hero
epoll() - The I/O Heroepoll() - The I/O Hero
epoll() - The I/O Hero
 
Open Source Swift Under the Hood
Open Source Swift Under the HoodOpen Source Swift Under the Hood
Open Source Swift Under the Hood
 
Swift 2 Under the Hood - Gotober 2015
Swift 2 Under the Hood - Gotober 2015Swift 2 Under the Hood - Gotober 2015
Swift 2 Under the Hood - Gotober 2015
 
Application Logging in the 21st century - 2014.key
Application Logging in the 21st century - 2014.keyApplication Logging in the 21st century - 2014.key
Application Logging in the 21st century - 2014.key
 
uWSGI - Swiss army knife for your Python web apps
uWSGI - Swiss army knife for your Python web appsuWSGI - Swiss army knife for your Python web apps
uWSGI - Swiss army knife for your Python web apps
 
How do event loops work in Python?
How do event loops work in Python?How do event loops work in Python?
How do event loops work in Python?
 
Threads
ThreadsThreads
Threads
 
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple SpacesConcurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
 
Fedora Developer's Conference 2014 Talk
Fedora Developer's Conference 2014 TalkFedora Developer's Conference 2014 Talk
Fedora Developer's Conference 2014 Talk
 
The basics of fluentd
The basics of fluentdThe basics of fluentd
The basics of fluentd
 
CocoaConf: The Language of Mobile Software is APIs
CocoaConf: The Language of Mobile Software is APIsCocoaConf: The Language of Mobile Software is APIs
CocoaConf: The Language of Mobile Software is APIs
 
3.1.c apend scripting, crond, atd
3.1.c apend   scripting, crond, atd3.1.c apend   scripting, crond, atd
3.1.c apend scripting, crond, atd
 
Syslog Protocols
Syslog ProtocolsSyslog Protocols
Syslog Protocols
 
Возможности интерпретатора Python в NX-OS
Возможности интерпретатора Python в NX-OSВозможности интерпретатора Python в NX-OS
Возможности интерпретатора Python в NX-OS
 
101 apend. scripting, crond, atd
101 apend. scripting, crond, atd101 apend. scripting, crond, atd
101 apend. scripting, crond, atd
 

Mehr von Naotoshi Seo

HTTP/2 でリバプロするだけでグラフツールを 高速化できた話
HTTP/2 でリバプロするだけでグラフツールを 高速化できた話HTTP/2 でリバプロするだけでグラフツールを 高速化できた話
HTTP/2 でリバプロするだけでグラフツールを 高速化できた話
Naotoshi Seo
 
Mobage を支える Ruby の技術 ~ 複数DB編 ~
Mobage を支える Ruby の技術 ~ 複数DB編 ~Mobage を支える Ruby の技術 ~ 複数DB編 ~
Mobage を支える Ruby の技術 ~ 複数DB編 ~
Naotoshi Seo
 
InfluxDB の概要 - sonots #tokyoinfluxdb
InfluxDB の概要 - sonots #tokyoinfluxdbInfluxDB の概要 - sonots #tokyoinfluxdb
InfluxDB の概要 - sonots #tokyoinfluxdb
Naotoshi Seo
 
Sinatra Pattern 20130415
Sinatra Pattern 20130415Sinatra Pattern 20130415
Sinatra Pattern 20130415
Naotoshi Seo
 
Serf という Orchestration ツール #immutableinfra
Serf という Orchestration ツール #immutableinfraSerf という Orchestration ツール #immutableinfra
Serf という Orchestration ツール #immutableinfra
Naotoshi Seo
 
Shadow Server on Fluentd at Fluentd Casual Talks #3
Shadow Server on Fluentd at Fluentd Casual Talks #3Shadow Server on Fluentd at Fluentd Casual Talks #3
Shadow Server on Fluentd at Fluentd Casual Talks #3
Naotoshi Seo
 
Haikanko rubykaigi 20130531
Haikanko rubykaigi 20130531Haikanko rubykaigi 20130531
Haikanko rubykaigi 20130531
Naotoshi Seo
 
Fluentdcasual 02-haikanko
Fluentdcasual 02-haikankoFluentdcasual 02-haikanko
Fluentdcasual 02-haikanko
Naotoshi Seo
 
capistrano-colorized-stream
capistrano-colorized-streamcapistrano-colorized-stream
capistrano-colorized-stream
Naotoshi Seo
 

Mehr von Naotoshi Seo (13)

ぼくのかんがえた Itamae/Serverspec 構成フレームワーク 〜 Kondate 〜
ぼくのかんがえた Itamae/Serverspec 構成フレームワーク 〜 Kondate 〜ぼくのかんがえた Itamae/Serverspec 構成フレームワーク 〜 Kondate 〜
ぼくのかんがえた Itamae/Serverspec 構成フレームワーク 〜 Kondate 〜
 
HTTP/2 でリバプロするだけでグラフツールを 高速化できた話
HTTP/2 でリバプロするだけでグラフツールを 高速化できた話HTTP/2 でリバプロするだけでグラフツールを 高速化できた話
HTTP/2 でリバプロするだけでグラフツールを 高速化できた話
 
Mobage を支える Ruby の技術 ~ 複数DB編 ~
Mobage を支える Ruby の技術 ~ 複数DB編 ~Mobage を支える Ruby の技術 ~ 複数DB編 ~
Mobage を支える Ruby の技術 ~ 複数DB編 ~
 
InfluxDB の概要 - sonots #tokyoinfluxdb
InfluxDB の概要 - sonots #tokyoinfluxdbInfluxDB の概要 - sonots #tokyoinfluxdb
InfluxDB の概要 - sonots #tokyoinfluxdb
 
Sinatra Pattern 20130415
Sinatra Pattern 20130415Sinatra Pattern 20130415
Sinatra Pattern 20130415
 
Serf という Orchestration ツール #immutableinfra
Serf という Orchestration ツール #immutableinfraSerf という Orchestration ツール #immutableinfra
Serf という Orchestration ツール #immutableinfra
 
Shadow Server on Fluentd at Fluentd Casual Talks #3
Shadow Server on Fluentd at Fluentd Casual Talks #3Shadow Server on Fluentd at Fluentd Casual Talks #3
Shadow Server on Fluentd at Fluentd Casual Talks #3
 
Yohoushi
YohoushiYohoushi
Yohoushi
 
Haikanko rubykaigi 20130531
Haikanko rubykaigi 20130531Haikanko rubykaigi 20130531
Haikanko rubykaigi 20130531
 
Mina 20130417
Mina 20130417Mina 20130417
Mina 20130417
 
Fluentdcasual 02-haikanko
Fluentdcasual 02-haikankoFluentdcasual 02-haikanko
Fluentdcasual 02-haikanko
 
capistrano-colorized-stream
capistrano-colorized-streamcapistrano-colorized-stream
capistrano-colorized-stream
 
Ruby test double
Ruby test doubleRuby test double
Ruby test double
 

Kürzlich hochgeladen

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 

Kürzlich hochgeladen (20)

Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 

Is ruby logger thread(process)-safe? at RubyConf 2013

  • 1. Is Ruby Logger Thread(Process)safe? RubyConf 2013 @Miami SEO Naotoshi (@sonots) 2013/11/09
  • 2. Naotoshi SEO like Now-toshi Say-oh @sonots (twitter, github) Dev Engineer for Operations Became as a commiter of Fluentd
  • 3. •realtime pipelining of log streams •plugins written in ruby
  • 4. Log Management Tool •syslog-ng / rsyslog Only collection Not Free •splunk No Plugin •scribed Java Plugin •flumeNG Ruby Plugin (Not Gem) •logstash •fluentd Ruby Plugin (Gem Support)
  • 5. Try the fluentd! It s very nice tool, check it out! http://fluentd.org/
  • 8. We needed a Logger which works safely in multiprocess for Fluentd
  • 9. Examined Ruby Logger • 1) Will logs not be mixtured in multi-threads? • 2) Will logs not be mixtured in multi-processes? • 3) Does log rotation work safely in multi-threads? • 4) Does log rotation work safely in multi-processes?
  • 10. 1) Will logs not be mixtured in multi-threads? Code require 'logger' require 'parallel' logger = Logger.new("/tmp/test.log") Parallel.map(['a', 'b'], :in_threads => 2) do |letter| 10000.times do logger.info letter * 5000 end end Result $ egrep -e 'ab' -e 'ba' /tmp/test.log [empty] OK
  • 12. 2) Will logs not be mixtured in multi-processes? Code require 'logger' require 'parallel' logger = Logger.new("/tmp/test.log") Parallel.map(['a', 'b'], :in_processes => 2) do |letter| 10000.times do logger.info letter * 5000 end end Result $ egrep -e 'ab' -e 'ba' /tmp/test.log [empty] OK
  • 13. Really? Why? Mutex does not work to exclusively lock in multiprocesses environment. Atomic/non-atomic: A write is atomic if the whole amount written in one operation is not interleaved with data from any other process. This is useful when there are multiple writers sending data to a single reader. Applications need to know how large a write request can be expected to be performed atomically. This maximum is called {PIPE_BUF}. This volume of IEEE Std 1003.1-2001 does not say whether write requests for more than {PIPE_BUF} bytes are atomic, but requires that writes of {PIPE_BUF} or fewer bytes shall be atomic. But, Linux system call write(2) itself is atomic as long as writing to a local file. OK
  • 14. 3) Does log rotation work safely multi-threads? Code require 'logger' require 'parallel' logger = Logger.new("/tmp/test.log", 3, 1024 * 10) Parallel.map(['a', 'b'], :in_threads => 2) do |letter| 10000.times do logger.info letter * 5000 end end Result $ ls -l /tmp/test* -rw-r--r-- 1 sonots sonots 10806 9月 29 23:03 /tmp/test.log -rw-r--r-- 1 sonots sonots 10806 9月 29 23:03 /tmp/test.log.0 -rw-r--r-- 1 sonots sonots 10806 9月 29 23:03 /tmp/test.log.1 No Error, OK
  • 15. 4) Does log rotation work safely in multi-processes? Code require 'logger' require 'parallel' logger = Logger.new("/tmp/test.log", 3, 1024 * 10) Parallel.map(['a', 'b'], :in_processes => 2) do |letter| 10000.times do logger.info letter * 5000 end end Result log log log log ... writing failed. closed stream shifting failed. closed stream writing failed. closed stream shifting failed. closed stream Oops, Bad!
  • 16. WHY? logger.rb#L559 def write(message) begin @mutex.synchronize do ... check_shift_log ... end Ruby’s Logger is taking mutex to be thread-safe, but was not treating anything for multi-processes.
  • 17. We(@frsyuki and me) Fixed • The approach is to use flock(2) without creating another *.lock file
  • 18. Pull requested to ruby My first contribution to ruby
  • 19. Merged!! Thanks to @naruse for his review and advice!!
  • 20. Will be released with 2.1.0!! on 12/25
  • 21. CONCLUSION • Ruby Logger was not safe in multiprocesses, but it is safe now!!! • Try ruby-trunk or https://github.com/sonots/ process_safe_logger
  • 22. Try the fluentd! It s very nice tool, check it out! http://fluentd.org/