@Override public void run() { logger.trace("Checking critical analyzer"); check(); } };
@Override public void check() { boolean retry = true; while (retry) { try { for (CriticalComponent component : components) { if (component.isExpired(timeoutNanoSeconds)) { fireAction(component); // no need to keep running if there's already a component failed return; } } retry = false; // got to the end of the list, no need to retry } catch (ConcurrentModificationException dontCare) { // lets retry on the loop } } }
@Override public long getCheckTimeNanoSeconds() { if (checkTimeNanoSeconds == 0) { checkTimeNanoSeconds = getTimeout(TimeUnit.NANOSECONDS) / 2; } return checkTimeNanoSeconds; }
@Override public CriticalAnalyzer setTimeout(long timeout, TimeUnit unit) { if (checkTimeNanoSeconds <= 0) { this.setCheckTime(timeout / 2, unit); } this.timeoutNanoSeconds = unit.toNanos(timeout); return this; }
@Test public void testAction() throws Exception { analyzer = new CriticalAnalyzerImpl().setTimeout(100, TimeUnit.MILLISECONDS).setCheckTime(50, TimeUnit.MILLISECONDS); analyzer.add(new CriticalComponent() { @Override public boolean isExpired(long timeout) { return true; } }); CountDownLatch latch = new CountDownLatch(1); analyzer.start(); analyzer.addAction((CriticalComponent comp) -> { System.out.println("component " + comp + " received"); latch.countDown(); }); Assert.assertTrue(latch.await(10, TimeUnit.SECONDS)); analyzer.stop(); }
@Test public void testCriticalMeasureTakingLongButSucceeding() throws Exception { CriticalAnalyzer analyzer = new CriticalAnalyzerImpl(); CriticalComponent component = new CriticalComponentImpl(analyzer, 5); CriticalMeasure measure = new CriticalMeasure(component, 1); long time = System.nanoTime(); CriticalMeasure.TIME_ENTER_UPDATER.set(measure, time - TimeUnit.MINUTES.toNanos(5)); measure.leaveCritical(); Assert.assertFalse(measure.isExpired(TimeUnit.SECONDS.toNanos(30))); }
@Override public CriticalAnalyzer setTimeout(long timeout, TimeUnit unit) { if (checkTimeNanoSeconds <= 0) { this.setCheckTime(timeout / 2, unit); } this.timeoutNanoSeconds = unit.toNanos(timeout); return this; }
@Test public void testEnterNoLeaveNoExpire() throws Exception { analyzer = new CriticalAnalyzerImpl().setTimeout(10, TimeUnit.MILLISECONDS).setCheckTime(5, TimeUnit.MILLISECONDS); CriticalComponent component = new CriticalComponentImpl(analyzer, 2); component.enterCritical(0); Assert.assertFalse(component.isExpired(TimeUnit.MINUTES.toNanos(1))); analyzer.stop(); }
@Test public void testCriticalFailure() throws Exception { CriticalAnalyzer analyzer = new CriticalAnalyzerImpl(); CriticalComponent component = new CriticalComponentImpl(analyzer, 5); CriticalMeasure measure = new CriticalMeasure(component, 1); long time = System.nanoTime(); measure.enterCritical(); CriticalMeasure.TIME_ENTER_UPDATER.set(measure, time - TimeUnit.MINUTES.toNanos(5)); CriticalMeasure.TIME_LEFT_UPDATER.set(measure, time - TimeUnit.MINUTES.toNanos(10)); Assert.assertTrue(measure.isExpired(TimeUnit.SECONDS.toNanos(30))); measure.leaveCritical(); } }
@Override public CriticalAnalyzer setTimeout(long timeout, TimeUnit unit) { if (checkTimeNanoSeconds <= 0) { this.setCheckTime(timeout / 2, unit); } this.timeoutNanoSeconds = unit.toNanos(timeout); return this; }
@Override public long getCheckTimeNanoSeconds() { if (checkTimeNanoSeconds == 0) { checkTimeNanoSeconds = getTimeout(TimeUnit.NANOSECONDS) / 2; } return checkTimeNanoSeconds; }
@Override public void run() { logger.trace("Checking critical analyzer"); check(); } };
@Override public void check() { boolean retry = true; while (retry) { try { for (CriticalComponent component : components) { if (component.isExpired(timeoutNanoSeconds)) { fireAction(component); // no need to keep running if there's already a component failed return; } } retry = false; // got to the end of the list, no need to retry } catch (ConcurrentModificationException dontCare) { // lets retry on the loop } } }
@Test public void testEnterNoLeaveExpire() throws Exception { analyzer = new CriticalAnalyzerImpl().setTimeout(10, TimeUnit.MILLISECONDS).setCheckTime(5, TimeUnit.MILLISECONDS); CriticalComponent component = new CriticalComponentImpl(analyzer, 2); component.enterCritical(0); Thread.sleep(50); Assert.assertTrue(component.isExpired(0)); analyzer.stop(); }
if (configuration.isCriticalAnalyzer()) { analyzer = new CriticalAnalyzerImpl(); } else { analyzer = EmptyCriticalAnalyzer.getInstance();
@Override public CriticalAnalyzer setTimeout(long timeout, TimeUnit unit) { if (checkTimeNanoSeconds <= 0) { this.setCheckTime(timeout / 2, unit); } this.timeoutNanoSeconds = unit.toNanos(timeout); return this; }
@Override public long getCheckTimeNanoSeconds() { if (checkTimeNanoSeconds == 0) { checkTimeNanoSeconds = getTimeout(TimeUnit.NANOSECONDS) / 2; } return checkTimeNanoSeconds; }
@Override public void run() { logger.trace("Checking critical analyzer"); check(); } };
@Override public void check() { boolean retry = true; while (retry) { try { for (CriticalComponent component : components) { if (component.isExpired(timeoutNanoSeconds)) { fireAction(component); // no need to keep running if there's already a component failed return; } } retry = false; // got to the end of the list, no need to retry } catch (ConcurrentModificationException dontCare) { // lets retry on the loop } } }
@Test public void testNegative() throws Exception { analyzer = new CriticalAnalyzerImpl().setTimeout(10, TimeUnit.MILLISECONDS).setCheckTime(5, TimeUnit.MILLISECONDS); CriticalComponent component = new CriticalComponentImpl(analyzer, 1); analyzer.add(component); component.enterCritical(0); component.leaveCritical(0); CountDownLatch latch = new CountDownLatch(1); analyzer.start(); analyzer.addAction((CriticalComponent comp) -> { System.out.println("component " + comp + " received"); latch.countDown(); }); Assert.assertFalse(latch.await(100, TimeUnit.MILLISECONDS)); analyzer.stop(); }