use of bio.terra.buffer.common.PoolAndResourceStates in project terra-resource-buffer by DataBiosphere.
the class BufferDaoTest method retrievePoolWithResourceState.
@Test
public void retrievePoolWithResourceState() {
Pool pool1 = newPool(PoolId.create("poolId1"));
Pool pool2 = newPool(PoolId.create("poolId2"));
Pool pool3 = newPool(PoolId.create("poolId3"));
// Pool1 has 1 CREATING, 2 READY, Pool2 has 1 READY, 1 HANDED_OUT, Pool3 is empty
bufferDao.createPools(ImmutableList.of(pool1, pool2, pool3));
bufferDao.createResource(newResource(pool1.id(), ResourceState.CREATING));
bufferDao.createResource(newResource(pool1.id(), ResourceState.READY));
bufferDao.createResource(newResource(pool1.id(), ResourceState.READY));
bufferDao.createResource(newResource(pool2.id(), ResourceState.READY));
bufferDao.createResource(newResource(pool2.id(), ResourceState.HANDED_OUT));
PoolAndResourceStates pool1State = PoolAndResourceStates.builder().setPool(pool1).setResourceStateCount(ResourceState.CREATING, 1).setResourceStateCount(ResourceState.READY, 2).build();
assertThat(bufferDao.retrievePoolAndResourceStates(), Matchers.containsInAnyOrder(pool1State, PoolAndResourceStates.builder().setPool(pool2).setResourceStateCount(ResourceState.READY, 1).setResourceStateCount(ResourceState.HANDED_OUT, 1).build(), PoolAndResourceStates.builder().setPool(pool3).build()));
assertEquals(pool1State, bufferDao.retrievePoolAndResourceStatesById(pool1.id()).get());
}
use of bio.terra.buffer.common.PoolAndResourceStates in project terra-resource-buffer by DataBiosphere.
the class PoolService method getPoolInfo.
/**
* Gets pool information by given {@link PoolId}.
*/
public PoolInfo getPoolInfo(PoolId poolId) {
Optional<PoolAndResourceStates> poolAndResourceStates = bufferDao.retrievePoolAndResourceStatesById(poolId);
if (poolAndResourceStates.isEmpty()) {
throw new NotFoundException(String.format("Pool %s not found", poolId));
}
Pool pool = poolAndResourceStates.get().pool();
Multiset<ResourceState> resourceStates = poolAndResourceStates.get().resourceStates();
return new PoolInfo().poolConfig(new PoolConfig().poolId(poolId.toString()).size(pool.size()).resourceConfigName(pool.resourceConfig().getConfigName())).status(bio.terra.buffer.generated.model.PoolStatus.valueOf(pool.status().toString())).putResourceStateCountItem(ResourceState.CREATING.name(), resourceStates.count(ResourceState.CREATING)).putResourceStateCountItem(ResourceState.READY.name(), resourceStates.count(ResourceState.READY)).putResourceStateCountItem(ResourceState.DELETED.name(), resourceStates.count(ResourceState.DELETED)).putResourceStateCountItem(ResourceState.HANDED_OUT.name(), resourceStates.count(ResourceState.HANDED_OUT));
}
use of bio.terra.buffer.common.PoolAndResourceStates in project terra-resource-buffer by DataBiosphere.
the class FlightScheduler method scheduleFlights.
/**
* Try to schedule flights to create and delete resources until resource count matches each pool
* state or reach to configuration limit.
*/
private void scheduleFlights() {
logger.info("Beginning scheduling flights.");
List<PoolAndResourceStates> poolAndResourceStatesList = bufferDao.retrievePoolAndResourceStates();
for (PoolAndResourceStates poolAndResources : poolAndResourceStatesList) {
recordResourceStateCount(poolAndResources);
if (poolAndResources.pool().status().equals(PoolStatus.ACTIVE)) {
int poolSize = poolAndResources.pool().size();
int readyAndCreatingCount = poolAndResources.resourceStates().count(ResourceState.CREATING) + poolAndResources.resourceStates().count(ResourceState.READY);
logger.info("Pool id: {}, size:{}, readyAndCreatingCount: {}.", poolAndResources.pool().id(), poolSize, readyAndCreatingCount);
if (poolSize > readyAndCreatingCount) {
int poolsToCreateCount = poolSize - readyAndCreatingCount;
scheduleCreationFlights(poolAndResources.pool(), poolsToCreateCount);
} else if (primaryConfiguration.isDeleteExcessResources() && poolAndResources.resourceStates().count(ResourceState.READY) > poolSize) {
// Only delete READY resource, we hope future schedule runs will delete resources.
// just turns to READY from CREATING.
int excessReadyCount = poolAndResources.resourceStates().count(ResourceState.READY) - poolSize;
scheduleDeletionFlights(poolAndResources.pool(), excessReadyCount);
}
} else {
// Only deletion READY resource, we hope future schedule runs will deletion resources
// just turns to READY from CREATING.
scheduleDeletionFlights(poolAndResources.pool(), poolAndResources.resourceStates().count(ResourceState.READY));
}
}
}
Aggregations