Running C++ process functions in a companion
The C++ companion SDK hosts process functions in a separate process managed by a Flow worker. It is useful when your user code needs a separate executable and address space. Build the companion separately from the worker and the runner; the worker sends batches to it through the companion protocol.
This reference assumes you already have a C++ process function and know how to configure and deploy a pipeline. The maintained resource companion executable, its build file and pipeline spec show the registration and wiring together.
Register hosted functions and resources
Use NYT::NFlow::NCompanionServer::TPipeline from yt/yt/flow/library/cpp/companion/server/pipeline.h. Populate it before starting the server:
AddTransform<TFunction, TStaticParameters, TDynamicParameters>(computationId)declares a hosted transform computation and registers its process function and parameter schemas.AddSource<TFunction, TStaticParameters, TDynamicParameters>(computationId)declares a hosted source computation. Both parameter types default toTEmptyProcessFunctionParameters.AddResource<TResource>()registers a companion-hosted resource class. Resource ids are declared in the spec, not in this call.
The computation id must match the spec. The spec's processing_function still selects the registered function by its C++ type name. Typed function registration replaces YT_FLOW_DEFINE_PROCESS_FUNCTION for that declaration; AddResource replaces YT_FLOW_DEFINE_RESOURCE. Avoid duplicate registration through both mechanisms. Duplicate computation ids are rejected.
Pass the populated pipeline to NYT::NFlow::NCompanionServer::RunCompanionMain(argc, argv, std::move(pipeline)), declared in companion_main.h. The executable's ya.make must depend on yt/yt/flow/library/cpp/companion/server; add the dependencies required by your functions and resources. The maintained build file above contains the complete dependency list for its example.
Connect the executable to the worker
- Make the companion executable and its runtime dependencies available in the worker job environment. Use the existing deployment instructions for the worker; a path in the spec does not upload the executable by itself.
- Declare a
NYT::NFlow::NCompanion::TCompanionManagerresource. In itsparameters.entrypoint, setexecutableto the executable's absolute path in that environment.argssupplies command-line arguments andenvsupplies additional environment variables. - For each hosted computation, select a companion computation class in
computation_class_name, setprocessing_function, and require the manager on the worker under aliasCompanionManager(worker = %true,controller = %false). Keep the computation id consistent withAddTransformorAddSource. - When using the standard
flow_serverrunner with function types registered only in the companion, set the runner configuration'sabort_on_specs_parseability_errorto%false. The runner cannot parse a function type absent from its own registry. The companion executable must still register the selected function and parameter schemas correctly.
The C++ companion test harness shows this separation: flow_server acts as the runner, while the manager entrypoint points to the C++ executable. Its integration test checks output that includes values loaded in the serving companion process.
Startup and execution boundaries
RunCompanionMain starts the companion server. It requires worker-supplied YT_FLOW_MODE=Worker and YT_FLOW_COMPANION_CONFIG containing a positive RPC port. Use the runner to launch the pipeline; the companion entry point does not implement the runner's pipeline-launch command.
The C++ companion uses one multithreaded process. Make shared objects safe for concurrent use.
The supported interfaces and their boundaries are described in C++ companion limitations. Select the hosting mode explicitly rather than assuming every native process-function facility is available out of process.
Companion-hosted resources and HTTP clients
Register hosted resource classes with AddResource<TResource>() and connect worker proxies using Companion resource wiring. In a process function's Init, IRuntimeInitContext::GetStaticResource(alias) returns the bound companion-side resource. The maintained mapper demonstrates this path. A resource that exists only in the worker is not transferred into the companion automatically.
IRuntimeInitContext::GetHttpClient() and GetHttpsClient() provide shared HTTP clients. Configure them through http_client_config, https_client_config and http_poller_threads in the worker's companion block; see TCompanionConfig.
Metrics
The worker collects the companion's metrics together with its own: the combined /solomon_proxy endpoint on the worker's monitoring port also reads the companion's /metrics. This requires an enabled worker metrics exporter and a companion monitoring port, companion.monitoring_port; a Vanilla launch fills it in automatically, see the port settings. Every companion series carries the flow_process=companion tag.
User code reports metrics under the same names as in the worker:
IRuntimeInitContext::GetProfiler()in a process function writesyt.flow.worker.computation.*with thecomputation_idtag.TResourceContext::Profilerof a companion-hosted resource writesyt.flow.worker.resource.*with theresourcetag. One resource instance serves all jobs of the companion process, so its metrics are not split by job.
The flow_process=companion tag separates these series from worker series with the same name; a sensor producer can't override it.
The companion also reports its own metrics, yt.flow.companion.*. Batch metrics are tagged with computation_id:
request.count,request.size,request.durationandrequest.cpu_time: received batches, their size, wall time and CPU time.request.message_count,request.timer_countandrequest.visit_count: inputs per batch.request.response.count: batch outcomes bystatus.job.recreation.count: batches that carry the job description and create the job in this process again, for example after a companion restart.state.sizeandstate.item_count: states transferred with batches, tagged withdirection,state_typeandstate_name.
job.count reports the number of jobs hosted by the process, and resource.execute.count counts resource commands by command and status. Metrics of the worker's connection to the companion are listed in Worker-side metrics.
Companion counters belong to the companion process. After a companion restart they start from zero, as worker counters do after a worker restart; the worker forwards companion series without adding them to its own. If the worker repeats a batch, for example after an RPC timeout, user code can process it again and count it twice.
Source and tests
- Companion server SDK.
- Registration tests and startup configuration tests.
- Resource integration test and shared lifecycle scenario, covering dependency values, dynamic updates and recovery after companion restart.