use of bio.terra.buffer.common.Pool in project terra-resource-buffer by DataBiosphere.
the class BufferDao method deactivatePools.
/**
* Updates list of pools' status to DEACTIVATED.
*/
// TODO: consider delaying expiration so pool can be recovered
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE)
public void deactivatePools(List<PoolId> poolIds) {
String sql = "UPDATE pool SET status = :status, expiration = :expiration WHERE id = :id ";
MapSqlParameterSource[] sqlParameterSourceList = poolIds.stream().map(poolId -> new MapSqlParameterSource().addValue("id", poolId.id()).addValue("status", PoolStatus.DEACTIVATED.toString()).addValue("expiration", OffsetDateTime.now(ZoneOffset.UTC))).toArray(MapSqlParameterSource[]::new);
jdbcTemplate.batchUpdate(sql, sqlParameterSourceList);
}
use of bio.terra.buffer.common.Pool in project terra-resource-buffer by DataBiosphere.
the class PoolService method handoutResourceTransactionally.
/**
* Process handout resource in on transaction (anything failure will cause database rollback).
*/
private Resource handoutResourceTransactionally(PoolId poolId, RequestHandoutId requestHandoutId) {
Optional<Pool> pool = bufferDao.retrievePool(poolId);
if (pool.isEmpty() || !pool.get().status().equals(PoolStatus.ACTIVE)) {
throw new BadRequestException(String.format("Invalid pool id: %s.", poolId));
}
try {
// Retry 20 times of 2 seconds each.
Optional<Resource> resource = executeAndRetry(() -> bufferDao.updateOneReadyResourceToHandedOut(poolId, requestHandoutId), Duration.ofSeconds(2), 20);
Resource result = resource.orElseThrow(() -> new NotFoundException(String.format("No resource is ready to use at this moment for pool: %s. Please try later", poolId)));
logger.info("Handed out resource ID {}, Handout ID {}, Pool ID {}", result.cloudResourceUid(), result.requestHandoutId(), poolId);
return result;
} catch (InterruptedException | DataAccessException e) {
throw new InternalServerErrorException(String.format("Failed to update one resource state from READY to HANDED_OUT for pool %s", poolId));
}
}
use of bio.terra.buffer.common.Pool in project terra-resource-buffer by DataBiosphere.
the class PoolService method updateFromConfig.
/**
* Given parsed pool configurations, create new pools, deactivate removed pools, or update pool
* size, as required.
*
* @param parsedPoolConfigs - previously parsed pool/resource configurations
*/
@VisibleForTesting
public void updateFromConfig(List<PoolWithResourceConfig> parsedPoolConfigs) {
transactionTemplate.execute(unused -> {
final Map<PoolId, Pool> allDbPoolsMap = Maps.uniqueIndex(bufferDao.retrievePools(), Pool::id);
final Map<PoolId, PoolWithResourceConfig> parsedPoolConfigMap = Maps.uniqueIndex(parsedPoolConfigs, config -> PoolId.create(config.poolConfig().getPoolId()));
final Set<PoolId> allPoolIds = Sets.union(allDbPoolsMap.keySet(), parsedPoolConfigMap.keySet());
final List<PoolWithResourceConfig> poolsToCreate = new ArrayList<>();
final List<Pool> poolsToDeactivate = new ArrayList<>();
final List<PoolWithResourceConfig> poolsToReactivate = new ArrayList<>();
final Map<PoolId, Integer> poolIdToNewSize = new HashMap<>();
// on the change.
for (PoolId id : allPoolIds) {
if (parsedPoolConfigMap.containsKey(id) && !allDbPoolsMap.containsKey(id)) {
// Exists in config but not in DB.
poolsToCreate.add(parsedPoolConfigMap.get(id));
} else if (!parsedPoolConfigMap.containsKey(id) && allDbPoolsMap.containsKey(id)) {
// Exists in DB but not in Config.
poolsToDeactivate.add(allDbPoolsMap.get(id));
} else {
// Exists in DB and Config.
final Pool dbPool = allDbPoolsMap.get(id);
final PoolWithResourceConfig configPool = parsedPoolConfigMap.get(id);
if (dbPool.status().equals(PoolStatus.DEACTIVATED)) {
PoolWithResourceConfig poolWithConfig = parsedPoolConfigMap.get(id);
// Re-activate a deactivated pool
poolsToReactivate.add(poolWithConfig);
}
if (!dbPool.resourceConfig().equals(configPool.resourceConfig())) {
// ResourceConfig name when loading from file.
throw new BadPoolConfigException(String.format("Updating ResourceConfig on existing pool (id= %s) " + "is not allowed for any attributes except size. " + "Please create a new pool config instead.", id));
} else if (dbPool.size() != (configPool.poolConfig().getSize())) {
// Exists in both places but need to update size.
poolIdToNewSize.put(dbPool.id(), configPool.poolConfig().getSize());
}
}
}
createPools(poolsToCreate);
deactivatePools(poolsToDeactivate);
reactivatePools(poolsToReactivate);
updatePoolSizes(poolIdToNewSize);
return true;
});
}
use of bio.terra.buffer.common.Pool 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.Pool in project terra-resource-buffer by DataBiosphere.
the class CleanupSchedulerTest method testScheduleCleanup.
@Test
public void testScheduleCleanup() throws Exception {
Pool pool = Pool.builder().creation(CREATION).id(PoolId.create("poolId")).resourceType(ResourceType.GOOGLE_PROJECT).size(1).resourceConfig(new ResourceConfig()).status(PoolStatus.ACTIVE).build();
CloudResourceUid cloudResourceUid = new CloudResourceUid().googleProjectUid(new GoogleProjectUid().projectId("p1"));
Resource resource = Resource.builder().id(ResourceId.create(UUID.randomUUID())).poolId(pool.id()).creation(Instant.now()).state(ResourceState.CREATING).build();
bufferDao.createPools(ImmutableList.of(pool));
bufferDao.createResource(resource);
bufferDao.updateResourceAsReady(resource.id(), cloudResourceUid);
bufferDao.updateOneReadyResourceToHandedOut(pool.id(), RequestHandoutId.create("1111"));
assertEquals(1, bufferDao.retrieveResourceToCleanup(10).size());
cleanupScheduler.initialize();
Thread.sleep(1000);
verify(mockPublisher).publish(messageArgumentCaptor.capture());
assertThat(messageArgumentCaptor.getAllValues().stream().map(m -> m.getData().toStringUtf8()).collect(Collectors.toList()), Matchers.containsInAnyOrder(objectMapper.writeValueAsString(new CreateResourceRequestBody().creation(CREATION.atOffset(ZoneOffset.UTC)).expiration(CREATION.plus(crlConfiguration.getTestResourceTimeToLive()).atOffset(ZoneOffset.UTC)).putLabelsItem("client", CLIENT_NAME).resourceUid(new bio.terra.janitor.model.CloudResourceUid().googleProjectUid(new bio.terra.janitor.model.GoogleProjectUid().projectId("p1"))))));
assertTrue(bufferDao.retrieveResourceToCleanup(10).isEmpty());
}
Aggregations