-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add job workers support to K2 runtime (#1097)
- Loading branch information
Showing
32 changed files
with
707 additions
and
145 deletions.
There are no files selected for viewing
This file contains 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 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,49 @@ | ||
<?php | ||
|
||
// === Job Worker ================================================================================= | ||
|
||
/** @kphp-immutable-class */ | ||
interface KphpJobWorkerSharedMemoryPiece {} | ||
|
||
interface KphpJobWorkerRequest {} | ||
interface KphpJobWorkerResponse {} | ||
|
||
class KphpJobWorkerResponseError implements KphpJobWorkerResponse { | ||
// Job script execution errors: | ||
const JOB_MEMORY_LIMIT_ERROR = -101; | ||
const JOB_TIMEOUT_ERROR = -102; | ||
const JOB_EXCEPTION_ERROR = -103; | ||
const JOB_STACK_OVERFLOW_ERROR = -104; | ||
const JOB_PHP_ASSERT_ERROR = -105; | ||
|
||
const JOB_CLIENT_MEMORY_LIMIT_ERROR = -1001; // client doesn't have enough memory to accept job response | ||
const JOB_NOTHING_REPLIED_ERROR = -2001; // kphp_job_worker_store_response() was not succeeded | ||
|
||
const JOB_STORE_RESPONSE_INCORRECT_CALL_ERROR = -3000; | ||
const JOB_STORE_RESPONSE_NOT_ENOUGH_SHARED_MESSAGES_ERROR = -3001; | ||
const JOB_STORE_RESPONSE_TOO_BIG_ERROR = -3002; | ||
const JOB_STORE_RESPONSE_CANT_SEND_ERROR = -3003; | ||
|
||
public function getError() ::: string; | ||
public function getErrorCode() ::: int; // returns one of listed above error codes | ||
} | ||
|
||
/** @kphp-extern-func-info interruptible */ | ||
function kphp_job_worker_start(string $request, float $timeout): future<string> | false; | ||
|
||
/** @kphp-extern-func-info interruptible */ | ||
function kphp_job_worker_start_no_reply(string $request, float $timeout): bool; | ||
|
||
/** @kphp-extern-func-info interruptible */ | ||
function kphp_job_worker_start_multi(string[] $request, float $timeout): (future<string> | false)[]; | ||
|
||
/** @kphp-extern-func-info interruptible */ | ||
function kphp_job_worker_fetch_request(): string; | ||
|
||
/** @kphp-extern-func-info interruptible */ | ||
function kphp_job_worker_store_response(string $response): int; | ||
|
||
function is_kphp_job_workers_enabled(): bool; | ||
|
||
function get_job_workers_number(): int; | ||
|
This file contains 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 was deleted.
Oops, something went wrong.
This file contains 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 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 |
---|---|---|
@@ -1 +1 @@ | ||
prepend(RUNTIME_COMPONENT_SRC component/ component.cpp) | ||
prepend(RUNTIME_COMPONENT_SRC component/ component.cpp init-functions.cpp) |
This file contains 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 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 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 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,69 @@ | ||
// Compiler for PHP (aka KPHP) | ||
// Copyright (c) 2024 LLC «V Kontakte» | ||
// Distributed under the GPL v3 License, see LICENSE.notice.txt | ||
|
||
#include "runtime-light/component/init-functions.h" | ||
|
||
#include <cstdint> | ||
|
||
#include "runtime-core/runtime-core.h" | ||
#include "runtime-core/utils/kphp-assert-core.h" | ||
#include "runtime-light/component/component.h" | ||
#include "runtime-light/coroutine/awaitable.h" | ||
#include "runtime-light/coroutine/task.h" | ||
#include "runtime-light/header.h" | ||
#include "runtime-light/stdlib/job-worker/job-worker-context.h" | ||
#include "runtime-light/streams/streams.h" | ||
#include "runtime-light/tl/tl-core.h" | ||
#include "runtime-light/tl/tl-functions.h" | ||
#include "runtime-light/utils/context.h" | ||
|
||
namespace { | ||
|
||
void process_k2_invoke_job_worker(tl::TLBuffer &tlb) noexcept { | ||
tl::K2InvokeJobWorker invoke_jw{}; | ||
if (!invoke_jw.fetch(tlb)) { | ||
php_error("erroneous job worker request"); | ||
} | ||
php_assert(invoke_jw.image_id == vk_k2_describe()->build_timestamp); // ensure that we got the request from ourselves | ||
|
||
auto &jw_server_ctx{JobWorkerServerComponentContext::get()}; | ||
jw_server_ctx.kind = invoke_jw.ignore_answer ? JobWorkerServerComponentContext::Kind::NoReply : JobWorkerServerComponentContext::Kind::Regular; | ||
jw_server_ctx.state = JobWorkerServerComponentContext::State::Working; | ||
jw_server_ctx.job_id = invoke_jw.job_id; | ||
jw_server_ctx.body = std::move(invoke_jw.body); | ||
get_component_context()->php_script_mutable_globals_singleton.get_superglobals().v$_SERVER.set_value(string{"JOB_ID"}, invoke_jw.job_id); | ||
} | ||
|
||
void process_k2_invoke_http([[maybe_unused]] tl::TLBuffer &tlb) noexcept {} | ||
|
||
} // namespace | ||
|
||
task_t<uint64_t> init_kphp_server_component() noexcept { | ||
auto stream_d{co_await wait_for_incoming_stream_t{}}; | ||
const auto [buffer, size]{co_await read_all_from_stream(stream_d)}; | ||
php_assert(size >= sizeof(uint32_t)); // check that we can fetch at least magic | ||
tl::TLBuffer tlb{}; | ||
tlb.store_bytes(buffer, static_cast<size_t>(size)); | ||
get_platform_context()->allocator.free(buffer); | ||
|
||
switch (const auto magic{*reinterpret_cast<const uint32_t *>(tlb.data())}) { // lookup magic | ||
case tl::K2_INVOKE_HTTP_MAGIC: { | ||
process_k2_invoke_http(tlb); | ||
break; | ||
} | ||
case tl::K2_INVOKE_JOB_WORKER_MAGIC: { | ||
process_k2_invoke_job_worker(tlb); | ||
// release standard stream in case of a no reply job worker since we don't need that stream anymore | ||
if (JobWorkerServerComponentContext::get().kind == JobWorkerServerComponentContext::Kind::NoReply) { | ||
get_component_context()->release_stream(stream_d); | ||
stream_d = INVALID_PLATFORM_DESCRIPTOR; | ||
} | ||
break; | ||
} | ||
default: { | ||
php_error("unexpected magic: 0x%x", magic); | ||
} | ||
} | ||
co_return stream_d; | ||
} |
This file contains 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,16 @@ | ||
// Compiler for PHP (aka KPHP) | ||
// Copyright (c) 2024 LLC «V Kontakte» | ||
// Distributed under the GPL v3 License, see LICENSE.notice.txt | ||
|
||
#pragma once | ||
|
||
#include "runtime-light/coroutine/awaitable.h" | ||
#include "runtime-light/coroutine/task.h" | ||
|
||
// Returns a stream descriptor that is supposed to be a stream to stdout | ||
inline task_t<uint64_t> init_kphp_cli_component() noexcept { | ||
co_return co_await wait_for_incoming_stream_t{}; | ||
} | ||
|
||
// Performs some initialization and returns a stream descriptor we need to write server response into | ||
task_t<uint64_t> init_kphp_server_component() noexcept; |
Oops, something went wrong.