Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Cleaning up a huge ruby application

Shia
April 20, 2019

Cleaning up a huge ruby application

Shia

April 20, 2019
Tweet

More Decks by Shia

Other Decks in Technology

Transcript

  1. !3

  2. Index •about cookpad_all •define the code want to delete •what

    makes it difficult •what we try & solve !4
  3. cookpad_all •Make a bug to fix a bug •Gems are

    too old version which have no feature we want, and it's hard to update •A lot of monkey patch to Object, String, gems !9
  4. Project Odaiba •Clean up codes •Let it work on (docker)

    container •Extract system/services which mixed in it !11
  5. Project Odaiba •Clean up codes ‣ Today's topic •Let it

    work on (docker) container •Extract system/services which mixed in it !12
  6. the code we may delete •Known unnecessary code •Unknown unnecessary

    code ‣ Executed meaningless code ‣ Not executed code !14
  7. the code we may delete •Known unnecessary code •Unknown unnecessary

    code ‣ Executed meaningless code ‣ Not executed code !15
  8. the code we may delete •Known unnecessary code •Unknown unnecessary

    code ‣ Executed meaningless code ‣ Not executed code !16
  9. What makes it difficult? !23 def foo # ... end

    def foo_with_bar # ... end alias_method_chain :foo, :bar alias_method_chain :foo, :bar # => alias_method :foo_without_bar, :foo alias_method :foo, :foo_with_bar
  10. What can we do •Low priority ‣ Raise priority •Continuously

    growing •High cost(especially time) !29
  11. What can we do •Low priority ‣ Raise priority •Continuously

    growing ‣ Continuously delete code •High cost(especially time) !30
  12. What can we do •Low priority ‣ Raise priority •Continuously

    growing ‣ Continuously delete code •High cost(especially time) ‣ Auto detect potential unused code to reduce cost !31
  13. KitchenCleaner •Auto-detect potential unused code •Guess people related these code

    •Open issue or ping assignee •Give context as much as possible for decision making !34
  14. Target code •Controller which has no pv in a year

    •Batch class which have no execution history in 3 month •Unused (chanko) unit !38
  15. Target developer •List up authors from "git log" •Filter out

    who quited the job •Random assign from it !39
  16. !42 ko1: It seems to be possible to find out

    unused codes on production environment by iseq lazy loading introduced in Ruby 2.4 shia: Why not? Let's try! ko1: I wrote the patch on my way to work.
  17. InstructionSequence(iseq) !48 > puts RubyVM::InstructionSequence.compile_file('sample.rb').disasm == disasm: #<ISeq:<main>@sample.rb:1 (1,0)-(5,3)> (catch:

    FALSE) ... == disasm: #<ISeq:<class:Cat>@sample.rb:1 (1,0)-(5,3)> (catch: FALSE) ... == disasm: #<ISeq:[email protected]:2 (2,2)-(4,5)> (catch: FALSE) ...
  18. Logging code execution !50 # sample.rb class Cat # <-

    executed def sleep? # <- not executed true end def go_out # <- executed # ... end end Cat.new.go_out
  19. Logging code execution !51 // https://github.com/ruby/ruby/blob/v2_4_3/vm_core.h#L415-L424 static inline const rb_iseq_t

    * rb_iseq_check(const rb_iseq_t *iseq) { #if USE_LAZY_LOAD if (iseq->body == NULL) { rb_iseq_complete((rb_iseq_t *)iseq); } #endif return iseq; }
  20. Logging code execution !52 static inline const rb_iseq_t * rb_iseq_check(const

    rb_iseq_t *iseq) { @@ -419,6 +429,11 @@ rb_iseq_check(const rb_iseq_t *iseq) if (iseq->body == NULL) { rb_iseq_complete((rb_iseq_t *)iseq); } +#endif +#if USE_EXECUTED_CHECK + if ((iseq->flags & ISEQ_FL_EXECUTED) == 0) { + rb_iseq_executed_check_dump((rb_iseq_t *)iseq); + } #endif
  21. Performance •Impact to performance is almost zero. •Boot time might

    be slower. ‣ In our case, logging via File I/O gives 2~3% slower boot time. !53
  22. Transfer logs app fluentd Redshift •log to file when it

    triggered. •{ created_at:, filename:, lineno:, label: } !54
  23. Generate report •What we need ‣ Which iseq is used

    or not ‣ Base on latest source code !55
  24. Logging code execution !56 Iseq = Struct.new(:path, :lineno, :label) def

    traverse(results, iseq) results << Iseq.new(iseq.absolute_path.sub(PROJECT_BASE_PATH + '/', ''), iseq.first_lineno, iseq.label) iseq.each_child do |child| traverse(results, child) end results end iseq = RubyVM::InstructionSequence.compile_file(some_path)
  25. Logging code execution !57 { created_at: ..., filename: 'sample.rb', lineno:

    1, label: 'main' } { created_at: ..., filename: 'sample.rb', lineno: 1, label: '<class:Cat>' } { created_at: ..., filename: 'sample.rb', lineno: 2, label: 'sleep?' } { created_at: ..., filename: 'sample.rb', lineno: 1, label: 'main' } { created_at: ..., filename: 'sample.rb', lineno: 1, label: '<class:Cat>' } Executed iseqs Iseqs from latest code
  26. Judge which log is usable •How to judge which log

    is usable or not? ‣ Let log usable if previous log have same filename + lineno + label with latest source code. !63
  27. Judge which log is usable !64 # revision A #

    monthly_batch.rb # ... # recipe.rb class Recipe def author_name author.name end end # revision B # monthly_batch.rb # ... # recipe.rb class Recipe def author_name author.name end def author_id author.id end end
  28. Judge which log is usable !65 Logs - 4/19, recipe.rb,

    L1, “<class:Recipe>”, revision A - 4/19, recipe.rb, L2, “<class:Recipe>”, revision A - 4/19, monthly_batch.rb, L1, “<class:MonthlyBatch>”, revision A - 4/20, recipe.rb, L1, “<class:Recipe>”, revision B - 4/20, recipe.rb, L6, “<class:Recipe>”, revision B # revision B # monthly_batch.rb # ... # recipe.rb class Recipe def author_name author.name end def author_id author.id end end
  29. Judge which log is usable •In our case, only 65%

    of files updated in a year •Assume file which rarely updated might have unused codes then frequently updated one !66
  30. Bad point !69 # It has dead condition if some_method_returns_always_false

    not_called_in_if else always_called end # What we want always_called not_called_in_if
  31. Bad point !70 # It has dead condition if some_method_returns_always_false

    not_called_in_if else always_called end not_called_in_if # What we want always_called not_called_in_if
  32. !73 mame: I think coverage is enough for that use

    case. shia: I'm worry about degrade performance by coverage, because it tries to hook when every line execution. mame: How about remove hook after it triggered once?
  33. Oneshot coverage •It just work as same as normal coverage,

    but it only check line executed or not. ‣ It removes hook after triggered. !77
  34. Oneshot coverage !78 require "coverage" Coverage.start(oneshot_lines: true) load "test.rb" p

    Coverage.result # {"test.rb"=>{:oneshot_lines=>[2, 10, 3, 4, 11]}} # 1: # test.rb # 2: def foo(n) # 3: if n <= 10 # 4: p "n < 10" # 5: else # 6: p "n >= 10" # 7: end # 8: end # 9: # 10: foo(1) # 11: foo(2)
  35. Transfer logs app DynamoDB Redshift •Send log when: ‣ per

    1 hour ‣ after batch execution finished •{ filename:, md5_hash:, executed_lines: } !79 per 1 hour per 1 day
  36. Transfer logs app DynamoDB Redshift •Use dynamoDB: ‣ Sending logs

    per line is not practical approach in this case ‣ Send data to Redshift as daily snapshot •{ filename:, md5_hash:, executed_lines: } !80 per 1 hour per 1 day Purge per day
  37. How to merge logs !81 Case: need to merge -

    DynamoDB => "recipe.rb-somehash": [1,2,3,5,9] - Coverage.result => "recipe.rb-somehash": [1,2,3,6,7,8] - New data => "recipe.rb-somehash": [1,2,3,5,9,6,7,8] Case: Don't need to merge - DynamoDB => "recipe.rb-somehash": [1,2,3,5,9] - Coverage.result => "recipe.rb-somehash": [1,2,3]
  38. Transfer logs app DynamoDB Redshift •Use dynamoDB: ‣ Sending logs

    per line is not practical approach in this case ‣ Send data to Redshift as daily snapshot •{ filename:, md5_hash:, executed_lines: } !82 per 1 hour per 1 day Purge per day
  39. Generate report •What we need ‣ Which line is used

    or not ‣ Base on latest source code !83
  40. Judge which log is usable •In our case, only 65%

    of files updated in a year •Assume file which rarely updated might have unused codes then frequently updated one !86
  41. Good point •No need to build Ruby! •Per line logs

    give us much more information !88
  42. Bad point •Too many number of logs •Hard to find

    unused file (than IseqLogger) !89
  43. Bad point !90 1: class Recipe 2: def name 3:

    @name 4: end 5: 6: def author 7: @author 8: end 9:end
  44. Bad point !91 1: class Recipe 2: def name 3:

    @name -: end -: 6: def author 7: @author -: end -:end
  45. Bad point !92 # oneshot coverage 1: class Recipe 2:

    def name 3: @name -: end -: 6: def author 7: @author -: end -:end # IseqLogger class Recipe # <- Executed def name # <- Not executed @name end def author # <- Not executed @author end end
  46. Coverband •https://github.com/danmayer/coverband/ •We don't use this: ‣ oneshot coverage is

    not supported, yet ‣ verbose toolkit to us ‣ might loss unused information with not changed file !96
  47. !105 •Low priority ‣ Let code cleaning be a part

    of project. •Continuously growing ‣ Let developers remove their own unused codes per month •High cost(especially time) ‣ Auto detect potential unused code on production to reduce cost Summary
  48. It is necessary to work hard for cleaning codes !108

    It is necessary to work little more