mirror of
https://github.com/wassname/ray.git
synced 2026-06-28 02:01:24 +08:00
Global state accessor jni (#8637)
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
package io.ray.runtime.gcs;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* `GlobalStateAccessor` is used for accessing information from GCS.
|
||||
*
|
||||
**/
|
||||
public class GlobalStateAccessor {
|
||||
// NOTE(lingxuan.zlx): this is a singleton, it can not be changed during a Ray session.
|
||||
// Native pointer to the C++ GcsStateAccessor.
|
||||
private Long globalStateAccessorNativePointer = 0L;
|
||||
private static GlobalStateAccessor globalStateAccessor;
|
||||
|
||||
public static synchronized GlobalStateAccessor getInstance(String redisAddress,
|
||||
String redisPassword) {
|
||||
if (null == globalStateAccessor) {
|
||||
globalStateAccessor = new GlobalStateAccessor(redisAddress, redisPassword);
|
||||
}
|
||||
return globalStateAccessor;
|
||||
}
|
||||
|
||||
public static synchronized void destroyInstance() {
|
||||
if (null != globalStateAccessor) {
|
||||
globalStateAccessor.destroyGlobalStateAccessor();
|
||||
}
|
||||
}
|
||||
|
||||
private GlobalStateAccessor(String redisAddress, String redisPassword) {
|
||||
globalStateAccessorNativePointer =
|
||||
nativeCreateGlobalStateAccessor(redisAddress, redisPassword);
|
||||
Preconditions.checkState(globalStateAccessorNativePointer != 0,
|
||||
"Global state accessor native pointer must not be 0.");
|
||||
connect();
|
||||
}
|
||||
|
||||
private boolean connect() {
|
||||
return this.nativeConnect(globalStateAccessorNativePointer);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return A list of job info with JobInfo protobuf schema.
|
||||
*/
|
||||
public List<byte[]> getAllJobInfo() {
|
||||
// Fetch a job list with protobuf bytes format from GCS.
|
||||
synchronized (GlobalStateAccessor.class) {
|
||||
Preconditions.checkState(globalStateAccessorNativePointer != 0);
|
||||
return this.nativeGetAllJobInfo(globalStateAccessorNativePointer);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return A list of node info with GcsNodeInfo protobuf schema.
|
||||
*/
|
||||
public List<byte[]> getAllNodeInfo() {
|
||||
// Fetch a node list with protobuf bytes format from GCS.
|
||||
synchronized (GlobalStateAccessor.class) {
|
||||
Preconditions.checkState(globalStateAccessorNativePointer != 0);
|
||||
return this.nativeGetAllNodeInfo(globalStateAccessorNativePointer);
|
||||
}
|
||||
}
|
||||
|
||||
private void destroyGlobalStateAccessor() {
|
||||
synchronized (GlobalStateAccessor.class) {
|
||||
if (0 == globalStateAccessorNativePointer) {
|
||||
return;
|
||||
}
|
||||
this.nativeDestroyGlobalStateAccessor(globalStateAccessorNativePointer);
|
||||
globalStateAccessorNativePointer = 0L;
|
||||
}
|
||||
}
|
||||
|
||||
private native long nativeCreateGlobalStateAccessor(String redisAddress, String redisPassword);
|
||||
|
||||
private native void nativeDestroyGlobalStateAccessor(long nativePtr);
|
||||
|
||||
private native boolean nativeConnect(long nativePtr);
|
||||
|
||||
private native void nativeDisconnect(long nativePtr);
|
||||
|
||||
private native List<byte[]> nativeGetAllJobInfo(long nativePtr);
|
||||
|
||||
private native List<byte[]> nativeGetAllNodeInfo(long nativePtr);
|
||||
}
|
||||
Reference in New Issue
Block a user