Search in sources :

Example 1 with PoolAndResourceStates

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());
}
Also used : PoolAndResourceStates(bio.terra.buffer.common.PoolAndResourceStates) Pool(bio.terra.buffer.common.Pool) BaseUnitTest(bio.terra.buffer.common.BaseUnitTest) Test(org.junit.jupiter.api.Test)

Example 2 with PoolAndResourceStates

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));
}
Also used : PoolAndResourceStates(bio.terra.buffer.common.PoolAndResourceStates) NotFoundException(bio.terra.buffer.common.exception.NotFoundException) PoolConfig(bio.terra.buffer.generated.model.PoolConfig) PoolConfigLoader.loadPoolConfig(bio.terra.buffer.service.pool.PoolConfigLoader.loadPoolConfig) Pool(bio.terra.buffer.common.Pool) ResourceState(bio.terra.buffer.common.ResourceState) PoolInfo(bio.terra.buffer.generated.model.PoolInfo)

Example 3 with PoolAndResourceStates

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));
        }
    }
}
Also used : PoolAndResourceStates(bio.terra.buffer.common.PoolAndResourceStates)

Aggregations

PoolAndResourceStates (bio.terra.buffer.common.PoolAndResourceStates)3 Pool (bio.terra.buffer.common.Pool)2 BaseUnitTest (bio.terra.buffer.common.BaseUnitTest)1 ResourceState (bio.terra.buffer.common.ResourceState)1 NotFoundException (bio.terra.buffer.common.exception.NotFoundException)1 PoolConfig (bio.terra.buffer.generated.model.PoolConfig)1 PoolInfo (bio.terra.buffer.generated.model.PoolInfo)1 PoolConfigLoader.loadPoolConfig (bio.terra.buffer.service.pool.PoolConfigLoader.loadPoolConfig)1 Test (org.junit.jupiter.api.Test)1