Search in sources :

Example 6 with OperatorMetricGroup

use of org.apache.flink.metrics.groups.OperatorMetricGroup in project flink by apache.

the class KafkaPartitionSplitReaderTest method testNumBytesInCounter.

@Test
public void testNumBytesInCounter() throws Exception {
    final OperatorMetricGroup operatorMetricGroup = UnregisteredMetricGroups.createUnregisteredOperatorMetricGroup();
    final Counter numBytesInCounter = operatorMetricGroup.getIOMetricGroup().getNumBytesInCounter();
    KafkaPartitionSplitReader reader = createReader(new Properties(), InternalSourceReaderMetricGroup.wrap(operatorMetricGroup));
    // Add a split
    reader.handleSplitsChanges(new SplitsAddition<>(Collections.singletonList(new KafkaPartitionSplit(new TopicPartition(TOPIC1, 0), 0L))));
    reader.fetch();
    final long latestNumBytesIn = numBytesInCounter.getCount();
    // Since it's hard to know the exact number of bytes consumed, we just check if it is
    // greater than 0
    assertThat(latestNumBytesIn, Matchers.greaterThan(0L));
    // Add another split
    reader.handleSplitsChanges(new SplitsAddition<>(Collections.singletonList(new KafkaPartitionSplit(new TopicPartition(TOPIC2, 0), 0L))));
    reader.fetch();
    // We just check if numBytesIn is increasing
    assertThat(numBytesInCounter.getCount(), Matchers.greaterThan(latestNumBytesIn));
}
Also used : KafkaPartitionSplit(org.apache.flink.connector.kafka.source.split.KafkaPartitionSplit) Counter(org.apache.flink.metrics.Counter) TopicPartition(org.apache.kafka.common.TopicPartition) Properties(java.util.Properties) OperatorMetricGroup(org.apache.flink.metrics.groups.OperatorMetricGroup) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 7 with OperatorMetricGroup

use of org.apache.flink.metrics.groups.OperatorMetricGroup in project flink by apache.

the class ChainedOperatorsMetricTest method testOperatorIOMetricReuse.

@Test
public void testOperatorIOMetricReuse() throws Exception {
    // environment
    initEnvironment(MEMORY_MANAGER_SIZE, NETWORK_BUFFER_SIZE);
    this.mockEnv = new MockEnvironmentBuilder().setTaskName(HEAD_OPERATOR_NAME).setManagedMemorySize(MEMORY_MANAGER_SIZE).setInputSplitProvider(this.inputSplitProvider).setBufferSize(NETWORK_BUFFER_SIZE).setMetricGroup(TaskManagerMetricGroup.createTaskManagerMetricGroup(NoOpMetricRegistry.INSTANCE, "host", ResourceID.generate()).addJob(new JobID(), "jobName").addTask(new JobVertexID(), new ExecutionAttemptID(), "task", 0, 0)).build();
    final int keyCnt = 100;
    final int valCnt = 20;
    final int numRecords = keyCnt * valCnt;
    addInput(new UniformRecordGenerator(keyCnt, valCnt, false), 0);
    addOutput(this.outList);
    // the chained operator
    addChainedOperator();
    // creates the head operator and assembles the chain
    registerTask(FlatMapDriver.class, DuplicatingFlatMapFunction.class);
    final BatchTask<FlatMapFunction<Record, Record>, Record> testTask = new BatchTask<>(this.mockEnv);
    testTask.invoke();
    Assert.assertEquals(numRecords * 2 * 2, this.outList.size());
    final TaskMetricGroup taskMetricGroup = mockEnv.getMetricGroup();
    // verify task-level metrics
    {
        final TaskIOMetricGroup ioMetricGroup = taskMetricGroup.getIOMetricGroup();
        final Counter numRecordsInCounter = ioMetricGroup.getNumRecordsInCounter();
        final Counter numRecordsOutCounter = ioMetricGroup.getNumRecordsOutCounter();
        Assert.assertEquals(numRecords, numRecordsInCounter.getCount());
        Assert.assertEquals(numRecords * 2 * 2, numRecordsOutCounter.getCount());
    }
    // verify head operator metrics
    {
        // this only returns the existing group and doesn't create a new one
        final OperatorMetricGroup operatorMetricGroup1 = taskMetricGroup.getOrAddOperator(HEAD_OPERATOR_NAME);
        final OperatorIOMetricGroup ioMetricGroup = operatorMetricGroup1.getIOMetricGroup();
        final Counter numRecordsInCounter = ioMetricGroup.getNumRecordsInCounter();
        final Counter numRecordsOutCounter = ioMetricGroup.getNumRecordsOutCounter();
        Assert.assertEquals(numRecords, numRecordsInCounter.getCount());
        Assert.assertEquals(numRecords * 2, numRecordsOutCounter.getCount());
    }
    // verify chained operator metrics
    {
        // this only returns the existing group and doesn't create a new one
        final InternalOperatorMetricGroup operatorMetricGroup1 = taskMetricGroup.getOrAddOperator(CHAINED_OPERATOR_NAME);
        final InternalOperatorIOMetricGroup ioMetricGroup = operatorMetricGroup1.getIOMetricGroup();
        final Counter numRecordsInCounter = ioMetricGroup.getNumRecordsInCounter();
        final Counter numRecordsOutCounter = ioMetricGroup.getNumRecordsOutCounter();
        Assert.assertEquals(numRecords * 2, numRecordsInCounter.getCount());
        Assert.assertEquals(numRecords * 2 * 2, numRecordsOutCounter.getCount());
    }
}
Also used : MockEnvironmentBuilder(org.apache.flink.runtime.operators.testutils.MockEnvironmentBuilder) ExecutionAttemptID(org.apache.flink.runtime.executiongraph.ExecutionAttemptID) BatchTask(org.apache.flink.runtime.operators.BatchTask) TaskMetricGroup(org.apache.flink.runtime.metrics.groups.TaskMetricGroup) OperatorIOMetricGroup(org.apache.flink.metrics.groups.OperatorIOMetricGroup) InternalOperatorIOMetricGroup(org.apache.flink.runtime.metrics.groups.InternalOperatorIOMetricGroup) JobVertexID(org.apache.flink.runtime.jobgraph.JobVertexID) InternalOperatorIOMetricGroup(org.apache.flink.runtime.metrics.groups.InternalOperatorIOMetricGroup) Counter(org.apache.flink.metrics.Counter) InternalOperatorMetricGroup(org.apache.flink.runtime.metrics.groups.InternalOperatorMetricGroup) RichFlatMapFunction(org.apache.flink.api.common.functions.RichFlatMapFunction) FlatMapFunction(org.apache.flink.api.common.functions.FlatMapFunction) TaskIOMetricGroup(org.apache.flink.runtime.metrics.groups.TaskIOMetricGroup) Record(org.apache.flink.types.Record) UniformRecordGenerator(org.apache.flink.runtime.operators.testutils.UniformRecordGenerator) InternalOperatorMetricGroup(org.apache.flink.runtime.metrics.groups.InternalOperatorMetricGroup) OperatorMetricGroup(org.apache.flink.metrics.groups.OperatorMetricGroup) JobID(org.apache.flink.api.common.JobID) Test(org.junit.Test)

