use of org.apache.flink.api.common.resources.ExternalResource in project flink by apache.
the class ResourceSpec method subtract.
/**
* Subtracts another resource spec from this one.
*
* @param other The other resource spec to subtract.
* @return The subtracted resource spec.
*/
public ResourceSpec subtract(final ResourceSpec other) {
checkNotNull(other, "Cannot subtract null resources");
if (this.equals(UNKNOWN) || other.equals(UNKNOWN)) {
return UNKNOWN;
}
checkArgument(other.lessThanOrEqual(this), "Cannot subtract a larger ResourceSpec from this one.");
Map<String, ExternalResource> resultExtendedResources = new HashMap<>(extendedResources);
for (ExternalResource resource : other.extendedResources.values()) {
resultExtendedResources.merge(resource.getName(), resource, (v1, v2) -> v1.subtract(v2));
}
return new ResourceSpec(this.cpuCores.subtract(other.cpuCores), this.taskHeapMemory.subtract(other.taskHeapMemory), this.taskOffHeapMemory.subtract(other.taskOffHeapMemory), this.managedMemory.subtract(other.managedMemory), resultExtendedResources);
}
use of org.apache.flink.api.common.resources.ExternalResource in project flink by apache.
the class TaskExecutorProcessUtilsTest method testProcessSpecFromWorkerResourceSpec.
@Test
public void testProcessSpecFromWorkerResourceSpec() {
final WorkerResourceSpec workerResourceSpec = new WorkerResourceSpec.Builder().setCpuCores(1.0).setTaskHeapMemoryMB(100).setTaskOffHeapMemoryMB(200).setNetworkMemoryMB(300).setManagedMemoryMB(400).setNumSlots(5).setExtendedResource(new ExternalResource(EXTERNAL_RESOURCE_NAME_1, 1)).build();
final TaskExecutorProcessSpec taskExecutorProcessSpec = TaskExecutorProcessUtils.processSpecFromWorkerResourceSpec(new Configuration(), workerResourceSpec);
assertEquals(workerResourceSpec.getCpuCores(), taskExecutorProcessSpec.getCpuCores());
assertEquals(workerResourceSpec.getTaskHeapSize(), taskExecutorProcessSpec.getTaskHeapSize());
assertEquals(workerResourceSpec.getTaskOffHeapSize(), taskExecutorProcessSpec.getTaskOffHeapSize());
assertEquals(workerResourceSpec.getNetworkMemSize(), taskExecutorProcessSpec.getNetworkMemSize());
assertEquals(workerResourceSpec.getManagedMemSize(), taskExecutorProcessSpec.getManagedMemorySize());
assertEquals(workerResourceSpec.getNumSlots(), taskExecutorProcessSpec.getNumSlots());
assertEquals(workerResourceSpec.getExtendedResources(), taskExecutorProcessSpec.getExtendedResources());
}
use of org.apache.flink.api.common.resources.ExternalResource in project flink by apache.
the class ResourceProfileTest method testAllFieldsNoLessThanProfile.
@Test
public void testAllFieldsNoLessThanProfile() {
final ResourceProfile rp1 = ResourceProfile.newBuilder().setCpuCores(1.0).setTaskHeapMemoryMB(100).setTaskOffHeapMemoryMB(100).setManagedMemoryMB(100).build();
final ResourceProfile rp2 = ResourceProfile.newBuilder().setCpuCores(1.0).setTaskHeapMemoryMB(200).setTaskOffHeapMemoryMB(200).setManagedMemoryMB(200).build();
final ResourceProfile rp3 = ResourceProfile.newBuilder().setCpuCores(2.0).setTaskHeapMemoryMB(100).setTaskOffHeapMemoryMB(100).setManagedMemoryMB(100).build();
final ResourceProfile rp4 = ResourceProfile.newBuilder().setCpuCores(2.0).setTaskHeapMemoryMB(200).setTaskOffHeapMemoryMB(200).setManagedMemoryMB(200).build();
assertFalse(rp1.allFieldsNoLessThan(rp2));
assertTrue(rp2.allFieldsNoLessThan(rp1));
assertFalse(rp1.allFieldsNoLessThan(rp3));
assertTrue(rp3.allFieldsNoLessThan(rp1));
assertFalse(rp2.allFieldsNoLessThan(rp3));
assertFalse(rp3.allFieldsNoLessThan(rp2));
assertTrue(rp4.allFieldsNoLessThan(rp1));
assertTrue(rp4.allFieldsNoLessThan(rp2));
assertTrue(rp4.allFieldsNoLessThan(rp3));
assertTrue(rp4.allFieldsNoLessThan(rp4));
final ResourceProfile rp5 = ResourceProfile.newBuilder().setCpuCores(2.0).setTaskHeapMemoryMB(100).setTaskOffHeapMemoryMB(100).setManagedMemoryMB(100).setNetworkMemoryMB(100).build();
assertFalse(rp4.allFieldsNoLessThan(rp5));
ResourceSpec rs1 = ResourceSpec.newBuilder(1.0, 100).setExtendedResource(new ExternalResource(EXTERNAL_RESOURCE_NAME, 2.2)).build();
ResourceSpec rs2 = ResourceSpec.newBuilder(1.0, 100).setExtendedResource(new ExternalResource(EXTERNAL_RESOURCE_NAME, 1.1)).build();
assertFalse(rp1.allFieldsNoLessThan(ResourceProfile.fromResourceSpec(rs1)));
assertTrue(ResourceProfile.fromResourceSpec(rs1).allFieldsNoLessThan(ResourceProfile.fromResourceSpec(rs2)));
assertFalse(ResourceProfile.fromResourceSpec(rs2).allFieldsNoLessThan(ResourceProfile.fromResourceSpec(rs1)));
}
use of org.apache.flink.api.common.resources.ExternalResource in project flink by apache.
the class ExternalResourceUtilsTest method testGetExternalResourcesCollection.
@Test
public void testGetExternalResourcesCollection() {
final Configuration config = new Configuration();
config.set(ExternalResourceOptions.EXTERNAL_RESOURCE_LIST, Collections.singletonList(RESOURCE_NAME_1));
config.setLong(ExternalResourceOptions.getAmountConfigOptionForResource(RESOURCE_NAME_1), RESOURCE_AMOUNT_1);
final Collection<ExternalResource> externalResources = ExternalResourceUtils.getExternalResourcesCollection(config);
assertThat(externalResources.size(), is(1));
assertThat(externalResources, contains(new ExternalResource(RESOURCE_NAME_1, RESOURCE_AMOUNT_1)));
}
use of org.apache.flink.api.common.resources.ExternalResource in project flink by apache.
the class TaskExecutorProcessSpecTest method testEquals.
@Test
public void testEquals() {
TaskExecutorProcessSpec spec1 = new TaskExecutorProcessSpec(new CPUResource(1.0), MemorySize.parse("1m"), MemorySize.parse("2m"), MemorySize.parse("3m"), MemorySize.parse("4m"), MemorySize.parse("5m"), MemorySize.parse("6m"), MemorySize.parse("7m"), MemorySize.parse("8m"), Collections.singleton(new ExternalResource(EXTERNAL_RESOURCE_NAME, 1)));
TaskExecutorProcessSpec spec2 = new TaskExecutorProcessSpec(new CPUResource(1.0), MemorySize.parse("1m"), MemorySize.parse("2m"), MemorySize.parse("3m"), MemorySize.parse("4m"), MemorySize.parse("5m"), MemorySize.parse("6m"), MemorySize.parse("7m"), MemorySize.parse("8m"), Collections.singleton(new ExternalResource(EXTERNAL_RESOURCE_NAME, 1)));
assertThat(spec1, is(spec2));
}
Aggregations