use of org.apache.flink.metrics.MetricGroup in project flink by apache.
the class TaskExecutorMetricsInitializer method instantiateMemoryMetrics.
private static void instantiateMemoryMetrics(MetricGroup metrics) {
final MemoryMXBean mxBean = ManagementFactory.getMemoryMXBean();
MetricGroup heap = metrics.addGroup("Heap");
heap.<Long, Gauge<Long>>gauge("Used", new Gauge<Long>() {
@Override
public Long getValue() {
return mxBean.getHeapMemoryUsage().getUsed();
}
});
heap.<Long, Gauge<Long>>gauge("Committed", new Gauge<Long>() {
@Override
public Long getValue() {
return mxBean.getHeapMemoryUsage().getCommitted();
}
});
heap.<Long, Gauge<Long>>gauge("Max", new Gauge<Long>() {
@Override
public Long getValue() {
return mxBean.getHeapMemoryUsage().getMax();
}
});
MetricGroup nonHeap = metrics.addGroup("NonHeap");
nonHeap.<Long, Gauge<Long>>gauge("Used", new Gauge<Long>() {
@Override
public Long getValue() {
return mxBean.getNonHeapMemoryUsage().getUsed();
}
});
nonHeap.<Long, Gauge<Long>>gauge("Committed", new Gauge<Long>() {
@Override
public Long getValue() {
return mxBean.getNonHeapMemoryUsage().getCommitted();
}
});
nonHeap.<Long, Gauge<Long>>gauge("Max", new Gauge<Long>() {
@Override
public Long getValue() {
return mxBean.getNonHeapMemoryUsage().getMax();
}
});
final MBeanServer con = ManagementFactory.getPlatformMBeanServer();
final String directBufferPoolName = "java.nio:type=BufferPool,name=direct";
try {
final ObjectName directObjectName = new ObjectName(directBufferPoolName);
MetricGroup direct = metrics.addGroup("Direct");
direct.<Long, Gauge<Long>>gauge("Count", new TaskExecutorMetricsInitializer.AttributeGauge<>(con, directObjectName, "Count", -1L));
direct.<Long, Gauge<Long>>gauge("MemoryUsed", new TaskExecutorMetricsInitializer.AttributeGauge<>(con, directObjectName, "MemoryUsed", -1L));
direct.<Long, Gauge<Long>>gauge("TotalCapacity", new TaskExecutorMetricsInitializer.AttributeGauge<>(con, directObjectName, "TotalCapacity", -1L));
} catch (MalformedObjectNameException e) {
LOG.warn("Could not create object name {}.", directBufferPoolName, e);
}
final String mappedBufferPoolName = "java.nio:type=BufferPool,name=mapped";
try {
final ObjectName mappedObjectName = new ObjectName(mappedBufferPoolName);
MetricGroup mapped = metrics.addGroup("Mapped");
mapped.<Long, Gauge<Long>>gauge("Count", new TaskExecutorMetricsInitializer.AttributeGauge<>(con, mappedObjectName, "Count", -1L));
mapped.<Long, Gauge<Long>>gauge("MemoryUsed", new TaskExecutorMetricsInitializer.AttributeGauge<>(con, mappedObjectName, "MemoryUsed", -1L));
mapped.<Long, Gauge<Long>>gauge("TotalCapacity", new TaskExecutorMetricsInitializer.AttributeGauge<>(con, mappedObjectName, "TotalCapacity", -1L));
} catch (MalformedObjectNameException e) {
LOG.warn("Could not create object name {}.", mappedBufferPoolName, e);
}
}
use of org.apache.flink.metrics.MetricGroup in project flink by apache.
the class RichAsyncFunctionTest method testRuntimeContext.
/**
* Test the set of runtime context methods in the context of a {@link RichAsyncFunction}.
*/
@Test
public void testRuntimeContext() throws Exception {
RichAsyncFunction<Integer, Integer> function = new RichAsyncFunction<Integer, Integer>() {
private static final long serialVersionUID = 1707630162838967972L;
@Override
public void asyncInvoke(Integer input, AsyncCollector<Integer> collector) throws Exception {
// no op
}
};
final String taskName = "foobarTask";
final MetricGroup metricGroup = mock(MetricGroup.class);
final int numberOfParallelSubtasks = 42;
final int indexOfSubtask = 43;
final int attemptNumber = 1337;
final String taskNameWithSubtask = "barfoo";
final ExecutionConfig executionConfig = mock(ExecutionConfig.class);
final ClassLoader userCodeClassLoader = mock(ClassLoader.class);
RuntimeContext mockedRuntimeContext = mock(RuntimeContext.class);
when(mockedRuntimeContext.getTaskName()).thenReturn(taskName);
when(mockedRuntimeContext.getMetricGroup()).thenReturn(metricGroup);
when(mockedRuntimeContext.getNumberOfParallelSubtasks()).thenReturn(numberOfParallelSubtasks);
when(mockedRuntimeContext.getIndexOfThisSubtask()).thenReturn(indexOfSubtask);
when(mockedRuntimeContext.getAttemptNumber()).thenReturn(attemptNumber);
when(mockedRuntimeContext.getTaskNameWithSubtasks()).thenReturn(taskNameWithSubtask);
when(mockedRuntimeContext.getExecutionConfig()).thenReturn(executionConfig);
when(mockedRuntimeContext.getUserCodeClassLoader()).thenReturn(userCodeClassLoader);
function.setRuntimeContext(mockedRuntimeContext);
RuntimeContext runtimeContext = function.getRuntimeContext();
assertEquals(taskName, runtimeContext.getTaskName());
assertEquals(metricGroup, runtimeContext.getMetricGroup());
assertEquals(numberOfParallelSubtasks, runtimeContext.getNumberOfParallelSubtasks());
assertEquals(indexOfSubtask, runtimeContext.getIndexOfThisSubtask());
assertEquals(attemptNumber, runtimeContext.getAttemptNumber());
assertEquals(taskNameWithSubtask, runtimeContext.getTaskNameWithSubtasks());
assertEquals(executionConfig, runtimeContext.getExecutionConfig());
assertEquals(userCodeClassLoader, runtimeContext.getUserCodeClassLoader());
try {
runtimeContext.getDistributedCache();
fail("Expected getDistributedCached to fail with unsupported operation exception.");
} catch (UnsupportedOperationException e) {
// expected
}
try {
runtimeContext.getState(new ValueStateDescriptor<>("foobar", Integer.class, 42));
fail("Expected getState to fail with unsupported operation exception.");
} catch (UnsupportedOperationException e) {
// expected
}
try {
runtimeContext.getListState(new ListStateDescriptor<>("foobar", Integer.class));
fail("Expected getListState to fail with unsupported operation exception.");
} catch (UnsupportedOperationException e) {
// expected
}
try {
runtimeContext.getReducingState(new ReducingStateDescriptor<>("foobar", new ReduceFunction<Integer>() {
private static final long serialVersionUID = 2136425961884441050L;
@Override
public Integer reduce(Integer value1, Integer value2) throws Exception {
return value1;
}
}, Integer.class));
fail("Expected getReducingState to fail with unsupported operation exception.");
} catch (UnsupportedOperationException e) {
// expected
}
try {
runtimeContext.getFoldingState(new FoldingStateDescriptor<>("foobar", 0, new FoldFunction<Integer, Integer>() {
@Override
public Integer fold(Integer accumulator, Integer value) throws Exception {
return accumulator;
}
}, Integer.class));
} catch (UnsupportedOperationException e) {
// expected
}
try {
runtimeContext.getMapState(new MapStateDescriptor<>("foobar", Integer.class, String.class));
} catch (UnsupportedOperationException e) {
// expected
}
try {
runtimeContext.addAccumulator("foobar", new Accumulator<Integer, Integer>() {
private static final long serialVersionUID = -4673320336846482358L;
@Override
public void add(Integer value) {
// no op
}
@Override
public Integer getLocalValue() {
return null;
}
@Override
public void resetLocal() {
}
@Override
public void merge(Accumulator<Integer, Integer> other) {
}
@Override
public Accumulator<Integer, Integer> clone() {
return null;
}
});
fail("Expected addAccumulator to fail with unsupported operation exception.");
} catch (UnsupportedOperationException e) {
// expected
}
try {
runtimeContext.getAccumulator("foobar");
fail("Expected getAccumulator to fail with unsupported operation exception.");
} catch (UnsupportedOperationException e) {
// expected
}
try {
runtimeContext.getAllAccumulators();
fail("Expected getAllAccumulators to fail with unsupported operation exception.");
} catch (UnsupportedOperationException e) {
// expected
}
try {
runtimeContext.getIntCounter("foobar");
fail("Expected getIntCounter to fail with unsupported operation exception.");
} catch (UnsupportedOperationException e) {
// expected
}
try {
runtimeContext.getLongCounter("foobar");
fail("Expected getLongCounter to fail with unsupported operation exception.");
} catch (UnsupportedOperationException e) {
// expected
}
try {
runtimeContext.getDoubleCounter("foobar");
fail("Expected getDoubleCounter to fail with unsupported operation exception.");
} catch (UnsupportedOperationException e) {
// expected
}
try {
runtimeContext.getHistogram("foobar");
fail("Expected getHistogram to fail with unsupported operation exception.");
} catch (UnsupportedOperationException e) {
// expected
}
try {
runtimeContext.hasBroadcastVariable("foobar");
fail("Expected hasBroadcastVariable to fail with unsupported operation exception.");
} catch (UnsupportedOperationException e) {
// expected
}
try {
runtimeContext.getBroadcastVariable("foobar");
fail("Expected getBroadcastVariable to fail with unsupported operation exception.");
} catch (UnsupportedOperationException e) {
// expected
}
try {
runtimeContext.getBroadcastVariableWithInitializer("foobar", new BroadcastVariableInitializer<Object, Object>() {
@Override
public Object initializeBroadcastVariable(Iterable<Object> data) {
return null;
}
});
fail("Expected getBroadcastVariableWithInitializer to fail with unsupported operation exception.");
} catch (UnsupportedOperationException e) {
// expected
}
}
use of org.apache.flink.metrics.MetricGroup in project flink by apache.
the class ScheduledDropwizardReporterTest method testMetricCleanup.
/**
* This test verifies that metrics are properly added and removed to/from the ScheduledDropwizardReporter and
* the underlying Dropwizard MetricRegistry.
*/
@Test
public void testMetricCleanup() {
TestingScheduledDropwizardReporter rep = new TestingScheduledDropwizardReporter();
MetricGroup mp = new UnregisteredMetricsGroup();
Counter c = new SimpleCounter();
Meter m = new Meter() {
@Override
public void markEvent() {
}
@Override
public void markEvent(long n) {
}
@Override
public double getRate() {
return 0;
}
@Override
public long getCount() {
return 0;
}
};
Histogram h = new Histogram() {
@Override
public void update(long value) {
}
@Override
public long getCount() {
return 0;
}
@Override
public HistogramStatistics getStatistics() {
return null;
}
};
Gauge g = new Gauge() {
@Override
public Object getValue() {
return null;
}
};
rep.notifyOfAddedMetric(c, "counter", mp);
assertEquals(1, rep.getCounters().size());
assertEquals(1, rep.registry.getCounters().size());
rep.notifyOfAddedMetric(m, "meter", mp);
assertEquals(1, rep.getMeters().size());
assertEquals(1, rep.registry.getMeters().size());
rep.notifyOfAddedMetric(h, "histogram", mp);
assertEquals(1, rep.getHistograms().size());
assertEquals(1, rep.registry.getHistograms().size());
rep.notifyOfAddedMetric(g, "gauge", mp);
assertEquals(1, rep.getGauges().size());
assertEquals(1, rep.registry.getGauges().size());
rep.notifyOfRemovedMetric(c, "counter", mp);
assertEquals(0, rep.getCounters().size());
assertEquals(0, rep.registry.getCounters().size());
rep.notifyOfRemovedMetric(m, "meter", mp);
assertEquals(0, rep.getMeters().size());
assertEquals(0, rep.registry.getMeters().size());
rep.notifyOfRemovedMetric(h, "histogram", mp);
assertEquals(0, rep.getHistograms().size());
assertEquals(0, rep.registry.getHistograms().size());
rep.notifyOfRemovedMetric(g, "gauge", mp);
assertEquals(0, rep.getGauges().size());
assertEquals(0, rep.registry.getGauges().size());
}
use of org.apache.flink.metrics.MetricGroup in project flink by apache.
the class TaskExecutorMetricsInitializer method instantiateGarbageCollectorMetrics.
private static void instantiateGarbageCollectorMetrics(MetricGroup metrics) {
List<GarbageCollectorMXBean> garbageCollectors = ManagementFactory.getGarbageCollectorMXBeans();
for (final GarbageCollectorMXBean garbageCollector : garbageCollectors) {
MetricGroup gcGroup = metrics.addGroup(garbageCollector.getName());
gcGroup.<Long, Gauge<Long>>gauge("Count", new Gauge<Long>() {
@Override
public Long getValue() {
return garbageCollector.getCollectionCount();
}
});
gcGroup.<Long, Gauge<Long>>gauge("Time", new Gauge<Long>() {
@Override
public Long getValue() {
return garbageCollector.getCollectionTime();
}
});
}
}
use of org.apache.flink.metrics.MetricGroup in project flink by apache.
the class MetricGroupTest method sameGroupOnNameCollision.
@Test
public void sameGroupOnNameCollision() {
GenericMetricGroup group = new GenericMetricGroup(registry, new DummyAbstractMetricGroup(registry), "somegroup");
String groupName = "sometestname";
MetricGroup subgroup1 = group.addGroup(groupName);
MetricGroup subgroup2 = group.addGroup(groupName);
assertNotNull(subgroup1);
assertNotNull(subgroup2);
assertTrue(subgroup1 == subgroup2);
}
Aggregations