msgpack-ruby icon indicating copy to clipboard operation
msgpack-ruby copied to clipboard

Does msgpack_each support nested calls in design principle?

Open frankcrc opened this issue 2 years ago • 1 comments

Hi,

I'm using fluentd to collect logs. I saw nested calls in error handle method of fluent-plugin-elasticsearch, and I simplified it to the following code,

# sorry, these code cannot run directly, so I add additional description
# chunk is file, and I guessed that chunk.msgpack_each invoked the following function
#
# def each(unpacker: nil, &block)
#      open do |io|
#        (unpacker || Fluent::MessagePackFactory.msgpack_unpacker(io)).each(&block)
#      end
#      nil
#    end
#    alias :msgpack_each :each
count = 0
chunk.msgpack_each do |time, record|
  count += 1
  chunk.msgpack_each do |time, record|
  end
end

Let's assume chunk has 500 messages inside. If I comment out the nested call, count would be 500 finally. Otherwise, count would be less than 500.

So I want to ask if msgpack_each support nested calls in design principle.

Please help!


Source logical process in fluent-plugin-elasticsearch,

  1. When event comes, write method would be invoked, https://github.com/uken/fluent-plugin-elasticsearch/blob/728329b22143e921bb4f1f680daedda61412f403/lib/fluent/plugin/out_elasticsearch.rb#L825-L855
  2. write method is going to msgpack_each chunk, https://github.com/uken/fluent-plugin-elasticsearch/blob/728329b22143e921bb4f1f680daedda61412f403/lib/fluent/plugin/out_elasticsearch.rb#L841
  3. When condition met, send_bulk method would be invoked, https://github.com/uken/fluent-plugin-elasticsearch/blob/728329b22143e921bb4f1f680daedda61412f403/lib/fluent/plugin/out_elasticsearch.rb#L854-L862
  4. If send_bulk encounter error, handle_error method would be invoked, https://github.com/uken/fluent-plugin-elasticsearch/blob/728329b22143e921bb4f1f680daedda61412f403/lib/fluent/plugin/out_elasticsearch.rb#L1114-L1117
  5. handle_error would msgpack_each the same chunk again, https://github.com/uken/fluent-plugin-elasticsearch/blob/728329b22143e921bb4f1f680daedda61412f403/lib/fluent/plugin/elasticsearch_error_handler.rb#L38-L50

frankcrc avatar Apr 14 '22 02:04 frankcrc

This seems contrary to normal Ruby idioms to me. You can't do the same with Array#each for example.

chrisseaton avatar Jul 19 '22 19:07 chrisseaton

Open to discussion?

chrisseaton avatar Aug 22 '22 12:08 chrisseaton

This seems contrary to normal Ruby idioms to me. You can't do the same with Array#each for example.

Sorry for replying late. As you comment, maybe it should not code like that, and i think there is no need to further discuss.

And I had commit a PR to fix that problem, by avoiding nested callding. :)

frankcrc avatar Aug 22 '22 14:08 frankcrc