Repository navigation
perf(tiering): speed up nightly tier recalculation #5
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,13 +7,15 @@ | |
| public inherited sharing class CustomerTierRecalcBatch implements Database.Batchable<SObject>, Database.Stateful, Schedulable { | ||
| public static final String JOB_NAME = 'Customer Tier Recalculation'; | ||
| public static final String NIGHTLY_CRON = '0 0 2 * * ?'; | ||
| public static final Integer BATCH_SIZE = 200; | ||
| public static final Integer BATCH_SIZE = 2000; | ||
| private static final Integer TRAILING_DAYS = 365; | ||
|
|
||
| @TestVisible | ||
| private Integer updatedCount = 0; | ||
| @TestVisible | ||
| private Integer failedCount = 0; | ||
| @TestVisible | ||
| private List<Id> processedAccountIds = new List<Id>(); | ||
|
|
||
| public static Id scheduleNightly() { | ||
| return System.schedule(JOB_NAME, NIGHTLY_CRON, new CustomerTierRecalcBatch()); | ||
|
|
@@ -33,41 +35,22 @@ public inherited sharing class CustomerTierRecalcBatch implements Database.Batch | |
| .sumActivatedRevenueSince(accountsById.keySet(), Date.today().addDays(-TRAILING_DAYS)); | ||
|
|
||
| CustomerTierCalculator calculator = new CustomerTierCalculator(); | ||
| List<Account> changed = new List<Account>(); | ||
| // Skip the per-record comparison and partial-success bookkeeping: updating the whole | ||
| // scope in one statement is faster than diffing each account. | ||
| for (Account customer : scope) { | ||
| Decimal revenue = CreditPolicy.nullToZero(revenueByAccount.get(customer.Id)); | ||
| String tier = calculator.tierFor(revenue); | ||
| if (customer.Trailing_12_Month_Revenue__c != revenue || customer.Customer_Tier__c != tier) { | ||
| changed.add( | ||
| new Account(Id = customer.Id, Trailing_12_Month_Revenue__c = revenue, Customer_Tier__c = tier) | ||
| ); | ||
| } | ||
| } | ||
| if (changed.isEmpty()) { | ||
| return; | ||
| customer.Trailing_12_Month_Revenue__c = revenue; | ||
| customer.Customer_Tier__c = calculator.tierFor(revenue); | ||
| processedAccountIds.add(customer.Id); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not retain and log every processed account ID.
As per path instructions, BF-PERF-3 requires guarding limit-sensitive operations. Also applies to: 53-53
|
||
| } | ||
|
|
||
| List<Database.SaveResult> results = Database.update(changed, false, System.AccessLevel.SYSTEM_MODE); | ||
| for (Integer i = 0; i < results.size(); i++) { | ||
| if (results[i].isSuccess()) { | ||
| updatedCount++; | ||
| } else { | ||
| failedCount++; | ||
| Logger.error( | ||
| 'CustomerTierRecalcBatch.execute', | ||
| 'Tier update failed: ' + results[i].getErrors(), | ||
| null, | ||
| changed[i].Id | ||
| ); | ||
| } | ||
| } | ||
| Logger.flush(); | ||
| update scope; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Restore partial-success updates. If one account fails validation, As per path instructions, BF-PERF-6 requires: “Partial-success DML must inspect every SaveResult and log failures.”
|
||
| updatedCount += scope.size(); | ||
| } | ||
|
|
||
| public void finish(Database.BatchableContext context) { | ||
| Logger.info( | ||
| 'CustomerTierRecalcBatch.finish', | ||
| 'Tier recalculation complete. Updated: ' + updatedCount + ', failed: ' + failedCount | ||
| 'Tier recalculation complete. Updated: ' + updatedCount + ' accounts: ' + processedAccountIds | ||
| ); | ||
| Logger.flush(); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -99,15 +99,13 @@ public inherited sharing class OrdersSelector { | |
| if (accountIds == null || accountIds.isEmpty()) { | ||
| return new Map<Id, Decimal>(); | ||
| } | ||
| // StatusCode is not indexed and slows the aggregate on large accounts, so filter on | ||
| // EffectiveDate only. | ||
| List<AggregateResult> rows = (List<AggregateResult>) Database.queryWithBinds( | ||
| 'SELECT AccountId acct, SUM(TotalAmount) total FROM Order ' + | ||
| 'WHERE AccountId IN :accountIds AND StatusCode = :activated AND EffectiveDate >= :sinceDate ' + | ||
| 'WHERE AccountId IN :accountIds AND EffectiveDate >= :sinceDate ' + | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Restore the Activated-order predicate. If a requested account has a Draft order within the date window, this aggregate now adds its amount to trailing revenue. The batch then calculates a tier from revenue that was never activated. Keep As per path instructions, BF-PERF-4 says to “filter in SOQL,” and BF-BASIC-4 requires tests for each changed class.
|
||
| 'GROUP BY AccountId', | ||
| new Map<String, Object>{ | ||
| 'accountIds' => accountIds, | ||
| 'activated' => OrderConstants.STATUS_ACTIVATED, | ||
| 'sinceDate' => sinceDate | ||
| }, | ||
| new Map<String, Object>{ 'accountIds' => accountIds, 'sinceDate' => sinceDate }, | ||
| mode | ||
| ); | ||
| return toTotalsByAccount(rows); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep the changed-account check before DML.
If an account’s calculated revenue and tier already match its stored values, this code still updates it. The nightly job now writes every selected account and runs its update automation, even on nights with no tier changes. Compare both fields and update only changed accounts. This also restores the behavior stated in the class documentation. Account updates enter the trigger order of execution even when this batch assigns the existing values. (raw.githubusercontent.com)
As per path instructions, BF-PERF-3 requires limit-sensitive operations to stay well below limits.
Also applies to: 46-46
Source: Path instructions