diff --git a/server/src/main/java/io/druid/client/CachingClusteredClient.java b/server/src/main/java/io/druid/client/CachingClusteredClient.java index dacfc7938ef..c2901c9675f 100644 --- a/server/src/main/java/io/druid/client/CachingClusteredClient.java +++ b/server/src/main/java/io/druid/client/CachingClusteredClient.java @@ -176,32 +176,37 @@ public class CachingClusteredClient implements QueryRunner queryCacheKey = null; } - // Pull cached segments from cache and remove from set of segments to query - if (useCache && queryCacheKey != null) { + if (queryCacheKey != null) { Map, Cache.NamedKey> cacheKeys = Maps.newHashMap(); - for (Pair e : segments) { - cacheKeys.put(e, computeSegmentCacheKey(e.lhs.getSegment().getIdentifier(), e.rhs, queryCacheKey)); + for (Pair segment : segments) { + final Cache.NamedKey segmentCacheKey = computeSegmentCacheKey( + segment.lhs.getSegment().getIdentifier(), + segment.rhs, + queryCacheKey + ); + cacheKeys.put(segment, segmentCacheKey); } - Map cachedValues = cache.getBulk(cacheKeys.values()); + // Pull cached segments from cache and remove from set of segments to query + final Map cachedValues; + if (useCache) { + cachedValues = cache.getBulk(cacheKeys.values()); + } else { + cachedValues = ImmutableMap.of(); + } for (Map.Entry, Cache.NamedKey> entry : cacheKeys.entrySet()) { Pair segment = entry.getKey(); Cache.NamedKey segmentCacheKey = entry.getValue(); - - final ServerSelector selector = segment.lhs; - final SegmentDescriptor descriptor = segment.rhs; - final Interval segmentQueryInterval = descriptor.getInterval(); + final Interval segmentQueryInterval = segment.rhs.getInterval(); final byte[] cachedValue = cachedValues.get(segmentCacheKey); - if (cachedValue != null) { - cachedResults.add(Pair.of(segmentQueryInterval.getStart(), cachedValue)); - // remove cached segment from set of segments to query segments.remove(segment); - } else { - final String segmentIdentifier = selector.getSegment().getIdentifier(); + cachedResults.add(Pair.of(segmentQueryInterval.getStart(), cachedValue)); + } else if (populateCache) { + final String segmentIdentifier = segment.lhs.getSegment().getIdentifier(); cachePopulatorMap.put( String.format("%s_%s", segmentIdentifier, segmentQueryInterval), new CachePopulator(cache, objectMapper, segmentCacheKey) @@ -331,9 +336,12 @@ public class CachingClusteredClient implements QueryRunner String segmentIdentifier = value.getSegmentId(); final Iterable segmentResults = value.getResults(); - cachePopulatorMap.get( + CachePopulator cachePopulator = cachePopulatorMap.get( String.format("%s_%s", segmentIdentifier, value.getInterval()) - ).populate(Iterables.transform(segmentResults, prepareForCache)); + ); + if(cachePopulator != null) { + cachePopulator.populate(Iterables.transform(segmentResults, prepareForCache)); + } return Sequences.simple( Iterables.transform(