use of org.apache.mesos.Protos.Value in project dcos-commons by mesosphere.
the class ValueUtils method subtract.
public static Value subtract(Value val1, Value val2) {
Type type1 = val1.getType();
Type type2 = val2.getType();
if (type1 != type2) {
return null;
}
switch(type1) {
case SCALAR:
Value.Scalar scalar = subtract(val1.getScalar(), val2.getScalar());
return Value.newBuilder().setType(type1).setScalar(scalar).build();
case RANGES:
Value.Ranges ranges = subtract(val1.getRanges(), val2.getRanges());
return Value.newBuilder().setType(type1).setRanges(ranges).build();
default:
return null;
}
}
use of org.apache.mesos.Protos.Value in project dcos-commons by mesosphere.
the class MesosResourcePool method consumeReservableMerged.
public Optional<MesosResource> consumeReservableMerged(String name, Value desiredValue, String preReservedRole) {
Map<String, Value> pool = reservableMergedPoolByRole.get(preReservedRole);
if (pool == null) {
LOGGER.info("No unreserved resources available for role '{}'. Reservable roles are: {}", preReservedRole, reservableMergedPoolByRole.keySet());
return Optional.empty();
}
Value availableValue = pool.get(name);
if (sufficientValue(desiredValue, availableValue)) {
pool.put(name, ValueUtils.subtract(availableValue, desiredValue));
reservableMergedPoolByRole.put(preReservedRole, pool);
Resource.Builder builder = ResourceBuilder.fromUnreservedValue(name, desiredValue).build().toBuilder();
if (Capabilities.getInstance().supportsPreReservedResources() && !preReservedRole.equals(Constants.ANY_ROLE)) {
builder.addReservations(Resource.ReservationInfo.newBuilder().setRole(preReservedRole).setType(Resource.ReservationInfo.Type.STATIC));
}
return Optional.of(new MesosResource(builder.build()));
} else {
if (availableValue == null) {
LOGGER.info("Offer lacks any unreserved {} resources for role {}", name, preReservedRole);
} else {
LOGGER.info("Offered quantity of {} for role {} is insufficient: desired {}, offered {}", name, preReservedRole, TextFormat.shortDebugString(desiredValue), TextFormat.shortDebugString(availableValue));
}
return Optional.empty();
}
}
use of org.apache.mesos.Protos.Value in project dcos-commons by mesosphere.
the class MesosResourcePool method getResourcePool.
private static Map<String, Value> getResourcePool(Collection<MesosResource> mesosResources) {
Map<String, Value> pool = new HashMap<>();
for (MesosResource mesosResource : mesosResources) {
String name = mesosResource.getName();
Value currValue = pool.get(name);
if (currValue == null) {
currValue = ValueUtils.getZero(mesosResource.getType());
}
pool.put(name, ValueUtils.add(currValue, mesosResource.getValue()));
}
return pool;
}
use of org.apache.mesos.Protos.Value in project dcos-commons by mesosphere.
the class MesosResourcePool method freeMergedResource.
private void freeMergedResource(MesosResource mesosResource) {
if (mesosResource.getResourceId().isPresent()) {
dynamicallyReservedPoolByResourceId.remove(mesosResource.getResourceId().get());
LOGGER.info("Freed resource: {}", !dynamicallyReservedPoolByResourceId.containsKey(mesosResource.getResourceId().get()));
}
String previousRole = mesosResource.getPreviousRole();
Map<String, Value> pool = reservableMergedPoolByRole.get(previousRole);
if (pool == null) {
pool = new HashMap<>();
}
Value currValue = pool.get(mesosResource.getName());
if (currValue == null) {
currValue = ValueUtils.getZero(mesosResource.getType());
}
Value updatedValue = ValueUtils.add(currValue, mesosResource.getValue());
pool.put(mesosResource.getName(), updatedValue);
reservableMergedPoolByRole.put(previousRole, pool);
}
use of org.apache.mesos.Protos.Value in project dcos-commons by mesosphere.
the class MesosResourcePool method consumeReserved.
public Optional<MesosResource> consumeReserved(String name, Value value, String resourceId) {
MesosResource mesosResource = dynamicallyReservedPoolByResourceId.get(resourceId);
if (mesosResource != null) {
if (mesosResource.isAtomic()) {
if (sufficientValue(value, mesosResource.getValue())) {
dynamicallyReservedPoolByResourceId.remove(resourceId);
} else {
LOGGER.warn("Reserved atomic quantity of {} is insufficient: desired {}, reserved {}", name, TextFormat.shortDebugString(value), TextFormat.shortDebugString(mesosResource.getValue()));
return Optional.empty();
}
} else {
Value availableValue = mesosResource.getValue();
if (ValueUtils.compare(availableValue, value) > 0) {
// Update the value in pool with the remaining unclaimed resource amount
Resource remaining = ResourceBuilder.fromExistingResource(mesosResource.getResource()).setValue(ValueUtils.subtract(availableValue, value)).build();
dynamicallyReservedPoolByResourceId.put(resourceId, new MesosResource(remaining));
// Return only the claimed resource amount from this reservation
} else {
dynamicallyReservedPoolByResourceId.remove(resourceId);
}
}
} else {
LOGGER.warn("Failed to find reserved {} resource with ID: {}. Reserved resource IDs are: {}", name, resourceId, dynamicallyReservedPoolByResourceId.keySet());
}
return Optional.ofNullable(mesosResource);
}