0
# Runtime Management
1
2
Core runtime lifecycle management including initialization, shutdown, and access to runtime context information.
3
4
## Capabilities
5
6
### Runtime Initialization
7
8
Initialize the Ray runtime with default or custom configuration.
9
10
```java { .api }
11
/**
12
* Initialize Ray runtime with the default runtime implementation.
13
* Must be called before any other Ray operations.
14
*/
15
public static void init();
16
17
/**
18
* Check if Ray has been initialized.
19
* @return True if init() has been called, false otherwise
20
*/
21
public static boolean isInitialized();
22
```
23
24
**Usage Example:**
25
26
```java
27
import io.ray.api.Ray;
28
29
// Initialize Ray runtime
30
Ray.init();
31
32
// Check initialization status
33
if (Ray.isInitialized()) {
34
// Perform Ray operations
35
ObjectRef<String> ref = Ray.put("Hello Ray!");
36
}
37
```
38
39
### Runtime Shutdown
40
41
Cleanly shutdown the Ray runtime and release resources.
42
43
```java { .api }
44
/**
45
* Shutdown Ray runtime and release all resources.
46
* Should be called when Ray operations are complete.
47
*/
48
public static void shutdown();
49
```
50
51
**Usage Example:**
52
53
```java
54
try {
55
Ray.init();
56
// Perform Ray operations...
57
} finally {
58
// Always shutdown Ray when done
59
Ray.shutdown();
60
}
61
```
62
63
### Runtime Context Access
64
65
Access runtime information including job IDs, node information, and actor context.
66
67
```java { .api }
68
/**
69
* Get the runtime context for accessing runtime information.
70
* @return RuntimeContext instance
71
*/
72
public static RuntimeContext getRuntimeContext();
73
74
public interface RuntimeContext {
75
/** Get current job ID */
76
JobId getCurrentJobId();
77
78
/** Get current task ID */
79
TaskId getCurrentTaskId();
80
81
/** Get current actor ID (null if not in actor) */
82
ActorId getCurrentActorId();
83
84
/** Check if current actor was restarted */
85
boolean wasCurrentActorRestarted();
86
87
/** Check if running in local mode */
88
boolean isLocalMode();
89
90
/** Get information about all cluster nodes */
91
List<NodeInfo> getAllNodeInfo();
92
93
/** Get information about all actors */
94
List<ActorInfo> getAllActorInfo();
95
96
/** Get current actor handle (null if not in actor) */
97
BaseActorHandle getCurrentActorHandle();
98
99
/** Get GPU IDs available to current process */
100
List<Integer> getGpuIds();
101
102
/** Get current namespace */
103
String getNamespace();
104
105
/** Get current node ID */
106
String getCurrentNodeId();
107
108
/** Get current runtime environment */
109
RuntimeEnv getCurrentRuntimeEnv();
110
}
111
```
112
113
**Usage Example:**
114
115
```java
116
RuntimeContext context = Ray.getRuntimeContext();
117
118
// Get current job information
119
JobId jobId = context.getCurrentJobId();
120
System.out.println("Current job: " + jobId);
121
122
// Check if running in actor
123
ActorId actorId = context.getCurrentActorId();
124
if (actorId != null) {
125
System.out.println("Running in actor: " + actorId);
126
127
// Check if actor was restarted
128
if (context.wasCurrentActorRestarted()) {
129
System.out.println("Actor was restarted - handle recovery logic");
130
}
131
}
132
133
// Get cluster information
134
List<NodeInfo> nodes = context.getAllNodeInfo();
135
System.out.println("Cluster has " + nodes.size() + " nodes");
136
137
// Get GPU information
138
List<Integer> gpus = context.getGpuIds();
139
if (!gpus.isEmpty()) {
140
System.out.println("Available GPUs: " + gpus);
141
}
142
```
143
144
### Actor Exit
145
146
Intentionally exit the current actor process.
147
148
```java { .api }
149
/**
150
* Intentionally exit the current actor.
151
* Can only be called from within an actor.
152
* @throws RuntimeException if not called from within an actor
153
*/
154
public static void exitActor();
155
```
156
157
**Usage Example:**
158
159
```java
160
public class MyActor {
161
private boolean shouldExit = false;
162
163
public void processData(String data) {
164
// Process data...
165
166
if (shouldExit) {
167
// Clean shutdown of actor
168
Ray.exitActor();
169
}
170
}
171
172
public void shutdown() {
173
shouldExit = true;
174
}
175
}
176
```
177
178
## Supporting Types
179
180
### Node Information
181
182
```java { .api }
183
public class NodeInfo {
184
/** Get node ID */
185
public String getNodeId();
186
187
/** Check if node is alive */
188
public boolean isAlive();
189
190
/** Get node resources */
191
public Map<String, Double> getResources();
192
}
193
```
194
195
### Actor Information
196
197
```java { .api }
198
public class ActorInfo {
199
/** Get actor ID */
200
public ActorId getActorId();
201
202
/** Get actor class name */
203
public String getClassName();
204
205
/** Get actor state */
206
public ActorState getState();
207
208
/** Get node ID where actor is running */
209
public String getNodeId();
210
}
211
212
public enum ActorState {
213
DEPENDENCIES_UNREADY,
214
PENDING_CREATION,
215
ALIVE,
216
RESTARTING,
217
DEAD
218
}
219
```
220
221
### Runtime Environment
222
223
```java { .api }
224
public interface RuntimeEnv {
225
/** Get runtime environment configuration */
226
RuntimeEnvConfig getConfig();
227
}
228
229
public class RuntimeEnvConfig {
230
/** Get working directory */
231
public String getWorkingDir();
232
233
/** Get environment variables */
234
public Map<String, String> getEnvVars();
235
236
/** Get Python dependencies */
237
public List<String> getPipPackages();
238
239
/** Get Conda dependencies */
240
public String getCondaEnv();
241
}
242
```