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 14 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 All @@ -63,7 +63,7 @@ public class CloudTaskInitialCreditsExpiryController

@SuppressWarnings("unchecked")
@Override
public ResponseEntity<Void> handleInitialCreditsExpiryBatch(
public ResponseEntity<Void> handleInitialCreditsExhaustionBatch(
ExpiredInitialCreditsEventRequest request) {

if (request.getUsers().isEmpty()) {
Expand Down Expand Up @@ -121,7 +121,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 @@ -183,7 +183,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 @@ -271,7 +271,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 @@ -57,21 +57,21 @@ public class CloudTaskUserController implements CloudTaskUserApiDelegate {

private final AccessModuleService accessModuleService;
private final CloudResourceManagerService cloudResourceManagerService;
private final InitialCreditsBatchUpdateService freeTierBillingUpdateService;
private final InitialCreditsBatchUpdateService initialCreditsBatchUpdateService;
private final InitialCreditsService initialCreditsService;
private final Provider<Stopwatch> stopwatchProvider;
private final UserService userService;

CloudTaskUserController(
AccessModuleService accessModuleService,
CloudResourceManagerService cloudResourceManagerService,
InitialCreditsBatchUpdateService freeTierBillingUpdateService,
InitialCreditsBatchUpdateService initialCreditsBatchUpdateService,
InitialCreditsService initialCreditsService,
Provider<Stopwatch> stopwatchProvider,
UserService userService) {
this.accessModuleService = accessModuleService;
this.cloudResourceManagerService = cloudResourceManagerService;
this.freeTierBillingUpdateService = freeTierBillingUpdateService;
this.initialCreditsBatchUpdateService = initialCreditsBatchUpdateService;
this.initialCreditsService = initialCreditsService;
this.stopwatchProvider = stopwatchProvider;
this.userService = userService;
Expand Down Expand Up @@ -127,25 +127,6 @@ private int auditOneUser(DbUser user) {
return 0; // no error
}

/**
* 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 userIds : Batch of user IDs from cloud task queue: freeTierBillingQueue
* @return
*/
@Override
public ResponseEntity<Void> checkAndAlertFreeTierBillingUsageBatch(List<Long> userIds) {
if (userIds == null || userIds.isEmpty()) {
return ResponseEntity.noContent().build();
}

return processUserIdBatch(
userIds,
"alerting for initial credits usage",
freeTierBillingUpdateService::checkAndAlertFreeTierBillingUsage);
}

@Override
public ResponseEntity<Void> synchronizeUserAccessBatch(List<Long> userIds) {
int errorCount = processDbUserBatch(userIds, "synchronizing access", this::syncOneUser);
Expand Down Expand Up @@ -217,6 +198,21 @@ public ResponseEntity<Void> sendAccessExpirationEmailsBatch(List<Long> userIds)
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> checkCreditsExhaustionForUserIDsBatch(List<Long> userIdsList) {
if (userIdsList != null && userIdsList.size() > 0) {
initialCreditsBatchUpdateService.checkCreditsExhaustionForUserIDs(userIdsList);
}
return ResponseEntity.noContent().build();
}

private ResponseEntity<Void> processUserIdBatch(
List<Long> userIds, String description, Consumer<List<Long>> fn) {
processUserBatch(
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
Loading