Refine Java worker code (#3735)

This commit is contained in:
Wang Qing
2019-01-12 22:45:33 +08:00
committed by Hao Chen
parent 8723d6b061
commit a0cf8ee5a8
10 changed files with 26 additions and 89 deletions
@@ -113,8 +113,7 @@ public abstract class AbstractRayRuntime implements RayRuntime {
int numObjectIds = objectIds.size();
// Do an initial fetch for remote objects.
List<List<UniqueId>> fetchBatches =
splitIntoBatches(objectIds, FETCH_BATCH_SIZE);
List<List<UniqueId>> fetchBatches = splitIntoBatches(objectIds);
for (List<UniqueId> batch : fetchBatches) {
rayletClient.fetchOrReconstruct(batch, true, workerContext.getCurrentTaskId());
}
@@ -139,8 +138,7 @@ public abstract class AbstractRayRuntime implements RayRuntime {
while (unreadys.size() > 0) {
retryCounter++;
List<UniqueId> unreadyList = new ArrayList<>(unreadys.keySet());
List<List<UniqueId>> reconstructBatches =
splitIntoBatches(unreadyList, FETCH_BATCH_SIZE);
List<List<UniqueId>> reconstructBatches = splitIntoBatches(unreadyList);
for (List<UniqueId> batch : reconstructBatches) {
rayletClient.fetchOrReconstruct(batch, false, workerContext.getCurrentTaskId());
@@ -198,12 +196,12 @@ public abstract class AbstractRayRuntime implements RayRuntime {
rayletClient.freePlasmaObjects(objectIds, localOnly);
}
private List<List<UniqueId>> splitIntoBatches(List<UniqueId> objectIds, int batchSize) {
private List<List<UniqueId>> splitIntoBatches(List<UniqueId> objectIds) {
List<List<UniqueId>> batches = new ArrayList<>();
int objectsSize = objectIds.size();
for (int i = 0; i < objectsSize; i += batchSize) {
int endIndex = i + batchSize;
for (int i = 0; i < objectsSize; i += FETCH_BATCH_SIZE) {
int endIndex = i + FETCH_BATCH_SIZE;
List<UniqueId> batchIds = (endIndex < objectsSize)
? objectIds.subList(i, endIndex)
: objectIds.subList(i, objectsSize);
@@ -256,17 +254,6 @@ public abstract class AbstractRayRuntime implements RayRuntime {
return (RayActor<T>) actor;
}
/**
* Generate the return ids of a task.
*/
private UniqueId[] genReturnIds(UniqueId taskId, int numReturns) {
UniqueId[] ret = new UniqueId[numReturns];
for (int i = 0; i < numReturns; i++) {
ret[i] = UniqueIdUtil.computeReturnId(taskId, i + 1);
}
return ret;
}
/**
* Create the task specification.
* @param func The target remote function.
@@ -280,7 +267,7 @@ public abstract class AbstractRayRuntime implements RayRuntime {
UniqueId taskId = rayletClient.generateTaskId(workerContext.getCurrentDriverId(),
workerContext.getCurrentTaskId(), workerContext.nextTaskIndex());
int numReturns = actor.getId().isNil() ? 1 : 2;
UniqueId[] returnIds = genReturnIds(taskId, numReturns);
UniqueId[] returnIds = UniqueIdUtil.genReturnIds(taskId, numReturns);
UniqueId actorCreationId = UniqueId.NIL;
if (isActorCreationTask) {
@@ -1,13 +1,9 @@
package org.ray.runtime;
import com.google.common.base.Strings;
import java.lang.reflect.Field;
import java.util.stream.Collectors;
import org.ray.api.runtime.RayRuntime;
import org.ray.api.runtime.RayRuntimeFactory;
import org.ray.runtime.config.RayConfig;
import org.ray.runtime.config.RunMode;
import org.ray.runtime.util.logger.RayLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -20,7 +16,6 @@ public class DefaultRayRuntimeFactory implements RayRuntimeFactory {
@Override
public RayRuntime createRayRuntime() {
RayLog.init();
RayConfig rayConfig = RayConfig.create();
try {
AbstractRayRuntime runtime;
@@ -31,7 +31,7 @@ public final class RayNativeRuntime extends AbstractRayRuntime {
super(rayConfig);
}
private void resetLibaryPath() {
private void resetLibraryPath() {
String path = System.getProperty("java.library.path");
if (Strings.isNullOrEmpty(path)) {
path = "";
@@ -60,7 +60,7 @@ public final class RayNativeRuntime extends AbstractRayRuntime {
public void start() throws Exception {
// Load native libraries.
try {
resetLibaryPath();
resetLibraryPath();
System.loadLibrary("raylet_library_java");
System.loadLibrary("plasma_java");
} catch (Exception e) {
@@ -37,7 +37,7 @@ public class RayletClientImpl implements RayletClient {
);
/**
* Point to c++'s local scheduler client.
* The pointer to c++'s local scheduler client.
*/
private long client = 0;
@@ -16,14 +16,10 @@ public class DefaultWorker {
try {
System.setProperty("ray.worker.mode", "WORKER");
Ray.init();
} catch (Exception e) {
LOGGER.error("Worker failed to start.", e);
}
LOGGER.info("Worker started.");
try {
LOGGER.info("Worker started.");
((AbstractRayRuntime)Ray.internal()).loop();
} catch (Exception e) {
LOGGER.error("Error occurred in worker.", e);
LOGGER.error("Failed to start worker.", e);
}
}
}
@@ -1,25 +0,0 @@
package org.ray.runtime.util;
import java.lang.reflect.InvocationTargetException;
public class ObjectUtil {
public static <T> T newObject(Class<T> cls) {
try {
return cls.getConstructor().newInstance();
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException
| InvocationTargetException e) {
e.printStackTrace();
return null;
}
}
public static boolean[] toBooleanArray(Object[] vs) {
boolean[] nvs = new boolean[vs.length];
for (int i = 0; i < vs.length; i++) {
nvs[i] = (boolean) vs[i];
}
return nvs;
}
}
@@ -71,6 +71,21 @@ public class UniqueIdUtil {
return new UniqueId(taskId);
}
/**
* Generate the return ids of a task.
*
* @param taskId The ID of the task that generates returnsIds.
* @param numReturns The number of returnIds.
* @return The Return Ids of this task.
*/
public static UniqueId[] genReturnIds(UniqueId taskId, int numReturns) {
UniqueId[] ret = new UniqueId[numReturns];
for (int i = 0; i < numReturns; i++) {
ret[i] = UniqueIdUtil.computeReturnId(taskId, i + 1);
}
return ret;
}
public static byte[][] getIdBytes(List<UniqueId> objectIds) {
int size = objectIds.size();
byte[][] ids = new byte[size][];
@@ -1,27 +0,0 @@
package org.ray.runtime.util.logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* loggers in Ray.
* 1. core logger is used for internal Ray status logging.
* 2. rapp for ray applications logging.
*/
public class RayLog {
/**
* for ray itself.
*/
public static Logger core;
/**
* for ray app.
*/
public static Logger rapp;
public static void init() {
core = LoggerFactory.getLogger("core");
rapp = core;
}
}