forked from ray-project/ray
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Java] Add runtime context (ray-project#4194)
- Loading branch information
1 parent
c73d508
commit a116b7f
Showing
12 changed files
with
238 additions
and
2 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,46 @@ | ||
package org.ray.api; | ||
|
||
import org.ray.api.id.UniqueId; | ||
|
||
/** | ||
* A class used for getting information of Ray runtime. | ||
*/ | ||
public interface RuntimeContext { | ||
|
||
/** | ||
* Get the current Driver ID. | ||
* | ||
* If called in a driver, this returns the driver ID. If called in a worker, this returns the ID | ||
* of the associated driver. | ||
*/ | ||
UniqueId getCurrentDriverId(); | ||
|
||
/** | ||
* Get the current actor ID. | ||
* | ||
* Note, this can only be called in actors. | ||
*/ | ||
UniqueId getCurrentActorId(); | ||
|
||
/** | ||
* Returns true if the current actor was reconstructed, false if it's created for the first time. | ||
* | ||
* Note, this method should only be called from an actor creation task. | ||
*/ | ||
boolean wasCurrentActorReconstructed(); | ||
|
||
/** | ||
* Get the raylet socket name. | ||
*/ | ||
String getRayletSocketName(); | ||
|
||
/** | ||
* Get the object store socket name. | ||
*/ | ||
String getObjectStoreSocketName(); | ||
|
||
/** | ||
* Return true if Ray is running in single-process mode, false if Ray is running in cluster mode. | ||
*/ | ||
boolean isSingleProcess(); | ||
} |
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
56 changes: 56 additions & 0 deletions
56
java/runtime/src/main/java/org/ray/runtime/RuntimeContextImpl.java
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,56 @@ | ||
package org.ray.runtime; | ||
|
||
import com.google.common.base.Preconditions; | ||
import org.ray.api.RuntimeContext; | ||
import org.ray.api.id.UniqueId; | ||
import org.ray.runtime.config.RunMode; | ||
import org.ray.runtime.config.WorkerMode; | ||
import org.ray.runtime.task.TaskSpec; | ||
|
||
public class RuntimeContextImpl implements RuntimeContext { | ||
|
||
private AbstractRayRuntime runtime; | ||
|
||
public RuntimeContextImpl(AbstractRayRuntime runtime) { | ||
this.runtime = runtime; | ||
} | ||
|
||
@Override | ||
public UniqueId getCurrentDriverId() { | ||
return runtime.getWorkerContext().getCurrentDriverId(); | ||
} | ||
|
||
@Override | ||
public UniqueId getCurrentActorId() { | ||
Preconditions.checkState(runtime.rayConfig.workerMode == WorkerMode.WORKER); | ||
return runtime.getWorker().getCurrentActorId(); | ||
} | ||
|
||
@Override | ||
public boolean wasCurrentActorReconstructed() { | ||
TaskSpec currentTask = runtime.getWorkerContext().getCurrentTask(); | ||
Preconditions.checkState(currentTask != null && currentTask.isActorCreationTask(), | ||
"This method can only be called from an actor creation task."); | ||
if (isSingleProcess()) { | ||
return false; | ||
} | ||
|
||
return ((RayNativeRuntime) runtime).actorExistsInGcs(getCurrentActorId()); | ||
} | ||
|
||
@Override | ||
public String getRayletSocketName() { | ||
return runtime.getRayConfig().rayletSocketName; | ||
} | ||
|
||
@Override | ||
public String getObjectStoreSocketName() { | ||
return runtime.getRayConfig().objectStoreSocketName; | ||
} | ||
|
||
@Override | ||
public boolean isSingleProcess() { | ||
return RunMode.SINGLE_PROCESS == runtime.getRayConfig().runMode; | ||
} | ||
|
||
} |
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
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
52 changes: 52 additions & 0 deletions
52
java/test/src/main/java/org/ray/api/test/RuntimeContextTest.java
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,52 @@ | ||
package org.ray.api.test; | ||
|
||
import org.ray.api.Ray; | ||
import org.ray.api.RayActor; | ||
import org.ray.api.annotation.RayRemote; | ||
import org.ray.api.id.UniqueId; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
public class RuntimeContextTest extends BaseTest { | ||
|
||
private static UniqueId DRIVER_ID = | ||
UniqueId.fromHexString("0011223344556677889900112233445566778899"); | ||
private static String RAYLET_SOCKET_NAME = "/tmp/ray/test/raylet_socket"; | ||
private static String OBJECT_STORE_SOCKET_NAME = "/tmp/ray/test/object_store_socket"; | ||
|
||
@Override | ||
public void beforeInitRay() { | ||
System.setProperty("ray.driver.id", DRIVER_ID.toString()); | ||
System.setProperty("ray.raylet.socket-name", RAYLET_SOCKET_NAME); | ||
System.setProperty("ray.object-store.socket-name", OBJECT_STORE_SOCKET_NAME); | ||
} | ||
|
||
@Test | ||
public void testRuntimeContextInDriver() { | ||
Assert.assertEquals(DRIVER_ID, Ray.getRuntimeContext().getCurrentDriverId()); | ||
Assert.assertEquals(RAYLET_SOCKET_NAME, Ray.getRuntimeContext().getRayletSocketName()); | ||
Assert.assertEquals(OBJECT_STORE_SOCKET_NAME, | ||
Ray.getRuntimeContext().getObjectStoreSocketName()); | ||
} | ||
|
||
@RayRemote | ||
public static class RuntimeContextTester { | ||
|
||
public String testRuntimeContext(UniqueId actorId) { | ||
Assert.assertEquals(DRIVER_ID, Ray.getRuntimeContext().getCurrentDriverId()); | ||
Assert.assertEquals(actorId, Ray.getRuntimeContext().getCurrentActorId()); | ||
Assert.assertEquals(RAYLET_SOCKET_NAME, Ray.getRuntimeContext().getRayletSocketName()); | ||
Assert.assertEquals(OBJECT_STORE_SOCKET_NAME, | ||
Ray.getRuntimeContext().getObjectStoreSocketName()); | ||
return "ok"; | ||
} | ||
} | ||
|
||
@Test | ||
public void testRuntimeContextInActor() { | ||
RayActor<RuntimeContextTester> actor = Ray.createActor(RuntimeContextTester::new); | ||
Assert.assertEquals("ok", | ||
Ray.call(RuntimeContextTester::testRuntimeContext, actor, actor.getId()).get()); | ||
} | ||
|
||
} |