Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RW-13720][risk=no] Cleaned up credit exhaustion cron language #8994

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
10 changes: 5 additions & 5 deletions api/config/config_local.json
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicating "freecredit" and iinitialcredits" to ensure deploy safety. Plan to delete freetier later.

Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@
"accountId": "013713-75CFF6-1751E5",
"projectNamePrefix": "aou-rw-local1-",
"exportBigQueryTable": "all-of-us-workbench-test-bd.billing_data.rw_test_firecloud_view",
"defaultFreeCreditsDollarLimit": 300.0,
"freeTierCostAlertThresholds": [
"defaultInitialCreditDollarLimit": 300.0,
"initialCreditCostAlertThresholds": [
0.5,
0.75
],
"initialCreditsValidityPeriodDays": 120,
"initialCreditsExtensionPeriodDays": 365,
"initialCreditsExpirationWarningDays": 14,
"carahsoftEmail": "workbench-notificatio-aaaaekogch7vg3c6hi4sgyx4le@pmi-engteam.slack.com",
"freeTierCronUserBatchSize": 100,
"minutesBeforeLastFreeTierJob": 60,
"numberOfDaysToConsiderForFreeTierUsageUpdate": 2
"initialCreditCronUserBatchSize": 100,
"minutesBeforeLastInitialCreditJob": 60,
"numberOfDaysToConsiderForInitialCreditUsageUpdate": 2
},
"auth": {
"serviceAccountApiUsers": [
Expand Down
10 changes: 5 additions & 5 deletions api/config/config_preprod.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@
"accountId": "01DDC8-7ED304-6E46FE",
"projectNamePrefix": "aou-rw-preprod-",
"exportBigQueryTable": "all-of-us-rw-prod-bdata.RwProdBillingDataExport.rw_firecloud_view",
"defaultFreeCreditsDollarLimit": 300.0,
"freeTierCostAlertThresholds": [
"defaultInitialCreditDollarLimit": 300.0,
"initialCreditCostAlertThresholds": [
0.5,
0.75
],
"initialCreditsValidityPeriodDays": 120,
"initialCreditsExtensionPeriodDays": 365,
"initialCreditsExpirationWarningDays": 14,
"carahsoftEmail": "[email protected]",
"freeTierCronUserBatchSize": 100,
"minutesBeforeLastFreeTierJob": 60,
"numberOfDaysToConsiderForFreeTierUsageUpdate": 2
"initialCreditCronUserBatchSize": 100,
"minutesBeforeLastInitialCreditJob": 60,
"numberOfDaysToConsiderForInitialCreditUsageUpdate": 2
},
"auth": {
"serviceAccountApiUsers": [
Expand Down
12 changes: 8 additions & 4 deletions api/config/config_prod.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,22 @@
"accountId": "01DDC8-7ED304-6E46FE",
"projectNamePrefix": "aou-rw-",
"exportBigQueryTable": "all-of-us-rw-prod-bdata.RwProdBillingDataExport.rw_firecloud_view",
"defaultFreeCreditsDollarLimit": 300.0,
"defaultInitialCreditDollarLimit": 300.0,
"freeTierCostAlertThresholds": [
0.5,
0.75
],
"initialCreditCostAlertThresholds": [
0.5,
0.75
],
"initialCreditsValidityPeriodDays": 120,
"initialCreditsExtensionPeriodDays": 365,
"initialCreditsExpirationWarningDays": 14,
"carahsoftEmail": "[email protected]",
"freeTierCronUserBatchSize": 200,
"minutesBeforeLastFreeTierJob": 60,
"numberOfDaysToConsiderForFreeTierUsageUpdate": 7
"initialCreditCronUserBatchSize": 200,
"minutesBeforeLastInitialCreditJob": 60,
"numberOfDaysToConsiderForInitialCreditUsageUpdate": 7
},
"auth": {
"serviceAccountApiUsers": [
Expand Down
10 changes: 5 additions & 5 deletions api/config/config_stable.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@
"accountId": "01DDC8-7ED304-6E46FE",
"projectNamePrefix": "aou-rw-stable-",
"exportBigQueryTable": "all-of-us-rw-prod-bdata.RwProdBillingDataExport.rw_firecloud_view",
"defaultFreeCreditsDollarLimit": 300.0,
"freeTierCostAlertThresholds": [
"defaultInitialCreditDollarLimit": 300.0,
"initialCreditCostAlertThresholds": [
0.5,
0.75
],
"initialCreditsValidityPeriodDays": 120,
"initialCreditsExtensionPeriodDays": 365,
"initialCreditsExpirationWarningDays": 14,
"carahsoftEmail": "workbench-notificatio-aaaaekogch7vg3c6hi4sgyx4le@pmi-engteam.slack.com",
"freeTierCronUserBatchSize": 100,
"minutesBeforeLastFreeTierJob": 60,
"numberOfDaysToConsiderForFreeTierUsageUpdate": 2
"initialCreditCronUserBatchSize": 100,
"minutesBeforeLastInitialCreditJob": 60,
"numberOfDaysToConsiderForInitialCreditUsageUpdate": 2
},
"auth": {
"serviceAccountApiUsers": [
Expand Down
10 changes: 5 additions & 5 deletions api/config/config_staging.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@
"accountId": "01DDC8-7ED304-6E46FE",
"projectNamePrefix": "aou-rw-staging-",
"exportBigQueryTable": "all-of-us-rw-prod-bdata.RwProdBillingDataExport.rw_firecloud_view",
"defaultFreeCreditsDollarLimit": 300.0,
"freeTierCostAlertThresholds": [
"defaultInitialCreditDollarLimit": 300.0,
"initialCreditCostAlertThresholds": [
0.5,
0.75
],
"initialCreditsValidityPeriodDays": 120,
"initialCreditsExtensionPeriodDays": 365,
"initialCreditsExpirationWarningDays": 14,
"carahsoftEmail": "workbench-notificatio-aaaaekogch7vg3c6hi4sgyx4le@pmi-engteam.slack.com",
"freeTierCronUserBatchSize": 100,
"minutesBeforeLastFreeTierJob": 60,
"numberOfDaysToConsiderForFreeTierUsageUpdate": 2
"initialCreditCronUserBatchSize": 100,
"minutesBeforeLastInitialCreditJob": 60,
"numberOfDaysToConsiderForInitialCreditUsageUpdate": 2
},
"auth": {
"serviceAccountApiUsers": [
Expand Down
10 changes: 5 additions & 5 deletions api/config/config_test.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@
"accountId": "013713-75CFF6-1751E5",
"projectNamePrefix": "aou-rw-test-",
"exportBigQueryTable": "all-of-us-workbench-test-bd.billing_data.rw_test_firecloud_view",
"defaultFreeCreditsDollarLimit": 300.0,
"freeTierCostAlertThresholds": [
"defaultInitialCreditDollarLimit": 300.0,
"initialCreditCostAlertThresholds": [
0.5,
0.75
],
"initialCreditsValidityPeriodDays": 120,
"initialCreditsExtensionPeriodDays": 365,
"initialCreditsExpirationWarningDays": 14,
"carahsoftEmail": "workbench-notificatio-aaaaekogch7vg3c6hi4sgyx4le@pmi-engteam.slack.com",
"freeTierCronUserBatchSize": 5,
"minutesBeforeLastFreeTierJob": 60,
"numberOfDaysToConsiderForFreeTierUsageUpdate": 2
"initialCreditCronUserBatchSize": 5,
"minutesBeforeLastInitialCreditJob": 60,
"numberOfDaysToConsiderForInitialCreditUsageUpdate": 2
},
"auth": {
"serviceAccountApiUsers": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CloudTaskInitialCreditsExpiryController
implements CloudTaskInitialCreditExpiryApiDelegate {
public class CloudTaskInitialCreditsExhaustionController
implements CloudTaskInitialCreditExhaustionApiDelegate {

private static final Logger logger =
LoggerFactory.getLogger(CloudTaskInitialCreditsExpiryController.class);
LoggerFactory.getLogger(CloudTaskInitialCreditsExhaustionController.class);

private final WorkspaceDao workspaceDao;
private final WorkspaceService workspaceService;
Expand All @@ -46,7 +46,7 @@ public class CloudTaskInitialCreditsExpiryController
private final LeonardoApiClient leonardoApiClient;
private final MailService mailService;

CloudTaskInitialCreditsExpiryController(
CloudTaskInitialCreditsExhaustionController(
WorkspaceDao workspaceDao,
WorkspaceService workspaceService,
UserDao userDao,
Expand Down Expand Up @@ -120,7 +120,7 @@ private void alertUsersBasedOnTheThreshold(
Map<Long, Double> liveCostByCreator,
Set<DbUser> newlyExpiredUsers) {
final List<Double> costThresholdsInDescOrder =
workbenchConfig.get().billing.freeTierCostAlertThresholds;
workbenchConfig.get().billing.initialCreditCostAlertThresholds;
costThresholdsInDescOrder.sort(Comparator.reverseOrder());

Map<Long, DbUser> usersCache =
Expand Down Expand Up @@ -182,7 +182,7 @@ private Set<DbUser> getNewlyExpiredUsers(
e.getValue(),
Math.max(
dbCostByCreator.get(e.getKey()), liveCostByCreator.get(e.getKey())),
workbenchConfig.get().billing.defaultFreeCreditsDollarLimit))
workbenchConfig.get().billing.defaultInitialCreditDollarLimit))
.map(Map.Entry::getValue)
.collect(Collectors.toSet());

Expand Down Expand Up @@ -270,7 +270,7 @@ private void maybeAlertOnCostThresholds(

final double limit =
getUserFreeTierDollarLimit(
user, workbenchConfig.get().billing.defaultFreeCreditsDollarLimit);
user, workbenchConfig.get().billing.defaultInitialCreditDollarLimit);
final double remainingBalance = limit - currentCost;

// this shouldn't happen, but it did (RW-4678)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,21 @@ public class CloudTaskUserController implements CloudTaskUserApiDelegate {
private final CloudResourceManagerService cloudResourceManagerService;
private final UserService userService;
private final AccessModuleService accessModuleService;
private final InitialCreditsBatchUpdateService freeTierBillingUpdateService;
private final InitialCreditsBatchUpdateService initialCreditsBatchUpdateService;
private final InitialCreditsService initialCreditsService;

CloudTaskUserController(
UserDao userDao,
CloudResourceManagerService cloudResourceManagerService,
UserService userService,
AccessModuleService accessModuleService,
InitialCreditsBatchUpdateService freeTierBillingUpdateService,
InitialCreditsBatchUpdateService initialCreditsBatchUpdateService,
InitialCreditsService initialCreditsService) {
this.userDao = userDao;
this.cloudResourceManagerService = cloudResourceManagerService;
this.userService = userService;
this.accessModuleService = accessModuleService;
this.freeTierBillingUpdateService = freeTierBillingUpdateService;
this.initialCreditsBatchUpdateService = initialCreditsBatchUpdateService;
this.initialCreditsService = initialCreditsService;
}

Expand Down Expand Up @@ -121,21 +121,6 @@ public ResponseEntity<Void> auditProjectAccess(AuditProjectAccessRequest request
return ResponseEntity.noContent().build();
}

/**
* Takes in batch of user Ids check whether users have incurred sufficient cost in their
* workspaces to trigger alerts due to passing thresholds or exceeding limits
*
* @param body : Batch of user IDs from cloud task queue: freeTierBillingQueue
* @return
*/
@Override
public ResponseEntity<Void> checkAndAlertFreeTierBillingUsage(List<Long> userIdsList) {
if (userIdsList != null && userIdsList.size() > 0) {
freeTierBillingUpdateService.checkAndAlertFreeTierBillingUsage(userIdsList);
}
return ResponseEntity.noContent().build();
}

@Override
public ResponseEntity<Void> synchronizeUserAccess(SynchronizeUserAccessRequest request) {
int errorCount = 0;
Expand Down Expand Up @@ -177,6 +162,21 @@ public ResponseEntity<Void> synchronizeUserAccess(SynchronizeUserAccessRequest r
return ResponseEntity.noContent().build();
}

/**
* Takes in batch of user Ids check whether users have incurred sufficient cost in their
* workspaces to trigger alerts due to passing thresholds or exceeding limits
*
* @param body : Batch of user IDs from cloud task queue: freeTierBillingQueue
* @return
*/
@Override
public ResponseEntity<Void> checkCreditsExhaustionForUserIDs(List<Long> userIdsList) {
if (userIdsList != null && userIdsList.size() > 0) {
initialCreditsBatchUpdateService.checkCreditsExhaustionForUserIDs(userIdsList);
}
return ResponseEntity.noContent().build();
}

/**
* Takes in batch of user Ids check whether users have initial credits that have expired
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,32 @@
@RestController
public class OfflineBillingController implements OfflineBillingApiDelegate {

private final InitialCreditsBatchUpdateService freeTierBillingService;
private final InitialCreditsBatchUpdateService initialCreditsBatchUpdateService;
private final GoogleProjectPerCostDao googleProjectPerCostDao;
private final TaskQueueService taskQueueService;

private final UserService userService;

@Autowired
OfflineBillingController(
InitialCreditsBatchUpdateService freeTierBillingService,
InitialCreditsBatchUpdateService initialCreditsBatchUpdateService,
GoogleProjectPerCostDao googleProjectPerCostDao,
UserService userService,
TaskQueueService taskQueueService) {
this.freeTierBillingService = freeTierBillingService;
this.initialCreditsBatchUpdateService = initialCreditsBatchUpdateService;
this.taskQueueService = taskQueueService;
this.userService = userService;
this.googleProjectPerCostDao = googleProjectPerCostDao;
}

@Override
public ResponseEntity<Void> checkFreeTierBillingUsage() {
public ResponseEntity<Void> checkForInitialCreditUsage() {
// Get cost for all workspace from BQ
Map<String, Double> freeTierForAllWorkspace =
freeTierBillingService.getFreeTierWorkspaceCostsFromBQ();
Map<String, Double> initialCreditCostsForAllWorkspace =
initialCreditsBatchUpdateService.getInitialCreditWorkspaceCostsFromBQ();

List<DbGoogleProjectPerCost> googleProjectCostList =
freeTierForAllWorkspace.entrySet().stream()
initialCreditCostsForAllWorkspace.entrySet().stream()
.map(DbGoogleProjectPerCost::new)
.collect(Collectors.toList());

Expand All @@ -51,7 +51,7 @@ public ResponseEntity<Void> checkFreeTierBillingUsage() {

List<Long> allUserIds = userService.getAllUserIds();

taskQueueService.groupAndPushFreeTierBilling(allUserIds);
taskQueueService.groupAndPushCheckInitialCreditExhaustionTasks(allUserIds);
log.info("Pushed all users to Cloud Task for Free Tier Billing");

return ResponseEntity.noContent().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ public class TaskQueueService {
BASE_PATH + "/deleteTestUserWorkspacesInRawls";
private static final String CHECK_CREDITS_EXPIRATION_FOR_USER_IDS_PATH =
BASE_PATH + "/checkCreditsExpirationForUserIDs";
private static final String CHECK_AND_ALERT_FREE_TIER_USAGE =
BASE_PATH + "/checkAndAlertFreeTierBillingUsage";
private static final String CHECK_CREDITS_EXHAUSTION_FOR_USER_IDS_PATH =
BASE_PATH + "/checkCreditsExhaustionForUserIDs";

private static final String INITIAL_CREDITS_EXPIRY_PATH =
private static final String EXHAUSTED_INITIAL_CREDITS_PATH =
BASE_PATH + "/handleInitialCreditsExpiry";
private static final String AUDIT_PROJECTS_QUEUE_NAME = "auditProjectQueue";
private static final String SYNCHRONIZE_ACCESS_QUEUE_NAME = "synchronizeAccessQueue";
Expand All @@ -59,8 +59,9 @@ public class TaskQueueService {
private static final String DELETE_TEST_WORKSPACES_QUEUE_NAME = "deleteTestUserWorkspacesQueue";
private static final String DELETE_RAWLS_TEST_WORKSPACES_QUEUE_NAME =
"deleteTestUserRawlsWorkspacesQueue";
private static final String FREE_TIER_BILLING_QUEUE = "freeTierBillingQueue";
private static final String EXPIRED_FREE_CREDITS_QUEUE_NAME = "expiredFreeCreditsQueue";
private static final String CHECK_CREDITS_EXHAUSTION_FOR_USER_IDS_QUEUE_NAME =
"freeTierBillingQueue";
private static final String EXHAUSTED_INITIAL_CREDITS_QUEUE_NAME = "expiredFreeCreditsQueue";
private static final String CHECK_CREDITS_EXPIRATION_FOR_USER_IDS_QUEUE_NAME =
"checkCreditsExpirationForUserIDsQueue";

Expand Down Expand Up @@ -122,15 +123,6 @@ public void groupAndPushAuditProjectsTasks(List<Long> userIds) {
new AuditProjectAccessRequest().userIds(batch)));
}

public void groupAndPushFreeTierBilling(List<Long> userIds) {
Integer freeTierCronUserBatchSize =
workbenchConfigProvider.get().billing.freeTierCronUserBatchSize;
CloudTasksUtils.partitionList(userIds, freeTierCronUserBatchSize)
.forEach(
batch ->
createAndPushTask(FREE_TIER_BILLING_QUEUE, CHECK_AND_ALERT_FREE_TIER_USAGE, batch));
}

public List<String> groupAndPushSynchronizeAccessTasks(List<Long> userIds) {
WorkbenchConfig workbenchConfig = workbenchConfigProvider.get();
return CloudTasksUtils.partitionList(
Expand Down Expand Up @@ -210,17 +202,29 @@ public void pushDuplicateWorkspaceTask(
"Authorization", "Bearer " + userAuthenticationProvider.get().getCredentials()));
}

public void pushInitialCreditsExpiryTask(
public void pushCheckInitialCreditExhaustionTask(
List<Long> users, Map<Long, Double> dbCostByCreator, Map<Long, Double> liveCostByCreator) {
createAndPushTask(
EXPIRED_FREE_CREDITS_QUEUE_NAME,
INITIAL_CREDITS_EXPIRY_PATH,
EXHAUSTED_INITIAL_CREDITS_QUEUE_NAME,
EXHAUSTED_INITIAL_CREDITS_PATH,
new ExpiredInitialCreditsEventRequest()
.users(users)
.dbCostByCreator(dbCostByCreator)
.liveCostByCreator(liveCostByCreator));
}

public void groupAndPushCheckInitialCreditExhaustionTasks(List<Long> userIds) {
Integer initialCreditCronUserBatchSize =
workbenchConfigProvider.get().billing.initialCreditCronUserBatchSize;
CloudTasksUtils.partitionList(userIds, initialCreditCronUserBatchSize)
.forEach(
batch ->
createAndPushTask(
CHECK_CREDITS_EXHAUSTION_FOR_USER_IDS_QUEUE_NAME,
CHECK_CREDITS_EXHAUSTION_FOR_USER_IDS_PATH,
batch));
}

public void groupAndPushCheckInitialCreditExpirationTasks(List<Long> userIds) {
WorkbenchConfig workbenchConfig = workbenchConfigProvider.get();
CloudTasksUtils.partitionList(
Expand Down
Loading