Skip to content

Commit

Permalink
feat(cats): Allow partial cache updates
Browse files Browse the repository at this point in the history
Currently, cache providers (either Default or SQL) evicts all those
keys not present on the CacheResult. This prevents doing partial
updates in on-demand caching agents, which may come in handy when
the query operation is a costly one and only a subset of the
keys may actually need updating.

This patch introduces the notion of a partial CacheResult, which allows
keeping the keys not present on the CacheResult if the partialResult
flag is set to true. This change is backwards compatible, keeping
the previous behaviour (replacement strategy) by default.
  • Loading branch information
Xavi León authored and asher committed Jun 13, 2019
1 parent a316ab9 commit d694f7b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,13 @@ default Map<String, Collection<String>> getEvictions() {
default Map<String, Object> getIntrospectionDetails() {
return Collections.emptyMap();
}

/**
* If true, no evictions of existing keys are done unless specified by the getEvictions() method.
*
* @return
*/
default boolean isPartialResult() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,11 @@ public void storeAgentResult(Agent agent, CacheResult result) {
}
}

cache.putCacheResult(agent.getAgentType(), authoritative, result);
if (result.isPartialResult()) {
cache.addCacheResult(agent.getAgentType(), authoritative, result);
} else {
cache.putCacheResult(agent.getAgentType(), authoritative, result);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,17 @@ public class DefaultCacheResult implements CacheResult {
private final Map<String, Collection<CacheData>> cacheResults;
private final Map<String, Collection<String>> evictions;
@Getter private final Map<String, Object> introspectionDetails;
@Getter private final boolean partialResult;

public DefaultCacheResult(Map<String, Collection<CacheData>> cacheResults) {
this(cacheResults, new HashMap<>());
}

public DefaultCacheResult(
Map<String, Collection<CacheData>> cacheResults, boolean partialResult) {
this(cacheResults, new HashMap<>(), new HashMap<>(), partialResult);
}

public DefaultCacheResult(
Map<String, Collection<CacheData>> cacheResults, Map<String, Collection<String>> evictions) {
this(cacheResults, evictions, new HashMap<>());
Expand All @@ -41,9 +47,18 @@ public DefaultCacheResult(
Map<String, Collection<CacheData>> cacheResults,
Map<String, Collection<String>> evictions,
Map<String, Object> introspectionDetails) {
this(cacheResults, evictions, introspectionDetails, false);
}

public DefaultCacheResult(
Map<String, Collection<CacheData>> cacheResults,
Map<String, Collection<String>> evictions,
Map<String, Object> introspectionDetails,
boolean partialResult) {
this.cacheResults = cacheResults;
this.evictions = evictions;
this.introspectionDetails = introspectionDetails;
this.partialResult = partialResult;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ class CatsOnDemandCacheUpdater implements OnDemandCacheUpdater {
}
}
agent.metricsSupport.cacheWrite {
providerCache.putCacheResult(result.sourceAgentType, result.authoritativeTypes, result.cacheResult)
if (result.cacheResult.isPartialResult()) {
providerCache.addCacheResult(result.sourceAgentType, result.authoritativeTypes, result.cacheResult)
} else {
providerCache.putCacheResult(result.sourceAgentType, result.authoritativeTypes, result.cacheResult)
}
}
}
if (result.evictions) {
Expand Down

0 comments on commit d694f7b

Please sign in to comment.