Skip to content

Commit 05ecaee

Browse files
Jonas-Isrnewtork
andauthored
fix: Fix caching in getOrComputeAllDestinationsCommand (#1055)
Co-authored-by: Alexander Dümont <[email protected]>
1 parent b4a3b0e commit 05ecaee

File tree

4 files changed

+43
-8
lines changed

4 files changed

+43
-8
lines changed

cloudplatform/connectivity-destination-service/src/main/java/com/sap/cloud/sdk/cloudplatform/connectivity/DestinationService.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
import java.time.Duration;
44
import java.util.ArrayList;
5+
import java.util.Arrays;
56
import java.util.Collection;
67
import java.util.LinkedHashMap;
78
import java.util.List;
89
import java.util.Map;
10+
import java.util.Set;
911
import java.util.concurrent.locks.ReentrantLock;
1012
import java.util.function.BiFunction;
1113
import java.util.function.Function;
@@ -847,6 +849,11 @@ private static Try<Destination> getOrComputeDestination(
847849
@Nullable
848850
final GetOrComputeAllDestinationsCommand getAllCommand;
849851
if( changeDetectionEnabled ) {
852+
if( isUsingExperimentalFeatures(options) ) {
853+
final String msg =
854+
"Using change detection together with either fragments, cross-level options, or custom headers is discouraged and might lead to unexpected caching behaviour.";
855+
log.warn(msg);
856+
}
850857
getAllCommand =
851858
GetOrComputeAllDestinationsCommand
852859
.prepareCommand(
@@ -878,11 +885,24 @@ private static Try<List<DestinationProperties>> getOrComputeAllDestinations(
878885
return destinationDownloader.apply(options);
879886
}
880887

888+
if( isUsingExperimentalFeatures(options) ) {
889+
final String msg =
890+
"Using caching together with either fragments, cross-level options, or custom headers is discouraged and might lead to unexpected caching behaviour.";
891+
log.warn(msg);
892+
}
893+
881894
return GetOrComputeAllDestinationsCommand
882895
.prepareCommand(options, instanceAll(), isolationLocks(), destinationDownloader)
883896
.execute();
884897
}
885898

899+
private static boolean isUsingExperimentalFeatures( @Nonnull final DestinationOptions options )
900+
{
901+
final String[] featureNames = { "X-fragment-name", "crossLevelSetting", "customHeader" };
902+
final Set<String> keys = options.getOptionKeys();
903+
return keys.stream().anyMatch(s -> Arrays.stream(featureNames).anyMatch(s::equalsIgnoreCase));
904+
}
905+
886906
private Cache()
887907
{
888908
throw new IllegalStateException("This static class must never be instantiated.");

cloudplatform/connectivity-destination-service/src/main/java/com/sap/cloud/sdk/cloudplatform/connectivity/GetOrComputeAllDestinationsCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static GetOrComputeAllDestinationsCommand prepareCommand(
4141

4242
final CacheKey cacheKey = CacheKey.ofTenantOptionalIsolation();
4343

44-
cacheKey.append(destinationOptions);
44+
cacheKey.append(DestinationServiceOptionsAugmenter.getRetrievalStrategy(destinationOptions));
4545

4646
final ReentrantLock isolationLock =
4747
Objects.requireNonNull(isolationLocks.get(cacheKey, any -> new ReentrantLock()));

cloudplatform/connectivity-destination-service/src/test/java/com/sap/cloud/sdk/cloudplatform/connectivity/DestinationServiceTest.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,9 +1255,13 @@ void testConcurrentFetchSameDestinationButDifferentTenant()
12551255
assertThat(DestinationService.Cache.isolationLocks().asMap())
12561256
.containsOnlyKeys(
12571257
CacheKey.fromIds("TenantA", null).append(destinationName, options),
1258-
CacheKey.fromIds("TenantA", null).append(options),
1258+
CacheKey
1259+
.fromIds("TenantA", null)
1260+
.append(DestinationServiceOptionsAugmenter.getRetrievalStrategy(options)),
12591261
CacheKey.fromIds("TenantB", null).append(destinationName, options),
1260-
CacheKey.fromIds("TenantB", null).append(options));
1262+
CacheKey
1263+
.fromIds("TenantB", null)
1264+
.append(DestinationServiceOptionsAugmenter.getRetrievalStrategy(options)));
12611265

12621266
// assert cache entries for one get-single command for each tenant
12631267
assertThat(DestinationService.Cache.instanceSingle().asMap())
@@ -1327,7 +1331,9 @@ void testPrincipalIsolationForDestinationWithUserPropagationWithExchangeOnlyStra
13271331
.containsOnlyKeys(
13281332
CacheKey.of(subscriberTenant, principal1).append(destinationName, options),
13291333
CacheKey.of(subscriberTenant, principal2).append(destinationName, options),
1330-
CacheKey.of(subscriberTenant, null).append(options));
1334+
CacheKey
1335+
.of(subscriberTenant, null)
1336+
.append(DestinationServiceOptionsAugmenter.getRetrievalStrategy(options)));
13311337

13321338
assertThat(DestinationService.Cache.instanceSingle().asMap())
13331339
.containsOnlyKeys(
@@ -1371,7 +1377,9 @@ void testPrincipalIsolationForDestinationWithUserPropagationWithDefaultExchangeS
13711377
assertThat(DestinationService.Cache.isolationLocks().asMap())
13721378
.containsOnlyKeys(
13731379
CacheKey.of(subscriberTenant, null).append(destinationName, options),
1374-
CacheKey.of(subscriberTenant, null).append(options));
1380+
CacheKey
1381+
.of(subscriberTenant, null)
1382+
.append(DestinationServiceOptionsAugmenter.getRetrievalStrategy(options)));
13751383

13761384
assertThat(DestinationService.Cache.instanceSingle().asMap())
13771385
.containsOnlyKeys(

cloudplatform/connectivity-destination-service/src/test/java/com/sap/cloud/sdk/cloudplatform/connectivity/GetOrComputeAllDestinationsCommandTest.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,12 @@ void testCommandIsIdempotent()
9090
assertThat(result.get()).containsExactly(destination);
9191

9292
assertThat(allDestinationsCache.estimatedSize()).isEqualTo(1);
93-
assertThat(allDestinationsCache.getIfPresent(CacheKey.ofNoIsolation().append(EMPTY_OPTIONS)))
93+
assertThat(
94+
allDestinationsCache
95+
.getIfPresent(
96+
CacheKey
97+
.ofNoIsolation()
98+
.append(DestinationServiceOptionsAugmenter.getRetrievalStrategy(EMPTY_OPTIONS))))
9499
.containsExactly(destination);
95100
verify(tryGetAllDestinations, times(1)).get();
96101
}
@@ -104,8 +109,10 @@ void testIsolationAndAtomicityPerTenant()
104109
final CountDownLatch mainThreadLatch = new CountDownLatch(1);
105110
final CountDownLatch getAllLatch = new CountDownLatch(1);
106111
final AtomicInteger lockInvocations = new AtomicInteger();
107-
final CacheKey t1Key = CacheKey.of(t1, null).append(EMPTY_OPTIONS);
108-
final CacheKey t2Key = CacheKey.of(t2, null).append(EMPTY_OPTIONS);
112+
final CacheKey t1Key =
113+
CacheKey.of(t1, null).append(DestinationServiceOptionsAugmenter.getRetrievalStrategy(EMPTY_OPTIONS));
114+
final CacheKey t2Key =
115+
CacheKey.of(t2, null).append(DestinationServiceOptionsAugmenter.getRetrievalStrategy(EMPTY_OPTIONS));
109116
final ReentrantLock tenantIsolationLock = spy(ReentrantLock.class);
110117

111118
doAnswer(invocation -> {

0 commit comments

Comments
 (0)