Example 8 with OperatorMetricGroup

use of org.apache.flink.metrics.groups.OperatorMetricGroup in project flink by apache.

the class SinkMetricsITCase method assertSinkMetrics.

private void assertSinkMetrics(JobID jobId, long processedRecordsPerSubtask, int parallelism, int numSplits) {
    List<OperatorMetricGroup> groups = reporter.findOperatorMetricGroups(jobId, "MetricTestSink");
    assertThat(groups, hasSize(parallelism));
    int subtaskWithMetrics = 0;
    for (OperatorMetricGroup group : groups) {
        Map<String, Metric> metrics = reporter.getMetricsByGroup(group);
        // there are only 2 splits assigned; so two groups will not update metrics
        if (group.getIOMetricGroup().getNumRecordsOutCounter().getCount() == 0) {
            continue;
        }
        subtaskWithMetrics++;
        // I/O metrics
        assertThat(group.getIOMetricGroup().getNumRecordsOutCounter(), isCounter(equalTo(processedRecordsPerSubtask)));
        assertThat(group.getIOMetricGroup().getNumBytesOutCounter(), isCounter(equalTo(processedRecordsPerSubtask * MetricWriter.RECORD_SIZE_IN_BYTES)));
        // MetricWriter is just incrementing errors every even record
        assertThat(metrics.get(MetricNames.NUM_RECORDS_OUT_ERRORS), isCounter(equalTo((processedRecordsPerSubtask + 1) / 2)));
        // check if the latest send time is fetched
        assertThat(metrics.get(MetricNames.CURRENT_SEND_TIME), isGauge(equalTo((processedRecordsPerSubtask - 1) * MetricWriter.BASE_SEND_TIME)));
    }
    assertThat(subtaskWithMetrics, equalTo(numSplits));
}
Also used : Metric(org.apache.flink.metrics.Metric) OperatorMetricGroup(org.apache.flink.metrics.groups.OperatorMetricGroup)

Aggregations

OperatorMetricGroup (org.apache.flink.metrics.groups.OperatorMetricGroup)8 Test (org.junit.Test)4 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)3 Counter (org.apache.flink.metrics.Counter)3 InternalOperatorMetricGroup (org.apache.flink.runtime.metrics.groups.InternalOperatorMetricGroup)3 Accumulator (org.apache.flink.api.common.accumulators.Accumulator)2 RuntimeContext (org.apache.flink.api.common.functions.RuntimeContext)2 Metric (org.apache.flink.metrics.Metric)2 OperatorID (org.apache.flink.runtime.jobgraph.OperatorID)2 TaskMetricGroup (org.apache.flink.runtime.metrics.groups.TaskMetricGroup)2 HashMap (java.util.HashMap)1 Properties (java.util.Properties)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 JobID (org.apache.flink.api.common.JobID)1 DistributedCache (org.apache.flink.api.common.cache.DistributedCache)1 AggregateFunction (org.apache.flink.api.common.functions.AggregateFunction)1 BroadcastVariableInitializer (org.apache.flink.api.common.functions.BroadcastVariableInitializer)1 FlatMapFunction (org.apache.flink.api.common.functions.FlatMapFunction)1 IterationRuntimeContext (org.apache.flink.api.common.functions.IterationRuntimeContext)1 ReduceFunction (org.apache.flink.api.common.functions.ReduceFunction)1