-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add unit to RuntimeMetric #17737
Add unit to RuntimeMetric #17737
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ | |
public class RuntimeMetric | ||
{ | ||
private final String name; | ||
private final RuntimeUnit unit; | ||
private final AtomicLong sum = new AtomicLong(); | ||
private final AtomicLong count = new AtomicLong(); | ||
private final AtomicLong max = new AtomicLong(Long.MIN_VALUE); | ||
|
@@ -39,28 +40,31 @@ public class RuntimeMetric | |
* Creates a new empty RuntimeMetric. | ||
* | ||
* @param name Name of this metric. If used in the presto core code base, this should be a value defined in {@link RuntimeMetricName}. But connectors could use arbitrary names. | ||
* @param unit Unit of this metric. Available units are defined in {@link RuntimeUnit}. | ||
*/ | ||
public RuntimeMetric(String name) | ||
public RuntimeMetric(String name, RuntimeUnit unit) | ||
{ | ||
this.name = requireNonNull(name, "name is null"); | ||
this.unit = requireNonNull(unit, "unit is null"); | ||
} | ||
|
||
public static RuntimeMetric copyOf(RuntimeMetric metric) | ||
{ | ||
requireNonNull(metric, "metric is null"); | ||
return new RuntimeMetric(metric.getName(), metric.getSum(), metric.getCount(), metric.getMax(), metric.getMin()); | ||
return new RuntimeMetric(metric.getName(), metric.getUnit(), metric.getSum(), metric.getCount(), metric.getMax(), metric.getMin()); | ||
} | ||
|
||
@JsonCreator | ||
@ThriftConstructor | ||
public RuntimeMetric( | ||
@JsonProperty("name") String name, | ||
@JsonProperty("unit") RuntimeUnit unit, | ||
@JsonProperty("sum") long sum, | ||
@JsonProperty("count") long count, | ||
@JsonProperty("max") long max, | ||
@JsonProperty("min") long min) | ||
{ | ||
this(name); | ||
this(name, unit); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the older version would pass in null as the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will be checked inside There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, and it throws exception, so wouldn't the new version of presto that has this change be incompatible with older version? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got your point. I've tested it, it works fine using the old presto cli. |
||
set(sum, count, max, min); | ||
} | ||
|
||
|
@@ -75,6 +79,7 @@ private void set(long sum, long count, long max, long min) | |
public void set(RuntimeMetric metric) | ||
{ | ||
requireNonNull(metric, "metric is null"); | ||
checkState(unit == metric.getUnit(), "The metric must have the same unit type as the current one."); | ||
set(metric.getSum(), metric.getCount(), metric.getMax(), metric.getMin()); | ||
} | ||
|
||
|
@@ -104,6 +109,8 @@ public static RuntimeMetric merge(RuntimeMetric metric1, RuntimeMetric metric2) | |
if (metric2 == null) { | ||
return metric1; | ||
} | ||
checkState(metric1.getUnit() == metric2.getUnit(), "Two metrics to be merged must have the same unit type."); | ||
|
||
RuntimeMetric mergedMetric = copyOf(metric1); | ||
mergedMetric.mergeWith(metric2); | ||
return mergedMetric; | ||
|
@@ -117,6 +124,7 @@ public void mergeWith(RuntimeMetric metric) | |
if (metric == null) { | ||
return; | ||
} | ||
checkState(unit == metric.getUnit(), "The metric to be merged must have the same unit type as the current one."); | ||
sum.addAndGet(metric.getSum()); | ||
count.addAndGet(metric.getCount()); | ||
max.accumulateAndGet(metric.getMax(), Math::max); | ||
|
@@ -150,4 +158,18 @@ public long getMin() | |
{ | ||
return min.get(); | ||
} | ||
|
||
@JsonProperty | ||
@ThriftField(6) | ||
public RuntimeUnit getUnit() | ||
{ | ||
return unit; | ||
} | ||
|
||
private static void checkState(boolean condition, String message) | ||
{ | ||
if (!condition) { | ||
throw new IllegalStateException(message); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* 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. | ||
*/ | ||
package com.facebook.presto.common; | ||
|
||
import com.facebook.drift.annotations.ThriftEnum; | ||
import com.facebook.drift.annotations.ThriftEnumValue; | ||
|
||
@ThriftEnum | ||
public enum RuntimeUnit | ||
{ | ||
NONE(0), NANO(1), BYTE(2); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
You mean use
I would suggest keeping it:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good, can we
|
||
|
||
private final int value; | ||
|
||
RuntimeUnit(int value) | ||
{ | ||
this.value = value; | ||
} | ||
|
||
@ThriftEnumValue | ||
public int getValue() | ||
{ | ||
return value; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we still need the
name
here since it's duplicate information?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm kinda cautious to remove the existing field from the protocol. Some components might rely on it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like the Velox definition doesn't have it, so to make it more consistent, can we at least check what components are relying on it and would it be easy to remove?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm afraid this is another breaking change. The main reason we choose to keep the map structure instead of converting into array is that we cannot remove the existing information.