/** * {@inheritDoc} */ @Override @SuppressWarnings("unchecked") protected Collection<Future<Void>> processData(DefaultData defaultData) { E timerData = (E) defaultData; long alteredTimestamp = getAlteredTimestamp(timerData); int cacheHash = getCacheHash(timerData, alteredTimestamp); IAggregatedData<E> aggData = map.get(cacheHash); if (null == aggData) { aggData = clone(timerData, alteredTimestamp); IAggregatedData<E> insertedData = map.putIfAbsent(cacheHash, aggData); // if put happened null will be returned if (null == insertedData) { queue.add(aggData); int count = elementCount.incrementAndGet(); if (maxElements < count) { this.writeOldest(); } } else { aggData = insertedData; } } dataAggregator.aggregate(aggData, timerData); return Collections.emptyList(); }
/** * Passes data to StorageWriter to be written. * * @param data * Data to be written. * @return {@link Future} received from Storage writer. */ private Future<Void> passToStorageWriter(DefaultData data) { // clear aggregated ids when saving to storage if (data instanceof IIdsAwareAggregatedData) { ((IIdsAwareAggregatedData<?>) data).clearAggregatedIds(); } // if I am writing the InvocationAwareData and invocations are not saved // make sure we don't save the invocation affiliation if (!writeInvocationAffiliation) { Map<String, Boolean> kryoPreferences = new HashMap<>(1); kryoPreferences.put(KryoSerializationPreferences.WRITE_INVOCATION_AFFILIATION_DATA, Boolean.FALSE); return getStorageWriter().write(data, kryoPreferences); } else { return getStorageWriter().write(data); } }
/** * Writes the oldest data to the storage. */ private void writeOldest() { IAggregatedData<E> oldest = queue.poll(); E data = oldest.getData(); map.remove(getCacheHash(data, data.getTimeStamp().getTime())); elementCount.decrementAndGet(); passToStorageWriter(data); }
/** * Test the {@link DataAggregatorProcessor} for a correct aggregation of data. */ @Test public void dataAggregatorProcessorAggregation() { int aggregationPeriod = 100; DataAggregatorProcessor<TimerData> dataAggregatorProcessor = new DataAggregatorProcessor<>(TimerData.class, aggregationPeriod, new TimerDataAggregator(), true); dataAggregatorProcessor.setStorageWriter(storageWriter); long timestampValue = new Date().getTime(); Random random = new Random(); long platformIdent = random.nextLong(); long sensorTypeIdent = random.nextLong(); long methodIdent = random.nextLong(); TimerData timerData = new TimerData(new Timestamp(timestampValue), platformIdent, sensorTypeIdent, methodIdent); assertThat(dataAggregatorProcessor.canBeProcessed(timerData), is(true)); final int elements = 1000; for (int i = 0; i < (elements / 2); i++) { dataAggregatorProcessor.process(timerData); } Collection<Future<Void>> futures = dataAggregatorProcessor.flush(); assertThat(futures, hasSize(1)); verify(storageWriter, times(1)).write(Matchers.<TimerData> anyObject()); }
when(aggregator.getAggregationKey(Matchers.<TimerData> anyObject())).thenReturn(1L); DataAggregatorProcessor<TimerData> dataProcessor = new DataAggregatorProcessor<>(TimerData.class, 1000, 1, aggregator, false); IWriter writer = mock(IWriter.class); dataProcessor.setStorageWriter(writer); when(aggregator.getClone(Matchers.<TimerData> anyObject())).thenReturn(new AggregatedTimerData()); dataProcessor.process(timerData1); dataProcessor.process(timerData1); timerData1.setTimeStamp(newTimestamp); dataProcessor.process(timerData1);
normalProcessors.add(new DataAggregatorProcessor<>(TimerData.class, 5000, new TimerDataAggregator(), true)); normalProcessors.add(new DataAggregatorProcessor<>(SqlStatementData.class, 5000, new SqlStatementDataAggregator(true), true));
if (saveClassesList.contains(TimerData.class)) { saveClassesList.remove(TimerData.class); DataAggregatorProcessor<TimerData> dataAggregatorProcessor = new DataAggregatorProcessor<>(TimerData.class, aggregationPeriod, new TimerDataAggregator(), writeInvocationAffiliation); normalProcessors.add(dataAggregatorProcessor); DataAggregatorProcessor<SqlStatementData> dataAggregatorProcessor = new DataAggregatorProcessor<>(SqlStatementData.class, aggregationPeriod, new SqlStatementDataAggregator(true), writeInvocationAffiliation); normalProcessors.add(dataAggregatorProcessor);
/** * {@inheritDoc} */ @Override public Collection<Future<Void>> flush() { Collection<Future<Void>> futures = new ArrayList<>(); IAggregatedData<E> oldest = queue.poll(); while (null != oldest) { E data = oldest.getData(); map.remove(getCacheHash(data, data.getTimeStamp().getTime())); elementCount.decrementAndGet(); Future<Void> future = passToStorageWriter(data); CollectionUtils.addIgnoreNull(futures, future); oldest = queue.poll(); } return futures; }