From cca68cf4a7d6136346e9677472af187459089f47 Mon Sep 17 00:00:00 2001 From: Chia-Ping Tsai Date: Tue, 16 May 2023 01:20:53 +0800 Subject: [PATCH] [METRICS] rewrite ServerMetrics by java 17 record --- .../common/metrics/broker/ServerMetrics.java | 228 ++---------------- .../astraea/common/cost/NetworkCostTest.java | 10 +- .../cost/ServerMetricsMeterBuilder.java | 85 +++++++ 3 files changed, 105 insertions(+), 218 deletions(-) create mode 100644 common/src/test/java/org/astraea/common/cost/ServerMetricsMeterBuilder.java diff --git a/common/src/main/java/org/astraea/common/metrics/broker/ServerMetrics.java b/common/src/main/java/org/astraea/common/metrics/broker/ServerMetrics.java index 7823d01937..989c4d79bb 100644 --- a/common/src/main/java/org/astraea/common/metrics/broker/ServerMetrics.java +++ b/common/src/main/java/org/astraea/common/metrics/broker/ServerMetrics.java @@ -18,11 +18,8 @@ import java.util.Arrays; import java.util.Collection; -import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Objects; -import java.util.concurrent.TimeUnit; import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -54,7 +51,7 @@ public final class ServerMetrics { ReplicaManager.ALL.values().stream(), Socket.QUERIES.stream()) .flatMap(f -> f) - .collect(Collectors.toUnmodifiableList()); + .toList(); public static List appInfo(MBeanClient client) { return client.beans(APP_INFO_QUERY).stream() @@ -93,12 +90,10 @@ public String metricName() { } public HasBeanObject fetch(MBeanClient mBeanClient) { - switch (this) { - case CLUSTER_ID: - return new ClusterIdGauge(mBeanClient.bean(ALL.get(this))); - default: - return new Gauge(mBeanClient.bean(ALL.get(this))); - } + if (this == KafkaServer.CLUSTER_ID) + return new ClusterIdGauge(mBeanClient.bean(ALL.get(this))); + + return new Gauge(mBeanClient.bean(ALL.get(this))); } public static KafkaServer ofAlias(String alias) { @@ -115,12 +110,7 @@ public String toString() { return alias(); } - public static class Gauge implements HasGauge { - private final BeanObject beanObject; - - public Gauge(BeanObject beanObject) { - this.beanObject = beanObject; - } + public record Gauge(BeanObject beanObject) implements HasGauge { public String metricsName() { return beanObject().properties().get("name"); @@ -129,25 +119,9 @@ public String metricsName() { public KafkaServer type() { return ofAlias(metricsName()); } - - @Override - public BeanObject beanObject() { - return beanObject; - } } - public static class ClusterIdGauge implements HasGauge { - private final BeanObject beanObject; - - public ClusterIdGauge(BeanObject beanObject) { - this.beanObject = beanObject; - } - - @Override - public BeanObject beanObject() { - return beanObject; - } - } + public record ClusterIdGauge(BeanObject beanObject) implements HasGauge {} } public enum DelayedOperationPurgatory implements EnumInfo { @@ -200,12 +174,7 @@ public Gauge fetch(MBeanClient mBeanClient) { return new Gauge(mBeanClient.bean(ALL.get(this))); } - public static class Gauge implements HasGauge { - private final BeanObject beanObject; - - public Gauge(BeanObject beanObject) { - this.beanObject = beanObject; - } + public record Gauge(BeanObject beanObject) implements HasGauge { public String metricsName() { return beanObject().properties().get("delayedOperation"); @@ -214,11 +183,6 @@ public String metricsName() { public DelayedOperationPurgatory type() { return ofAlias(metricsName()); } - - @Override - public BeanObject beanObject() { - return beanObject; - } } } @@ -272,81 +236,7 @@ public List fetch(MBeanClient mBeanClient) { .collect(Collectors.toList()); } - public Builder builder() { - return new Builder(this); - } - - public static class Builder { - - private final ServerMetrics.Topic metric; - private String topic; - private long time; - private final Map attributes = new HashMap<>(); - - public Builder(Topic metric) { - this.metric = metric; - } - - public Builder topic(String topic) { - this.topic = topic; - return this; - } - - public Builder time(long time) { - this.time = time; - return this; - } - - public Builder meanRate(double value) { - this.attributes.put("MeanRate", value); - return this; - } - - public Builder oneMinuteRate(double value) { - this.attributes.put("OneMinuteRate", value); - return this; - } - - public Builder fiveMinuteRate(double value) { - this.attributes.put("FiveMinuteRate", value); - return this; - } - - public Builder fifteenMinuteRate(double value) { - this.attributes.put("FifteenMinuteRate", value); - return this; - } - - public Builder rateUnit(TimeUnit timeUnit) { - this.attributes.put("RateUnit", timeUnit); - return this; - } - - public Builder count(long count) { - this.attributes.put("Count", count); - return this; - } - - public Meter build() { - return new Meter( - new BeanObject( - ServerMetrics.DOMAIN_NAME, - Map.ofEntries( - Map.entry("type", "BrokerTopicMetrics"), - Map.entry("topic", topic), - Map.entry("name", metric.metricName())), - Map.copyOf(attributes))); - } - } - - public static class Meter implements HasMeter { - - private final BeanObject beanObject; - - public Meter(BeanObject beanObject) { - this.beanObject = Objects.requireNonNull(beanObject); - } - + public record Meter(BeanObject beanObject) implements HasMeter { public String metricsName() { return beanObject().properties().get("name"); } @@ -358,16 +248,6 @@ public String topic() { public Topic type() { return ofAlias(metricsName()); } - - @Override - public String toString() { - return beanObject().toString(); - } - - @Override - public BeanObject beanObject() { - return beanObject; - } } } @@ -467,20 +347,14 @@ public Collection of(Collection objects) { .filter(o -> o instanceof Meter) .filter(o -> metricName().equals(o.beanObject().properties().get("name"))) .map(o -> (Meter) o) - .collect(Collectors.toUnmodifiableList()); + .toList(); } public Meter fetch(MBeanClient mBeanClient) { return new Meter(mBeanClient.bean(ALL.get(this))); } - public static class Meter implements HasMeter { - - private final BeanObject beanObject; - - public Meter(BeanObject beanObject) { - this.beanObject = Objects.requireNonNull(beanObject); - } + public record Meter(BeanObject beanObject) implements HasMeter { public String metricsName() { return beanObject().properties().get("name"); @@ -489,16 +363,6 @@ public String metricsName() { public BrokerTopic type() { return ofAlias(metricsName()); } - - @Override - public String toString() { - return beanObject().toString(); - } - - @Override - public BeanObject beanObject() { - return beanObject; - } } } @@ -551,14 +415,7 @@ public String toString() { return alias(); } - public static class Gauge implements HasGauge { - - private final BeanObject beanObject; - - public Gauge(BeanObject beanObject) { - this.beanObject = Objects.requireNonNull(beanObject); - } - + public record Gauge(BeanObject beanObject) implements HasGauge { public String metricsName() { return beanObject().properties().get("name"); } @@ -566,16 +423,6 @@ public String metricsName() { public ReplicaManager type() { return ReplicaManager.ofAlias(metricsName()); } - - @Override - public String toString() { - return beanObject().toString(); - } - - @Override - public BeanObject beanObject() { - return beanObject; - } } } @@ -638,22 +485,11 @@ public static List client(MBeanClient mBeanClient) { return mBeanClient.beans(CLIENT_QUERY).stream().map(Client::new).collect(Collectors.toList()); } - public static class SocketMetric implements HasBeanObject { + public record SocketMetric(BeanObject beanObject) implements HasBeanObject { private static final String MEMORY_POOL_DEPLETED_TIME_TOTAL = "MemoryPoolDepletedTimeTotal"; private static final String MEMORY_POOL_AVG_DEPLETED_PERCENT = "MemoryPoolAvgDepletedPercent"; private static final String BROKER_CONNECTION_ACCEPT_RATE = "broker-connection-accept-rate"; - private final BeanObject beanObject; - - public SocketMetric(BeanObject beanObject) { - this.beanObject = Objects.requireNonNull(beanObject); - } - - @Override - public BeanObject beanObject() { - return beanObject; - } - public double memoryPoolDepletedTimeTotal() { return (double) beanObject().attributes().get(MEMORY_POOL_DEPLETED_TIME_TOTAL); } @@ -668,24 +504,13 @@ public double brokerConnectionAcceptRate() { } /** property : listener */ - public static class SocketListenerMetric implements HasBeanObject { + public record SocketListenerMetric(BeanObject beanObject) implements HasBeanObject { private static final String CONNECTION_ACCEPT_THROTTLE_TIME = "connection-accept-throttle-time"; private static final String CONNECTION_ACCEPT_RATE = "connection-accept-rate"; private static final String IP_CONNECTION_ACCEPT_THROTTLE_TIME = "ip-connection-accept-throttle-time"; - private final BeanObject beanObject; - - public SocketListenerMetric(BeanObject beanObject) { - this.beanObject = Objects.requireNonNull(beanObject); - } - - @Override - public BeanObject beanObject() { - return beanObject; - } - public String listener() { return beanObject().properties().get(PROP_LISTENER); } @@ -704,7 +529,7 @@ public double ipConnectionAcceptThrottleTime() { } /** property : listener and networkProcessor */ - public static class SocketNetworkProcessorMetric implements HasBeanObject { + public record SocketNetworkProcessorMetric(BeanObject beanObject) implements HasBeanObject { private static final String INCOMING_BYTE_TOTAL = "incoming-byte-total"; private static final String SELECT_TOTAL = "select-total"; private static final String SUCCESSFUL_AUTHENTICATION_RATE = "successful-authentication-rate"; @@ -750,17 +575,6 @@ public static class SocketNetworkProcessorMetric implements HasBeanObject { private static final String REQUEST_TOTAL = "request-total"; private static final String IO_WAITTIME_TOTAL = "io-waittime-total"; - private final BeanObject beanObject; - - public SocketNetworkProcessorMetric(BeanObject beanObject) { - this.beanObject = Objects.requireNonNull(beanObject); - } - - @Override - public BeanObject beanObject() { - return beanObject; - } - public String listener() { return beanObject().properties().get(PROP_LISTENER); } @@ -927,19 +741,9 @@ public double ioWaittimeTotal() { } /** property : listener and networkProcessor and clientSoftwareName */ - public static class Client implements HasBeanObject { + public record Client(BeanObject beanObject) implements HasBeanObject { private static final String CONNECTIONS = "connections"; - private final BeanObject beanObject; - - public Client(BeanObject beanObject) { - this.beanObject = Objects.requireNonNull(beanObject); - } - - @Override - public BeanObject beanObject() { - return beanObject; - } public String listener() { return beanObject().properties().get(PROP_LISTENER); diff --git a/common/src/test/java/org/astraea/common/cost/NetworkCostTest.java b/common/src/test/java/org/astraea/common/cost/NetworkCostTest.java index da9b785f53..7e7daaab04 100644 --- a/common/src/test/java/org/astraea/common/cost/NetworkCostTest.java +++ b/common/src/test/java/org/astraea/common/cost/NetworkCostTest.java @@ -633,8 +633,7 @@ public LargeTestCase(int brokers, int partitions, int seed) { .timeRange(LocalDateTime.now(), Duration.ZERO) .seriesByBrokerTopic( (time, broker, topic) -> - ServerMetrics.Topic.BYTES_IN_PER_SEC - .builder() + new ServerMetricsMeterBuilder(ServerMetrics.Topic.BYTES_IN_PER_SEC) .topic(topic) .time(time.toEpochSecond(ZoneOffset.UTC)) .oneMinuteRate( @@ -661,8 +660,7 @@ public LargeTestCase(int brokers, int partitions, int seed) { .build()) .seriesByBrokerTopic( (time, broker, topic) -> - ServerMetrics.Topic.BYTES_OUT_PER_SEC - .builder() + new ServerMetricsMeterBuilder(ServerMetrics.Topic.BYTES_OUT_PER_SEC) .topic(topic) .time(time.toEpochSecond(ZoneOffset.UTC)) .oneMinuteRate( @@ -701,8 +699,8 @@ public LargeTestCase(int brokers, int partitions, int seed) { IntStream.range(0, 10) .mapToObj( i -> - ServerMetrics.Topic.TOTAL_FETCH_REQUESTS_PER_SEC - .builder() + new ServerMetricsMeterBuilder( + ServerMetrics.Topic.TOTAL_FETCH_REQUESTS_PER_SEC) .topic("Noise_" + i) .time(time.toEpochSecond(ZoneOffset.UTC)) .build())) diff --git a/common/src/test/java/org/astraea/common/cost/ServerMetricsMeterBuilder.java b/common/src/test/java/org/astraea/common/cost/ServerMetricsMeterBuilder.java new file mode 100644 index 0000000000..b0945243e1 --- /dev/null +++ b/common/src/test/java/org/astraea/common/cost/ServerMetricsMeterBuilder.java @@ -0,0 +1,85 @@ +/* + * 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.HashMap; +import java.util.Map; +import java.util.concurrent.TimeUnit; +import org.astraea.common.metrics.BeanObject; +import org.astraea.common.metrics.broker.ServerMetrics; + +public class ServerMetricsMeterBuilder { + private final ServerMetrics.Topic metric; + private String topic; + private long time; + private final Map attributes = new HashMap<>(); + + public ServerMetricsMeterBuilder(ServerMetrics.Topic metric) { + this.metric = metric; + } + + public ServerMetricsMeterBuilder topic(String topic) { + this.topic = topic; + return this; + } + + public ServerMetricsMeterBuilder time(long time) { + this.time = time; + return this; + } + + public ServerMetricsMeterBuilder meanRate(double value) { + this.attributes.put("MeanRate", value); + return this; + } + + public ServerMetricsMeterBuilder oneMinuteRate(double value) { + this.attributes.put("OneMinuteRate", value); + return this; + } + + public ServerMetricsMeterBuilder fiveMinuteRate(double value) { + this.attributes.put("FiveMinuteRate", value); + return this; + } + + public ServerMetricsMeterBuilder fifteenMinuteRate(double value) { + this.attributes.put("FifteenMinuteRate", value); + return this; + } + + public ServerMetricsMeterBuilder rateUnit(TimeUnit timeUnit) { + this.attributes.put("RateUnit", timeUnit); + return this; + } + + public ServerMetricsMeterBuilder count(long count) { + this.attributes.put("Count", count); + return this; + } + + public ServerMetrics.Topic.Meter build() { + return new ServerMetrics.Topic.Meter( + new BeanObject( + ServerMetrics.DOMAIN_NAME, + Map.ofEntries( + Map.entry("type", "BrokerTopicMetrics"), + Map.entry("topic", topic), + Map.entry("name", metric.metricName())), + Map.copyOf(attributes))); + } +}