[Metrics] Java metric API (#9377)

This commit is contained in:
Lingxuan Zuo
2020-07-22 10:35:08 +08:00
committed by GitHub
parent a5f4659d9f
commit cd42450fc1
14 changed files with 668 additions and 7 deletions
+1
View File
@@ -1819,6 +1819,7 @@ cc_binary(
"//:global_state_accessor_lib",
"//:src/ray/ray_exported_symbols.lds",
"//:src/ray/ray_version_script.lds",
"//:stats_lib",
"@bazel_tools//tools/jdk:jni",
],
)
+2 -1
View File
@@ -40,7 +40,8 @@ generate_one io.ray.runtime.actor.NativeActorHandle
generate_one io.ray.runtime.object.NativeObjectStore
generate_one io.ray.runtime.task.NativeTaskExecutor
generate_one io.ray.runtime.gcs.GlobalStateAccessor
generate_one io.ray.runtime.metric.NativeMetric
# Remove empty files
rm -f io_ray_runtime_RayNativeRuntime_AsyncContext.h
rm -f io_ray_runtime_task_NativeTaskExecutor_NativeActorContext.h
rm -f io_ray_runtime_task_NativeTaskExecutor_NativeActorContext.h
@@ -135,8 +135,6 @@ public class GlobalStateAccessor {
private native boolean nativeConnect(long nativePtr);
private native void nativeDisconnect(long nativePtr);
private native List<byte[]> nativeGetAllJobInfo(long nativePtr);
private native List<byte[]> nativeGetAllNodeInfo(long nativePtr);
@@ -0,0 +1,47 @@
package io.ray.runtime.metric;
import com.google.common.base.Preconditions;
import java.util.Map;
import java.util.stream.Collectors;
public class Count extends Metric {
private double count;
public Count(String name, String description, String unit, Map<TagKey, String> tags) {
super(name, tags);
count = 0.0d;
metricNativePointer = NativeMetric.registerCountNative(name, description, unit,
tags.keySet().stream().map(TagKey::getTagKey).collect(Collectors.toList()));
Preconditions.checkState(metricNativePointer != 0, "Count native pointer must not be 0.");
}
@Override
public void update(double value) {
super.update(value);
count += value;
}
@Override
public void update(double value, Map<TagKey, String> tags) {
super.update(value, tags);
count += value;
}
@Override
public void reset() {
}
public double getCount() {
return count;
}
/**
* @param delta add delta for counter
*/
public void inc(double delta) {
update(delta);
}
}
@@ -0,0 +1,25 @@
package io.ray.runtime.metric;
import com.google.common.base.Preconditions;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Gauge metric for recording last value and mapping object from stats.
*/
public class Gauge extends Metric {
public Gauge(String name, String description, String unit, Map<TagKey, String> tags) {
super(name, tags);
metricNativePointer = NativeMetric.registerGaugeNative(name, description, unit,
tags.keySet().stream().map(TagKey::getTagKey).collect(Collectors.toList()));
Preconditions.checkState(metricNativePointer != 0, "Gauge native pointer must not be 0.");
}
@Override
public void reset() {
}
}
@@ -0,0 +1,58 @@
package io.ray.runtime.metric;
import com.google.common.base.Preconditions;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Histogram measurement is mapped to histogram object in stats.
* In order to reduce JNI calls overhead, a memory historical window is used
* for storing transient value and we assume its max size is 100.
*/
public class Histogram extends Metric {
private List<Double> histogramWindow;
public static final int HISTOGRAM_WINDOW_SIZE = 100;
public Histogram(String name, String description, String unit, List<Double> boundaries,
Map<TagKey, String> tags) {
super(name, tags);
metricNativePointer = NativeMetric.registerHistogramNative(name, description, unit,
boundaries.stream().mapToDouble(Double::doubleValue).toArray(),
tags.keySet().stream().map(TagKey::getTagKey).collect(Collectors.toList()));
Preconditions.checkState(metricNativePointer != 0,
"Histogram native pointer must not be 0.");
histogramWindow = new ArrayList<>();
}
private void updateForWindow(double value) {
if (histogramWindow.size() == HISTOGRAM_WINDOW_SIZE) {
histogramWindow.remove(0);
}
histogramWindow.add(value);
}
@Override
public void update(double value) {
super.update(value);
updateForWindow(value);
}
@Override
public void update(double value, Map<TagKey, String> tags) {
super.update(value, tags);
updateForWindow(value);
}
@Override
public void reset() {
}
public List<Double> getHistogramWindow() {
return histogramWindow;
}
}
@@ -0,0 +1,91 @@
package io.ray.runtime.metric;
import com.google.common.base.Preconditions;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Class metric is mapped to stats metric object in core worker.
* it must be in categories set [Gague, Count, Sum, Histogram].
*/
public abstract class Metric {
protected String name;
protected double value;
// Native pointer mapping to gauge object of stats.
protected long metricNativePointer = 0L;
protected Map<TagKey, String> tags;
public Metric(String name, Map<TagKey, String> tags) {
Preconditions.checkNotNull(tags, "Metric tags map must not be null.");
Preconditions.checkNotNull(name, "Metric name must not be null.");
this.name = name;
this.tags = tags;
this.value = 0.0d;
}
// Sync metric with core worker stats for registry.
// Metric data will be flushed into stats view data inside core worker immediately after
// record is called.
/**
* Flush records to stats in last aggregator.
*/
public void record() {
Preconditions.checkState(metricNativePointer != 0, "Metric native pointer must not be 0.");
// Get tag key list from map;
List<TagKey> nativeTagKeyList = new ArrayList<>();
List<String> tagValues = new ArrayList<>();
for (Map.Entry<TagKey, String> entry : tags.entrySet()) {
nativeTagKeyList.add(entry.getKey());
tagValues.add(entry.getValue());
}
// Get tag value list from map;
NativeMetric.recordNative(metricNativePointer, value, nativeTagKeyList.stream()
.map(TagKey::getTagKey).collect(Collectors.toList()), tagValues);
}
/** Update gauge value without tags.
* Update metric info for user.
* @param value lastest value for updating
*/
public void update(double value) {
this.value = value;
}
/** Update gauge value with dynamic tag values.
* @param value lastest value for updating
* @param tags tag map
*/
public void update(double value, Map<TagKey, String> tags) {
this.value = value;
this.tags = tags;
}
/**
* Deallocate object from stats and reset native pointer in null.
*/
public void unregister() {
if (0 != metricNativePointer) {
NativeMetric.unregisterMetricNative(metricNativePointer);
}
metricNativePointer = 0;
}
/**
* @return lastest updating value.
*/
public double getValue() {
return value;
}
/**
* It's abstract method for each metric measurements, so metric registry can store transient
* value and aggregate historical data for flushing.
*/
public abstract void reset();
}
@@ -0,0 +1,31 @@
package io.ray.runtime.metric;
import java.util.List;
/**
* Native metric provide a native interface to register tag or metric for current metric package.
*/
class NativeMetric {
public static native void registerTagkeyNative(String tagKey);
public static native long registerCountNative(String name, String description,
String unit, List<String> tagKeys);
public static native long registerGaugeNative(String name, String description,
String unit, List<String> tagKeys);
public static native long registerHistogramNative(String name, String description,
String unit, double[] boundaries,
List<String> tagKeys);
public static native long registerSumNative(String name, String description,
String unit, List<String> tagKeys);
public static native void recordNative(long metricNativePointer, double value,
List tagKeys, List<String> tagValues);
public static native void unregisterMetricNative(long gaugePtr);
}
@@ -0,0 +1,44 @@
package io.ray.runtime.metric;
import com.google.common.base.Preconditions;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Sum measurement is mapped to sum object in stats.
* Property sum is used for storing transient sum for registry aggregation.
*/
public class Sum extends Metric {
private double sum;
public Sum(String name, String description, String unit, Map<TagKey, String> tags) {
super(name, tags);
metricNativePointer = NativeMetric.registerSumNative(name, description, unit,
tags.keySet().stream().map(TagKey::getTagKey).collect(Collectors.toList()));
Preconditions.checkState(metricNativePointer != 0,"Count native pointer must not be 0.");
this.sum = 0.0d;
}
@Override
public void update(double value) {
super.update(value);
sum += value;
}
@Override
public void update(double value, Map<TagKey, String> tags) {
super.update(value, tags);
sum += value;
}
@Override
public void reset() {
}
public double getSum() {
return sum;
}
}
@@ -0,0 +1,44 @@
package io.ray.runtime.metric;
import java.util.Objects;
/**
* Tagkey is mapping java object to stats tagkey object.
*/
public class TagKey {
private String tagKey;
public TagKey(String key) {
tagKey = key;
NativeMetric.registerTagkeyNative(key);
}
public String getTagKey() {
return tagKey;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof TagKey)) {
return false;
}
TagKey tagKey1 = (TagKey) o;
return Objects.equals(tagKey, tagKey1.tagKey);
}
@Override
public int hashCode() {
return Objects.hash(tagKey);
}
@Override
public String toString() {
return "TagKey{" +
", tagKey='" + tagKey + '\'' +
'}';
}
}
@@ -0,0 +1,87 @@
package io.ray.test;
import io.ray.runtime.metric.Count;
import io.ray.runtime.metric.Gauge;
import io.ray.runtime.metric.Histogram;
import io.ray.runtime.metric.Sum;
import io.ray.runtime.metric.TagKey;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.testng.Assert;
import org.testng.annotations.Test;
public class MetricTest extends BaseTest {
boolean doubleEqual(double value, double other) {
return value <= other + 1e-5 && value >= other - 1e-5;
}
@Test
public void testAddGauge() {
TestUtils.skipTestUnderSingleProcess();
Map<TagKey, String> tags = new HashMap<>();
tags.put(new TagKey("tag1"), "value1");
Gauge gauge = new Gauge("metric1", "", "", tags);
gauge.update(2);
gauge.record();
Assert.assertTrue(doubleEqual(gauge.getValue(), 2.0));
gauge.unregister();
}
@Test
public void testAddCount() {
TestUtils.skipTestUnderSingleProcess();
Map<TagKey, String> tags = new HashMap<>();
tags.put(new TagKey("tag1"), "value1");
tags.put(new TagKey("count_tag"), "default");
Count count = new Count("metric_count", "counter", "1pc", tags);
count.inc(10.0);
count.inc(20.0);
count.record();
Assert.assertTrue(doubleEqual(count.getValue(), 20.0));
Assert.assertTrue(doubleEqual(count.getCount(), 30.0));
}
@Test
public void testAddSum() {
TestUtils.skipTestUnderSingleProcess();
Map<TagKey, String> tags = new HashMap<>();
tags.put(new TagKey("tag1"), "value1");
tags.put(new TagKey("sum_tag"), "default");
Sum sum = new Sum("metric_sum", "sum", "sum", tags);
sum.update(10.0);
sum.update(20.0);
sum.record();
Assert.assertTrue(doubleEqual(sum.getValue(), 20.0));
Assert.assertTrue(doubleEqual(sum.getSum(), 30.0));
}
@Test
public void testAddHistogram() {
TestUtils.skipTestUnderSingleProcess();
Map<TagKey, String> tags = new HashMap<>();
tags.put(new TagKey("tag1"), "value1");
tags.put(new TagKey("histogram_tag"), "default");
List<Double> boundaries = new ArrayList<>();
boundaries.add(10.0);
boundaries.add(15.0);
boundaries.add(12.0);
Histogram histogram = new Histogram("metric_histogram", "histogram", "1pc",
boundaries, tags);
for (int i = 1; i <= 200; ++i) {
histogram.update(i * 1.0d);
histogram.record();
}
List<Double> window = histogram.getHistogramWindow();
for (int i = 0; i < Histogram.HISTOGRAM_WINDOW_SIZE; ++i) {
Assert.assertTrue(doubleEqual(i + 101.0d, window.get(i)));
}
}
}
@@ -0,0 +1,145 @@
// Copyright 2017 The Ray Authors.
//
// Licensed 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.
#include "io_ray_runtime_metric_NativeMetric.h"
#include "jni_utils.h"
#include "ray/stats/metric.h"
#include <jni.h>
#include <algorithm>
#include "opencensus/tags/tag_key.h"
using TagKeyType = opencensus::tags::TagKey;
using TagsType = std::vector<std::pair<opencensus::tags::TagKey, std::string>>;
/// Convert jni metric related data to native type for stats.
/// \param[in] j_name metric name in jni string.
/// \param[in] j_description metric description in jni string.
/// \param[in] j_unit metric measurement unit in jni string.
/// \param[in] tag_key_list tag key list in java list.
/// \param[out] metric_name metric name in native string.
/// \param[out] description metric description in native string.
/// \param[out] unit metric measurement unit in native string.
/// \param[out] tag_keys metric tag key vector unit in native vector.
inline void MetricTransform(JNIEnv *env, jstring j_name, jstring j_description,
jstring j_unit, jobject tag_key_list,
std::string *metric_name, std::string *description,
std::string *unit, std::vector<TagKeyType> &tag_keys) {
*metric_name = JavaStringToNativeString(env, static_cast<jstring>(j_name));
*description = JavaStringToNativeString(env, static_cast<jstring>(j_description));
*unit = JavaStringToNativeString(env, static_cast<jstring>(j_unit));
std::vector<std::string> tag_key_str_list;
JavaStringListToNativeStringVector(env, tag_key_list, &tag_key_str_list);
// We just call TagKeyType::Register to get tag object since opencensus tags
// registry is thread-safe and registry can return a new tag or registered
// item when it already exists.
std::transform(tag_key_str_list.begin(), tag_key_str_list.end(),
std::back_inserter(tag_keys),
[](std::string tag_key) { return TagKeyType::Register(tag_key); });
}
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT void JNICALL Java_io_ray_runtime_metric_NativeMetric_registerTagkeyNative(
JNIEnv *env, jclass obj, jstring str) {
std::string tag_key_name = JavaStringToNativeString(env, static_cast<jstring>(str));
RAY_IGNORE_EXPR(TagKeyType::Register(tag_key_name));
}
JNIEXPORT jlong JNICALL Java_io_ray_runtime_metric_NativeMetric_registerGaugeNative(
JNIEnv *env, jclass obj, jstring j_name, jstring j_description, jstring j_unit,
jobject tag_key_list) {
std::string metric_name;
std::string description;
std::string unit;
std::vector<TagKeyType> tag_keys;
MetricTransform(env, j_name, j_description, j_unit, tag_key_list, &metric_name,
&description, &unit, tag_keys);
auto *gauge = new ray::stats::Gauge(metric_name, description, unit, tag_keys);
return reinterpret_cast<long>(gauge);
}
JNIEXPORT jlong JNICALL Java_io_ray_runtime_metric_NativeMetric_registerCountNative(
JNIEnv *env, jclass obj, jstring j_name, jstring j_description, jstring j_unit,
jobject tag_key_list) {
std::string metric_name;
std::string description;
std::string unit;
std::vector<TagKeyType> tag_keys;
MetricTransform(env, j_name, j_description, j_unit, tag_key_list, &metric_name,
&description, &unit, tag_keys);
auto *count = new ray::stats::Count(metric_name, description, unit, tag_keys);
return reinterpret_cast<long>(count);
}
JNIEXPORT jlong JNICALL Java_io_ray_runtime_metric_NativeMetric_registerSumNative(
JNIEnv *env, jclass obj, jstring j_name, jstring j_description, jstring j_unit,
jobject tag_key_list) {
std::string metric_name;
std::string description;
std::string unit;
std::vector<TagKeyType> tag_keys;
MetricTransform(env, j_name, j_description, j_unit, tag_key_list, &metric_name,
&description, &unit, tag_keys);
auto *sum = new ray::stats::Sum(metric_name, description, unit, tag_keys);
return reinterpret_cast<long>(sum);
}
JNIEXPORT jlong JNICALL Java_io_ray_runtime_metric_NativeMetric_registerHistogramNative(
JNIEnv *env, jclass obj, jstring j_name, jstring j_description, jstring j_unit,
jdoubleArray j_boundaries, jobject tag_key_list) {
std::string metric_name;
std::string description;
std::string unit;
std::vector<TagKeyType> tag_keys;
MetricTransform(env, j_name, j_description, j_unit, tag_key_list, &metric_name,
&description, &unit, tag_keys);
std::vector<double> boundaries;
JavaDoubleArrayToNativeDoubleVector(env, j_boundaries, &boundaries);
auto *histogram =
new ray::stats::Histogram(metric_name, description, unit, boundaries, tag_keys);
return reinterpret_cast<long>(histogram);
}
JNIEXPORT void JNICALL Java_io_ray_runtime_metric_NativeMetric_unregisterMetricNative(
JNIEnv *env, jclass obj, jlong metric_native_pointer) {
ray::stats::Metric *metric =
reinterpret_cast<ray::stats::Metric *>(metric_native_pointer);
delete metric;
}
JNIEXPORT void JNICALL Java_io_ray_runtime_metric_NativeMetric_recordNative(
JNIEnv *env, jclass obj, jlong metric_native_pointer, jdouble value,
jobject tag_key_list, jobject tag_value_list) {
ray::stats::Metric *metric =
reinterpret_cast<ray::stats::Metric *>(metric_native_pointer);
std::vector<std::string> tag_key_str_list;
std::vector<std::string> tag_value_str_list;
JavaStringListToNativeStringVector(env, tag_key_list, &tag_key_str_list);
JavaStringListToNativeStringVector(env, tag_value_list, &tag_value_str_list);
TagsType tags;
for (size_t i = 0; i < tag_key_str_list.size(); ++i) {
tags.push_back({TagKeyType::Register(tag_key_str_list[i]), tag_value_str_list[i]});
}
metric->Record(value, tags);
}
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,69 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class io_ray_runtime_metric_NativeMetric */
#ifndef _Included_io_ray_runtime_metric_NativeMetric
#define _Included_io_ray_runtime_metric_NativeMetric
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: io_ray_runtime_metric_NativeMetric
* Method: registerTagkeyNative
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL
Java_io_ray_runtime_metric_NativeMetric_registerTagkeyNative(JNIEnv *, jclass, jstring);
/*
* Class: io_ray_runtime_metric_NativeMetric
* Method: registerCountNative
* Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/util/List;)J
*/
JNIEXPORT jlong JNICALL Java_io_ray_runtime_metric_NativeMetric_registerCountNative(
JNIEnv *, jclass, jstring, jstring, jstring, jobject);
/*
* Class: io_ray_runtime_metric_NativeMetric
* Method: registerGaugeNative
* Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/util/List;)J
*/
JNIEXPORT jlong JNICALL Java_io_ray_runtime_metric_NativeMetric_registerGaugeNative(
JNIEnv *, jclass, jstring, jstring, jstring, jobject);
/*
* Class: io_ray_runtime_metric_NativeMetric
* Method: registerHistogramNative
* Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[DLjava/util/List;)J
*/
JNIEXPORT jlong JNICALL Java_io_ray_runtime_metric_NativeMetric_registerHistogramNative(
JNIEnv *, jclass, jstring, jstring, jstring, jdoubleArray, jobject);
/*
* Class: io_ray_runtime_metric_NativeMetric
* Method: registerSumNative
* Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/util/List;)J
*/
JNIEXPORT jlong JNICALL Java_io_ray_runtime_metric_NativeMetric_registerSumNative(
JNIEnv *, jclass, jstring, jstring, jstring, jobject);
/*
* Class: io_ray_runtime_metric_NativeMetric
* Method: recordNative
* Signature: (JDLjava/util/List;Ljava/util/List;)V
*/
JNIEXPORT void JNICALL Java_io_ray_runtime_metric_NativeMetric_recordNative(
JNIEnv *, jclass, jlong, jdouble, jobject, jobject);
/*
* Class: io_ray_runtime_metric_NativeMetric
* Method: unregisterMetricNative
* Signature: (J)V
*/
JNIEXPORT void JNICALL
Java_io_ray_runtime_metric_NativeMetric_unregisterMetricNative(JNIEnv *, jclass, jlong);
#ifdef __cplusplus
}
#endif
#endif
+24 -4
View File
@@ -15,12 +15,12 @@
#pragma once
#include <jni.h>
#include <algorithm>
#include "ray/common/buffer.h"
#include "ray/common/function_descriptor.h"
#include "ray/common/id.h"
#include "ray/common/ray_object.h"
#include "ray/common/status.h"
#include "ray/core_worker/core_worker.h"
/// Boolean class
@@ -282,6 +282,26 @@ inline void JavaStringListToNativeStringVector(JNIEnv *env, jobject java_list,
});
}
/// Convert a Java long array to C++ std::vector<long>.
inline void JavaLongArrayToNativeLongVector(JNIEnv *env, jlongArray long_array,
std::vector<long> *native_vector) {
jlong *long_array_ptr = env->GetLongArrayElements(long_array, nullptr);
jsize vec_size = env->GetArrayLength(long_array);
native_vector->insert(native_vector->begin(), long_array_ptr,
long_array_ptr + vec_size);
env->ReleaseLongArrayElements(long_array, long_array_ptr, 0);
}
/// Convert a Java double array to C++ std::vector<double>.
inline void JavaDoubleArrayToNativeDoubleVector(JNIEnv *env, jdoubleArray double_array,
std::vector<double> *native_vector) {
jdouble *double_array_ptr = env->GetDoubleArrayElements(double_array, nullptr);
jsize vec_size = env->GetArrayLength(double_array);
native_vector->insert(native_vector->begin(), double_array_ptr,
double_array_ptr + vec_size);
env->ReleaseDoubleArrayElements(double_array, double_array_ptr, 0);
}
/// Convert a C++ std::vector to a Java List.
template <typename NativeT>
inline jobject NativeVectorToJavaList(
@@ -438,6 +458,6 @@ inline std::string GetActorFullName(bool global, std::string name) {
return "";
}
return global ? name
: ::ray::CoreWorkerProcess::GetCoreWorker().GetCurrentJobId().Hex() + "-" +
name;
}
: ::ray::CoreWorkerProcess::GetCoreWorker().GetCurrentJobId().Hex() +
"-" + name;
}