Skip to content

Commit

Permalink
[#3152] feat(core): using time sliding window to record timer metrics (
Browse files Browse the repository at this point in the history
…#3341)

### What changes were proposed in this pull request?

using time sliding window to record timer metrics

### Why are the changes needed?

Fix: #3152 

### Does this PR introduce _any_ user-facing change?
no

### How was this patch tested?
test in local env to check whether Pxx comes to 0 after a while
  • Loading branch information
FANNG1 authored May 27, 2024
1 parent e745864 commit 048f114
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ private ConfigConstants() {}
public static final String VERSION_0_4_0 = "0.4.0";
/** The version number for the 0.5.0 release. */
public static final String VERSION_0_5_0 = "0.5.0";
/** The version number for the 0.5.1 release. */
public static final String VERSION_0_5_1 = "0.5.1";
}
8 changes: 8 additions & 0 deletions core/src/main/java/com/datastrato/gravitino/Configs.java
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,12 @@ private Configs() {}
.version(ConfigConstants.VERSION_0_5_0)
.longConf()
.createWithDefault(60 * 60 * 1000L);

public static final int DEFAULT_METRICS_TIME_SLIDING_WINDOW_SECONDS = 60;
public static final ConfigEntry<Integer> METRICS_TIME_SLIDING_WINDOW_SECONDS =
new ConfigBuilder("gravitino.metrics.timeSlidingWindowSecs")
.doc("The seconds of Gravitino metrics time sliding window")
.version(ConfigConstants.VERSION_0_5_1)
.intConf()
.createWithDefault(DEFAULT_METRICS_TIME_SLIDING_WINDOW_SECONDS);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
import com.codahale.metrics.Gauge;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.SlidingTimeWindowArrayReservoir;
import com.codahale.metrics.Timer;
import com.datastrato.gravitino.Config;
import com.datastrato.gravitino.Configs;
import com.datastrato.gravitino.GravitinoEnv;
import java.util.concurrent.TimeUnit;

/**
* MetricsSource provides utilities to collect specified kind metrics, all metrics must create with
Expand All @@ -24,10 +29,19 @@ public abstract class MetricsSource {
public static final String JVM_METRIC_NAME = "jvm";
private final MetricRegistry metricRegistry;
private final String metricsSourceName;
private final int timeSlidingWindowSeconds;

protected MetricsSource(String name) {
this.metricsSourceName = name;
metricRegistry = new MetricRegistry();
Config config = GravitinoEnv.getInstance().config();
if (config != null) {
this.timeSlidingWindowSeconds =
config.get(Configs.METRICS_TIME_SLIDING_WINDOW_SECONDS).intValue();
} else {
// Couldn't get config when testing
this.timeSlidingWindowSeconds = Configs.DEFAULT_METRICS_TIME_SLIDING_WINDOW_SECONDS;
}
}

/**
Expand Down Expand Up @@ -75,7 +89,12 @@ public Counter getCounter(String name) {
* @return a new or pre-existing Histogram
*/
public Histogram getHistogram(String name) {
return this.metricRegistry.histogram(name);
return this.metricRegistry.histogram(
name,
() ->
new Histogram(
new SlidingTimeWindowArrayReservoir(
getTimeSlidingWindowSeconds(), TimeUnit.SECONDS)));
}

/**
Expand All @@ -85,6 +104,15 @@ public Histogram getHistogram(String name) {
* @return a new or pre-existing Timer
*/
public Timer getTimer(String name) {
return this.metricRegistry.timer(name);
return this.metricRegistry.timer(
name,
() ->
new Timer(
new SlidingTimeWindowArrayReservoir(
getTimeSlidingWindowSeconds(), TimeUnit.SECONDS)));
}

protected int getTimeSlidingWindowSeconds() {
return timeSlidingWindowSeconds;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@
import org.junit.jupiter.api.Test;

public class TestMetricsSystem {
MetricsSystem metricsSystem = new MetricsSystem();

private long getCounterValue(String metricsSourceName, String name) {
return metricsSystem.getMetricRegistry().counter(metricsSourceName + "." + name).getCount();
}
private MetricsSystem metricsSystem = new MetricsSystem();

@Test
void testRegisterMetricsSource() {
Expand Down Expand Up @@ -53,4 +49,8 @@ void testRegisterMetricsSource() {
.getCounters()
.containsKey(metricsSource.getMetricsSourceName() + "a.b"));
}

private long getCounterValue(String metricsSourceName, String name) {
return metricsSystem.getMetricRegistry().counter(metricsSourceName + "." + name).getCount();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public TestMetricsSource() {

@BeforeAll
void init() {
metricsSystem = new MetricsSystem();
this.metricsSystem = new MetricsSystem();
metricsSystem.register(this);
}

Expand Down
6 changes: 6 additions & 0 deletions docs/gravitino-server-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ For more details, please refer to the definition of the plugin.

Refer to [security](security.md) for HTTPS and authentication configurations.

### Metrics configuration

| Property name | Description | Default value | Required | Since Version |
|-------------------------------------------|------------------------------------------------------|---------------|----------|---------------|
| `gravitino.metrics.timeSlidingWindowSecs` | The seconds of Gravitino metrics time sliding window | 60 | No | 0.5.1 |

## Gravitino catalog properties configuration

There are three types of catalog properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,25 @@

package com.datastrato.gravitino.server.web;

import com.codahale.metrics.Clock;
import com.codahale.metrics.SlidingTimeWindowArrayReservoir;
import com.codahale.metrics.jersey2.InstrumentedResourceMethodApplicationListener;
import com.datastrato.gravitino.metrics.MetricNames;
import com.datastrato.gravitino.metrics.source.MetricsSource;
import java.util.concurrent.TimeUnit;
import org.glassfish.jersey.server.ResourceConfig;

public class HttpServerMetricsSource extends MetricsSource {
public HttpServerMetricsSource(String name, ResourceConfig resourceConfig, JettyServer server) {
super(name);
resourceConfig.register(new InstrumentedResourceMethodApplicationListener(getMetricRegistry()));
resourceConfig.register(
new InstrumentedResourceMethodApplicationListener(
getMetricRegistry(),
Clock.defaultClock(),
false,
() ->
new SlidingTimeWindowArrayReservoir(
getTimeSlidingWindowSeconds(), TimeUnit.SECONDS)));
registerGauge(
MetricNames.SERVER_IDLE_THREAD_NUM, () -> server.getThreadPool().getIdleThreads());
}
Expand Down

0 comments on commit 048f114

Please sign in to comment.