Search in sources :

Example 1 with BridgeServiceException

use of org.sagebionetworks.bridge.exceptions.BridgeServiceException in project BridgeServer2 by Sage-Bionetworks.

the class BridgeUtils method ifFailuresThrowException.

/**
 * All batch methods in Dynamo return a list of failures rather than
 * throwing an exception. We should have an exception specifically for
 * these so the caller gets a list of items back, but for now, convert
 * to a generic exception;
 * @param failures
 */
public static void ifFailuresThrowException(List<FailedBatch> failures) {
    if (!failures.isEmpty()) {
        List<String> messages = Lists.newArrayList();
        for (FailedBatch failure : failures) {
            String message = failure.getException().getMessage();
            messages.add(message);
            String ids = Joiner.on("; ").join(failure.getUnprocessedItems().keySet());
            messages.add(ids);
        }
        throw new BridgeServiceException(Joiner.on(", ").join(messages));
    }
}
Also used : BridgeServiceException(org.sagebionetworks.bridge.exceptions.BridgeServiceException) FailedBatch(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.FailedBatch)

Example 2 with BridgeServiceException

use of org.sagebionetworks.bridge.exceptions.BridgeServiceException in project BridgeServer2 by Sage-Bionetworks.

the class DynamoAppConfigElementDao method getMostRecentElements.

@Override
public List<AppConfigElement> getMostRecentElements(String appId, boolean includeDeleted) {
    DynamoAppConfigElement key = new DynamoAppConfigElement();
    key.setAppId(appId);
    DynamoDBQueryExpression<DynamoAppConfigElement> query = new DynamoDBQueryExpression<DynamoAppConfigElement>().withIndexName(STUDY_ID_INDEX_NAME).withHashKeyValues(key).withConsistentRead(false).withScanIndexForward(false);
    List<DynamoAppConfigElement> elementIndices = mapper.query(DynamoAppConfigElement.class, query);
    Map<String, AppConfigElement> versionMap = Maps.newHashMap();
    Map<String, List<Object>> resultMap = mapper.batchLoad(elementIndices);
    for (List<Object> resultList : resultMap.values()) {
        for (Object oneResult : resultList) {
            if (!(oneResult instanceof DynamoAppConfigElement)) {
                // This should never happen, but just in case.
                throw new BridgeServiceException("DynamoDB returned objects of type " + oneResult.getClass().getName() + " instead of DynamoAppConfigElement");
            }
            DynamoAppConfigElement oneElement = (DynamoAppConfigElement) oneResult;
            if (!includeDeleted && oneElement.isDeleted()) {
                continue;
            }
            AppConfigElement existingElement = versionMap.get(oneElement.getId());
            Long existingRevision = (existingElement == null) ? null : existingElement.getRevision();
            if (existingRevision == null || oneElement.getRevision() > existingRevision) {
                versionMap.put(oneElement.getId(), oneElement);
            }
        }
    }
    List<AppConfigElement> elements = Lists.newArrayList(versionMap.values());
    Collections.sort(elements, Comparator.comparing(AppConfigElement::getId));
    return elements;
}
Also used : BridgeServiceException(org.sagebionetworks.bridge.exceptions.BridgeServiceException) DynamoDBQueryExpression(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression) AppConfigElement(org.sagebionetworks.bridge.models.appconfig.AppConfigElement) List(java.util.List) PaginatedQueryList(com.amazonaws.services.dynamodbv2.datamodeling.PaginatedQueryList)

Example 3 with BridgeServiceException

use of org.sagebionetworks.bridge.exceptions.BridgeServiceException in project BridgeServer2 by Sage-Bionetworks.

the class CacheProvider method removeSetOfCacheKeys.

public void removeSetOfCacheKeys(CacheKey cacheKeyOfSet) {
    checkNotNull(cacheKeyOfSet);
    try {
        Set<String> members = jedisOps.smembers(cacheKeyOfSet.toString());
        if (members != null && !members.isEmpty()) {
            try (JedisTransaction transaction = jedisOps.getTransaction()) {
                for (String oneMember : members) {
                    transaction.del(oneMember);
                }
                transaction.del(cacheKeyOfSet.toString());
                transaction.exec();
            }
        }
    } catch (Throwable e) {
        promptToStartRedisIfLocal(e);
        throw new BridgeServiceException(e);
    }
}
Also used : JedisTransaction(org.sagebionetworks.bridge.redis.JedisTransaction) BridgeServiceException(org.sagebionetworks.bridge.exceptions.BridgeServiceException)

Example 4 with BridgeServiceException

use of org.sagebionetworks.bridge.exceptions.BridgeServiceException in project BridgeServer2 by Sage-Bionetworks.

the class CacheProvider method getObject.

public <T> T getObject(CacheKey cacheKey, TypeReference<T> typeRef) {
    checkNotNull(cacheKey);
    checkNotNull(typeRef);
    try {
        String ser = jedisOps.get(cacheKey.toString());
        if (ser != null) {
            JsonNode node = adjustJsonWithStudyIdentifier(ser);
            return BridgeObjectMapper.get().readValue(node.toString(), typeRef);
        }
    } catch (Throwable e) {
        promptToStartRedisIfLocal(e);
        throw new BridgeServiceException(e);
    }
    return null;
}
Also used : BridgeServiceException(org.sagebionetworks.bridge.exceptions.BridgeServiceException) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 5 with BridgeServiceException

use of org.sagebionetworks.bridge.exceptions.BridgeServiceException in project BridgeServer2 by Sage-Bionetworks.

the class CacheProvider method getObject.

/**
 * Get the object, resetting its expiration period.
 */
public <T> T getObject(CacheKey cacheKey, Class<T> clazz, int expireInSeconds) {
    checkNotNull(cacheKey);
    checkNotNull(clazz);
    try {
        String ser = jedisOps.get(cacheKey.toString());
        if (ser != null) {
            jedisOps.expire(cacheKey.toString(), expireInSeconds);
            return BridgeObjectMapper.get().readValue(ser, clazz);
        }
    } catch (Throwable e) {
        promptToStartRedisIfLocal(e);
        throw new BridgeServiceException(e);
    }
    return null;
}
Also used : BridgeServiceException(org.sagebionetworks.bridge.exceptions.BridgeServiceException)

Aggregations

BridgeServiceException (org.sagebionetworks.bridge.exceptions.BridgeServiceException)78 IOException (java.io.IOException)25 Test (org.testng.annotations.Test)21 PersistenceException (javax.persistence.PersistenceException)7 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 CacheKey (org.sagebionetworks.bridge.cache.CacheKey)5 App (org.sagebionetworks.bridge.models.apps.App)5 AmazonServiceException (com.amazonaws.AmazonServiceException)4 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)4 EntityNotFoundException (org.sagebionetworks.bridge.exceptions.EntityNotFoundException)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Stopwatch (com.google.common.base.Stopwatch)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 List (java.util.List)3 BadRequestException (org.sagebionetworks.bridge.exceptions.BadRequestException)3 FailedBatch (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.FailedBatch)2 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)2 SendMessageResult (com.amazonaws.services.sqs.model.SendMessageResult)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2