|
| 1 | +package io.debezium.connector.postgresql.metrics; |
| 2 | + |
| 3 | +import io.debezium.connector.base.ChangeEventQueueMetrics; |
| 4 | +import io.debezium.connector.common.CdcSourceTaskContext; |
| 5 | +import io.debezium.connector.postgresql.PostgresPartition; |
| 6 | +import io.debezium.data.Envelope; |
| 7 | +import io.debezium.metrics.Metrics; |
| 8 | +import io.debezium.pipeline.ConnectorEvent; |
| 9 | +import io.debezium.pipeline.metrics.ChangeEventSourceMetrics; |
| 10 | +import io.debezium.pipeline.spi.OffsetContext; |
| 11 | +import io.debezium.spi.schema.DataCollectionId; |
| 12 | +import io.debezium.util.Collect; |
| 13 | +import org.apache.kafka.connect.data.Struct; |
| 14 | + |
| 15 | +import java.util.Collection; |
| 16 | +import java.util.HashMap; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.function.Consumer; |
| 19 | +import java.util.function.Function; |
| 20 | + |
| 21 | +abstract class AbstractYugabyteDBTaskMetrics<B extends AbstractYugabyteDBPartitionMetrics> extends YugabyteDBMetrics |
| 22 | + implements ChangeEventSourceMetrics<PostgresPartition>, YugabyteDBTaskMetricsMXBean { |
| 23 | + |
| 24 | + private final ChangeEventQueueMetrics changeEventQueueMetrics; |
| 25 | + private final Map<PostgresPartition, B> beans = new HashMap<>(); |
| 26 | + |
| 27 | + AbstractYugabyteDBTaskMetrics(CdcSourceTaskContext taskContext, |
| 28 | + String contextName, |
| 29 | + ChangeEventQueueMetrics changeEventQueueMetrics, |
| 30 | + Collection<PostgresPartition> partitions, |
| 31 | + Function<PostgresPartition, B> beanFactory) { |
| 32 | + super(taskContext, Collect.linkMapOf( |
| 33 | + "server", taskContext.getConnectorName(), |
| 34 | + "task", taskContext.getTaskId(), |
| 35 | + "context", contextName)); |
| 36 | + this.changeEventQueueMetrics = changeEventQueueMetrics; |
| 37 | + |
| 38 | + for (PostgresPartition partition : partitions) { |
| 39 | + beans.put(partition, beanFactory.apply(partition)); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public synchronized void register() { |
| 45 | + super.register(); |
| 46 | + beans.values().forEach(YugabyteDBMetrics::register); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public synchronized void unregister() { |
| 51 | + beans.values().forEach(YugabyteDBMetrics::unregister); |
| 52 | + super.unregister(); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void reset() { |
| 57 | + beans.values().forEach(B::reset); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void onEvent(PostgresPartition partition, DataCollectionId source, OffsetContext offset, Object key, |
| 62 | + Struct value, Envelope.Operation operation) { |
| 63 | + onPartitionEvent(partition, bean -> bean.onEvent(source, offset, key, value, operation)); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void onFilteredEvent(PostgresPartition partition, String event) { |
| 68 | + onPartitionEvent(partition, bean -> bean.onFilteredEvent(event)); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void onFilteredEvent(PostgresPartition partition, String event, Envelope.Operation operation) { |
| 73 | + onPartitionEvent(partition, bean -> bean.onFilteredEvent(event, operation)); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void onErroneousEvent(PostgresPartition partition, String event) { |
| 78 | + onPartitionEvent(partition, bean -> bean.onErroneousEvent(event)); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void onErroneousEvent(PostgresPartition partition, String event, Envelope.Operation operation) { |
| 83 | + onPartitionEvent(partition, bean -> bean.onErroneousEvent(event, operation)); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void onConnectorEvent(PostgresPartition partition, ConnectorEvent event) { |
| 88 | + onPartitionEvent(partition, bean -> bean.onConnectorEvent(event)); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public int getQueueTotalCapacity() { |
| 93 | + return changeEventQueueMetrics.totalCapacity(); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public int getQueueRemainingCapacity() { |
| 98 | + return changeEventQueueMetrics.remainingCapacity(); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public long getMaxQueueSizeInBytes() { |
| 103 | + return changeEventQueueMetrics.maxQueueSizeInBytes(); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public long getCurrentQueueSizeInBytes() { |
| 108 | + return changeEventQueueMetrics.currentQueueSizeInBytes(); |
| 109 | + } |
| 110 | + |
| 111 | + protected void onPartitionEvent(PostgresPartition partition, Consumer<B> handler) { |
| 112 | + B bean = beans.get(partition); |
| 113 | + if (bean == null) { |
| 114 | + throw new IllegalArgumentException("MBean for partition " + partition + " are not registered"); |
| 115 | + } |
| 116 | + handler.accept(bean); |
| 117 | + } |
| 118 | +} |
0 commit comments