Implementation:Ray project Ray RuntimeContext
| Knowledge Sources | |
|---|---|
| Domains | Distributed_Computing, Java_Runtime |
| Last Updated | 2026-02-13 16:00 GMT |
Overview
Public API interface through which Java Ray workers and drivers introspect the state of the running Ray runtime.
Description
RuntimeContext is a Java interface with thirteen methods covering identity queries, state checks, cluster introspection, handle retrieval, resource discovery, and environment access. It provides methods for retrieving identifiers (getCurrentJobId, getCurrentTaskId, getCurrentActorId, getCurrentNodeId, getNamespace), checking state (wasCurrentActorRestarted, isLocalMode), discovering cluster topology (getAllNodeInfo, getAllActorInfo), obtaining actor handles (getCurrentActorHandle), finding GPU resources (getGpuIds), and accessing the runtime environment (getCurrentRuntimeEnv).
Usage
Use RuntimeContext via Ray.getRuntimeContext() to obtain execution context information from within Ray tasks and actors. It is the single entry point Java applications use for building fault-tolerant actors, resource-aware task placement, and dynamic cluster-aware logic. Concrete implementations are provided by the Ray runtime.
Code Reference
Source Location
- Repository: Ray
- File:
java/api/src/main/java/io/ray/api/runtimecontext/RuntimeContext.java
Signature
public interface RuntimeContext {
JobId getCurrentJobId();
TaskId getCurrentTaskId();
ActorId getCurrentActorId();
boolean wasCurrentActorRestarted();
boolean isLocalMode();
List<NodeInfo> getAllNodeInfo();
List<ActorInfo> getAllActorInfo();
List<ActorInfo> getAllActorInfo(JobId jobId, ActorState actorState);
<T extends BaseActorHandle> T getCurrentActorHandle();
List<Long> getGpuIds();
String getNamespace();
UniqueId getCurrentNodeId();
RuntimeEnv getCurrentRuntimeEnv();
}
Import
import io.ray.api.runtimecontext.RuntimeContext;
I/O Contract
Output (Method Return Values)
| Method | Return Type | Description |
|---|---|---|
getCurrentJobId() |
JobId |
The current Ray job identifier |
getCurrentTaskId() |
TaskId |
The currently executing task identifier |
getCurrentActorId() |
ActorId |
The current actor identifier (only valid inside actors) |
wasCurrentActorRestarted() |
boolean |
Whether the current actor was restarted from a failure |
isLocalMode() |
boolean |
Whether Ray is running in local mode |
getAllNodeInfo() |
List<NodeInfo> |
Information about all nodes in the cluster |
getAllActorInfo() |
List<ActorInfo> |
Information about all actors across all jobs |
getAllActorInfo(JobId, ActorState) |
List<ActorInfo> |
Filtered actor information by job ID and state |
getCurrentActorHandle() |
T extends BaseActorHandle |
Handle to the current actor (only valid inside actors) |
getGpuIds() |
List<Long> |
Available GPU device IDs for this worker |
getNamespace() |
String |
The namespace of the current job |
getCurrentNodeId() |
UniqueId |
The node ID where this worker is running |
getCurrentRuntimeEnv() |
RuntimeEnv |
The runtime environment of this worker or driver |
Usage Examples
// Access the runtime context
RuntimeContext ctx = Ray.getRuntimeContext();
// Query execution identity
JobId jobId = ctx.getCurrentJobId();
boolean localMode = ctx.isLocalMode();
String namespace = ctx.getNamespace();
// Inspect cluster topology
List<NodeInfo> nodes = ctx.getAllNodeInfo();
System.out.println("Cluster has " + nodes.size() + " nodes");
// Check available GPUs
List<Long> gpuIds = ctx.getGpuIds();
System.out.println("Available GPUs: " + gpuIds);
// Inside an actor: check if restarted
if (ctx.wasCurrentActorRestarted()) {
// Recover state from checkpoint
}