Search in sources :

Example 1 with ObjectOptimisticLockingFailureException

use of org.springframework.orm.ObjectOptimisticLockingFailureException in project workbench by all-of-us.

the class WorkspaceDaoTest method testWorkspaceVersionLocking.

@Test
public void testWorkspaceVersionLocking() {
    Workspace ws = new Workspace();
    ws.setVersion(1);
    ws = workspaceDao.save(ws);
    // Version incremented to 2.
    ws.setName("foo");
    ws = workspaceDao.save(ws);
    try {
        ws.setName("bar");
        ws.setVersion(1);
        workspaceDao.save(ws);
        fail("expected optimistic lock exception on stale version update");
    } catch (ObjectOptimisticLockingFailureException e) {
    // expected
    }
}
Also used : Workspace(org.pmiops.workbench.db.model.Workspace) ObjectOptimisticLockingFailureException(org.springframework.orm.ObjectOptimisticLockingFailureException) Test(org.junit.Test) DataJpaTest(org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest)

Example 2 with ObjectOptimisticLockingFailureException

use of org.springframework.orm.ObjectOptimisticLockingFailureException in project workbench by all-of-us.

the class UserService method updateWithRetries.

private User updateWithRetries(Function<User, User> userModifier, User user) {
    int numAttempts = 0;
    while (true) {
        user = userModifier.apply(user);
        updateDataAccessLevel(user);
        try {
            user = userDao.save(user);
            return user;
        } catch (ObjectOptimisticLockingFailureException e) {
            if (numAttempts < MAX_RETRIES) {
                user = userDao.findOne(user.getUserId());
                numAttempts++;
            } else {
                throw new ConflictException(String.format("Could not update user %s", user.getUserId()));
            }
        }
    }
}
Also used : ConflictException(org.pmiops.workbench.exceptions.ConflictException) ObjectOptimisticLockingFailureException(org.springframework.orm.ObjectOptimisticLockingFailureException)

Example 3 with ObjectOptimisticLockingFailureException

use of org.springframework.orm.ObjectOptimisticLockingFailureException in project workbench by all-of-us.

the class ProfileController method initializeUserIfNeeded.

private User initializeUserIfNeeded() {
    UserAuthentication userAuthentication = userAuthenticationProvider.get();
    User user = userAuthentication.getUser();
    if (userAuthentication.getUserType() == UserType.SERVICE_ACCOUNT) {
        // Service accounts don't need further initialization.
        return user;
    }
    // On first sign-in, create a FC user, billing project, and set the first sign in time.
    if (user.getFirstSignInTime() == null) {
        // instead use the freeTierBillingProjectStatus.
        if (user.getFreeTierBillingProjectName() == null) {
            String billingProjectName = createFirecloudUserAndBillingProject(user);
            user.setFreeTierBillingProjectName(billingProjectName);
            user.setFreeTierBillingProjectStatus(BillingProjectStatus.PENDING);
        }
        user.setFirstSignInTime(new Timestamp(clock.instant().toEpochMilli()));
        try {
            return userDao.save(user);
        } catch (ObjectOptimisticLockingFailureException e) {
            log.log(Level.WARNING, "version conflict for user update", e);
            throw new ConflictException("Failed due to concurrent modification");
        }
    }
    // Free tier billing project setup is complete; nothing to do.
    if (BillingProjectStatus.READY.equals(user.getFreeTierBillingProjectStatus())) {
        return user;
    }
    // On subsequent sign-ins to the first, attempt to complete the setup of the FC billing project
    // and mark the Workbench's project setup as completed. FC project creation is asynchronous, so
    // first confirm whether Firecloud claims the project setup is complete.
    BillingProjectStatus status = null;
    try {
        status = fireCloudService.getBillingProjectMemberships().stream().filter(m -> user.getFreeTierBillingProjectName().equals(m.getProjectName())).map(m -> fcToWorkbenchBillingMap.get(m.getCreationStatus())).findFirst().orElse(BillingProjectStatus.NONE);
    } catch (ApiException e) {
        log.log(Level.WARNING, "failed to retrieve billing projects, continuing", e);
        return user;
    }
    switch(status) {
        case NONE:
        case PENDING:
            log.log(Level.INFO, "free tier project is still initializing, continuing");
            return user;
        case ERROR:
            log.log(Level.SEVERE, String.format("free tier project %s failed to be created", user.getFreeTierBillingProjectName()));
            user.setFreeTierBillingProjectStatus(status);
            return userDao.save(user);
        case READY:
            break;
        default:
            log.log(Level.SEVERE, String.format("unrecognized status '%s'", status));
            return user;
    }
    // notebooks.
    try {
        fireCloudService.grantGoogleRoleToUser(user.getFreeTierBillingProjectName(), FireCloudService.BIGQUERY_JOB_USER_GOOGLE_ROLE, user.getEmail());
    } catch (ApiException e) {
        log.log(Level.WARNING, "granting BigQuery role on created free tier billing project failed", e);
        // Allow the user to continue, as most workbench functionality will still be usable.
        return user;
    }
    log.log(Level.INFO, "free tier project initialized and BigQuery role granted");
    user.setFreeTierBillingProjectStatus(BillingProjectStatus.READY);
    return userDao.save(user);
}
Also used : Message(javax.mail.Message) ObjectOptimisticLockingFailureException(org.springframework.orm.ObjectOptimisticLockingFailureException) IdVerificationListResponse(org.pmiops.workbench.model.IdVerificationListResponse) Provider(javax.inject.Provider) MailChimpService(org.pmiops.workbench.mailchimp.MailChimpService) FireCloudService(org.pmiops.workbench.firecloud.FireCloudService) MessagingException(javax.mail.MessagingException) Autowired(org.springframework.beans.factory.annotation.Autowired) EmailException(org.pmiops.workbench.exceptions.EmailException) DirectoryService(org.pmiops.workbench.google.DirectoryService) Authority(org.pmiops.workbench.model.Authority) CreateAccountRequest(org.pmiops.workbench.model.CreateAccountRequest) Map(java.util.Map) CloudStorageService(org.pmiops.workbench.google.CloudStorageService) User(org.pmiops.workbench.db.model.User) Profile(org.pmiops.workbench.model.Profile) UserService(org.pmiops.workbench.db.dao.UserService) UserDao(org.pmiops.workbench.db.dao.UserDao) Transport(javax.mail.Transport) ImmutableMap(com.google.common.collect.ImmutableMap) Timestamp(java.sql.Timestamp) AuthorityRequired(org.pmiops.workbench.annotations.AuthorityRequired) InvitationVerificationRequest(org.pmiops.workbench.model.InvitationVerificationRequest) ConflictException(org.pmiops.workbench.exceptions.ConflictException) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) RestController(org.springframework.web.bind.annotation.RestController) List(java.util.List) Address(com.blockscore.models.Address) BillingProjectStatus(org.pmiops.workbench.model.BillingProjectStatus) WorkbenchEnvironment(org.pmiops.workbench.config.WorkbenchEnvironment) UnsupportedEncodingException(java.io.UnsupportedEncodingException) UserType(org.pmiops.workbench.auth.UserAuthentication.UserType) BillingProjectMembership(org.pmiops.workbench.model.BillingProjectMembership) UsernameTakenResponse(org.pmiops.workbench.model.UsernameTakenResponse) ApiException(org.pmiops.workbench.firecloud.ApiException) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) InternetAddress(javax.mail.internet.InternetAddress) ProfileService(org.pmiops.workbench.auth.ProfileService) BadRequestException(org.pmiops.workbench.exceptions.BadRequestException) ExceptionUtils(org.pmiops.workbench.exceptions.ExceptionUtils) Properties(java.util.Properties) IdVerificationRequest(org.pmiops.workbench.model.IdVerificationRequest) IdVerificationReviewRequest(org.pmiops.workbench.model.IdVerificationReviewRequest) BlockscoreIdVerificationStatus(org.pmiops.workbench.model.BlockscoreIdVerificationStatus) IOException(java.io.IOException) MimeMessage(javax.mail.internet.MimeMessage) BlockscoreService(org.pmiops.workbench.blockscore.BlockscoreService) UserAuthentication(org.pmiops.workbench.auth.UserAuthentication) HttpStatus(org.springframework.http.HttpStatus) ServerErrorException(org.pmiops.workbench.exceptions.ServerErrorException) WorkbenchConfig(org.pmiops.workbench.config.WorkbenchConfig) Clock(java.time.Clock) Session(javax.mail.Session) EmailVerificationStatus(org.pmiops.workbench.model.EmailVerificationStatus) Person(com.blockscore.models.Person) ResponseEntity(org.springframework.http.ResponseEntity) CreationStatusEnum(org.pmiops.workbench.firecloud.model.BillingProjectMembership.CreationStatusEnum) BillingProjectStatus(org.pmiops.workbench.model.BillingProjectStatus) User(org.pmiops.workbench.db.model.User) ConflictException(org.pmiops.workbench.exceptions.ConflictException) UserAuthentication(org.pmiops.workbench.auth.UserAuthentication) Timestamp(java.sql.Timestamp) ObjectOptimisticLockingFailureException(org.springframework.orm.ObjectOptimisticLockingFailureException) ApiException(org.pmiops.workbench.firecloud.ApiException)

Aggregations

ConflictException (org.pmiops.workbench.exceptions.ConflictException)2 ObjectOptimisticLockingFailureException (org.springframework.orm.ObjectOptimisticLockingFailureException)2 Address (com.blockscore.models.Address)1 Person (com.blockscore.models.Person)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Timestamp (java.sql.Timestamp)1 Clock (java.time.Clock)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Properties (java.util.Properties)1 Function (java.util.function.Function)1 Level (java.util.logging.Level)1 Logger (java.util.logging.Logger)1 Collectors (java.util.stream.Collectors)1 Provider (javax.inject.Provider)1 Message (javax.mail.Message)1 MessagingException (javax.mail.MessagingException)1