Skip to content

Commit 3e6df21

Browse files
siddhijainCopilot
andauthored
Fixes AB#3590267 Extend filter-then-clone optimization to deleteAccessTokensWithInters… (#3114)
Fixes [AB#3590267](https://identitydivision.visualstudio.com/fac9d424-53d2-45c0-91b5-ef6ba7a6bf26/_workitems/edit/3590267) This PR extends use-filter-then-clone flight to one more method and add some telemetry attributes to check if the latency of ATS can be correlated to the number of accounts and credentials in memory. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: siddhijain <30181294+siddhijain@users.noreply.github.com>
1 parent 26de108 commit 3e6df21

5 files changed

Lines changed: 55 additions & 20 deletions

File tree

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
vNext
22
----------
3+
- [PATCH] Extend filter-then-clone optimization to deleteAccessTokensWithIntersectingScopes and add telemetry attributes (#3114)
34
- [PATCH] Wire ClientDataInfo through AcquireTokenResult, exceptions (#3109)
45
- [PATCH] Handle app_link Intent redirection by validating broker install links and rejecting unsupported redirect URIs with appropriate error responses (#3102)
56
- [MINOR] Add onboarding telemetry blob fields to BrokerRequest/BrokerResult and command parameters for client↔broker IPC transport (#3111)

common4j/src/main/com/microsoft/identity/common/java/cache/BrokerOAuth2TokenCache.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1648,7 +1648,6 @@ public static IAccountCredentialCache getCacheToBeUsed(@NonNull final IPlatformC
16481648
final boolean isFlightEnabled = CommonFlightsManager.INSTANCE
16491649
.getFlightsProvider()
16501650
.isFlightEnabled(CommonFlight.USE_IN_MEMORY_CACHE_FOR_ACCOUNTS_AND_CREDENTIALS);
1651-
SpanExtension.current().setAttribute(AttributeName.in_memory_cache_used_for_accounts_and_credentials.name(), isFlightEnabled);
16521651
if (isFlightEnabled) {
16531652
return inMemoryCacheMapByStorage.computeIfAbsent(storeName, s ->
16541653
new SharedPreferencesAccountCredentialCacheWithMemoryCache(

common4j/src/main/com/microsoft/identity/common/java/cache/MsalOAuth2TokenCache.java

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1803,20 +1803,44 @@ private void deleteAccessTokensWithIntersectingScopes(
18031803
final String methodName = "deleteAccessTokensWithIntersectingScopes";
18041804
final long startTimeNanos = System.nanoTime();
18051805

1806-
final List<Credential> accessTokens = mAccountCredentialCache.getCredentialsFilteredBy(
1807-
referenceToken.getHomeAccountId(),
1808-
referenceToken.getEnvironment(),
1809-
CredentialType.fromString(referenceToken.getCredentialType()),
1810-
referenceToken.getClientId(),
1811-
referenceToken.getApplicationIdentifier(),
1812-
referenceToken.getMamEnrollmentIdentifier(),
1813-
referenceToken.getRealm(),
1814-
null, // Wildcard (*)
1815-
referenceToken.getAccessTokenType(),
1816-
referenceToken.getRequestedClaims(),
1817-
mustMatchExactClaims,
1818-
mAccountCredentialCache.getCredentials()
1819-
);
1806+
final boolean useFilterThenClone = CommonFlightsManager.INSTANCE
1807+
.getFlightsProvider()
1808+
.isFlightEnabled(CommonFlight.ENABLE_FILTER_THEN_CLONE_IN_MEMORY_CACHE);
1809+
1810+
final List<Credential> accessTokens;
1811+
1812+
if (useFilterThenClone) {
1813+
// Filter-then-clone: use the direct overload that reads from cache
1814+
// and clones only matching credentials.
1815+
accessTokens = mAccountCredentialCache.getCredentialsFilteredBy(
1816+
referenceToken.getHomeAccountId(),
1817+
referenceToken.getEnvironment(),
1818+
CredentialType.fromString(referenceToken.getCredentialType()),
1819+
referenceToken.getClientId(),
1820+
referenceToken.getApplicationIdentifier(),
1821+
referenceToken.getMamEnrollmentIdentifier(),
1822+
referenceToken.getRealm(),
1823+
null, // Wildcard (*)
1824+
referenceToken.getAccessTokenType(),
1825+
referenceToken.getRequestedClaims()
1826+
);
1827+
} else {
1828+
// Legacy path: clone all credentials, then filter.
1829+
accessTokens = mAccountCredentialCache.getCredentialsFilteredBy(
1830+
referenceToken.getHomeAccountId(),
1831+
referenceToken.getEnvironment(),
1832+
CredentialType.fromString(referenceToken.getCredentialType()),
1833+
referenceToken.getClientId(),
1834+
referenceToken.getApplicationIdentifier(),
1835+
referenceToken.getMamEnrollmentIdentifier(),
1836+
referenceToken.getRealm(),
1837+
null, // Wildcard (*)
1838+
referenceToken.getAccessTokenType(),
1839+
referenceToken.getRequestedClaims(),
1840+
mustMatchExactClaims,
1841+
mAccountCredentialCache.getCredentials()
1842+
);
1843+
}
18201844

18211845
Logger.verbose(
18221846
TAG + ":" + methodName,

common4j/src/main/com/microsoft/identity/common/java/cache/SharedPreferencesAccountCredentialCacheWithMemoryCache.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,12 @@ public List<AccountRecord> getAccounts() {
301301

302302
synchronized (mCacheLock) {
303303
waitForInitialLoad();
304+
SpanExtension.current().setAttribute(
305+
AttributeName.number_of_accounts_in_cache.name(),
306+
mCachedAccountRecordsWithKeys.size());
307+
SpanExtension.current().setAttribute(
308+
AttributeName.number_of_credentials_in_cache.name(),
309+
mCachedCredentialsWithKeys.size());
304310
final List<AccountRecord> accounts = cloneItems(mCachedAccountRecordsWithKeys.values(), methodTag);
305311
Logger.info(methodTag, "Found [" + accounts.size() + "] Accounts...");
306312
return accounts;

common4j/src/main/com/microsoft/identity/common/java/opentelemetry/AttributeName.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,16 @@ public enum AttributeName {
265265
*/
266266
elapsed_time_cache_get_all_client_ids,
267267

268+
/**
269+
* The total number of account records in the in-memory cache at the time of the request.
270+
*/
271+
number_of_accounts_in_cache,
272+
273+
/**
274+
* The total number of credential records in the in-memory cache at the time of the request.
275+
*/
276+
number_of_credentials_in_cache,
277+
268278
/**
269279
* The time (in milliseconds) spent on network when acquiring PRT.
270280
*/
@@ -509,11 +519,6 @@ public enum AttributeName {
509519
*/
510520
is_in_web_cp_flow,
511521

512-
/**
513-
* Indicates whether or not in memory cache is used for accounts and credentials.
514-
*/
515-
in_memory_cache_used_for_accounts_and_credentials,
516-
517522
/**
518523
* Indicates whether the filter-then-clone optimization is enabled for in-memory cache
519524
* getCredentialsFilteredBy()/getAccountsFilteredBy() operations.

0 commit comments

Comments
 (0)