Skip to content
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

Isolate MAL CounterWindow cache by metric name. #11526

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/en/changes/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* Support output key parameters in the booting logs.
* Fix cannot query zipkin traces with `annotationQuery` parameter in the JDBC related storage.
* Fix `limit` doesn't work for `findEndpoint` API in ES storage.
* Isolate MAL CounterWindow cache by metric name.

#### UI

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static Analyzer build(final String metricName,
final String filterExpression,
final String expression,
final MeterSystem meterSystem) {
Expression e = DSL.parse(expression);
Expression e = DSL.parse(metricName, expression);
FilterExpression filter = null;
if (!Strings.isNullOrEmpty(filterExpression)) {
filter = new FilterExpression(filterExpression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private String formatMetricName(MetricRuleConfig rule, String meterRuleName) {
}

private void handleInitExp(String exp) {
Expression e = DSL.parse(exp);
Expression e = DSL.parse(null, exp);
final Result result = e.run(ImmutableMap.of());
if (!result.isSuccess() && result.isThrowable()) {
throw new ExpressionParsingException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ public final class DSL {
/**
* Parse string literal to Expression object, which can be reused.
*
* @param metricName the name of metric defined in mal rule
* @param expression string literal represents the DSL expression.
* @return Expression object could be executed.
*/
public static Expression parse(final String expression) {
public static Expression parse(final String metricName, final String expression) {
CompilerConfiguration cc = new CompilerConfiguration();
cc.setScriptBaseClass(DelegatingScript.class.getName());
ImportCustomizer icz = new ImportCustomizer();
Expand Down Expand Up @@ -82,6 +83,6 @@ public static Expression parse(final String expression) {

GroovyShell sh = new GroovyShell(new Binding(), cc);
DelegatingScript script = (DelegatingScript) sh.parse(expression);
return new Expression(expression, script);
return new Expression(metricName, expression, script);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@
public class Expression {
private static final ThreadLocal<Map<String, SampleFamily>> PROPERTY_REPOSITORY = new ThreadLocal<>();

private final String metricName;

private final String literal;

private final DelegatingScript expression;

public Expression(final String literal, final DelegatingScript expression) {
public Expression(final String metricName, final String literal, final DelegatingScript expression) {
this.metricName = metricName;
this.literal = literal;
this.expression = expression;
this.empower();
Expand Down Expand Up @@ -94,7 +97,7 @@ public Result run(final Map<String, SampleFamily> sampleFamilies) {
}

private void empower() {
expression.setDelegate(new ExpressionDelegate(literal));
expression.setDelegate(new ExpressionDelegate(metricName, literal));
extendNumber(Number.class);
}

Expand All @@ -115,23 +118,26 @@ private static class ExpressionDelegate extends GroovyObjectSupport {
public static final DownsamplingType LATEST = DownsamplingType.LATEST;
public static final DownsamplingType SUM_PER_MIN = DownsamplingType.SUM_PER_MIN;

private final String metricName;
private final String literal;

public SampleFamily propertyMissing(String metricName) {
public SampleFamily propertyMissing(String sampleName) {
ExpressionParsingContext.get().ifPresent(ctx -> {
if (!ctx.samples.contains(metricName)) {
ctx.samples.add(metricName);
if (!ctx.samples.contains(sampleName)) {
ctx.samples.add(sampleName);
}
});
Map<String, SampleFamily> sampleFamilies = PROPERTY_REPOSITORY.get();
if (sampleFamilies == null) {
return SampleFamily.EMPTY;
}
if (sampleFamilies.containsKey(metricName)) {
return sampleFamilies.get(metricName);
if (sampleFamilies.containsKey(sampleName)) {
SampleFamily sampleFamily = sampleFamilies.get(sampleName);
sampleFamily.context.setMetricName(this.metricName);
return sampleFamily;
}
if (!ExpressionParsingContext.get().isPresent()) {
log.warn("{} referred by \"{}\" doesn't exist in {}", metricName, literal, sampleFamilies.keySet());
if (ExpressionParsingContext.get().isEmpty()) {
log.warn("{} referred by \"{}\" doesn't exist in {}", sampleName, literal, sampleFamilies.keySet());
}
return SampleFamily.EMPTY;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ Sample newValue(Function<Double, Double> transform) {
return toBuilder().value(transform.apply(value)).build();
}

Sample increase(String range, Function2<Double, Long, Double> transform) {
Tuple2<Long, Double> i = CounterWindow.INSTANCE.increase(name, labels, value, Duration.parse(range).toMillis(), timestamp);
Sample increase(String range, String metricName, Function2<Double, Long, Double> transform) {
Tuple2<Long, Double> i = CounterWindow.INSTANCE.increase(metricName, labels, value, Duration.parse(range).toMillis(), timestamp);
double nv = transform.apply(i._2, i._1);
return newValue(ignored -> nv);
}

Sample increase(Function2<Double, Long, Double> transform) {
Tuple2<Long, Double> i = CounterWindow.INSTANCE.pop(name, labels, value, timestamp);
Sample increase(String metricName, Function2<Double, Long, Double> transform) {
Tuple2<Long, Double> i = CounterWindow.INSTANCE.pop(metricName, labels, value, timestamp);
double nv = transform.apply(i._2, i._1);
return newValue(ignored -> nv);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,11 @@ public SampleFamily increase(String range) {
return SampleFamily.build(
this.context,
Arrays.stream(samples)
.map(sample -> sample.increase(range, (lowerBoundValue, unused) -> sample.value - lowerBoundValue))
.map(sample -> sample.increase(
range,
context.metricName,
(lowerBoundValue, unused) -> sample.value - lowerBoundValue
))
.toArray(Sample[]::new)
);
}
Expand All @@ -291,6 +295,7 @@ public SampleFamily rate(String range) {
Arrays.stream(samples)
.map(sample -> sample.increase(
range,
context.metricName,
(lowerBoundValue, lowerBoundTime) -> {
final long timeDiff = (sample.timestamp - lowerBoundTime) / 1000;
return timeDiff < 1L ? 0.0 : (sample.value - lowerBoundValue) / timeDiff;
Expand All @@ -308,6 +313,7 @@ public SampleFamily irate() {
this.context,
Arrays.stream(samples)
.map(sample -> sample.increase(
context.metricName,
(lowerBoundValue, lowerBoundTime) -> {
final long timeDiff = (sample.timestamp - lowerBoundTime) / 1000;
return timeDiff < 1L ? 0.0 : (sample.value - lowerBoundValue) / timeDiff;
Expand Down Expand Up @@ -637,7 +643,7 @@ public SampleFamily downsampling(final DownsamplingType type) {
* The parsing context holds key results more than sample collection.
*/
@ToString
@EqualsAndHashCode
@EqualsAndHashCode(exclude = "metricName")
@Getter
@Setter
@Builder
Expand All @@ -651,6 +657,8 @@ static RunningContext instance() {
.build();
}

private String metricName;

@Builder.Default
private Map<MeterEntity, Sample[]> meterSamples = new HashMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public void test(String name,
String expression,
Result want,
boolean isThrow) {
Expression e = DSL.parse(expression);
Expression e = DSL.parse(name, expression);
Result r = null;
try {
r = e.run(input);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ public void test(String name,
String expression,
Result want,
boolean isThrow) {
Expression e = DSL.parse(expression);
Expression e = DSL.parse(name, expression);
Result r = null;
try {
r = e.run(input);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void test(String name,
String expression,
Result want,
boolean isThrow) {
Expression e = DSL.parse(expression);
Expression e = DSL.parse(name, expression);
Result r = null;
try {
r = e.run(input);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void test(String name,
String expression,
ExpressionParsingContext want,
boolean isThrow) {
Expression e = DSL.parse(expression);
Expression e = DSL.parse(name, expression);
ExpressionParsingContext r = null;
try {
r = e.parse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void test(String name,
ImmutableMap<String, SampleFamily> input,
String expression,
Result want) {
Expression e = DSL.parse(expression);
Expression e = DSL.parse(name, expression);
Result r = e.run(input);
assertThat(r).isEqualTo(want);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public void test(String name,
String expression,
Result want,
boolean isThrow) {
Expression e = DSL.parse(expression);
Expression e = DSL.parse(name, expression);
Result r = null;
try {
r = e.run(input);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public void test(String name,
String expression,
List<Result> want,
boolean isThrow) {
Expression e = DSL.parse(expression);
Expression e = DSL.parse(name, expression);
CounterWindow.INSTANCE.reset();
for (int i = 0; i < input.size(); i++) {
Result r = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public void test(String name,
String expression,
Result want,
boolean isThrow) {
Expression e = DSL.parse(expression);
Expression e = DSL.parse(name, expression);
Result r = null;
try {
r = e.run(input);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ public void test(final String name,
final String expression,
final boolean isThrow,
final Map<MeterEntity, Sample[]> want) {
Expression e = DSL.parse(expression);
Expression e = DSL.parse(name, expression);
Result r = null;
try {
r = e.run(input);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void test(String name,
String expression,
Result want,
boolean isThrow) {
Expression e = DSL.parse(expression);
Expression e = DSL.parse(name, expression);
Result r = null;
try {
r = e.run(input);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public void test(final String name,
final String expression,
final Result want,
final boolean isThrow) {
Expression e = DSL.parse(expression);
Expression e = DSL.parse(name, expression);
Result r = null;
try {
r = e.run(input);
Expand Down
Loading