@Override public ByteBuffer toBuffer(int headerPos) { ByteBuffer buffer = ByteBuffer.allocate(headerPos + 1 /* version */ + 1 /* raw metric type */ + Long.BYTES /* time */ + Integer.BYTES /* broker id */ + Double.BYTES /* value */); buffer.position(headerPos); buffer.put(METRIC_VERSION); buffer.put(rawMetricType().id()); buffer.putLong(time()); buffer.putInt(brokerId()); buffer.putDouble(value()); return buffer; }
private static SortedMap<Byte, Set<RawMetricType>> buildBrokerMetricTypesDiffByVersion() { SortedMap<Byte, Set<RawMetricType>> buildBrokerMetricTypesDiffByVersion = new TreeMap<>(); for (RawMetricType type : RawMetricType.values()) { if (type.metricScope() == BROKER) { buildBrokerMetricTypesDiffByVersion.computeIfAbsent(type.supportedVersionSince(), t -> new HashSet<>()).add(type); } } return buildBrokerMetricTypesDiffByVersion; }
private void setNullableBrokerMetrics() { if (_brokerSampleDeserializationVersion != -1) { Set<RawMetricType> nullableBrokerMetrics = new HashSet<>(); for (byte v = (byte) (_brokerSampleDeserializationVersion + 1); v <= BrokerMetricSample.LATEST_SUPPORTED_VERSION; v++) { Set<RawMetricType> nullableMetrics = new HashSet<>(RawMetricType.brokerMetricTypesDiffForVersion(v)); nullableBrokerMetrics.addAll(nullableMetrics); } nullableBrokerMetrics.forEach(nullableMetric -> _brokerMetrics.setRawMetricValue(nullableMetric, 0.0, 0L)); } }
private static List<RawMetricType> buildMetricTypeList(MetricScope metricScope) { List<RawMetricType> brokerMetricTypes = new ArrayList<>(); for (RawMetricType type : RawMetricType.values()) { if (type.metricScope() == metricScope) { brokerMetricTypes.add(type); } } return brokerMetricTypes; } }
public BrokerMetric(RawMetricType rawMetricType, long time, int brokerId, double value) { super(rawMetricType, time, brokerId, value); if (rawMetricType.metricScope() != RawMetricType.MetricScope.BROKER) { throw new IllegalArgumentException(String.format("Cannot construct a BrokerMetric for %s whose scope is %s", rawMetricType, rawMetricType.metricScope())); } }
for (RawMetricType rawMetricType : RawMetricType.brokerMetricTypesDiffForVersion(BrokerMetricSample.MIN_SUPPORTED_VERSION)) { switch (rawMetricType) { case ALL_TOPIC_BYTES_IN: for (RawMetricType rawMetricType : RawMetricType.topicMetricTypes()) { switch (rawMetricType) { case TOPIC_BYTES_IN:
static BrokerMetric fromBuffer(ByteBuffer buffer) throws UnknownVersionException { byte version = buffer.get(); if (version > METRIC_VERSION) { throw new UnknownVersionException("Cannot deserialize the topic metrics for version " + version + ". " + "Current version is " + METRIC_VERSION); } RawMetricType rawMetricType = RawMetricType.forId(buffer.get()); long time = buffer.getLong(); int brokerId = buffer.getInt(); double value = buffer.getDouble(); return new BrokerMetric(rawMetricType, time, brokerId, value); }
for (Map.Entry<Byte, Set<RawMetricType>> entry : RawMetricType.brokerMetricTypesDiffByVersion().entrySet()) { for (RawMetricType rawBrokerMetricType : entry.getValue()) {
MetricDef commonMetricDef = KafkaMetricDef.commonMetricDef(); int numLeaderPartitionsOnBroker = leaderDistributionStats.get(leaderId).get(tpDotNotHandled.topic()); for (RawMetricType rawTopicMetricType : RawMetricType.topicMetricTypes()) { MetricInfo metricInfo = commonMetricDef.metricInfo(KafkaMetricDef.forRawMetricType(rawTopicMetricType).name()); double metricValue = brokerLoad.topicMetrics(tpWithDotHandled.topic(), rawTopicMetricType);
private void checkMetricScope(RawMetricType rawMetricType, MetricScope expectedMetricScope) { if (rawMetricType.metricScope() != expectedMetricScope) { throw new IllegalArgumentException(String.format("Metric scope %s does not match the expected metric scope %s", rawMetricType.metricScope(), expectedMetricScope)); } }
private static List<RawMetricType> buildMetricTypeList(MetricScope metricScope) { List<RawMetricType> brokerMetricTypes = new ArrayList<>(); for (RawMetricType type : RawMetricType.values()) { if (type.metricScope() == metricScope) { brokerMetricTypes.add(type); } } return brokerMetricTypes; } }
static TopicMetric fromBuffer(ByteBuffer buffer) throws UnknownVersionException { byte version = buffer.get(); if (version > METRIC_VERSION) { throw new UnknownVersionException("Cannot deserialize the topic metrics for version " + version + ". " + "Current version is " + METRIC_VERSION); } RawMetricType rawMetricType = RawMetricType.forId(buffer.get()); long time = buffer.getLong(); int brokerId = buffer.getInt(); int topicLength = buffer.getInt(); String topic = new String(buffer.array(), buffer.arrayOffset() + buffer.position(), topicLength, StandardCharsets.UTF_8); buffer.position(buffer.position() + topicLength); double value = buffer.getDouble(); return new TopicMetric(rawMetricType, time, brokerId, topic, value); }
@Test public void testSerdeWithLatestSerdeVersion() throws UnknownVersionException { MetricDef brokerMetricDef = KafkaMetricDef.brokerMetricDef(); BrokerMetricSample sample = new BrokerMetricSample("host", 0, BrokerMetricSample.LATEST_SUPPORTED_VERSION); double value = 1.0; for (MetricInfo metricInfo : brokerMetricDef.all()) { sample.record(metricInfo, value); value += 1; } sample.close((long) value); byte[] bytes = sample.toBytes(); assertEquals(461, bytes.length); BrokerMetricSample deserializedSample = BrokerMetricSample.fromBytes(bytes); assertEquals("host", deserializedSample.entity().host()); assertEquals(0, deserializedSample.entity().brokerId()); assertEquals(BrokerMetricSample.LATEST_SUPPORTED_VERSION, deserializedSample.deserializationVersion()); // Disk usage is not one of the broker raw metric type so we add 1. int expectedNumRecords = 1; expectedNumRecords += RawMetricType.brokerMetricTypesDiffByVersion().entrySet().stream() .mapToInt(entry -> entry.getValue().size()).sum(); assertEquals(expectedNumRecords, deserializedSample.allMetricValues().size()); value = 1.0; for (MetricInfo metricInfo : brokerMetricDef.all()) { assertEquals(value, deserializedSample.metricValue(metricInfo.id()), 0.0); value += 1; } assertEquals(value, deserializedSample.sampleTime(), 0.0); }
private static SortedMap<Byte, Set<RawMetricType>> buildBrokerMetricTypesDiffByVersion() { SortedMap<Byte, Set<RawMetricType>> buildBrokerMetricTypesDiffByVersion = new TreeMap<>(); for (RawMetricType type : RawMetricType.values()) { if (type.metricScope() == BROKER) { buildBrokerMetricTypesDiffByVersion.computeIfAbsent(type.supportedVersionSince(), t -> new HashSet<>()).add(type); } } return buildBrokerMetricTypesDiffByVersion; }
public ByteBuffer toBuffer(int headerPos) { byte[] topic = _topic.getBytes(StandardCharsets.UTF_8); ByteBuffer buffer = ByteBuffer.allocate(headerPos + 1 /* version */ + 1 /* raw metric type */ + Long.BYTES /* time */ + Integer.BYTES /* broker id */ + Integer.BYTES /* topic length */ + topic.length /* topic */ + Double.BYTES /* value */); buffer.position(headerPos); buffer.put(METRIC_VERSION); buffer.put(rawMetricType().id()); buffer.putLong(time()); buffer.putInt(brokerId()); buffer.putInt(topic.length); buffer.put(topic); buffer.putDouble(value()); return buffer; }
public PartitionMetric(RawMetricType rawMetricType, long time, int brokerId, String topic, int partition, double value) { super(rawMetricType, time, brokerId, value); if (rawMetricType.metricScope() != RawMetricType.MetricScope.PARTITION) { throw new IllegalArgumentException(String.format("Cannot construct a PartitionMetric for %s whose scope is %s", rawMetricType, rawMetricType.metricScope())); } _topic = topic; _partition = partition; }
static PartitionMetric fromBuffer(ByteBuffer buffer) throws UnknownVersionException { byte version = buffer.get(); if (version > METRIC_VERSION) { throw new UnknownVersionException("Cannot deserialize the topic metrics for version " + version + ". " + "Current version is " + METRIC_VERSION); } RawMetricType rawMetricType = RawMetricType.forId(buffer.get()); long time = buffer.getLong(); int brokerId = buffer.getInt(); int topicLength = buffer.getInt(); String topic = new String(buffer.array(), buffer.arrayOffset() + buffer.position(), topicLength, StandardCharsets.UTF_8); buffer.position(buffer.position() + topicLength); int partition = buffer.getInt(); double value = buffer.getDouble(); return new PartitionMetric(rawMetricType, time, brokerId, topic, partition, value); }
for (byte v = BrokerMetricSample.MIN_SUPPORTED_VERSION; v <= BrokerMetricSample.LATEST_SUPPORTED_VERSION; v++) { Set<RawMetricType> missingBrokerMetrics = new HashSet<>(); for (RawMetricType rawBrokerMetricType : RawMetricType.brokerMetricTypesDiffForVersion(v)) { if (_brokerMetrics.metricValue(rawBrokerMetricType) == null) { if (allowMissingBrokerMetric(cluster, brokerId, rawBrokerMetricType)) {