Skip to content

Commit

Permalink
[METRICS] rewrite LogMetrics by java 17 record (#1748)
Browse files Browse the repository at this point in the history
  • Loading branch information
chia7712 authored May 15, 2023
1 parent bae3ca3 commit 63235ae
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public final class LogMetrics {
public static final Collection<BeanQuery> QUERIES =
Stream.of(LogCleanerManager.ALL.values().stream(), Log.ALL.values().stream())
.flatMap(f -> f)
.collect(Collectors.toUnmodifiableList());
.toList();

public enum LogCleanerManager implements EnumInfo {
UNCLEANABLE_BYTES("uncleanable-bytes"),
Expand Down Expand Up @@ -80,17 +80,10 @@ public String toString() {
}

public List<Gauge> fetch(MBeanClient mBeanClient) {
return mBeanClient.beans(ALL.get(this)).stream()
.map(Gauge::new)
.collect(Collectors.toUnmodifiableList());
return mBeanClient.beans(ALL.get(this)).stream().map(Gauge::new).toList();
}

public static class Gauge implements HasGauge<Long> {
private final BeanObject beanObject;

public Gauge(BeanObject beanObject) {
this.beanObject = beanObject;
}
public record Gauge(BeanObject beanObject) implements HasGauge<Long> {

public String path() {
var path = beanObject().properties().get("logDirectory");
Expand All @@ -106,11 +99,6 @@ public String metricsName() {
public LogCleanerManager type() {
return ofAlias(metricsName());
}

@Override
public BeanObject beanObject() {
return beanObject;
}
}
}

Expand Down Expand Up @@ -171,70 +159,21 @@ public static Collection<Gauge> gauges(Collection<HasBeanObject> beans, Log type
.filter(m -> m instanceof Gauge)
.map(m -> (Gauge) m)
.filter(m -> m.type() == type)
.collect(Collectors.toUnmodifiableList());
.toList();
}

public List<Gauge> fetch(MBeanClient mBeanClient) {
return mBeanClient.beans(ALL.get(this)).stream()
.map(Gauge::new)
.collect(Collectors.toUnmodifiableList());
}

public Builder builder() {
return new Builder(this);
}

public static class Builder {
private final LogMetrics.Log metric;
private String topic;
private int partition;
private long value;

public Builder(Log metric) {
this.metric = metric;
}

public Builder topic(String topic) {
this.topic = topic;
return this;
}

public Builder partition(int partition) {
this.partition = partition;
return this;
}

public Builder logSize(long value) {
this.value = value;
return this;
}

public Gauge build() {
return new Gauge(
new BeanObject(
LogMetrics.DOMAIN_NAME,
Map.ofEntries(
Map.entry("type", LOG_TYPE),
Map.entry("name", metric.metricName()),
Map.entry("topic", topic),
Map.entry("partition", String.valueOf(partition))),
Map.of("Value", value)));
}
return mBeanClient.beans(ALL.get(this)).stream().map(Gauge::new).toList();
}

public static class Gauge implements HasGauge<Long> {
private final BeanObject beanObject;

public Gauge(BeanObject beanObject) {
this.beanObject = beanObject;
}
public record Gauge(BeanObject beanObject) implements HasGauge<Long> {

public String topic() {
return beanObject().properties().get("topic");
return topicIndex().get();
}

public int partition() {
return Integer.parseInt(beanObject().properties().get("partition"));
return partitionIndex().get().partition();
}

public String metricsName() {
Expand All @@ -244,11 +183,6 @@ public String metricsName() {
public Log type() {
return ofAlias(metricsName());
}

@Override
public BeanObject beanObject() {
return beanObject;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.astraea.common.cost;

import java.util.Map;
import org.astraea.common.metrics.BeanObject;
import org.astraea.common.metrics.broker.LogMetrics;

public class LogMetricsGaugeBuilder {
private final LogMetrics.Log metric;
private String topic;
private int partition;
private long value;

public LogMetricsGaugeBuilder(LogMetrics.Log metric) {
this.metric = metric;
}

public LogMetricsGaugeBuilder topic(String topic) {
this.topic = topic;
return this;
}

public LogMetricsGaugeBuilder partition(int partition) {
this.partition = partition;
return this;
}

public LogMetricsGaugeBuilder logSize(long value) {
this.value = value;
return this;
}

public LogMetrics.Log.Gauge build() {
return new LogMetrics.Log.Gauge(
new BeanObject(
LogMetrics.DOMAIN_NAME,
Map.ofEntries(
Map.entry("type", LogMetrics.LOG_TYPE),
Map.entry("name", metric.metricName()),
Map.entry("topic", topic),
Map.entry("partition", String.valueOf(partition))),
Map.of("Value", value)));
}
}
36 changes: 24 additions & 12 deletions common/src/test/java/org/astraea/common/cost/NetworkCostTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,19 +208,35 @@ void testReplicationAware() {
List.of(
MetricFactory.ofPartitionMetric("Rain", 0, 2),
MetricFactory.ofPartitionMetric("Drop", 0, 0),
LogMetrics.Log.SIZE.builder().topic("Rain").partition(0).logSize(1).build(),
LogMetrics.Log.SIZE.builder().topic("Drop").partition(0).logSize(1).build(),
new LogMetricsGaugeBuilder(LogMetrics.Log.SIZE)
.topic("Rain")
.partition(0)
.logSize(1)
.build(),
new LogMetricsGaugeBuilder(LogMetrics.Log.SIZE)
.topic("Drop")
.partition(0)
.logSize(1)
.build(),
bandwidth(ServerMetrics.Topic.BYTES_IN_PER_SEC, "Rain", 100),
bandwidth(ServerMetrics.Topic.BYTES_OUT_PER_SEC, "Rain", 300)),
2,
List.of(
MetricFactory.ofPartitionMetric("Rain", 0, 0),
LogMetrics.Log.SIZE.builder().topic("Rain").partition(0).logSize(1).build(),
new LogMetricsGaugeBuilder(LogMetrics.Log.SIZE)
.topic("Rain")
.partition(0)
.logSize(1)
.build(),
noise(5566)),
3,
List.of(
MetricFactory.ofPartitionMetric("Drop", 0, 2),
LogMetrics.Log.SIZE.builder().topic("Drop").partition(0).logSize(1).build(),
new LogMetricsGaugeBuilder(LogMetrics.Log.SIZE)
.topic("Drop")
.partition(0)
.logSize(1)
.build(),
bandwidth(ServerMetrics.Topic.BYTES_IN_PER_SEC, "Drop", 80),
bandwidth(ServerMetrics.Topic.BYTES_OUT_PER_SEC, "Drop", 800))));

Expand Down Expand Up @@ -274,8 +290,7 @@ void testZeroBandwidth() {
1,
List.of(
MetricFactory.ofPartitionMetric("Pipeline", 0, 2),
LogMetrics.Log.SIZE
.builder()
new LogMetricsGaugeBuilder(LogMetrics.Log.SIZE)
.topic("Pipeline")
.partition(0)
.logSize(0)
Expand All @@ -284,8 +299,7 @@ void testZeroBandwidth() {
2,
List.of(
MetricFactory.ofPartitionMetric("Pipeline", 0, 0),
LogMetrics.Log.SIZE
.builder()
new LogMetricsGaugeBuilder(LogMetrics.Log.SIZE)
.topic("Pipeline")
.partition(0)
.logSize(0)
Expand All @@ -294,8 +308,7 @@ void testZeroBandwidth() {
3,
List.of(
MetricFactory.ofPartitionMetric("Pipeline", 0, 0),
LogMetrics.Log.SIZE
.builder()
new LogMetricsGaugeBuilder(LogMetrics.Log.SIZE)
.topic("Pipeline")
.partition(0)
.logSize(0)
Expand Down Expand Up @@ -695,8 +708,7 @@ public LargeTestCase(int brokers, int partitions, int seed) {
.build()))
.seriesByBrokerReplica(
(time, broker, replica) ->
LogMetrics.Log.SIZE
.builder()
new LogMetricsGaugeBuilder(LogMetrics.Log.SIZE)
.topic(replica.topic())
.partition(replica.partition())
.logSize(replica.size())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.astraea.common.admin.Admin;
import org.astraea.common.admin.Replica;
import org.astraea.common.admin.TopicPartition;
import org.astraea.common.cost.LogMetricsGaugeBuilder;
import org.astraea.common.metrics.BeanObject;
import org.astraea.common.metrics.ClusterBean;
import org.astraea.common.metrics.HasBeanObject;
Expand Down Expand Up @@ -132,15 +133,13 @@ void testFollower() {
1,
List.of(
MetricFactory.ofPartitionMetric("TwoReplica", 0, 2),
LogMetrics.Log.SIZE
.builder()
new LogMetricsGaugeBuilder(LogMetrics.Log.SIZE)
.topic("TwoReplica")
.partition(0)
.logSize(200)
.build(),
MetricFactory.ofPartitionMetric("OneReplica", 0, 1),
LogMetrics.Log.SIZE
.builder()
new LogMetricsGaugeBuilder(LogMetrics.Log.SIZE)
.topic("OneReplica")
.partition(0)
.logSize(100)
Expand All @@ -149,8 +148,7 @@ void testFollower() {
2,
List.of(
MetricFactory.ofPartitionMetric("TwoReplica", 0, 0),
LogMetrics.Log.SIZE
.builder()
new LogMetricsGaugeBuilder(LogMetrics.Log.SIZE)
.topic("TwoReplica")
.partition(0)
.logSize(150)
Expand All @@ -177,8 +175,7 @@ void testVariousReplicaFactor() {
Stream<HasBeanObject> partition(int partition, int replica) {
return Stream.of(
MetricFactory.ofPartitionMetric("topic", partition, replica),
LogMetrics.Log.SIZE
.builder()
new LogMetricsGaugeBuilder(LogMetrics.Log.SIZE)
.topic("topic")
.partition(partition)
.logSize(0)
Expand Down

0 comments on commit 63235ae

Please sign in to comment.