mirror of
https://github.com/wassname/ray.git
synced 2026-07-13 01:17:37 +08:00
[Placement Group]Enhance create placement group java api (#11702)
* enhance create pg java api * add state for PlacementGroup * fix comment * move default pg * make default pg name private * add bundle size and bundle resource size check when placement group create
This commit is contained in:
@@ -51,6 +51,7 @@ public abstract class AbstractRayRuntime implements RayRuntimeInternal {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractRayRuntime.class);
|
||||
public static final String PYTHON_INIT_METHOD_NAME = "__init__";
|
||||
private static final String DEFAULT_PLACEMENT_GROUP_NAME = "unnamed_group";
|
||||
protected RayConfig rayConfig;
|
||||
protected TaskExecutor taskExecutor;
|
||||
protected FunctionManager functionManager;
|
||||
@@ -165,9 +166,22 @@ public abstract class AbstractRayRuntime implements RayRuntimeInternal {
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlacementGroup createPlacementGroup(List<Map<String, Double>> bundles,
|
||||
PlacementStrategy strategy) {
|
||||
return taskSubmitter.createPlacementGroup(bundles, strategy);
|
||||
public PlacementGroup createPlacementGroup(String name,
|
||||
List<Map<String, Double>> bundles, PlacementStrategy strategy) {
|
||||
boolean bundleResourceValid = bundles.stream().allMatch(
|
||||
bundle -> bundle.values().stream().allMatch(resource -> resource > 0));
|
||||
|
||||
if (bundles.isEmpty() || !bundleResourceValid) {
|
||||
throw new IllegalArgumentException(
|
||||
"Bundles cannot be empty or bundle's resource must be positive.");
|
||||
}
|
||||
return taskSubmitter.createPlacementGroup(name, bundles, strategy);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlacementGroup createPlacementGroup(
|
||||
List<Map<String, Double>> bundles, PlacementStrategy strategy) {
|
||||
return createPlacementGroup(DEFAULT_PLACEMENT_GROUP_NAME, bundles, strategy);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
||||
@@ -1,28 +1,115 @@
|
||||
package io.ray.runtime.placementgroup;
|
||||
|
||||
import io.ray.api.placementgroup.PlacementGroup;
|
||||
import io.ray.api.placementgroup.PlacementStrategy;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* The default implementation of `PlacementGroup` interface.
|
||||
*/
|
||||
public class PlacementGroupImpl implements PlacementGroup {
|
||||
|
||||
private PlacementGroupId id;
|
||||
private int bundleCount = 0;
|
||||
private final PlacementGroupId id;
|
||||
private final String name;
|
||||
private final List<Map<String, Double>> bundles;
|
||||
private final PlacementStrategy strategy;
|
||||
private final PlacementGroupState state;
|
||||
|
||||
public PlacementGroupImpl() {
|
||||
}
|
||||
|
||||
public PlacementGroupImpl(PlacementGroupId id, int bundleCount) {
|
||||
private PlacementGroupImpl(PlacementGroupId id, String name,
|
||||
List<Map<String, Double>> bundles,
|
||||
PlacementStrategy strategy,
|
||||
PlacementGroupState state) {
|
||||
this.id = id;
|
||||
this.bundleCount = bundleCount;
|
||||
this.name = name;
|
||||
this.bundles = bundles;
|
||||
this.strategy = strategy;
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public PlacementGroupId getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public int getBundleCount() {
|
||||
return bundleCount;
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public List<Map<String, Double>> getBundles() {
|
||||
return bundles;
|
||||
}
|
||||
|
||||
public PlacementStrategy getStrategy() {
|
||||
return strategy;
|
||||
}
|
||||
|
||||
public PlacementGroupState getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
/**
|
||||
* A help class for create the Placement Group.
|
||||
*/
|
||||
public static class Builder {
|
||||
private PlacementGroupId id;
|
||||
private String name;
|
||||
private List<Map<String, Double>> bundles;
|
||||
private PlacementStrategy strategy;
|
||||
private PlacementGroupState state;
|
||||
|
||||
/**
|
||||
* Set the Id of the Placement Group.
|
||||
* @param id Id of the Placement Group.
|
||||
* @return self.
|
||||
*/
|
||||
public Builder setId(PlacementGroupId id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the name of the Placement Group.
|
||||
* @param name Name of the Placement Group.
|
||||
* @return self.
|
||||
*/
|
||||
public Builder setName(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the bundles of the Placement Group.
|
||||
* @param bundles the bundles of the Placement Group.
|
||||
* @return self.
|
||||
*/
|
||||
public Builder setBundles(List<Map<String, Double>> bundles) {
|
||||
this.bundles = bundles;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the placement strategy of the Placement Group.
|
||||
* @param strategy the placement strategy of the Placement Group.
|
||||
* @return self.
|
||||
*/
|
||||
public Builder setStrategy(PlacementStrategy strategy) {
|
||||
this.strategy = strategy;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the placement state of the Placement Group.
|
||||
* @param state the state of the Placement Group.
|
||||
* @return self.
|
||||
*/
|
||||
public Builder setState(PlacementGroupState state) {
|
||||
this.state = state;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PlacementGroupImpl build() {
|
||||
return new PlacementGroupImpl(id, name, bundles, strategy, state);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package io.ray.runtime.placementgroup;
|
||||
|
||||
/**
|
||||
* State of Placement Group.
|
||||
*/
|
||||
public enum PlacementGroupState {
|
||||
|
||||
/**
|
||||
* Wait for resource to schedule.
|
||||
*/
|
||||
PENDING(0),
|
||||
|
||||
/**
|
||||
* The Placement Group has created on some node.
|
||||
*/
|
||||
CREATED(1),
|
||||
|
||||
/**
|
||||
* The Placement Group has removed.
|
||||
*/
|
||||
REMOVED(2);
|
||||
|
||||
private int value = 0;
|
||||
|
||||
PlacementGroupState(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public int value() {
|
||||
return this.value;
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,6 @@ import io.ray.runtime.generated.Common.TaskSpec;
|
||||
import io.ray.runtime.generated.Common.TaskType;
|
||||
import io.ray.runtime.object.LocalModeObjectStore;
|
||||
import io.ray.runtime.object.NativeRayObject;
|
||||
import io.ray.runtime.placementgroup.PlacementGroupId;
|
||||
import io.ray.runtime.placementgroup.PlacementGroupImpl;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
@@ -171,7 +170,7 @@ public class LocalModeTaskSubmitter implements TaskSubmitter {
|
||||
if (options.group != null) {
|
||||
PlacementGroupImpl group = (PlacementGroupImpl)options.group;
|
||||
Preconditions.checkArgument(options.bundleIndex >= 0
|
||||
&& options.bundleIndex < group.getBundleCount(),
|
||||
&& options.bundleIndex < group.getBundles().size(),
|
||||
String.format("Bundle index %s is invalid", options.bundleIndex));
|
||||
}
|
||||
}
|
||||
@@ -224,9 +223,10 @@ public class LocalModeTaskSubmitter implements TaskSubmitter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlacementGroup createPlacementGroup(List<Map<String, Double>> bundles,
|
||||
public PlacementGroup createPlacementGroup(String name, List<Map<String, Double>> bundles,
|
||||
PlacementStrategy strategy) {
|
||||
return new PlacementGroupImpl(PlacementGroupId.fromRandom(), bundles.size());
|
||||
return new PlacementGroupImpl.Builder()
|
||||
.setName(name).setBundles(bundles).setStrategy(strategy).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -43,7 +43,7 @@ public class NativeTaskSubmitter implements TaskSubmitter {
|
||||
if (options.group != null) {
|
||||
PlacementGroupImpl group = (PlacementGroupImpl)options.group;
|
||||
Preconditions.checkArgument(options.bundleIndex >= 0
|
||||
&& options.bundleIndex < group.getBundleCount(),
|
||||
&& options.bundleIndex < group.getBundles().size(),
|
||||
String.format("Bundle index %s is invalid", options.bundleIndex));
|
||||
}
|
||||
|
||||
@@ -78,10 +78,12 @@ public class NativeTaskSubmitter implements TaskSubmitter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlacementGroup createPlacementGroup(List<Map<String, Double>> bundles,
|
||||
public PlacementGroup createPlacementGroup(String name, List<Map<String, Double>> bundles,
|
||||
PlacementStrategy strategy) {
|
||||
byte[] bytes = nativeCreatePlacementGroup(bundles, strategy.value());
|
||||
return new PlacementGroupImpl(PlacementGroupId.fromBytes(bytes), bundles.size());
|
||||
byte[] bytes = nativeCreatePlacementGroup(name, bundles, strategy.value());
|
||||
return new PlacementGroupImpl.Builder()
|
||||
.setId(PlacementGroupId.fromBytes(bytes))
|
||||
.setName(name).setBundles(bundles).setStrategy(strategy).build();
|
||||
}
|
||||
|
||||
private static native List<byte[]> nativeSubmitTask(FunctionDescriptor functionDescriptor,
|
||||
@@ -95,6 +97,6 @@ public class NativeTaskSubmitter implements TaskSubmitter {
|
||||
FunctionDescriptor functionDescriptor, int functionDescriptorHash, List<FunctionArg> args,
|
||||
int numReturns, CallOptions callOptions);
|
||||
|
||||
private static native byte[] nativeCreatePlacementGroup(List<Map<String, Double>> bundles,
|
||||
int strategy);
|
||||
private static native byte[] nativeCreatePlacementGroup(String name,
|
||||
List<Map<String, Double>> bundles, int strategy);
|
||||
}
|
||||
|
||||
@@ -52,11 +52,13 @@ public interface TaskSubmitter {
|
||||
|
||||
/**
|
||||
* Create a placement group.
|
||||
* @param bundles Preallocated resource list.
|
||||
*
|
||||
* @param name Name of the Placement Group.
|
||||
* @param bundles Pre-allocated resource list.
|
||||
* @param strategy Actor placement strategy.
|
||||
* @return A handle to the created placement group.
|
||||
*/
|
||||
PlacementGroup createPlacementGroup(List<Map<String, Double>> bundles,
|
||||
PlacementGroup createPlacementGroup(String name, List<Map<String, Double>> bundles,
|
||||
PlacementStrategy strategy);
|
||||
|
||||
BaseActorHandle getActor(ActorId actorId);
|
||||
|
||||
Reference in New Issue
Block a user