@Test public void testFailedUpdate() throws Exception { // Just making sure exception does not propagate MetricObserver fmo = new FailingMetricObserver(); AsyncMetricObserver amo = new AsyncMetricObserver("async", fmo, 50, 250); amo.update(mkList(1)); amo.update(mkList(1)); amo.update(mkList(1)); amo.update(mkList(1)); amo.update(mkList(1)); amo.update(mkList(1)); Thread.sleep(1000); amo.stop(); } }
/** * {@inheritDoc} */ public void updateImpl(List<Metric> metrics) { long now = System.currentTimeMillis(); TimestampedUpdate update = new TimestampedUpdate(now, metrics); boolean result = updateQueue.offer(update); final int maxAttempts = 5; int attempts = 0; while (!result && attempts < maxAttempts) { updateQueue.remove(); result = updateQueue.offer(update); ++attempts; } if (!result) { incrementFailedCount(); } }
private static MetricObserver async(String name, MetricObserver observer) { final long expireTime = 2000 * Config.getPollInterval(); final int queueSize = 10; return new AsyncMetricObserver(name, observer, queueSize, expireTime); }
MetricObserver createAsyncMetricObserver(MetricObserver observer, int queueSize, long expireTime) { return new AsyncMetricObserver(ASYNC_METRIC_OBSERVER_NAME, observer, queueSize, expireTime); }
@Test public void testExpiration() throws Exception { MemoryMetricObserver mmo = new MemoryMetricObserver("mem", 50); MetricObserver smo = new SlowMetricObserver(mmo, 500L); AsyncMetricObserver amo = new AsyncMetricObserver("async", smo, 50, 250); amo.update(mkList(1)); amo.update(mkList(2)); amo.update(mkList(3)); amo.update(mkList(4)); Thread.sleep(4000); // Only the first update should have made it on time List<List<Metric>> observations = mmo.getObservations(); assertEquals(observations.size(), 1); assertEquals(observations.get(0), mkList(1)); amo.stop(); }
protected MetricObserver createAsyncObserver(final String name, final MetricObserver observer, final long pollInterval, final TimeUnit timeUnit) { return new AsyncMetricObserver(name, observer, observerQueueSize, getExpireTimeForInterval(pollInterval, timeUnit)); }
private void processUpdate() { TimestampedUpdate update; try { update = updateQueue.take(); long cutoff = System.currentTimeMillis() - expireTime; if (update.getTimestamp() < cutoff) { expiredUpdateCount.increment(); return; } wrappedObserver.update(update.getMetrics()); } catch (InterruptedException ie) { LOGGER.warn("interrupted while adding to queue, update dropped"); incrementFailedCount(); } catch (Throwable t) { LOGGER.warn("update failed for downstream queue", t); incrementFailedCount(); } }
@Test public void testUpdate() throws Exception { MemoryMetricObserver mmo = new MemoryMetricObserver("mem", 50); AsyncMetricObserver amo = new AsyncMetricObserver("async", mmo, 50); amo.update(mkList(1)); amo.update(mkList(2)); amo.update(mkList(3)); amo.update(mkList(4)); Thread.sleep(1000); assertEquals(mmo.getObservations(), UnmodifiableList.of(mkList(1), mkList(2), mkList(3), mkList(4))); amo.stop(); }
/** * {@inheritDoc} */ public void updateImpl(List<Metric> metrics) { long now = System.currentTimeMillis(); TimestampedUpdate update = new TimestampedUpdate(now, metrics); boolean result = updateQueue.offer(update); final int maxAttempts = 5; int attempts = 0; while (!result && attempts < maxAttempts) { updateQueue.remove(); result = updateQueue.offer(update); ++attempts; } if (!result) { incrementFailedCount(); } }
@Test public void testExceedQueueSize() throws Exception { MemoryMetricObserver mmo = new MemoryMetricObserver("mem", 50); MetricObserver smo = new SlowMetricObserver(mmo, 500L); AsyncMetricObserver amo = new AsyncMetricObserver("async", smo, 2); amo.update(mkList(1)); amo.update(mkList(2)); amo.update(mkList(3)); amo.update(mkList(4)); Thread.sleep(4000); // We should always get at least queueSize updates List<List<Metric>> observations = mmo.getObservations(); assertTrue(observations.size() >= 2); // Older entries are overwritten, the last queueSize updates should // be in the output int last = observations.size() - 1; assertEquals(observations.get(last - 1), mkList(3)); assertEquals(observations.get(last), mkList(4)); amo.stop(); }
private void processUpdate() { TimestampedUpdate update; try { update = updateQueue.take(); long cutoff = System.currentTimeMillis() - expireTime; if (update.getTimestamp() < cutoff) { expiredUpdateCount.increment(); return; } wrappedObserver.update(update.getMetrics()); } catch (InterruptedException ie) { LOGGER.warn("interrupted while adding to queue, update dropped"); incrementFailedCount(); } catch (Throwable t) { LOGGER.warn("update failed for downstream queue", t); incrementFailedCount(); } }