Fluentd Hacking Guide at RubyKaigi 2014

20
Copyright (C) 2014 DeNA Co.,Ltd. All Rights Reserved. Fluentd Hacking Guide (Fluentd ソースコード完全解説) September 20th, 2014 Naotoshi Seo @sonots DeNA Co., Ltd.

Transcript of Fluentd Hacking Guide at RubyKaigi 2014

Page 1: Fluentd Hacking Guide at RubyKaigi 2014

Copyright (C) 2014 DeNA Co.,Ltd. All Rights Reserved.

Fluentd Hacking Guide (Fluentd ソースコード完全解説)

September 20th, 2014 !Naotoshi Seo @sonotsDeNA Co., Ltd.

Page 2: Fluentd Hacking Guide at RubyKaigi 2014

Copyright (C) 2014 DeNA Co.,Ltd. All Rights Reserved.

2

Who am I?・Naotoshi Seo @sonots

・DeNA Co, Ltd.

・Infrastructure Engineer

・Fluentd Commiter

Page 3: Fluentd Hacking Guide at RubyKaigi 2014

Copyright (C) 2014 DeNA Co.,Ltd. All Rights Reserved.

⁃ An application (or a framework) to process log streaming ⁃ Powerful plugin architecture (+250 plugins) ⁃ written in Ruby

Page 4: Fluentd Hacking Guide at RubyKaigi 2014

Copyright (C) 2014 DeNA Co.,Ltd. All Rights Reserved.

Disclaimer

4

1. I do not explain what is Fluentd, how to use Fluentd 2. I assume audiences have ever used Fluentd, have ever

created Fluentd plugins, have interests inside Fluentd !

3. Source Codes are from v0.10 branch as of September 2014

Page 5: Fluentd Hacking Guide at RubyKaigi 2014

Copyright (C) 2014 DeNA Co.,Ltd. All Rights Reserved.

Topics

5

1. The bootstrap sequence of Fluentd, and how Fluentd loads plugins

2. How an input plugin passes data to output plugins 3. How BufferedOutput plugin works 4. How Fluentd parses the config file 5. The event-driven programming using cool.io, and effects of

GVL

Page 6: Fluentd Hacking Guide at RubyKaigi 2014

Copyright (C) 2014 DeNA Co.,Ltd. All Rights Reserved.

6

Let's Dive into Source Codes

Page 7: Fluentd Hacking Guide at RubyKaigi 2014

Copyright (C) 2014 DeNA Co.,Ltd. All Rights Reserved.

7

vim

Page 8: Fluentd Hacking Guide at RubyKaigi 2014

Copyright (C) 2014 DeNA Co.,Ltd. All Rights Reserved.

Topics

8

1. The bootstrap sequence of Fluentd, and how Fluentd loads plugins

2. How an input plugin passes data to output plugins 3. How BufferedOutput plugin works

Page 9: Fluentd Hacking Guide at RubyKaigi 2014

Copyright (C) 2014 DeNA Co.,Ltd. All Rights Reserved.

The Bootstrap Sequence

9

Fluent::Supervisor#run_configure 1. require 2. new 3. configure(conf ) Fluent::Supervisor#run_engine 4. start 5. shutdown (if signal received)

Input plugin creates threads on #startOutput plugin does nothing on #start (typically)

Page 10: Fluentd Hacking Guide at RubyKaigi 2014

Copyright (C) 2014 DeNA Co.,Ltd. All Rights Reserved.

Topics

10

1. The bootstrap sequence of Fluentd, and how Fluentd loads plugins

2. How an input plugin passes data to output plugins 3. How BufferedOutput plugin works

Page 11: Fluentd Hacking Guide at RubyKaigi 2014

Copyright (C) 2014 DeNA Co.,Ltd. All Rights Reserved.

Dataflow from Input to Output

11

Input Engine Output

emit(tag, es)

emit(tag, es)

If an input thread receives data, call Engine.emit

Page 12: Fluentd Hacking Guide at RubyKaigi 2014

Copyright (C) 2014 DeNA Co.,Ltd. All Rights Reserved.

CAUTION: Output Plugin

12

Input Engine Output

emit(tag, es)

emit(tag, es)

BLOCK!!!

Can not receive new input during blocking

ex) HTTP POST

Page 13: Fluentd Hacking Guide at RubyKaigi 2014

Copyright (C) 2014 DeNA Co.,Ltd. All Rights Reserved.

13

Use BufferedOutput to avoid blocking

Page 14: Fluentd Hacking Guide at RubyKaigi 2014

Copyright (C) 2014 DeNA Co.,Ltd. All Rights Reserved.

Topics

14

1. The bootstrap sequence of Fluentd, and how Fluentd loads plugins

2. How an input plugin passes data to output plugins 3. How BufferedOutput plugin works

Page 15: Fluentd Hacking Guide at RubyKaigi 2014

Copyright (C) 2014 DeNA Co.,Ltd. All Rights Reserved.

BufferedOutput Plugin

15

Input Engine BufferedOutput BasicBuffer

emit(tag,es)emit(tag,es)

emit(tag,data)enqueueimmediately return!!

Can receive new input. No blocking.

Page 16: Fluentd Hacking Guide at RubyKaigi 2014

Copyright (C) 2014 DeNA Co.,Ltd. All Rights Reserved.

BufferedOutput Plugin

16

BufferedOutput BasicBuffer

try_flush(push)

OutputThread

pop

write(chunk)

do some EXPENSIVE things

Run heavy processing in OTHER threads

Page 17: Fluentd Hacking Guide at RubyKaigi 2014

Copyright (C) 2014 DeNA Co.,Ltd. All Rights Reserved.

CAUTION: BufferedOutput

17

Stuck if enqueued over its capability !

HOW TO IMPROVE 1. Increase num_threads 2. Enlarge buffer_chunk_limit 3. Set smaller queued_chunk_flush_interval, try_flush_interval

(Secret parameters)

Page 18: Fluentd Hacking Guide at RubyKaigi 2014

Copyright (C) 2014 DeNA Co.,Ltd. All Rights Reserved.

18

But,

Page 19: Fluentd Hacking Guide at RubyKaigi 2014

Copyright (C) 2014 DeNA Co.,Ltd. All Rights Reserved.

19

Improving actual processing throughputs

is most important

fluent-plugin-elasticsearch gets stuck? Then, tune Elasticsearch!

Page 20: Fluentd Hacking Guide at RubyKaigi 2014

Copyright (C) 2014 DeNA Co.,Ltd. All Rights Reserved.

Conclusion

20

1. Output plugin blocks 2. BufferedOutput does not block, but stuck if enqueued

over its capability • Tune with option parameters such as num_threads

3. In either case, improving actual processing capability itself is most important!!

4. I am happy if this talk helps your operation, your plugin development, and contributions