Skip to content

Use Ractor::Port to ensure continuous processing#205

Open
Tamada-Arino wants to merge 1 commit intopubannotation:masterfrom
Tamada-Arino:ractor-port-stability
Open

Use Ractor::Port to ensure continuous processing#205
Tamada-Arino wants to merge 1 commit intopubannotation:masterfrom
Tamada-Arino:ractor-port-stability

Conversation

@Tamada-Arino
Copy link
Copy Markdown
Contributor

@Tamada-Arino Tamada-Arino commented Jun 16, 2025

概要

  • 重いファイルを複数同時に実行しようとすると、問題の修正を行いました
    • 現状の3.5-devの仕様に合わせています
    • https://bugs.ruby-lang.org/issues/21398
    • ここでいただいたコメントを参考に、Ractor::Portを使用した記述に変更しました。
    • スレッド数を制限することで処理が途中で止まらないようにしました

動作確認

  • Rubyのバージョンを3.5-devに切り替えました
  • 1.5MBほどのjsonlファイル2つを同時に処理しました
  • 300MBほどのtgzファイル2つを同時に処理しました
  • 止まらずに最後まで処理が完了することを確認しました
スクリーンショット 2025-06-20 13 52 40

@Tamada-Arino Tamada-Arino force-pushed the ractor-port-stability branch 2 times, most recently from 726d307 to d128279 Compare June 17, 2025 04:53
# - annotations are already normal, and
# - documents exist in the database
class StoreAnnotationsCollection
THREAD_POOL_SIZE = 4
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

このファイルの変更は必要でしょうか?
Ractor::Portをつかう変更だけでは不十分かどうか知りたいです。
そのため、StoreAnnotationsCollectionに変更を入れないときに、問題が解決しているか知りたいです。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

300MBのtgzファイルを2つ同時に処理した時、途中で止まることがありました。
バグレポートでコメントいただいたコードも、スレッド数やジョブを増やすと途中で止まっていたので、極端に処理が多いと(環境も関係していると思いますが)止まってしまう可能性があると考えています。

スレッド数を指定しないときは12件まで膨らみ、そのまま処理が止まってしまったのですが、スレッド数を指定することで比較的安定して処理を終えられました。

大容量のファイルを複数処理するためには、この変更は必要かと考えています。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

途中で止まった例:

THREADS = 10
JOBS_PER_THREAD = 100
ARRAY_SIZE = 1_000_000

def ractor_job(job_count, array_size)
  port = Ractor::Port.new

  workers = (1..4).map do |i|
    Ractor.new(port) do |job_port|
      while job = Ractor.receive
        result = job.map { |x| x * 2 }.sum
        job_port.send result
      end
    end
  end

  jobs = Array.new(job_count) { Array.new(array_size) { rand(1000) } }
  jobs.each_with_index do |job, i|
    w_idx = i % 4
    workers[w_idx].send(job)
  end

  results = []
  jobs.size.times do
    puts "Waiting for results..."
    _ractor, result = Ractor.select(port)
    puts "Received result: #{result}"
    results << result
  end
  results
end

threads = []
THREADS.times do
  threads << Thread.new do
    ractor_job(JOBS_PER_THREAD, ARRAY_SIZE)
  end
end

threads.each(&:join)
puts "All threads finished."

Copy link
Copy Markdown
Collaborator

@ledsun ledsun Jun 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://bugs.ruby-lang.org/issues/21398 のコメントでもらったサンプルコードのパラメータを大きくするとRuby 3.5-dev でもRactorがhangするのでしょうが?

そうであればhangsする条件をbugs.ruby で報告すると良いと思います。
今のところメンテナの人達もRuby 3.5で直ると思っていそうです。
もしそうなら報告しないと直してもらえません。
バグに気づかれないままRuby 3.5がリリースされると私たちもメンテナも嬉しくないです。
早めに怪しい挙動を共有することでRubyの発展への貢献になります。

個人の環境に依存しているかもしれません。
bugs.rubyで報告するまえに、念のため他の誰かに確認してもらうのが良いと思います。

あとRactorの呼び出しは15行目のresult = aligner.callで終わっています。
これよりあとに作成しているスレッドはRactorに影響を与えていないはずです。
もし、この変更で動作に影響があるのであれば、Ractorとは別の問題も潜んでいそうです。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

本間さんに協力いただいたのですが、コメントのコードは(時間がかかりますが)最後まで実行できました。
また、該当のコミットを削除した状態のブランチと大きいファイルの処理について比べたところ、大きく変化はありませんでした。
該当のコミットは削除しました。

@Tamada-Arino Tamada-Arino force-pushed the ractor-port-stability branch 3 times, most recently from 7165f26 to decc584 Compare June 23, 2025 05:51
@Tamada-Arino Tamada-Arino force-pushed the ractor-port-stability branch from decc584 to f51a1ef Compare July 2, 2025 07:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants