@Override public void run() { while (true) { final MeasurementDataNumeric data; try { data = numericQueue.take(); if (data == KILL_SIGNAL) { return; } } catch (InterruptedException e) { // We still keep getting interrupted.. we really need to die return; } DateTime collectionTimeSlice = dateTimeService.getTimeSlice( new DateTime(data.getTimestamp()), configuration.getRawTimeSliceDuration()); Days days = Days.daysBetween(collectionTimeSlice, dateTimeService.now()); if (days.isGreaterThan(rawDataAgeLimit)) { log.info( data + " is older than the raw data age limit of " + rawDataAgeLimit.getDays() + " days. It will not be stored."); } else { StorageResultSetFuture rawFuture = dao.insertRawData(data); StorageResultSetFuture indexFuture = dao.updateIndex( IndexBucket.RAW, collectionTimeSlice.getMillis(), data.getScheduleId()); ListenableFuture<List<ResultSet>> insertsFuture = Futures.successfulAsList(rawFuture, indexFuture); Futures.addCallback( insertsFuture, new FutureCallback<List<ResultSet>>() { @Override public void onSuccess(List<ResultSet> result) {} @Override public void onFailure(Throwable t) { boolean offerSuccess = numericQueue.offer(data); if (!offerSuccess) { if (log.isDebugEnabled()) { log.debug( "An error occurred while inserting raw data", ThrowableUtil.getRootCause(t)); } else { log.warn( "An error occurred while inserting raw data: " + ThrowableUtil.getRootMessage(t) + ", data was: " + data); } } } }, tasks); } } }
public void addNumericData( final Set<MeasurementDataNumeric> dataSet, final RawDataInsertedCallback callback) { if (log.isDebugEnabled()) { log.debug("Inserting " + dataSet.size() + " raw metrics"); } final Stopwatch stopwatch = new Stopwatch().start(); final AtomicInteger remainingInserts = new AtomicInteger(dataSet.size()); for (final MeasurementDataNumeric data : dataSet) { DateTime collectionTimeSlice = dateTimeService.getTimeSlice( new DateTime(data.getTimestamp()), configuration.getRawTimeSliceDuration()); Days days = Days.daysBetween(collectionTimeSlice, dateTimeService.now()); if (days.isGreaterThan(rawDataAgeLimit)) { log.info( data + " is older than the raw data age limit of " + rawDataAgeLimit.getDays() + " days. It will not be stored."); } else { StorageResultSetFuture rawFuture = dao.insertRawData(data); StorageResultSetFuture indexFuture = dao.updateIndex(IndexBucket.RAW, collectionTimeSlice.getMillis(), data.getScheduleId()); ListenableFuture<List<ResultSet>> insertsFuture = Futures.successfulAsList(rawFuture, indexFuture); Futures.addCallback( insertsFuture, new FutureCallback<List<ResultSet>>() { @Override public void onSuccess(List<ResultSet> result) { callback.onSuccess(data); if (remainingInserts.decrementAndGet() == 0) { stopwatch.stop(); if (log.isDebugEnabled()) { log.debug( "Finished inserting " + dataSet.size() + " raw metrics in " + stopwatch.elapsed(TimeUnit.MILLISECONDS) + " ms"); } callback.onFinish(); } } @Override public void onFailure(Throwable t) { if (log.isDebugEnabled()) { log.debug( "An error occurred while inserting raw data", ThrowableUtil.getRootCause(t)); } else { log.warn( "An error occurred while inserting raw data: " + ThrowableUtil.getRootMessage(t)); } callback.onFailure(t); } }, tasks); } } }