-
Notifications
You must be signed in to change notification settings - Fork 109
Improved prefork run-once mode #1532
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| // Compiler for PHP (aka KPHP) | ||
| // Copyright (c) 2026 LLC «V Kontakte» | ||
| // Distributed under the GPL v3 License, see LICENSE.notice.txt | ||
|
|
||
| #include "server/php-script-run-once-invoker.h" | ||
| #include "server/php-engine.h" | ||
|
|
||
| #include <algorithm> | ||
| #include <cerrno> | ||
| #include <cstring> | ||
| #include <unistd.h> | ||
|
|
||
| #include "common/crc32c.h" | ||
| #include "common/kprintf.h" | ||
| #include "common/pipe-utils.h" | ||
| #include "common/tl/constants/common.h" | ||
| #include "net/net-connections.h" | ||
| #include "net/net-socket.h" | ||
| #include "net/net-tcp-rpc-client.h" | ||
|
|
||
| // These are defined in php-engine.cpp | ||
| extern conn_type_t ct_php_rpc_client; | ||
| extern tcp_rpc_client_functions rpc_client_methods; | ||
|
|
||
| void PhpScriptRunOnceInvoker::init(int total_run_once_count) { | ||
| remaining_count_ = total_run_once_count; | ||
|
|
||
| int pipe_fd[2]; | ||
| if (pipe(pipe_fd) != 0) { | ||
| kprintf("Failed to create pipe for run_once mode: %s\n", strerror(errno)); | ||
| exit(1); | ||
| } | ||
|
|
||
| int read_fd = pipe_fd[0]; | ||
| write_fd_ = pipe_fd[1]; | ||
|
|
||
| bool ok = set_fd_nonblocking(write_fd_); | ||
| if (!ok) { | ||
| kprintf("Failed to set pipe non-blocking: %s\n", strerror(errno)); | ||
| exit(1); | ||
| } | ||
|
|
||
| rpc_client_methods.rpc_ready = nullptr; | ||
| auto* connection = epoll_insert_pipe(pipe_for_read, read_fd, &ct_php_rpc_client, &rpc_client_methods); | ||
| if (connection == nullptr) { | ||
| kprintf("Failed to insert pipe to epoll reactor\n"); | ||
| exit(1); | ||
| } | ||
| } | ||
|
|
||
| bool PhpScriptRunOnceInvoker::invoke_run_once(int runs_count) { | ||
PetrShumilov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (remaining_count_ <= 0 || write_fd_ == -1) { | ||
| return false; | ||
| } | ||
|
|
||
| int q[6]; | ||
| int qsize = 6 * sizeof(int); | ||
| q[2] = TL_RPC_INVOKE_REQ; | ||
|
|
||
| int batch_size = std::min(remaining_count_, runs_count); | ||
| for (int i = 0; i < batch_size; i++) { | ||
| prepare_rpc_query_raw(next_packet_id_, q, qsize, crc32c_partial); | ||
| ssize_t written = write(write_fd_, q, static_cast<size_t>(qsize)); | ||
| if (written != qsize) { | ||
| if (written == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) { | ||
| break; | ||
| } | ||
| // Other error or partial write - this shouldn't happen | ||
| kprintf("Failed to write to run_once pipe: %s\n", strerror(errno)); | ||
| exit(1); | ||
| } | ||
| --remaining_count_; | ||
| ++next_packet_id_; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| bool PhpScriptRunOnceInvoker::has_pending() const { | ||
| return remaining_count_ > 0; | ||
| } | ||
|
|
||
| bool PhpScriptRunOnceInvoker::enabled() const { | ||
| return remaining_count_ != -1; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // Compiler for PHP (aka KPHP) | ||
| // Copyright (c) 2026 LLC «V Kontakte» | ||
| // Distributed under the GPL v3 License, see LICENSE.notice.txt | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "common/mixin/not_copyable.h" | ||
| #include "common/smart_ptrs/singleton.h" | ||
|
|
||
| // Helper class to run PHP scripts N times via self-RPC requests through a pipe | ||
| // Avoids blocking on pipe write by writing messages in batches from within the event loop | ||
| class PhpScriptRunOnceInvoker : vk::not_copyable { | ||
| public: | ||
| static constexpr int DEFAULT_RUNS_BATCH_SIZE = 128; | ||
|
|
||
| // Initialize the pipe and register read end with epoll | ||
| void init(int total_run_once_count); | ||
|
|
||
| // Try to send a batch of run-once trigger messages to the pipe | ||
| // Returns true if there are more messages to send, false when done | ||
| bool invoke_run_once(int runs_count = DEFAULT_RUNS_BATCH_SIZE); | ||
|
|
||
| // Check if we still have messages pending | ||
| bool has_pending() const; | ||
|
|
||
| bool enabled() const; | ||
|
|
||
| private: | ||
| int remaining_count_{-1}; | ||
| int write_fd_{-1}; | ||
| int next_packet_id_{0}; | ||
|
|
||
| PhpScriptRunOnceInvoker() = default; | ||
|
|
||
| friend class vk::singleton<PhpScriptRunOnceInvoker>; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <?php | ||
|
|
||
| function suspend_($time) { | ||
| sched_yield_sleep($time); | ||
| return null; | ||
| } | ||
|
|
||
| function suspend($time) { | ||
| $f = fork(suspend_($time)); | ||
| wait($f); | ||
| } | ||
|
|
||
| echo "PID: " . posix_getpid() . " - before suspend\n"; | ||
| suspend(0.001); | ||
| echo "Done - after suspend\n"; |
76 changes: 76 additions & 0 deletions
76
tests/python/tests/run_once_prefork/test_run_once_prefork.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import pytest | ||
|
|
||
| from python.lib.testcase import WebServerAutoTestCase | ||
|
|
||
| @pytest.mark.k2_skip_suite | ||
| class TestRunOncePrefork(WebServerAutoTestCase): | ||
| @classmethod | ||
| def extra_class_setup(cls): | ||
| cls.web_server.update_options(options={"--http-port": None}) | ||
|
|
||
| def _start_run_once_server(self, runs_count, workers_num=1, auto_start=True): | ||
| """Create a KPHP server configured for run-once prefork mode""" | ||
| self.web_server.update_options(options={ | ||
| "--once={}".format(runs_count): True, | ||
| "--workers-num": workers_num, | ||
| }) | ||
| self.web_server.restart() | ||
|
|
||
| def test_run_once_single(self): | ||
| """Test basic run-once prefork mode: worker must execute script N times, then restart the process and repeat""" | ||
| runs_count = 1 | ||
| self._start_run_once_server(runs_count=runs_count, workers_num=1) | ||
| self.web_server.assert_stats( | ||
| { | ||
| "kphp_server.workers_general_requests_total_incoming_queries": self.cmpGe(2), | ||
| "kphp_server.server_workers_started": self.cmpGe(2) | ||
| }, | ||
| timeout=5 | ||
| ) | ||
| self.web_server.stop() | ||
|
|
||
| def test_run_once_with_multiple_workers(self): | ||
| """Test run-once with multiple workers""" | ||
| runs_count = 10 | ||
| workers_num = 4 | ||
| self._start_run_once_server(runs_count=runs_count, workers_num=workers_num) | ||
|
|
||
| self.web_server.assert_stats( | ||
| { | ||
| "kphp_server.workers_general_requests_total_incoming_queries": self.cmpGe(runs_count * workers_num), | ||
| "kphp_server.server_workers_started": self.cmpGe(workers_num) | ||
| }, | ||
| timeout=5 | ||
| ) | ||
|
|
||
| self.web_server.stop() | ||
|
|
||
| def test_run_once_large_batch(self): | ||
| """Test run-once with large number of runs (tests batching)""" | ||
| runs_count = 1000 | ||
| self._start_run_once_server(runs_count=runs_count, workers_num=1) | ||
|
|
||
| self.web_server.assert_stats( | ||
| { | ||
| "kphp_server.workers_general_requests_total_incoming_queries": self.cmpGe(runs_count), | ||
| "kphp_server.server_workers_started": self.cmpGe(1) | ||
| }, | ||
| timeout=5 | ||
| ) | ||
|
|
||
| self.web_server.stop() | ||
|
|
||
| def test_run_once_infinite(self): | ||
| """Test run-once with large number of runs (tests batching)""" | ||
| runs_count = 2**31 - 1 # max int32 | ||
| self._start_run_once_server(runs_count=runs_count, workers_num=1) | ||
|
|
||
| self.web_server.assert_stats( | ||
| { | ||
| "kphp_server.workers_general_requests_total_incoming_queries": self.cmpGe(200), | ||
| "kphp_server.server_workers_started": 1 | ||
| }, | ||
| timeout=5 | ||
| ) | ||
|
|
||
| self.web_server.stop() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.