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); }
public static CruiseControlMetric fromBytes(byte[] bytes) throws UnknownVersionException { ByteBuffer buffer = ByteBuffer.wrap(bytes); switch (CruiseControlMetric.MetricClassId.forId(buffer.get())) { case BROKER_METRIC: return BrokerMetric.fromBuffer(buffer); case TOPIC_METRIC: return TopicMetric.fromBuffer(buffer); case PARTITION_METRIC: return PartitionMetric.fromBuffer(buffer); default: // This could happen when a new type of metric is added but we are still running the old code. // simply ignore the metric by returning a null. return null; } }
private void recordMetric(CruiseControlMetric ccm) { RawMetricType rawMetricType = ccm.rawMetricType(); switch (rawMetricType.metricScope()) { case BROKER: _brokerMetrics.recordCruiseControlMetric(ccm); break; case TOPIC: TopicMetric tm = (TopicMetric) ccm; _dotHandledTopicMetrics.computeIfAbsent(tm.topic(), t -> new RawMetricsHolder()) .recordCruiseControlMetric(ccm); break; case PARTITION: PartitionMetric pm = (PartitionMetric) ccm; _dotHandledPartitionMetrics.computeIfAbsent(new TopicPartition(pm.topic(), pm.partition()), tp -> new RawMetricsHolder()) .recordCruiseControlMetric(ccm); _dotHandledTopicsWithPartitionSizeReported.add(pm.topic()); break; default: throw new IllegalStateException(String.format("Should never be here. Unrecognized metric scope %s", rawMetricType.metricScope())); } }
@Test public void testPartitionMetricSerde() throws UnknownVersionException { PartitionMetric partitionMetric = new PartitionMetric(RawMetricType.PARTITION_SIZE, 123L, 0, TOPIC, PARTITION, 0.1); CruiseControlMetric deserialized = MetricSerde.fromBytes(MetricSerde.toBytes(partitionMetric)); assertEquals(CruiseControlMetric.MetricClassId.PARTITION_METRIC.id(), deserialized.metricClassId().id()); assertEquals(RawMetricType.PARTITION_SIZE.id(), deserialized.rawMetricType().id()); assertEquals(TIME, deserialized.time()); assertEquals(BROKER_ID, deserialized.brokerId()); assertEquals(TOPIC, ((PartitionMetric) deserialized).topic()); assertEquals(PARTITION, ((PartitionMetric) deserialized).partition()); assertEquals(VALUE, deserialized.value(), 0.000001); } }
@Test public void testMissingPartitionSizeMetric() throws UnknownVersionException { CruiseControlMetricsProcessor processor = new CruiseControlMetricsProcessor(); Set<CruiseControlMetric> metrics = getCruiseControlMetrics(); for (CruiseControlMetric metric : metrics) { boolean shouldAdd = true; if (metric.rawMetricType() == RawMetricType.PARTITION_SIZE) { PartitionMetric pm = (PartitionMetric) metric; if (pm.topic().equals(TOPIC1) && pm.partition() == P0) { shouldAdd = false; } } if (shouldAdd) { processor.addMetric(metric); } } MetricSampler.Samples samples = processor.process(getCluster(), Arrays.asList(T1P0, T1P1, T2P0, T2P1), MetricSampler.SamplingMode.ALL); assertEquals("Should have ignored partition " + T1P0, 3, samples.partitionMetricSamples().size()); assertEquals("Should have reported both brokers", 2, samples.brokerMetricSamples().size()); }
public ByteBuffer toBuffer(int headerPos) { byte[] topic = topic().getBytes(StandardCharsets.UTF_8); ByteBuffer buffer = ByteBuffer.allocate(headerPos + 1 /* version */ + 1 /* metric type */ + Long.BYTES /* time */ + Integer.BYTES /* broker id */ + Integer.BYTES /* topic length */ + topic.length /* topic */ + Integer.BYTES /* partition */ + 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.putInt(_partition); buffer.putDouble(value()); return buffer; }
metrics.add(new PartitionMetric(RawMetricType.PARTITION_SIZE, _time.milliseconds(), BROKER_ID_0, TOPIC1, P0, 100 * BYTES_IN_MB)); metrics.add(new PartitionMetric(RawMetricType.PARTITION_SIZE, _time.milliseconds(), BROKER_ID_0, TOPIC1, P1, 300 * BYTES_IN_MB)); metrics.add(new PartitionMetric(RawMetricType.PARTITION_SIZE, _time.milliseconds(), BROKER_ID_0, TOPIC2, P0, 200 * BYTES_IN_MB)); metrics.add(new PartitionMetric(RawMetricType.PARTITION_SIZE, _time.milliseconds(), BROKER_ID_0, TOPIC2, P1, 500 * BYTES_IN_MB)); metrics.add(new PartitionMetric(RawMetricType.PARTITION_SIZE, _time.milliseconds(), BROKER_ID_1, TOPIC1, P0, 100 * BYTES_IN_MB)); metrics.add(new PartitionMetric(RawMetricType.PARTITION_SIZE, _time.milliseconds(), BROKER_ID_1, TOPIC1, P1, 300 * BYTES_IN_MB)); metrics.add(new PartitionMetric(RawMetricType.PARTITION_SIZE, _time.milliseconds(), BROKER_ID_1, TOPIC2, P0, 200 * BYTES_IN_MB)); metrics.add(new PartitionMetric(RawMetricType.PARTITION_SIZE, _time.milliseconds(), BROKER_ID_1, TOPIC2, P1, 500 * BYTES_IN_MB)); return metrics;
public static CruiseControlMetric fromBytes(byte[] bytes) throws UnknownVersionException { ByteBuffer buffer = ByteBuffer.wrap(bytes); switch (CruiseControlMetric.MetricClassId.forId(buffer.get())) { case BROKER_METRIC: return BrokerMetric.fromBuffer(buffer); case TOPIC_METRIC: return TopicMetric.fromBuffer(buffer); case PARTITION_METRIC: return PartitionMetric.fromBuffer(buffer); default: // This could happen when a new type of metric is added but we are still running the old code. // simply ignore the metric by returning a null. return null; } }
return new PartitionMetric(RawMetricType.PARTITION_SIZE, now, brokerId, topic, partition, value); case LOG_FLUSH_RATE_AND_TIME_MS: if (attribute == null) {
public ByteBuffer toBuffer(int headerPos) { byte[] topic = topic().getBytes(StandardCharsets.UTF_8); ByteBuffer buffer = ByteBuffer.allocate(headerPos + 1 /* version */ + 1 /* metric type */ + Long.BYTES /* time */ + Integer.BYTES /* broker id */ + Integer.BYTES /* topic length */ + topic.length /* topic */ + Integer.BYTES /* partition */ + 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.putInt(_partition); buffer.putDouble(value()); return buffer; }
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); }
return new PartitionMetric(RawMetricType.PARTITION_SIZE, now, brokerId, topic, partition, value); case LOG_FLUSH_RATE_AND_TIME_MS: if (attribute == null) {