@Test public void testRemoveStaleChunks() { final Map.Entry<Long, Chunk> fakeMapEntry = new Map.Entry<Long, Chunk>() { @Override public Long getKey() { return 100L; } @Override public Chunk getValue() { return null; } @Override public Chunk setValue(Chunk value) { return null; } }; assertTrue(chunkManager.getChunkMap().isEmpty()); chunkManager.removeStaleChunks(Collections.emptyList()); chunkManager.removeStaleChunks(Collections.singletonList(fakeMapEntry)); assertTrue(chunkManager.getChunkMap().isEmpty()); chunkManager.addMetric(MetricUtils .makeMetricString(testMetricName, inputTagString, startTime + 1, testValue)); assertEquals(1, chunkManager.getChunkMap().size()); ArrayList<Map.Entry<Long, Chunk>> chunks = new ArrayList<>(chunkManager.getChunkMap().entrySet()); chunkManager.removeStaleChunks(chunks); assertTrue(chunkManager.getChunkMap().isEmpty()); chunkManager.addMetric(MetricUtils .makeMetricString(testMetricName, inputTagString, startTime + 1, testValue)); chunkManager.addMetric(MetricUtils .makeMetricString(testMetricName, inputTagString, startTimePlusTwoHours + 1, testValue)); assertEquals(2, chunkManager.getChunkMap().size()); ArrayList<Map.Entry<Long, Chunk>> chunks2 = new ArrayList<>(chunkManager.getChunkMap().entrySet()); chunkManager.removeStaleChunks(chunks2); assertTrue(chunkManager.getChunkMap().isEmpty()); chunkManager.addMetric(MetricUtils .makeMetricString(testMetricName, inputTagString, startTime + 1, testValue)); chunkManager.addMetric(MetricUtils .makeMetricString(testMetricName, inputTagString, startTimePlusTwoHours, testValue)); chunkManager.addMetric(MetricUtils .makeMetricString(testMetricName, inputTagString, startTimePlusFourHours, testValue)); assertEquals(3, chunkManager.getChunkMap().size()); ArrayList<Map.Entry<Long, Chunk>> chunks3 = new ArrayList<>(); for (Map.Entry<Long, Chunk> entry : chunkManager.getChunkMap().entrySet()) { if (entry.getKey() < startTimePlusFourHours) { chunks3.add(entry); } } chunkManager.removeStaleChunks(chunks3); assertEquals(1, chunkManager.getChunkMap().size()); assertEquals(startTimePlusFourHours, chunkManager.getChunkMap().get(startTimePlusFourHours).info().startTimeSecs); chunkManager.removeStaleChunks(Collections.singletonList(fakeMapEntry)); assertEquals(1, chunkManager.getChunkMap().size()); assertEquals(startTimePlusFourHours, chunkManager.getChunkMap().get(startTimePlusFourHours).info().startTimeSecs); chunkManager.removeStaleChunks(Collections.emptyList()); assertEquals(1, chunkManager.getChunkMap().size()); assertEquals(startTimePlusFourHours, chunkManager.getChunkMap().get(startTimePlusFourHours).info().startTimeSecs); }
public void removeStaleChunks(List<Map.Entry<Long, Chunk>> staleChunks) { LOG.info("Stale chunks to be removed are: {}", staleChunks); if (chunkMap.isEmpty()) { LOG.warn("Possible bug or race condition. There are no chunks in chunk map."); } staleChunks.forEach(entry -> { try { if (chunkMap.containsKey(entry.getKey())) { final Chunk chunk = entry.getValue(); String chunkInfo = chunk.info().toString(); LOG.info("Deleting chunk {}.", chunkInfo); synchronized (chunkMapSync) { chunkMap.remove(entry.getKey()); } chunk.close(); LOG.info("Deleted chunk {}.", chunkInfo); } else { LOG.warn("Possible bug or race condition! Chunk {} doesn't exist in chunk map {}.", entry, chunkMap); } } catch (Exception e) { LOG.error("Exception when deleting chunk.", e); } }); }
ChunkManager { public void removeStaleChunks(List<Map.Entry<Long, Chunk>> staleChunks) { LOG.info("Stale chunks to be removed are: {}", staleChunks); if (chunkMap.isEmpty()) { LOG.warn("Possible bug or race condition. There are no chunks in chunk map."); } staleChunks.forEach(entry -> { try { if (chunkMap.containsKey(entry.getKey())) { final Chunk chunk = entry.getValue(); String chunkInfo = chunk.info().toString(); LOG.info("Deleting chunk {}.", chunkInfo); synchronized (chunkMapSync) { chunkMap.remove(entry.getKey()); } chunk.close(); LOG.info("Deleted chunk {}.", chunkInfo); } else { LOG.warn("Possible bug or race condition! Chunk {} doesn't exist in chunk map {}.", entry, chunkMap); } } catch (Exception e) { LOG.error("Exception when deleting chunk.", e); } }); } }
ChunkManager { public void removeStaleChunks(List<Map.Entry<Long, Chunk>> staleChunks) { LOG.info("Stale chunks to be removed are: {}", staleChunks); if (chunkMap.isEmpty()) { LOG.warn("Possible bug or race condition. There are no chunks in chunk map."); } staleChunks.forEach(entry -> { try { if (chunkMap.containsKey(entry.getKey())) { final Chunk chunk = entry.getValue(); String chunkInfo = chunk.info().toString(); LOG.info("Deleting chunk {}.", chunkInfo); synchronized (chunkMapSync) { chunkMap.remove(entry.getKey()); } chunk.close(); LOG.info("Deleted chunk {}.", chunkInfo); } else { LOG.warn("Possible bug or race condition! Chunk {} doesn't exist in chunk map {}.", entry, chunkMap); } } catch (Exception e) { LOG.error("Exception when deleting chunk.", e); } }); } ChunkManager(String chunkDataPrefix, int expectedTagStoreSize);  ChunkManager(String chunkDataPrefix, int expectedTagStoreSize, String dataDirectory); }
ChunkManager { public void removeStaleChunks(List<Map.Entry<Long, Chunk>> staleChunks) { LOG.info("Stale chunks to be removed are: {}", staleChunks); if (chunkMap.isEmpty()) { LOG.warn("Possible bug or race condition. There are no chunks in chunk map."); } staleChunks.forEach(entry -> { try { if (chunkMap.containsKey(entry.getKey())) { final Chunk chunk = entry.getValue(); String chunkInfo = chunk.info().toString(); LOG.info("Deleting chunk {}.", chunkInfo); synchronized (chunkMapSync) { chunkMap.remove(entry.getKey()); } chunk.close(); LOG.info("Deleted chunk {}.", chunkInfo); } else { LOG.warn("Possible bug or race condition! Chunk {} doesn't exist in chunk map {}.", entry, chunkMap); } } catch (Exception e) { LOG.error("Exception when deleting chunk.", e); } }); } ChunkManager(String chunkDataPrefix, int expectedTagStoreSize);  ChunkManager(String chunkDataPrefix, int expectedTagStoreSize, String dataDirectory); Chunk getChunk(long timestamp); void addMetric(final String metricString); List<TimeSeries> queryAroundChunkBoundaries(Query query, long startTsSecs,
                                                     long endTsSecs,
                                                     QueryAggregation queryAggregation); List<TimeSeries> query(Query query, long startTsSecs, long endTsSecs,
                                QueryAggregation queryAggregation); void toReadOnlyChunks(List<Map.Entry<Long, Chunk>> expiredChunks); void removeStaleChunks(List<Map.Entry<Long, Chunk>> staleChunks); }
ChunkManager { public void removeStaleChunks(List<Map.Entry<Long, Chunk>> staleChunks) { LOG.info("Stale chunks to be removed are: {}", staleChunks); if (chunkMap.isEmpty()) { LOG.warn("Possible bug or race condition. There are no chunks in chunk map."); } staleChunks.forEach(entry -> { try { if (chunkMap.containsKey(entry.getKey())) { final Chunk chunk = entry.getValue(); String chunkInfo = chunk.info().toString(); LOG.info("Deleting chunk {}.", chunkInfo); synchronized (chunkMapSync) { chunkMap.remove(entry.getKey()); } chunk.close(); LOG.info("Deleted chunk {}.", chunkInfo); } else { LOG.warn("Possible bug or race condition! Chunk {} doesn't exist in chunk map {}.", entry, chunkMap); } } catch (Exception e) { LOG.error("Exception when deleting chunk.", e); } }); } ChunkManager(String chunkDataPrefix, int expectedTagStoreSize);  ChunkManager(String chunkDataPrefix, int expectedTagStoreSize, String dataDirectory); Chunk getChunk(long timestamp); void addMetric(final String metricString); List<TimeSeries> queryAroundChunkBoundaries(Query query, long startTsSecs,
                                                     long endTsSecs,
                                                     QueryAggregation queryAggregation); List<TimeSeries> query(Query query, long startTsSecs, long endTsSecs,
                                QueryAggregation queryAggregation); void toReadOnlyChunks(List<Map.Entry<Long, Chunk>> expiredChunks); void removeStaleChunks(List<Map.Entry<Long, Chunk>> staleChunks); static Duration DEFAULT_CHUNK_DURATION; }