Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize garbage collecting #391

Merged
merged 9 commits into from
Jan 18, 2024
Merged

Optimize garbage collecting #391

merged 9 commits into from
Jan 18, 2024

Conversation

roxblnfk
Copy link
Collaborator

@roxblnfk roxblnfk commented Jan 17, 2024

What was changed

  • Added destroy() methods to Workflow-related classes
  • Added a GarbageCollector which is responsible for the frequency of calling the finding circular references function (garbage collecting).
  • phpunit has been updated to version 10. This is a necessary measure because changes in the PR broke some tests that couldn't be handled using phpunit 9.

Why?

A few months ago, when we were troubleshooting a memory leak issue, I added a call to the gc_collect_cycles() method after each DestroyWorkflow command execution.

Garbage collection is quite a costly operation, and Workflows in Temporal are created and destroyed very frequently. We can verify this by looking at the metrics provided by the user Pyjac

image

We recently returned to the issue of potential memory leaks. In the process, some important optimizations were implemented:

  1. Added methods for manual destruction of Workflow-related objects, which free resources and break circular references between objects. Thus, all SDK code works without leaks, using PHP's self-cleaning mechanisms.
  2. We left the call to gc_collect_cycles(), but introduced control over the frequency of these calls. The collecting of circular references is now called by a 30-second timeout or when 1000 Workflows are destroyed. This way we are combating memory leaks in user code.

After this update, we are seeing a significant increase in the throughput of the Workflow worker and almost no TaskTimeout errors under highload.

Before

Screenshot 2024-01-16 153241

After
Screenshot 2024-01-16 161004

@roxblnfk roxblnfk added enhancement Tests Update tests or testing tools labels Jan 17, 2024
@roxblnfk roxblnfk added this to the 2.7.4 milestone Jan 17, 2024
src/Internal/Transport/Router/DestroyWorkflow.php Outdated Show resolved Hide resolved
src/Internal/Workflow/Process/Scope.php Show resolved Hide resolved
@roxblnfk roxblnfk merged commit ba2d5c3 into master Jan 18, 2024
91 checks passed
@roxblnfk roxblnfk deleted the optimize-gc branch January 18, 2024 13:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Tests Update tests or testing tools
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants