From 0a306bd4e2ec444f5d65c21c9afde84efb10224a Mon Sep 17 00:00:00 2001 From: sergeyPensov Date: Tue, 13 Nov 2018 12:32:27 +0200 Subject: [PATCH 1/4] Implement ttl --- .../AccountRepositoryAdditional.java | 3 +++ .../AccountRepositoryAdditionalImpl.java | 18 ++++++++++-------- .../repositories/PendingAccountRepository.java | 3 +++ .../services/decomposition/AccountCreator.java | 4 ++-- .../account/services/AccountServiceTests.java | 8 ++++---- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditional.java b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditional.java index 3503b42..80a8f85 100644 --- a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditional.java +++ b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditional.java @@ -15,6 +15,7 @@ import biz.nynja.account.models.AuthenticationProvider; import biz.nynja.account.models.ContactInfo; import biz.nynja.account.models.PendingAccountByAuthenticationProvider; import biz.nynja.account.models.Profile; +import biz.nynja.account.models.PendingAccount; @Repository public interface AccountRepositoryAdditional { @@ -44,4 +45,6 @@ public interface AccountRepositoryAdditional { boolean deleteContactInfo(UUID accountId, ContactInfo contactInfo); boolean editContactInfo(UUID accountId, ContactInfo oldContactInfo, ContactInfo editedContactInfo); + + PendingAccount savePendingAccount(PendingAccount updatedPendingAccount); } diff --git a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java index 38f4bff..1cdafe2 100644 --- a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java +++ b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java @@ -97,17 +97,11 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio PendingAccount pendingAccount = pendingAccountRepository .findByAccountId(UUID.fromString(request.getAccountId())); if (pendingAccount == null) { - logger.info("Existing pending account with the provided id was not found."); - logger.debug("Existing pending account with the provided id {} was not found.", request.getAccountId()); + logger.info("Existing pending account with the provided id was not found or creation timeout expired."); + logger.debug("Existing pending account with the provided id {} was not found or creation timeout expired.", request.getAccountId()); return null; } Long timeCreated = Instant.now().toEpochMilli(); - Long checkMinutes = timeCreated - pendingAccount.getCreationTimestamp(); - if (checkMinutes > COMPLETE_PENDING_ACCOUNT_TIMEOUT_IN_MINUTES * 60 * 1000) { - logger.info("Account creation timeout expired."); - pendingAccountRepository.deleteById(UUID.fromString(request.getAccountId())); - return null; - } WriteResult wr = null; try { newAccountInsert(batchOperations, request, pendingAccount, timeCreated); @@ -654,4 +648,12 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio } return false; } + + public PendingAccount savePendingAccount(PendingAccount pendingAccount) { + String cql = "INSERT INTO pendingAccount (accountId, profileId, authenticationProvider, authenticationProviderType, creationTimestamp)" + + " VALUES (" + pendingAccount.getAccountId() + ", " + pendingAccount.getProfileId() + ", '" + pendingAccount.getAuthenticationProvider() + "', '" + pendingAccount.getAuthenticationProviderType() + "'," + pendingAccount.getCreationTimestamp() + ") " + + "USING TTL "+COMPLETE_PENDING_ACCOUNT_TIMEOUT_IN_MINUTES *60 +";"; + cassandraTemplate.getCqlOperations().execute(cql); + return pendingAccount; + } } diff --git a/src/main/java/biz/nynja/account/repositories/PendingAccountRepository.java b/src/main/java/biz/nynja/account/repositories/PendingAccountRepository.java index 4d3c6f1..f7d2c3c 100644 --- a/src/main/java/biz/nynja/account/repositories/PendingAccountRepository.java +++ b/src/main/java/biz/nynja/account/repositories/PendingAccountRepository.java @@ -16,4 +16,7 @@ public interface PendingAccountRepository extends CassandraRepository S save(S s); } diff --git a/src/main/java/biz/nynja/account/services/decomposition/AccountCreator.java b/src/main/java/biz/nynja/account/services/decomposition/AccountCreator.java index 645d3bc..70783c0 100644 --- a/src/main/java/biz/nynja/account/services/decomposition/AccountCreator.java +++ b/src/main/java/biz/nynja/account/services/decomposition/AccountCreator.java @@ -80,7 +80,7 @@ public class AccountCreator { pendingAccount.setProfileId(UUID.randomUUID()); pendingAccount.setCreationTimestamp(Instant.now().toEpochMilli()); - PendingAccount savedPendingAccount = pendingAccountRepository.save(pendingAccount); + PendingAccount savedPendingAccount = accountRepositoryAdditional.savePendingAccount(pendingAccount); logger.debug("Pending account \"{}\" saved into the DB", savedPendingAccount); response = CreatePendingAccountResponse.newBuilder().setPendingAccountDetails(savedPendingAccount.toProto()) .build(); @@ -105,7 +105,7 @@ public class AccountCreator { .setAuthenticationProviderType(foundExistingPendingAccount.getAuthenticationProviderType()); updatedPendingAccount.setCreationTimestamp(Instant.now().toEpochMilli()); - PendingAccount updatePendingAccount = pendingAccountRepository.save(updatedPendingAccount); + PendingAccount updatePendingAccount = accountRepositoryAdditional.savePendingAccount(updatedPendingAccount); return CreatePendingAccountResponse.newBuilder().setPendingAccountDetails(updatePendingAccount.toProto()) .build(); } diff --git a/src/test/java/biz/nynja/account/services/AccountServiceTests.java b/src/test/java/biz/nynja/account/services/AccountServiceTests.java index 8f02eed..c3048e6 100644 --- a/src/test/java/biz/nynja/account/services/AccountServiceTests.java +++ b/src/test/java/biz/nynja/account/services/AccountServiceTests.java @@ -461,7 +461,7 @@ public class AccountServiceTests extends GrpcServerTestBase { AuthenticationProvider.createAuthenticationProviderFromStrings(request.getAuthenticationType().name(), request.getAuthenticationProvider()))) .willReturn(existingPendingAccountByAuthenticationProvider); - given(pendingAccountRepository.save(Mockito.any(PendingAccount.class))).willReturn(existingPendingAccount); + given(accountRepositoryAdditional.savePendingAccount(Mockito.any(PendingAccount.class))).willReturn(existingPendingAccount); final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); final CreatePendingAccountResponse reply = accountServiceBlockingStub.createPendingAccount(request); @@ -500,7 +500,7 @@ public class AccountServiceTests extends GrpcServerTestBase { public void testCreatePendingAccountOK() { final CreatePendingAccountRequest request = CreatePendingAccountRequest.newBuilder() .setAuthenticationType(AuthenticationType.EMAIL).setAuthenticationProvider(Util.EMAIL).build(); - given(pendingAccountRepository.save(Mockito.any(PendingAccount.class))).willReturn(pendingAccount); + given(accountRepositoryAdditional.savePendingAccount(Mockito.any(PendingAccount.class))).willReturn(pendingAccount); final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); final CreatePendingAccountResponse reply = accountServiceBlockingStub.createPendingAccount(request); @@ -514,7 +514,7 @@ public class AccountServiceTests extends GrpcServerTestBase { final CreatePendingAccountRequest request = CreatePendingAccountRequest.newBuilder() .setAuthenticationType(AuthenticationType.EMAIL) .setAuthenticationProvider("invalid.E-mail1.@domain_test.com1").build(); - given(pendingAccountRepository.save(Mockito.any(PendingAccount.class))).willReturn(pendingAccount); + given(accountRepositoryAdditional.savePendingAccount(Mockito.any(PendingAccount.class))).willReturn(pendingAccount); final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); final CreatePendingAccountResponse reply = accountServiceBlockingStub.createPendingAccount(request); @@ -527,7 +527,7 @@ public class AccountServiceTests extends GrpcServerTestBase { public void testCreatePendingAccountInvalidPhone() { final CreatePendingAccountRequest request = CreatePendingAccountRequest.newBuilder() .setAuthenticationType(AuthenticationType.PHONE).setAuthenticationProvider("BG:084365:5555").build(); - given(pendingAccountRepository.save(Mockito.any(PendingAccount.class))).willReturn(pendingAccount); + given(accountRepositoryAdditional.savePendingAccount(Mockito.any(PendingAccount.class))).willReturn(pendingAccount); final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); final CreatePendingAccountResponse reply = accountServiceBlockingStub.createPendingAccount(request); -- GitLab From 1d0c4a7451164e3f4b6edf10f415b3fd48fb984c Mon Sep 17 00:00:00 2001 From: sergeyPensov Date: Tue, 13 Nov 2018 14:45:25 +0200 Subject: [PATCH 2/4] Check cassandra response for creating pending account --- .../AccountRepositoryAdditionalImpl.java | 17 +++++++++++----- .../decomposition/AccountCreator.java | 20 ++++++++++++------- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java index 1cdafe2..7aaf0e2 100644 --- a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java +++ b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java @@ -650,10 +650,17 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio } public PendingAccount savePendingAccount(PendingAccount pendingAccount) { - String cql = "INSERT INTO pendingAccount (accountId, profileId, authenticationProvider, authenticationProviderType, creationTimestamp)" + - " VALUES (" + pendingAccount.getAccountId() + ", " + pendingAccount.getProfileId() + ", '" + pendingAccount.getAuthenticationProvider() + "', '" + pendingAccount.getAuthenticationProviderType() + "'," + pendingAccount.getCreationTimestamp() + ") " + - "USING TTL "+COMPLETE_PENDING_ACCOUNT_TIMEOUT_IN_MINUTES *60 +";"; - cassandraTemplate.getCqlOperations().execute(cql); + //for saving back compatibility. need refactoring in the future + if (!createSavePendingAccount(pendingAccount)) { + throw new RuntimeException("Exception for save save pending account id=" + pendingAccount.getAccountId()); + } return pendingAccount; } -} + + private boolean createSavePendingAccount(PendingAccount pendingAccount) { + String cql = "INSERT INTO pendingAccount (accountId, profileId, authenticationProvider, authenticationProviderType, creationTimestamp)" + + " VALUES (?, ?, ? ,?, ?) " + + "USING TTL " + COMPLETE_PENDING_ACCOUNT_TIMEOUT_IN_MINUTES * 60 + ";"; + return cassandraTemplate.getCqlOperations().execute(cql, pendingAccount.getAccountId(), pendingAccount.getProfileId(), pendingAccount.getAuthenticationProvider(), pendingAccount.getAuthenticationProviderType(), pendingAccount.getCreationTimestamp()); + } +} \ No newline at end of file diff --git a/src/main/java/biz/nynja/account/services/decomposition/AccountCreator.java b/src/main/java/biz/nynja/account/services/decomposition/AccountCreator.java index 70783c0..19171e4 100644 --- a/src/main/java/biz/nynja/account/services/decomposition/AccountCreator.java +++ b/src/main/java/biz/nynja/account/services/decomposition/AccountCreator.java @@ -80,13 +80,19 @@ public class AccountCreator { pendingAccount.setProfileId(UUID.randomUUID()); pendingAccount.setCreationTimestamp(Instant.now().toEpochMilli()); - PendingAccount savedPendingAccount = accountRepositoryAdditional.savePendingAccount(pendingAccount); - logger.debug("Pending account \"{}\" saved into the DB", savedPendingAccount); - response = CreatePendingAccountResponse.newBuilder().setPendingAccountDetails(savedPendingAccount.toProto()) - .build(); - logger.info("Pending account created successfully."); - logger.debug("Pending account: \"{}\" created successfully.", response); - return response; + try{ + PendingAccount savedPendingAccount = accountRepositoryAdditional.savePendingAccount(pendingAccount); + logger.debug("Pending account \"{}\" saved into the DB", savedPendingAccount); + response = CreatePendingAccountResponse.newBuilder().setPendingAccountDetails(savedPendingAccount.toProto()) + .build(); + logger.info("Pending account created successfully."); + logger.debug("Pending account: \"{}\" created successfully.", response); + return response; + }catch (Exception e){ + logger.error(e.getMessage()); + return CreatePendingAccountResponse.newBuilder().setError(ErrorResponse.newBuilder().setCause(Cause.ERROR_CREATING_ACCOUNT)) + .build(); + } } private CreatePendingAccountResponse updateAndValidatePendingAccountCreation(CreatePendingAccountRequest request) { -- GitLab From 7433b1f452506397efe1e244606225a2a2c4b4ae Mon Sep 17 00:00:00 2001 From: sergeyPensov Date: Tue, 13 Nov 2018 15:11:03 +0200 Subject: [PATCH 3/4] Add end of file --- .../account/repositories/AccountRepositoryAdditionalImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java index 7aaf0e2..2497cbb 100644 --- a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java +++ b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java @@ -663,4 +663,4 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio "USING TTL " + COMPLETE_PENDING_ACCOUNT_TIMEOUT_IN_MINUTES * 60 + ";"; return cassandraTemplate.getCqlOperations().execute(cql, pendingAccount.getAccountId(), pendingAccount.getProfileId(), pendingAccount.getAuthenticationProvider(), pendingAccount.getAuthenticationProviderType(), pendingAccount.getCreationTimestamp()); } -} \ No newline at end of file +} -- GitLab From d0cf0837b39aed75546b2f2c8c74f7bd1fd5b140 Mon Sep 17 00:00:00 2001 From: sergeyPensov Date: Tue, 13 Nov 2018 15:12:39 +0200 Subject: [PATCH 4/4] Change server error --- .../nynja/account/services/decomposition/AccountCreator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/biz/nynja/account/services/decomposition/AccountCreator.java b/src/main/java/biz/nynja/account/services/decomposition/AccountCreator.java index 19171e4..227c4ce 100644 --- a/src/main/java/biz/nynja/account/services/decomposition/AccountCreator.java +++ b/src/main/java/biz/nynja/account/services/decomposition/AccountCreator.java @@ -90,7 +90,7 @@ public class AccountCreator { return response; }catch (Exception e){ logger.error(e.getMessage()); - return CreatePendingAccountResponse.newBuilder().setError(ErrorResponse.newBuilder().setCause(Cause.ERROR_CREATING_ACCOUNT)) + return CreatePendingAccountResponse.newBuilder().setError(ErrorResponse.newBuilder().setCause(Cause.INTERNAL_SERVER_ERROR)) .build(); } } -- GitLab